Skip to content

Commit 33b1c3a

Browse files
committed
Fix double-free on wolfSSH_SFTPNAME_readdir
The filename of the `WS_SFTPNAME` could be freed in this function upon an error, but it is not set to `NULL`, so when `wolfSSH_SFTPNAME_free` is called, a double-free occurs. Found when working on ZD 16290.
1 parent e0a1bdd commit 33b1c3a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/wolfsftp.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121

22+
#include <strings.h>
2223
#ifdef HAVE_CONFIG_H
2324
#include <config.h>
2425
#endif
@@ -3228,12 +3229,16 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out,
32283229
>= (int)sizeof(r)) {
32293230
WLOG(WS_LOG_SFTP, "Path length too large");
32303231
WFREE(out->fName, out->heap, DYNTYPE_SFTP);
3232+
out->fName = NULL;
3233+
out->fSz = 0;
32313234
return WS_FATAL_ERROR;
32323235
}
32333236

32343237
if (wolfSSH_RealPath(ssh->sftpDefaultPath, r, s, sizeof(s)) < 0) {
32353238
WFREE(out->fName, out->heap, DYNTYPE_SFTP);
32363239
WLOG(WS_LOG_SFTP, "Error cleaning path to get attributes");
3240+
out->fName = NULL;
3241+
out->fSz = 0;
32373242
return WS_FATAL_ERROR;
32383243
}
32393244

@@ -3248,6 +3253,8 @@ static int wolfSSH_SFTPNAME_readdir(WOLFSSH* ssh, WDIR* dir, WS_SFTPNAME* out,
32483253
if (SFTP_CreateLongName(out) != WS_SUCCESS) {
32493254
WLOG(WS_LOG_DEBUG, "Error creating long name for %s", out->fName);
32503255
WFREE(out->fName, out->heap, DYNTYPE_SFTP);
3256+
out->fName = NULL;
3257+
out->fSz = 0;
32513258
return WS_FATAL_ERROR;
32523259
}
32533260

@@ -3976,7 +3983,7 @@ int wolfSSH_SFTP_RecvClose(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
39763983
if (sz == sizeof(WFD)) {
39773984
WMEMSET((byte*)&fd, 0, sizeof(WFD));
39783985
WMEMCPY((byte*)&fd, data + idx, sz);
3979-
3986+
39803987
#ifdef MICROCHIP_MPLAB_HARMONY
39813988
ret = WFCLOSE(ssh->fs, &fd);
39823989
#else
@@ -5003,7 +5010,7 @@ int SFTP_GetAttributes(void* fs, const char* fileName, WS_SFTP_FILEATRB* atr,
50035010
{
50045011
WOLFSSH_UNUSED(heap);
50055012
WOLFSSH_UNUSED(fs);
5006-
5013+
50075014
return SFTP_GetAttributesHelper(atr, fileName);
50085015
}
50095016

@@ -5028,7 +5035,7 @@ int SFTP_GetAttributes_Handle(WOLFSSH* ssh, byte* handle, int handleSz,
50285035
WLOG(WS_LOG_SFTP, "Unknown handle");
50295036
return WS_BAD_FILE_E;
50305037
}
5031-
5038+
50325039
return SFTP_GetAttributesHelper(atr, cur->name);
50335040
}
50345041

@@ -8844,7 +8851,7 @@ int wolfSSH_SFTP_Get(WOLFSSH* ssh, char* from,
88448851
if (state->gOfst[0] > 0 || state->gOfst[1] > 0)
88458852
ret = WFOPEN(ssh->fs, &state->fl, to, WOLFSSH_O_APPEND);
88468853
else
8847-
ret = WFOPEN(ssh->fs, &state->fl, to, WOLFSSH_O_WRONLY);
8854+
ret = WFOPEN(ssh->fs, &state->fl, to, WOLFSSH_O_WRONLY);
88488855
#elif defined(USE_WINDOWS_API)
88498856
{
88508857
DWORD desiredAccess = GENERIC_WRITE;

0 commit comments

Comments
 (0)