Crash [@ ModuleGetExportedNames]
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
People
(Reporter: decoder, Unassigned)
Details
(Keywords: ai-involved, testcase)
Attachments
(1 file)
|
1.70 KB,
application/javascript
|
Details |
Summary
ModuleGetExportedNames (js/src/vm/Modules.cpp:741, ES2023 16.2.1.6.2 GetExportedNames) recursively walks a module's [[StarExportEntries]] (export * from "...") by calling itself for every star-export target (js/src/vm/Modules.cpp:803). Unlike every other recursive Cyclic Module Record operation in the same file, it performs no AutoCheckRecursionLimit check on this recursion path.
A module graph containing a sufficiently deep export * chain therefore drives unbounded native C++ recursion and overflows the engine's native stack, producing an uncontrolled SIGSEGV (ASan: stack-overflow) instead of a catchable InternalError: too much recursion. This reproduces deterministically on both the debug and opt ASan shells (process exits 141).
Root cause
SpiderMonkey converts otherwise-fatal native stack exhaustion into a recoverable InternalError: too much recursion by checking AutoCheckRecursionLimit at every level of any recursion whose depth is attacker/script-controlled. The sibling Cyclic Module Record operations all do exactly this:
InnerModuleLoading—js/src/vm/Modules.cpp:1587InnerModuleLinking—js/src/vm/Modules.cpp:1909InnerModuleEvaluation—js/src/vm/Modules.cpp:2602
ModuleGetExportedNames omits this guard, so the depth of the export * graph directly controls the depth of native C++ recursion. Each frame additionally holds several Rooted<> locals plus a Rooted<ExportNameVector>, so the stack is consumed quickly and a chain of tens of thousands of modules overflows it. (CyclicModuleResolveExport shares the same star-export recursion pattern in this file and is likewise unguarded.)
Reachability / why the PoC links bottom-up
The standard module loader operations are both guarded and run top-down: InnerModuleLoading and InnerModuleLinking each have their own AutoCheckRecursionLimit, so loading or linking a deep chain the normal way throws too much recursion (empirically around depth ~800) long before ModuleGetExportedNames is reached, and GetExportedNames at that bounded depth does not yet overflow. As a result this is not reachable from standard web content going through the HTML top-down module loader.
The PoC reaches the unguarded recursion using the module-record embedding API: it pre-registers every module (registerModule/parseModule, so no deep InnerModuleLoading runs) and then links the chain bottom-up (moduleLink from the leaf upward, so each individual moduleLink only descends into an already-linked dependency and stays shallow, passing the linker's guard). After the whole arbitrarily deep chain is linked, importing the head module's namespace (import * as ns from 'm0') calls GetOrCreateModuleNamespace → ModuleGetExportedNames(m0), which walks the entire chain m0 → m1 → … → mK in one unguarded recursion and overflows the stack. Out-of-order / pre-registered linking is a normal capability of the module-record embedding API (JS::ModuleLink), so this is reachable through the public embedding interface, not merely a shell artifact — but it is an unusual prerequisite, which is reflected in the requested severity. (Suggest the security team re-rate: up if embedder out-of-order linking is considered in scope and the overflow is deemed potentially corrupting; down if treated as a clean guard-page DoS crash.)
Reproduction
Testcase in this finding's directory: poc.js (builds a K = 50000-deep export * chain, registers it, links it bottom-up, then imports the head namespace).
Run with an ASan/debug shell:
/srv/repos/firefox/m-c-asan-debug/dist/bin/js --fuzzing-safe poc.js
/srv/repos/firefox/m-c-fuzzing-asan-opt/dist/bin/js --fuzzing-safe poc.js
The overflow is reproducible and deterministic (a pure function of chain depth vs. native stack size); K = 50000 overflows comfortably on both the debug and opt ASan shells. No prefs are required — only the standard shell module helpers.
Crash signature (debug ASan build)
AddressSanitizer:DEADLYSIGNAL
==...==ERROR: AddressSanitizer: stack-overflow on address 0x... (... T0)
#0 ... __asan_memset
#1 ... mozilla::HashSet<js::ModuleObject*, ...>::put<...>(...) HashTable.h:576
#3 ... ModuleGetExportedNames(...) Modules.cpp:760
#4 ... ModuleGetExportedNames(...) Modules.cpp:803
#5 ... ModuleGetExportedNames(...) Modules.cpp:803
#6 ... ModuleGetExportedNames(...) Modules.cpp:803
... (thousands of identical Modules.cpp:803 frames) ...
The opt ASan build shows the same AddressSanitizer: stack-overflow with the deep ModuleGetExportedNames recursion at Modules.cpp:803. Both builds terminate with exit code 141 (SIGSEGV).
Suggested fix
Add an AutoCheckRecursionLimit check at the start of ModuleGetExportedNames (and CyclicModuleResolveExport), mirroring InnerModuleLinking / InnerModuleLoading / InnerModuleEvaluation:
AutoCheckRecursionLimit recursion(cx);
if (!recursion.check(cx)) {
return false;
}
Related observation (not exercised by this PoC)
In the same function, exportStarSet has type ModuleSet = GCHashSet<ModuleObject*, DefaultHasher<ModuleObject*>, SystemAllocPolicy> (js/src/vm/Modules.cpp:620). This hashes raw, movable GC pointers with a non-stable hasher — the pattern fixed for the sibling VisitedModuleSet in Bug 2034791 (switched to StableCellHasher), and which LoadedModuleMap already uses correctly. In the JS shell this is not observable, because the ModuleGetExportedNames recursion performs only SystemAllocPolicy mallocs and no GC-cell allocations, so no GC can run between building and querying the set; noted here only as a latent hardening opportunity. The demonstrated, reproducible crash is the missing recursion-limit guard described above.
| Reporter | ||
Comment 1•6 months ago
|
||
| Reporter | ||
Comment 2•6 months ago
|
||
We should make sure that this always safely crashes with over-recursion - in which case it wouldn't be a sec bug.
Updated•6 months ago
|
Updated•2 months ago
|
Description
•