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
pull/1394/head
John Safranek 2026-11-02 14:58:07 -07:00 committed by philljj
parent 8fd8f3fd15
commit 57dfc44c93
4 changed files with 108 additions and 9 deletions

View File

@ -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) {

View File

@ -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));

View File

@ -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();

View File

@ -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);