CRASH-005-2: double-free in rcfg_decode
rcfg_decode @ rcfg.c:254starthandle_drop @ rcfg.c:169RCF1 magic, one STAGE field (type 0x05), and one DROP field (type 0x06) — reaches it. An external party decoding an untrusted stream gets a double-free of a 4-byte heap block. handle_drop at rcfg.c:169 calls free(ctx->pending) without clearing ctx->pending; rcfg_decode then calls free(ctx.pending) again at rcfg.c:254../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 (8 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder can stage a heap-allocated record and later discard it. The discard handler frees the record but leaves the context still pointing at the freed block, so a second free of the same allocation runs at end of decode. A 9-byte stream — the RCF1 magic, one STAGE field (type 0x05), and one DROP field (type 0x06) — reaches it. An external party decoding an untrusted stream gets a double-free of a 4-byte heap block. handle_drop at rcfg.c:169 calls free(ctx->pending) without clearing ctx->pending; rcfg_decode then calls free(ctx.pending) again at rcfg.c:254.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.9 |
| Class | memory-safety |
| Primitive | double-free |
| Location | src/rcfg.c:handle_drop:169 |
| Surface | cli— report boundary is a shipped command-line tool |
| Boundary | RCF stream bytes decoded by rcfg_decode (CLI file input, attacker_controls=bytes) |
| Trigger source | bytes |
| Caller controls | field type sequence (STAGE 0x05, DROP 0x06) and field values |
| Caller contract | unspecified |
| Trusted caller actions | reads a file and calls rcfg_decode(data, len) once over the exact length |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | no— patch.diff included |
| Strategy | S7— Adversarial input |
| Dedup frames | handle_drop rcfg.c:169 -> dispatch rcfg.c:213 -> rcfg_decode rcfg.c:247 (use: rcfg_decode rcfg.c:254) |
Root Cause¶
rcfg_ctx.pending (rcfg.c:34) owns a rcfg_record allocated by the STAGE handler: handle_stage does rec = malloc(...), free(ctx->pending), ctx->pending = rec (rcfg.c:155-163). The invariant the rest of the code assumes is that ctx->pending is always either NULL or a live, singly-owned allocation. handle_drop breaks it: it executes free(ctx->pending) (rcfg.c:169) but does not set ctx->pending = NULL, leaving a dangling pointer in the live context. After the field loop, rcfg_decode performs its end-of-pass cleanup free(ctx.pending) (rcfg.c:254), freeing the same block a second time. The correct code must null the owner on release; the free-without- reset is the whole bug. The same dangling pointer also feeds handle_stage's free(ctx->pending) (double-free on a later STAGE) and handle_flush's ctx->pending->tag read (use-after-free), so DROP followed by any of STAGE / DROP / FLUSH / end-of-decode is unsafe.
Data Flow¶
Caller-controlled stream bytes stage a record, then drop it, and the freed block is freed again at decode end.
- step: main (src/rcfg_cli.c:59) — reads the file and calls rcfg_decode over the exact byte length.
- step: rcfg_decode (src/rcfg.c:235-247) — walks TLV fields and dispatches each type.
- step: handle_stage (src/rcfg.c:155-163) — mallocs a rcfg_record and stores it in ctx->pending.
- step: dispatch (src/rcfg.c:213) — routes the DROP field (type 0x06) to handle_drop.
- step: handle_drop (src/rcfg.c:169) — frees ctx->pending but leaves the pointer dangling (no reset to NULL).
- step: rcfg_decode (src/rcfg.c:254) — end-of-pass free(ctx.pending) frees the already-freed block a second time.
Source snippets referenced in Data Flow:
↳ Referenced in: step: main (src/rcfg_cli.c:59) — reads the file and calls rcfg_decode over the exact byte 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:235-247) — walks TLV fields and dispatches each type.
From src/rcfg.c (lines 233–237):
233 int fields = 0;
234
235 ▶ while (remaining >= RCFG_FIELD_HEADER) {
236 uint8_t type = p[0];
237 uint16_t flen = read_be16(p + 1);
↳ Referenced in: step: handle_stage (src/rcfg.c:155-163) — mallocs a rcfg_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) — routes the DROP field (type 0x06) 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 dangling (no reset to NULL).
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 already-freed block a second time.
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 decoding an untrusted RCF stream on the configured build is exposed with a 9-byte input. ASan reports a double-free of the 4-byte rcfg_record. Beyond the abort, the freed-then-reused ctx->pending gives an external party a controllable dangling pointer usable through DROP/STAGE/FLUSH; turning the double-free into a stronger primitive is unproven here, but the lifetime violation is caller-reachable and deterministic.
Fix Direction¶
Set ctx->pending = NULL immediately after free(ctx->pending) in handle_drop (and audit handle_stage similarly). Nulling the owner on release restores the "NULL or live" invariant that both the later handlers and the end-of-pass cleanup rely on; clamping or reordering the free does not, because multiple independent paths free the same field.
Expected sanitizer output¶
==78776==ERROR: AddressSanitizer: attempting double-free on 0x605000000720 in thread T0:
SCARINESS: 42 (double-free)
#0 0x000105425258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000104d78a8c in rcfg_decode rcfg.c:254
#2 0x000104d7a418 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 0x000105425258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000104d79b04 in handle_drop rcfg.c:169
#2 0x000104d78dac in dispatch rcfg.c:213
#3 0x000104d78a4c in rcfg_decode rcfg.c:247
#4 0x000104d7a418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
previously allocated by thread T0 here:
#0 0x000105425164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000104d798cc in handle_stage rcfg.c:155
#2 0x000104d78da0 in dispatch rcfg.c:212
#3 0x000104d78a4c in rcfg_decode rcfg.c:247
#4 0x000104d7a418 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)
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
Patch¶
Captured patch — patch.diff (11 lines)
--- a/src/rcfg.c+++ b/src/rcfg.c@@ -166,7 +166,8 @@ static void handle_stage(rcfg_ctx *ctx, const uint8_t *val, uint16_t len) /* DROP: discard the staged record without committing it. */ static void handle_drop(rcfg_ctx *ctx) { free(ctx->pending);+ ctx->pending = NULL; } /* FLUSH: commit the staged record by recording its tag. */
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)