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
@@ -11,7 +11,7 @@ those is a design decision, not a commit.
|
||||
make check # tests/run.sh, generates keys in a tmpdir, no network
|
||||
make install PREFIX=$HOME/.local # installs bin/sshcrypt
|
||||
|
||||
`make check` is the gate: 25 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
||||
`make check` is the gate: 27 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
||||
`python3` (tamper test), `script(1)` (pty cases) and `pgrep` (SIGINT case);
|
||||
those last ones skip themselves if the tool is missing. No network. After
|
||||
touching parsing or the envelope, also run
|
||||
@@ -58,6 +58,11 @@ you pass; do not remove that `override`.
|
||||
* OpenSSL has no Ed25519→X25519 helper. `scr_ed25519_pk_to_x25519()` in
|
||||
`keys.c` is the standard `u = (1+y)/(1-y)` map, cross-checked against
|
||||
libcrypto X25519 in `tests/run.sh`. Do not replace it with an assumption.
|
||||
* Lengths inside a key or message file are attacker-controlled. Every copy into
|
||||
a fixed field must be bounded: the public blob from `openssh-key-v1` is
|
||||
`memcpy`'d into `struct scr_priv.blob[4096]`, and it used to be copied at
|
||||
whatever length the file claimed (stack overflow, caught in review). The
|
||||
guard is the `publ > sizeof out->blob` check in `scr_openssh_load`.
|
||||
* `memmem` is GNU, not POSIX. Use `find_bytes()` in `keys.c`.
|
||||
* Terminal echo: save termios *before* clearing `ECHO` and restore that saved
|
||||
copy. Re-reading with `tcgetattr` after disabling restores no-echo, and
|
||||
|
||||
+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;
|
||||
|
||||
|
||||
@@ -98,6 +98,40 @@ else
|
||||
ok
|
||||
fi
|
||||
|
||||
# A public blob larger than the 4096-byte field must be rejected, not memcpy'd
|
||||
# into struct scr_priv (this was a stack overflow in the container parser).
|
||||
python3 - "$T/rsa" "$T/oversize" <<'EOF'
|
||||
import base64, struct, sys
|
||||
def rd(b, o):
|
||||
n = struct.unpack('>I', b[o:o+4])[0]
|
||||
return b[o+4:o+4+n], o+4+n
|
||||
def put(s):
|
||||
return struct.pack('>I', len(s)) + s
|
||||
body = base64.b64decode(''.join(
|
||||
l for l in open(sys.argv[1]).read().splitlines() if '---' not in l))
|
||||
o = 15
|
||||
cipher, o = rd(body, o)
|
||||
kdf, o = rd(body, o)
|
||||
ko, o = rd(body, o)
|
||||
o += 4
|
||||
_, o = rd(body, o)
|
||||
priv, o = rd(body, o)
|
||||
new = (body[:15] + put(cipher) + put(kdf) + put(ko) + struct.pack('>I', 1)
|
||||
+ put(b'\x00' * 6000) + put(priv))
|
||||
b64 = base64.b64encode(new).decode()
|
||||
open(sys.argv[2], 'w').write(
|
||||
'-----BEGIN OPENSSH PRIVATE KEY-----\n'
|
||||
+ '\n'.join(b64[i:i + 70] for i in range(0, len(b64), 70))
|
||||
+ '\n-----END OPENSSH PRIVATE KEY-----\n')
|
||||
EOF
|
||||
rc=0
|
||||
"$BIN" -d -k "$T/oversize" -i "$T/msg.asc" -o /dev/null 2>"$T/over.err" ||
|
||||
rc=$?
|
||||
check "oversized key blob is rejected, not a crash" \
|
||||
'[ "$rc" -eq 1 ] || [ "$rc" -eq 2 ]'
|
||||
check "oversized key blob is named as such" \
|
||||
'grep -q "public key blob is" "$T/over.err"'
|
||||
|
||||
# --- passphrase-protected OpenSSH key, and -r with a private key --------------
|
||||
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pw.pub" -i "$T/msg" -o "$T/pw.asc"
|
||||
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pw" -i "$T/pw.asc" -o "$T/pw.out"
|
||||
|
||||
Reference in new issue
Block a user