Review found a stack-buffer-overflow in scr_openssh_load: the public blob length came straight from the file and was memcpy'd into struct scr_priv's 4096-byte field. A crafted RSA key file (the ed25519 path cross-checks first) overwrote the stack: ASan reported a 6000-byte write. Also reject p or q <= 1 before BN_mod, arm the temp-file cleanup before the write rather than after, clear the active flag on the conversion-failure path, and block INT/TERM/HUP across ssh-keygen so a signalled child cannot rename a fresh copy back after the handler unlinked it. The new case crafts an oversized blob and asserts a clean exit 1, not a crash.
This commit is contained in:
1 parent
abdac42c83
commit
6851364ccb
4 files changed
+75
-11
No files matched your search
+28
-10
@@ -572,21 +572,39 @@ static char *strip_passphrase(const char *path, const struct buf *raw)
|
||||
scr_die(SCR_EIO, "cannot create a temporary key file: %s",
|
||||
strerror(errno));
|
||||
(void)fchmod(fd, 0600);
|
||||
scr_write_fd(fd, raw->data, raw->len);
|
||||
close(fd);
|
||||
/* arm the cleanup before writing, so a signal cannot leave a file */
|
||||
snprintf(g_tmp_path, sizeof g_tmp_path, "%s", tmp);
|
||||
g_tmp_active = 1;
|
||||
tmp_signals_install();
|
||||
scr_write_fd(fd, raw->data, raw->len);
|
||||
close(fd);
|
||||
|
||||
pw = env ? scr_xstrdup(env) : get_passphrase(path);
|
||||
if (run_ssh_keygen(tmp, pw) != 0) {
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
unlink(tmp);
|
||||
free(tmp);
|
||||
scr_die(SCR_EUSAGE,
|
||||
"%s: wrong passphrase, or ssh-keygen could not read it",
|
||||
path);
|
||||
{
|
||||
/* Hold the cleanup signals across the child: if it were signalled
|
||||
* mid-write it could rename a fresh copy back after the handler
|
||||
* unlinked the path. A pending signal is delivered on unblock,
|
||||
* when the conversion is finished. */
|
||||
sigset_t block, old;
|
||||
int rc;
|
||||
|
||||
sigemptyset(&block);
|
||||
sigaddset(&block, SIGINT);
|
||||
sigaddset(&block, SIGTERM);
|
||||
sigaddset(&block, SIGHUP);
|
||||
sigprocmask(SIG_BLOCK, &block, &old);
|
||||
rc = run_ssh_keygen(tmp, pw);
|
||||
sigprocmask(SIG_SETMASK, &old, NULL);
|
||||
if (rc != 0) {
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
unlink(tmp);
|
||||
g_tmp_active = 0;
|
||||
free(tmp);
|
||||
scr_die(SCR_EUSAGE,
|
||||
"%s: wrong passphrase, or ssh-keygen could not "
|
||||
"read it", path);
|
||||
}
|
||||
}
|
||||
scr_secure_zero(pw, strlen(pw));
|
||||
free(pw);
|
||||
|
||||
@@ -120,6 +120,8 @@ static EVP_PKEY *rsa_from_parts(const unsigned char *nb, size_t nl,
|
||||
goto done;
|
||||
if (!BN_sub_word(pm1, 1) || !BN_sub_word(qm1, 1))
|
||||
goto done;
|
||||
if (BN_is_zero(pm1) || BN_is_zero(qm1))
|
||||
goto done;
|
||||
if (!BN_mod(dp, d, pm1, ctx) || !BN_mod(dq, d, qm1, ctx))
|
||||
goto done;
|
||||
|
||||
@@ -273,6 +275,11 @@ int scr_openssh_load(const unsigned char *text, size_t n, struct scr_priv *out)
|
||||
"supported\n", nkeys);
|
||||
goto done;
|
||||
}
|
||||
if (publ == 0 || publ > sizeof out->blob) {
|
||||
fprintf(stderr, "sshcrypt: the public key blob is %zu bytes; the "
|
||||
"limit is %zu\n", publ, sizeof out->blob);
|
||||
goto done;
|
||||
}
|
||||
if (parse_private(priv, privl, pub, publ, out) < 0)
|
||||
goto done;
|
||||
|
||||
|
||||
Reference in new issue
Block a user