From 9f4b0b62ebd235ac1fa862f2d126ea1bee28a0b5 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 11:50:23 -0800 Subject: [PATCH 1/3] sftp: take the buffer_read size as a word32 wolfSSH_SFTP_buffer_read() now takes its size as a word32, the type of the buffer size its callers pass, and rejects a size over 0x7FFFFFFF before it is returned as an int. wolfSSH_SFTP_DoName() also rejects a read offset past the size, so its size minus idx cannot wrap. Issue: CID-661741, CID-661744, CID-661745, CID-661747, CID-661748, CID-661749, CID-661750, CID-661751 --- src/wolfsftp.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index f51f70d4e..062dce2c3 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -751,7 +751,7 @@ int wolfSSH_TestSftpSendPacketType(WOLFSSH* ssh, byte type, byte* buf, /* returns the amount read on success */ static int wolfSSH_SFTP_buffer_read(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer, - int readSz) + word32 readSz) { int ret; int polled; @@ -766,6 +766,11 @@ static int wolfSSH_SFTP_buffer_read(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer, return 0; } + /* The size is returned as an int. */ + if (readSz > (word32)0x7FFFFFFF || buffer->sz > (word32)0x7FFFFFFF) { + return WS_BUFFER_E; + } + if (buffer->data == NULL) { buffer->idx = 0; buffer->sz = readSz; @@ -842,7 +847,7 @@ static int wolfSSH_SFTP_buffer_read(WOLFSSH* ssh, WS_SFTP_BUFFER* buffer, buffer->sz); } while (buffer->idx < buffer->sz); - return buffer->sz; + return (int)buffer->sz; } @@ -1424,7 +1429,7 @@ static int SFTP_ServerRecvInit(WOLFSSH* ssh) { /* silently ignore extensions if not supported */ if (state->extSz > 0) { ret = wolfSSH_SFTP_buffer_read(ssh, - &state->buffer, (int)state->extSz); + &state->buffer, state->extSz); if (ret < 0) { return WS_FATAL_ERROR; } @@ -6921,7 +6926,7 @@ static int SFTP_ClientRecvInit(WOLFSSH* ssh) { /* silently ignore extensions if not supported */ if (state->extSz > 0) { ret = wolfSSH_SFTP_buffer_read(ssh, - &state->buffer, (int)state->extSz); + &state->buffer, state->extSz); if (ret < 0) { return WS_FATAL_ERROR; } @@ -7620,6 +7625,12 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) case SFTP_NAME_GET_PACKET: /* get number of files */ /* using idx as an offset for partial reads */ + if (wolfSSH_SFTP_buffer_idx(&state->buffer) > + wolfSSH_SFTP_buffer_size(&state->buffer)) { + ssh->error = WS_BUFFER_E; + wolfSSH_SFTP_ClearState(ssh, STATE_ID_NAME); + return NULL; + } ret = wolfSSH_SFTP_buffer_read(ssh, &state->buffer, wolfSSH_SFTP_buffer_size(&state->buffer) - wolfSSH_SFTP_buffer_idx(&state->buffer)); From 8ad93bbc84bdca8fbe9d41f733aa8d8eb8117bce Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 11:50:23 -0800 Subject: [PATCH 2/3] wolfsshd: do GetConfigInt arithmetic in int GetConfigInt() checks each step against 0x7FFFFFFF, so the value, the time multiplier and the running total are now int, the same width as the limit. The return type stays long for the callers. Issue: CID-661742 --- apps/wolfsshd/configuration.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/apps/wolfsshd/configuration.c b/apps/wolfsshd/configuration.c index 87ae6ef0e..28dee7250 100644 --- a/apps/wolfsshd/configuration.c +++ b/apps/wolfsshd/configuration.c @@ -135,9 +135,8 @@ static int ConfigLoad(WOLFSSHD_CONFIG* conf, const char* filename, int depth); static int CountWhitespace(const char* in, int inSz, byte inv); static int SetFileString(char** dst, const char* src, void* heap); -/* Largest value GetConfigInt returns, before and after a time multiplier. It - * fits in a long on every target. */ -#define CONFIG_INT_MAX 0x7FFFFFFFL +/* Largest value GetConfigInt returns, before and after a time multiplier. */ +#define CONFIG_INT_MAX 0x7FFFFFFF /* Convert a decimal string into a number. With isTime, the string is the * sshd_config(5) time format: one or more groups of a number and a unit @@ -148,7 +147,7 @@ static int SetFileString(char** dst, const char* src, void* heap); * Returns the value on success and negative value on failure */ static long GetConfigInt(const char* in, int inSz, int isTime, void* heap) { - long total = 0; + int total = 0; int i = 0; (void)heap; @@ -158,8 +157,8 @@ static long GetConfigInt(const char* in, int inSz, int isTime, void* heap) } do { - long val = 0; - long mult = 1; /* multiplier */ + int val = 0; + int mult = 1; /* multiplier */ int start = i; for (; i < inSz && in[i] >= '0' && in[i] <= '9'; i++) { From bd1ab0d4b7a0fbce3954c2cb5db7e15da8fa504b Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 13:41:37 -0800 Subject: [PATCH 3/3] wolfssh: skip terminal modes when stdin has none modes_store() records whether tcgetattr() read the stdin modes. modes_clear() and modes_reset() do nothing when it did not, and a failed raw-mode tcsetattr() leaves nothing for modes_reset() to restore. Issue: CID-661743, CID-661746 --- apps/wolfssh/wolfssh.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/apps/wolfssh/wolfssh.c b/apps/wolfssh/wolfssh.c index 8e5689d15..f5137e997 100644 --- a/apps/wolfssh/wolfssh.c +++ b/apps/wolfssh/wolfssh.c @@ -201,16 +201,22 @@ static int NonBlockSSH_connect(WOLFSSH* ssh) #if defined(HAVE_TERMIOS_H) && defined(WOLFSSH_TERM) WOLFSSH_TERMIOS oldTerm; +/* Set while oldTerm holds the stdin modes and stdin may be in raw mode. */ +static int termSaved = 0; static void modes_store(void) { - tcgetattr(STDIN_FILENO, &oldTerm); + termSaved = (tcgetattr(STDIN_FILENO, &oldTerm) == 0); } static void modes_clear(void) { WOLFSSH_TERMIOS term = oldTerm; + if (!termSaved) { + return; + } + term.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOE | ECHOK | ECHONL | NOFLSH | TOSTOP); @@ -245,12 +251,16 @@ static void modes_clear(void) #ifdef CRTSCTS term.c_cflag &= ~(CRTSCTS); #endif - tcsetattr(STDIN_FILENO, TCSANOW, &term); + if (tcsetattr(STDIN_FILENO, TCSANOW, &term) != 0) { + termSaved = 0; + } } static void modes_reset(void) { - tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldTerm); + if (termSaved) { + (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldTerm); + } } #define MODES_STORE() modes_store()