Review found the cap used as an output bound as well as an input bound: a file of exactly 1 GiB, the documented maximum, was rejected twice. scr_read_fd could not tell a full buffer from a larger input, and scr_encrypt refused anything over SCR_INPUT_MAX minus the envelope overhead. The reader now probes a single byte past a full buffer to distinguish the two, and the encrypt guard is a plain size_t overflow check. The suite builds a second binary with -DSCR_INPUT_MAX=65536 so both sides of the boundary are tested without a 1 GiB file; the previous code fails the exact-cap case. Also adds CRLF armor and 'a failed decrypt leaves the output file alone' cases, and documents that -r on a protected key asks for its passphrase.
This commit is contained in:
1 parent
2c381cbe75
commit
154d25be91
7 files changed
+67
-3
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 check # tests/run.sh, generates keys in a tmpdir, no network
|
||||||
make install PREFIX=$HOME/.local # installs bin/sshcrypt
|
make install PREFIX=$HOME/.local # installs bin/sshcrypt
|
||||||
|
|
||||||
`make check` is the gate: 31 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
`make check` is the gate: 35 end-to-end cases. It needs `ssh-keygen`, `cmp`,
|
||||||
`python3` (tamper and crafted-key cases), `script(1)` (pty cases), `pgrep`
|
`python3` (tamper and crafted-key cases), `script(1)` (pty cases), `pgrep`
|
||||||
(SIGINT case) and `setsid(1)` (no-tty case); those skip themselves if the tool
|
(SIGINT case) and `setsid(1)` (no-tty case); those skip themselves if the tool
|
||||||
is missing. No network. After touching parsing or the envelope, also run
|
is missing. No network. After touching parsing or the envelope, also run
|
||||||
@@ -77,6 +77,12 @@ you pass; do not remove that `override`.
|
|||||||
a field that merely contains a type name is skipped unless its base64 decodes
|
a field that merely contains a type name is skipped unless its base64 decodes
|
||||||
to that same type. A substring search accepts `# disabled: ssh-ed25519 ...`
|
to that same type. A substring search accepts `# disabled: ssh-ed25519 ...`
|
||||||
and breaks on `command="echo ssh-rsa hi"`.
|
and breaks on `command="echo ssh-rsa hi"`.
|
||||||
|
* `SCR_INPUT_MAX` bounds the *input*, not the output. It used to guard the
|
||||||
|
envelope size too, so an input of exactly the documented cap was rejected by
|
||||||
|
the reader and again by `scr_encrypt`. The reader now probes one byte past a
|
||||||
|
full buffer to distinguish "full" from "too large", and `scr_encrypt` only
|
||||||
|
guards against `size_t` overflow. `tests/run.sh` rebuilds the tool with a
|
||||||
|
64 KiB cap so both sides of the boundary are covered without a 1 GiB file.
|
||||||
* `scr_die()` is declared `SCR_NORETURN`. Without the attribute both
|
* `scr_die()` is declared `SCR_NORETURN`. Without the attribute both
|
||||||
`gcc -fanalyzer` and `clang --analyze` treat the code after a fatal call as
|
`gcc -fanalyzer` and `clang --analyze` treat the code after a fatal call as
|
||||||
reachable and report a NULL dereference and a double free that cannot happen.
|
reachable and report a NULL dereference and a double free that cannot happen.
|
||||||
|
|||||||
@@ -34,6 +34,8 @@ Code: https://git.kropa.tech/hiddenbox/sshcrypt
|
|||||||
|
|
||||||
`-r` also accepts a private key path: the public half is derived from it, so
|
`-r` also accepts a private key path: the public half is derived from it, so
|
||||||
`sshcrypt -r ~/.ssh/id_ed25519` works when you want to encrypt to yourself.
|
`sshcrypt -r ~/.ssh/id_ed25519` works when you want to encrypt to yourself.
|
||||||
|
A passphrase-protected key asks for its passphrase first, because the public
|
||||||
|
half is read out of the key.
|
||||||
|
|
||||||
Nothing is written to stdout except the payload. The one-line
|
Nothing is written to stdout except the payload. The one-line
|
||||||
`encrypted for SHA256:...` note goes to stderr, and only when stderr is a
|
`encrypted for SHA256:...` note goes to stderr, and only when stderr is a
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ The recipient's public key, in
|
|||||||
.I authorized_keys
|
.I authorized_keys
|
||||||
format.
|
format.
|
||||||
A private key is accepted too: its public half is derived from it.
|
A private key is accepted too: its public half is derived from it.
|
||||||
|
If that key has a passphrase, it is asked for first.
|
||||||
A file that holds more than one supported key is rejected.
|
A file that holds more than one supported key is rejected.
|
||||||
.TP
|
.TP
|
||||||
.BR \-k ", " \-\-key " " \fIFILE\fP
|
.BR \-k ", " \-\-key " " \fIFILE\fP
|
||||||
|
|||||||
+3
-1
@@ -301,7 +301,9 @@ int scr_encrypt(const struct scr_pub *to, const unsigned char *pt, size_t n,
|
|||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
body = SCR_HDR_LEN + 2 + kemlen + SCR_NONCE_LEN;
|
body = SCR_HDR_LEN + 2 + kemlen + SCR_NONCE_LEN;
|
||||||
if (n > SCR_INPUT_MAX - body)
|
/* Overflow guard only: the reader already enforced the input cap, and
|
||||||
|
* refusing exactly-cap input here would contradict it. */
|
||||||
|
if (n > (size_t)-1 - body - SCR_TAG_LEN)
|
||||||
goto done;
|
goto done;
|
||||||
env = scr_xmalloc(body + n + SCR_TAG_LEN);
|
env = scr_xmalloc(body + n + SCR_TAG_LEN);
|
||||||
memcpy(env, header, SCR_HDR_LEN);
|
memcpy(env, header, SCR_HDR_LEN);
|
||||||
|
|||||||
@@ -23,7 +23,9 @@
|
|||||||
#define SCR_TAG_LEN 16
|
#define SCR_TAG_LEN 16
|
||||||
#define SCR_HDR_LEN 40 /* magic4 version1 suite1 reserved2 fingerprint32 */
|
#define SCR_HDR_LEN 40 /* magic4 version1 suite1 reserved2 fingerprint32 */
|
||||||
#define SCR_PUB_MAX 4096
|
#define SCR_PUB_MAX 4096
|
||||||
|
#ifndef SCR_INPUT_MAX
|
||||||
#define SCR_INPUT_MAX (1024u * 1024u * 1024u)
|
#define SCR_INPUT_MAX (1024u * 1024u * 1024u)
|
||||||
|
#endif
|
||||||
|
|
||||||
#define SCR_OK 0
|
#define SCR_OK 0
|
||||||
#define SCR_EUSAGE 1
|
#define SCR_EUSAGE 1
|
||||||
|
|||||||
+14
-1
@@ -66,10 +66,23 @@ struct buf scr_read_fd(int fd, size_t max, const char *what)
|
|||||||
ssize_t r;
|
ssize_t r;
|
||||||
|
|
||||||
if (b.len == cap) {
|
if (b.len == cap) {
|
||||||
if (cap >= max)
|
if (cap >= max) {
|
||||||
|
/* A fill of exactly max bytes is allowed; only one
|
||||||
|
* more byte proves the input is too large. */
|
||||||
|
unsigned char extra;
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = read(fd, &extra, 1);
|
||||||
|
} while (r < 0 && errno == EINTR);
|
||||||
|
if (r < 0)
|
||||||
|
scr_die(SCR_EIO, "%s: %s", what,
|
||||||
|
strerror(errno));
|
||||||
|
if (r == 0)
|
||||||
|
break;
|
||||||
scr_die(SCR_EDATA,
|
scr_die(SCR_EDATA,
|
||||||
"%s: larger than the %zu byte limit",
|
"%s: larger than the %zu byte limit",
|
||||||
what, max);
|
what, max);
|
||||||
|
}
|
||||||
cap *= 2;
|
cap *= 2;
|
||||||
if (cap > max)
|
if (cap > max)
|
||||||
cap = max;
|
cap = max;
|
||||||
|
|||||||
@@ -92,6 +92,15 @@ check "RSA round trip" 'cmp -s "$T/msg" "$T/rsa.out"'
|
|||||||
"$BIN" -d -k "$T/ed" -i "$T/mail.txt" -o "$T/mail.out"
|
"$BIN" -d -k "$T/ed" -i "$T/mail.txt" -o "$T/mail.out"
|
||||||
check "armor inside surrounding text" 'cmp -s "$T/msg" "$T/mail.out"'
|
check "armor inside surrounding text" 'cmp -s "$T/msg" "$T/mail.out"'
|
||||||
|
|
||||||
|
# Mail clients may have written CRLF line endings.
|
||||||
|
python3 - "$T/msg.asc" "$T/crlf.asc" <<'EOF'
|
||||||
|
import sys
|
||||||
|
open(sys.argv[2], 'wb').write(
|
||||||
|
open(sys.argv[1], 'rb').read().replace(b'\n', b'\r\n'))
|
||||||
|
EOF
|
||||||
|
"$BIN" -d -k "$T/ed" -i "$T/crlf.asc" -o "$T/crlf.out"
|
||||||
|
check "CRLF armor decrypts" 'cmp -s "$T/msg" "$T/crlf.out"'
|
||||||
|
|
||||||
# --- errors ------------------------------------------------------------------
|
# --- errors ------------------------------------------------------------------
|
||||||
if "$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/nope" 2>"$T/err"; then
|
if "$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/nope" 2>"$T/err"; then
|
||||||
bad "decrypting with the wrong key must fail"
|
bad "decrypting with the wrong key must fail"
|
||||||
@@ -102,6 +111,12 @@ check "wrong key names both fingerprints" \
|
|||||||
'grep -q "but your key is SHA256:" "$T/err"'
|
'grep -q "but your key is SHA256:" "$T/err"'
|
||||||
check "wrong key writes nothing" '[ ! -e "$T/nope" ]'
|
check "wrong key writes nothing" '[ ! -e "$T/nope" ]'
|
||||||
|
|
||||||
|
# An existing output file must survive a failed decryption untouched.
|
||||||
|
printf 'keep me\n' >"$T/keep"
|
||||||
|
"$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/keep" 2>/dev/null || true
|
||||||
|
check "a failed decrypt leaves the output file alone" \
|
||||||
|
'[ "$(cat "$T/keep")" = "keep me" ]'
|
||||||
|
|
||||||
cp "$T/blob.bin" "$T/tampered.bin"
|
cp "$T/blob.bin" "$T/tampered.bin"
|
||||||
python3 - "$T/tampered.bin" <<'EOF'
|
python3 - "$T/tampered.bin" <<'EOF'
|
||||||
import sys
|
import sys
|
||||||
@@ -248,5 +263,28 @@ head -c 1048576 /dev/urandom >"$T/big"
|
|||||||
"$BIN" -d -k "$T/ed" -i "$T/big.asc" -o "$T/big.out"
|
"$BIN" -d -k "$T/ed" -i "$T/big.asc" -o "$T/big.out"
|
||||||
check "1 MiB round trip" 'cmp -s "$T/big" "$T/big.out"'
|
check "1 MiB round trip" 'cmp -s "$T/big" "$T/big.out"'
|
||||||
|
|
||||||
|
# --- the input cap boundary --------------------------------------------------
|
||||||
|
# Built with a 64 KiB cap so the exact boundary is testable without a 1 GiB
|
||||||
|
# file. A fill of exactly the cap must work; one byte more must not.
|
||||||
|
CAP_BIN=$T/sshcrypt-cap
|
||||||
|
if ${CC:-cc} -O1 -std=c99 -D_POSIX_C_SOURCE=200809L -DSCR_INPUT_MAX=65536 \
|
||||||
|
-I src -o "$CAP_BIN" src/*.c \
|
||||||
|
$(pkg-config --libs libcrypto 2>/dev/null || echo -lcrypto) 2>/dev/null
|
||||||
|
then
|
||||||
|
head -c 65536 /dev/urandom >"$T/exact"
|
||||||
|
rc=0
|
||||||
|
"$CAP_BIN" -r "$T/ed.pub" -b -i "$T/exact" -o "$T/exact.bin" \
|
||||||
|
2>/dev/null || rc=$?
|
||||||
|
check "exactly the input cap is accepted" \
|
||||||
|
'[ "$rc" -eq 0 ] && [ -s "$T/exact.bin" ]'
|
||||||
|
head -c 65537 /dev/urandom >"$T/over"
|
||||||
|
rc=0
|
||||||
|
"$CAP_BIN" -r "$T/ed.pub" -b -i "$T/over" -o "$T/over.bin" \
|
||||||
|
2>/dev/null || rc=$?
|
||||||
|
check "one byte over the cap is rejected" '[ "$rc" -eq 2 ]'
|
||||||
|
else
|
||||||
|
printf 'skip: could not build the small-cap binary\n'
|
||||||
|
fi
|
||||||
|
|
||||||
printf '%d passed, %d failed\n' "$pass" "$fail"
|
printf '%d passed, %d failed\n' "$pass" "$fail"
|
||||||
[ "$fail" -eq 0 ]
|
[ "$fail" -eq 0 ]
|
||||||
Reference in new issue
Block a user