From 57dfc44c93bfe00b4e64bf6a4e3051dfefd4d4da Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 2 Nov 2026 14:58:07 -0700 Subject: [PATCH] sftp: send the caller's attributes with MKDIR wolfSSH_SFTP_MKDIR() encodes atr with SFTP_SetAttributes(), as the other attribute-carrying requests do. A NULL atr requests permissions 0777, the request every MKDIR used to send. - sftpclient: pass NULL instead of an uninitialized atr - api: a 0700 request makes a 0700 directory on the echoserver Issue: F-14634 --- examples/sftpclient/sftpclient.c | 3 +- src/wolfsftp.c | 25 ++++++--- tests/api.c | 87 ++++++++++++++++++++++++++++++++ wolfssh/wolfsftp.h | 2 + 4 files changed, 108 insertions(+), 9 deletions(-) diff --git a/examples/sftpclient/sftpclient.c b/examples/sftpclient/sftpclient.c index 281e1333..768df521 100644 --- a/examples/sftpclient/sftpclient.c +++ b/examples/sftpclient/sftpclient.c @@ -667,7 +667,6 @@ static int doCmds(func_args* args) } if ((pt = WSTRNSTR(msg, "mkdir", sizeof(msg))) != NULL) { - WS_SFTP_FILEATRB atrb; int sz; char* f = NULL; @@ -691,7 +690,7 @@ static int doCmds(func_args* args) do { err = WS_SUCCESS; - if ((ret = wolfSSH_SFTP_MKDIR(ssh, pt, &atrb)) != WS_SUCCESS) { + if ((ret = wolfSSH_SFTP_MKDIR(ssh, pt, NULL)) != WS_SUCCESS) { err = wolfSSH_get_error(ssh); if (ret == WS_PERMISSIONS) { if (SFTP_FPUTS(args, "Insufficient permissions\n") < 0) { diff --git a/src/wolfsftp.c b/src/wolfsftp.c index d70704e0..57bd70c1 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -9032,6 +9032,7 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz, int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr) { struct WS_SFTP_MKDIR_STATE* state; + WS_SFTP_FILEATRB defAtr; int ret; byte type; @@ -9058,13 +9059,23 @@ int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr) if (wolfSSH_SFTP_buffer_size(&state->buffer) == 0) { /* packet not created yet */ int sz = (int)WSTRLEN(dir); + int atrSz; + + if (atr == NULL) { + WMEMSET(&defAtr, 0, sizeof(defAtr)); + defAtr.flags = WOLFSSH_FILEATRB_PERM; + defAtr.per = 0777; + atr = &defAtr; + } + atrSz = SFTP_AttributesSz(ssh, atr); + if (wolfSSH_SFTP_buffer_create(ssh, &state->buffer, sz + - WOLFSSH_SFTP_HEADER + UINT32_SZ * 3) != 0) { + WOLFSSH_SFTP_HEADER + UINT32_SZ + atrSz) != 0) { return WS_MEMORY_E; } if (SFTP_SetHeader(ssh, ssh->reqId, WOLFSSH_FTP_MKDIR, - sz + UINT32_SZ * 3, + sz + UINT32_SZ + atrSz, wolfSSH_SFTP_buffer_data(&state->buffer)) != WS_SUCCESS) { return WS_FATAL_ERROR; } @@ -9078,11 +9089,11 @@ int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr) wolfSSH_SFTP_buffer_seek(&state->buffer, wolfSSH_SFTP_buffer_idx(&state->buffer), sz); - wolfSSH_SFTP_buffer_c32toa(&state->buffer, UINT32_SZ); - - /* @TODO handle setting attributes */ - WOLFSSH_UNUSED(atr); - wolfSSH_SFTP_buffer_c32toa(&state->buffer, 0777); + SFTP_SetAttributes(ssh, + wolfSSH_SFTP_buffer_data(&state->buffer) + + wolfSSH_SFTP_buffer_idx(&state->buffer), atrSz, atr); + wolfSSH_SFTP_buffer_seek(&state->buffer, + wolfSSH_SFTP_buffer_idx(&state->buffer), atrSz); ret = wolfSSH_SFTP_buffer_set_size(&state->buffer, wolfSSH_SFTP_buffer_idx(&state->buffer)); diff --git a/tests/api.c b/tests/api.c index 38884f34..141f6d4c 100644 --- a/tests/api.c +++ b/tests/api.c @@ -7594,8 +7594,94 @@ static void test_wolfSSH_SFTP_GetResume(void) } +/* MKDIR sends the caller's attributes: the in-process echoserver creates the + * directory with the requested permissions. */ +static void test_wolfSSH_SFTP_MKDIR_attrs(void) +{ +#if !defined(USE_WINDOWS_API) && !defined(WOLFSSH_FATFS) && \ + !defined(WOLFSSH_ZEPHYR) + func_args ser; + tcp_ready ready; + int argsCount; + WS_SOCKET_T clientFd; + + const char* args[10]; + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + + THREAD_TYPE serThread; + + WS_SFTP_FILEATRB atr; + char dirName[48]; + + /* pid-stamped so concurrent runs in one tree don't share it */ + WSNPRINTF(dirName, sizeof(dirName), "wolfssh_14634_mkdir_%d.tmp", + (int)getpid()); + WMEMSET(&ser, 0, sizeof(func_args)); + + argsCount = 0; + args[argsCount++] = "."; + args[argsCount++] = "-1"; + args[argsCount++] = "-p"; + args[argsCount++] = "0"; + ser.argv = (char**)args; + ser.argc = argsCount; + ser.signal = &ready; + InitTcpReady(ser.signal); + ThreadStart(echoserver_test, (void*)&ser, &serThread); + WaitTcpReady(&ready); + + sftp_client_connect(&ctx, &ssh, ready.port); + AssertNotNull(ctx); + AssertNotNull(ssh); + + (void)wolfSSH_SFTP_RMDIR(ssh, dirName); /* normally absent */ + + /* A private directory, which the fixed 0777 request never produced. */ + WMEMSET(&atr, 0, sizeof(atr)); + atr.flags = WOLFSSH_FILEATRB_PERM; + atr.per = 0700; + AssertIntEQ(wolfSSH_SFTP_MKDIR(ssh, dirName, &atr), WS_SUCCESS); + WMEMSET(&atr, 0, sizeof(atr)); + AssertIntEQ(wolfSSH_SFTP_STAT(ssh, dirName, &atr), WS_SUCCESS); + AssertTrue((atr.flags & WOLFSSH_FILEATRB_PERM) != 0); + AssertIntEQ(atr.per & 0777, 0700); + AssertIntEQ(wolfSSH_SFTP_RMDIR(ssh, dirName), WS_SUCCESS); + + /* A NULL atr still makes a directory the owner can use. */ + AssertIntEQ(wolfSSH_SFTP_MKDIR(ssh, dirName, NULL), WS_SUCCESS); + WMEMSET(&atr, 0, sizeof(atr)); + AssertIntEQ(wolfSSH_SFTP_STAT(ssh, dirName, &atr), WS_SUCCESS); + AssertIntEQ(atr.per & 0700, 0700); + AssertIntEQ(wolfSSH_SFTP_RMDIR(ssh, dirName), WS_SUCCESS); + + /* take care of re-keying state before shutdown call */ + while (wolfSSH_get_error(ssh) == WS_REKEYING) { + wolfSSH_worker(ssh, NULL); + } + + argsCount = AbsorbBenignReset(ssh, wolfSSH_shutdown(ssh)); +#if DEFAULT_HIGHWATER_MARK < 8000 + if (argsCount == WS_REKEYING) { + argsCount = WS_SUCCESS; + } +#endif + AssertIntEQ(argsCount, WS_SUCCESS); + + clientFd = wolfSSH_get_fd(ssh); + WCLOSESOCKET(clientFd); + + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + ThreadJoin(serThread); + FreeTcpReady(&ready); +#endif /* !USE_WINDOWS_API && !WOLFSSH_FATFS && !WOLFSSH_ZEPHYR */ +} + + #else /* WOLFSSH_SFTP && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED */ static void test_wolfSSH_SFTP_SendReadPacket(void) { ; } +static void test_wolfSSH_SFTP_MKDIR_attrs(void) { ; } static void test_wolfSSH_SFTP_PartialSend(void) { ; } static void test_wolfSSH_SFTP_ReKey(void) { ; } static void test_wolfSSH_SFTP_ReKey_NonBlock(void) { ; } @@ -9868,6 +9954,7 @@ int wolfSSH_ApiTest(int argc, char** argv) test_wolfSSH_SFTP_SendPacketType_negotiation_non_block(); test_wolfSSH_SFTP_PutResume(); test_wolfSSH_SFTP_GetResume(); + test_wolfSSH_SFTP_MKDIR_attrs(); /* Either SCP or SFTP */ test_wolfSSH_RealPath(); diff --git a/wolfssh/wolfsftp.h b/wolfssh/wolfsftp.h index 29b9bc84..329f5a17 100644 --- a/wolfssh/wolfsftp.h +++ b/wolfssh/wolfsftp.h @@ -293,6 +293,8 @@ WOLFSSH_API void wolfSSH_SFTP_Interrupt(WOLFSSH* ssh); /* functions used for commands */ WOLFSSH_API int wolfSSH_SFTP_Remove(WOLFSSH* ssh, char* f); +/* Sends atr with the request; the server picks the mode when atr carries no + * permissions. A NULL atr requests permissions 0777. */ WOLFSSH_API int wolfSSH_SFTP_MKDIR(WOLFSSH* ssh, char* dir, WS_SFTP_FILEATRB* atr); WOLFSSH_API int wolfSSH_SFTP_RMDIR(WOLFSSH* ssh, char* dir);