From f8de131e81893768b4528ebccf5767934e7d60a1 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Fri, 13 Oct 2023 16:56:17 -0600 Subject: [PATCH 1/3] fix for shared socket ID between connections --- apps/wolfsshd/auth.c | 2 +- apps/wolfsshd/wolfsshd.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/apps/wolfsshd/auth.c b/apps/wolfsshd/auth.c index 4da1fb8d8..136e75cd8 100644 --- a/apps/wolfsshd/auth.c +++ b/apps/wolfsshd/auth.c @@ -762,7 +762,7 @@ static int CheckPasswordWIN(const char* usr, const byte* pw, word32 pwSz, WOLFSS usrWSz = WSTRLEN(usr) * sizeof(WCHAR); - usrW = (WCHAR*)WMALLOC(usrWSz + 1, authCtx->heap, DYNTYPE_SSHD); + usrW = (WCHAR*)WMALLOC((usrWSz * sizeof(WCHAR)) + sizeof(WCHAR), authCtx->heap, DYNTYPE_SSHD); if (usrW == NULL) { wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Ran out of memory"); ret = WSSHD_AUTH_FAILURE; diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index db48ea8d5..e61ac30ed 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -1671,6 +1671,7 @@ static void* HandleConnection(void* arg) WCLOSESOCKET(conn->fd); } wolfSSH_Log(WS_LOG_INFO, "[SSHD] Return from closing connection = %d", ret); + WFREE(conn, NULL, DYNTYPE_SSHD); #ifdef _WIN32 return 0; @@ -1966,7 +1967,11 @@ static int StartSSHD(int argc, char** argv) break; #else ShowUsage(); + #ifndef _WIN32 return WS_FATAL_ERROR; + #else + return; + #endif #endif case 't': @@ -2144,27 +2149,34 @@ static int StartSSHD(int argc, char** argv) #endif /* wait for incoming connections and fork them off */ while (ret == WS_SUCCESS && quit == 0) { - WOLFSSHD_CONNECTION conn; + WOLFSSHD_CONNECTION* conn; #ifdef WOLFSSL_NUCLEUS struct addr_struct clientAddr; #else SOCKADDR_IN_T clientAddr; socklen_t clientAddrSz = sizeof(clientAddr); #endif - conn.auth = auth; - conn.listenFd = (int)listenFd; - conn.isThreaded = isDaemon; + conn = (WOLFSSHD_CONNECTION*)WMALLOC(sizeof(WOLFSSHD_CONNECTION), NULL, DYNTYPE_SSHD); + if (conn == NULL) { + wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failed to malloc memory for connection"); + ret = WS_MEMORY_E; + break; + } + + conn->auth = auth; + conn->listenFd = (int)listenFd; + conn->isThreaded = isDaemon; /* wait for a connection */ if (PendingConnection(listenFd)) { - conn.ctx = ctx; + conn->ctx = ctx; #ifdef WOLFSSL_NUCLEUS - conn.fd = NU_Accept(listenFd, &clientAddr, 0); + conn->fd = NU_Accept(listenFd, &clientAddr, 0); #else - conn.fd = (int)accept(listenFd, (struct sockaddr*)&clientAddr, + conn->fd = (int)accept(listenFd, (struct sockaddr*)&clientAddr, &clientAddrSz); - if (conn.fd >= 0) { - inet_ntop(AF_INET, &clientAddr.sin_addr, conn.ip, + if (conn->fd >= 0) { + inet_ntop(AF_INET, &clientAddr.sin_addr, conn->ip, INET_ADDRSTRLEN); } #endif @@ -2172,7 +2184,7 @@ static int StartSSHD(int argc, char** argv) { #ifdef USE_WINDOWS_API unsigned long blocking = 1; - if (ioctlsocket(conn.fd, FIONBIO, &blocking) + if (ioctlsocket(conn->fd, FIONBIO, &blocking) == SOCKET_ERROR) err_sys("ioctlsocket failed"); #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) \ @@ -2180,7 +2192,7 @@ static int StartSSHD(int argc, char** argv) defined(WOLFSSL_NUCLEUS) /* non blocking not supported, for now */ #else - int flags = fcntl(conn.fd, F_GETFL, 0); + int flags = fcntl(conn->fd, F_GETFL, 0); if (flags < 0) err_sys("fcntl get failed"); flags = fcntl(conn.fd, F_SETFL, flags | O_NONBLOCK); @@ -2188,7 +2200,7 @@ static int StartSSHD(int argc, char** argv) err_sys("fcntl set failed"); #endif } - ret = NewConnection(&conn); + ret = NewConnection(conn); } #ifdef _WIN32 /* check if service has been shutdown */ @@ -2258,7 +2270,7 @@ int main(int argc, char** argv) } } else { - StartSSHD(argc, (LPSTR*)argv); + StartSSHD(argc, (LPTSTR*)argv); } return 0; #else From 46d2017d7630b3acdc56ca1ba7ac096854bc0f61 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Mon, 16 Oct 2023 07:57:50 -0600 Subject: [PATCH 2/3] fix reference to struct --- apps/wolfsshd/wolfsshd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index e61ac30ed..bf3ef8c3e 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2195,7 +2195,7 @@ static int StartSSHD(int argc, char** argv) int flags = fcntl(conn->fd, F_GETFL, 0); if (flags < 0) err_sys("fcntl get failed"); - flags = fcntl(conn.fd, F_SETFL, flags | O_NONBLOCK); + flags = fcntl(conn->fd, F_SETFL, flags | O_NONBLOCK); if (flags < 0) err_sys("fcntl set failed"); #endif From e3637574dd6e6000cee9da5d69d444e35a2b04eb Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Mon, 16 Oct 2023 09:16:34 -0600 Subject: [PATCH 3/3] remove unused ret value --- apps/wolfsshd/wolfsshd.c | 1 - 1 file changed, 1 deletion(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index bf3ef8c3e..003d112be 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -2159,7 +2159,6 @@ static int StartSSHD(int argc, char** argv) conn = (WOLFSSHD_CONNECTION*)WMALLOC(sizeof(WOLFSSHD_CONNECTION), NULL, DYNTYPE_SSHD); if (conn == NULL) { wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Failed to malloc memory for connection"); - ret = WS_MEMORY_E; break; }