SSH Port Forwarding Redux

1. Fixed a leak from a commit earlier in this branch.
2. Freed the correct address name when a forwarded connection closes.
3. Only muck about with the childFd when using the shell.
4. Treat the error code WS_CHANNEL_CLOSED as status, not a failure.
pull/343/head
John Safranek 2021-10-06 13:06:08 -07:00
parent d66fb26158
commit fde6ce4550
No known key found for this signature in database
GPG Key ID: 8CE817DE0D3CCB4A
2 changed files with 34 additions and 27 deletions

View File

@ -846,10 +846,10 @@ static int ssh_worker(thread_ctx_t* threadCtx)
to listening. */
WCLOSESOCKET(fwdFd);
fwdFd = -1;
if (threadCtx->fwdCbCtx.hostName != NULL) {
WFREE(threadCtx->fwdCbCtx.hostName,
if (threadCtx->fwdCbCtx.originName != NULL) {
WFREE(threadCtx->fwdCbCtx.originName,
NULL, 0);
threadCtx->fwdCbCtx.hostName = NULL;
threadCtx->fwdCbCtx.originName = NULL;
}
threadCtx->fwdCbCtx.state = FWD_STATE_LISTEN;
#endif
@ -866,29 +866,32 @@ static int ssh_worker(thread_ctx_t* threadCtx)
}
#ifdef WOLFSSH_SHELL
if (FD_ISSET(childFd, &readFds)) {
cnt_r = (int)read(childFd,
threadCtx->shellBuffer,
sizeof threadCtx->shellBuffer);
if (cnt_r < 0) {
int err = errno;
if (err != EAGAIN) {
#ifdef SHELL_DEBUG
printf("Break:read childFd returns %d: errno =%x\n",
cnt_r, err);
#endif
break;
}
}
else {
#ifdef SHELL_DEBUG
buf_dump(threadCtx->shellBuffer, cnt_r);
#endif
if (cnt_r > 0) {
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
threadCtx->shellBuffer, cnt_r);
if (cnt_w < 0)
if (!threadCtx->echo) {
if (FD_ISSET(childFd, &readFds)) {
cnt_r = (int)read(childFd,
threadCtx->shellBuffer,
sizeof threadCtx->shellBuffer);
if (cnt_r < 0) {
int err = errno;
if (err != EAGAIN) {
#ifdef SHELL_DEBUG
printf("Break:read childFd returns %d: "
"errno =%x\n",
cnt_r, err);
#endif
break;
}
}
else {
#ifdef SHELL_DEBUG
buf_dump(threadCtx->shellBuffer, cnt_r);
#endif
if (cnt_r > 0) {
cnt_w = wolfSSH_ChannelIdSend(ssh, shellChannelId,
threadCtx->shellBuffer, cnt_r);
if (cnt_w < 0)
break;
}
}
}
}
@ -1088,7 +1091,8 @@ static int ssh_worker(thread_ctx_t* threadCtx)
#endif
}
#ifdef WOLFSSH_SHELL
WCLOSESOCKET(childFd);
if (!threadCtx->echo)
WCLOSESOCKET(childFd);
#endif
}

View File

@ -4773,6 +4773,9 @@ static int DoGlobalRequestFwd(WOLFSSH* ssh,
}
}
if (bindAddr != NULL)
WFREE(bindAddr, ssh->ctx->heap, DYNTYPE_STRING);
WLOG(WS_LOG_DEBUG, "Leaving DoGlobalRequestFwd(), ret = %d", ret);
return ret;
}
@ -6241,7 +6244,7 @@ int DoReceive(WOLFSSH* ssh)
case PROCESS_PACKET:
ret = DoPacket(ssh);
ssh->error = ret;
if (ret < 0 && ret != WS_CHAN_RXD) {
if (ret < 0 && !(ret == WS_CHAN_RXD || ret == WS_CHANNEL_CLOSED)) {
return WS_FATAL_ERROR;
}
WLOG(WS_LOG_DEBUG, "PR3: peerMacSz = %u", peerMacSz);