diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 4a088127e..406ae789b 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); @@ -2267,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 850025079..a39583c93 100644 --- a/tests/regress.c +++ b/tests/regress.c @@ -20946,6 +20946,89 @@ 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. 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; + WOLFSSH* ssh; + int rid = 950; + word32 idx; + byte pkt[3 * UINT32_SZ]; + /* 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); + ssh = wolfSSH_new(ctx); + AssertNotNull(ssh); + AssertIntEQ(wolfSSH_SFTP_TestRecvStateInit(ssh), WS_SUCCESS); + + WMEMSET(cwd, 0, sizeof(cwd)); + 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, 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 >= 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() */ + 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); + + /* 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); +} + #endif /* !NO_WOLFSSH_SERVER && !USE_WINDOWS_API && !NO_FILESYSTEM */ #if !defined(NO_WOLFSSH_SERVER) && defined(USE_WINDOWS_API) && \ @@ -25317,6 +25400,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 */