Closed Bug 2042928 Opened 6 months ago Closed 6 months ago

Gray/black mark-color invariant violation in WeakMap delegate ephemeron-edge marking [@ js::gc::MarkingTracerT::markImplicitEdges]

Categories

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

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 --- wontfix
firefox151 --- wontfix
firefox152 --- wontfix
firefox153 --- fixed

People

(Reporter: bugmon, Assigned: sfink)

References

(Blocks 1 open bug)

Details

(Keywords: ai-involved, assertion, testcase)

Attachments

(3 files, 1 external file)

The WeakMap garbage-collection marking subsystem (js/src/gc/WeakMap.cpp and WeakMap-inl.h) maintains implicit "ephemeron" edges so that a WeakMap entry whose key is a cross-compartment wrapper (CCW) is kept alive while the wrapper's delegate (the wrapped object, living in a different zone) is alive. When a WeakMap is marked, WeakMap::markEntry calls WeakMapBase::addEphemeronEdgesForEntry/addEphemeronEdge (WeakMap.cpp:99-140) to install a 'delegate -> key' edge, colored with the map's color, into the delegate zone's gcEphemeronEdges table.

If the WeakMap and its CCW key are marked GRAY, tracing the gray key (markEntry -> traceKey) traces and pushes the key's delegate onto the GRAY mark stack. If that delegate is subsequently marked BLACK, the later gray weak-marking pass (GCRuntime::markWeakReferences -> processMarkStackTop -> maybeMarkImplicitEdges) pops the now-BLACK delegate while the marker color is GRAY and calls markImplicitEdges on it to process the WeakMap-installed ephemeron edge. There thingColor = markColor() = Gray but GetEffectiveColor(delegate) = Black, violating the invariant asserted at js/src/gc/Marking.cpp:820-821. The MOZ_ASSERT fails and MOZ_CrashSequence performs a null write (SEGV).

The assert encodes a real GC invariant. In a release/opt build (no assert) the same state causes markEphemeronEdges to propagate the delegate's edge at GRAY rather than at the delegate's true BLACK reachability, under-marking the WeakMap key and value gray even though they are reachable from a black (strongly reachable) object. Such gray/black mismarking can cause the cycle collector to reclaim an object that is still strongly reachable, a use-after-free.

Build Info

Affected Code

File: js/src/gc/WeakMap-inl.h, line 287-292

      if (!this->addEphemeronEdgesForEntry(AsMarkColor(mapColor),
                                           &keyCell->asTenured(), delegate,
                                           tenuredValue)) {
        marker->abortLinearWeakMarking();
      }

File: js/src/gc/WeakMap.cpp, line 128-140

bool WeakMapBase::addEphemeronEdge(MarkColor color, gc::TenuredCell* src,
                                   gc::TenuredCell* dst) {
  // Add an implicit edge from |src| to |dst|.
  auto& edgeTable = src->zone()->gcEphemeronEdges();
  auto p = edgeTable.lookupForAdd(src);
  if (!p) {
    if (!edgeTable.add(p, src, EphemeronEdgeVector())) {
      return false;
    }
  }
  return p->value().emplaceBack(color, dst);
}

File: js/src/gc/Marking.cpp, line 819-823

  MarkColor thingColor = markColor();
  MOZ_ASSERT(CellColor(thingColor) ==
             gc::detail::GetEffectiveColor(gcMarker(), markedThing));

  markEphemeronEdges(edges, thingColor);

File: js/src/gc/WeakMap.cpp, line 78-82

  // We can attempt to mark gray after marking black when a barrier pushes the
  // map object onto the black mark stack when it's already present on the
  // gray mark stack, since this is marked later.

The WeakMap code installs a map-colored 'delegate -> key' ephemeron edge into the delegate's zone; the gray weak-marking pass later processes it for a delegate whose color no longer matches the marker color, tripping the invariant assert.

Exploit Chain

  1. Create a WeakMap m in the main compartment.
  2. Use a cross-compartment-wrapper as the key: key = g.eval('Object.create(null)') where g is a new-compartment global; the wrapper's delegate is the wrapped object in g's zone.
  3. m.set(key, val) installs the entry; on marking, WeakMap::markEntry will install a 'delegate -> key' ephemeron edge into g's zone's gcEphemeronEdges table colored with the map's mark color.
  4. Drive marking so the WeakMap and the CCW key are marked GRAY (this traces the key and pushes its delegate onto the gray mark stack and installs the gray-colored ephemeron edge).
  5. Then mark the delegate BLACK (in incremental GC this corresponds to a black write-barrier promoting an object already queued on the gray stack; in the PoC it is forced deterministically with enqueueMark('set-color-black')).
  6. Finish the incremental GC; the gray weak-marking pass pops the now-black delegate and calls markImplicitEdges on it while markColor()==Gray.
  7. thingColor (Gray) != GetEffectiveColor(delegate) (Black) -> MOZ_ASSERT fails at Marking.cpp:821 -> MOZ_CrashSequence null write (SEGV). In a release build, the value is under-marked gray instead of black, enabling premature reclamation (use-after-free) of an object still reachable from a black object.

Steps to Reproduce

  1. Build/obtain the SpiderMonkey debug shell at /firefox/obj-js-debug/dist/bin/js (assertions enabled).
  2. Run weakmap_poc.js with the --fuzzing-safe js shell evaluator (all hooks used -- gczeal, newGlobal, enqueueMark, startgc, gcslice, gcstate -- are available under --fuzzing-safe).
  3. Observe the deterministic assertion failure / SEGV at js/src/gc/Marking.cpp:821 in js::gc::MarkingTracerT<8u>::markImplicitEdges<JSObject> during gray weak-marking.

Security Impact

  • Severity: High
  • Attacker capability: Script can drive the WeakMap delegate ephemeron-edge marking into a state where a black object's implicit edges are processed during gray weak-marking. In debug builds this is an immediate assertion-guarded crash; in release builds the missing color check causes gray/black under-marking of a WeakMap key and value that are reachable from a black (strongly reachable) object, which can cause the cycle collector to free a still-live object -- a use-after-free / memory-safety violation in the GC.
  • Preconditions: Reachable from ordinary JavaScript in a renderer/content process with no special privileges: a WeakMap with a cross-compartment-wrapper key (newGlobal) plus incremental GC with cycle-collector-style gray marking. The standard, committed GC testing hooks (gczeal/enqueueMark) make the gray-then-black mark ordering deterministic; the same ordering can occur naturally when an incremental black write-barrier promotes a delegate already queued on the gray mark stack (documented in WeakMapBase::markMap).

ASAN Report

Assertion failure: CellColor(thingColor) == gc::detail::GetEffectiveColor(gcMarker(), markedThing), at js/src/gc/Marking.cpp:821
#01: MOZ_CrashSequence  mozilla/Assertions.h:261
#02: js::gc::MarkingTracerT<8u>::markImplicitEdges<JSObject>(JSObject*)  js/src/gc/Marking.cpp:820
#03: js::gc::MarkingTracerT<8u>::maybeMarkImplicitEdges<JSObject>(JSObject*)  js/src/gc/Marking.cpp:793
#04: js::gc::MarkingTracerT<8u>::processMarkStackTop(JS::SliceBudget&)  js/src/gc/Marking.cpp:1812
#05: js::gc::MarkingTracerT<8u>::markCurrentColor(JS::SliceBudget&)  js/src/gc/Marking.cpp:1435
#06: js::gc::MarkingTracerT<8u>::markOneColor<(js::gc::MarkColor)1>(JS::SliceBudget&)  js/src/gc/Marking.cpp:1429
#07: js::gc::MarkingTracerT<8u>::doMarking(JS::SliceBudget&, js::gc::ShouldReportMarkTime)  js/src/gc/Marking.cpp:1406
#08: js::GCMarker::markUntilBudgetExhausted(JS::SliceBudget&, js::gc::ShouldReportMarkTime)  js/src/gc/Marking.cpp:1383
#09: js::gc::GCRuntime::markWeakReferences<js::gc::SweepGroupZonesIter>(JS::SliceBudget&)  js/src/gc/Sweeping.cpp:680
#10: js::gc::GCRuntime::markWeakReferencesInCurrentGroup / endMarkingSweepGroup  js/src/gc/Sweeping.cpp:734/1360
#11: js::gc::GCRuntime::sweepPhase / incrementalSlice / gcCycle / collect  js/src/gc/GC.cpp:4489
#12: GCSlice(JSContext*, unsigned int, JS::Value*)  js/src/builtin/TestingFunctions.cpp:3256

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (WRITE; zero page)
SUMMARY: UndefinedBehaviorSanitizer: SEGV in MOZ_CrashSequence
Attached file weakmap_poc.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security
Assignee: nobody → sphink

This turns out to be an overzealous assertion, though it does point out that the whole setup is more fragile than I realized.

The exploit chain in the description doesn't happen because in the case where you could have a Black delegate -> key ephemeron during Gray marking, that means you already visited it during Black marking, which would discard the edge. The assertion fires because this case is unexpected, but there won't be any problematic edges left in the table so it's harmless to scan through any remaining edges of the unexpectedly Black src.

Group: javascript-core-security
Status: UNCONFIRMED → NEW
Ever confirmed: true
Blocks: GC.stability
Severity: -- → S3
Priority: -- → P1
Pushed by sfink@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/cfb2211b13c4 https://hg.mozilla.org/integration/autoland/rev/d8f273f22cc6 Relax assertion that can fail if a barrier marks an object on the gray mark stack r=jonco
Status: NEW → RESOLVED
Closed: 6 months ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: