Skip to content

Commit 1f7957b

Browse files
committed
Coverity: Resource leak
1. Fix some resource leaks during error conditions where a socket or a file descriptor doesn't get closed in all error cases. Fixes CIDs: 572856 572902 573012 573019 573021 573076
1 parent 0724553 commit 1f7957b

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

apps/wolfsshd/wolfsshd.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap)
245245
word32 fileSz;
246246
word32 readSz;
247247

248+
WOLFSSH_UNUSED(heap);
249+
248250
if (fileName == NULL) return NULL;
249251

250252
if (WFOPEN(NULL, &file, fileName, "rb") != 0)
@@ -263,10 +265,9 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap)
263265
}
264266
if (bufSz)
265267
*bufSz = readSz;
266-
WFCLOSE(NULL, file);
267268
}
269+
WFCLOSE(NULL, file);
268270

269-
(void)heap;
270271
return buf;
271272
}
272273
#endif /* NO_FILESYSTEM */

src/wolfsftp.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,29 +2111,37 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
21112111
WLOG(WS_LOG_SFTP, "Unable to store handle");
21122112
res = ier;
21132113
if (wolfSSH_SFTP_CreateStatus(ssh, WOLFSSH_FTP_FAILURE, reqId, res,
2114-
"English", NULL, &outSz) != WS_SIZE_ONLY) {
2114+
"English", NULL, &outSz) != WS_SIZE_ONLY) {
2115+
WCLOSE(ssh->fs, fd);
21152116
return WS_FATAL_ERROR;
21162117
}
21172118
ret = WS_FATAL_ERROR;
21182119
}
21192120
}
21202121
#endif
21212122

2122-
/* create packet */
2123-
out = (byte*)WMALLOC(outSz, ssh->ctx->heap, DYNTYPE_BUFFER);
2124-
if (out == NULL) {
2125-
return WS_MEMORY_E;
2123+
if (ret == WS_SUCCESS) {
2124+
/* create packet */
2125+
out = (byte*)WMALLOC(outSz, ssh->ctx->heap, DYNTYPE_BUFFER);
2126+
if (out == NULL) {
2127+
WCLOSE(ssh->fs, fd);
2128+
return WS_MEMORY_E;
2129+
}
21262130
}
21272131
if (ret == WS_SUCCESS) {
21282132
if (SFTP_CreatePacket(ssh, WOLFSSH_FTP_HANDLE, out, outSz,
21292133
(byte*)&fd, sizeof(WFD)) != WS_SUCCESS) {
2134+
WCLOSE(ssh->fs, fd);
21302135
return WS_FATAL_ERROR;
21312136
}
21322137
}
21332138
else {
21342139
if (wolfSSH_SFTP_CreateStatus(ssh, WOLFSSH_FTP_FAILURE, reqId, res,
21352140
"English", out, &outSz) != WS_SUCCESS) {
21362141
WFREE(out, ssh->ctx->heap, DYNTYPE_BUFFER);
2142+
if (fd >= 0) {
2143+
WCLOSE(ssh->fs, fd);
2144+
}
21372145
return WS_FATAL_ERROR;
21382146
}
21392147
}

tests/api.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,17 @@ static void sftp_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port)
927927

928928
build_addr(&clientAddr, host, port);
929929
tcp_socket(&sockFd, ((struct sockaddr_in *)&clientAddr)->sin_family);
930+
if (sockFd < 0) {
931+
wolfSSH_free(*ssh);
932+
wolfSSH_CTX_free(*ctx);
933+
*ctx = NULL;
934+
*ssh = NULL;
935+
return;
936+
}
937+
930938
ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz);
931939
if (ret != 0){
940+
WCLOSESOCKET(sockFd);
932941
wolfSSH_free(*ssh);
933942
wolfSSH_CTX_free(*ctx);
934943
*ctx = NULL;
@@ -945,6 +954,7 @@ static void sftp_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port)
945954
ret = wolfSSH_SFTP_connect(*ssh);
946955

947956
if (ret != WS_SUCCESS){
957+
WCLOSESOCKET(sockFd);
948958
wolfSSH_free(*ssh);
949959
wolfSSH_CTX_free(*ctx);
950960
*ctx = NULL;
@@ -1611,8 +1621,17 @@ static void keyboard_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port)
16111621

16121622
build_addr(&clientAddr, host, port);
16131623
tcp_socket(&sockFd, ((struct sockaddr_in *)&clientAddr)->sin_family);
1624+
if (sockFd < 0) {
1625+
wolfSSH_free(*ssh);
1626+
wolfSSH_CTX_free(*ctx);
1627+
*ctx = NULL;
1628+
*ssh = NULL;
1629+
return;
1630+
}
1631+
16141632
ret = connect(sockFd, (const struct sockaddr *)&clientAddr, clientAddrSz);
16151633
if (ret != 0){
1634+
WCLOSESOCKET(sockFd);
16161635
wolfSSH_free(*ssh);
16171636
wolfSSH_CTX_free(*ctx);
16181637
*ctx = NULL;
@@ -1628,6 +1647,7 @@ static void keyboard_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port)
16281647
ret = wolfSSH_connect(*ssh);
16291648

16301649
if (ret != WS_SUCCESS){
1650+
WCLOSESOCKET(sockFd);
16311651
wolfSSH_free(*ssh);
16321652
wolfSSH_CTX_free(*ctx);
16331653
*ctx = NULL;

tests/auth.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ static int basic_client_connect(WOLFSSH_CTX** ctx, WOLFSSH** ssh, int port)
443443
wolfSSH_CTX_free(*ctx);
444444
*ctx = NULL;
445445
*ssh = NULL;
446+
WCLOSESOCKET(sockFd);
446447
return ret;
447448
}
448449

0 commit comments

Comments
 (0)