Closed Bug 2035206 Opened 7 months ago Closed 5 months ago

Memory leak and accounting corruption in AllocateMappedContent OOM error path

Categories

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

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr140 --- wontfix
firefox151 --- wontfix
firefox152 --- wontfix
firefox153 --- fixed

People

(Reporter: bugmon, Assigned: jonco)

References

(Blocks 1 open bug)

Details

(Keywords: ai-involved, assertion, testcase)

Attachments

(3 files, 1 external file)

The AllocateMappedContent function in js/src/gc/Memory.cpp and its call chain contain two related bugs that cause memory leak and memory accounting corruption when OOM occurs during mapped ArrayBuffer creation.

Bug 1 (Memory.cpp lines 1143-1145): On the Linux path of AllocateMappedContent, when mmap with MAP_FIXED fails after MapAlignedPages succeeds, the function calls UnmapInternal(region, mappedLength) but does NOT call RecordMemoryFree(mappedLength). The RecordMemoryAlloc(mappedLength) was already called inside MapAlignedPages, so the gMappedMemorySizeBytes counter becomes permanently inflated. The Windows path (line 1110) correctly calls RecordMemoryFree on failure, confirming this is an oversight on the Linux path.

Bug 2 (shell/js.cpp lines 2370-2374): In CreateMappedArrayBuffer, when JS::NewMappedArrayBufferWithContents(cx, size, contents) fails due to OOM during the ArrayBufferObject allocation, the function returns false without calling JS::ReleaseMappedArrayBufferContents(contents, size). The mapped memory allocated by AllocateMappedContent is leaked, and gMappedMemorySizeBytes is never decremented. The testcase triggers this path via oomTest, which systematically injects OOM at every allocation point. When OOM hits during the ArrayBuffer object creation, the mapped contents are leaked, causing the shutdown assertion MOZ_ASSERT(gMappedMemorySizeBytes == 0) at Memory.cpp:534 to fire.

Build Info

Affected Code

File: js/src/gc/Memory.cpp, line 1133-1146

  void* region = MapAlignedPages(mappedLength, alignment);
  if (!region) {
    return nullptr;
  }

  // Calling mmap with MAP_FIXED will replace the previous mapping, allowing
  // us to reuse the region we obtained without racing with other threads.
  uint8_t* map =
      static_cast<uint8_t*>(mmap(region, alignedLength, PROT_READ | PROT_WRITE,
                                 MAP_PRIVATE | MAP_FIXED, fd, alignedOffset));
  if (map == MAP_FAILED) {
    UnmapInternal(region, mappedLength);
    return nullptr;
    // BUG: RecordMemoryFree(mappedLength) is NOT called here!
  }

File: js/src/gc/Memory.cpp, line 1097-1110

    uintptr_t region = uintptr_t(MapAlignedPages(mappedLength, alignment));
    if (region == 0) {
      break;
    }
    UnmapInternal(reinterpret_cast<void*>(region), mappedLength);
    map = static_cast<uint8_t*>(
        MapViewOfFileEx(hMap, FILE_MAP_COPY, offsetH, offsetL, alignedLength,
                        reinterpret_cast<void*>(region)));
    if (map) {
      break;
    }

    RecordMemoryFree(mappedLength);  // Windows path correctly calls this!

File: js/src/shell/js.cpp, line 2361-2374

  void* contents =
      JS::CreateMappedArrayBufferContents(GET_FD_FROM_FILE(file), offset, size);
  if (!contents) {
    JS_ReportErrorASCII(cx,
                        "failed to allocate mapped array buffer contents "
                        "(possibly due to bad alignment)");
    return false;
  }

  RootedObject obj(cx,
                   JS::NewMappedArrayBufferWithContents(cx, size, contents));
  if (!obj) {
    return false;
    // BUG: contents leaked! Should call JS::ReleaseMappedArrayBufferContents(contents, size)
  }

File: js/src/gc/Memory.cpp, line 533-535

void CheckMemorySubsystemOnShutDown() {
  MOZ_ASSERT(gMappedMemorySizeBytes == 0);
}

Two missing cleanup calls in the AllocateMappedContent error handling chain. Bug 1 is the missing RecordMemoryFree in the Linux mmap failure path. Bug 2 is the missing ReleaseMappedArrayBufferContents in the shell's CreateMappedArrayBuffer when NewMappedArrayBufferWithContents fails.

Exploit Chain

  1. Attacker crafts JavaScript that calls createMappedArrayBuffer() under memory pressure conditions (or uses oomTest to systematically inject OOM)
  2. createMappedArrayBuffer() calls JS::CreateMappedArrayBufferContents() which calls AllocateMappedContent() - this succeeds, allocating mapped memory and incrementing gMappedMemorySizeBytes via RecordMemoryAlloc(mappedLength)
  3. createMappedArrayBuffer() then calls JS::NewMappedArrayBufferWithContents() which attempts to allocate an ArrayBufferObject - this fails due to OOM
  4. createMappedArrayBuffer() returns false without calling JS::ReleaseMappedArrayBufferContents() - the mapped memory is permanently leaked and gMappedMemorySizeBytes is never decremented
  5. On process shutdown, CheckMemorySubsystemOnShutDown() fires MOZ_ASSERT(gMappedMemorySizeBytes == 0) causing a crash
  6. Repeated triggering leads to resource exhaustion (DoS) as leaked mmap'd regions accumulate in the process address space

Steps to Reproduce

  1. Build SpiderMonkey with debug assertions enabled (--enable-debug)
  2. Run the debug JS shell with --fuzzing-safe flag
  3. Execute the testcase: oomTest(function() { createMappedArrayBuffer("/firefox/obj-js-debug/dist/bin/js", 0, 4096); });
  4. The shell crashes on shutdown with: Assertion failure: gMappedMemorySizeBytes == 0, at js/src/gc/Memory.cpp:534

Security Impact

  • Severity: Moderate
  • Attacker capability: Denial of service through resource exhaustion. Each OOM failure during mapped ArrayBuffer creation permanently leaks an mmap'd region. Repeated triggering under memory pressure exhausts virtual address space. The corrupted gMappedMemorySizeBytes counter means the GC's view of mapped memory usage is permanently wrong, potentially masking other memory issues. Leaked mappings also degrade ASLR effectiveness.
  • Preconditions: Requires OOM condition during ArrayBufferObject allocation after AllocateMappedContent succeeds. In browser context, this can occur under normal memory pressure. The oomTest shell function demonstrates the path systematically.

ASAN Report

[579] Assertion failure: gMappedMemorySizeBytes == 0, at /firefox/js/src/gc/Memory.cpp:534
#01: ???[/firefox/obj-js-debug/dist/bin/js +0x2e4ff19]
#02: ???[/firefox/obj-js-debug/dist/bin/js +0x3efb948]
#03: ???[/firefox/obj-js-debug/dist/bin/js +0x2703629]
#04: ???[/lib/x86_64-linux-gnu/libc.so.6 +0x2a1ca]
#05: __libc_start_main[/lib/x86_64-linux-gnu/libc.so.6 +0x2a28b]
#06: ???[/firefox/obj-js-debug/dist/bin/js +0x26ca379]
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==579==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000
    #0 0x5bc084bfaf31 in MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3
    #1 0x5bc084bfaf31 in js::gc::CheckMemorySubsystemOnShutDown() /firefox/js/src/gc/Memory.cpp:534:3
    #2 0x5bc085ca6947 in ShutdownImpl(JS::detail::FrontendOnly) /firefox/js/src/vm/Initialization.cpp:293:5
    #3 0x5bc0844ae628 in main::$_0::operator()() const /firefox/js/src/shell/js.cpp:12746:44
    #4 0x5bc0844ae628 in mozilla::ScopeExit<main::$_0>::~ScopeExit() /firefox/obj-js-debug/dist/include/mozilla/ScopeExit.h:104:7
    #5 0x5bc0844ae628 in main /firefox/js/src/shell/js.cpp:12865:1
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
==579==ABORTING
Attached file testcase.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security

This is a defect, yes, but this does not look like something that would sound actionable to me.

Blocks: GC.stability
Severity: -- → S4
Priority: -- → P3
Keywords: sec-other

This is not security sensitive.

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

Add some missed recovery if we fail to allocate.

Status: UNCONFIRMED → RESOLVED
Closed: 5 months ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: