Use-after-free via incremental GC snapshot-at-the-beginning violation in the Wasm JSPI implementation
Categories
(Core :: JavaScript: WebAssembly, defect, P3)
Tracking
()
People
(Reporter: tsmith, Unassigned)
References
Details
(5 keywords)
Attachments
(2 files)
Summary
Use-after-free via incremental GC snapshot-at-the-beginning violation in the Wasm JSPI (JavaScript Promise Integration) implementation. The root cause is at /firefox/js/src/wasm/WasmContext.cpp:98 — Context::traceRoots() returns early for major GC, delegating suspended-stack tracing to SuspenderObject::trace(). However, during incremental major GC, the PreWriteBarrier in SuspenderObject::resume() only pushes the suspender onto the mark stack without eagerly tracing its children. If the wasm function completes and leave() → setMoribund() runs before the marker pops the suspender, SuspenderObject::trace() finds the object in Moribund state, isTraceable() returns false, and the stack frames are never traced — allowing objects rooted only via wasm locals to be swept while still reachable through a wasm global.
Affected Code
File: /firefox/js/src/wasm/WasmContext.cpp, lines 93–108
void Context::traceRoots(JSTracer* trc) {
// The suspendedStacks_ contains suspended stacks frames that need to be
// traced only during minor GC. The major GC tracing is happening via
// SuspenderObject::trace.
// Non-suspended stack frames are traced as part of TraceJitActivations.
if (!trc->isTenuringTracer()) {
return; // ← BUG: skips major-GC root marking
}
gc::AssertRootMarkingPhase(trc);
for (auto iter = suspenders_.iter(); !iter.done(); iter.next()) {
SuspenderObject* object = iter.get();
if (object->state() == SuspenderState::Suspended) {
TraceSuspendableStack(trc, object);
}
}
}
The comment claims major-GC tracing is handled by SuspenderObject::trace. But this is only safe for non-incremental GC. During incremental GC, the following sequence defeats it.
File: /firefox/js/src/wasm/WasmPI.cpp, lines 273–279
void SuspenderObject::resume(JSContext* cx) {
MOZ_ASSERT(state() == SuspenderState::Suspended);
setActive(cx);
// Use barrier because object is being removed from the suspendable stack
// from roots.
gc::PreWriteBarrier(this); // ← Marks black + pushes to mark stack only
...
}
gc::PreWriteBarrier(this) dispatches to GCMarker::traverse(JSObject*) which, per /firefox/js/src/gc/Marking.cpp:1073–1075, is pushThing(thing) — it marks the object black and pushes it onto the mark stack, but does not eagerly scan its children. The suspended stack frames remain untraced until the marker pops this entry.
File: /firefox/js/src/wasm/WasmPI.cpp, lines 205–214
void SuspenderObject::trace(JSTracer* trc, JSObject* obj) {
SuspenderObject& suspender = obj->as<SuspenderObject>();
// The SuspenderObject refers stacks frames that need to be traced
// only during major GC to determine if SuspenderObject content is
// reachable from JS.
if (!suspender.isTraceable() || trc->isTenuringTracer()) {
return; // ← Moribund → returns without tracing
}
TraceSuspendableStack(trc, &suspender);
}
File: /firefox/js/src/wasm/WasmPI.h, lines 169–174
bool isTraceable() const {
SuspenderState current = state();
return current == SuspenderState::Active ||
current == SuspenderState::Suspended ||
current == SuspenderState::CalledOnMain; // Moribund → false
}
File: /firefox/js/src/wasm/WasmPI.cpp, lines 296–303 and 225–233
void SuspenderObject::leave(JSContext* cx) {
switch (state()) {
case SuspenderState::Active: {
setMoribund(cx); // ← Called when wasm returns normally
break;
}
...
}
}
void SuspenderObject::setMoribund(JSContext* cx) {
MOZ_ASSERT(state() == SuspenderState::Active);
cx->wasm().leaveSuspendableStack(cx);
if (!this->isMoribund()) {
cx->wasm().suspenders_.remove(this);
}
this->releaseStackMemory(); // ← Frees stack, sets state=Moribund
MOZ_ASSERT(this->isMoribund());
}
Exploit Chain
- Setup: Create a wasm module with an exported
externrefglobalgand a functionf(obj)that calls a suspending import, then storesobjintog. - Suspend: Call
WebAssembly.promising(f)(obj). The wasm function suspends at the import call withobjlive in a wasm local on the suspendable stack. TheSuspenderObjectis inSuspendedstate. - Drop JS refs: Remove all JS-side references to
obj. The only remaining reference is the wasm local on the suspended stack. - Heap grooming: Build a deep linked list ending in the Promise resolver, so that marking it takes many incremental slices.
- Start incremental major GC:
startgc(1)+gcslicethroughPrepareintoMark. During root marking,Context::traceRootsis called fromTraceJitActivations(/firefox/js/src/jit/JitFrames.cpp:1491) but returns immediately because!trc->isTenuringTracer().objis not marked as a root. - Slice marking: Run
gcsliceuntilgetMarks()shows theWasmGlobalObjectwrapper isblack(already fully traced — its trace hook won't run again) andobjis stillunmarked. - Resume: Resolve the Promise →
SuspenderObject::resume()→gc::PreWriteBarrier(this)→GCMarker::traverse(JSObject*)→pushThing→ suspender is marked black and pushed onto the mark stack. Children are not scanned. - Wasm executes:
local.get $objreads the pointer from the suspendable stack (still valid),global.set $gstores it in the wasm global. Theglobal.setpre-write barrier fires on the old value (null), not onobj. No post-barrier marksobj— the snapshot-at-the-beginning invariant relies onobjalready being reachable from a traced root, which it isn't. - Wasm returns:
SuspenderObject::leave()→setMoribund()→releaseStackMemory()frees the stack, sets state toMoribund, and removes the suspender fromcx->wasm().suspenders_. - Finish marking:
finishgc()drains the mark stack. The marker pops theSuspenderObject, callsSuspenderObject::trace(),isTraceable()returnsfalse(Moribund), trace returns immediately.objis never marked. - Sweep:
objis swept. Its cell is poisoned withJS_SWEPT_TENURED_PATTERN(0x4B). - Use:
globalWrapper.value→WasmGlobalObject::valueGetter→AnyRef::toJSValue()→JSObject::is<WasmValueBox>()→getClass()→shape()->base()→ dereferences0x4b4b4b4b00000000→ SEGV.
Security Impact
Severity: High / sec-high
Attacker capability: Obtain a dangling JSObject* reachable from JS as a first-class value (globalWrapper.value). The freed slot can be reclaimed by a fresh JSObject of any class under attacker control, yielding a full type confusion (fake shape → fake getClass() → arbitrary read/write → code execution). The crash registers already demonstrate a controlled pointer load — rdi = 0x4b4b4b4b00000000 is the poisoned shape; in a release build without poisoning, this would be reclaimed memory.
Preconditions:
wasm_js_promise_integrationenabled (JSPI — a Stage-3 proposal being shipped in browsers)- Incremental GC (default in Firefox)
- The
addMarkObservers/getMarkstesting functions are used here only for deterministic timing; the underlying race exists in production and can be hit with heap grooming
Reliability: 7/10 runs with the provided PoC; the timing window is wide enough to be hit probabilistically without testing functions.
ASAN Report
rdi = 0x4b4b4b4b00000000: the shape pointer read from the swept JSObject is JS_SWEPT_TENURED_PATTERN (0x4B) poison — proof that the wasm global holds a pointer into a swept tenured arena.
Suggested Fix
Remove the early return in Context::traceRoots so suspended stacks are traced as roots in every GC root-marking phase:
// /firefox/js/src/wasm/WasmContext.cpp
void Context::traceRoots(JSTracer* trc) {
// Suspended stack frames must be traced as roots for both minor and major GC.
// During incremental major GC, SuspenderObject::trace cannot be relied upon:
// the PreWriteBarrier in resume() only pushes the suspender onto the mark
// stack (JSObject traversal is lazy), and if the suspender transitions to
// Moribund before being popped, its trace hook bails out.
gc::AssertRootMarkingPhase(trc);
for (auto iter = suspenders_.iter(); !iter.done(); iter.next()) {
SuspenderObject* object = iter.get();
if (object->state() == SuspenderState::Suspended) {
TraceSuspendableStack(trc, object);
}
}
}
An alternative (or additional defense-in-depth) is to eagerly trace the suspendable stack in resume() before transitioning out of Suspended, rather than relying on the lazy barrier.
Note: PoC requires --setpref=wasm_js_promise_integration=true flag.
| Reporter | ||
Comment 1•8 months ago
|
||
Updated•8 months ago
|
Comment 2•8 months ago
|
||
We're rewriting the JS-PI implementation in bug 2023217. That includes a fix to this by adding a 'resume barrier' which eagerly traces the stack and doesn't just push it on the mark stack. Exactly as recommended here.
Comment 3•8 months ago
|
||
The severity field for this bug is set to S3. However, the bug is flagged with the sec-high keyword.
:rhunt, could you consider increasing the severity of this security bug?
For more information, please visit BugBot documentation.
Comment 5•8 months ago
|
||
This is a duplicate of the same core issue as bug 2020895. I can confirm that this is fixed by the patches that fix that bug.
Updated•7 months ago
|
Updated•2 months ago
|
Description
•