Skip to content

Commit

Permalink
fix for shared socket ID between connections
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobBarthelmeh committed Oct 13, 2023
1 parent 7d81671 commit f8de131
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/wolfsshd/auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 25 additions & 13 deletions apps/wolfsshd/wolfsshd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -2144,51 +2149,58 @@ 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

{
#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) \
|| defined (WOLFSSL_TIRTOS)|| defined(WOLFSSL_VXWORKS) || \
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);
if (flags < 0)
err_sys("fcntl set failed");
#endif
}
ret = NewConnection(&conn);
ret = NewConnection(conn);
}
#ifdef _WIN32
/* check if service has been shutdown */
Expand Down Expand Up @@ -2258,7 +2270,7 @@ int main(int argc, char** argv)
}
}
else {
StartSSHD(argc, (LPSTR*)argv);
StartSSHD(argc, (LPTSTR*)argv);
}
return 0;
#else
Expand Down

0 comments on commit f8de131

Please sign in to comment.