bound the public blob copied out of a key container
ci / build (push) Successful in 1m7s

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:
lotherk committed 2026-11-21 18:38:46 +00:00
1 parent abdac42c83
commit 6851364ccb
4 files changed
+75 -11

No files matched your search

+34
View File
@@ -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"