From 3d6dfb350444194390d280dd678213a283fd97bb Mon Sep 17 00:00:00 2001 From: Fabio Alemagna <507164+falemagn@users.noreply.github.com> Date: Mon, 17 Apr 2023 07:25:28 +0200 Subject: [PATCH 1/3] WOLFSSH_FTP_FSETSTAT --- src/wolfsftp.c | 127 ++++++++++++++++++++++++++++++++++++++++++++- wolfssh/port.h | 5 ++ wolfssh/wolfsftp.h | 2 + 3 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 47a2ac43a..5aa24c7fc 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -1416,6 +1416,11 @@ int wolfSSH_SFTP_read(WOLFSSH* ssh) wolfSSH_SFTP_buffer_data(&state->buffer), wolfSSH_SFTP_buffer_size(&state->buffer)); break; + case WOLFSSH_FTP_FSETSTAT: + ret = wolfSSH_SFTP_RecvFSetSTAT(ssh, state->reqId, + wolfSSH_SFTP_buffer_data(&state->buffer), + wolfSSH_SFTP_buffer_size(&state->buffer)); + break; #endif #ifndef NO_WOLFSSH_DIR @@ -4856,8 +4861,10 @@ static int SFTP_SetFileAttributes(WOLFSSH* ssh, char* name, WS_SFTP_FILEATRB* at #endif /* check if time attribute present */ - if (atr->flags & WOLFSSH_FILEATRB_TIME) { - /* @TODO set time */ + if (ret == WS_SUCCESS && (atr->flags & WOLFSSH_FILEATRB_TIME)) { + if (WSETTIME(ssh->fs, name, atr->atime, atr->mtime) != 0) { + ret = WS_BAD_FILE_E; + } } /* check if extended attributes are present */ @@ -4870,6 +4877,47 @@ static int SFTP_SetFileAttributes(WOLFSSH* ssh, char* name, WS_SFTP_FILEATRB* at } +/* sets a files attributes + * returns WS_SUCCESS on success */ +static int SFTP_SetFileAttributesHandle(WOLFSSH* ssh, WFD handle, WS_SFTP_FILEATRB* atr) +{ + int ret = WS_SUCCESS; + + /* check if size attribute present */ + if (atr->flags & WOLFSSH_FILEATRB_SIZE) { + /* @TODO set file size */ + } + + /* check if uid and gid attribute present */ + if (atr->flags & WOLFSSH_FILEATRB_UIDGID) { + /* @TODO set group and user id */ + } + +#ifndef USE_WINDOWS_API + /* check if permissions attribute present */ + if (atr->flags & WOLFSSH_FILEATRB_PERM) { + if (WFCHMOD(ssh->fs, handle, atr->per) != 0) { + ret = WS_BAD_FILE_E; + } + } +#endif + + /* check if time attribute present */ + if (ret == WS_SUCCESS && (atr->flags & WOLFSSH_FILEATRB_TIME)) { + if (WFSETTIME(ssh->fs, handle, atr->atime, atr->mtime) != 0) { + ret = WS_BAD_FILE_E; + } + } + + /* check if extended attributes are present */ + if (atr->flags & WOLFSSH_FILEATRB_EXT) { + /* @TODO handle extensions */ + } + + (void)ssh; + return ret ; +} + /* Handles a packet sent to set attributes of path * * returns WS_SUCCESS on success @@ -4946,6 +4994,81 @@ int wolfSSH_SFTP_RecvSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) return ret; } + +/* Handles a packet sent to set attributes of file handle + * + * returns WS_SUCCESS on success + */ +int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) +{ + WS_SFTP_FILEATRB atr; + int ret = WS_SUCCESS; + + WFD fd; + word32 sz; + word32 idx = 0; + + byte* out = NULL; + word32 outSz = 0; + + char suc[] = "Set Attirbutes"; + char ser[] = "Unable to set attributes error"; + char per[] = "Unable to parse attributes error"; + char* res = suc; + byte type = WOLFSSH_FTP_OK; + + if (ssh == NULL) { + return WS_BAD_ARGUMENT; + } + + WLOG(WS_LOG_SFTP, "Receiving WOLFSSH_FTP_FSETSTAT"); + + /* get file handle */ + ato32(data + idx, &sz); idx += UINT32_SZ; + if (sz + idx > maxSz || sz > WOLFSSH_MAX_HANDLE) { + return WS_BUFFER_E; + } + WMEMSET((byte*)&fd, 0, sizeof(WFD)); + WMEMCPY((byte*)&fd, data + idx, sz); idx += sz; + + if (ret == WS_SUCCESS && + SFTP_ParseAtributes_buffer(ssh, &atr, data, &idx, maxSz) != 0) { + type = WOLFSSH_FTP_FAILURE; + res = per; + ret = WS_BAD_FILE_E; + } + + + + /* try to set file attributes and send status back to client */ + if (ret == WS_SUCCESS && (ret = SFTP_SetFileAttributesHandle(ssh, fd, &atr)) + != WS_SUCCESS) { + /* tell peer that was not ok */ + WLOG(WS_LOG_SFTP, "Unable to get set attributes of open file"); + type = WOLFSSH_FTP_FAILURE; + res = ser; + ret = WS_BAD_FILE_E; + } + + if (wolfSSH_SFTP_CreateStatus(ssh, type, reqId, res, "English", NULL, + &outSz) != WS_SIZE_ONLY) { + return WS_FATAL_ERROR; + } + out = (byte*)WMALLOC(outSz, ssh->ctx->heap, DYNTYPE_BUFFER); + if (out == NULL) { + return WS_MEMORY_E; + } + if (wolfSSH_SFTP_CreateStatus(ssh, type, reqId, res, "English", out, + &outSz) != WS_SUCCESS) { + WFREE(out, ssh->ctx->heap, DYNTYPE_BUFFER); + return WS_FATAL_ERROR; + } + + /* set send out buffer, "out" is taken by ssh */ + wolfSSH_SFTP_RecvSetSend(ssh, out, outSz); + return ret; +} + #endif /* _WIN32_WCE */ #endif /* !NO_WOLFSSH_SERVER */ diff --git a/wolfssh/port.h b/wolfssh/port.h index ddbd7ed11..0603cee1f 100644 --- a/wolfssh/port.h +++ b/wolfssh/port.h @@ -364,6 +364,9 @@ extern "C" { #define WREWIND(s) rewind((s)) #define WSEEK_END SEEK_END #define WBADFILE NULL + #define WSETTIME(fs,f,a,m) (0) + #define WFSETTIME(fs,fd,a,m) (0) + #ifdef WOLFSSL_VXWORKS #define WUTIMES(f,t) (WS_SUCCESS) #else @@ -372,8 +375,10 @@ extern "C" { #ifndef USE_WINDOWS_API #define WCHMOD(fs,f,m) chmod((f),(m)) + #define WFCHMOD(fs,fd,m) fchmod((fd),(m)) #else #define WCHMOD(fs,f,m) _chmod((f),(m)) + #define WFCHMOD(fs,fd,m) _fchmod((fd),(m)) #endif #if (defined(WOLFSSH_SCP) || \ diff --git a/wolfssh/wolfsftp.h b/wolfssh/wolfsftp.h index cdb919e58..5391ffdbf 100644 --- a/wolfssh/wolfsftp.h +++ b/wolfssh/wolfsftp.h @@ -254,6 +254,8 @@ WOLFSSH_LOCAL int wolfSSH_SFTP_RecvSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz); WOLFSSH_LOCAL int wolfSSH_SFTP_RecvFSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz); +WOLFSSH_API int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, + word32 maxSz); #ifndef NO_WOLFSSH_DIR WOLFSSH_LOCAL int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, From cc5a037f649c6d333172e6009c570b0a4b7b95ce Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Tue, 8 Aug 2023 13:52:47 -0600 Subject: [PATCH 2/3] make new internal function use WOLFSSL_LOCAL --- wolfssh/wolfsftp.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wolfssh/wolfsftp.h b/wolfssh/wolfsftp.h index 5391ffdbf..b0272b2d6 100644 --- a/wolfssh/wolfsftp.h +++ b/wolfssh/wolfsftp.h @@ -254,7 +254,7 @@ WOLFSSH_LOCAL int wolfSSH_SFTP_RecvSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz); WOLFSSH_LOCAL int wolfSSH_SFTP_RecvFSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz); -WOLFSSH_API int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, +WOLFSSH_LOCAL int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz); #ifndef NO_WOLFSSH_DIR From ef394908a7d716c9fd807500e70dd05e17bee67d Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 15 Sep 2023 15:35:35 -0600 Subject: [PATCH 3/3] additional sanity check on input buffer size --- src/wolfsftp.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 388188f6d..89f37fed3 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -5174,6 +5174,11 @@ int wolfSSH_SFTP_RecvFSetSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz) WLOG(WS_LOG_SFTP, "Receiving WOLFSSH_FTP_FSETSTAT"); + if (maxSz < UINT32_SZ) { + /* not enough for an ato32 call */ + return WS_BUFFER_E; + } + /* get file handle */ ato32(data + idx, &sz); idx += UINT32_SZ; if (sz + idx > maxSz || sz > WOLFSSH_MAX_HANDLE) {