Closed Bug 2035211 Opened 6 months ago Closed 6 months ago

Assertion failure in [@ CheckTargetValue] - FinalizationRegistry targets not relocated during JS_TransplantObject

Categories

(Core :: JavaScript Engine, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2032328

People

(Reporter: bugmon, Unassigned)

Details

(5 keywords)

Attachments

(3 files)

JS_TransplantObject() in js/src/jsapi.cpp fails to relocate FinalizationRegistry target entries when transplanting an object to a different compartment. This is an asymmetric bug: the structurally identical WeakRef target case is correctly handled via gc::GCRuntime::relocateWeakRefTarget() (lines 718-720), but no equivalent relocation exists for the recordMap in FinalizationObservers.

When an object registered as a FinalizationRegistry target is transplanted cross-compartment, the original object becomes a cross-compartment wrapper (CCW) to the new identity. However, the old zone's FinalizationObservers::recordMap still holds the original object (now a CCW) as a key, violating the invariant that FinalizationRegistry targets must never be CCWs.

In debug builds, the assertion at FinalizationObservers.cpp:32 fires during GC sweep. In release builds, the assertion is compiled out and the violation proceeds silently, leading to incorrect zone-based GC bookkeeping, potential use-after-free if zones are collected in the wrong order, and missed finalization callbacks.

Build Info

Affected Code

File: js/src/jsapi.cpp, line 714-721

  // Lastly, update the original object to point to the new one.
  if (origobj->compartment() != destination) {
    // If origobj is a weak ref target, relocate the weak ref map entry to
    // newIdentity before the swap turns origobj into a CCW.
    if (!gc::GCRuntime::relocateWeakRefTarget(ObjectValue(*origobj),
                                              ObjectValue(*newIdentity))) {
      oomUnsafe.crash("JS_TransplantObject weak ref relocation");
    }
    // NOTE: No equivalent relocation for FinalizationRegistry targets!

File: js/src/gc/FinalizationObservers.cpp, line 29-33

static inline void CheckTargetValue(const Value& target) {
  MOZ_ASSERT(CanBeHeldWeakly(target));
  MOZ_ASSERT_IF(target.isObject(),
                !IsCrossCompartmentWrapper(&target.toObject()));
}

File: js/src/gc/FinalizationObservers.cpp, line 429-453

    // Queue remaining finalization records if the target is dying.
    if (!TraceWeakEdge(trc, &iter.get().mutableKey(),
                       "FinalizationRecord target")) {
      for (auto iter = records.iter(); !iter.done(); iter.next()) {
        auto* record = &iter->as<FinalizationRecordObject>();
        record->setInRecordMap(false);
        record->unlink();
        FinalizationQueueObject* queue = record->queue();
        queue->queueRecordToBeCleanedUp(record);
        if (shouldQueueFinalizationRegistryForCleanup(queue)) {
          gc->queueFinalizationRegistryForCleanup(queue);
        }
      }
      iter.remove();
    } else {
      CheckTargetValue(iter.get().key());  // CRASH: target is now a CCW
    }

JS_TransplantObject relocates WeakRef targets but omits the equivalent relocation for FinalizationRegistry targets. The CheckTargetValue assertion explicitly requires that FinalizationRegistry targets never be cross-compartment wrappers.

Exploit Chain

  1. Create a transplantable DOM object using transplantableObject()
  2. Create a FinalizationRegistry and register the transplantable object as a target
  3. Create a new global in a different compartment using newGlobal({newCompartment: true})
  4. Transplant the object to the new compartment - JS_TransplantObject relocates WeakRef targets but NOT FinalizationRegistry targets
  5. The original object becomes a cross-compartment wrapper (CCW) but the old zone's recordMap still holds it as a key
  6. Trigger GC - during sweep, traceWeakFinalizationRegistryEdges encounters the CCW target, violating the no-CCW invariant
  7. In debug builds: assertion failure crash. In release builds: incorrect zone-based GC bookkeeping leading to potential use-after-free

Steps to Reproduce

  1. Build SpiderMonkey with --enable-debug (for debug assertions)
  2. Run the JS shell with --fuzzing-safe
  3. Execute: var {object, transplant} = transplantableObject();
  4. Execute: var g = newGlobal({newCompartment: true});
  5. Execute: var fr = new FinalizationRegistry(function(val) {});
  6. Execute: fr.register(object, 'test');
  7. Execute: transplant(g);
  8. Execute: gc();
  9. Observe assertion failure at FinalizationObservers.cpp:32

Security Impact

  • Severity: Moderate
  • Attacker capability: In release builds where the assertion is compiled out, the zone mismatch causes incorrect GC behavior. The GC may trace a CCW instead of the actual object, potentially missing the target's death. If the new zone (containing the transplanted object) is collected before the old zone processes its recordMap, the CCW key may reference freed memory via TraceWeakEdge, leading to potential use-after-free. Additionally, FinalizationRegistry cleanup callbacks may never fire, causing resource leaks.
  • Preconditions: Attacker must be able to trigger object transplantation (e.g., through DOM node adoption across documents) on an object that has been registered as a FinalizationRegistry target. In the browser, this corresponds to scenarios where DOM elements registered with FinalizationRegistry are adopted into documents in different compartments.

ASAN Report

[907] Assertion failure: !IsCrossCompartmentWrapper(&target.toObject()), at js/src/gc/FinalizationObservers.cpp:32
    #0 0x59b9197d02ce in MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3
    #1 0x59b9197d02ce in CheckTargetValue(JS::Value const&) /firefox/obj-js-debug/js/src/gc/FinalizationObservers.cpp:31:3
    #2 0x59b9197d13f3 in js::gc::FinalizationObservers::traceWeakFinalizationRegistryEdges(JSTracer*, JS::Zone*) /firefox/obj-js-debug/js/src/gc/FinalizationObservers.cpp:452:7
    #3 0x59b9197cca66 in js::gc::FinalizationObservers::traceWeakEdges(JSTracer*, JS::Zone*) /firefox/obj-js-debug/js/src/gc/FinalizationObservers.cpp:367:3
    #4 0x59b9197cca66 in js::gc::GCRuntime::traceWeakFinalizationObserverEdges(JSTracer*, JS::Zone*) /firefox/obj-js-debug/js/src/gc/FinalizationObservers.cpp:357:16
    #5 0x59b9198ce89d in js::gc::GCRuntime::sweepFinalizationObserversOnMainThread() /firefox/obj-js-debug/js/src/gc/Sweeping.cpp:1483:5
    #6 0x59b9198d01ed in js::gc::GCRuntime::beginSweepingSweepGroup(JS::GCContext*, JS::SliceBudget&) /firefox/obj-js-debug/js/src/gc/Sweeping.cpp:1748:3
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
Attached file testcase.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security

I just found the same bug yesterday :)

Flags: needinfo?(jcoppeard)
Status: UNCONFIRMED → RESOLVED
Closed: 6 months ago
Duplicate of bug: 2032328
Flags: needinfo?(jcoppeard)
Resolution: --- → DUPLICATE

I posted a patch for this yesterday. Maybe clauditor is reading bug mail?

Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: