From 289bb5a5f1fc562f16e4acccf5eb25d5db4dab7d Mon Sep 17 00:00:00 2001 From: Ruby Martin Date: Wed, 5 Aug 2026 17:18:09 -0600 Subject: [PATCH 1/3] Define WFSEEK_SUCCESS() for instances where WFSEEK() returns non-zero success case --- wolfssh/port.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wolfssh/port.h b/wolfssh/port.h index 3be71494f..1f5680ae0 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -124,6 +124,7 @@ extern "C" { #define WREWIND(fs,s) NU_Seek(*(s), 0, PSEEK_SET) #define WSEEK_END PSEEK_END #define WBADFILE NULL + #define WFSEEK_SUCCESS(r) ((int)(r) >= 0) #define WS_DELIM '\\' #define WOLFSSH_O_RDWR PO_RDWR @@ -440,6 +441,7 @@ extern "C" { #define WSETTIME(fs,f,a,m) (0) #define WFSETTIME(fs,fd,a,m) (0) #define WCHDIR(fs,b) SYS_FS_DirectryChange((b)) + #define WFSEEK_SUCCESS(r) ((int)(r) >= 0) #else #include @@ -1624,6 +1626,13 @@ extern "C" { #define WOLFSSH_O_NOFOLLOW 0 #endif +/* Catch-all so callers can test a seek result unconditionally. Ports whose + * seek returns a file position rather than a status must define this as + * ((int)(r) >= 0); see the Nucleus and MPLAB Harmony blocks above. */ +#ifndef WFSEEK_SUCCESS + #define WFSEEK_SUCCESS(r) ((r) == 0) +#endif + /* wIsSymlink lives in the always-compiled port.c, but its filesystem * dependencies (WSTAT_T/WLSTAT/S_ISLNK on POSIX, WS_GetFileAttributesExA on * Windows) and its only callers exist solely in the SFTP and SCP code, so From badc5f7004192f6354c97294014f11b7d0b5e961 Mon Sep 17 00:00:00 2001 From: Ruby Martin Date: Thu, 6 Aug 2026 11:31:27 -0600 Subject: [PATCH 2/3] Update WFSEEK call sites that may misinterpret return value in Harmony/Nucleus builds --- apps/wolfssh/common.c | 3 ++- examples/client/common.c | 3 ++- src/port.c | 8 ++++---- src/ssh.c | 3 ++- src/wolfsftp.c | 3 ++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/apps/wolfssh/common.c b/apps/wolfssh/common.c index 2d6e7693d..bc3354556 100644 --- a/apps/wolfssh/common.c +++ b/apps/wolfssh/common.c @@ -89,7 +89,8 @@ static int load_der_file(const char* filename, byte** out, word32* outSz) if (ret != 0 || file == WBADFILE) return -1; - if (WFSEEK(NULL, file, 0, WSEEK_END) != 0) { + ret = WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(ret)) { WFCLOSE(NULL, file); return -1; } diff --git a/examples/client/common.c b/examples/client/common.c index b9df3416e..6bc80ef54 100644 --- a/examples/client/common.c +++ b/examples/client/common.c @@ -274,7 +274,8 @@ static int load_der_file(const char* filename, byte** out, word32* outSz, if (ret != 0 || file == WBADFILE) return -1; - if (WFSEEK(NULL, file, 0, WSEEK_END) != 0) { + ret = WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(ret)) { WFCLOSE(NULL, file); return -1; } diff --git a/src/port.c b/src/port.c index 8c1255a64..d27240aa8 100644 --- a/src/port.c +++ b/src/port.c @@ -131,8 +131,8 @@ int wfopen(WFILE** f, const char* filename, const char* mode) { int ret; - ret = (int)WFSEEK(NULL, &fd, shortOffset[0], SYS_FS_SEEK_SET); - if (ret != -1) { + ret = WFSEEK(NULL, &fd, shortOffset[0], SYS_FS_SEEK_SET); + if (WFSEEK_SUCCESS(ret)) { ret = (int)WFWRITE(NULL, buf, 1, sz, &fd); } @@ -144,8 +144,8 @@ int wfopen(WFILE** f, const char* filename, const char* mode) { int ret; - ret = (int)WFSEEK(NULL, &fd, shortOffset[0], SYS_FS_SEEK_SET); - if (ret != -1) + ret = WFSEEK(NULL, &fd, shortOffset[0], SYS_FS_SEEK_SET); + if (WFSEEK_SUCCESS(ret)) ret = (int)WFREAD(NULL, buf, 1, sz, &fd); return ret; diff --git a/src/ssh.c b/src/ssh.c index 1ca8771b6..74e0b7a05 100644 --- a/src/ssh.c +++ b/src/ssh.c @@ -2410,7 +2410,8 @@ int wolfSSH_ReadKey_file(const char* name, if (ret != 0 || file == WBADFILE) return WS_BAD_FILE_E; #endif - if (WFSEEK(NULL, file, 0, WSEEK_END) != 0) { + ret = WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(ret)) { WFCLOSE(NULL, file); return WS_BAD_FILE_E; } diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 1ec9d6e56..2b3f46f89 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -10053,7 +10053,8 @@ int wolfSSH_SFTP_Put(WOLFSSH* ssh, char* from, char* to, byte resume, #if SIZEOF_OFF_T == 8 offset = (((word64)state->pOfst[1]) << 32) | offset; #endif - if (WFSEEK(ssh->fs, state->fl, offset, 0) != 0) { + ret = WFSEEK(ssh->fs, state->fl, offset, 0); + if (!WFSEEK_SUCCESS(ret)) { WLOG(WS_LOG_SFTP, "Unable to seek input file"); ssh->error = WS_BAD_FILE_E; ret = WS_FATAL_ERROR; From 31509d3e8979d959fd0b968350261a8f97114d3e Mon Sep 17 00:00:00 2001 From: Ruby Martin Date: Thu, 6 Aug 2026 11:32:11 -0600 Subject: [PATCH 3/3] Add WFSEEK return value checks where previously discarded --- apps/wolfsshd/wolfsshd.c | 6 ++++- examples/client/common.c | 5 +++- examples/echoserver/echoserver.c | 12 ++++++++-- examples/tpmcertserver/tpmcertclient.c | 24 +++++++++++-------- .../wolfssh_echoserver/main/echoserver.c | 11 +++++++-- src/wolfscp.c | 10 ++++---- 6 files changed, 48 insertions(+), 20 deletions(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index 19b757240..e87d88da2 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -291,7 +291,11 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap, return NULL; } } - WFSEEK(NULL, file, 0, WSEEK_END); + + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + WFCLOSE(NULL, file); + return NULL; + } fileSz = WFTELL(NULL, file); if (fileSz < 0) { WFCLOSE(NULL, file); diff --git a/examples/client/common.c b/examples/client/common.c index 6bc80ef54..f0d685667 100644 --- a/examples/client/common.c +++ b/examples/client/common.c @@ -800,7 +800,10 @@ static int readKeyBlob(const char* filename, WOLFTPM2_KEYBLOB* key) rc = BUFFER_E; goto exit; } if (fp != WBADFILE) { - WFSEEK(NULL, fp, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(WFSEEK(NULL, fp, 0, WSEEK_END))) { + printf("File seek failed\n"); + rc = BUFFER_E; goto exit; + } fileSz = WFTELL(NULL, fp); WREWIND(NULL, fp); diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index 876761fc4..e0f789bb9 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -1735,7 +1735,11 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz) if (WFOPEN(NULL, &file, fileName, "rb") != 0) return 0; - WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + WFCLOSE(NULL, file); + return 0; + } + fileSz = (word32)WFTELL(NULL, file); WREWIND(NULL, file); @@ -2555,7 +2559,11 @@ static char* LoadTpmSshKey(const char* keyFile, const char* username) "Failed to open TPM key file: %s\n", keyFile); return NULL; } - WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + fprintf(stderr, "TPM key file seek failed\n"); + WFCLOSE(NULL, file); + return NULL; + } length = WFTELL(NULL, file); WREWIND(NULL, file); diff --git a/examples/tpmcertserver/tpmcertclient.c b/examples/tpmcertserver/tpmcertclient.c index 1ebf76d13..3abfbf908 100644 --- a/examples/tpmcertserver/tpmcertclient.c +++ b/examples/tpmcertserver/tpmcertclient.c @@ -79,22 +79,26 @@ static int TpmCcLoadFile(const char* file, byte* buf, word32* bufSz) ret = -1; } else { - WFSEEK(NULL, f, 0, WSEEK_END); - fileSz = (word32)WFTELL(NULL, f); - WREWIND(NULL, f); + if (WFSEEK_SUCCESS(WFSEEK(NULL, f, 0, WSEEK_END))) { + fileSz = (word32)WFTELL(NULL, f); + WREWIND(NULL, f); - if (fileSz == 0 || fileSz > *bufSz) { - ret = -1; - } - else { - readSz = (word32)WFREAD(NULL, buf, 1, fileSz, f); - if (readSz != fileSz) { + if (fileSz == 0 || fileSz > *bufSz) { ret = -1; } else { - *bufSz = fileSz; + readSz = (word32)WFREAD(NULL, buf, 1, fileSz, f); + if (readSz != fileSz) { + ret = -1; + } + else { + *bufSz = fileSz; + } } } + else { + ret = -1; + } WFCLOSE(NULL, f); } diff --git a/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c b/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c index 2f4076566..268fd2777 100644 --- a/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c +++ b/ide/Espressif/ESP-IDF/examples/wolfssh_echoserver/main/echoserver.c @@ -1665,7 +1665,10 @@ static int load_file(const char* fileName, byte* buf, word32* bufSz) if (WFOPEN(NULL, &file, fileName, "rb") != 0) return 0; - WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + WFCLOSE(NULL, file); + return 0; + } fileSz = (word32)WFTELL(NULL, file); WREWIND(NULL, file); @@ -2173,7 +2176,11 @@ static char* LoadTpmSshKey(const char* keyFile, const char* username) "Failed to open TPM key file: %s\n", keyFile); return NULL; } - WFSEEK(NULL, file, 0, WSEEK_END); + if (!WFSEEK_SUCCESS(WFSEEK(NULL, file, 0, WSEEK_END))) { + fprintf(stderr, "TPM key file seek failed\n"); + WFCLOSE(NULL, file); + return NULL; + } length = WFTELL(NULL, file); WREWIND(NULL, file); diff --git a/src/wolfscp.c b/src/wolfscp.c index f357aa360..079d07ad6 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -2692,11 +2692,13 @@ static int _GetFileSize(void* fs, WFILE* fp, word32* fileSz) return WS_BAD_ARGUMENT; /* get file size */ - WFSEEK(fs, fp, 0, WSEEK_END); - *fileSz = (word32)WFTELL(fs, fp); - WREWIND(fs, fp); + if (WFSEEK_SUCCESS(WFSEEK(fs, fp, 0, WSEEK_END))) { + *fileSz = (word32)WFTELL(fs, fp); + WREWIND(fs, fp); - return WS_SUCCESS; + return WS_SUCCESS; + } + return WS_BAD_FILE_E; } static int GetFileStats(void *fs, ScpSendCtx* ctx, const char* fileName,