Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions examples/echoserver/echoserver.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx)

if (action == WOLFSSH_AGENT_LOCAL_SETUP) {
struct sockaddr_un* name = &ctx->name;
size_t size;
int envSet = 0;

WMEMSET(name, 0, sizeof(struct sockaddr_un));
ctx->pid = getpid();
Expand All @@ -391,33 +391,37 @@ static int wolfSSH_AGENT_DefaultActions(WS_AgentCbAction action, void* vCtx)
ret = snprintf(name->sun_path, sizeof(name->sun_path),
"/tmp/wolfserver.%d", ctx->pid);

if (ret == 0) {
if (ret > 0) {
name->sun_path[sizeof(name->sun_path) - 1] = '\0';
size = WSTRLEN(name->sun_path) +
offsetof(struct sockaddr_un, sun_path);
ctx->listenFd = socket(AF_UNIX, SOCK_STREAM, 0);
if (ctx->listenFd == -1) {
ret = -1;
}
ret = (ctx->listenFd == -1) ? -1 : 0;
}

if (ret == 0) {
ret = bind(ctx->listenFd,
(struct sockaddr *)name, (socklen_t)size);
ret = bind(ctx->listenFd, (struct sockaddr *)name,
(socklen_t)sizeof(struct sockaddr_un));
Comment on lines 400 to +402
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If socket() succeeds but bind()/setenv()/listen() fails, the code returns an error without closing ctx->listenFd or removing any partially-created UNIX socket path/env var. Since the agent callback return value is not always checked by the library, this can leak FDs and leave stale /tmp/wolfserver. sockets on failure. Consider adding an error-cleanup path that closes ctx->listenFd (and unlinks/unsets as applicable) before returning.

Copilot uses AI. Check for mistakes.
}

if (ret == 0) {
ret = setenv(EnvNameAuthPort, name->sun_path, 1);
}

if (ret == 0) {
envSet = 1;
ret = listen(ctx->listenFd, 5);
}

if (ret == 0) {
ctx->state = AGENT_STATE_LISTEN;
}
else {
if (envSet) {
unsetenv(EnvNameAuthPort);
}
if (ctx->listenFd >= 0) {
close(ctx->listenFd);
ctx->listenFd = -1;
}
ret = WS_AGENT_SETUP_E;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ static int PostLock(WOLFSSH_AGENT_CTX* agent,
word32 ppSz;

WLOG(WS_LOG_AGENT, "Posting lock to agent %p", agent);
WOLFSSH_UNUSED(agent);

ppSz = sizeof(pp) - 1;
if (passphraseSz < ppSz)
Expand All @@ -395,6 +396,7 @@ static int PostUnlock(WOLFSSH_AGENT_CTX* agent,
word32 ppSz;

WLOG(WS_LOG_AGENT, "Posting unlock to agent %p", agent);
WOLFSSH_UNUSED(agent);

ppSz = sizeof(pp) - 1;
if (passphraseSz < ppSz)
Expand Down
2 changes: 1 addition & 1 deletion src/ssh.c
Original file line number Diff line number Diff line change
Expand Up @@ -2605,7 +2605,7 @@ int wolfSSH_worker(WOLFSSH* ssh, word32* channelId)
}
#endif /* WOLFSSH_TEST_BLOCK */

if (ret == WS_SUCCESS) {
if (ret == WS_SUCCESS || ret == WS_CHAN_RXD) {
if (channelId != NULL) {
*channelId = ssh->lastRxId;
}
Expand Down