Skip to content

Commit f8a4fa2

Browse files
committed
wolfsshd: wipe host key copies left behind
The shell child frees its session and the server context before it drops to the user, so the host private key and the session keys are wiped from its image ahead of the exec. It keeps copies of the user name and command it still needs. A short read of a key file wipes the buffer before freeing it. Issue: F-8841, F-14227
1 parent 8c6fc9c commit f8a4fa2

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

‎apps/wolfsshd/wolfsshd.c‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap,
438438
readSz = (word32)WFREAD(NULL, buf, 1, fileSz, file);
439439
if (readSz < (size_t)fileSz) {
440440
WFCLOSE(NULL, file);
441+
/* whatever part of a key was read */
442+
WS_FORCEZERO(buf, (word32)fileSz + 1);
441443
WFREE(buf, heap, DYNTYPE_SSHD);
442444
return NULL;
443445
}
@@ -2962,11 +2964,33 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
29622964
const char *args[] = {"-sh", NULL, NULL, NULL};
29632965
char cmd[MAX_COMMAND_SZ];
29642966
char shell[MAX_COMMAND_SZ];
2967+
char* userName;
29652968
int ret;
29662969

29672970
signal(SIGINT, SIG_DFL);
29682971
signal(SIGCHLD, SIG_DFL);
29692972

2973+
/* The shell needs none of the session, so drop the host key and the
2974+
* session keys this image inherited before giving up root. The
2975+
* command can live in the session, so keep a copy of it. */
2976+
userName = WSTRDUP(wolfSSH_GetUsername(ssh), NULL, DYNTYPE_SSHD);
2977+
if (forcedCmd != NULL) {
2978+
forcedCmd = WSTRDUP(forcedCmd, NULL, DYNTYPE_SSHD);
2979+
if (forcedCmd == NULL) {
2980+
wolfSSH_Log(WS_LOG_ERROR,
2981+
"[SSHD] Error copying forced command");
2982+
exit(1);
2983+
}
2984+
}
2985+
if (userName == NULL) {
2986+
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error copying user name");
2987+
exit(1);
2988+
}
2989+
wolfSSH_free(ssh);
2990+
ssh = NULL;
2991+
wolfSSH_CTX_free(conn->ctx);
2992+
conn->ctx = NULL;
2993+
29702994
if (!ptyReq || forcedCmd) {
29712995
close(stdoutPipe[0]);
29722996
close(stderrPipe[0]);
@@ -2993,7 +3017,7 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
29933017
}
29943018

29953019
/* set additional groups if needed */
2996-
if (wolfSSHD_AuthSetGroups(conn->auth, wolfSSH_GetUsername(ssh),
3020+
if (wolfSSHD_AuthSetGroups(conn->auth, userName,
29973021
pPasswd->pw_gid) != WS_SUCCESS) {
29983022
wolfSSH_Log(WS_LOG_ERROR, "[SSHD] Error setting groups");
29993023
exit(1);

0 commit comments

Comments
 (0)