CRASH-003-2: stack-buffer-overflow in handle_str
handle_str @ rcfg.c:86dispatch @ rcfg.c:208rcfg_decode @ rcfg.c:247RCF1, a 3-byte field header 01 00 40, then 64 value bytes. The caller who supplies the stream bytes gets a one-byte stack write immediately after the buffer. In handle_str the guard if (len <= sizeof(label)) (src/rcfg.c:84) admits len == 64, and label[len] = '\0' (src/rcfg.c:86) then writes at index 64 of a 64-element array../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.rcf (71 B); input repro.cmd (96 B); sanitizer log sanitizer.txt (14 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder's STR field interns a short label by copying it into a 64-byte stack buffer and NUL-terminating it. When the field length equals the buffer capacity exactly (64), the terminator is written one byte past the end of the buffer. The shortest input is the 4-byte magic RCF1, a 3-byte field header 01 00 40, then 64 value bytes. The caller who supplies the stream bytes gets a one-byte stack write immediately after the buffer. In handle_str the guard if (len <= sizeof(label)) (src/rcfg.c:84) admits len == 64, and label[len] = '\0' (src/rcfg.c:86) then writes at index 64 of a 64-element array.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.9 |
| Class | memory-safety |
| Primitive | stack-buffer-overflow |
| Location | src/rcfg.c:handle_str:86 |
| Surface | cli— report boundary is a shipped command-line tool |
| Boundary | RCF stream bytes read from the input file (rcfg_decode public API / CLI) |
| Trigger source | bytes |
| Caller controls | full stream contents — field type tag, u16 length, and value bytes |
| Caller contract | unspecified |
| Trusted caller actions | calls rcfg_decode(data, len) once over the exact file length; no internal state mutated |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | yes — no patch.diff — fix described in prose, see Fix Direction |
| Strategy | S7— Adversarial input |
| Dedup frames | handle_str rcfg.c:86 -> dispatch rcfg.c:208 -> rcfg_decode rcfg.c:247 |
Root Cause¶
handle_str declares char label[RCFG_LABEL_CAP] with RCFG_LABEL_CAP = 64 (src/rcfg.c:19, 82). It guards the copy with an inclusive comparison: if (len <= sizeof(label)) (src/rcfg.c:84). Inside, it does memcpy(label, val, len) (in-bounds for len == 64) and then label[len] = '\0' (src/rcfg.c:85-86). The wrong assumption is that a length passing len <= sizeof(label) leaves room for the terminator; it does not when len == sizeof(label), because valid indices are 0..63 and the terminator lands at index 64. The correct guard is strict: len < sizeof(label). len is the field length parsed from the stream (read_be16, src/rcfg.c:237, clamped only to bytes remaining), so a caller chooses it directly; setting it to exactly 64 with 64 value bytes present drives the off-by-one write.
Data Flow¶
A STR field of length 64 with 64 value bytes flows from the input file into the intern buffer, whose NUL terminator is written past the end.
- step: main (src/rcfg_cli.c:59) — reads the file and calls rcfg_decode over its exact length.
- step: rcfg_decode (src/rcfg.c:237) — reads the u16 field length 64; 64 value bytes remain so no clamp.
- step: dispatch (src/rcfg.c:208) — routes type tag 0x01 to handle_str with len=64.
- step: handle_str (src/rcfg.c:84) — guard len <= sizeof(label) (64 <= 64) passes.
- step: handle_str (src/rcfg.c:85) — memcpy copies 64 bytes into label[64] (in bounds).
- step: handle_str (src/rcfg.c:86) — label[64] = '\0' writes one byte past the 64-byte buffer; ASan traps.
Source snippets referenced in Data Flow:
↳ Referenced in: step: main (src/rcfg_cli.c:59) — reads the file and calls rcfg_decode over its exact length.
From src/rcfg_cli.c (lines 57–61):
57 }
58
59 ▶ int fields = rcfg_decode(data, len);
60 free(data);
61
↳ Referenced in: step: rcfg_decode (src/rcfg.c:237) — reads the u16 field length 64; 64 value bytes remain so no clamp.
From src/rcfg.c (lines 235–239):
235 while (remaining >= RCFG_FIELD_HEADER) {
236 uint8_t type = p[0];
237 ▶ uint16_t flen = read_be16(p + 1);
238 p += RCFG_FIELD_HEADER;
239 remaining -= RCFG_FIELD_HEADER;
↳ Referenced in: step: dispatch (src/rcfg.c:208) — routes type tag 0x01 to handle_str with len=64.
From src/rcfg.c (lines 206–210):
206 {
207 switch (type) {
208 ▶ case RCFG_T_STR: handle_str(ctx, val, len); break;
209 case RCFG_T_BLOB: handle_blob(ctx, val, len); break;
210 case RCFG_T_ARRAY: handle_array(val, len); break;
↳ Referenced in: step: handle_str (src/rcfg.c:84) — guard len <= sizeof(label) (64 <= 64) passes.
From src/rcfg.c (lines 82–86):
82 char label[RCFG_LABEL_CAP];
83
84 ▶ if (len <= sizeof(label)) {
85 memcpy(label, val, len);
86 label[len] = '\0';
↳ Referenced in: step: handle_str (src/rcfg.c:85) — memcpy copies 64 bytes into label[64] (in bounds).
From src/rcfg.c (lines 83–87):
83
84 if (len <= sizeof(label)) {
85 ▶ memcpy(label, val, len);
86 label[len] = '\0';
87 ctx->labels += strlen(label) ? 1 : 0;
↳ Referenced in: step: handle_str (src/rcfg.c:86) — label[64] = '\0' writes one byte past the 64-byte buffer; ASan traps.
From src/rcfg.c (lines 84–88):
84 if (len <= sizeof(label)) {
85 memcpy(label, val, len);
86 ▶ label[len] = '\0';
87 ctx->labels += strlen(label) ? 1 : 0;
88 }
Impact¶
Any producer of an RCF stream can write a single NUL byte one position past a 64-byte decoder stack buffer, reachable through the sole documented input boundary (a file of stream bytes). A one-byte stack overwrite of an adjacent saved value can be safety-relevant depending on frame layout; exploitability beyond a crash is unproven.
Fix Direction¶
Change the guard to strict less-than so the terminator stays in bounds: if (len < sizeof(label)). This reserves one byte for the NUL. An inclusive bound that treats the whole buffer as available for the payload leaves no room for the terminator handle_str unconditionally writes.
Expected sanitizer output¶
==77216==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x00016d70ca20 at pc 0x0001026f108c bp 0x00016d70c9b0 sp 0x00016d70c9a8
WRITE of size 1 at 0x00016d70ca20 thread T0
SCARINESS: 46 (1-byte-write-stack-buffer-overflow)
#0 0x0001026f1088 in handle_str rcfg.c:86
#1 0x0001026f0d4c in dispatch rcfg.c:208
#2 0x0001026f0a4c in rcfg_decode rcfg.c:247
#3 0x0001026f2418 in main rcfg_cli.c:59
#4 0x00018d1844e0 in start (in dyld) + 6988
Address 0x00016d70ca20 is located in stack of thread T0 at offset 96 in frame
#0 0x0001026f0ef4 in handle_str rcfg.c:81
This frame has 1 object(s):
[32, 96) 'label' (line 82) <== Memory access at offset 96 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow (targets/samples/sample-c/build-asan/rcfg:arm64+0x100001088)
Full original output: sanitizer.txt.
Annotated stack frames (top-of-stack source):
#0 handle_str — rcfg.c:86
From src/rcfg.c (lines 84–88):
84 if (len <= sizeof(label)) {
85 memcpy(label, val, len);
86 ▶ label[len] = '\0';
87 ctx->labels += strlen(label) ? 1 : 0;
88 }
#1 dispatch — rcfg.c:208
From src/rcfg.c (lines 206–210):
206 {
207 switch (type) {
208 ▶ case RCFG_T_STR: handle_str(ctx, val, len); break;
209 case RCFG_T_BLOB: handle_blob(ctx, val, len); break;
210 case RCFG_T_ARRAY: handle_array(val, len); break;
#2 rcfg_decode — rcfg.c:247
From src/rcfg.c (lines 245–249):
245 }
246
247 ▶ dispatch(&ctx, data, len, type, p, flen);
248
249 p += flen;
#3 main — rcfg_cli.c:59
From src/rcfg_cli.c (lines 57–61):
57 }
58
59 ▶ int fields = rcfg_decode(data, len);
60 free(data);
61
#0 handle_str — rcfg.c:81
From src/rcfg.c (lines 79–83):
79 * stored NUL-terminated. */
80 static void handle_str(rcfg_ctx *ctx, const uint8_t *val, uint16_t len)
81 ▶ {
82 char label[RCFG_LABEL_CAP];
83
Reproduce¶
Script — reproduce.sh
Run the sibling reproduce.sh:
./reproduce.sh # builds/runs with ASan against the in-place audit source
./reproduce.sh /path/to/src # or an existing checkout
Severity rationale¶
Severity is the CVSS v4.0 score — one industry-standard metric, computed by the vendored FIRST reference scorer. Base metrics come from the primitive and surface, with Attack Vector/Requirements also reflecting the trigger's caller-control facts (a trusted local API/call-sequence reads as AV:L/AT:P). The Threat metric (E) is read locally, not from threat intelligence: it grades how far this audit got toward proving the bug — a reproducer that fires is E:P, an unproven argument is E:U — so a demonstrated bug outranks a written one. FIRST defines E against public exploit availability, which is not what is measured here, and nothing in this run is publicly disclosed. Read E as reproduction confidence. The Environmental Security Requirements (CR/IR/AR) are left Not Defined — they model a deploying organisation's asset importance, which the scorer cannot derive, so a generic score keeps the CVSS-B worst case. Environmental modified impacts are set only to floor impact where reachability facts justify it (non-shipping surfaces, caller-only triggers). Cluster size remains a priority/verification fact because CVSS v4.0 has no cluster metric.
CVSS-BT v4.0: CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:P → 8.9 (High) CVSS-B baseline: CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N → 9.3 (Critical)
| Metric | Value | Derived from |
|---|---|---|
| Attack Vector (AV) | N | surface tier = library |
| Attack Complexity (AC) | L | fixed — not separately modelled |
| Attack Requirements (AT) | N | intrinsic attack preconditions |
| Privileges Required (PR) | N | worst case — no auth signal |
| User Interaction (UI) | N | surface tier |
| Vuln Conf/Integ/Avail | H/H/H | primitive class = stack_write |
| Subseq Conf/Integ/Avail | N/N/N | primitive class |
| Threat Exploit Maturity (E) | P | local reproduction evidence — not public exploit availability |
| Env Requirements (CR/IR/AR) | X/X/X | not defined — deployer-set asset importance (CVSS-B baseline) |
| Env Modified AV/AC/AT/PR/UI | X/X/X/X/X | caller control / contract concern |
| Env Modified C/I/A impacts | X/X/X/X/X/X | non-shipping / caller-only reachability floor; demonstrated disclosure / availability |
Severity level: High — CVSS bands: Critical ≥ 9.0, High ≥ 7.0, Medium ≥ 4.0, Low ≥ 0.1, None = 0.0.
Verification facts (not part of severity — triage priority): reproduction rate 5/5; testcase + sanitizer log on disk.
Field derivation (each line is a reviewable claim):
- surface=library → AV:N/UI:N
- class=stack_write → VC:H/VI:H/VA:H/SC:N/SI:N/SA:N
- class preconditions → AT:N
- PR:N (worst case — no auth signal), AC:L
- reproduction rate 5/5 → E:P
- CR/IR/AR not defined — environmental requirements are a deployer concern (CVSS-B baseline)
- caller controls input bytes → MAT:X