CRASH-008-3: double-free in rcfg_decode
rcfg_decode @ rcfg.c:254starthandle_drop @ rcfg.c:169DROP field. A stream of "RCF1" magic, one STAGE field (type 0x05), then one DROP field (type 0x06) frees the staged record twice. The external party who supplies the stream bytes causes the same heap allocation to be passed to free() at handle_drop (rcfg.c:169) and again at the end-of-decode cleanup (rcfg.c:254). handle_drop releases ctx->pending but never clears the pointer../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 record for later commit and discard it with a DROP field. A stream of "RCF1" magic, one STAGE field (type 0x05), then one DROP field (type 0x06) frees the staged record twice. The external party who supplies the stream bytes causes the same heap allocation to be passed to free() at handle_drop (rcfg.c:169) and again at the end-of-decode cleanup (rcfg.c:254). handle_drop releases ctx->pending but never clears the pointer.
Fields¶
| Field | Value |
|---|---|
| Severity | High8.2 |
| Class | memory-safety |
| Primitive | double-free |
| Location | src/rcfg.c:handle_drop:169 |
| 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 | S5— Lifetime and state |
| Dedup frames | handle_drop rcfg.c:169 -> dispatch rcfg.c:213 -> rcfg_decode rcfg.c:247 (use: rcfg_decode rcfg.c:254) |
Root Cause¶
STAGE allocates a rcfg_record and stores it in ctx->pending (rcfg.c:155-163). DROP runs handle_drop, which is exactly free(ctx->pending); (rcfg.c:169) — it frees the record but does not set ctx->pending = NULL. The context now holds a dangling pointer. When the decode pass finishes, rcfg_decode unconditionally runs free(ctx.pending); (rcfg.c:254) to release any still-pending record. Since ctx.pending was never reset, this frees the already-freed block a second time. The wrong assumption is that handle_drop leaves ctx->pending in a released state that the final cleanup can treat as empty; every other consumer of ctx->pending (the final free, a later FLUSH read, a later STAGE free) tests or frees the pointer as if a non-NULL value means "still owns a live record".
Data Flow¶
A staged record is allocated, freed by DROP without clearing the owner, then freed again by the decode cleanup.
- stage: handle_stage (src/rcfg.c:163) —
STAGEstores a freshly malloc'd record inctx->pending. - drop: handle_drop (src/rcfg.c:169) —
DROPfreesctx->pendingbut does not null it, leaving a dangling pointer. - cleanup: rcfg_decode (src/rcfg.c:254) —
free(ctx.pending)frees the same block a second time.
Source snippets referenced in Data Flow:
↳ Referenced in: stage: handle_stage (src/rcfg.c:163) — STAGE stores a freshly malloc'd record in ctx->pending.
From src/rcfg.c (lines 161–165):
161 rec->size = len;
162 free(ctx->pending);
163 ▶ ctx->pending = rec;
164 }
165
↳ Referenced in: drop: handle_drop (src/rcfg.c:169) — DROP frees ctx->pending but does not null it, leaving a dangling pointer.
From src/rcfg.c (lines 167–171):
167 static void handle_drop(rcfg_ctx *ctx)
168 {
169 ▶ free(ctx->pending);
170 }
171
↳ Referenced in: cleanup: rcfg_decode (src/rcfg.c:254) — free(ctx.pending) frees the same 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 producer of RCF stream bytes (the sole untrusted input, read from a file by rcfg_cli.c:59) triggers a double-free with a two-field payload. The same dangling pointer also enables a use-after-free read via a subsequent FLUSH (ctx->pending->tag at rcfg.c:176) and a repeated free via a subsequent DROP or STAGE (rcfg.c:162,169). Double-free of a heap chunk is a classic allocator corruption primitive; with heap grooming it can lead to controlled reuse. Exploitability beyond the crash is unproven here.
Fix Direction¶
Set ctx->pending = NULL immediately after the free in handle_drop (and after any other release of the pending record), so the pointer never outlives the allocation and the end-of-decode cleanup and later handlers see an empty slot.
Expected sanitizer output¶
==81822==ERROR: AddressSanitizer: attempting double-free on 0x605000000720 in thread T0:
SCARINESS: 42 (double-free)
#0 0x000100999258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x0001002bca8c in rcfg_decode rcfg.c:254
#2 0x0001002be418 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 0x000100999258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x0001002bdb04 in handle_drop rcfg.c:169
#2 0x0001002bcdac in dispatch rcfg.c:213
#3 0x0001002bca4c in rcfg_decode rcfg.c:247
#4 0x0001002be418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
previously allocated by thread T0 here:
#0 0x000100999164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x0001002bd8cc in handle_stage rcfg.c:155
#2 0x0001002bcda0 in dispatch rcfg.c:212
#3 0x0001002bca4c in rcfg_decode rcfg.c:247
#4 0x0001002be418 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
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 = 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/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=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 limited trigger shape → MAT:P