Memory leak and accounting corruption in AllocateMappedContent OOM error path
Categories
(Core :: JavaScript: GC, defect, P3)
Tracking
()
People
(Reporter: bugmon, Assigned: jonco)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved, assertion, testcase)
Attachments
(3 files, 1 external file)
|
99 bytes,
text/plain
|
Details | |
|
1.03 KB,
patch
|
Details | Diff | Splinter Review | |
|
1.29 KB,
text/plain
|
Details |
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
- Branch: main
- Revision: cdf7090f93822ada0f5e1b5124fddadbd82c06b2
- Timestamp: 2026-04-25T17:37:40+00:00
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
- Attacker crafts JavaScript that calls createMappedArrayBuffer() under memory pressure conditions (or uses oomTest to systematically inject OOM)
- createMappedArrayBuffer() calls JS::CreateMappedArrayBufferContents() which calls AllocateMappedContent() - this succeeds, allocating mapped memory and incrementing gMappedMemorySizeBytes via RecordMemoryAlloc(mappedLength)
- createMappedArrayBuffer() then calls JS::NewMappedArrayBufferWithContents() which attempts to allocate an ArrayBufferObject - this fails due to OOM
- createMappedArrayBuffer() returns false without calling JS::ReleaseMappedArrayBufferContents() - the mapped memory is permanently leaked and gMappedMemorySizeBytes is never decremented
- On process shutdown, CheckMemorySubsystemOnShutDown() fires MOZ_ASSERT(gMappedMemorySizeBytes == 0) causing a crash
- Repeated triggering leads to resource exhaustion (DoS) as leaked mmap'd regions accumulate in the process address space
Steps to Reproduce
- Build SpiderMonkey with debug assertions enabled (--enable-debug)
- Run the debug JS shell with --fuzzing-safe flag
- Execute the testcase: oomTest(function() { createMappedArrayBuffer("/firefox/obj-js-debug/dist/bin/js", 0, 4096); });
- 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
| Reporter | ||
Comment 1•7 months ago
|
||
| Reporter | ||
Comment 2•7 months ago
|
||
| Reporter | ||
Comment 3•7 months ago
|
||
Updated•7 months ago
|
Comment 5•7 months ago
|
||
This is a defect, yes, but this does not look like something that would sound actionable to me.
| Assignee | ||
Comment 6•5 months ago
|
||
This is not security sensitive.
| Assignee | ||
Comment 7•5 months ago
|
||
Add some missed recovery if we fail to allocate.
Comment 9•5 months ago
|
||
| bugherder | ||
Updated•5 months ago
|
Description
•