Data race leading to heap use-after-free in off-thread baseline batch compilation
Categories
(Core :: JavaScript: GC, defect)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox150 | --- | affected |
People
(Reporter: tsmith, Unassigned)
Details
(5 keywords)
Attachments
(2 files)
Summary
Data race leading to heap use-after-free in SpiderMonkey's off-thread baseline batch compilation. A SingleLinkedList<BumpChunk> embedded in GCRuntime::lifoBlocksToFreeAfterFullMinorGC (a MainThreadData<LifoAlloc>) is mutated lock-free by the main thread during GC sweep script finalization (/firefox/js/src/gc/GC.cpp:2392) while the GC background-free task simultaneously traverses and frees a list that was transferred from it but shares BumpChunk nodes due to a torn transfer. The race window opens only when off-thread baseline batch compilation is enabled, because the baseline compile-task lifecycle interleaves with GC's minor-collection-inside-major-GC timing (collectNurseryFromMajorGC at /firefox/js/src/gc/Sweeping.cpp:2631).
Affected Code
File: /firefox/js/src/gc/GC.cpp, lines 2391–2393
void GCRuntime::queueAllLifoBlocksForFreeAfterMinorGC(LifoAlloc* lifo) {
lifoBlocksToFreeAfterFullMinorGC.ref().transferFrom(lifo);
}
File: /firefox/js/src/gc/GCRuntime.h, line 1303
MainThreadData<LifoAlloc> lifoBlocksToFreeAfterFullMinorGC;
File: /firefox/js/src/ds/LifoAlloc.h, lines 260–261 (violated invariant)
void assertInvariants() {
MOZ_ASSERT(bool(head_) == bool(last_));
MOZ_ASSERT_IF(last_, !last_->next_);
}
MainThreadData<T> enforces thread-affinity only via MOZ_ASSERT in DEBUG builds (/firefox/js/src/threading/ProtectedData.h:283). In release builds there is no synchronization. transferFrom performs a multi-step non-atomic mutation of head_ / last_ fields. Under torn observation: T2 inherits head_=null, last_=non-null → popFirst null-derefs; T0 holds a stale last_ pointing to a BumpChunk T2 already freed → page reused for a JitScript → last_->next_ (offset 0) reads baselineScript_ = BaselineQueuedScriptPtr = 0x3.
Compare with the sibling function immediately above, which correctly takes the lock:
void GCRuntime::queueUnusedLifoBlocksForFree(LifoAlloc* lifo) {
MOZ_ASSERT(JS::RuntimeHeapIsBusy());
AutoLockHelperThreadState lock; // present
lifoBlocksToFree.ref().transferUnusedFrom(lifo);
}
void GCRuntime::queueAllLifoBlocksForFreeAfterMinorGC(LifoAlloc* lifo) {
lifoBlocksToFreeAfterFullMinorGC.ref().transferFrom(lifo); // NO lock
}
Correct Pattern
void GCRuntime::queueAllLifoBlocksForFreeAfterMinorGC(LifoAlloc* lifo) {
AutoLockHelperThreadState lock;
lifoBlocksToFreeAfterFullMinorGC.ref().transferFrom(lifo);
}
Exploit Chain
- JS creates 8 fresh
Functionscripts, warms them past--baseline-warmup-threshold=3under--baseline-batching=on. - Scripts enqueued in
Realm::baselineCompileQueue_withbaselineScript_ = 0x3. DispatchOffThreadBaselineBatchImpl(/firefox/js/src/jit/BaselineJIT.cpp:279) dispatches aBaselineCompileTaskto a helper thread.- JS drops references, calls
gc(). - GC prepare:
CancelOffThreadCompile(rt, Zone::Prepare)waits for and cancels tasks;FinishOffThreadTaskruns under helper-thread lock,js_delete(task->alloc_)frees ~64 KB chunk. forceDiscardJitCode→ICStubSpace::freeAllAfterMinorGC(BaselineJIT.cpp:53) →queueAllLifoBlocksForFreeAfterMinorGC— no lock.collectNurseryFromMajorGC(GC.cpp:3092) →startBackgroundFreeAfterMinorGCpromotes tolifoBlocksToFreeunder lock, dispatches T2.- T2 enters
freeFromBackgroundThread, transfers under lock, releases lock, entersfreeAll(). - T0 sweep begins.
prepareForSweepSlice(Sweeping.cpp:2631) may fire anothercollectNurseryFromMajorGC. Foreground finalization:BaseScript::finalize→ICScript::prepareForDestruction→queueAllLifoBlocksForFreeAfterMinorGC(&allocSitesSpace_)— no lock, T2 still live. - Torn list:
head_=null, last_=non-null.last_dangles to a freedBumpChunk. - Allocator recycles the page into a fresh
JitScript(next loop iteration) whosebaselineScript_ = 0x3lands atBumpChunk::next_'s offset. - T0
appendAll:last_->next_ = std::move(list.head_)→unique_ptr::resetreads0x3→ SEGV. T2popFirstnull-derefs simultaneously.
Security Impact
Severity: High. The crashing instruction (last_->next_ = std::move(list.head_) at LifoAlloc.h:364) first calls unique_ptr::reset, which deletes the stale pointer, then writes list.head_ into [last_+0]. With heap spraying so the reused slot holds a controlled pointer instead of 0x3, an attacker obtains a controlled js_free() on an arbitrary address followed by a controlled 8-byte store — write-what-where.
Preconditions: --baseline-batching=on + --baseline-offthread-compile=on (shipping defaults), multi-threaded runtime, attacker can spray short-lived Functions and induce GC (trivial from JS). ~80% hit rate per 100-iteration loop.
ASAN Report
==856497==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000003 (pc 0x59de31960365 bp 0x7ffcdce7cf50 sp 0x7ffcdce7cf20 T0)
==856497==The signal is caused by a READ memory access.
#0 0x59de31960365 in std::__uniq_ptr_impl<js::detail::BumpChunk, JS::DeletePolicy<js::detail::BumpChunk>>::reset(js::detail::BumpChunk*) unique_ptr.h:179:26
#4 0x59de31960365 in js::detail::SingleLinkedList<...>::appendAll(...) /firefox/js/src/ds/LifoAlloc.h:364:20
#5 0x59de3196077a in js::detail::SingleLinkedList<...>::prependAll(...) /firefox/js/src/ds/LifoAlloc.h:381:10
#6 0x59de3195ffe5 in js::LifoAlloc::transferFrom(js::LifoAlloc*) /firefox/js/src/ds/LifoAlloc.cpp:391:13
#7 0x59de32bafbc8 in js::jit::ICScript::prepareForDestruction(JS::Zone*) /firefox/js/src/jit/JitScript.cpp:407:10
#10 0x59de32bafbc8 in js::jit::JitScript::prepareForDestruction(JS::Zone*) /firefox/js/src/jit/JitScript.cpp:414:3
#11 0x59de32bac370 in js::jit::JitScript::Destroy(JS::Zone*, js::jit::JitScript*) /firefox/js/src/jit/JitScript.cpp:377:11
#12 0x59de32bac370 in JSScript::releaseJitScript(JS::GCContext*) /firefox/js/src/jit/JitScript.cpp:169:3
#13 0x59de332eaa68 in js::BaseScript::finalize(JS::GCContext*) /firefox/js/src/vm/JSScript.cpp:158:13
#30 0x59de3173c583 in GC(JSContext*, unsigned int, JS::Value*) /firefox/js/src/builtin/TestingFunctions.cpp:789:3
#31 0x150ba8d9f1ce ([anon:js-executable-memory]+0x21ce)
==856497==Register values:
rax = 0x0000000000000003 rdi = 0x00007bee09056300 r14 = 0x00007bee09056308
SUMMARY: AddressSanitizer: SEGV /firefox/js/src/ds/LifoAlloc.h:364:20 in js::detail::SingleLinkedList<...>::appendAll
rax = 0x3 = BaselineQueuedScriptPtr; rdi = dangling last_ into reused heap; r14 = rdi+8.
Secondary signature (T2, same root cause):
==855479==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5c1416946052 ... T2)
AddressSanitizer:DEADLYSIGNAL ← second thread crashing simultaneously
#4 in js::detail::SingleLinkedList<...>::popFirst() /firefox/js/src/ds/LifoAlloc.h:387:11
#5 in js::LifoAlloc::freeAll() /firefox/js/src/ds/LifoAlloc.cpp:125:36
#6 in js::gc::GCRuntime::freeFromBackgroundThread(...) /firefox/js/src/gc/Sweeping.cpp:585:16
Suggested Fix
// /firefox/js/src/gc/GC.cpp:2391
void GCRuntime::queueAllLifoBlocksForFreeAfterMinorGC(LifoAlloc* lifo) {
AutoLockHelperThreadState lock;
lifoBlocksToFreeAfterFullMinorGC.ref().transferFrom(lifo);
}
// /firefox/js/src/gc/GCRuntime.h:1303-1304 — make it structurally enforced
HelperThreadLockData<LifoAlloc> lifoBlocksToFreeAfterFullMinorGC;
HelperThreadLockData<LifoAlloc> lifoBlocksToFreeAfterNextMinorGC;
Defensively also harden /firefox/js/src/jit/JitScript.cpp:150:
void JSScript::maybeReleaseJitScript(JS::GCContext* gcx) {
if (zone()->jitZone()->keepJitScripts() ||
jitScript()->hasBaselineScript() ||
jitScript()->isBaselineQueued() ||
jitScript()->isBaselineCompiling() ||
jitScript()->icScript()->active()) {
return;
}
releaseJitScript(gcx);
}
| Reporter | ||
Comment 1•8 months ago
|
||
| Reporter | ||
Comment 2•8 months ago
|
||
Required flags:
--baseline-batching=on
--baseline-offthread-compile=on
--baseline-warmup-threshold=3
--no-ion --blinterp-warmup-threshold=1
Updated•8 months ago
|
Comment 3•8 months ago
|
||
This is confusing. The description is very different, but the actual crash here appears to be the same as the one in bug 2022627, which I duped to bug 2022337. I queued the fix for landing this morning, although it doesn't appear to have landed just yet. The attached testcase here fails without that patch, but passes with the patch.
The analysis in the other bug was much better, correctly identifying the actual problem (which is in the baseline interpreter, not GC). This one ... just seems like a big mess? I'm not sure I've ever seen Claude struggle so badly. I think perhaps it was confused because it can't directly see jitcode, so the store that we generate here (which is responsible for the 0x3 value showing up in the lifoAlloc) is invisible, and it has to thrash around to make up a semi-plausible alternative.
I can't make heads or tails of the proposed exploit chain or the proposed fixes. queueUnusedLifoBlocksForFree takes the lock because lifoBlocksToFree is shared with helper threads; queueAllLifoBlocksForFreeAfterMinorGC doesn't because lifoBlocksToFreeAfterFullMinorGC is main-thread-only. Nothing seems amiss there.
I'm going to open this up and dupe it to bug 2022337. The underlying bug is unexploitable and not accessible in the browser.
Updated•8 months ago
|
Description
•