CRASH-6: heap-use-after-free in handle_flush
handle_flush @ rcfg.c:176dispatch @ rcfg.c:214rcfg_decode @ rcfg.c:2470x05), DROP (0x06), FLUSH (0x07) — reads through the dangling pointer. The attacker gains a use-after-free read whose result (the record tag) is folded into decoder state; combined with heap grooming it becomes a type-confusion / information-leak primitive../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.bin (14 B); sanitizer log sanitizer.txt (3 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder stages a heap record on STAGE, discards it on DROP, and commits it on FLUSH. DROP frees the pending record without clearing the pointer, so a following FLUSH dereferences freed memory. A 15-byte stream — magic, STAGE (0x05), DROP (0x06), FLUSH (0x07) — reads through the dangling pointer. The attacker gains a use-after-free read whose result (the record tag) is folded into decoder state; combined with heap grooming it becomes a type-confusion / information-leak primitive.
Fields¶
| Field | Value |
|---|---|
| Severity | Medium5.5 |
| Primitive | heap-use-after-free |
| Location | src/rcfg.c:handle_flush:176 |
| 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: handle_flush rcfg.c:176) |
Root Cause¶
handle_drop (src/rcfg.c:169) frees ctx->pending but leaves the pointer set, violating the "ctx->pending is NULL or live" invariant. handle_flush (src/rcfg.c:173-178) then tests if (ctx->pending != NULL) — which passes for a dangling pointer — and reads ctx->pending->tag (line 176) out of the freed 4-byte block. The NULL check is the wrong guard: after a DROP the pointer is non-NULL but no longer valid. If the freed chunk has been reused by an intervening allocation (e.g. a later STAGE or any other malloc), the tag read returns attacker-influenced contents.
Data Flow¶
Three ordered fields free then read the record.
- step: handle_stage (src/rcfg.c:155-163) — STAGE allocates the record.
- step: handle_drop (src/rcfg.c:169) — DROP frees it, pointer left dangling.
- step: dispatch (src/rcfg.c:214) — FLUSH routes to
handle_flush. - step: handle_flush (src/rcfg.c:176) — reads
ctx->pending->tagfrom the freed block.
Source snippets referenced in Data Flow:
↳ Referenced in: step: handle_stage (src/rcfg.c:155-163) — STAGE allocates the record.
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) — DROP frees it, pointer left 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: dispatch (src/rcfg.c:214) — FLUSH routes to handle_flush.
From src/rcfg.c (lines 212–216):
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;
216 case RCFG_T_CHECK: handle_check(ctx, val, len); break;
↳ Referenced in: step: handle_flush (src/rcfg.c:176) — reads ctx->pending->tag from the freed block.
From src/rcfg.c (lines 174–178):
174 {
175 if (ctx->pending != NULL) {
176 ▶ ctx->last_tag = ctx->pending->tag;
177 }
178 }
Impact¶
Any consumer of untrusted RCF streams is exposed. A 15-byte input causes a 1-byte heap-use-after-free READ (ASan, SCARINESS 40). The stale byte becomes ctx->last_tag; with heap grooming the freed chunk can be reallocated with attacker data, turning the read into a controlled value and a stepping stone to further corruption. (This same DROP defect also yields the CRASH-5 double-free.)
Fix Direction¶
Fix at the source: null ctx->pending in handle_drop after freeing. Defense in depth: handle_flush should copy out the record's data at STAGE/commit time rather than holding a raw pointer, so a stale pointer can never be dereferenced.
Expected sanitizer output¶
==73708==ERROR: AddressSanitizer: heap-use-after-free on address 0x605000000720 at pc 0x000104ec1be4 bp 0x00016af3dab0 sp 0x00016af3daa8
READ of size 1 at 0x605000000720 thread T0
SCARINESS: 40 (1-byte-read-heap-use-after-free)
==73708==WARNING: failed to spawn external symbolizer (errno: 9)
==73708==WARNING: failed to spawn external symbolizer (errno: 9)
==73708==WARNING: failed to spawn external symbolizer (errno: 9)
==73708==WARNING: failed to spawn external symbolizer (errno: 9)
==73708==WARNING: failed to spawn external symbolizer (errno: 9)
==73708==WARNING: Failed to use and restart external symbolizer!
#0 0x000104ec1be0 in handle_flush rcfg.c:176
#1 0x000104ec0db8 in dispatch rcfg.c:214
#2 0x000104ec0a4c in rcfg_decode rcfg.c:247
#3 0x000104ec2418 in main rcfg_cli.c:59
#4 0x00018d1844e0 in start (in dyld) + 6988
0x605000000720 is located 0 bytes inside of 4-byte region [0x605000000720,0x605000000724)
freed by thread T0 here:
#0 0x0001055d9258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000104ec1b04 in handle_drop rcfg.c:169
#2 0x000104ec0dac in dispatch rcfg.c:213
#3 0x000104ec0a4c in rcfg_decode rcfg.c:247
#4 0x000104ec2418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
previously allocated by thread T0 here:
#0 0x0001055d9164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000104ec18cc in handle_stage rcfg.c:155
#2 0x000104ec0da0 in dispatch rcfg.c:212
#3 0x000104ec0a4c in rcfg_decode rcfg.c:247
#4 0x000104ec2418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
SUMMARY: AddressSanitizer: heap-use-after-free (targets/samples/sample-c/build-asan/rcfg:arm64+0x100001be0) in handle_flush+0xcc
Full original output: sanitizer.txt.
Annotated stack frames (top-of-stack source):
#0 handle_flush — rcfg.c:176
From src/rcfg.c (lines 174–178):
174 {
175 if (ctx->pending != NULL) {
176 ▶ ctx->last_tag = ctx->pending->tag;
177 }
178 }
#1 dispatch — rcfg.c:214
From src/rcfg.c (lines 212–216):
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;
216 case RCFG_T_CHECK: handle_check(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_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;
#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:N/VI:N/VA:L/SC:N/SI:N/SA:N/E:P → 5.5 (Medium) CVSS-B baseline: CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:N/VA:L/SC:N/SI:N/SA:N → 6.9 (Medium)
| 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 | N/N/L | primitive class = uaf_read |
| 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: Medium — 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=uaf_read → VC:N/VI:N/VA:L/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