Compare commits

...
2 Commits
Author SHA1 Message Date
lotherk 154d25be91 accept input of exactly the documented size limit
ci / build (push) Successful in 28s
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.
2026-11-21 20:07:04 +00:00
lotherk 2c381cbe75 mark scr_die noreturn so the error paths are provably dead ends
ci / build (push) Successful in 37s
gcc -fanalyzer and clang --analyze both reported findings that trace back to
scr_die() looking like an ordinary call: a possible NULL passphrase dereference
in strip_passphrase, and a double free of the temp key path (the analyzer
believed the conversion-failure branch returned and handed the freed pointer
back to the caller). exit() never returns, so say so with SCR_NORETURN.

Both analyzers are silent now; the suite is unchanged at 31/31.
2026-11-21 19:47:47 +00:00
7 changed files with 82 additions and 4 deletions

No files matched your search

+11 -1
View File
@@ -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: 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`
(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
@@ -77,6 +77,16 @@ you pass; do not remove that `override`.
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 ...`
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
`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.
Keep it when touching the error paths.
* Every `.c` includes its own headers; missing `<stdio.h>` is a hard error under
`-std=c99 -Wpedantic`.
+2
View File
@@ -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
`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
`encrypted for SHA256:...` note goes to stderr, and only when stderr is a
+1
View File
@@ -63,6 +63,7 @@ The recipient's public key, in
.I authorized_keys
format.
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.
.TP
.BR \-k ", " \-\-key " " \fIFILE\fP
+3 -1
View File
@@ -301,7 +301,9 @@ int scr_encrypt(const struct scr_pub *to, const unsigned char *pt, size_t n,
goto done;
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;
env = scr_xmalloc(body + n + SCR_TAG_LEN);
memcpy(env, header, SCR_HDR_LEN);
+13 -1
View File
@@ -3,6 +3,16 @@
#include <stddef.h>
/* C99 has no _Noreturn; both compilers we target do have the attribute. It
* matters: without it a static analyzer cannot tell that an error path ends,
* and it reads the cleanup after scr_die() as a fall-through (it reported a
* double free of the temp path for exactly that reason). */
#if defined(__GNUC__) || defined(__clang__)
#define SCR_NORETURN __attribute__((__noreturn__))
#else
#define SCR_NORETURN
#endif
#define SCR_VERSION 1
#define SCR_VERSION_STR "0.1.1"
#define SCR_SUITE_X25519 1
@@ -13,7 +23,9 @@
#define SCR_TAG_LEN 16
#define SCR_HDR_LEN 40 /* magic4 version1 suite1 reserved2 fingerprint32 */
#define SCR_PUB_MAX 4096
#ifndef SCR_INPUT_MAX
#define SCR_INPUT_MAX (1024u * 1024u * 1024u)
#endif
#define SCR_OK 0
#define SCR_EUSAGE 1
@@ -27,7 +39,7 @@ struct buf {
};
/* util.c */
void scr_die(int code, const char *fmt, ...);
void scr_die(int code, const char *fmt, ...) SCR_NORETURN;
void *scr_xmalloc(size_t n);
void *scr_xrealloc(void *p, size_t n);
char *scr_xstrdup(const char *s);
+14 -1
View File
@@ -66,10 +66,23 @@ struct buf scr_read_fd(int fd, size_t max, const char *what)
ssize_t r;
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,
"%s: larger than the %zu byte limit",
what, max);
}
cap *= 2;
if (cap > max)
cap = max;
+38
View File
@@ -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"
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 ------------------------------------------------------------------
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"
@@ -102,6 +111,12 @@ check "wrong key names both fingerprints" \
'grep -q "but your key is SHA256:" "$T/err"'
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"
python3 - "$T/tampered.bin" <<'EOF'
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"
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"
[ "$fail" -eq 0 ]