From 1a64c577f6e33f2b519cca1db78e3fbb137fa54f Mon Sep 17 00:00:00 2001 From: Eshan Kelkar Date: Fri, 17 Jan 2025 21:59:21 +0530 Subject: [PATCH] sftp.c: Validate that the SSH session is in nonblocking mode The sftp API functions cannot interoperate properly with a nonblocking ssh session. Therefore code has been added in sftp_new() due to which the function will return failure if the caller passes a non blocking session without even trying to connect. Signed-off-by: Eshan Kelkar Reviewed-by: Jakub Jelen --- src/sftp.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sftp.c b/src/sftp.c index 2428c328..0c7e5777 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -102,12 +102,21 @@ static void sftp_ext_free(sftp_ext ext) sftp_session sftp_new(ssh_session session) { - sftp_session sftp; + sftp_session sftp = NULL; + int rc; if (session == NULL) { return NULL; } + if (!ssh_is_blocking(session)) { + ssh_set_error(session, + SSH_FATAL, + "The SSH session needs to be set to blocking mode for " + "SFTP to work correctly."); + return NULL; + } + sftp = calloc(1, sizeof(struct sftp_session_struct)); if (sftp == NULL) { ssh_set_error_oom(session); @@ -140,11 +149,18 @@ sftp_session sftp_new(ssh_session session) goto error; } - if (ssh_channel_open_session(sftp->channel)) { + /* + * The following two calls shouldn't return SSH_AGAIN + * as the code has validated above that the SSH session + * is in blocking mode. + */ + rc = ssh_channel_open_session(sftp->channel); + if (rc != SSH_OK) { goto error; } - if (ssh_channel_request_sftp(sftp->channel)) { + rc = ssh_channel_request_sftp(sftp->channel); + if (rc != SSH_OK) { goto error; }