Closed Bug 2023286 Opened 8 months ago Closed 8 months ago

Use-after-free via incremental GC snapshot-at-the-beginning violation in the Wasm JSPI implementation

Categories

(Core :: JavaScript: WebAssembly, defect, P3)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2020895
Tracking Status
firefox-esr115 --- disabled
firefox-esr140 --- disabled
firefox149 --- disabled
firefox150 --- disabled
firefox151 --- fixed

People

(Reporter: tsmith, Unassigned)

References

Details

(5 keywords)

Attachments

(2 files)

Attached file crash_stack.txt —

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

  1. Setup: Create a wasm module with an exported externref global g and a function f(obj) that calls a suspending import, then stores obj into g.
  2. Suspend: Call WebAssembly.promising(f)(obj). The wasm function suspends at the import call with obj live in a wasm local on the suspendable stack. The SuspenderObject is in Suspended state.
  3. Drop JS refs: Remove all JS-side references to obj. The only remaining reference is the wasm local on the suspended stack.
  4. Heap grooming: Build a deep linked list ending in the Promise resolver, so that marking it takes many incremental slices.
  5. Start incremental major GC: startgc(1) + gcslice through Prepare into Mark. During root marking, Context::traceRoots is called from TraceJitActivations (/firefox/js/src/jit/JitFrames.cpp:1491) but returns immediately because !trc->isTenuringTracer(). obj is not marked as a root.
  6. Slice marking: Run gcslice until getMarks() shows the WasmGlobalObject wrapper is black (already fully traced — its trace hook won't run again) and obj is still unmarked.
  7. 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.
  8. Wasm executes: local.get $obj reads the pointer from the suspendable stack (still valid), global.set $g stores it in the wasm global. The global.set pre-write barrier fires on the old value (null), not on obj. No post-barrier marks obj — the snapshot-at-the-beginning invariant relies on obj already being reachable from a traced root, which it isn't.
  9. Wasm returns: SuspenderObject::leave() → setMoribund() → releaseStackMemory() frees the stack, sets state to Moribund, and removes the suspender from cx->wasm().suspenders_.
  10. Finish marking: finishgc() drains the mark stack. The marker pops the SuspenderObject, calls SuspenderObject::trace(), isTraceable() returns false (Moribund), trace returns immediately. obj is never marked.
  11. Sweep: obj is swept. Its cell is poisoned with JS_SWEPT_TENURED_PATTERN (0x4B).
  12. Use: globalWrapper.value → WasmGlobalObject::valueGetter → AnyRef::toJSValue() → JSObject::is<WasmValueBox>() → getClass() → shape()->base() → dereferences 0x4b4b4b4b00000000 → 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_integration enabled (JSPI — a Stage-3 proposal being shipped in browsers)
  • Incremental GC (default in Firefox)
  • The addMarkObservers/getMarks testing 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.

Attached file testcase.js —

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.

Severity: -- → S3
Depends on: 2023217
Priority: -- → P3

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.

Flags: needinfo?(rhunt)

unsupported-config

Flags: needinfo?(rhunt)

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.

Status: NEW → RESOLVED
Closed: 8 months ago
Duplicate of bug: 2020895
Resolution: --- → DUPLICATE
Group: javascript-core-security → core-security-release
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: