From 4b948e68cf0c6c1fd880f780ff9e26fa9ae1e03a Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 13 May 2026 10:21:23 -0700 Subject: [PATCH] sftp: NULL fName after free in all readdir variants - Apply the prior FATFS fix across Nucleus, MQX, Windows, Zephyr, Harmony, and user-filesystem readdir paths. - Prevents double-free crash when SFTPNAME_free runs on a name struct after readdir returns an error. --- src/wolfsftp.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index f8c6d531..8f06b31c 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -2820,6 +2820,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -2889,6 +2890,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, >= (int)sizeof(r)) { WLOG(WS_LOG_SFTP, "Path length too large"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } } @@ -2896,6 +2898,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (out->fSz + 1 > (sizeof r)) { WLOG(WS_LOG_SFTP, "Path length too large"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } WSTRNCPY(r, out->fName, sizeof(r)); @@ -2904,6 +2907,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (wolfSSH_RealPath(ssh->sftpDefaultPath, r, s, sizeof(s)) < 0) { WLOG(WS_LOG_SFTP, "Error cleaning path to get attributes"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -2930,6 +2934,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -2987,12 +2992,14 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, >= (int)sizeof(r)) { WLOG(WS_LOG_SFTP, "Path length too large"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } if (wolfSSH_RealPath(ssh->sftpDefaultPath, r, s, sizeof(s)) < 0) { WLOG(WS_LOG_SFTP, "Error cleaning path to get attributes"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -3007,6 +3014,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -3084,6 +3092,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, buf = (char*)WMALLOC(bufSz, out->heap, DYNTYPE_SFTP); if (buf == NULL) { WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_MEMORY_E; } @@ -3105,6 +3114,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -3221,11 +3231,13 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, >= (int)sizeof(r)) { WLOG(WS_LOG_SFTP, "Path length too large"); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } if (wolfSSH_RealPath(ssh->sftpDefaultPath, r, s, sizeof(s)) < 0) { WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; WLOG(WS_LOG_SFTP, "Error cleaning path to get attributes"); return WS_FATAL_ERROR; } @@ -3241,6 +3253,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } @@ -3298,6 +3311,7 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out, if (SFTP_CreateLongName(out) != WS_SUCCESS) { WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName); WFREE(out->fName, out->heap, DYNTYPE_SFTP); + out->fName = NULL; return WS_FATAL_ERROR; } return WS_SUCCESS;