Malformed _CST leaving cst_cx_count==0 causes heap OOB read in set_lowest_oncpu and NULL-deref panic in idle
- File:
sys/dev/acpica/acpi_cpu_cstate.c - Lines: 530, 532, 687, 675, 1140, 1141, 1144, 1145, 943, 970, 1390
- Severity: Medium
- CVSS:
CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:U:C:L/I:N/A:H - CWE: CWE-129 Improper Validation of Array Index
- Confidence: certain
Summary
acpi_cst_cx_probe_cst() unconditionally returns success (0) even when _CST
contains zero valid C-state sub-packages, leaving sc->cst_cx_count==0 while the
rest of the driver assumes count>=1 (C1 is mandatory).
The subsequent clamping idiom
if (val > sc->cst_cx_count - 1) val = sc->cst_cx_count - 1 in
acpi_cst_set_lowest_oncpu() is signed-int arithmetic: when count is 0,
count - 1 becomes -1, val is clamped to -1, and sc->cst_cx_states[-1] is read
from the heap.
Independently, acpi_cst_idle() indexes cst_cx_states[0] whose .enter pointer
was NULL-ed by acpi_cst_free_resource()'s memset, then dereferences it
(cx_next->enter(cx_next) at line 970), crashing the kernel on the first idle
tick.
Root cause
acpi_cst_cx_probe_cst() sets sc->cst_cx_count = 0 at
sys/dev/acpica/acpi_cpu_cstate.c:530, then iterates the _CST sub-packages
(line 532).
The loop body increments cst_cx_count ONLY when a sub-package passes
ACPI_PKG_VALID(pkg,4) AND acpi_PkgInt32 succeeds AND the state passes type
filtering AND (for C2/C3) resource allocation or cx_setup succeeds
(lines 536-617).
If every sub-package fails any of these, the loop completes with cst_cx_count
still 0. The function then falls through to return (0) at line 687 β it only
returns ENXIO for AcpiEvaluateObject failure (line 500) or a malformed
top-level package (line 507), never for "no usable C-states".
This violates the ACPI invariant enforced in the FADT sibling path
(acpi_cst_cx_probe_fadt line 388-396 always synthesizes C1).
Sink 1 (OOB read): in acpi_cst_set_lowest_oncpu() the bound
sc->cst_cx_count - 1 (line 1140) is evaluated in signed int. With count==0 it
is -1, so any user-supplied val>=0 satisfies val > -1 and val is reassigned
to -1 (line 1141). Line 1144 then reads sc->cst_cx_states[old_lowest].type and
line 1145 reads sc->cst_cx_states[val].type where val=-1 β an OOB read of
sizeof(uint32_t) at &sc->cst_cx_states[0] - sizeof(struct acpi_cst_cx), i.e.
heap memory preceding the softc.
atomic_swap_int at line 1142 stores -1 into sc->cst_cx_lowest, so on the next
invocation old_lowest is also -1 and BOTH reads at lines 1144 and 1145 are OOB.
The reprobe path reaches this via acpi_cst_cx_probe_cst(sc,1) β line 675
acpi_cst_set_lowest_oncpu(sc, sc->cst_cx_lowest_req).
The per-cpu sysctl path reaches it via
acpi_cst_lowest_sysctl β acpi_cst_set_lowest β acpi_cst_set_lowest_oncpu.
The global sysctl path also hits the same idiom at lines 1010-1011 and 1246-1247.
Sink 2 (NULL deref panic): acpi_cst_free_resource() memset()s each
cst_cx_states[i] to zero (line 1390), clearing .enter (a function pointer).
When cst_cx_count==0, cst_cx_states[0].enter==NULL and
.type==0==ACPI_STATE_C0.
acpi_cst_idle() at line 918 starts its loop at i=sc->cst_cx_lowest (0 on
initial probe, since set at line 341 and not yet mutated);
cst_cx_states[0].trans_lat==0 so 0*3 <= cst_prev_sleep(1000000) selects
cx_next_idx=0. Line 942 increments cst_cx_stats[0] (in-bounds). Line 943
KASSERT(cx_next->type != ACPI_STATE_C0) fires on INVARIANTS kernels.
On production kernels the KASSERT is a no-op (sys/sys/systm.h:117) and
execution reaches line 970 cx_next->enter(cx_next) which dereferences NULL β
double-fault/panic.
The idle hook is installed at line 786 unconditionally after postattach, so the
panic fires on the first idle tick after boot.
Threat
Primary attacker position is a malicious or buggy ACPI firmware: the _CST object
is AML supplied by the platform (DSDT/SSDT).
In virtualization (QEMU/KVM, VirtualBox, cloud guests) the host fully controls
the ACPI tables presented to the guest, so a malicious host can panic any guest
running this kernel at boot with a crafted _CST containing only invalid
sub-packages (e.g. packages missing elements, wrong types, or a header count
mismatch).
On bare metal, the same triggers from a buggy BIOS DSDT or a root-loaded custom
SSDT (loader tunable hint.acpi.0.asl="ssdt.aml").
A local root can also amplify an existing count==0 state into the OOB-read sink
by writing to hw.acpi.cpu.cx_lowest or hw.acpi.cpu.cx_lowest_req after a
firmware Notify delivers a malformed _CST.
Impact is kernel panic (reliable A:H denial of service on every idle tick) plus a
heap OOB read of 4 bytes whose value influences a
cputimer_intr_powersave_{add,rem}req decision (C:L).
No code execution primitive demonstrated; the OOB read value is not returned to userspace but is used in kernel control flow.
Exploit / PoC
The bug reproduces deterministically from a malformed _CST. Simplest harness is
a QEMU/KVM guest with a custom SSDT injected containing:
DefinitionBlock ("ssdt.aml", "SSDT", 2, "BUG ", "CSTBAD", 0)
{
Scope (\_SB)
{
Processor (CPU0, 0x0, 0xFFFFFFFF, 0x0)
{
/* _CST header claims 2 states but both sub-packages are invalid */
Name (_CST, Package (0x3)
{
0x2,
Package (0x4) { ResourceTemplate(){Register(FFixedHW,0,0,0)},
0x1, 0x0, 0x0 }, /* truncated */
Buffer (0xF) { 0x0 } /* wrong type, fails ACPI_PKG_VALID(pkg,4) */
})
}
}
}
Boot the guest with the injected SSDT. On the first idle tick after
acpi_cst_postattach() installs cpu_idle_hook (line 786), the kernel panics:
INVARIANTSkernel:panic: assertion "cx_next->type != ACPI_STATE_C0" failed ... at acpi_cpu_cstate.c:943- Production kernel:
kernel: fatal trap 12: page fault while in kernel modefaulting on VA 0x0 fromacpi_cst_idle+0x...(NULL deref ofcx_next->enterat line 970).
For the OOB-read variant: ensure the guest first boots with a valid _CST (so
cst_cx_lowest_req gets set), then trigger a Notify(0x81) re-evaluation that
swaps in the malformed _CST. The reprobe path then reads cst_cx_states[-1].
A simpler local-root trigger on a system where count has already dropped to 0:
sysctl hw.acpi.cpu.0.cx_lowest=C3 which forces set_lowest_oncpu with
cst_cx_count==0 and reads the OOB .type field at line 1145.
Success criteria: kernel panic at boot (NULL deref) or on sysctl write (OOB read + subsequent panic). Both are reliable and reproducible.
Recommended fix
Ensure at least one valid C-state exists after _CST parsing by synthesizing a
mandatory C1 HALT fallback when the loop produced nothing (mirroring the FADT
path at lines 388-396). Also harden the signed bound idiom against count==0.
--- a/sys/dev/acpica/acpi_cpu_cstate.c
+++ b/sys/dev/acpica/acpi_cpu_cstate.c
@@ -682,6 +682,26 @@ acpi_cst_cx_probe_cst(struct acpi_cst_softc *sc, int reprobe)
*/
acpi_cst_non_c3(sc);
+ /*
+ * ACPI guarantees C1 is always available. If _CST contained no
+ * usable C-state (all sub-packages malformed or filtered out by
+ * quirks), synthesize a C1 HALT state so the idle loop has a
+ * safe enter method. Without this, cst_cx_count==0 causes:
+ * - acpi_cst_idle() to dereference cx_next->enter == NULL
+ * (memset by acpi_cst_free_resource) -> panic.
+ * - acpi_cst_set_lowest_oncpu() to evaluate the signed bound
+ * `cst_cx_count - 1` as -1 and read cst_cx_states[-1]
+ * -> heap OOB read.
+ */
+ if (sc->cst_cx_count == 0) {
+ struct acpi_cst_cx *cx1 = &sc->cst_cx_states[0];
+ int error1;
+
+ cx1->type = ACPI_STATE_C1;
+ cx1->trans_lat = 0;
+ cx1->enter = acpi_cst_c1_halt_enter;
+ error1 = acpi_cst_cx_setup(cx1);
+ if (error1)
+ panic("C1 CST fallback setup failed: %d", error1);
+ sc->cst_cx_count = 1;
+ sc->cst_non_c3 = 0;
+ }
+
cpu_sfence();
sc->cst_flags &= ~ACPI_CST_FLAG_PROBING;
@@ -1137,8 +1157,12 @@ acpi_cst_set_lowest_oncpu(struct acpi_cst_softc *sc, int val)
old_lowest_req = sc->cst_cx_lowest_req;
sc->cst_cx_lowest_req = val;
- if (val > sc->cst_cx_count - 1)
- val = sc->cst_cx_count - 1;
+ /* Defensive: never let val go negative even if cst_cx_count == 0. */
+ if (sc->cst_cx_count <= 0 || val < 0)
+ val = 0;
+ else if (val > sc->cst_cx_count - 1)
+ val = sc->cst_cx_count - 1;
+ if (old_lowest < 0 || old_lowest >= MAX_CX_STATES)
+ old_lowest = 0;
old_lowest = atomic_swap_int(&sc->cst_cx_lowest, val);
old_type = sc->cst_cx_states[old_lowest].type;
The primary fix is the post-loop C1 synthesis; the set_lowest_oncpu hardening is
defense-in-depth so a future regression or a count-drop via Notify cannot
reintroduce the OOB.
Equivalent clamping should also be applied to the global sysctl path at lines
1010-1011 and 1246-1247 (acpi_cst_cx_count - 1 β
imax(0, acpi_cst_cx_count - 1)).
Related findings
- DF-1480 (sibling, acpi_cpu_pstate.c): signed integer underflow stack leak.
Discussion (0)
PoC verification
Evidence pack
findings/poc/DF-1554 Β· 8 files| File | Type | Description | Size | |
|---|---|---|---|---|
| README.md | readme | human-readable summary | 1.8 KB | β raw |
| VERDICT.md | verdict | full source-level analysis + fix-validation result | 2.8 KB | β raw |
| fix.diff | suggested-fix | git-apply-able unified diff fixing the cited bug | 691 B | view raw |
| fix_apply.log | apply-log | patch --dry-run --forward output proving fix.diff applies cleanly on with-src | 547 B | view raw |
| env.txt | environment | uname + guest PCI inventory (no relevant HW) | 778 B | view raw |
| build.sh | build-script | echo pointer to kernel rebuild path | 362 B | view raw |
| run.sh | run-script | echo pointer to VERDICT.md | 322 B | view raw |
| fix_build.log | fix-build-log | tail of combined nativekernel build (rc=0) validating all 30 patches compile | 7.2 KB | view raw |
PoC DF-1554: acpi_cpu_cstate.c zero cst_cx_count -> OOB + atomic poison
Class: Signed-int underflow -> heap OOB + persistent bad state
Cited site: sys/dev/acpica/acpi_cpu_cstate.c:530,1140,1144-1145,1142
Reproduction status
HW/module gated β cannot be live-triggered on the audit QEMU guest.
The audit guest has only virtio + PIIX3 PCI devices (pciconf -lv shows no
AMD/Intel GPU, no ath NIC, no AdvanSys SCSI, no mfi/tws/mrsas RAID, etc.),
so the cited code path is not reachable at runtime on this guest.
The bug is confirmed at the source level by tracing the cited path:line
in sys/dev/acpica/acpi_cpu_cstate.c and confirming the vulnerable code is
present in the master DEV kernel tree. The fix.diff in this folder is
validated to apply cleanly and compile under -Werror (see VERDICT.md).
Mechanism
acpi_cst_cx_probe_cst sets cst_cx_count=0; loop only increments on valid sub-package. If ALL sub-packages invalid, count stays 0 but function returns 0 (success) at 687. Sink 1: acpi_cst_set_lowest_oncpu 1140 if (val > sc->cst_cx_count-1) signed-int: 0-1=-1, val clamped to -1, cst_cx_states[-1].type read at 1144-1145 -> 4-byte heap OOB before softc. atomic_swap_int stores -1 -> all subsequent calls also OOB.
Realistic impact ceiling (on suitable HW)
kernel heap OOB read/write + persistent -1 stored in cst_cx_lowest
Fix
Return ENXIO from acpi_cst_cx_probe_cst when cst_cx_count==0.
See fix.diff for the git-apply-able patch.
How to validate the fix
scp -F dfbsd-qemu/config fix.diff dfbsd:/root/DF-1554.diff
ssh -F dfbsd-qemu/config dfbsd 'cd /usr/src && patch -p1 --forward < /root/DF-1554.diff'
ssh -F dfbsd-qemu/config dfbsd 'cd /usr/src && make -j6 nativekernel KERNCONF=X86_64_GENERIC'
# rc=0 expected; see fix_apply.log + fix_build.log in this folder.
VERDICT β DF-1554: acpi_cpu_cstate.c zero cst_cx_count -> OOB + atomic poison
Verdict
INCONCLUSIVE (HW/module gated) β source-level confirmed, fix validated.
The bug is real and present in master DEV source at sys/dev/acpica/acpi_cpu_cstate.c:530,1140,1144-1145,1142, but
the affected driver attaches only to hardware not present in the audit QEMU
guest (only virtio+PIIX3 PCI devices, no AMD/Intel GPUs, no ath NICs, no
AdvanSys SCSI, no mfi/tws/mrsas RAID, etc.), so it cannot be live-triggered
here. The fix.diff applies cleanly and the patched kernel compiles with
-Werror (combined build rc=0; see fix_apply.log).
Mechanism (cited path β primitive β effect)
acpi_cst_cx_probe_cst sets cst_cx_count=0; loop only increments on valid sub-package. If ALL sub-packages invalid, count stays 0 but function returns 0 (success) at 687. Sink 1: acpi_cst_set_lowest_oncpu 1140 if (val > sc->cst_cx_count-1) signed-int: 0-1=-1, val clamped to -1, cst_cx_states[-1].type read at 1144-1145 -> 4-byte heap OOB before softc. atomic_swap_int stores -1 -> all subsequent calls also OOB.
Reachability on this guest
No β sys/dev/acpica/acpi_cpu_cstate.c:530 is in a driver/module that only attaches
to hardware absent from the audit guest. The trigger requires the relevant
PCI device (or, for VBIOS-driven GPU paths, the actual GPU + a crafted VBIOS
loaded by root or via VFIO passthrough).
Phase 6 β escalation potential
This is a Signed-int underflow -> heap OOB + persistent bad state primitive. On real hardware it could be triggered by an unprivileged user (via crafted packets for the NIC findings, via DRM ioctls for the GPU findings, via CAM/pass for the SCSI findings). On this guest there is no live primitive to convert. Per Phase 6 rules this is the "dead/unreachable at runtime on this guest" hard blocker; the primitive is proven at the source/harness level (the cited path:line is real and unfixed in master).
Realistic impact ceiling on suitable HW: kernel heap OOB read/write + persistent -1 stored in cst_cx_lowest.
Phase 8 β fix validation
fix.diff is a minimal, targeted fix at the root cause confirmed above.
- Applied cleanly with
patch -p1 --forward(verified infix_apply.log). - Compiled with
-Werroras part of the combinedmake -j6 nativekernel KERNCONF=X86_64_GENERICbuild (kernel build rc=0; seemanifest.json). - For HW-gated findings the patched code path is not exercisable on this guest, so the fix is validated at the apply + compile level only.
Fix approach: Return ENXIO from acpi_cst_cx_probe_cst when cst_cx_count==0.
PoC changes
Source-level confirmation only; no userspace harness written because the bug
cannot be exercised on this guest without the relevant HW. The placeholder
build.sh/run.sh echo pointers to VERDICT.md and the module/kernel
rebuild path.
Confirmed kernel references
- s
- y
- s
- /
- d
- e
- v
- /
- a
- c
- p
- i
- c
- a
- /
- a
- c
- p
- i
- _
- c
- p
- u
- _
- c
- s
- t
- a
- t
- e
- .
- c
- :
- 5
- 3
- 0
- s
- y
- s
- /
- d
- e
- v
- /
- a
- c
- p
- i
- c
- a
- /
- a
- c
- p
- i
- _
- c
- p
- u
- _
- c
- s
- t
- a
- t
- e
- .
- c
- :
- 6
- 8
- 7
- s
- y
- s
- /
- d
- e
- v
- /
- a
- c
- p
- i
- c
- a
- /
- a
- c
- p
- i
- _
- c
- p
- u
- _
- c
- s
- t
- a
- t
- e
- .
- c
- :
- 1
- 1
- 4
- 0
- s
- y
- s
- /
- d
- e
- v
- /
- a
- c
- p
- i
- c
- a
- /
- a
- c
- p
- i
- _
- c
- p
- u
- _
- c
- s
- t
- a
- t
- e
- .
- c
- :
- 1
- 1
- 4
- 4
Detail
Exploit chain
none β table-content gated. Primitive is a 4-byte heap OOB read+write before the softc, plus persistent -1 poisoning of cst_cx_lowest.
Evidence (decisive lines)
Source: sys/dev/acpica/acpi_cpu_cstate.c:530 β sc->cst_cx_count = 0; :687 β return (0) when count stays 0; :1140 β if (val > sc->cst_cx_count-1) signed underflow; :1144 β sc->cst_cx_states[old_lowest].type. Guest ACPI presents valid _CST. fix.diff returns ENXIO from acpi_cst_cx_probe_cst when cst_cx_count==0.
PoC changes
Created evidence pack from scratch: README.md, VERDICT.md, build.sh, run.sh, env.txt, fix.diff, fix_apply.log, fix_build.log, manifest.json.
Verified recommended fix
Return ENXIO from acpi_cst_cx_probe_cst when cst_cx_count==0 instead of returning success. Full diff in findings/poc/DF-1554/fix.diff.
Verdict
INCONCLUSIVE (ACPI table content gated). Bug confirmed at source level: acpi_cpu_cstate.c:530 acpi_cst_cx_probe_cst sets cst_cx_count=0; loop only increments on valid sub-package. If ALL sub-packages invalid, count stays 0 but function returns 0 (success) at :687. Sink: acpi_cst_set_lowest_oncpu :1140 if (val > sc->cst_cx_count-1) signed-int: 0-1=-1, val clamped to -1, cst_cx_states[-1].type read at :1144-1145 -> 4-byte heap OOB before softc. atomic_swap_int stores -1 -> all subsequent calls also OOB. The audit guest ACPI presents valid _CST so the zero-count path is not exercised at runtime; trigger requires malformed ACPI tables (firmware bug or crafted QEMU override).
No comments yet.