CRASH-006-1: heap-use-after-free in handle_flush
handle_flush @ rcfg.c:176dispatch @ rcfg.c:214rcfg_decode @ rcfg.c:247RCF1 followed by three fields: STAGE (type 0x05), DROP (type 0x06), FLUSH (type 0x07). FLUSH then reads one byte of freed heap memory. ASan reports a heap-use-after-free READ of size 1 at handle_flush (src/rcfg.c:176), on the 4-byte record allocated by handle_stage (src/rcfg.c:155) and freed by handle_drop (src/rcfg.c:169)../reproduce.sh — it clones the audited revision, builds with the sanitizer and replays the input; input input.bin (14 B); input repro.cmd (96 B); sanitizer log sanitizer.txt (16 KB); reproduced 5/5 times when re-run.Summary¶
The RCF decoder stages a small heap record for a later commit and lets the stream discard it before committing. Discarding frees the record but leaves the context pointer aimed at the freed block, so a following commit reads through that dangling pointer. The shortest reaching stream is the 4-byte magic RCF1 followed by three fields: STAGE (type 0x05), DROP (type 0x06), FLUSH (type 0x07). FLUSH then reads one byte of freed heap memory. ASan reports a heap-use-after-free READ of size 1 at handle_flush (src/rcfg.c:176), on the 4-byte record allocated by handle_stage (src/rcfg.c:155) and freed by handle_drop (src/rcfg.c:169).
Fields¶
| Field | Value |
|---|---|
| Severity | Medium5.5 |
| Class | memory-safety |
| Primitive | heap-use-after-free |
| Location | src/rcfg.c:handle_flush:176 |
| Surface | cli— ASan frames are inside the target's shipped binary |
| Boundary | rcfg_decode public API — caller passes untrusted RCF stream bytes. |
| Trigger source | bytes |
| Caller controls | full stream contents, including field type bytes and their order (STAGE/DROP/FLUSH). |
| Caller contract | obeyed |
| Trusted caller actions | calls rcfg_decode(data, len) with data pointing at len readable bytes. |
| Parameter control | direct |
| Reproduction rate | 5/5 |
| Reproducer carrier | cli |
| Advisory | yes — no patch.diff — fix described in prose, see Fix Direction |
| Strategy | S3— Spec vs. implementation |
| Dedup frames | handle_drop rcfg.c:169 -> dispatch rcfg.c:213 -> rcfg_decode rcfg.c:247 (use: handle_flush rcfg.c:176) |
Root Cause¶
The decode context holds rcfg_record *pending, the record staged by RCFG_T_STAGE and awaiting an RCFG_T_FLUSH commit. handle_stage (src/rcfg.c:153) allocates a 4-byte rcfg_record and stores it in ctx->pending. handle_drop (src/rcfg.c:167) is meant to discard the staged record; it does free(ctx->pending) at line 169 but never clears the pointer, so ctx->pending still holds the address of the just-freed block. The code assumes "pending is discarded" means "pending is unusable", but it only frees the storage and leaves the pointer live. handle_flush (src/rcfg.c:173) guards its commit with if (ctx->pending != NULL) (line 175) and then reads ctx->pending->tag (line 176). That guard tests the pointer for NULL, not for liveness: after a DROP the pointer is non-NULL but dangling, so the read dereferences freed memory. The field type is caller-selected in each field header, so an untrusted stream chooses the STAGE→DROP→FLUSH order freely. This is the same dangling-pointer root cause as the DROP double-free (CRASH-005) but a distinct sink and diagnostic: a use-after-free READ at line 176 rather than a second free.
Data Flow¶
An untrusted stream stages a record, drops it (freeing without clearing the pointer), then flushes it, reading the freed block.
- parse: rcfg_decode (src/rcfg.c:235) — walks TLV fields, dispatching each by caller-chosen type byte.
- stage: handle_stage (src/rcfg.c:155) — malloc()s a 4-byte rcfg_record and stores it in ctx->pending.
- drop: handle_drop (src/rcfg.c:169) — free(ctx->pending) but leaves ctx->pending pointing at the freed block.
- commit: handle_flush (src/rcfg.c:176) — non-NULL guard passes; reads ctx->pending->tag from freed memory (UAF READ).
Source snippets referenced in Data Flow:
↳ Referenced in: parse: rcfg_decode (src/rcfg.c:235) — walks TLV fields, dispatching each by caller-chosen type byte.
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: stage: handle_stage (src/rcfg.c:155) — malloc()s a 4-byte 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: drop: handle_drop (src/rcfg.c:169) — free(ctx->pending) but leaves ctx->pending pointing at the freed block.
From src/rcfg.c (lines 167–171):
167 static void handle_drop(rcfg_ctx *ctx)
168 {
169 ▶ free(ctx->pending);
170 }
171
↳ Referenced in: commit: handle_flush (src/rcfg.c:176) — non-NULL guard passes; reads ctx->pending->tag from freed memory (UAF READ).
From src/rcfg.c (lines 174–178):
174 {
175 if (ctx->pending != NULL) {
176 ▶ ctx->last_tag = ctx->pending->tag;
177 }
178 }
Impact¶
Any caller that feeds an untrusted RCF stream to rcfg_decode can force a read of one byte of freed heap memory during decode. The read value lands in ctx->last_tag, a decode summary a caller may read back, so the disclosed byte is observable rather than purely internal. With allocator reuse between the DROP and FLUSH the byte can be attacker-influenced freed-slot contents. Impact beyond a one-byte out-of-lifetime read (e.g. corruption) is not demonstrated here; the double-free variant is filed separately as CRASH-005.
Fix Direction¶
In handle_drop set ctx->pending = NULL immediately after free(ctx->pending) so the discarded record cannot be dereferenced or re-freed. This single change also closes the CRASH-005 double-free path. Auditing the other writers, handle_stage already frees any prior pending before replacing it, and the final free(ctx.pending) in rcfg_decode becomes safe once DROP clears the pointer. A guard hardening in handle_flush alone would not suffice because the same dangling pointer is also re-freed elsewhere.
Expected sanitizer output¶
==79254==ERROR: AddressSanitizer: heap-use-after-free on address 0x605000000720 at pc 0x000100b19be4 bp 0x00016f2e4a90 sp 0x00016f2e4a88
READ of size 1 at 0x605000000720 thread T0
SCARINESS: 40 (1-byte-read-heap-use-after-free)
#0 0x000100b19be0 in handle_flush rcfg.c:176
#1 0x000100b18db8 in dispatch rcfg.c:214
#2 0x000100b18a4c in rcfg_decode rcfg.c:247
#3 0x000100b1a418 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 0x0001012d1258 in wrap_free (in libclang_rt.asan_osx_dynamic.dylib) + 124
#1 0x000100b19b04 in handle_drop rcfg.c:169
#2 0x000100b18dac in dispatch rcfg.c:213
#3 0x000100b18a4c in rcfg_decode rcfg.c:247
#4 0x000100b1a418 in main rcfg_cli.c:59
#5 0x00018d1844e0 in start (in dyld) + 6988
previously allocated by thread T0 here:
#0 0x0001012d1164 in wrap_malloc (in libclang_rt.asan_osx_dynamic.dylib) + 120
#1 0x000100b198cc in handle_stage rcfg.c:155
#2 0x000100b18da0 in dispatch rcfg.c:212
#3 0x000100b18a4c in rcfg_decode rcfg.c:247
#4 0x000100b1a418 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)
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