Assertion failure in [@ CheckTargetValue] - FinalizationRegistry targets not relocated during JS_TransplantObject
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: bugmon, Unassigned)
Details
(5 keywords)
Attachments
(3 files)
|
197 bytes,
text/plain
|
Details | |
|
4.14 KB,
patch
|
Details | Diff | Splinter Review | |
|
2.01 KB,
text/plain
|
Details |
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
- Branch: main
- Revision: cdf7090f93822ada0f5e1b5124fddadbd82c06b2
- Timestamp: 2026-04-25T17:37:40+00:00
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
- Create a transplantable DOM object using transplantableObject()
- Create a FinalizationRegistry and register the transplantable object as a target
- Create a new global in a different compartment using newGlobal({newCompartment: true})
- Transplant the object to the new compartment - JS_TransplantObject relocates WeakRef targets but NOT FinalizationRegistry targets
- The original object becomes a cross-compartment wrapper (CCW) but the old zone's recordMap still holds it as a key
- Trigger GC - during sweep, traceWeakFinalizationRegistryEdges encounters the CCW target, violating the no-CCW invariant
- In debug builds: assertion failure crash. In release builds: incorrect zone-based GC bookkeeping leading to potential use-after-free
Steps to Reproduce
- Build SpiderMonkey with --enable-debug (for debug assertions)
- Run the JS shell with --fuzzing-safe
- Execute: var {object, transplant} = transplantableObject();
- Execute: var g = newGlobal({newCompartment: true});
- Execute: var fr = new FinalizationRegistry(function(val) {});
- Execute: fr.register(object, 'test');
- Execute: transplant(g);
- Execute: gc();
- 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)
| Reporter | ||
Comment 1•6 months ago
|
||
| Reporter | ||
Comment 2•6 months ago
|
||
| Reporter | ||
Comment 3•6 months ago
|
||
Updated•6 months ago
|
Updated•6 months ago
|
Comment 7•6 months ago
|
||
I posted a patch for this yesterday. Maybe clauditor is reading bug mail?
Updated•1 month ago
|
Description
•