CRASH-002-2: heap-buffer-overflow in handle_array
handle_array @ rcfg.c:122dispatch @ rcfg.c:210rcfg_decode @ rcfg.c:247RCF1, a 3-byte field header 03 00 08, then eight value bytes 00 01 00 00 00 01 00 00 (rows = 0x10000, width = 0x10000). The caller who supplies the stream bytes gets a heap write that runs immediately past a 1-byte allocation. The mismatch is between uint32_t total = rows * width (src/rcfg.c:113) used for malloc and (size_t)rows * width (src/rcfg.c:121) used as the loop bound../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.rcf (15 B); input repro.cmd (96 B); sanitizer log sanitizer.txt (14 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder's ARRAY field allocates a row table and then fills every cell. It computes the allocation size in 32-bit arithmetic but drives the fill loop with a 64-bit product, so a field carrying a large row count and row width allocates a tiny (or 1-byte) buffer yet writes billions of bytes into it. The shortest input is the 4-byte magic RCF1, a 3-byte field header 03 00 08, then eight value bytes 00 01 00 00 00 01 00 00 (rows = 0x10000, width = 0x10000). The caller who supplies the stream bytes gets a heap write that runs immediately past a 1-byte allocation. The mismatch is between uint32_t total = rows * width (src/rcfg.c:113) used for malloc and (size_t)rows * width (src/rcfg.c:121) used as the loop bound.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.2 |
| Class | memory-safety |
| Primitive | heap-buffer-overflow |
| Location | src/rcfg.c:handle_array:113 |
| 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 the u32 rows/width in the ARRAY value |
| 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_array rcfg.c:122 -> dispatch rcfg.c:210 -> rcfg_decode rcfg.c:247 |
Root Cause¶
handle_array reads two attacker-supplied 32-bit values from the field: rows = read_be32(val) and width = read_be32(val + 4) (src/rcfg.c:111-112). It sizes the allocation with uint32_t total = rows * width (src/rcfg.c:113) — a multiplication performed in 32-bit, which wraps modulo 2^32. It then allocates malloc(total ? total : 1) (src/rcfg.c:115). The fill loop, however, recomputes the count at full width: for (size_t i = 0; i < (size_t)rows * width; i++) table[i] = 0xFF; (src/rcfg.c:121-123). The wrong assumption is that the allocation size and the loop trip count are the same value; they are computed in different integer widths. With rows = 0x10000 and width = 0x10000, the 32-bit product is 0 (so malloc(1)), while the 64-bit product is 0x1_0000_0000, so the loop writes 4294967296 bytes into a 1-byte buffer. len >= 8 is the only guard (src/rcfg.c:107), which the 8-byte value satisfies. The row/width values come straight from the input stream, so nothing bounds the product.
Data Flow¶
An ARRAY field carrying rows = 0x10000 and width = 0x10000 flows from the input file into a mis-sized heap allocation whose fill loop runs far past its end.
- step: main (src/rcfg_cli.c:59) — reads the input file and calls rcfg_decode over its exact length.
- step: rcfg_decode (src/rcfg.c:247) — dispatches the type-0x03 field to handle_array with the 8-byte value.
- step: handle_array (src/rcfg.c:111) — reads rows=0x10000 and width=0x10000 from the value.
- step: handle_array (src/rcfg.c:113) — total = rows*width wraps to 0 in uint32_t.
- step: handle_array (src/rcfg.c:115) — malloc(total?total:1) allocates 1 byte.
- step: handle_array (src/rcfg.c:121) — loop bound (size_t)rows*width = 0x100000000 writes 0xFF past the 1-byte region; ASan traps at table[1].
Source snippets referenced in Data Flow:
↳ Referenced in: step: main (src/rcfg_cli.c:59) — reads the input 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:247) — dispatches the type-0x03 field to handle_array with the 8-byte value.
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_array (src/rcfg.c:111) — reads rows=0x10000 and width=0x10000 from the value.
From src/rcfg.c (lines 109–113):
109 }
110
111 ▶ uint32_t rows = read_be32(val);
112 uint32_t width = read_be32(val + 4);
113 uint32_t total = rows * width;
↳ Referenced in: step: handle_array (src/rcfg.c:113) — total = rows*width wraps to 0 in uint32_t.
From src/rcfg.c (lines 111–115):
111 uint32_t rows = read_be32(val);
112 uint32_t width = read_be32(val + 4);
113 ▶ uint32_t total = rows * width;
114
115 uint8_t *table = malloc(total ? total : 1);
↳ Referenced in: step: handle_array (src/rcfg.c:115) — malloc(total?total:1) allocates 1 byte.
From src/rcfg.c (lines 113–117):
113 uint32_t total = rows * width;
114
115 ▶ uint8_t *table = malloc(total ? total : 1);
116 if (table == NULL) {
117 return;
↳ Referenced in: step: handle_array (src/rcfg.c:121) — loop bound (size_t)rows*width = 0x100000000 writes 0xFF past the 1-byte region; ASan traps at…
From src/rcfg.c (lines 119–123):
119
120 /* Fill the table cell by cell. */
121 ▶ for (size_t i = 0; i < (size_t)rows * width; i++) {
122 table[i] = 0xFF;
123 }
Impact¶
Any producer of an RCF stream can drive a heap out-of-range write of attacker-influenced extent with a fixed 0xFF byte, reachable through the sole documented input boundary (a file of stream bytes). Even where the loop count is astronomically large, the very first out-of-range store faults; smaller overflow extents (a controlled few bytes past a controlled-size allocation) are reachable by choosing rows/width so the 32-bit product wraps to a small nonzero value while the 64-bit product is slightly larger. Exploitability beyond a crash is unproven.
Fix Direction¶
Compute the element count once in a width that cannot silently wrap and reject oversize requests: use uint64_t total = (uint64_t)rows * width; (or size_t), check it against a sane ceiling before allocating, and use that same value for both malloc and the loop bound. Deriving the allocation from a truncated 32-bit product while iterating at 64-bit width is the defect; the two must be the same value.
Expected sanitizer output¶
==76293==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x605000000721 at pc 0x000104fb157c bp 0x00016ae4ca90 sp 0x00016ae4ca88
WRITE of size 1 at 0x605000000721 thread T0
SCARINESS: 31 (1-byte-write-heap-buffer-overflow)
#0 0x000104fb1578 in handle_array rcfg.c:122
#1 0x000104fb0d70 in dispatch rcfg.c:210
#2 0x000104fb0a4c in rcfg_decode rcfg.c:247
#3 0x000104fb2418 in main rcfg_cli.c:59
#4 0x00018d1844e0 in start (in dyld) + 6988
0x605000000721 is located 0 bytes after 1-byte region [0x605000000720,0x605000000721)
allocated by thread T0 here:
#0 0x000105555164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000104fb14dc in handle_array rcfg.c:115
#2 0x000104fb0d70 in dispatch rcfg.c:210
#3 0x000104fb0a4c in rcfg_decode rcfg.c:247
#4 0x000104fb2418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
SUMMARY: AddressSanitizer: heap-buffer-overflow (targets/samples/sample-c/build-asan/rcfg:arm64+0x100001578)
Full original output: sanitizer.txt.
Annotated stack frames (top-of-stack source):
#0 handle_array — rcfg.c:122
From src/rcfg.c (lines 120–124):
120 /* Fill the table cell by cell. */
121 for (size_t i = 0; i < (size_t)rows * width; i++) {
122 ▶ table[i] = 0xFF;
123 }
124
#1 dispatch — rcfg.c:210
From src/rcfg.c (lines 208–212):
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;
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;
#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
#1 handle_array — rcfg.c:115
From src/rcfg.c (lines 113–117):
113 uint32_t total = rows * width;
114
115 ▶ uint8_t *table = malloc(total ? total : 1);
116 if (table == NULL) {
117 return;
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:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:P/MAT:P → 8.2 (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 = heap_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/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: 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=heap_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 limited trigger shape → MAT:P