Use-after-free in [@ TenuringTracer::traceObject] via relocateFinalizationObserverTarget when WeakRef zone is in a later sweep group
Categories
(Core :: JavaScript: GC, defect, P1)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox-esr115 | --- | unaffected |
| firefox-esr140 | --- | unaffected |
| firefox152 | --- | unaffected |
| firefox153 | + | fixed |
| firefox154 | + | fixed |
People
(Reporter: bugmon, Assigned: jonco)
References
(Blocks 1 open bug)
Details
(5 keywords)
Attachments
(3 files, 3 external files)
|
1.20 KB,
text/plain
|
Details | |
|
1014 bytes,
patch
|
Details | Diff | Splinter Review | |
|
4.10 KB,
text/plain
|
Details |
GCRuntime::relocateFinalizationObserverTarget (called from JS_TransplantObject) updates the TargetSlot of every WeakRefObject that observes the transplanted object. Since bug 2045416 it skips WeakRefs for which IsAboutToBeFinalizedUnbarriered() is true, but that predicate only fires when the WeakRef's zone is in the Sweep state. If the WeakRef lives in a zone that is being collected in a later sweep group (zone state MarkBlackOnly while the runtime is already in State::Sweep), the predicate returns false and setTarget() is called on a WeakRef that is unmarked and destined to be swept.
setTarget() stores a nursery-allocated newIdentity proxy via setReservedSlotGCThingAsPrivate, whose PostWriteBarrierCell records the WeakRef in the WholeCellBuffer. When the later sweep group is processed the WeakRef is swept and its memory poisoned with JS_SWEPT_TENURED_PATTERN (0x4B). The stale store-buffer entry is then traced by the next minor GC: TenuringTracer::traceObject reads the poisoned shape pointer and crashes (rdi = 0x4b4b4b4b4b4b4b4b). With heap grooming the freed slot can be reclaimed by attacker-controlled data, turning the trace into a type-confusion primitive.
The bug is in core GC code on a production path: JS_TransplantObject is invoked by Gecko for DOM brain-transplants (e.g. WindowProxy/Location on navigation, document.adoptNode across globals), WeakRef is web-exposed, and multi-sweep-group incremental GC is the default. The shell-only helpers in the testcase (transplantableObject, schedulezone, gcslice) merely make the race deterministic.
Build Info
- Branch: main
- Revision: 3b0ec2e6ef5bd362c37420922deaddf9d5d5e175
- Timestamp: 2026-06-20T22:36:09+00:00
Affected Code
File: js/src/gc/FinalizationObservers.cpp, line 678-684
// Update the target stored in each WeakRefObject, skipping dying objects.
for (auto iter = weakRefList.iter(); !iter.done(); iter.next()) {
auto* weakRef = &iter.get()->as<WeakRefObject>();
if (!IsAboutToBeFinalizedUnbarriered(weakRef)) { // <-- false when weakRef->zone() is MarkBlackOnly
MOZ_ASSERT(weakRef->target() == oldTarget);
weakRef->setTarget(newTarget); // <-- writes nursery ptr into doomed cell
}
}
File: js/src/gc/Marking.cpp, line 3093
return zone->isGCSweeping() && !TenuredThingIsMarkedAny(thing); // MarkBlackOnly => returns false
File: js/src/vm/NativeObject.h, line 1732-1746
void setReservedSlotGCThingAsPrivate(uint32_t slot, gc::Cell* cell) {
...
setReservedSlotGCThingAsPrivateUnbarriered(slot, cell);
gc::PostWriteBarrierCell(this, prev, cell); // <-- adds dying WeakRef to WholeCellBuffer
}
File: js/src/gc/Tenuring.cpp, line 879-886
void js::gc::TenuringTracer::traceObject(JSObject* obj) {
const JSClass* clasp = obj->getClass(); // <-- UAF: obj is swept, shape is 0x4b4b4b4b4b4b4b4b
MOZ_ASSERT(clasp);
if (clasp->hasTrace()) {
AutoSetSourceHeap setHeap(*this, obj);
clasp->doTrace(this, obj);
}
The IsAboutToBeFinalizedUnbarriered guard in relocateFinalizationObserverTarget cannot detect a dying WeakRef whose zone is in a later sweep group, because IsAboutToBeFinalizedInternal only consults isGCSweeping(). setTarget() then post-barriers the doomed cell into the WholeCellBuffer, which is later traced after the cell is swept.
Exploit Chain
- Create zones A, B, C, D in separate compartments. In C, create transplantable proxy targets. In B, create WeakRefs to those targets, then drop the only references to the WeakRefs so they become unreachable.
- Create a large number of WeakRefs in A so that A and B end up in different sweep groups and incremental sweeping yields between them.
- Start an incremental zone GC collecting A and B. Advance slices until the runtime is in State::Sweep with zone A in Sweep and zone B still in MarkBlackOnly (later sweep group).
- Call JS_TransplantObject on each target in C, moving it to D. JS_TransplantObject invokes GCRuntime::relocateFinalizationObserverTarget(oldTarget, newTarget).
- For each observing WeakRef in B, IsAboutToBeFinalizedUnbarriered returns false (B is MarkBlackOnly, not Sweep), so weakRef->setTarget(newTarget) runs and PostWriteBarrierCell records the WeakRef in the WholeCellBuffer because newTarget is nursery-allocated.
- The incremental GC advances to B's sweep group; the unmarked WeakRefs are finalized and their cells poisoned with JS_SWEPT_TENURED_PATTERN (0x4B). The WholeCellBuffer still references them.
- The next minor GC traces the WholeCellBuffer: TenuringTracer::traceObject is called on the freed cell, dereferencing the poisoned shape pointer. With the freed slot reclaimed by attacker data this yields a controlled JSClass*/trace-hook, enabling type confusion and arbitrary read/write.
Steps to Reproduce
- Build SpiderMonkey with AddressSanitizer (mozconfig.linux.asan.fuzzing).
- Run obj-firefox-asan/dist/bin/js --fuzzing-safe testcase.js
- Observe ASan SEGV in js::gc::TenuringTracer::traceObject with rdi = 0x4b4b4b4b4b4b4b4b.
Security Impact
- Severity: High
- Attacker capability: Use-after-free of a tenured JSObject that is subsequently traced by the nursery TenuringTracer. The tracer reads the object's Shape/JSClass and may invoke a trace hook and traverse slots; if the freed cell is reallocated with attacker-controlled data this gives a fake-object / type-confusion primitive leading to arbitrary read/write and likely code execution in the content process.
- Preconditions: Web content must create cross-zone WeakRefs to an object that later undergoes a brain-transplant (JS_TransplantObject, e.g. WindowProxy/Location on iframe navigation or document.adoptNode across globals) while an incremental GC has the WeakRef's zone scheduled in a later sweep group than another zone already in Sweep. No non-default preferences are required; the shell testcase uses testing functions only to make the incremental-GC timing deterministic.
ASAN Report
AddressSanitizer:DEADLYSIGNAL
=================================================================
==105561==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x58b437391343 bp 0x7ffd1191adc0 sp 0x7ffd1191ad90 T0)
==105561==The signal is caused by a READ memory access.
==105561==Hint: this fault was caused by a dereference of a high value address (see register values below). Disassemble the provided pc to learn which register was used.
#0 0x58b437391343 in js::gc::HeaderWord::get() const /firefox/js/src/gc/Cell.h:113:23
#1 0x58b437391343 in js::gc::CellWithTenuredGCPointer<js::gc::TenuredCell, js::BaseShape>::headerPtr() const /firefox/js/src/gc/Cell.h:846:60
#2 0x58b437391343 in js::Shape::base() const /firefox/js/src/vm/Shape.h:327:36
#3 0x58b437391343 in js::Shape::getObjectClass() const /firefox/js/src/vm/Shape.h:390:50
#4 0x58b437391343 in JSObject::getClass() const /firefox/js/src/vm/JSObject.h:113:53
#5 0x58b437391343 in js::gc::TenuringTracer::traceObject(JSObject*) /firefox/js/src/gc/Tenuring.cpp:880:31
#6 0x58b4373a98f2 in TraceWholeCell(js::gc::TenuringTracer&, JSObject*) /firefox/js/src/gc/Tenuring.cpp:446:9
#7 0x58b4373a98f2 in void js::gc::TenuringTracer::traceBufferedCells<JSObject>(js::gc::Arena*, js::gc::ArenaCellSet*) /firefox/js/src/gc/Tenuring.cpp:507:7
#8 0x58b437390fc0 in js::gc::ArenaCellSet::trace(js::gc::TenuringTracer&) /firefox/js/src/gc/Tenuring.cpp:524:13
#9 0x58b437391197 in js::gc::StoreBuffer::WholeCellBuffer::trace(js::gc::TenuringTracer&, js::gc::StoreBuffer*) /firefox/js/src/gc/Tenuring.cpp:543:14
#10 0x58b437339f56 in js::Nursery::traceRoots(js::gc::AutoGCSession&, js::gc::TenuringTracer&) /firefox/js/src/gc/Nursery.cpp:1670:8
#11 0x58b437337249 in js::Nursery::doCollection(js::gc::AutoGCSession&, JS::GCOptions, JS::GCReason) /firefox/js/src/gc/Nursery.cpp:1550:3
#12 0x58b437335dfa in js::Nursery::collect(JS::GCOptions, JS::GCReason) /firefox/js/src/gc/Nursery.cpp:1409:31
#13 0x58b4372ac60c in js::gc::GCRuntime::collectNursery(JS::GCOptions, JS::GCReason, js::gcstats::PhaseKind) /firefox/js/src/gc/GC.cpp:5505:13
#14 0x58b43728ed30 in js::gc::GCRuntime::minorGC(JS::GCReason, js::gcstats::PhaseKind) /firefox/js/src/gc/GC.cpp:5474:3
#15 0x58b436be4c8e in JSContext::minorGC(JS::GCReason) /firefox/js/src/vm/JSContext-inl.h:286:17
#16 0x58b436be4c8e in MinorGC(JSContext*, unsigned int, JS::Value*) /firefox/js/src/builtin/TestingFunctions.cpp:833:7
#17 0x58b43838d7ca in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:498:13
#18 0x58b43838d7ca in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:594:12
#19 0x58b4375fac6b in js::jit::DoCallFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, unsigned int, JS::Value*, JS::MutableHandle<JS::Value>) /firefox/js/src/jit/BaselineIC.cpp:1713:10
#20 0x241416c15803 (<unknown module>)
==105561==Register values:
rax = 0x0969696969696969 rbx = 0x000077002e40f078 rcx = 0x000077002e40f000 rdx = 0x0000795037708130
rdi = 0x4b4b4b4b4b4b4b4b rsi = 0x000077002e40f078 rbp = 0x00007ffd1191adc0 rsp = 0x00007ffd1191ad90
r8 = 0x0000000000000000 r9 = 0x000000000000000f r10 = 0xffffffffffffffff r11 = 0x0000000000000000
r12 = 0x0000000000000000 r13 = 0x00000ee005c81e0f r14 = 0x0000770036644c40 r15 = 0x0000000020408000
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /firefox/js/src/gc/Cell.h:113:23 in js::gc::HeaderWord::get() const
==105561==ABORTING
| Reporter | ||
Comment 1•5 months ago
|
||
| Reporter | ||
Comment 2•5 months ago
|
||
| Reporter | ||
Comment 3•5 months ago
|
||
| Reporter | ||
Comment 4•5 months ago
|
||
Updated•5 months ago
|
Updated•5 months ago
|
Updated•5 months ago
|
| Assignee | ||
Comment 6•5 months ago
|
||
The AI fix is exactly the same as the patch I was about to write.
| Assignee | ||
Comment 7•5 months ago
|
||
This is the same issue as bug 2045416 but the fix was incomplete.
Comment 8•5 months ago
|
||
The severity field for this bug is set to S3. However, the bug is flagged with the sec-high keyword.
:jonco, could you consider increasing the severity of this security bug?
For more information, please visit BugBot documentation.
| Assignee | ||
Updated•5 months ago
|
| Assignee | ||
Comment 9•5 months ago
|
||
The problem is that the weak ref we are updating is weakly held and may later
be found to be dead and swept. The post barrier triggered on the update can add
a pointer to the weak ref to the nursery store buffer, which can result in UAF
if we don't clear out the nursery before we sweep the object.
The fix is to make use of the existing flag that is used to handle this
situation.
I removed the previous incomplete fix for reasons of simplicitly.
| Assignee | ||
Comment 10•5 months ago
|
||
Comment 11•5 months ago
|
||
Comment 12•5 months ago
|
||
https://hg.mozilla.org/mozilla-central/rev/aa057662f0c1
Please request Beta uplift on this.
| Assignee | ||
Updated•5 months ago
|
Comment 13•5 months ago
|
||
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 small change that increases the scope of a previous fix to this area. This has been on central for three days with no problems foudn.
- String changes made/needed?: None.
- Is Android affected?: yes
| Assignee | ||
Comment 14•5 months ago
|
||
The problem is that the weak ref we are updating is weakly held and may later
be found to be dead and swept. The post barrier triggered on the update can add
a pointer to the weak ref to the nursery store buffer, which can result in UAF
if we don't clear out the nursery before we sweep the object.
The fix is to make use of the existing flag that is used to handle this
situation.
I removed the previous incomplete fix for reasons of simplicitly.
Original Revision: https://phabricator.services.mozilla.com/D308546
Updated•5 months ago
|
Updated•5 months ago
|
Comment 15•5 months ago
|
||
| uplift | ||
Updated•5 months ago
|
Updated•2 months ago
|
Updated•2 months ago
|
Comment 17•2 months ago
|
||
Comment 18•2 months ago
|
||
| bugherder | ||
Description
•