CRASH-003-3: heap-buffer-overflow in handle_slice
handle_slice @ rcfg.c:147dispatch @ rcfg.c:211rcfg_decode @ rcfg.c:247[offset, offset+length) window out of the whole input stream into a scratch buffer and checksums it. The bounds check validates only the starting offset against the stream length, not the window length, so a window that starts inside the stream but extends past its end reads out of bounds. The shortest input is RCF1 plus one field: type 0x04, length 0x0004, value = big-endian offset then big-endian window length. With offset near the end of an 11-byte stream and length 512, the memcpy at rcfg.c:147 reads up to 512 bytes past the heap allocation holding the input../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.rcf (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 field copies a [offset, offset+length) window out of the whole input stream into a scratch buffer and checksums it. The bounds check validates only the starting offset against the stream length, not the window length, so a window that starts inside the stream but extends past its end reads out of bounds. The shortest input is RCF1 plus one field: type 0x04, length 0x0004, value = big-endian offset then big-endian window length. With offset near the end of an 11-byte stream and length 512, the memcpy at rcfg.c:147 reads up to 512 bytes past the heap allocation holding the input.
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 passed to rcfg_decode (file input via rcfg CLI) |
| Trigger source | bytes |
| Caller controls | SLICE offset and window length fields, plus the stream length |
| Caller contract | unspecified |
| Trusted caller actions | none beyond supplying the input stream |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | no— patch.diff included |
| Strategy | S2— Invariant negation |
| Dedup frames | handle_slice rcfg.c:147 -> dispatch rcfg.c:211 -> rcfg_decode rcfg.c:247 |
Root Cause¶
handle_slice reads off and want as big-endian u16 values from the field (rcfg.c:138-139) and rejects only off > stream_len (rcfg.c:141). It then computes n = min(want, sizeof(window)) — capped to the 512-byte scratch buffer, not to the remaining stream — and runs memcpy(window, stream + off, n) (rcfg.c:145-147). The invariant the copy assumes is off + n <= stream_len; the code only enforces off <= stream_len. When off is valid but off + n exceeds the stream, the read runs off the end of the input allocation. In the reproducer off = 10, want = 512, and the input buffer is 11 bytes, so it reads ~501 bytes past a heap region allocated by read_file (rcfg_cli.c:29). The stream length is the file length, fully under caller control.
Data Flow¶
A file of bytes reaches the SLICE handler, which reads a window extending past the stream end.
- read: main (src/rcfg_cli.c:59) — reads the input file into a heap buffer sized to the file, calls rcfg_decode.
- parse: rcfg_decode (src/rcfg.c:247) — passes
dataandlen(the whole stream) to dispatch for the SLICE field. - dispatch: dispatch (src/rcfg.c:211) — routes type
0x04to handle_slice with the stream base and length. - offset guard: handle_slice (src/rcfg.c:141) — rejects only
off > stream_len. - length: handle_slice (src/rcfg.c:146) —
n = min(want, 512), not bounded by remaining stream. - overflow: handle_slice (src/rcfg.c:147) —
memcpy(window, stream + off, n)reads past the input allocation.
Source snippets referenced in Data Flow:
↳ Referenced in: read: main (src/rcfg_cli.c:59) — reads the input file into a heap buffer sized to the file, calls 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: parse: rcfg_decode (src/rcfg.c:247) — passes data and len (the whole stream) to dispatch for the SLICE field.
From src/rcfg.c (lines 245–249):
245 }
246
247 ▶ dispatch(&ctx, data, len, type, p, flen);
248
249 p += flen;
↳ Referenced in: dispatch: dispatch (src/rcfg.c:211) — routes type 0x04 to handle_slice with the stream base and length.
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;
↳ Referenced in: offset guard: handle_slice (src/rcfg.c:141) — rejects only off > stream_len.
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: length: handle_slice (src/rcfg.c:146) — n = min(want, 512), not bounded 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: overflow: handle_slice (src/rcfg.c:147) — memcpy(window, stream + off, n) reads past the input allocation.
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 caller supplying a stream reads out-of-bounds heap memory past the input buffer — up to ~512 bytes per SLICE field. The bytes are folded into ctx->slice_sum (a checksum), so this is primarily an out-of-range read that can crash the process and, on non-sanitizer builds, may leak adjacent heap contents into the derived checksum. Direct disclosure of the raw bytes to the caller is not shown; the certain impact is a heap over-read and denial of service.
Fix Direction¶
After clamping n to the scratch buffer, also clamp it to the bytes remaining in the stream: reject or shrink the read when off + n > stream_len (e.g. if (off > stream_len) return; n = min(n, stream_len - off);). Validating only the offset is insufficient because the length is independently caller-controlled.
Expected sanitizer output¶
==76807==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6050000006db at pc 0x000102ed6c84 bp 0x00016d5a87b0 sp 0x00016d5a7f60
READ of size 512 at 0x6050000006db thread T0
SCARINESS: 26 (multi-byte-read-heap-buffer-overflow)
#0 0x000102ed6c80 in __asan_memcpy (in libclang_rt.asan_osx_dynamic.dylib) + 1024
#1 0x000102855790 in handle_slice rcfg.c:147
#2 0x000102854d8c in dispatch rcfg.c:211
#3 0x000102854a4c in rcfg_decode rcfg.c:247
#4 0x000102856418 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 0x000102ed9164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000102856610 in read_file rcfg_cli.c:29
#2 0x0001028563a8 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 (14 lines)
--- a/src/rcfg.c+++ b/src/rcfg.c@@ -142,7 +142,10 @@ static void handle_slice(rcfg_ctx *ctx, const uint8_t *stream, return; } uint8_t window[RCFG_SLICE_CAP]; size_t n = want < sizeof(window) ? want : sizeof(window);+ if (n > stream_len - off) {+ n = stream_len - off;+ } 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