CRASH-5: double-free in rcfg_decode
rcfg_decode @ rcfg.c:254starthandle_drop @ rcfg.c:169ctx->pending dangling. When decoding finishes, rcfg_decode frees ctx->pending again, double-freeing the same 4-byte allocation. A 12-byte stream — magic, one STAGE field (type 0x05), one DROP field (type 0x06) — triggers it. The attacker gains a double-free, an allocator-metadata corruption primitive../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.bin (11 B); sanitizer log sanitizer.txt (2 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder stages a heap record on a STAGE field and discards it on a DROP field. The DROP handler frees the pending record but never clears the pointer, leaving ctx->pending dangling. When decoding finishes, rcfg_decode frees ctx->pending again, double-freeing the same 4-byte allocation. A 12-byte stream — magic, one STAGE field (type 0x05), one DROP field (type 0x06) — triggers it. The attacker gains a double-free, an allocator-metadata corruption primitive.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.9 |
| Primitive | double-free |
| Location | src/rcfg.c:handle_drop:169 |
| Surface | unknown |
| Boundary | untrusted RCF stream decoded by rcfg_decode |
| Trigger source | bytes |
| Caller controls | bytes |
| Caller contract | unspecified |
| Trusted caller actions | normal public call |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | yes — no patch.diff — fix described in prose, see Fix Direction |
| Strategy | — |
| Dedup frames | handle_drop rcfg.c:169 -> dispatch rcfg.c:213 -> rcfg_decode rcfg.c:247 (use: rcfg_decode rcfg.c:254) |
Root Cause¶
handle_drop (src/rcfg.c:167-170) is just free(ctx->pending);. It does not set ctx->pending = NULL, so after a DROP the context still holds the address of freed memory. The invariant every other consumer assumes — "ctx->pending is either NULL or a live allocation" — is broken. At end of the pass rcfg_decode unconditionally runs free(ctx.pending) (src/rcfg.c:254) to clean up any uncommitted record; since the pointer was never nulled, this frees the already-freed block. The STAGE handler (src/rcfg.c:162) has the same latent hazard: it does free(ctx->pending) before reassigning, which would also double -free a post-DROP dangling pointer, but the end-of-decode free alone is enough.
Data Flow¶
Two ordered fields produce the stale-then-refreed pointer.
- step: handle_stage (src/rcfg.c:155-163) — STAGE
mallocs a record and stores it inctx->pending. - step: dispatch (src/rcfg.c:213) — DROP routes to
handle_drop. - step: handle_drop (src/rcfg.c:169) — frees
ctx->pendingbut leaves the pointer set. - step: rcfg_decode (src/rcfg.c:254) — end-of-pass
free(ctx.pending)frees the same block again.
Source snippets referenced in Data Flow:
↳ Referenced in: step: handle_stage (src/rcfg.c:155-163) — STAGE mallocs a record and stores it in ctx->pending.
From src/rcfg.c (lines 153–157):
153 static void handle_stage(rcfg_ctx *ctx, const uint8_t *val, uint16_t len)
154 {
155 ▶ rcfg_record *rec = malloc(sizeof(*rec));
156 if (rec == NULL) {
157 return;
↳ Referenced in: step: dispatch (src/rcfg.c:213) — DROP routes to handle_drop.
From src/rcfg.c (lines 211–215):
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;
214 case RCFG_T_FLUSH: handle_flush(ctx); break;
215 case RCFG_T_OPT: handle_opt(ctx, val, len); break;
↳ Referenced in: step: handle_drop (src/rcfg.c:169) — frees ctx->pending but leaves the pointer set.
From src/rcfg.c (lines 167–171):
167 static void handle_drop(rcfg_ctx *ctx)
168 {
169 ▶ free(ctx->pending);
170 }
171
↳ Referenced in: step: rcfg_decode (src/rcfg.c:254) — end-of-pass free(ctx.pending) frees the same block again.
From src/rcfg.c (lines 252–256):
252 }
253
254 ▶ free(ctx.pending);
255
256 /* A stream that carries a CHECK must match the digest of its host blob. */
Impact¶
Any consumer of untrusted RCF streams is exposed. A 12-byte input double-frees a 4-byte heap block (ASan attempting double-free, SCARINESS 42). Repeated STAGE/DROP sequences let an attacker interleave frees and allocations to corrupt allocator freelist metadata, a well-known path to controlled heap writes and potential code execution.
Fix Direction¶
Set ctx->pending = NULL immediately after the free in handle_drop (and, defensively, everywhere the record is freed). Nulling the pointer restores the "NULL or live" invariant so the end-of-decode cleanup and any later STAGE/FLUSH are safe.
Expected sanitizer output¶
==73706==ERROR: AddressSanitizer: attempting double-free on 0x605000000720 in thread T0:
SCARINESS: 42 (double-free)
==73706==WARNING: failed to spawn external symbolizer (errno: 9)
==73706==WARNING: failed to spawn external symbolizer (errno: 9)
==73706==WARNING: failed to spawn external symbolizer (errno: 9)
==73706==WARNING: failed to spawn external symbolizer (errno: 9)
==73706==WARNING: failed to spawn external symbolizer (errno: 9)
==73706==WARNING: Failed to use and restart external symbolizer!
#0 0x000102d65258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000102360a8c in rcfg_decode rcfg.c:254
#2 0x000102362418 in main rcfg_cli.c:59
#3 0x00018d1844e0 in start (in dyld) + 6988
0x605000000720 is located 0 bytes inside of 4-byte region [0x605000000720,0x605000000724)
freed by thread T0 here:
#0 0x000102d65258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000102361b04 in handle_drop rcfg.c:169
#2 0x000102360dac in dispatch rcfg.c:213
#3 0x000102360a4c in rcfg_decode rcfg.c:247
#4 0x000102362418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
previously allocated by thread T0 here:
#0 0x000102d65164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x0001023618cc in handle_stage rcfg.c:155
#2 0x000102360da0 in dispatch rcfg.c:212
#3 0x000102360a4c in rcfg_decode rcfg.c:247
#4 0x000102362418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
SUMMARY: AddressSanitizer: double-free (targets/samples/sample-c/build-asan/rcfg:arm64+0x100000a8c) in rcfg_decode+0x2ac
Full original output: sanitizer.txt.
Annotated stack frames (top-of-stack source):
#1 rcfg_decode — rcfg.c:254
From src/rcfg.c (lines 252–256):
252 }
253
254 ▶ free(ctx.pending);
255
256 /* A stream that carries a CHECK must match the digest of its host blob. */
#2 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_drop — rcfg.c:169
From src/rcfg.c (lines 167–171):
167 static void handle_drop(rcfg_ctx *ctx)
168 {
169 ▶ free(ctx->pending);
170 }
171
#2 dispatch — rcfg.c:213
From src/rcfg.c (lines 211–215):
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;
214 case RCFG_T_FLUSH: handle_flush(ctx); break;
215 case RCFG_T_OPT: handle_opt(ctx, val, len); 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;
#1 handle_stage — rcfg.c:155
From src/rcfg.c (lines 153–157):
153 static void handle_stage(rcfg_ctx *ctx, const uint8_t *val, uint16_t len)
154 {
155 ▶ rcfg_record *rec = malloc(sizeof(*rec));
156 if (rec == NULL) {
157 return;
#2 dispatch — rcfg.c:212
From src/rcfg.c (lines 210–214):
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;
214 case RCFG_T_FLUSH: handle_flush(ctx); break;
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-BT 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 → 8.9 (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 = double_free |
| 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/X/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=double_free → 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 input bytes → MAT:X