CRASH-006-3: heap-buffer-overflow in handle_array
handle_array @ rcfg.c:122dispatch @ rcfg.c:210rcfg_decode @ rcfg.c:247ARRAY field allocates a fixed-width row table and fills every cell. A stream of "RCF1" magic plus one ARRAY field (type 0x03) carrying a 32-bit row count and 32-bit row width whose product exceeds 2^32 reaches the bug. The external party who supplies the stream bytes drives a tiny allocation followed by an effectively unbounded fill loop, a heap out-of-range write. The allocation size at handle_array (rcfg.c:115) is a truncated 32-bit product while the fill loop bound is the full 64-bit product../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 fixed-width row table and fills every cell. A stream of "RCF1" magic plus one ARRAY field (type 0x03) carrying a 32-bit row count and 32-bit row width whose product exceeds 2^32 reaches the bug. The external party who supplies the stream bytes drives a tiny allocation followed by an effectively unbounded fill loop, a heap out-of-range write. The allocation size at handle_array (rcfg.c:115) is a truncated 32-bit product while the fill loop bound is the full 64-bit product.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.2 |
| Class | memory-safety |
| Primitive | heap-buffer-overflow |
| Location | src/rcfg.c:handle_array:113 |
| 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_array rcfg.c:122 -> dispatch rcfg.c:210 -> rcfg_decode rcfg.c:247 |
Root Cause¶
handle_array computes uint32_t total = rows * width (rcfg.c:113) where both operands are 32-bit values taken directly from the field value (rcfg.c:111-112). The multiplication is performed in 32-bit arithmetic, so rows=0x10000, width=0x10000 yields a mathematical product of 0x1_0000_0000 that truncates to total == 0. malloc(total ? total : 1) then allocates a single byte (rcfg.c:115). The fill loop, however, is bounded by (size_t)rows * width (rcfg.c:121) — a 64-bit multiply equal to 0x1_0000_0000 — so it writes 0xFF to table[0], table[1], … far past the 1-byte allocation. The wrong assumption is that the value used to size the buffer and the value used to bound the loop are the same; they are computed in different widths, so the loop bound can vastly exceed the allocation.
Data Flow¶
The two 32-bit counts are read from the field value, multiplied twice with different widths, and the mismatch drives writes past a truncated allocation.
- read: handle_array (src/rcfg.c:111-112) —
rowsandwidthread as big-endian u32 from the field value. - size-math: handle_array (src/rcfg.c:113) —
total = rows * widthtruncates to 32 bits (0 for the reproducer). - alloc: handle_array (src/rcfg.c:115) —
malloc(total ? total : 1)allocates 1 byte. - loop: handle_array (src/rcfg.c:121) — loop bound
(size_t)rows * widthis the untruncated 64-bit product. - overflow: handle_array (src/rcfg.c:122) —
table[i] = 0xFFwrites past the 1-byte allocation.
Source snippets referenced in Data Flow:
↳ Referenced in: read: handle_array (src/rcfg.c:111-112) — rows and width read as big-endian u32 from the field 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: size-math: handle_array (src/rcfg.c:113) — total = rows * width truncates to 32 bits (0 for the reproducer).
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: alloc: 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: loop: handle_array (src/rcfg.c:121) — loop bound (size_t)rows * width is the untruncated 64-bit product.
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 }
↳ Referenced in: overflow: handle_array (src/rcfg.c:122) — table[i] = 0xFF writes past the 1-byte allocation.
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
Impact¶
Any producer of RCF stream bytes (the sole untrusted input, read from a file by rcfg_cli.c:59) turns an 8-byte field payload into an out-of-range heap write that runs until it walks off the mapping and faults. The write value is constant (0xFF) and the write extent is caller-chosen, so this is at minimum a reliable denial of service and a heap-corruption primitive; exploitability beyond a crash is unproven.
Fix Direction¶
Detect the overflow before allocating: compute the product in size_t (or uint64_t), reject when it exceeds a sane cap or when width != 0 && total / width != rows, and size both the allocation and the loop from the same checked value.
Expected sanitizer output¶
==80593==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x605000000721 at pc 0x000100d6557c bp 0x00016f098a90 sp 0x00016f098a88
WRITE of size 1 at 0x605000000721 thread T0
SCARINESS: 31 (1-byte-write-heap-buffer-overflow)
#0 0x000100d65578 in handle_array rcfg.c:122
#1 0x000100d64d70 in dispatch rcfg.c:210
#2 0x000100d64a4c in rcfg_decode rcfg.c:247
#3 0x000100d66418 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 0x0001015d9164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000100d654dc in handle_array rcfg.c:115
#2 0x000100d64d70 in dispatch rcfg.c:210
#3 0x000100d64a4c in rcfg_decode rcfg.c:247
#4 0x000100d66418 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