remove possible double free and fix non blocking echoserver

pull/134/head
Jacob Barthelmeh 2019-01-17 17:22:41 -07:00
parent 19a4ce6403
commit 61a41b5b7b
2 changed files with 28 additions and 12 deletions

View File

@ -127,6 +127,20 @@ static int ssh_worker(thread_ctx_t* threadCtx) {
buf = tmpBuf; buf = tmpBuf;
if (!stop) { if (!stop) {
if (threadCtx->nonBlock) {
SOCKET_T sockfd;
int select_ret = 0;
sockfd = (SOCKET_T)wolfSSH_get_fd(threadCtx->ssh);
select_ret = tcp_select(sockfd, 1);
if (select_ret != WS_SELECT_RECV_READY &&
select_ret != WS_SELECT_ERROR_READY &&
select_ret != WS_SELECT_TIMEOUT) {
break;
}
}
rxSz = wolfSSH_stream_read(threadCtx->ssh, rxSz = wolfSSH_stream_read(threadCtx->ssh,
buf + backlogSz, buf + backlogSz,
EXAMPLE_BUFFER_SZ); EXAMPLE_BUFFER_SZ);
@ -161,16 +175,26 @@ static int ssh_worker(thread_ctx_t* threadCtx) {
} }
txSum += txSz; txSum += txSz;
} }
else if (txSz != WS_REKEYING) else if (txSz != WS_REKEYING) {
stop = 1; int error = wolfSSH_get_error(threadCtx->ssh);
if (error != WS_WANT_WRITE) {
stop = 1;
}
else {
txSz = 0;
}
}
} }
if (txSum < backlogSz) if (txSum < backlogSz)
memmove(buf, buf + txSum, backlogSz - txSum); memmove(buf, buf + txSum, backlogSz - txSum);
backlogSz -= txSum; backlogSz -= txSum;
} }
else else {
stop = 1; int error = wolfSSH_get_error(threadCtx->ssh);
if (error != WS_WANT_READ)
stop = 1;
}
} }
} while (!stop); } while (!stop);

View File

@ -1421,14 +1421,12 @@ int wolfSSH_SFTP_RecvOpen(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
ato32(data + idx, &sz); idx += UINT32_SZ; ato32(data + idx, &sz); idx += UINT32_SZ;
if (sz + idx > maxSz) { if (sz + idx > maxSz) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_BUFFER_E; return WS_BUFFER_E;
} }
/* plus one to make sure is null terminated */ /* plus one to make sure is null terminated */
dir = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER); dir = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER);
if (dir == NULL) { if (dir == NULL) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_MEMORY_E; return WS_MEMORY_E;
} }
WMEMCPY(dir, data + idx, sz); WMEMCPY(dir, data + idx, sz);
@ -1666,20 +1664,16 @@ int wolfSSH_SFTP_RecvOpenDir(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
ato32(data + idx, &sz); ato32(data + idx, &sz);
idx += UINT32_SZ; idx += UINT32_SZ;
if (sz + idx > maxSz) { if (sz + idx > maxSz) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_BUFFER_E; return WS_BUFFER_E;
} }
/* plus one to make sure is null terminated */ /* plus one to make sure is null terminated */
dirName = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER); dirName = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER);
if (dirName == NULL) { if (dirName == NULL) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_MEMORY_E; return WS_MEMORY_E;
} }
WMEMCPY(dirName, data + idx, sz); WMEMCPY(dirName, data + idx, sz);
dirName[sz] = '\0'; dirName[sz] = '\0';
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
clean_path(dirName); clean_path(dirName);
/* get directory handle */ /* get directory handle */
@ -2519,7 +2513,6 @@ int wolfSSH_SFTP_RecvRemove(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
} }
name = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER); name = (char*)WMALLOC(sz + 1, ssh->ctx->heap, DYNTYPE_BUFFER);
if (name == NULL) { if (name == NULL) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_MEMORY_E; return WS_MEMORY_E;
} }
WMEMCPY(name, data + idx, sz); WMEMCPY(name, data + idx, sz);
@ -3039,7 +3032,6 @@ int wolfSSH_SFTP_RecvFSTAT(WOLFSSH* ssh, int reqId, byte* data, word32 maxSz)
ato32(data + idx, &handleSz); idx += UINT32_SZ; ato32(data + idx, &handleSz); idx += UINT32_SZ;
if (handleSz + idx > maxSz) { if (handleSz + idx > maxSz) {
WFREE(data, ssh->ctx->heap, DYNTYPE_BUFFER);
return WS_BUFFER_E; return WS_BUFFER_E;
} }
handle = data + idx; handle = data + idx;