Closed Bug 2046917 Opened 5 months ago Closed 4 months ago

Lost abortLinearWeakMarking() on parallel GC helper markers -> use-after-free of swept WeakMap values (Assertion failure: WeakMapBase::checkMarkingForZone(zone), at gc/Sweeping.cpp:1263)

Categories

(Core :: JavaScript: GC, defect, P1)

x86_64
Linux
defect

Tracking

()

RESOLVED FIXED
154 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 153+ fixed
firefox152 --- wontfix
firefox153 + fixed
firefox154 + fixed

People

(Reporter: decoder, Assigned: jonco)

References

(Blocks 1 open bug, Regression)

Details

(4 keywords, Whiteboard: [adv-main153+r][adv-esr140.13+r])

Attachments

(1 file, 4 external files)

Summary

GCMarker::haveAllImplicitEdges (js/src/gc/GCMarker.h:689) is a per-marker flag, but it guards a runtime-global invariant: that the per-zone gcEphemeronEdges tables contain every implicit (weakmap) edge. During parallel marking (enabled by default), a helper-thread marker (GCRuntime::markers[1..]) can hit an allocation failure while populating the ephemeron edge tables and record the failure via GCMarker::abortLinearWeakMarking() on the helper marker. The only reader of the flag, GCMarker::enterWeakMarkingMode(), consults markers[0] exclusively, so the helper-side abort is silently lost. Weak marking then proceeds in linear mode trusting an incomplete ephemeron table; a WeakMap value whose key and map are both live is never marked and gets swept while still reachable. A later WeakMap.get(key) returns a pointer to a freed GC cell — a use-after-free.

SpiderMonkey JS shell version: 20260603-5cffec5992d3 (m-c-asan-debug / m-c-fuzzing-asan-opt).

Root cause

  1. Per-marker flag: js/src/gc/GCMarker.h:688-689 declares MainThreadOrGCTaskData<bool> haveAllImplicitEdges;, initialized true for every marker in GCMarker::start() (js/src/gc/Marking.cpp:2484, called for all markers at js/src/gc/GC.cpp:3395-3397).
  2. Helper markers populate the ephemeron tables: with parallelMarkingEnabled=true (default) and markers.length() >= 2, helper threads run WeakMap::trace -> markEntries -> markEntry -> addEphemeronEdgesForEntry under AutoLockGC (js/src/gc/WeakMap-inl.h:264-327), doing fallible HashMap::add / Vector::emplaceBack allocations (js/src/gc/WeakMap.cpp:99-140).
  3. Abort path: on allocation failure (js/src/gc/WeakMap-inl.h:287-291) marker->abortLinearWeakMarking() sets haveAllImplicitEdges = false on the helper marker (js/src/gc/Marking.cpp:2714-2719); the failed key->value edge is dropped.
  4. Lost read: the only reader is GCMarker::enterWeakMarkingMode() (js/src/gc/Marking.cpp:2636-2640), invoked only as marker().enterWeakMarkingMode() (i.e. markers[0]) from GCRuntime::markWeakReferences (js/src/gc/Sweeping.cpp:645). The helper markers' flags are never consulted.
  5. Consequence: weak marking enters linear mode and marks weakmap values purely from gcEphemeronEdges; the iterative rescue pass WeakMapBase::markZoneIteratively is skipped while weak-marking mode is active (js/src/gc/Sweeping.cpp:687-691). The dropped edge means the value is never marked. At sweep time traceWeakEdgesDuringSweeping keeps the entry (key is marked) while the value cell is finalized, leaving a dangling HeapPtr that map.get(key) returns to the mutator.

When the same OOM occurs on the main-thread marker, enterWeakMarkingMode() returns false and the engine falls back to the iterative fixpoint, which re-marks values directly from the entries — fully correct. Only the helper-side abort is lost. (Note: OOM-simulation fuzzing cannot reach this path — targeting THREAD_TYPE_GCPARALLEL disables parallel marking entirely, js/src/gc/GC.cpp:3505-3518.)

Reproduction

Pure-JS testcase, no prefs, no debug-only functions, no env vars:

m-c-asan-debug/dist/bin/js --fuzzing-safe poc.js        # ~2-5 min

poc.js builds 40 WeakMaps x 3600 entries whose keys are reachable only through a long linked chain (so they are unmarked when the maps are traced, forcing ~144k ephemeron edges and >8 MB edge-table growth), then exhausts the process virtual address space with untouched ArrayBuffers. The provided shells bake in allocator_may_return_null=1, so the multi-megabyte edge-table doubling fails by returning null while 1 MB GC chunks keep working. The failure lands on whichever marker thread performs the doubling; when that is a helper marker, the abort is lost. Reproduces reliably (3/3 runs).

Debug build (debug-only MaybeCheckWeakMapMarking validator catches the corruption at end of marking):

WeakMap value is less marked than map and key
(map ... is black, key ... is black, value ... is white)
Assertion failure: WeakMapBase::checkMarkingForZone(zone), at js/src/gc/Sweeping.cpp:1263

Opt ASan build (validator is debug/zeal-only, so the corruption proceeds to the real UAF — WeakMap.get() returns a swept, poisoned cell):

==ERROR: AddressSanitizer: SEGV on unknown address (READ)
rdi = 0x4b4b4b4b02680268   (swept-cell poison)
    #0 js::gc::Cell read in GetPropIRGenerator::tryAttachStub (gc/Cell.h:113)
    #1 js::jit::TryAttachStub<GetPropIRGenerator,...>
    #2 js::jit::DoGetPropFallback (jit/BaselineIC.cpp:1323)

The mechanism was independently confirmed by single-call fault injection with gdb on the stock debug binary: forcing one addEphemeronEdge to return false on a helper thread ($_thread != 1) trips the Sweeping.cpp:1263 assertion, while forcing it on the main thread ($_thread == 1) completes cleanly — isolating the defect to the helper-marker abort.

Crash signature

  • Debug: Assertion failure: WeakMapBase::checkMarkingForZone(zone), at js/src/gc/Sweeping.cpp:1263
  • Opt ASan: SEGV (READ) on swept-cell poison under js::jit::DoGetPropFallback (UAF through WeakMap.get).

Security impact

Release builds have no checkMarkingForZone validation; the corruption is silent until the dangling value is used. Trigger conditions are reachable from content JS: a small malloc failure on a GC helper thread while parallel marking traces a weakmap-heavy heap. Both parallel marking and incremental weakmap marking are default-on; no special prefs. Outcome is get()-able freed cells inside live WeakMaps and GC tracing of freed memory in later cycles — an attacker-groomable use-after-free.

Suggested fix

Make the abort signal runtime-global. Options: move haveAllImplicitEdges to GCRuntime; have abortLinearWeakMarking() set the flag on runtime->gc.marker() (helper-side weakmap marking already holds the GC lock, so this is race-free); or have enterWeakMarkingMode() consult every marker:

bool GCMarker::enterWeakMarkingMode() {
  ...
  for (auto& marker : runtime()->gc.markers) {
    if (!marker->haveAllImplicitEdges) {
      return false;
    }
  }
  ...
}

Additionally, consider removing the OOM-simulation blind spot for THREAD_TYPE_GCPARALLEL (js/src/gc/GC.cpp:3505-3518) so this path becomes fuzzable.

Files

  • poc.js — self-contained shell testcase (run with --fuzzing-safe, ~2-5 min timeout).

Jon, could you help look into this newer sec-high bug?

Blocks: sm-security
Severity: -- → S2
Flags: needinfo?(jcoppeard)
Priority: -- → P1
Assignee: nobody → jcoppeard
Flags: needinfo?(jcoppeard)
Attached file (secure) —

Currently this is tracked per-marker but the effect should be per-runtime.
There may be multiple markers due to parallel marking.

I couldn't get the tescase to reproduce here either.

Should we also follow up on the "Additionally, consider removing the OOM-simulation blind spot for THREAD_TYPE_GCPARALLEL (js/src/gc/GC.cpp:3505-3518) so this path becomes fuzzable." part?

Flags: needinfo?(jcoppeard)

Good catch. I'll file a bug.

Flags: needinfo?(jcoppeard)

(In reply to Jon Coppeard (:jonco) from comment #6)
Thinking more about this, I'm not sure that works. OOM simulation requires predictable behaviour and I don't think we can get that with parallel marking as you never know which code is going to execute on each thread. That's why the restriction was there in the first place. It might be possible to arrange parallel marking on a single thread though, so we can test this path.

Duplicate of this bug: 2052059
Group: javascript-core-security → core-security-release
Status: NEW → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 154 Branch

Please add uplift requests for beta, esr115, and esr140

Keywords: regression
Regressed by: 1802478

This bug only happens if parallel marking is in use, and that landed in version 124.

Attached file (secure) —

Currently this is tracked per-marker but the effect should be per-runtime.
There may be multiple markers due to parallel marking.

I couldn't get the tescase to reproduce here either.

Original Revision: https://phabricator.services.mozilla.com/D307221

Attachment #9604636 - Flags: approval-mozilla-beta?
Flags: needinfo?(jcoppeard)
Regressed by: 1875117

firefox-beta Uplift Approval Request

  • User impact if declined/Reason for urgency: Possible crash / security vulnerability.
  • Code covered by automated testing?: yes
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: This is a simple change to move a state flag from being per-marker to runtime wide. It's been on central for 4 days with no problems found.
  • String changes made/needed?: None.
  • Is Android affected?: yes
Attachment #9604636 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
QA Whiteboard: [sec] [qa-triage-done-c154/b153]
Attached file (secure) —
Attached file (secure) —
Attachment #9607158 - Flags: approval-mozilla-esr140?

firefox-esr140 Uplift Approval Request

  • User impact if declined/Reason for urgency: Possible crash / security vulnerability.
  • Code covered by automated testing?: yes
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: This is a simple fix that has been on central for 10 days without any issues found.
  • String changes made/needed?: None
  • Is Android affected?: yes
Attachment #9607158 - Flags: approval-mozilla-esr140? → approval-mozilla-esr140+

A patch has been attached on this bug, which was already closed. Filing a separate bug will ensure better tracking. If this was not by mistake and further action is needed, please alert the appropriate party. (Or: if the patch doesn't change behavior -- e.g. landing a test case, or fixing a typo -- then feel free to disregard this message)

Whiteboard: [adv-main153+r][adv-esr140.13+r]
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: