CRASH-003-1: heap-buffer-overflow in handle_slice
handle_slice @ rcfg.c:147dispatch @ rcfg.c:211rcfg_decode @ rcfg.c:247rcfg.c:141 validates only off against stream_len; the copy at rcfg.c:147 reads n bytes from stream + off without checking off + n <= stream_len../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.bin (11 B); input repro.cmd (96 B); sanitizer log sanitizer.txt (14 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder's SLICE handler copies a window out of the whole input stream into a scratch buffer and checksums it. A stream carrying one SLICE field whose declared offset is in range but whose length runs past the end of the stream reaches a copy that reads beyond the input buffer. The external party gets an out-of-bounds heap read of up to 512 bytes past the input. The bounds check at rcfg.c:141 validates only off against stream_len; the copy at rcfg.c:147 reads n bytes from stream + off without checking off + n <= stream_len.
Fields¶
| Field | Value |
|---|---|
| Severity | Low2.9 |
| Class | memory-safety |
| Primitive | heap-buffer-overflow |
| Location | src/rcfg.c:handle_slice:147 |
| Surface | cli— report boundary is a shipped command-line tool |
| Boundary | RCF stream bytes decoded by rcfg_decode (CLI file input, attacker_controls=bytes) |
| Trigger source | bytes |
| Caller controls | SLICE field offset and length (two big-endian u16 in the field value) |
| Caller contract | unspecified |
| Trusted caller actions | reads a file and calls rcfg_decode(data, len) once over the exact length |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | no— patch.diff included |
| Strategy | S3— Spec vs. implementation |
| Dedup frames | handle_slice rcfg.c:147 -> dispatch rcfg.c:211 -> rcfg_decode rcfg.c:247 |
Root Cause¶
handle_slice reads a caller-supplied big-endian u16 offset off and length want (rcfg.c:138-139). It rejects only off > stream_len (rcfg.c:141), then clamps the copy length to the scratch buffer with n = min(want, 512) (rcfg.c:146) and does memcpy(window, stream + off, n) (rcfg.c:147). The assumed invariant — that [off, off+n) lies within the stream — is never checked: n is bounded by the destination capacity, not by how many bytes remain after off. With off = 0, want = 512 on a ~15-byte stream, the copy reads 512 bytes from a 15-byte heap allocation, ~497 bytes out of bounds. off = stream_len also passes the guard and reads entirely past the end.
Data Flow¶
Caller-controlled file bytes select an in-range offset and an oversized length.
- step: main (src/rcfg_cli.c:59) — hands the exact-length input buffer to rcfg_decode.
- step: rcfg_decode (src/rcfg.c:247) — dispatches the SLICE field (type 0x04), passing
dataand fulllenas the stream. - step: handle_slice (src/rcfg.c:138-139) — reads
offandwantas big-endian u16 from the field value. - step: handle_slice (src/rcfg.c:141) — guard rejects only
off > stream_len, notoff + want. - step: handle_slice (src/rcfg.c:146) —
n = min(want, 512)bounds by scratch size, not by remaining stream. - step: handle_slice (src/rcfg.c:147) —
memcpy(window, stream + off, n)reads past the end of the input buffer.
Source snippets referenced in Data Flow:
↳ Referenced in: step: main (src/rcfg_cli.c:59) — hands the exact-length input buffer to rcfg_decode.
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:247) — dispatches the SLICE field (type 0x04), passing data and full len as the stream.
From src/rcfg.c (lines 245–249):
245 }
246
247 ▶ dispatch(&ctx, data, len, type, p, flen);
248
249 p += flen;
↳ Referenced in: step: handle_slice (src/rcfg.c:138-139) — reads off and want as big-endian u16 from the field value.
From src/rcfg.c (lines 136–140):
136 }
137
138 ▶ uint16_t off = read_be16(val);
139 uint16_t want = read_be16(val + 2);
140
↳ Referenced in: step: handle_slice (src/rcfg.c:141) — guard rejects only off > stream_len, not off + want.
From src/rcfg.c (lines 139–143):
139 uint16_t want = read_be16(val + 2);
140
141 ▶ if (off > stream_len) {
142 return;
143 }
↳ Referenced in: step: handle_slice (src/rcfg.c:146) — n = min(want, 512) bounds by scratch size, not by remaining stream.
From src/rcfg.c (lines 144–148):
144
145 uint8_t window[RCFG_SLICE_CAP];
146 ▶ size_t n = want < sizeof(window) ? want : sizeof(window);
147 memcpy(window, stream + off, n);
148 ctx->slice_sum = checksum(window, n);
↳ Referenced in: step: handle_slice (src/rcfg.c:147) — memcpy(window, stream + off, n) reads past the end of the input buffer.
From src/rcfg.c (lines 145–149):
145 uint8_t window[RCFG_SLICE_CAP];
146 size_t n = want < sizeof(window) ? want : sizeof(window);
147 ▶ memcpy(window, stream + off, n);
148 ctx->slice_sum = checksum(window, n);
149 }
Impact¶
Any consumer decoding an untrusted RCF stream is exposed with an ~15-byte input. Up to 512 bytes of heap memory adjacent to the input buffer are read and folded into ctx->slice_sum (rcfg.c:148). The checksum is not returned by the public API here, so this is primarily an out-of-bounds read / crash rather than a proven disclosure channel, but it is a memory-safety violation on caller-controlled input.
Fix Direction¶
Bound the copy by the stream, not only by the scratch buffer: compute the available bytes as avail = stream_len - off (after the existing off <= stream_len check) and use n = min(want, min(sizeof(window), avail)). Rejecting off + want > stream_len outright is also acceptable if partial windows are not intended.
Expected sanitizer output¶
==76591==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6050000006db at pc 0x0001017b6c84 bp 0x00016f0347b0 sp 0x00016f033f60
READ of size 512 at 0x6050000006db thread T0
SCARINESS: 26 (multi-byte-read-heap-buffer-overflow)
#0 0x0001017b6c80 in __asan_memcpy (in libclang_rt.asan_osx_dynamic.dylib) + 1024
#1 0x000100dc9790 in handle_slice rcfg.c:147
#2 0x000100dc8d8c in dispatch rcfg.c:211
#3 0x000100dc8a4c in rcfg_decode rcfg.c:247
#4 0x000100dca418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
0x6050000006db is located 0 bytes after 11-byte region [0x6050000006d0,0x6050000006db)
allocated by thread T0 here:
#0 0x0001017b9164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000100dca610 in read_file rcfg_cli.c:29
#2 0x000100dca3a8 in main rcfg_cli.c:53
#3 0x00018d1844e0 in start (in dyld) + 6988
SUMMARY: AddressSanitizer: heap-buffer-overflow (targets/samples/sample-c/build-asan/rcfg:arm64+0x100001790)
Full original output: sanitizer.txt.
Annotated stack frames (top-of-stack source):
#1 handle_slice — rcfg.c:147
From src/rcfg.c (lines 145–149):
145 uint8_t window[RCFG_SLICE_CAP];
146 size_t n = want < sizeof(window) ? want : sizeof(window);
147 ▶ memcpy(window, stream + off, n);
148 ctx->slice_sum = checksum(window, n);
149 }
#2 dispatch — rcfg.c:211
From src/rcfg.c (lines 209–213):
209 case RCFG_T_BLOB: handle_blob(ctx, val, len); break;
210 case RCFG_T_ARRAY: handle_array(val, len); break;
211 ▶ case RCFG_T_SLICE: handle_slice(ctx, stream, stream_len, val, len); break;
212 case RCFG_T_STAGE: handle_stage(ctx, val, len); break;
213 case RCFG_T_DROP: handle_drop(ctx); break;
#3 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;
#4 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
#1 read_file — rcfg_cli.c:29
From src/rcfg_cli.c (lines 27–31):
27 }
28
29 ▶ uint8_t *buf = malloc((size_t)size);
30 if (buf == NULL) {
31 fclose(fp);
#2 main — rcfg_cli.c:53
From src/rcfg_cli.c (lines 51–55):
51
52 size_t len = 0;
53 ▶ uint8_t *data = read_file(argv[1], &len);
54 if (data == NULL) {
55 fprintf(stderr, "could not read input\n");
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
Patch¶
Captured patch — patch.diff (13 lines)
--- a/src/rcfg.c+++ b/src/rcfg.c@@ -143,6 +143,8 @@ static void handle_slice(rcfg_ctx *ctx, const uint8_t *stream, } uint8_t window[RCFG_SLICE_CAP];- size_t n = want < sizeof(window) ? want : sizeof(window);+ size_t avail = stream_len - off;+ size_t n = want < sizeof(window) ? want : sizeof(window);+ if (n > avail) n = avail; memcpy(window, stream + off, n); ctx->slice_sum = checksum(window, n); }
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-BTE v4.0: CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N/E:P/MAT:P → 2.9 (Low) CVSS-B baseline: CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N → 6.9 (Medium)
| 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 | N/N/L | primitive class = heap_read_big |
| 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/P/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: Low — 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=heap_read_big → VC:N/VI:N/VA:L/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 limited trigger shape → MAT:P