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.
This commit is contained in:
lotherk committed 2026-11-21 20:07:04 +00:00
1 parent 2c381cbe75
commit 154d25be91
7 files changed
+67 -3

No files matched your search

+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);
+2
View File
@@ -23,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
+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;