CRASH-007-3: heap-buffer-overflow in handle_slice
handle_slice @ rcfg.c:147dispatch @ rcfg.c:211rcfg_decode @ rcfg.c:247SLICE field copies a [offset, offset+length) window out of the whole input stream into a scratch buffer and checksums it. A stream of "RCF1" magic plus one SLICE field (type 0x04) carrying a 16-bit offset equal to the stream length and a nonzero 16-bit window length reaches the bug. The external party who supplies the stream bytes reads up to 512 bytes past the end of the heap buffer holding the input. The bounds check at handle_slice (rcfg.c:141) validates only the window start, never offset + length../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. A stream of "RCF1" magic plus one SLICE field (type 0x04) carrying a 16-bit offset equal to the stream length and a nonzero 16-bit window length reaches the bug. The external party who supplies the stream bytes reads up to 512 bytes past the end of the heap buffer holding the input. The bounds check at handle_slice (rcfg.c:141) validates only the window start, never offset + length.
Fields¶
| Field | Value |
|---|---|
| Severity | Low2.9 |
| Class | memory-safety |
| Primitive | heap-buffer-overflow |
| Location | src/rcfg.c:handle_slice:141 |
| Surface | cli— ASan frames are inside the target's shipped binary |
| Boundary | rcfg_decode public API over an RCF stream (file bytes via rcfg_cli.c:59) |
| Trigger source | bytes |
| Caller controls | entire stream contents and length |
| Caller contract | unspecified |
| Trusted caller actions | supplies a byte buffer and its true length |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | yes — no patch.diff — fix described in prose, see Fix Direction |
| 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 a window offset off and desired length want from the field value (rcfg.c:138-139). Its only bounds check is if (off > stream_len) return; (rcfg.c:141). This admits off == stream_len (a pointer exactly at the end of the buffer) and, more generally, never verifies that off + n stays within stream_len. It then copies n = min(want, sizeof(window)) bytes with memcpy(window, stream + off, n) (rcfg.c:146-147). The wrong assumption is that "the start is in range" implies "the whole window is in range"; the window can extend n bytes past the input. stream is the exact-length heap buffer allocated by the CLI (rcfg_cli.c:29), so the copy reads out of bounds.
Data Flow¶
The window offset and length are read from the field value; only the start is range-checked; the copy then reads the full window past the input buffer.
- alloc: read_file (src/rcfg_cli.c:29) — the input stream is a heap buffer sized to the exact file length.
- read: handle_slice (src/rcfg.c:138-139) —
offandwantread as big-endian u16 from the field value. - guard: handle_slice (src/rcfg.c:141) — rejects only
off > stream_len, allowingoff == stream_lenandoff + n > stream_len. - clamp: handle_slice (src/rcfg.c:146) —
n = min(want, 512), unrelated to bytes actually available atoff. - overread: 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: alloc: read_file (src/rcfg_cli.c:29) — the input stream is a heap buffer sized to the exact file length.
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);
↳ Referenced in: read: handle_slice (src/rcfg.c:138-139) — off and want read 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: guard: handle_slice (src/rcfg.c:141) — rejects only off > stream_len, allowing off == stream_len and off + n > 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: clamp: handle_slice (src/rcfg.c:146) — n = min(want, 512), unrelated to bytes actually available at off.
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: overread: 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 producer of RCF stream bytes (the sole untrusted input, read from a file by rcfg_cli.c:59) can force an out-of-range read of up to 512 bytes beyond the input allocation. The bytes are folded into ctx->slice_sum (rcfg.c:148); the checksum is not returned to the caller here, so the primary consequence is a crash on unmapped memory (denial of service) and a potential adjacent-heap information read if the summary were exposed. Exploitability as an info leak is unproven in this harness.
Fix Direction¶
Bound the window against the input: after checking off <= stream_len, clamp want/n to stream_len - off (as well as sizeof(window)) before the memcpy, so the copy never reads past the available bytes.
Expected sanitizer output¶
==80854==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6050000006db at pc 0x000105206c84 bp 0x00016b17c7b0 sp 0x00016b17bf60
READ of size 512 at 0x6050000006db thread T0
SCARINESS: 26 (multi-byte-read-heap-buffer-overflow)
#0 0x000105206c80 in __asan_memcpy (in libclang_rt.asan_osx_dynamic.dylib) + 1024
#1 0x000104c81790 in handle_slice rcfg.c:147
#2 0x000104c80d8c in dispatch rcfg.c:211
#3 0x000104c80a4c in rcfg_decode rcfg.c:247
#4 0x000104c82418 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 0x000105209164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000104c82610 in read_file rcfg_cli.c:29
#2 0x000104c823a8 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
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