← all crashes · CRASH-0012 · samples/sample-c · S2 · accepted · filed 2026-09-16 14:34 UTC

CRASH-004-3: heap-buffer-overflow in handle_array

High8.2heap-buffer-overflowcli
#1 handle_array @ rcfg.c:122
#2 dispatch @ rcfg.c:210
#3 rcfg_decode @ rcfg.c:247
Repro 5/5Cluster CL-5d86ee93 (4 reports: CRASH-0004, CRASH-0005, CRASH-0017)
The RCF decoder's ARRAY field allocates a fixed-width row table and fills every cell. The allocation size is computed as a 32-bit product of a caller-supplied row count and row width, but the fill loop bounds itself with the same product computed in 64-bit size_t. When the 32-bit product wraps, the buffer is allocated small while the loop still writes the full untruncated number of cells, overrunning the heap allocation. The shortest input is RCF1 plus one field: type 0x03, length 0x0008, value = big-endian u32 rows then big-endian u32 width. With rows 0x00010001 and width 0x00010000 the allocation is 65536 bytes but the loop writes 0x100010000 cells.
Act on it
Fix
patch.diff is captured beside this report — Compute the table size in a width that cannot wrap and reject oversized requests: use `size_t total = (size_t)rows * width;` and bound it against a sane maximum (and the format's own limits) before `malloc`, then loop to the same `total`..
Where
src/rcfg.c:handle_array:113 · reached through the cli surface
Reproduce
run ./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.
Confidence
reportable — source review placed the trigger within attacker_controls=bytes; source-anchored trigger review voted Promote on 2 verified source anchors.
Affects
boundary RCF stream bytes passed to rcfg_decode (file input via rcfg CLI); attacker controls ARRAY row count and row width (two u32 fields); needs the caller to none beyond supplying the input stream.

Summary

The RCF decoder's ARRAY field allocates a fixed-width row table and fills every cell. The allocation size is computed as a 32-bit product of a caller-supplied row count and row width, but the fill loop bounds itself with the same product computed in 64-bit size_t. When the 32-bit product wraps, the buffer is allocated small while the loop still writes the full untruncated number of cells, overrunning the heap allocation. The shortest input is RCF1 plus one field: type 0x03, length 0x0008, value = big-endian u32 rows then big-endian u32 width. With rows 0x00010001 and width 0x00010000 the allocation is 65536 bytes but the loop writes 0x100010000 cells.

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 passed to rcfg_decode (file input via rcfg CLI)
Trigger source bytes
Caller controls ARRAY row count and row width (two u32 fields)
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_array rcfg.c:122 -> dispatch rcfg.c:210 -> rcfg_decode rcfg.c:247

Root Cause

handle_array reads rows and width as big-endian u32 values (rcfg.c:111- 112) and computes uint32_t total = rows * width (rcfg.c:113). This multiplication is performed in 32-bit and silently wraps: 0x10001 * 0x10000 = 0x100010000, which truncates to 0x10000 (65536). malloc(total ? total : 1) therefore reserves only 65536 bytes (rcfg.c:115). The fill loop, however, is bounded by (size_t)rows * width (rcfg.c:121) — the same product in 64-bit, which does not wrap and equals 4295032832. From cell index 65536 onward, table[i] = 0xFF writes past the allocation (rcfg.c:122). The wrong assumption is that the allocation size and the loop trip count are the same quantity; they diverge whenever rows * width exceeds UINT32_MAX. Both factors are caller-controlled stream bytes.

Data Flow

A file of bytes reaches the ARRAY handler, whose allocation size wraps below its write count.

Source snippets referenced in Data Flow:

↳ Referenced in: read: main (src/rcfg_cli.c:59) — reads the input file and 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: dispatch: dispatch (src/rcfg.c:210) — routes type 0x03 to handle_array with the field value.

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;

↳ Referenced in: size math: handle_array (src/rcfg.c:113) — uint32_t total = rows * width wraps modulo 2^32.

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) reserves the wrapped (small) size.

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 bound: handle_array (src/rcfg.c:121) — loop runs (size_t)rows * width iterations (unwrapped, large).

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 allocation once i reaches the wrapped size.

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 caller supplying a stream drives a heap-buffer-overflow write of 0xFF bytes far past a heap allocation, with the overrun length controlled by the gap between the 32-bit-wrapped size and the 64-bit cell count. This is a write primitive over the heap and a reliable memory-corruption / denial-of- service vector; with allocator grooming it is a plausible path to further control.

Fix Direction

Compute the table size in a width that cannot wrap and reject oversized requests: use size_t total = (size_t)rows * width; and bound it against a sane maximum (and the format's own limits) before malloc, then loop to the same total. Detect the multiplication overflow explicitly (e.g. via __builtin_mul_overflow) so the allocation size and the fill count can never diverge.

Expected sanitizer output

==77122==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x631000010800 at pc 0x00010450557c bp 0x00016b8f8a90 sp 0x00016b8f8a88
WRITE of size 1 at 0x631000010800 thread T0
SCARINESS: 31 (1-byte-write-heap-buffer-overflow)
    #0 0x000104505578 in handle_array rcfg.c:122
    #1 0x000104504d70 in dispatch rcfg.c:210
    #2 0x000104504a4c in rcfg_decode rcfg.c:247
    #3 0x000104506418 in main rcfg_cli.c:59
    #4 0x00018d1844e0 in start (in dyld) + 6988

0x631000010800 is located 0 bytes after 65536-byte region [0x631000000800,0x631000010800)
allocated by thread T0 here:
    #0 0x000104d41164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
    #1 0x0001045054dc in handle_array rcfg.c:115
    #2 0x000104504d70 in dispatch rcfg.c:210
    #3 0x000104504a4c in rcfg_decode rcfg.c:247
    #4 0x000104506418 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

Patch

Captured patch — patch.diff (16 lines)

--- a/src/rcfg.c+++ b/src/rcfg.c@@ -108,9 +108,12 @@ static void handle_array(const uint8_t *val, uint16_t len)     return;   }    uint32_t rows  = read_be32(val);   uint32_t width = read_be32(val + 4);-  uint32_t total = rows * width;+  size_t   total;+  if (__builtin_mul_overflow((size_t)rows, (size_t)width, &total)) {+    return;+  }    uint8_t *table = malloc(total ? total : 1);   if (table == NULL) {
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