← all crashes · CRASH-0013 · samples/sample-c · S3 · accepted · filed 2026-09-16 14:37 UTC

CRASH-005-1: double-free in rcfg_decode

High8.9double-freecli
#1 rcfg_decode @ rcfg.c:254
#2 start
#3 handle_drop @ rcfg.c:169
Repro 5/5Cluster CL-af1113b8 (4 reports: CRASH-0014, CRASH-0019, CRASH-0016)
The RCF decoder stages a record on the heap for a later commit and lets a DROP field discard it. The DROP handler frees the staged record but leaves the context pointer dangling, so the pointer is freed a second time. A stream of just two fields — STAGE (type 0x05) then DROP (type 0x06) — frees the same heap block twice: once in handle_drop (rcfg.c:169) and again in the terminal cleanup free(ctx.pending) (rcfg.c:254). The external party triggers a double-free from a ~10-byte input.
Act on it
Fix
patch.diff is captured beside this report — Set `ctx->pending = NULL` after freeing in `handle_drop`..
Where
src/rcfg.c:handle_drop:169 · reached through the cli surface
Reproduce
run ./reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.bin (10 B); input repro.cmd (96 B); sanitizer log sanitizer.txt (8 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 decoded by rcfg_decode (CLI file input, attacker_controls=bytes); attacker controls the field sequence (STAGE then DROP) encoded in the input stream; needs the caller to reads a file and calls rcfg_decode(data, len) once over the exact length.

Summary

The RCF decoder stages a record on the heap for a later commit and lets a DROP field discard it. The DROP handler frees the staged record but leaves the context pointer dangling, so the pointer is freed a second time. A stream of just two fields — STAGE (type 0x05) then DROP (type 0x06) — frees the same heap block twice: once in handle_drop (rcfg.c:169) and again in the terminal cleanup free(ctx.pending) (rcfg.c:254). The external party triggers a double-free from a ~10-byte input.

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 the field sequence (STAGE then DROP) encoded in the input stream
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 S3— Spec vs. implementation
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_stage allocates an rcfg_record and stores it in ctx->pending (rcfg.c:155-163). handle_drop is meant to discard it: it calls free(ctx->pending) (rcfg.c:169) but does not reset ctx->pending to NULL. The pointer therefore still references freed memory. rcfg_decode always performs a final free(ctx.pending) after the parse loop (rcfg.c:254) to release any uncommitted record; because DROP left the pointer dangling, this frees the same block a second time. The invariant the cleanup assumes — that ctx.pending is either NULL or a live allocation — is violated by DROP. The same dangling pointer also enables a use-after-free through FLUSH (handle_flush reads ctx->pending->tag, rcfg.c:176) and a second double-free through a repeated DROP.

Data Flow

Two fields in the input byte stream stage then drop the same record.

Source snippets referenced in Data Flow:

↳ Referenced in: step: main (src/rcfg_cli.c:59) — hands the input buffer/length to 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: step: rcfg_decode (src/rcfg.c:247) — dispatches STAGE (0x05) to handle_stage.

From src/rcfg.c (lines 245–249):

245       }
246   
247 ▶     dispatch(&ctx, data, len, type, p, flen);
248   
249       p += flen;

↳ Referenced in: step: handle_stage (src/rcfg.c:155-163) — mallocs an 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: handle_drop (src/rcfg.c:169) — frees ctx->pending but leaves the pointer dangling.

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) — terminal free(ctx.pending) frees the already-freed 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 decoding an untrusted RCF stream is exposed with a ~10-byte input. A double-free is a memory-safety violation that, depending on the allocator, can be escalated to controlled reuse of freed memory; the companion use-after-free (STAGE, DROP, FLUSH) reads through the dangling pointer. Exploitability beyond a crash depends on heap state, but the free/free and free/use hazards are direct.

Fix Direction

Set ctx->pending = NULL after freeing in handle_drop. The same reset should follow every free of ctx->pending (handle_stage already frees before replacing, which is safe because it immediately reassigns); making DROP null the pointer closes both the double-free and the FLUSH use-after-free.

Expected sanitizer output

==78101==ERROR: AddressSanitizer: attempting double-free on 0x605000000720 in thread T0:
SCARINESS: 42 (double-free)
    #0 0x000100ab1258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
    #1 0x000100448a8c in rcfg_decode rcfg.c:254
    #2 0x00010044a418 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 0x000100ab1258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
    #1 0x000100449b04 in handle_drop rcfg.c:169
    #2 0x000100448dac in dispatch rcfg.c:213
    #3 0x000100448a4c in rcfg_decode rcfg.c:247
    #4 0x00010044a418 in main rcfg_cli.c:59
    #5 0x00018d1844e0 in start (in dyld) + 6988

previously allocated by thread T0 here:
    #0 0x000100ab1164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
    #1 0x0001004498cc in handle_stage rcfg.c:155
    #2 0x000100448da0 in dispatch rcfg.c:212
    #3 0x000100448a4c in rcfg_decode rcfg.c:247
    #4 0x00010044a418 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 (9 lines)

--- a/src/rcfg.c+++ b/src/rcfg.c@@ -167,4 +167,5 @@ static void handle_drop(rcfg_ctx *ctx) static void handle_drop(rcfg_ctx *ctx) {-  free(ctx->pending);+  free(ctx->pending);+  ctx->pending = 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-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