Closed Bug 2078742 Opened 1 month ago Closed 1 month ago

Read of unmapped ArenaChunk in [@ js::gc::BackgroundUnmarkTask::unmark] after onOutOfMallocMemory releases held relocated arenas (debug builds)

Categories

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

defect

Tracking

()

RESOLVED FIXED
160 Branch
Tracking Status
firefox-esr153 --- wontfix
firefox158 --- wontfix
firefox159 --- wontfix
firefox160 --- fixed

People

(Reporter: bugmon, Assigned: jonco)

References

(Blocks 1 open bug)

Details

(Keywords: crash, testcase)

Attachments

(4 files, 1 external file)

GCRuntime::onOutOfMallocMemory() does not wait for the background unmark task before, in DEBUG builds, calling releaseHeldRelocatedArenasWithoutUnlocking() and freeEmptyChunks(). Releasing the held arenas moves chunks out of a zone's fullChunks list (to availableChunks, or to emptyChunks where they are then unmapped) while BackgroundUnmarkTask::unmark() on a helper thread still holds a raw ArenaChunk* cursor into that list across its AutoUnlockGC window. When the helper thread re-takes the GC lock it reads chunk->info of a chunk that has been unmapped, and faults.

A script with access to the SpiderMonkey shell testing functions in a debug build can crash the process with a helper-thread read of an unmapped GC chunk by reporting a malloc failure while an incremental GC is in the Prepare state following a DEBUG_GC compacting collection. The demonstrated effect is a read SEGV in the debug shell only; the releasing code is compiled under #ifdef DEBUG, held relocated arenas are created only for GCReason::DEBUG_GC, and the same testcase exits normally in the non-debug ASAN shell. It is a main-thread/helper-thread race, which the testcase retries over 8 rounds; it crashed in both of two verifier runs.

Build Info

Affected Code

File: js/src/gc/GC.cpp, line 3208-3218

    chunk = zone->fullChunks(lock).maybeHead();
    while (chunk) {
      {
        AutoUnlockGC unlock(lock);
        chunk->markBits.clear();
      }
      // Chunk must still be full.
      MOZ_ASSERT(!chunk->info.isCurrentChunk);   // faulting read (chunk + 0x30)
      MOZ_ASSERT(!chunk->hasAvailableArenas());
      chunk = chunk->next();
    }

File: js/src/gc/GC.cpp, line 5502-5517

void GCRuntime::onOutOfMallocMemory() {
  waitForBackgroundTasksOnAllocFailure();

  AutoLockGC lock(this);
  onOutOfMallocMemory(lock);
}

void GCRuntime::onOutOfMallocMemory(const AutoLockGC& lock) {
#ifdef DEBUG
  // Release any relocated arenas we may be holding on to, without releasing
  // the GC lock.
  releaseHeldRelocatedArenasWithoutUnlocking(lock);
#endif

  // Throw away any excess chunks we have lying around.
  freeEmptyChunks(lock);

File: js/src/gc/GC.cpp, line 5671-5700

MOZ_COLD bool GCRuntime::waitForBackgroundTasksOnAllocFailure() {
  bool waited = false;

  // Stop allocating new chunks.
  if (allocTask.cancelAndWait()) {
    waited = true;
  }
  if (nursery().joinSweepTask()) {
    waited = true;
  }
  if (nursery().joinDecommitTask()) {
    waited = true;
  }
  if (sweepTask.join()) {
    waited = true;
  }
  if (freeTask.join()) {
    waited = true;
  }
  if (decommitTask.join()) {
    waited = true;
  }
  // unmarkTask is not joined
  return waited;
}

File: js/src/gc/Heap.cpp, line 501-519

  bool wasFull = isFull();

  info.numArenasFree += numArenasFreed;
  if (wasCommitted) {
    info.numArenasFreeCommitted += numArenasFreed;
  }

  if (MOZ_UNLIKELY(wasFull)) {
    info.zone->fullChunks(lock).remove(this);
    info.zone->availableChunks(lock).push(this);
    return;
  }

  if (MOZ_UNLIKELY(isEmpty())) {
    info.zone->availableChunks(lock).remove(this);
    info.zone = nullptr;
    gc->recycleChunk(this, lock);
    return;
  }

File: js/src/gc/GC.cpp, line 400-413

static void FreeChunkPool(ChunkPool& pool) {
  for (ChunkPool::Iter iter(pool); !iter.done();) {
    ArenaChunk* chunk = iter.get();
    iter.next();
    pool.remove(chunk);
    MOZ_ASSERT(chunk->isEmpty());
    UnmapPages(static_cast<void*>(chunk), ChunkSize);
  }
  MOZ_ASSERT(pool.count() == 0);
}

void GCRuntime::freeEmptyChunks(const AutoLockGC& lock) {
  FreeChunkPool(emptyChunks(lock));
}

File: js/src/gc/Compacting.cpp, line 915-920, 961-966

static inline bool ShouldProtectRelocatedArenas(JS::GCReason reason) {
  // For zeal mode collections we don't release the relocated arenas
  // immediately. Instead we protect them and keep them around until the next
  // collection so we can catch any stray accesses to them.
  return reason == JS::GCReason::DEBUG_GC && CanProtectArenas();
}
...
void GCRuntime::releaseHeldRelocatedArenasWithoutUnlocking(
    const AutoLockGC& lock) {
  unprotectHeldRelocatedArenas(lock);
  releaseRelocatedArenasWithoutUnlocking(relocatedArenasToRelease, lock);
  relocatedArenasToRelease = nullptr;
}

unmark() drops the GC lock around markBits.clear() for each chunk in fullChunks and assumes a full chunk stays full and mapped. waitForBackgroundTasksOnAllocFailure() joins allocTask, the nursery sweep/decommit tasks, sweepTask, freeTask and decommitTask, but not unmarkTask. onOutOfMallocMemory(lock) then, under #ifdef DEBUG, returns held relocated arenas to their chunks; updateFreeCountsAfterFree() moves a previously full chunk to availableChunks and recycles an emptied chunk into emptyChunks, which freeEmptyChunks() unmaps via FreeChunkPool(). Held relocated arenas exist only when a compacting GC runs with GCReason::DEBUG_GC (Compacting.cpp:915-920) and are otherwise released in the next sweep phase (Sweeping.cpp:1997-1998), so they are still held during the next GC's Prepare state. The shell function reportLargeAllocationFailure(bytes) calls JSRuntime::onOutOfMemoryCanGC -> onOutOfMemory -> gc.onOutOfMallocMemory() (TestingFunctions.cpp:7335, Runtime.cpp:695,726).

Exploit Chain

  1. Allocate 400000 tenured objects, then drop all but 1 in 14 so that a compacting GC relocates the survivors out of nearly every chunk.
  2. Run a shrinking incremental GC to completion with startgc(1000000, "shrinking") and gcslice(); the reason is DEBUG_GC, so in a debug build the relocated arenas are protected and kept on relocatedArenasToRelease instead of being returned to their chunks, leaving those chunks on the zone's fullChunks list.
  3. Call startgc(1) to begin the next incremental GC; it returns in the Prepare state with BackgroundUnmarkTask::unmark() running on a helper thread, walking fullChunks and dropping the GC lock around each markBits.clear().
  4. Call reportLargeAllocationFailure(1); this reaches GCRuntime::onOutOfMallocMemory(), which does not join unmarkTask, releases the held arenas (moving chunks out of fullChunks and into emptyChunks) and unmaps the empty chunks in freeEmptyChunks().
  5. The helper thread re-takes the GC lock and reads chunk->info.isCurrentChunk at GC.cpp:3215 from a chunk that has been unmapped, producing a READ SEGV at chunk base + 0x30.

Steps to Reproduce

  1. Start from an unmodified tree (git -C /firefox diff is empty); no patches, prefs or extra shell flags are needed.
  2. Build the SpiderMonkey debug shell with build_firefox using /app/mozconfigs/mozconfig.js.linux.debug (build_dir /firefox/obj-js-debug).
  3. Run testcase.js with js_shell_evaluator and js_binary=/firefox/obj-js-debug/dist/bin/js (the evaluator adds --fuzzing-safe).
  4. Observe the SEGV READ in js::gc::BackgroundUnmarkTask::unmark() at js/src/gc/GC.cpp:3215 on a helper thread. The crash is a race retried over 8 rounds; it occurred in 2 of 2 verifier runs. The same script without the reportLargeAllocationFailure(1) call exits 0, and the unmodified script exits 0 in the non-debug ASAN shell /firefox/obj-firefox-asan/dist/bin/js.

Security Impact

  • Severity: Low
  • Attacker capability: Crash a debug-build SpiderMonkey process through a GC helper-thread read of an unmapped GC chunk header. Only a read fault was demonstrated; no write to freed or unmapped memory and no release-build effect was shown.
  • Preconditions: DEBUG build (the arena release in onOutOfMallocMemory is under #ifdef DEBUG); a prior compacting GC with GCReason::DEBUG_GC so that relocated arenas are held (reached here via the shell testing functions startgc/gcslice); a malloc-failure report (here the reportLargeAllocationFailure testing function) arriving while the next incremental GC's background unmark task is still running; winning a main-thread/helper-thread race.

ASAN Report

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==16027==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x39bd76500030 (pc 0x561fa9b34e6b bp 0x7eb020ffdb30 sp 0x7eb020ffdaf0 T16029)
==16027==The signal is caused by a READ memory access.
    #0 0x561fa9b34e6b in js::gc::BackgroundUnmarkTask::unmark() /firefox/obj-js-debug/js/src/gc/./../../../../js/src/gc/GC.cpp:3215:7
    #1 0x561fa9b34a17 in js::gc::BackgroundUnmarkTask::run(js::AutoLockHelperThreadState&) /firefox/obj-js-debug/js/src/gc/./../../../../js/src/gc/GC.cpp:3147:5
    #2 0x561fa9b4f58c in js::GCParallelTask::runTask(JS::GCContext*, js::AutoLockHelperThreadState&) /firefox/obj-js-debug/js/src/gc/./../../../../js/src/gc/GCParallelTask.cpp:219:3
    #3 0x561fa9b4f9b3 in js::GCParallelTask::runHelperThreadTask(js::AutoLockHelperThreadState&) /firefox/obj-js-debug/js/src/gc/./../../../../js/src/gc/GCParallelTask.cpp:201:3
    #4 0x561faac068aa in js::GlobalHelperThreadState::runTaskLocked(JS::HelperThreadTask*, js::AutoLockHelperThreadState&) /firefox/obj-js-debug/js/src/vm/./../../../../js/src/vm/HelperThreads.cpp:666:11
    #5 0x561faac233d7 in js::GlobalHelperThreadState::runOneTask(JS::HelperThreadTask*, js::AutoLockHelperThreadState&) /firefox/obj-js-debug/js/src/vm/./../../../../js/src/vm/HelperThreads.cpp:622:3
    #6 0x561faac233d7 in js::HelperThread::threadLoop(js::InternalThreadPool*) /firefox/obj-js-debug/js/src/vm/./../../../../js/src/vm/InternalThreadPool.cpp:324:25
    #7 0x561faac2313b in js::HelperThread::ThreadMain(js::InternalThreadPool*, js::HelperThread*) /firefox/obj-js-debug/js/src/vm/./../../../../js/src/vm/InternalThreadPool.cpp:251:11
    #8 0x561faac30696 in void js::detail::ThreadTrampoline<void (&)(js::InternalThreadPool*, js::HelperThread*), js::InternalThreadPool*&, js::HelperThread*>::callMain<0ul, 1ul>(std::integer_sequence<unsigned long, 0ul, 1ul>) /firefox/js/src/threading/Thread.h:223:5
    #9 0x561faac30696 in js::detail::ThreadTrampoline<void (&)(js::InternalThreadPool*, js::HelperThread*), js::InternalThreadPool*&, js::HelperThread*>::Start(void*) /firefox/js/src/threading/Thread.h:212:11
    #10 0x7eb021d82b83  (/lib/x86_64-linux-gnu/libc.so.6+0x9cb83) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #11 0x7eb021e0fecb  (/lib/x86_64-linux-gnu/libc.so.6+0x129ecb) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)

==16027==Register values:
rax = 0x00007eb020ffe6c0  rbx = 0x000039bd76500000  rcx = 0x0000000000000000  rdx = 0x0000000000000002  
rdi = 0x0000561facc259a0  rsi = 0x0000000000000080  rbp = 0x00007eb020ffdb30  rsp = 0x00007eb020ffdaf0  
 r8 = 0x0000000000000000   r9 = 0x00000000000022a2  r10 = 0x0000000000000000  r11 = 0x0000000000000282  
r12 = 0x0000561facc25b08  r13 = 0x0000000000000001  r14 = 0x0000561facc259c8  r15 = 0x0000561facc259a0  
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/js/src/gc/./../../../../js/src/gc/GC.cpp:3215:7 in js::gc::BackgroundUnmarkTask::unmark()
==16027==ABORTING
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file testcase.js —
Attached file README.md —

This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.

Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

This is not security sensitive. GCRuntime::releaseHeldRelocatedArenasWithoutUnlocking is only present in debug builds so this doesn't affect release builds.

Group: javascript-core-security
Keywords: sec-other
Assignee: nobody → jcoppeard

We need to wait for the background unmark task, which iterates through the
chunk lists, before we call GCRuntime::releaseRelocatedArenasWithoutUnlocking
which can mutate the chunk lists.

Pushed by jcoppeard@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/90f73766bb21 https://hg.mozilla.org/integration/autoland/rev/3a26ee747fbb Also wait for background unmarking task when handling alloc failure r=spidermonkey-reviewers,iain
Status: UNCONFIRMED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 160 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: