FIND-001: CHECK handler bounds a caller-controlled memcpy with only a debug assert (stack-buffer-overflow in NDEBUG builds)
assert, not a runtime check, so in any build compiled with -DNDEBUG the bound disappears and the copy runs at the caller-chosen length. The shortest reaching stream is the 4-byte magic RCF1 followed by one field with type byte 0x09 (CHECK), a big-endian u16 length greater than 32, and that many value bytes. handle_check (src/rcfg.c:198) then does memcpy(ctx->check, val, len) into the 32-byte ctx->check, writing caller-controlled bytes past the end of the stack-allocated rcfg_ctx. ASan on an -O2 -g1 -DNDEBUG build reports a stack-buffer-overflow WRITE of size 200.Fields¶
| Field | Value |
|---|---|
| Severity | High8.1 |
| Primitive | stack_write |
| Location | src/rcfg.c:handle_check:198 |
| Surface | library-api |
| Boundary | rcfg_decode public API — caller passes untrusted RCF stream bytes. |
| Trigger source | bytes |
| Caller controls | full stream contents, including the CHECK field's declared u16 length and its value bytes. |
| Caller contract | obeyed |
| Trusted caller actions | calls rcfg_decode(data, len) with data pointing at len readable bytes. |
| Parameter control | direct |
| Reproducer carrier | library-api |
| Strategy | S7— Adversarial input |
| Dedup key | [loc] src/rcfg.c:198 |
Summary¶
The RCF decoder retains a bounded "self-check" field from an untrusted stream into a fixed 32-byte scratch buffer. The copy is bounded only by a debug assert, not a runtime check, so in any build compiled with -DNDEBUG the bound disappears and the copy runs at the caller-chosen length. The shortest reaching stream is the 4-byte magic RCF1 followed by one field with type byte 0x09 (CHECK), a big-endian u16 length greater than 32, and that many value bytes. handle_check (src/rcfg.c:198) then does memcpy(ctx->check, val, len) into the 32-byte ctx->check, writing caller-controlled bytes past the end of the stack-allocated rcfg_ctx. ASan on an -O2 -g1 -DNDEBUG build reports a stack-buffer-overflow WRITE of size 200.
Root Cause¶
rcfg_ctx ends with uint8_t check[RCFG_CHECK_CAP] where RCFG_CHECK_CAP == 32 (src/rcfg.c:22,41), and the context is a stack local of rcfg_decode (src/rcfg.c:229). handle_check (src/rcfg.c:195) receives len, the field value length that rcfg_decode already clamped to the bytes remaining in the stream (src/rcfg.c:242-245), so len can be anything from 0 up to ~65535 and is fully caller-controlled. The only thing standing between that length and the memcpy is assert(len <= sizeof(ctx->check)) at src/rcfg.c:197. assert is a debug-only invariant: the C standard defines it as a no-op when NDEBUG is defined. The code assumes "a longer field trips the invariant" but that only holds while asserts are compiled in. CMakeLists.txt documents that both Debug and Release configurations are supported, and a Release build defines NDEBUG. In that supported build the assert vanishes and memcpy(ctx->check, val, len) copies up to len caller-chosen bytes into a 32-byte buffer — an out-of-bounds stack write whose length and contents both come from the stream.
Data Flow¶
An untrusted stream declares a CHECK field longer than 32 bytes; the decoder clamps the length only to the stream size, then copies it into a 32-byte stack buffer with no runtime bound.
- parse: rcfg_decode (src/rcfg.c:235) — validates magic, then walks TLV fields.
- length: rcfg_decode (src/rcfg.c:242) — reads the u16 length and clamps it only to bytes remaining in the stream, not to any handler capacity.
- dispatch: dispatch (src/rcfg.c:216) — routes type byte 0x09 to handle_check with (val, len).
- guard: handle_check (src/rcfg.c:197) — assert(len <= 32) is the only bound; compiled out under NDEBUG.
- write: handle_check (src/rcfg.c:198) — memcpy(ctx->check, val, len) writes len caller-controlled bytes into the 32-byte ctx->check, overflowing the stack rcfg_ctx.
Source snippets referenced in Data Flow:
↳ Referenced in: parse: rcfg_decode (src/rcfg.c:235) — validates magic, then walks TLV fields.
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: length: rcfg_decode (src/rcfg.c:242) — reads the u16 length and clamps it only to bytes remaining in the stream, not to any handler…
From src/rcfg.c (lines 240–244):
240
241 /* Clamp the value to what is actually present so a handler never walks off
242 ▶ * the end of the stream on the declared length alone. */
243 if (flen > remaining) {
244 flen = (uint16_t)remaining;
↳ Referenced in: dispatch: dispatch (src/rcfg.c:216) — routes type byte 0x09 to handle_check with (val, len).
From src/rcfg.c (lines 214–218):
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;
217 default: break; /* unknown tag: skip */
218 }
↳ Referenced in: guard: handle_check (src/rcfg.c:197) — assert(len <= 32) is the only bound; compiled out under NDEBUG.
From src/rcfg.c (lines 195–199):
195 static void handle_check(rcfg_ctx *ctx, const uint8_t *val, uint16_t len)
196 {
197 ▶ assert(len <= sizeof(ctx->check));
198 memcpy(ctx->check, val, len);
199 ctx->check_len = len;
↳ Referenced in: write: handle_check (src/rcfg.c:198) — memcpy(ctx->check, val, len) writes len caller-controlled bytes into the 32-byte ctx->check,…
From src/rcfg.c (lines 196–200):
196 {
197 assert(len <= sizeof(ctx->check));
198 ▶ memcpy(ctx->check, val, len);
199 ctx->check_len = len;
200 }
Impact¶
Any caller that decodes an untrusted RCF stream with a Release/NDEBUG build of this library takes a stack out-of-bounds write whose length (up to the stream's remaining bytes, tens of kilobytes) and contents are chosen by the stream. The write starts at the last member of the stack rcfg_ctx and runs forward over adjacent stack frame storage, saved registers, and the return address region, so it is a stack-smashing primitive, not a one-byte nudge. Exploitability depends on frame layout and mitigations; a controlled-length, controlled-content linear stack overwrite from parser input is a memory-safety defect regardless.
Evidence¶
sanitizer-ndebug.txt:-O2 -g1 -DNDEBUG -fsanitize=addressbuild on
check-overflow.rcf — AddressSanitizer: stack-buffer-overflow, WRITE of size 200, [288, 352) 'ctx' <== Memory access at offset 352 overflows this variable, inside rcfg_decode (the inlined handle_check memcpy).
sanitizer-default-assert.txt: the operator's shippedbuild-asanbinary
(asserts on) on the same input aborts with Assertion failed: (len <= sizeof(ctx->check)), function handle_check. This is the debug-only invariant masking the overflow; it is not a security stop in a Release build.
check-overflow.rcf:RCF1+ type 0x09 + u16 length 0x00C8 (200) + 200
value bytes.
Fix Direction¶
Replace the debug assert in handle_check with a runtime bound so the copy is capped in every build configuration, not only debug. The included patch.diff clamps len to sizeof(ctx->check) before the memcpy; rejecting the field outright (return;) is equally acceptable. An assert-only bound is insufficient because Release builds define NDEBUG and compile it out, which is precisely the configuration the CMake project supports.
Patch¶
Captured patch — patch.diff (13 lines)
--- a/src/rcfg.c+++ b/src/rcfg.c@@ -194,7 +194,9 @@ * corrupt stream and trips a debug invariant. */ static void handle_check(rcfg_ctx *ctx, const uint8_t *val, uint16_t len) {- assert(len <= sizeof(ctx->check));+ if (len > sizeof(ctx->check)) {+ len = (uint16_t)sizeof(ctx->check);+ } memcpy(ctx->check, val, len); ctx->check_len = len; }
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:U → 8.1 (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 = stack_write |
| Subseq Conf/Integ/Avail | N/N/N | primitive class |
| Threat Exploit Maturity (E) | U | 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.
Field derivation (each line is a reviewable claim):
- surface=library → AV:N/UI:N
- class=stack_write → 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
- no reproducer artifact or positive reproduction → E:U
- CR/IR/AR not defined — environmental requirements are a deployer concern (CVSS-B baseline)
- caller controls input bytes → MAT:X