From 50b225cad46ac194eb76b909cb0eb0ac5a41c2da Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 3 Dec 2026 09:13:30 -0800 Subject: [PATCH 1/4] sftpclient: dispatch commands on the first word doCmds() picks a command only when its keyword is the first word of the line, followed by a blank or the end of the line. A keyword inside a file name or inside another command no longer selects that command, so "rm getlog" removes getlog instead of fetching og. - sftpCmdArgs() returns the argument text, skipping blanks around the keyword; every command uses it, creat included - lcd and lls report "not supported" where they are compiled out - tests: rm of getlog, cdrom and tmkdir must remove the files; pwd and ls ended by a CR Issue: F-14766, F-16749 --- examples/sftpclient/sftpclient.c | 114 +++++++++++++++++-------------- tests/sftp.c | 53 ++++++++++++++ 2 files changed, 114 insertions(+), 53 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 5ca92c9bf..992e0853e 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -66,7 +66,6 @@ static WOLFSSH* ssh = NULL; static char* workingDir; #define fin stdin #define fout stdout -#define MAX_CMD_SZ 7 #define AUTOPILOT_OFF 0 @@ -472,9 +471,30 @@ static INLINE char* SFTP_FGETS(func_args* args, char* msg, int msgSz) } +/* Returns the argument text when kw is the first word of line, else NULL. + * Blanks before the keyword and between it and its arguments are skipped, + * so a keyword inside a file name or another command never matches. */ +static char* sftpCmdArgs(char* line, const char* kw) +{ + size_t kwSz = WSTRLEN(kw); + + while (*line == ' ' || *line == '\t') + line++; + if (WSTRNCMP(line, kw, kwSz) != 0) + return NULL; + line += kwSz; + if (*line != '\0' && *line != ' ' && *line != '\t' && *line != '\r' && + *line != '\n') + return NULL; + while (*line == ' ' || *line == '\t') + line++; + return line; +} + + /* * Parse " " from a SFTP command argument string. - * pt: points past the command keyword (caller advanced by sizeof). + * pt: points at the argument text after the command keyword. * modeBuf: caller buffer of WOLFSSH_MAX_OCTET_LEN bytes; receives the * null-terminated octal mode token on success. * pathOut: receives a pointer to the resolved remote path on success. @@ -575,19 +595,7 @@ static int doCmds(func_args* args) } msg[WOLFSSH_MAX_FILENAME * 2 - 1] = '\0'; - /* Anchored to the start of the line so a file name containing - * "creat" in another command does not match, and dispatched ahead - * of the unanchored substring matchers below so none of them can - * steal a creat line whose mode or path contains "get", "cd", etc. - * The keyword may be followed by a space, tab, or end of line; the - * bare-keyword form still reaches sftpParseModeAndPath so its - * empty-argument handling stays covered. */ - pt = msg; - while (*pt == ' ' || *pt == '\t') - pt++; - if (WSTRNCMP(pt, "creat", 5) == 0 && - (pt[5] == '\0' || pt[5] == ' ' || pt[5] == '\t' || - pt[5] == '\n')) { + if ((pt = sftpCmdArgs(msg, "creat")) != NULL) { char* f = NULL; char* path; char mode[WOLFSSH_MAX_OCTET_LEN]; @@ -599,9 +607,6 @@ static int doCmds(func_args* args) unsigned long perVal; char* modeEnd; - pt += 5; - while (*pt == ' ' || *pt == '\t') - pt++; parseRet = sftpParseModeAndPath(pt, mode, &path, &f, workingDir); if (parseRet == 1) { printf("error with getting mode\r\n"); @@ -669,11 +674,10 @@ static int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "mkdir", sizeof(msg))) != NULL) { + if ((pt = sftpCmdArgs(msg, "mkdir")) != NULL) { int sz; char* f = NULL; - pt += sizeof("mkdir"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -714,17 +718,18 @@ static int doCmds(func_args* args) continue; } - if (WSTRNSTR(msg, "reget", MAX_CMD_SZ) != NULL) { + if ((pt = sftpCmdArgs(msg, "reget")) != NULL) { resume = 1; } + else { + pt = sftpCmdArgs(msg, "get"); + } - if ((pt = WSTRNSTR(msg, "get", MAX_CMD_SZ)) != NULL) { + if (pt != NULL) { int sz; char* f = NULL; char* to = NULL; - pt += sizeof("get"); - sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -825,16 +830,18 @@ static int doCmds(func_args* args) } - if (WSTRNSTR(msg, "reput", MAX_CMD_SZ) != NULL) { + if ((pt = sftpCmdArgs(msg, "reput")) != NULL) { resume = 1; } + else { + pt = sftpCmdArgs(msg, "put"); + } - if ((pt = WSTRNSTR(msg, "put", MAX_CMD_SZ)) != NULL) { + if (pt != NULL) { int sz; char* f = NULL; char* to = NULL; - pt += sizeof("put"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -935,13 +942,10 @@ static int doCmds(func_args* args) continue; } - /* lcd must be checked before cd so that WSTRNSTR - * does not match "cd" inside "lcd" */ + if ((pt = sftpCmdArgs(msg, "lcd")) != NULL) { #ifndef WOLFSSH_FATFS /* WCHDIR not yet defined for FatFS */ - if ((pt = WSTRNSTR(msg, "lcd", MAX_CMD_SZ)) != NULL) { int sz; - pt += sizeof("lcd"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') { @@ -955,16 +959,20 @@ static int doCmds(func_args* args) return -1; } } + #else + if (SFTP_FPUTS(args, "lcd is not supported\n") < 0) { + err_msg("fputs error"); + return -1; + } + #endif /* !WOLFSSH_FATFS */ continue; } - #endif /* !WOLFSSH_FATFS */ - if ((pt = WSTRNSTR(msg, "cd", MAX_CMD_SZ)) != NULL) { + if ((pt = sftpCmdArgs(msg, "cd")) != NULL) { WS_SFTP_FILEATRB atrb; int sz; char* f = NULL; - pt += sizeof("cd"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -1023,13 +1031,12 @@ static int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "chmod", MAX_CMD_SZ)) != NULL) { + if ((pt = sftpCmdArgs(msg, "chmod")) != NULL) { char* f = NULL; char* path; char mode[WOLFSSH_MAX_OCTET_LEN]; int parseRet; - pt += sizeof("chmod"); parseRet = sftpParseModeAndPath(pt, mode, &path, &f, workingDir); if (parseRet == 1) { printf("error with getting mode\r\n"); @@ -1075,11 +1082,10 @@ static int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "rmdir", MAX_CMD_SZ)) != NULL) { + if ((pt = sftpCmdArgs(msg, "rmdir")) != NULL) { int sz; char* f = NULL; - pt += sizeof("rmdir"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -1132,11 +1138,10 @@ static int doCmds(func_args* args) } - if ((pt = WSTRNSTR(msg, "rm", MAX_CMD_SZ)) != NULL) { + if ((pt = sftpCmdArgs(msg, "rm")) != NULL) { int sz; char* f = NULL; - pt += sizeof("rm"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -1188,14 +1193,13 @@ static int doCmds(func_args* args) continue; } - if ((pt = WSTRNSTR(msg, "rename", MAX_CMD_SZ)) != NULL) { + if ((pt = sftpCmdArgs(msg, "rename")) != NULL) { int sz; char* f = NULL; char* fTo = NULL; char* to; int toSz; - pt += sizeof("rename"); sz = (int)WSTRLEN(pt); if (sz > 0 && pt[sz - 1] == '\n') pt[sz - 1] = '\0'; @@ -1277,10 +1281,8 @@ static int doCmds(func_args* args) } + if (sftpCmdArgs(msg, "lls") != NULL) { #if !defined(NO_WOLFSSH_DIR) && !defined(WOLFSSH_FATFS) - /* lls must be checked before ls so that WSTRNSTR - * does not match "ls" inside "lls" */ - if (WSTRNSTR(msg, "lls", MAX_CMD_SZ) != NULL) { char cwd[WOLFSSH_MAX_FILENAME]; int llsErr = 0; @@ -1381,13 +1383,19 @@ static int doCmds(func_args* args) if (llsErr) { return -1; } + #else + if (SFTP_FPUTS(args, "lls is not supported\n") < 0) { + err_msg("fputs error"); + return -1; + } + #endif /* !NO_WOLFSSH_DIR && !WOLFSSH_FATFS */ continue; } - #endif /* !NO_WOLFSSH_DIR && !WOLFSSH_FATFS */ - if (WSTRNSTR(msg, "ls", MAX_CMD_SZ) != NULL) { + if ((pt = sftpCmdArgs(msg, "ls")) != NULL) { WS_SFTPNAME* tmp; WS_SFTPNAME* current; + byte showSz = (WSTRNCMP(pt, "-s", 2) == 0); do { while (ret == WS_REKEYING || ssh->error == WS_REKEYING) { @@ -1403,7 +1411,7 @@ static int doCmds(func_args* args) err == WS_REKEYING) && (current == NULL && err != WS_SUCCESS)); - if (WSTRNSTR(msg, "-s", MAX_CMD_SZ) != NULL) { + if (showSz) { char tmpStr[WOLFSSH_MAX_FILENAME]; XMEMSET(tmpStr, 0, WOLFSSH_MAX_FILENAME); XSNPRINTF(tmpStr, WOLFSSH_MAX_FILENAME, "size in bytes, file name\n"); @@ -1415,7 +1423,7 @@ static int doCmds(func_args* args) tmp = current; while (tmp != NULL) { - if (WSTRNSTR(msg, "-s", MAX_CMD_SZ) != NULL) { + if (showSz) { char tmpStr[WOLFSSH_MAX_FILENAME]; XSNPRINTF(tmpStr, WOLFSSH_MAX_FILENAME, "%lld, ", (long long)(((long long)tmp->atrb.sz[1] << 32) | tmp->atrb.sz[0])); @@ -1440,7 +1448,7 @@ static int doCmds(func_args* args) } /* display current working directory */ - if (WSTRNSTR(msg, "pwd", MAX_CMD_SZ) != NULL) { + if (sftpCmdArgs(msg, "pwd") != NULL) { if (SFTP_FPUTS(args, workingDir) < 0 || SFTP_FPUTS(args, "\n") < 0) { err_msg("fputs error"); @@ -1449,17 +1457,17 @@ static int doCmds(func_args* args) continue; } - if (WSTRNSTR(msg, "help", MAX_CMD_SZ) != NULL) { + if (sftpCmdArgs(msg, "help") != NULL) { ShowCommands(); continue; } - if (WSTRNSTR(msg, "quit", MAX_CMD_SZ) != NULL) { + if (sftpCmdArgs(msg, "quit") != NULL) { quit = 1; continue; } - if (WSTRNSTR(msg, "exit", MAX_CMD_SZ) != NULL) { + if (sftpCmdArgs(msg, "exit") != NULL) { quit = 1; continue; } diff --git a/tests/sftp.c b/tests/sftp.c index a8fcdd1be..06bddc689 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -166,6 +166,24 @@ static int checkLsHasNoMycreat(void) return (WSTRNSTR(inBuf, "mycreat", sizeof(inBuf)) != NULL) ? 1 : 0; } +/* each of these names holds another command's keyword; a dispatcher that + * matched the keyword inside the file name would run that command instead + * of the rm (getlog ran get, cdrom ran cd, tmkdir ran mkdir), leaving the + * file behind for this check to find */ +static int checkLsHasNoKeywordNames(void) +{ + static const char* names[] = { "getlog", "cdrom", "tmkdir" }; + int i; + + for (i = 0; i < (int)(sizeof(names) / sizeof(names[0])); i++) { + if (WSTRNSTR(inBuf, names[i], sizeof(inBuf)) != NULL) { + fprintf(stderr, "rm %s did not run: %s\n", names[i], inBuf); + return 1; + } + } + return 0; +} + static int checkCdNonexistent(void) { if (WSTRNSTR(inBuf, "Error changing directory", @@ -258,6 +276,19 @@ static int checkChmodDirectory(void) } #endif /* !USE_WINDOWS_API && !WOLFSSH_FATFS && !WOLFSSH_ZEPHYR */ +#if defined(NO_WOLFSSH_DIR) || defined(WOLFSSH_FATFS) +/* a command compiled out of this build must say so rather than fall through + * to another command; lcd used to reach the remote cd and lls the remote ls */ +static int checkNotSupported(void) +{ + if (WSTRNSTR(inBuf, "is not supported", sizeof(inBuf)) == NULL) { + fprintf(stderr, "expected not-supported message in %s\n", inBuf); + return 1; + } + return 0; +} +#endif /* NO_WOLFSSH_DIR || WOLFSSH_FATFS */ + #if !defined(NO_WOLFSSH_DIR) && !defined(WOLFSSH_FATFS) static int checkLlsHasConfigureAc(void) { @@ -271,6 +302,7 @@ static int checkLlsHasConfigureAc(void) } #endif /* !NO_WOLFSSH_DIR && !WOLFSSH_FATFS */ +#ifndef WOLFSSH_FATFS static int checkLcdNonexistent(void) { if (WSTRNSTR(inBuf, "Error changing local directory", @@ -281,6 +313,7 @@ static int checkLcdNonexistent(void) } return 0; } +#endif /* !WOLFSSH_FATFS */ #if !defined(NO_WOLFSSH_DIR) && !defined(WOLFSSH_FATFS) \ && !defined(WOLFSSH_ZEPHYR) @@ -328,12 +361,18 @@ static const SftpTestCmd cmds[] = { { "rm test-creat-tab", NULL }, { "rm test-creat-mtab", NULL }, { "rm mycreat", NULL }, + { "rm getlog", NULL }, + { "rm cdrom", NULL }, + { "rm tmkdir", NULL }, /* --- test sequence starts here --- */ { "mkdir a", NULL }, { "cd a", NULL }, { "pwd", checkPwdInA }, { "ls", checkLsEmpty }, + /* a CR ending a bare keyword, as from a CRLF script */ + { "pwd\r", checkPwdInA }, + { "ls\r", checkLsEmpty }, #ifdef WOLFSSH_ZEPHYR { "put " CONFIG_WOLFSSH_SFTP_DEFAULT_DIR "/configure.ac", NULL }, #else @@ -376,6 +415,14 @@ static const SftpTestCmd cmds[] = { { "creat 0644 mycreat", NULL }, { "rm mycreat", NULL }, { "ls", checkLsHasNoMycreat }, + /* every other keyword is anchored the same way */ + { "creat 0644 getlog", NULL }, + { "creat 0644 cdrom", NULL }, + { "creat 0644 tmkdir", NULL }, + { "rm getlog", NULL }, + { "rm cdrom", NULL }, + { "rm tmkdir", NULL }, + { "ls", checkLsHasNoKeywordNames }, /* leading whitespace and a tab separator both reach the creat handler; * a parse failure would skip creation silently (rm ignores a missing * file), so check each with ls before removing */ @@ -394,8 +441,14 @@ static const SftpTestCmd cmds[] = { { "cd /nonexistent_path_xyz", checkCdNonexistent }, #if !defined(NO_WOLFSSH_DIR) && !defined(WOLFSSH_FATFS) { "lls", checkLlsHasConfigureAc }, +#else + { "lls", checkNotSupported }, #endif +#ifndef WOLFSSH_FATFS { "lcd /nonexistent_path_xyz", checkLcdNonexistent }, +#else + { "lcd /nonexistent_path_xyz", checkNotSupported }, +#endif #if !defined(NO_WOLFSSH_DIR) && !defined(WOLFSSH_FATFS) \ && !defined(WOLFSSH_ZEPHYR) /* lcd into a subdirectory, verify with lls, then return. From 62d2cb3639a5499a7e8d639e5cb9f7d8460f195f Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 3 Dec 2026 09:13:53 -0800 Subject: [PATCH 2/4] sftpclient: free the remote path on early returns The mkdir, get, put, cd, rmdir, rm and rename branches of doCmds() free the heap-built remote path (and rename's target) before every early return, as creat and chmod already do. The leak mattered when the client runs in-process under tests/sftp.c. Issue: F-16752 --- examples/sftpclient/sftpclient.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 992e0853e..cf0ef96e2 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -702,12 +702,14 @@ static int doCmds(func_args* args) if (ret == WS_PERMISSIONS) { if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } else if (err != WS_WANT_READ && err != WS_WANT_WRITE) { if (SFTP_FPUTS(args, "Error writing directory\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -783,6 +785,7 @@ static int doCmds(func_args* args) } if (SFTP_FPUTS(args, buf) < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -810,17 +813,20 @@ static int doCmds(func_args* args) if (wolfSSH_get_error(ssh) == WS_SFTP_NOT_FILE_E) { if (SFTP_FPUTS(args, "Not a regular file\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } if (SFTP_FPUTS(args, "Error getting file\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } else { if (SFTP_FPUTS(args, "\n") < 0) { /* new line after status output */ err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -895,6 +901,7 @@ static int doCmds(func_args* args) } if (SFTP_FPUTS(args, buf) < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } @@ -923,17 +930,20 @@ static int doCmds(func_args* args) if (wolfSSH_get_error(ssh) == WS_SFTP_NOT_FILE_E) { if (SFTP_FPUTS(args, "Not a regular file\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } if (SFTP_FPUTS(args, "Error pushing file\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } else { if (SFTP_FPUTS(args, "\n") < 0) { /* new line after status output */ err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -1010,6 +1020,7 @@ static int doCmds(func_args* args) if (ret != WS_SUCCESS) { if (SFTP_FPUTS(args, "Error changing directory\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -1020,6 +1031,7 @@ static int doCmds(func_args* args) workingDir = (char*)WMALLOC(sz + 1, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (workingDir == NULL) { err_msg("Error malloc'ing"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } WMEMCPY(workingDir, pt, sz); @@ -1123,12 +1135,14 @@ static int doCmds(func_args* args) if (ret == WS_PERMISSIONS) { if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } else { if (SFTP_FPUTS(args, "Error writing directory\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -1179,12 +1193,14 @@ static int doCmds(func_args* args) if (ret == WS_PERMISSIONS) { if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } else { if (SFTP_FPUTS(args, "Error writing directory\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } @@ -1272,6 +1288,8 @@ static int doCmds(func_args* args) if (ret != WS_SUCCESS) { if (SFTP_FPUTS(args, "Error with rename\n") < 0) { err_msg("fputs error"); + WFREE(f, NULL, DYNAMIC_TYPE_TMP_BUFFER); + WFREE(fTo, NULL, DYNAMIC_TYPE_TMP_BUFFER); return -1; } } From 5f4db6ecb219befb50384cdeaa0655b01816c793 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 3 Dec 2026 09:14:23 -0800 Subject: [PATCH 3/4] sftpclient: drop only whole .. path components clean_path() treats ".." as the parent only when it is a complete path component, followed by '/' or the end of the path. A directory such as "..dots" is kept, so cd into it no longer moves up a level. - tests: cd into ..dots and check pwd Issue: F-11666 --- examples/sftpclient/sftpclient.c | 5 +++-- tests/sftp.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index cf0ef96e2..40db0b3fe 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -333,8 +333,9 @@ static void clean_path(char* path) if (path[i] == '/') { int z; - /* if next two chars are .. then delete */ - if (path[i+1] == '.' && path[i+2] == '.') { + /* if the next component is exactly .. then delete */ + if (path[i+1] == '.' && path[i+2] == '.' && + (path[i+3] == '/' || path[i+3] == '\0')) { enIdx = i + 3; /* start at one char before / and retrace path */ diff --git a/tests/sftp.c b/tests/sftp.c index 06bddc689..8ceaedacf 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -87,6 +87,28 @@ static int checkPwdInA(void) return 0; } +/* check that pwd output ends in /..dots: a name that only starts with two + * dots is a directory of its own, not a step up to the parent */ +static int checkPwdInDots(void) +{ + int i; + int len; + + for (i = 0; i < (int)sizeof(inBuf); i++) { + if (inBuf[i] == '\n') { + inBuf[i] = '\0'; + break; + } + } + + len = (int)WSTRLEN(inBuf); + if (len < 7 || WSTRNCMP(inBuf + len - 7, "/..dots", 7) != 0) { + printf("unexpected pwd of %s, looking for /..dots\n", inBuf); + return -1; + } + return 0; +} + /* check that ls of empty dir shows only . and .. */ static int checkLsEmpty(void) { @@ -364,6 +386,7 @@ static const SftpTestCmd cmds[] = { { "rm getlog", NULL }, { "rm cdrom", NULL }, { "rm tmkdir", NULL }, + { "rmdir ..dots", NULL }, /* --- test sequence starts here --- */ { "mkdir a", NULL }, @@ -423,6 +446,12 @@ static const SftpTestCmd cmds[] = { { "rm cdrom", NULL }, { "rm tmkdir", NULL }, { "ls", checkLsHasNoKeywordNames }, + { "mkdir ..dots", NULL }, + { "cd ..dots", NULL }, + { "pwd", checkPwdInDots }, + { "ls", checkLsEmpty }, + { "cd ..", NULL }, + { "rmdir ..dots", NULL }, /* leading whitespace and a tab separator both reach the creat handler; * a parse failure would skip creation silently (rm ignores a missing * file), so check each with ls before removing */ From 402196ea6287e7f7dbef742516e23669157a1f74 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 3 Dec 2026 10:23:07 -0800 Subject: [PATCH 4/4] sftp: end an LS listing only on SSH_FX_EOF wolfSSH_SFTP_LS() ends a listing only on SSH_FX_EOF or an empty NAME, and every step that fails leaves an error set, so NULL with no error is an empty directory. A READDIR failure still closes the handle, then frees the entries read so far. - wolfSSH_SFTP_DoName() records how a NAME request ended and sets an error on every failure, STATUS(OK) included: it carries no names - SendPacketType() sets the error when its buffer allocation fails - sftpclient ls prints an error when the listing fails - tests: each LS outcome from OPENDIR to CLOSE, and each allocation in LS failing in turn; ls of a removed working directory Issue: F-18849 --- examples/sftpclient/sftpclient.c | 7 + src/wolfsftp.c | 110 ++++++++---- tests/sftp.c | 17 ++ tests/unit.c | 279 ++++++++++++++++++++++++++++--- wolfssh/internal.h | 3 + wolfssh/wolfsftp.h | 3 + 6 files changed, 359 insertions(+), 60 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 40db0b3fe..b73b0d00b 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -1429,6 +1429,13 @@ static int doCmds(func_args* args) } while ((err == WS_WANT_READ || err == WS_WANT_WRITE || err == WS_REKEYING) && (current == NULL && err != WS_SUCCESS)); + if (current == NULL && err != WS_SUCCESS) { + if (SFTP_FPUTS(args, "Error listing directory\n") < 0) { + err_msg("fputs error"); + return -1; + } + continue; + } if (showSz) { char tmpStr[WOLFSSH_MAX_FILENAME]; diff --git a/src/wolfsftp.c b/src/wolfsftp.c index d20201458..cb39b0824 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -247,6 +247,7 @@ typedef struct WS_SFTP_LS_STATE { enum WS_SFTP_LS_STATE_ID state; byte handle[WOLFSSH_MAX_HANDLE]; int sz; + int readErr; /* READDIR failure, reported once the handle is closed */ WS_SFTPNAME* name; } WS_SFTP_LS_STATE; @@ -7318,6 +7319,7 @@ int SendPacketType(WOLFSSH* ssh, byte type, byte* buf, word32 bufSz) if (wolfSSH_SFTP_buffer_create(ssh, &state->buffer, bufSz + WOLFSSH_SFTP_HEADER + UINT32_SZ) != WS_SUCCESS) { + ssh->error = WS_MEMORY_E; wolfSSH_SFTP_ClearState(ssh, STATE_ID_SEND); return WS_MEMORY_E; } @@ -7795,6 +7797,7 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) if (wolfSSH_SFTP_buffer_create(ssh, &state->buffer, maxSz) != WS_SUCCESS) { WLOG(WS_LOG_SFTP, "Could not malloc memory"); + ssh->error = WS_MEMORY_E; wolfSSH_SFTP_ClearState(ssh, STATE_ID_NAME); return NULL; } @@ -7806,6 +7809,9 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) wolfSSH_SFTP_buffer_size(&state->buffer)); if (ret < 0) { if (!NoticeError(ssh)) { + if (ssh->error == WS_SUCCESS) { + ssh->error = ret; + } wolfSSH_SFTP_ClearState(ssh, STATE_ID_NAME); } return NULL; @@ -7817,7 +7823,9 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) if (ret < 0) { ssh->error = ret; } - else if (ret != WOLFSSH_FTP_OK) { + else { + /* even STATUS(OK) carries no names, so it fails */ + ssh->sftpNameStatus = ret; WLOG(WS_LOG_SFTP, "SFTP server returned status %d", ret); ssh->error = WS_SFTP_STATUS_NOT_OK; } @@ -7840,6 +7848,9 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) wolfSSH_SFTP_buffer_idx(&state->buffer)); if (ret <= 0) { if (!NoticeError(ssh)) { + if (ssh->error == WS_SUCCESS) { + ssh->error = ret < 0 ? ret : WS_BUFFER_E; + } wolfSSH_SFTP_ClearState(ssh, STATE_ID_NAME); } return NULL; @@ -7929,9 +7940,14 @@ static WS_SFTPNAME* wolfSSH_SFTP_DoName(WOLFSSH* ssh) wolfSSH_SFTP_ClearState(ssh, STATE_ID_NAME); if (ret != WS_SUCCESS) { WLOG(WS_LOG_SFTP, "Error with reading file names"); + ssh->error = ret; wolfSSH_SFTPNAME_list_free(n); return NULL; } + if (n == NULL) { + /* an empty NAME ends a listing as EOF does */ + ssh->sftpNameStatus = WOLFSSH_FTP_EOF; + } break; } @@ -8126,20 +8142,32 @@ int wolfSSH_TestSftpGetHandle(WOLFSSH* ssh, byte* handle, word32* handleSz) #endif +/* A failed LS step keeps the error it set, or records err, so the NULL + * return never reads as an empty directory. */ +static void SFTP_LsSetError(WOLFSSH* ssh, int err) +{ + if (ssh->error == WS_SUCCESS) { + ssh->error = err; + } +} + + /* Used to get a list of all files and their attributes from a directory. * * dir NULL terminated string of the directory to list - * - * returns a linked list of files in the directory on success, NULL on failure */ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) { struct WS_SFTP_LS_STATE* state = NULL; WS_SFTPNAME* name = NULL; WS_SFTPNAME* names = NULL; + int ret; if (ssh == NULL || dir == NULL) { WLOG(WS_LOG_SFTP, "Bad argument passed in"); + if (ssh != NULL) { + ssh->error = WS_BAD_ARGUMENT; + } return NULL; } @@ -8163,6 +8191,7 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE && ssh->error != WS_REKEYING) { + SFTP_LsSetError(ssh, WS_FATAL_ERROR); wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS); } return NULL; @@ -8171,12 +8200,14 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) FALL_THROUGH; case STATE_LS_OPENDIR: - if (wolfSSH_SFTP_OpenDir(ssh, (byte*)state->name->fName, - state->name->fSz) != WS_SUCCESS) { + ret = wolfSSH_SFTP_OpenDir(ssh, (byte*)state->name->fName, + state->name->fSz); + if (ret != WS_SUCCESS) { WLOG(WS_LOG_SFTP, "Unable to open directory"); if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE && ssh->error != WS_REKEYING) { + SFTP_LsSetError(ssh, ret); wolfSSH_SFTPNAME_list_free(state->name); state->name = NULL; wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS); } @@ -8189,12 +8220,14 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) case STATE_LS_GETHANDLE: /* get the handle from opening the directory and read with it */ - if (wolfSSH_SFTP_GetHandle(ssh, state->handle, (word32*)&state->sz) - != WS_SUCCESS) { + ret = wolfSSH_SFTP_GetHandle(ssh, state->handle, + (word32*)&state->sz); + if (ret != WS_SUCCESS) { WLOG(WS_LOG_SFTP, "Unable to get handle"); if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE && ssh->error != WS_REKEYING) { + SFTP_LsSetError(ssh, ret); wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS); } return NULL; @@ -8203,20 +8236,14 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) FALL_THROUGH; case STATE_LS_READDIR: - /* Now read the dir. Note in non-blocking we might get here multiple - * times so we have to assign to state->name later. */ - names = wolfSSH_SFTP_ReadDir(ssh, state->handle, state->sz); - if (names == NULL) { - if (ssh->error == WS_WANT_READ - || ssh->error == WS_WANT_WRITE - || ssh->error == WS_REKEYING) { - return NULL; + /* Read until the server reports EOF. In non-blocking mode this + * case is entered again, so entries collect in state->name. */ + for (;;) { + ssh->sftpNameStatus = WOLFSSH_FTP_OK; + names = wolfSSH_SFTP_ReadDir(ssh, state->handle, state->sz); + if (names == NULL) { + break; } - WLOG(WS_LOG_SFTP, "Error reading directory"); - /* fall through because the handle should always be closed */ - } - - while (names != NULL) { if (state->name == NULL) { state->name = names; } @@ -8228,37 +8255,50 @@ WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir) runner = runner->next); runner->next = names; } - names = wolfSSH_SFTP_ReadDir(ssh, state->handle, state->sz); - if (names == NULL) { - if (ssh->error == WS_WANT_READ - || ssh->error == WS_WANT_WRITE - || ssh->error == WS_REKEYING) { - /* State does not change so we will get back to this - * case clause in non-blocking mode. */ - return NULL; - } - WLOG(WS_LOG_SFTP, "Error reading directory"); - /* fall through, the handle should always be closed */ - } } - + if (ssh->error == WS_WANT_READ + || ssh->error == WS_WANT_WRITE + || ssh->error == WS_REKEYING) { + return NULL; + } + /* only EOF, or a NAME with no entries, ends the listing */ + if (ssh->sftpNameStatus == WOLFSSH_FTP_EOF) { + ssh->error = WS_SUCCESS; + } + else { + WLOG(WS_LOG_SFTP, "Error reading directory"); + SFTP_LsSetError(ssh, WS_FATAL_ERROR); + state->readErr = ssh->error; + } + /* the handle is closed either way */ state->state = STATE_LS_CLOSE; FALL_THROUGH; case STATE_LS_CLOSE: /* close dir when finished */ - if (wolfSSH_SFTP_Close(ssh, state->handle, state->sz) - != WS_SUCCESS) { + ret = wolfSSH_SFTP_Close(ssh, state->handle, state->sz); + if (ret != WS_SUCCESS) { WLOG(WS_LOG_SFTP, "Error closing handle"); if (ssh->error != WS_WANT_READ && ssh->error != WS_WANT_WRITE && ssh->error != WS_REKEYING) { + if (state->readErr != WS_SUCCESS) { + ssh->error = state->readErr; + } + else { + SFTP_LsSetError(ssh, ret); + } wolfSSH_SFTPNAME_list_free(state->name); state->name = NULL; wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS); } return NULL; } + if (state->readErr != WS_SUCCESS) { + ssh->error = state->readErr; + wolfSSH_SFTP_ClearState(ssh, STATE_ID_LS); + return NULL; + } break; default: diff --git a/tests/sftp.c b/tests/sftp.c index 8ceaedacf..917b0631a 100644 --- a/tests/sftp.c +++ b/tests/sftp.c @@ -206,6 +206,16 @@ static int checkLsHasNoKeywordNames(void) return 0; } +/* a failed listing must say so, not look like an empty directory */ +static int checkLsError(void) +{ + if (WSTRNSTR(inBuf, "Error listing directory", sizeof(inBuf)) == NULL) { + fprintf(stderr, "ls: expected error not found in %s\n", inBuf); + return 1; + } + return 0; +} + static int checkCdNonexistent(void) { if (WSTRNSTR(inBuf, "Error changing directory", @@ -387,6 +397,7 @@ static const SftpTestCmd cmds[] = { { "rm cdrom", NULL }, { "rm tmkdir", NULL }, { "rmdir ..dots", NULL }, + { "rmdir lsgone", NULL }, /* --- test sequence starts here --- */ { "mkdir a", NULL }, @@ -452,6 +463,12 @@ static const SftpTestCmd cmds[] = { { "ls", checkLsEmpty }, { "cd ..", NULL }, { "rmdir ..dots", NULL }, + /* listing a working directory removed from under the client fails */ + { "mkdir lsgone", NULL }, + { "cd lsgone", NULL }, + { "rmdir ../lsgone", NULL }, + { "ls", checkLsError }, + { "cd ..", NULL }, /* leading whitespace and a tab separator both reach the creat handler; * a parse failure would skip creation silently (rm ignores a missing * file), so check each with ls before removing */ diff --git a/tests/unit.c b/tests/unit.c index 4cccbbac2..f317d95f2 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -28157,6 +28157,31 @@ static int SftpPutHandle(WOLFSSH* ssh, word32 id) } +/* Stage a NAME reply carrying one entry, "name", with no attributes. */ +static int SftpPutName(WOLFSSH* ssh, word32 id, const char* name) +{ + byte reply[WOLFSSH_SFTP_HEADER + UINT32_SZ * 4 + 16]; + word32 nameSz = (word32)WSTRLEN(name); + word32 idx = WOLFSSH_SFTP_HEADER; + word32 sz; + + sz = SftpBuildReply(reply, sizeof(reply), WOLFSSH_FTP_NAME, id, + UINT32_SZ * 4 + nameSz); + if (sz == 0) + return -1; + PutU32BE(reply + idx, 1); + idx += UINT32_SZ; + PutU32BE(reply + idx, nameSz); + idx += UINT32_SZ; + WMEMCPY(reply + idx, name, nameSz); + idx += nameSz; + PutU32BE(reply + idx, 0); + idx += UINT32_SZ; + PutU32BE(reply + idx, 0); + return wolfSSH_TestChannelPutData(ssh->channelList, reply, sz); +} + + /* An interrupted GET holds its local file open. Freeing the session must * close it. */ static int test_SftpGetFreeClosesLocal(void) @@ -28363,7 +28388,228 @@ static int test_SftpRmdirStatus(void) } +/* Stage a NAME reply with "count" entries and no entry data: with a count of + * 1 the entry is truncated, with 0 the NAME is empty. */ +static int SftpPutBareName(WOLFSSH* ssh, word32 id, word32 count) +{ + byte reply[WOLFSSH_SFTP_HEADER + UINT32_SZ]; + word32 sz; + + sz = SftpBuildReply(reply, sizeof(reply), WOLFSSH_FTP_NAME, id, + UINT32_SZ); + if (sz == 0) + return -1; + PutU32BE(reply + WOLFSSH_SFTP_HEADER, count); + return wolfSSH_TestChannelPutData(ssh->channelList, reply, sz); +} + + +/* in place of a status, the last READDIR gets a NAME */ +#define SFTP_LS_BAD_NAME 0xFFFFFFFFU +#define SFTP_LS_EMPTY_NAME 0xFFFFFFFEU + +typedef struct SftpLsCase { + word32 open; /* OPENDIR reply: OK for a handle, else that status */ + int entries; /* READDIRs answered with one name each */ + word32 end; /* reply to the READDIR after those */ + word32 close; /* CLOSE status */ + int closeLate; /* CLOSE reply arrives only after LS has returned once */ + int expErr; +} SftpLsCase; + +/* LS returns the whole listing, ended by SSH_FX_EOF or an empty NAME, or NULL + * with an error set: no partial list, and no failure that reads as an empty + * directory. */ +static int SftpLsStatusCase(const SftpLsCase* c) +{ + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + WS_SFTPNAME* name = NULL; + word32 id; + int ret; + int result = 0; + + ret = SftpClientNewSession(&ctx, &ssh); + if (ret != 0) { + result = ret; + goto done; + } + ssh->sftpState = SFTP_DONE; + ssh->channelList->peerWindowSz = 65536; + ssh->channelList->peerMaxPacketSz = 32768; + id = ssh->reqId; + if (SftpPutName(ssh, id++, "/d") != 0) { + result = -1269; + goto done; + } + if (c->open != WOLFSSH_FTP_OK) { + if (SftpGetStatus(ssh, id, c->open) != 0) + result = -1269; + } + else if (SftpPutHandle(ssh, id++) != 0 || + (c->entries > 0 && SftpPutName(ssh, id++, "entry") != 0) || + (c->end == SFTP_LS_BAD_NAME ? SftpPutBareName(ssh, id++, 1) : + c->end == SFTP_LS_EMPTY_NAME ? SftpPutBareName(ssh, id++, 0) : + SftpGetStatus(ssh, id++, c->end)) != 0 || + (!c->closeLate && SftpGetStatus(ssh, id, c->close) != 0)) { + result = -1269; + } + if (result != 0) + goto done; + + name = wolfSSH_SFTP_LS(ssh, (char*)"d"); + if (c->closeLate) { + if (name != NULL || wolfSSH_get_error(ssh) != WS_WANT_READ || + ssh->lsState == NULL) { + result = -1273; + goto done; + } + if (SftpGetStatus(ssh, id, c->close) != 0) { + result = -1269; + goto done; + } + name = wolfSSH_SFTP_LS(ssh, (char*)"d"); + } + if (wolfSSH_get_error(ssh) != c->expErr || ssh->lsState != NULL) + result = -1270; + else if (c->expErr != WS_SUCCESS || c->entries == 0) { + if (name != NULL) + result = -1271; + } + else if (name == NULL || name->next != NULL || + WSTRCMP(name->fName, "entry") != 0) + result = -1272; + +done: + wolfSSH_SFTPNAME_list_free(name); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + return result; +} + + +static int test_SftpLsStatus(void) +{ + static const SftpLsCase cases[] = { + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_EOF, WOLFSSH_FTP_OK, 0, WS_SUCCESS }, + { WOLFSSH_FTP_OK, 0, WOLFSSH_FTP_EOF, WOLFSSH_FTP_OK, 0, WS_SUCCESS }, + { WOLFSSH_FTP_OK, 1, SFTP_LS_EMPTY_NAME, WOLFSSH_FTP_OK, 0, + WS_SUCCESS }, + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_FAILURE, WOLFSSH_FTP_OK, 0, + WS_SFTP_STATUS_NOT_OK }, + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_PERMISSION, WOLFSSH_FTP_OK, 0, + WS_SFTP_STATUS_NOT_OK }, + { WOLFSSH_FTP_OK, 0, WOLFSSH_FTP_FAILURE, WOLFSSH_FTP_OK, 0, + WS_SFTP_STATUS_NOT_OK }, + /* STATUS(OK) carries no names and is not an EOF */ + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_OK, WOLFSSH_FTP_OK, 0, + WS_SFTP_STATUS_NOT_OK }, + { WOLFSSH_FTP_OK, 1, SFTP_LS_BAD_NAME, WOLFSSH_FTP_OK, 0, + WS_BUFFER_E }, + /* the READDIR error outlasts a CLOSE that waits, or fails */ + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_FAILURE, WOLFSSH_FTP_OK, 1, + WS_SFTP_STATUS_NOT_OK }, + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_FAILURE, WOLFSSH_FTP_FAILURE, 0, + WS_SFTP_STATUS_NOT_OK }, + /* failures outside READDIR */ + { WOLFSSH_FTP_PERMISSION, 0, 0, 0, 0, WS_SFTP_STATUS_NOT_OK }, + { WOLFSSH_FTP_OK, 1, WOLFSSH_FTP_EOF, WOLFSSH_FTP_FAILURE, 0, + WS_FATAL_ERROR }, + }; + word32 i; + int ret = 0; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]) && ret == 0; i++) { + ret = SftpLsStatusCase(&cases[i]); + if (ret != 0) + printf("SftpLsStatus case %u: %d\n", i, ret); + } + return ret; +} + + #ifdef WOLFSSH_TEST_CAPTURING_ALLOCATOR +/* Fails the lsFailAt'th allocation after install, counting them all. */ +static int lsFailAt = 0; +static int lsAllocs = 0; + +static void* LsFailMalloc(size_t size) +{ + if (++lsAllocs == lsFailAt) + return NULL; + return malloc(size); +} + + +/* Fail each allocation LS makes in turn. LS returns the whole two-entry + * listing, or NULL with an error set: never part of it, and never a NULL + * that reads as an empty directory. */ +static int test_SftpLsAllocFail(void) +{ + wolfSSL_Malloc_cb prevMf = NULL; + wolfSSL_Free_cb prevFf = NULL; + wolfSSL_Realloc_cb prevRf = NULL; + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + WS_SFTPNAME* name = NULL; + word32 id; + int failAt; + int err; + int done = 0; + int result = 0; + + wolfSSL_GetAllocators(&prevMf, &prevFf, &prevRf); + for (failAt = 1; !done && result == 0 && failAt < 1000; failAt++) { + result = SftpClientNewSession(&ctx, &ssh); + if (result != 0) + break; + ssh->sftpState = SFTP_DONE; + ssh->channelList->peerWindowSz = 65536; + ssh->channelList->peerMaxPacketSz = 32768; + id = ssh->reqId; + if (SftpPutName(ssh, id, "/d") != 0 || + SftpPutHandle(ssh, id + 1) != 0 || + SftpPutName(ssh, id + 2, "entry") != 0 || + SftpPutName(ssh, id + 3, "entry2") != 0 || + SftpGetStatus(ssh, id + 4, WOLFSSH_FTP_EOF) != 0 || + SftpGetStatus(ssh, id + 5, WOLFSSH_FTP_OK) != 0) { + result = -1274; + } + else { + lsAllocs = 0; + lsFailAt = failAt; + if (wolfSSL_SetAllocators(LsFailMalloc, prevFf, prevRf) != 0) { + result = -1274; + } + else { + name = wolfSSH_SFTP_LS(ssh, (char*)"d"); + wolfSSL_SetAllocators(prevMf, prevFf, prevRf); + done = lsAllocs < failAt; + err = wolfSSH_get_error(ssh); + if (name == NULL) { + if (done || err == WS_SUCCESS) + result = -1275; + } + else if (err != WS_SUCCESS || name->next == NULL || + name->next->next != NULL) + result = -1276; + if (result != 0) + printf("SftpLsAllocFail at %d: %d\n", failAt, result); + } + } + wolfSSH_SFTPNAME_list_free(name); + name = NULL; + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + ssh = NULL; + ctx = NULL; + } + if (result == 0 && !done) + result = -1277; + return result; +} + + /* Guard allocator. Each block carries its size in a header; a freed block is * poisoned and held, so a later write to it is seen by GuardIntact(), and the * count of live blocks shows a leak. */ @@ -28533,31 +28779,6 @@ static int SftpNegFailCase(int op) } -/* Stage a NAME reply carrying one entry, "name", with no attributes. */ -static int SftpPutName(WOLFSSH* ssh, word32 id, const char* name) -{ - byte reply[WOLFSSH_SFTP_HEADER + UINT32_SZ * 4 + 16]; - word32 nameSz = (word32)WSTRLEN(name); - word32 idx = WOLFSSH_SFTP_HEADER; - word32 sz; - - sz = SftpBuildReply(reply, sizeof(reply), WOLFSSH_FTP_NAME, id, - UINT32_SZ * 4 + nameSz); - if (sz == 0) - return -1; - PutU32BE(reply + idx, 1); - idx += UINT32_SZ; - PutU32BE(reply + idx, nameSz); - idx += UINT32_SZ; - WMEMCPY(reply + idx, name, nameSz); - idx += nameSz; - PutU32BE(reply + idx, 0); - idx += UINT32_SZ; - PutU32BE(reply + idx, 0); - return wolfSSH_TestChannelPutData(ssh->channelList, reply, sz); -} - - /* An interrupted LS holds the entries read so far. Freeing the session must * free them. */ static int SftpLsFreeCase(void) @@ -31324,7 +31545,15 @@ int wolfSSH_UnitTest(int argc, char** argv) printf("SftpRmdirStatus: %s\n", unitResult == 0 ? "SUCCESS" : "FAILED"); testResult = testResult || unitResult; + unitResult = test_SftpLsStatus(); + printf("SftpLsStatus: %s\n", unitResult == 0 ? "SUCCESS" : "FAILED"); + testResult = testResult || unitResult; + #ifdef WOLFSSH_TEST_CAPTURING_ALLOCATOR + unitResult = test_SftpLsAllocFail(); + printf("SftpLsAllocFail: %s\n", unitResult == 0 ? "SUCCESS" : "FAILED"); + testResult = testResult || unitResult; + unitResult = test_SftpClientStateTeardown(); printf("SftpClientStateTeardown: %s\n", unitResult == 0 ? "SUCCESS" : "FAILED"); diff --git a/wolfssh/internal.h b/wolfssh/internal.h index c19c514d9..8b7f1ad63 100644 --- a/wolfssh/internal.h +++ b/wolfssh/internal.h @@ -1518,6 +1518,9 @@ struct WOLFSSH { #endif #ifdef WOLFSSH_SFTP word32 reqId; + int sftpNameStatus; /* how the last NAME request ended: its STATUS + * code, or EOF for an empty NAME, so a READDIR + * can tell the end of a listing from failure */ byte sftpState; byte realState; byte sftpInt; diff --git a/wolfssh/wolfsftp.h b/wolfssh/wolfsftp.h index 31c85b3c9..5ee1bcc78 100644 --- a/wolfssh/wolfsftp.h +++ b/wolfssh/wolfsftp.h @@ -301,6 +301,9 @@ WOLFSSH_API int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WOLFSSH_API int wolfSSH_SFTP_RMDIR(WOLFSSH* ssh, char* dir); WOLFSSH_API int wolfSSH_SFTP_Rename(WOLFSSH* ssh, const char* old, const char* nw); +/* Returns the whole listing of dir. NULL with no error is an empty + * directory; NULL with an error set is a failure, including one partway + * through the listing, and no partial list is returned. */ WOLFSSH_API WS_SFTPNAME* wolfSSH_SFTP_LS(WOLFSSH* ssh, char* dir); WOLFSSH_API int wolfSSH_SFTP_CHMOD(WOLFSSH* ssh, char* n, char* oct);