From 4d8833b609e85491e122e23cc98de7b333163040 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 30 Nov 2026 13:05:31 -0700 Subject: [PATCH 1/2] sftp: skip the copy for an empty request path GetStringRef() returns a NULL pointer for a zero-length string, and wolfSSH_SFTP_RecvRealPath() and GetAndCleanPath() passed it straight to memcpy(). That is undefined even for a length of 0. Copy only when the path has bytes; an empty path still resolves to the start path. - regress: REALPATH, STAT and LSTAT with an empty path, which UBSan flags at both copies without the guard Issue: #1400 --- src/wolfsftp.c | 6 ++++-- tests/regress.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 4a088127e..98470d8f8 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -1628,7 +1628,8 @@ static int wolfSSH_SFTP_RecvRealPath(WOLFSSH* ssh, int reqId, byte* data, "Real Path Error"); return (ret == WS_SUCCESS) ? WS_BUFFER_E : ret; } - WMEMCPY(r, str, rSz); + if (rSz > 0) + WMEMCPY(r, str, rSz); r[rSz] = '\0'; WLOG(WS_LOG_SFTP, "Real Path Request = %s", r); @@ -2102,7 +2103,8 @@ static int GetAndCleanPath(WOLFSSH* ssh, if (sz >= sizeof r) return WS_BUFFER_E; - WMEMCPY(r, data, sz); + if (sz > 0) + WMEMCPY(r, data, sz); r[sz] = '\0'; ret = wolfSSH_RealPath(ssh->sftpDefaultPath, r, s, sSz); diff --git a/tests/regress.c b/tests/regress.c index 850025079..2f77c9e92 100644 --- a/tests/regress.c +++ b/tests/regress.c @@ -20946,6 +20946,57 @@ static void TestSftpFailedRequestsAnswered(void) wolfSSH_CTX_free(ctx); } +/* An empty path arrives from GetStringRef() as a NULL pointer. The handlers + * must not pass it to memcpy() (UB even for a length of 0), and still resolve + * it to the start path. Issue 1400. */ +static void TestSftpEmptyPath(void) +{ + WOLFSSH_CTX* ctx; + WOLFSSH* ssh; + int rid = 950; + word32 idx; + byte pkt[3 * UINT32_SZ]; + char cwd[WOLFSSH_MAX_FILENAME]; + + ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL); + AssertNotNull(ctx); + ssh = wolfSSH_new(ctx); + AssertNotNull(ssh); + AssertIntEQ(wolfSSH_SFTP_TestRecvStateInit(ssh), WS_SUCCESS); + + WMEMSET(cwd, 0, sizeof(cwd)); + AssertNotNull(WGETCWD(ssh->fs, cwd, sizeof(cwd) - 1)); + AssertIntEQ(wolfSSH_SFTP_SetDefaultPath(ssh, cwd), WS_SUCCESS); + + /* REALPATH copies the path itself */ + idx = SftpBuildPathPkt(pkt, "", 0, 0, 0, 0); + AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_REALPATH, ++rid, pkt, idx), + WS_SUCCESS); + { + const byte* reply; + word32 replySz; + + reply = wolfSSH_SFTP_TestRecvReply(ssh, &replySz); + AssertNotNull(reply); + AssertTrue(replySz >= WOLFSSH_SFTP_HEADER); + AssertIntEQ(reply[LENGTH_SZ], WOLFSSH_FTP_NAME); + AssertIntEQ((int)SftpGetU32(reply + LENGTH_SZ + MSG_ID_SZ), rid); + } + + /* STAT and LSTAT go through GetAndCleanPath() */ + idx = SftpBuildPathPkt(pkt, "", 0, 0, 0, 0); + AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_STAT, ++rid, pkt, idx), + WS_SUCCESS); + AssertSftpAttrsReply(ssh, rid); + AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_LSTAT, ++rid, pkt, idx), + WS_SUCCESS); + AssertSftpAttrsReply(ssh, rid); + + wolfSSH_SFTP_TestRecvStateFree(ssh); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); +} + #endif /* !NO_WOLFSSH_SERVER && !USE_WINDOWS_API && !NO_FILESYSTEM */ #if !defined(NO_WOLFSSH_SERVER) && defined(USE_WINDOWS_API) && \ @@ -25317,6 +25368,8 @@ int main(int argc, char** argv) TestSftpSetStatAttributes(); /* a refused request still gets its status reply */ TestSftpFailedRequestsAnswered(); + /* an empty path is not copied from NULL */ + TestSftpEmptyPath(); #endif #if !defined(NO_WOLFSSH_SERVER) && !defined(NO_FILESYSTEM) /* FXF_APPEND writes land at EOF regardless of the request offset */ From e4e850b3c663ce5d4928db868477bb4b432a3fe9 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 30 Nov 2026 13:15:11 -0700 Subject: [PATCH 2/2] sftp: refuse RMDIR of an empty path An empty path resolves to the session's start directory, so RMDIR "" removed it when it was empty and answered OK. Refuse an empty path, or one that starts with NUL, with NO_SUCH_FILE and remove nothing, as OpenSSH's sftp-server does. - regress: REALPATH of an empty path names the start path Issue: #1400 --- src/wolfsftp.c | 5 +++++ tests/regress.c | 44 ++++++++++++++++++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 98470d8f8..406ae789b 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -2269,6 +2269,11 @@ int wolfSSH_SFTP_RecvRMDIR(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) return SFTP_SendBadMessage(ssh, reqId); } + if (strSz == 0 || str[0] == '\0') { + rc = SFTP_SendStatus(ssh, WOLFSSH_FTP_NOFILE, reqId, "No such file"); + return (rc == WS_SUCCESS) ? WS_BAD_FILE_E : rc; + } + ret = GetAndCleanPath(ssh, str, strSz, dir, sizeof(dir)); if (ret != WS_SUCCESS && ret != WS_PERMISSIONS) { WLOG(WS_LOG_SFTP, "Unable to resolve directory path"); diff --git a/tests/regress.c b/tests/regress.c index 2f77c9e92..a39583c93 100644 --- a/tests/regress.c +++ b/tests/regress.c @@ -20948,7 +20948,8 @@ static void TestSftpFailedRequestsAnswered(void) /* An empty path arrives from GetStringRef() as a NULL pointer. The handlers * must not pass it to memcpy() (UB even for a length of 0), and still resolve - * it to the start path. Issue 1400. */ + * it to the start path. RMDIR refuses it with NO_SUCH_FILE, as OpenSSH does, + * rather than removing the start directory. Issue 1400. */ static void TestSftpEmptyPath(void) { WOLFSSH_CTX* ctx; @@ -20956,7 +20957,10 @@ static void TestSftpEmptyPath(void) int rid = 950; word32 idx; byte pkt[3 * UINT32_SZ]; - char cwd[WOLFSSH_MAX_FILENAME]; + /* short enough that the fixture suffix below still fits a full path */ + char cwd[WOLFSSH_MAX_FILENAME - 64]; + char start[WOLFSSH_MAX_FILENAME]; + WSTAT_T st; ctx = wolfSSH_CTX_new(WOLFSSH_ENDPOINT_SERVER, NULL); AssertNotNull(ctx); @@ -20965,26 +20969,37 @@ static void TestSftpEmptyPath(void) AssertIntEQ(wolfSSH_SFTP_TestRecvStateInit(ssh), WS_SUCCESS); WMEMSET(cwd, 0, sizeof(cwd)); - AssertNotNull(WGETCWD(ssh->fs, cwd, sizeof(cwd) - 1)); + if (WGETCWD(ssh->fs, cwd, sizeof(cwd) - 1) == NULL) { + wolfSSH_SFTP_TestRecvStateFree(ssh); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + return; + } AssertIntEQ(wolfSSH_SFTP_SetDefaultPath(ssh, cwd), WS_SUCCESS); - /* REALPATH copies the path itself */ + /* REALPATH copies the path itself, and names the start path */ idx = SftpBuildPathPkt(pkt, "", 0, 0, 0, 0); AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_REALPATH, ++rid, pkt, idx), WS_SUCCESS); { const byte* reply; word32 replySz; + word32 nameSz; + word32 nameIdx = WOLFSSH_SFTP_HEADER + UINT32_SZ; reply = wolfSSH_SFTP_TestRecvReply(ssh, &replySz); AssertNotNull(reply); - AssertTrue(replySz >= WOLFSSH_SFTP_HEADER); + AssertTrue(replySz >= nameIdx + UINT32_SZ); AssertIntEQ(reply[LENGTH_SZ], WOLFSSH_FTP_NAME); AssertIntEQ((int)SftpGetU32(reply + LENGTH_SZ + MSG_ID_SZ), rid); + AssertIntEQ((int)SftpGetU32(reply + WOLFSSH_SFTP_HEADER), 1); + nameSz = SftpGetU32(reply + nameIdx); + AssertTrue(replySz >= nameIdx + UINT32_SZ + nameSz); + AssertIntEQ((int)nameSz, (int)WSTRLEN(cwd)); + AssertIntEQ(WMEMCMP(reply + nameIdx + UINT32_SZ, cwd, nameSz), 0); } /* STAT and LSTAT go through GetAndCleanPath() */ - idx = SftpBuildPathPkt(pkt, "", 0, 0, 0, 0); AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_STAT, ++rid, pkt, idx), WS_SUCCESS); AssertSftpAttrsReply(ssh, rid); @@ -20992,6 +21007,23 @@ static void TestSftpEmptyPath(void) WS_SUCCESS); AssertSftpAttrsReply(ssh, rid); + /* an empty start directory survives RMDIR "", and RMDIR of a lone NUL */ + WSNPRINTF(start, sizeof(start), "%s/wolfssh_empty_%lu.dir", cwd, + SFTP_TEST_PID()); + (void)WRMDIR(ssh->fs, start); + AssertIntEQ(WMKDIR(ssh->fs, start, 0755), 0); + AssertIntEQ(wolfSSH_SFTP_SetDefaultPath(ssh, start), WS_SUCCESS); + AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_RMDIR, ++rid, pkt, idx), + WS_BAD_FILE_E); + AssertSftpStatusReply(ssh, rid, WOLFSSH_FTP_NOFILE); + AssertIntEQ(WSTAT(ssh->fs, start, &st), 0); + idx = SftpBuildPathPkt(pkt, "", 1, 0, 0, 0); + AssertIntEQ(SftpRawRequest(ssh, WOLFSSH_FTP_RMDIR, ++rid, pkt, idx), + WS_BAD_FILE_E); + AssertSftpStatusReply(ssh, rid, WOLFSSH_FTP_NOFILE); + AssertIntEQ(WSTAT(ssh->fs, start, &st), 0); + (void)WRMDIR(ssh->fs, start); + wolfSSH_SFTP_TestRecvStateFree(ssh); wolfSSH_free(ssh); wolfSSH_CTX_free(ctx);