Type confusion in readSharedWasmMemory: missing isWasm() check allows non-wasm SAB as WasmMemoryObject backing
Categories
(Core :: JavaScript Engine, defect, P2)
Tracking
()
People
(Reporter: bugmon, Assigned: jandem)
References
Details
(4 keywords, Whiteboard: [prefs-checked][adv-main151-])
Attachments
(3 files, 1 external file)
|
978 bytes,
text/plain
|
Details | |
|
1.13 KB,
patch
|
Details | Diff | Splinter Review | |
|
2.72 KB,
text/plain
|
Details |
Type confusion in readSharedWasmMemory: missing isWasm() check allows non-wasm SAB as WasmMemoryObject backing
JSStructuredCloneReader::readSharedWasmMemory validates that the deserialized payload is a non-growable SharedArrayBufferObject, but fails to check that the underlying SharedArrayRawBuffer is a wasm buffer (isWasm()). By crafting a SCTAG_BACK_REFERENCE_OBJECT tag in structured clone data to point at a previously-deserialized non-wasm SharedArrayBuffer, an attacker can have a calloc-allocated SharedArrayRawBuffer stored as the backing buffer of a WasmMemoryObject.
All subsequent operations on this WasmMemoryObject call SharedArrayBufferObject::rawWasmBufferObject(), which invokes SharedArrayRawBuffer::toWasmBuffer(). This function contains only a debug-mode MOZ_ASSERT(isWasm()) before performing static_cast<WasmSharedArrayRawBuffer*>(this). In release builds the cast proceeds unconditionally. Because non-wasm raw buffers place user-controllable JS data immediately after sizeof(SharedArrayRawBuffer) (per NonWasmSharedArrayAllocSize), and WasmSharedArrayRawBuffer's additional fields occupy exactly that region, the engine reads growLock_ (a Mutex), addressType_, pageSize_, clampedMaxPages_, sourceMaxPages_, and mappedSize_ directly from attacker-controlled bytes.
The ASAN crash with r14=0x41 (the byte the PoC writes into the SAB) demonstrates addressType_ being read from controlled memory. A separate verification test confirmed that writing 0x00 to the SAB causes confusedMem.type().address to return 'i32', proving the attacker can set valid enum values to bypass the MOZ_CRASH guard and reach deeper code paths including WasmSharedArrayRawBuffer::Lock (which calls lock() on an attacker-controlled Mutex) and wasmGrowToPagesInPlace (which calls CommitBufferMemory using attacker-controlled clampedMaxPages_ and mappedSize_ on a calloc'd heap allocation rather than mmap'd memory).
Build Info
- Branch: main
- Revision: 98bf4b92d3f5d7a9855281df4bf333210bcfbbc4
- Timestamp: 2026-04-01T20:06:40+00:00
Affected Code
File: js/src/vm/StructuredClone.cpp, line 3033-3058
// Read the SharedArrayBuffer object.
RootedValue payload(cx);
if (!startRead(&payload)) {
return false;
}
if (!payload.isObject() ||
!payload.toObject().is<SharedArrayBufferObject>() ||
payload.toObject().as<SharedArrayBufferObject>().isGrowable()) {
// BUG: missing check for .as<SharedArrayBufferObject>().isWasm()
JS_ReportErrorNumberASCII(context(), GetErrorMessage, nullptr,
JSMSG_SC_BAD_SERIALIZED_DATA,
"shared wasm memory must be backed by a "
"non-growable SharedArrayBuffer");
return false;
}
Rooted<ArrayBufferObjectMaybeShared*> sab(
cx, &payload.toObject().as<SharedArrayBufferObject>());
// Construct the memory.
RootedObject proto(
cx, GlobalObject::getOrCreatePrototype(cx, JSProto_WasmMemory));
if (!proto) {
return false;
}
RootedObject memory(
cx, WasmMemoryObject::create(cx, sab, isHuge.toBoolean(), proto));
File: js/src/vm/SharedArrayObject.h, line 214-225
inline WasmSharedArrayRawBuffer* SharedArrayRawBuffer::toWasmBuffer() {
MOZ_ASSERT(isWasm()); // debug-only, no release check
return static_cast<WasmSharedArrayRawBuffer*>(this);
}
inline SharedMem<uint8_t*> SharedArrayRawBuffer::dataPointerShared() const {
uint8_t* ptr =
reinterpret_cast<uint8_t*>(const_cast<SharedArrayRawBuffer*>(this));
ptr += isWasm() ? sizeof(WasmSharedArrayRawBuffer)
: sizeof(SharedArrayRawBuffer); // non-wasm: user data immediately after base header
return SharedMem<uint8_t*>::shared(ptr);
}
File: js/src/vm/SharedArrayObject.h, line 125-135
class WasmSharedArrayRawBuffer : public SharedArrayRawBuffer {
private:
Mutex growLock_ MOZ_UNANNOTATED; // overlays user data byte 0..
// The address type of this buffer.
wasm::AddressType addressType_; // overlays user data
// The size of each wasm page in this buffer.
wasm::PageSize pageSize_; // overlays user data
// The maximum size of this buffer in wasm pages.
wasm::Pages clampedMaxPages_; // overlays user data
wasm::Pages sourceMaxPages_; // overlays user data
size_t mappedSize_; // overlays user data
File: js/src/vm/SharedArrayObject.cpp, line 44-47
static size_t NonWasmSharedArrayAllocSize(size_t length) {
MOZ_ASSERT(length <= ArrayBufferObject::ByteLengthLimit);
return sizeof(SharedArrayRawBuffer) + length; // header + user data, single calloc
}
File: js/src/wasm/WasmJS.cpp, line 2539-2598
WasmSharedArrayRawBuffer* WasmMemoryObject::sharedArrayRawBuffer() const {
MOZ_ASSERT(isShared());
return buffer().as<SharedArrayBufferObject>().rawWasmBufferObject(); // unchecked downcast
}
// ...
wasm::AddressType WasmMemoryObject::addressType() const {
if (isShared()) {
return sharedArrayRawBuffer()->wasmAddressType(); // reads addressType_ from user data
}
return buffer().wasmAddressType();
}
The validation at StructuredClone.cpp:3038-3046 checks is<SharedArrayBufferObject>() and !isGrowable() but omits isWasm(). The startRead() call at line 3035 can return any object via SCTAG_BACK_REFERENCE_OBJECT, including a non-wasm SAB that was deserialized earlier in the same clone operation. This non-wasm SAB passes both existing checks. The resulting WasmMemoryObject holds a SharedArrayRawBuffer whose isWasm_ field is false, but every wasm code path calls toWasmBuffer() which only asserts isWasm() in debug builds before downcasting. The WasmSharedArrayRawBuffer subclass fields occupy the exact memory region that, in a non-wasm buffer, holds user-writable JavaScript data (per dataPointerShared()'s offset calculation and NonWasmSharedArrayAllocSize).
Exploit Chain
- Allocate a regular (non-wasm) SharedArrayBuffer and a real shared WebAssembly.Memory in the same realm.
- Serialize an array containing [regularSAB, mem.buffer, mem] so all three objects are recorded in the structured clone allObjs table during deserialization (regularSAB at index 1, mem.buffer at index 2).
- When the SCTAG_SHARED_WASM_MEMORY_OBJECT for
memis written, it emits a SCTAG_BACK_REFERENCE_OBJECT pointing at index 2 (mem.buffer, the wasm SAB). - Rewrite the structured clone bytes: locate the SCTAG_BACK_REFERENCE_OBJECT tag (high dword 0xFFFF000D) and change its index payload from 2 to 1, redirecting it to regularSAB.
- Inject the crafted bytes into a JSStructuredCloneData with SameProcess scope. In the shell this is achieved via a TOCTOU in the Deserialize testing function: the scope is read from obj->isSynthetic() before JS_GetProperty(opts, 'SharedArrayBuffer') invokes a user getter that swaps the buffer.
- During deserialization, readSharedWasmMemory calls startRead() which follows the modified backreference and returns regularSAB. The validation at line 3038-3046 passes because regularSAB is a non-growable SharedArrayBufferObject.
- WasmMemoryObject::create stores regularSAB (whose rawBuffer has isWasm_=false) in the BUFFER_SLOT of a new WasmMemoryObject.
- Write attacker-controlled bytes into regularSAB via a Uint8Array view. These bytes occupy the memory region starting at sizeof(SharedArrayRawBuffer), which is exactly where WasmSharedArrayRawBuffer's growLock_, addressType_, pageSize_, clampedMaxPages_, sourceMaxPages_, and mappedSize_ fields are located when the buffer is reinterpreted via static_cast.
- Call confusedMem.grow(1). WasmMemoryObject::growImpl calls addressType() -> sharedArrayRawBuffer() -> rawWasmBufferObject() -> toWasmBuffer(). The MOZ_ASSERT(isWasm()) is debug-only; release builds proceed with the static_cast.
- wasmAddressType() reads addressType_ from attacker-controlled memory. With 0x41, EnforceAddressValue hits MOZ_CRASH('unknown address type'). With 0x00 (verified), it returns valid I32, allowing execution to proceed to WasmSharedArrayRawBuffer::Lock (which calls Mutex::lock() on attacker-controlled growLock_ bytes) and wasmGrowToPagesInPlace (which uses attacker-controlled clampedMaxPages_ and may call CommitBufferMemory/mprotect on calloc'd heap memory).
Steps to Reproduce
- Build SpiderMonkey JS shell (debug or ASAN release).
- Save the testcase to test.js.
- Run: js --fuzzing-safe test.js
- Debug build: Assertion failure: isWasm() at SharedArrayObject.h:215.
- ASAN/release build: MOZ_CRASH(unknown address type) at WasmJS.cpp:631 with r14=0x41 (the controlled byte). To verify field-level control, replace the 0x41 fill loop with zeros and observe confusedMem.type().address returns 'i32' without crashing.
Security Impact
- Severity: High
- Attacker capability: Type confusion granting attacker control over all WasmSharedArrayRawBuffer-specific fields by reading them from a JS-writable SharedArrayBuffer data region. This includes: (1) growLock_ Mutex internals — calling lock()/unlock() on attacker-controlled pthread_mutex_t/CRITICAL_SECTION state; (2) addressType_ — settable to valid I32/I64 to bypass the MOZ_CRASH guard; (3) clampedMaxPages_, sourceMaxPages_, mappedSize_ — control of growth bounds and the engine's belief about how much virtual memory is mapped, enabling CommitBufferMemory (mprotect/VirtualAlloc) calls on calloc'd heap memory rather than the expected mmap'd region, leading to memory protection corruption of adjacent heap allocations and potential out-of-bounds writes via subsequent length_ updates. In the browser, the missing validation is in production structured clone code; while the shell trigger uses a test-only TOCTOU, the same validation flaw applies to any code path that deserializes attacker-influenced JSStructuredCloneData with SameProcess scope (e.g., a compromised content process sending malformed clone data over IPC).
- Preconditions: Ability to control raw structured clone bytes deserialized with JS::StructuredCloneScope::SameProcess and a CloneDataPolicy permitting shared memory. In the JS shell, achievable directly via a TOCTOU in the Deserialize() testing function. In the browser, would require either a compromised process sending crafted JSStructuredCloneData over IPC, or another bug allowing manipulation of clone bytes before deserialization. The validation flaw itself (StructuredClone.cpp:3038-3046) is in production code shipped in Firefox, not test-only code.
ASAN Report
[191036] Hit MOZ_CRASH(unknown address type) at /firefox/js/src/wasm/WasmJS.cpp:631
#01: ???[/firefox/obj-firefox-asan/dist/bin/js +0x5285872]
#02: ???[/firefox/obj-firefox-asan/dist/bin/js +0x52850ef]
#03: ???[/firefox/obj-firefox-asan/dist/bin/js +0x5285c0a]
#04: ???[/firefox/obj-firefox-asan/dist/bin/js +0x4a2231b]
#05: ???[/firefox/obj-firefox-asan/dist/bin/js +0x3cb4bfc]
#06: ??? (???:???)
AddressSanitizer:DEADLYSIGNAL
=================================================================
==191036==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000001 (pc 0x593f968dd89d bp 0x7ffcdcd35e90 sp 0x7ffcdcd35dc0 T0)
==191036==The signal is caused by a WRITE memory access.
==191036==Hint: address points to the zero page.
#0 0x593f968dd89d in MOZ_CrashSequence(void*, long) /firefox/obj-firefox-asan/dist/include/mozilla/Assertions.h:235:3
#1 0x593f968dd89d in EnforceAddressValue(JSContext*, JS::Handle<JS::Value>, js::wasm::AddressType, char const*, char const*, unsigned long*) /firefox/js/src/wasm/WasmJS.cpp:631:7
#2 0x593f968dd0ee in js::WasmMemoryObject::growImpl(JSContext*, JS::CallArgs const&) /firefox/js/src/wasm/WasmJS.cpp:2329:8
#3 0x593f968ddc09 in bool JS::CallNonGenericMethod<&IsMemory(JS::Handle<JS::Value>), &js::WasmMemoryObject::growImpl(JSContext*, JS::CallArgs const&)>(JSContext*, JS::CallArgs const&) /firefox/obj-firefox-asan/dist/include/js/CallNonGenericMethod.h:103:12
#4 0x593f968ddc09 in js::WasmMemoryObject::grow(JSContext*, unsigned int, JS::Value*) /firefox/js/src/wasm/WasmJS.cpp:2354:10
#5 0x593f9607a31a in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:490:13
#6 0x593f9607a31a in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:586:12
#7 0x593f9530cbfb in js::jit::DoCallFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, unsigned int, JS::Value*, JS::MutableHandle<JS::Value>) /firefox/js/src/jit/BaselineIC.cpp:1701:10
#8 0x2581cc902823 (<unknown module>)
==191036==Register values:
rax = 0x0000000000000001 rbx = 0x00007ffcdcd35f20 rcx = 0x0000000000000277 rdx = 0x0000000000000000
rdi = 0x0000593f98d18790 rsi = 0x00007ffcdcd35d78 rbp = 0x00007ffcdcd35e90 rsp = 0x00007ffcdcd35dc0
r8 = 0x0000000000000000 r9 = 0xffffff0000000000 r10 = 0xefffffffffffffff r11 = 0x4000000000000000
r12 = 0x00007d2be44e0c80 r13 = 0x00007ffcdcd36640 r14 = 0x0000000000000041 r15 = 0x00000fff9b9a6bbc
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /firefox/obj-firefox-asan/dist/include/mozilla/Assertions.h:235:3 in MOZ_CrashSequence(void*, long)
==191036==ABORTING
--- DEBUG BUILD ---
[190871] Assertion failure: isWasm(), at /firefox/js/src/vm/SharedArrayObject.h:215
==190871==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5820a5e8fa9a bp 0x7ffe1d554e00 sp 0x7ffe1d554df0 T190871)
#0 0x5820a5e8fa9a in MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:235:3
#1 0x5820a5e8fa9a in js::SharedArrayRawBuffer::toWasmBuffer() /firefox/js/src/vm/SharedArrayObject.h:215:3
#2 0x5820a5e8fa9a in js::SharedArrayBufferObject::rawWasmBufferObject() const /firefox/js/src/vm/SharedArrayObject.h:334:31
#3 0x5820a5e8fa9a in js::WasmMemoryObject::sharedArrayRawBuffer() const /firefox/js/src/wasm/WasmJS.cpp:2541:49
#4 0x5820a5e75fe5 in js::WasmMemoryObject::addressType() const /firefox/js/src/wasm/WasmJS.cpp:2595:12
#5 0x5820a5eabdbd in js::WasmMemoryObject::growImpl(JSContext*, JS::CallArgs const&) /firefox/js/src/wasm/WasmJS.cpp:2329:53
#6 0x5820a5eac63a in js::WasmMemoryObject::grow(JSContext*, unsigned int, JS::Value*) /firefox/js/src/wasm/WasmJS.cpp:2354:10
#7 0x5820a586df04 in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:490:13
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:235:3 in MOZ_CrashSequence(void*, long)
| Reporter | ||
Comment 1•7 months ago
|
||
| Reporter | ||
Comment 2•7 months ago
|
||
| Reporter | ||
Comment 3•7 months ago
|
||
Updated•7 months ago
|
| Assignee | ||
Comment 5•7 months ago
|
||
NI Yury because he has fixed other structured clone issues in this area.
Updated•7 months ago
|
Comment 6•7 months ago
|
||
Prefs: None required — serialize/deserialize are shell test functions but the missing isWasm() validation in readSharedWasmMemory is production structured-clone code reachable via IPC with SameProcess scope. Supported config.
Rating: sec-high looks right. r14=0x41 in the ASAN report confirms addressType_ is read from attacker-written SAB bytes; with a valid enum value written there, execution reaches Mutex::lock() on controlled growLock_ and CommitBufferMemory with controlled mappedSize_ against a calloc'd allocation.
Patch verification (attachment 9565231): Reproduced the crash on current main, applied the patch, rebuilt — testcase now throws InternalError: bad serialized structured data (shared wasm memory must be backed by a non-growable wasm SharedArrayBuffer) at the deserialize() call. Round-tripping a real shared WebAssembly.Memory (including the back-reference path for mem.buffer) and calling .grow() afterward still works. Audited the other WasmMemoryObject::create call sites — WasmModule.cpp uses CreateWasmBuffer, AsmJS.cpp uses non-shared ArrayBufferObject, the shell mailbox stores native pointers — readSharedWasmMemory is the only path accepting an arbitrary deserialized SAB. The patch is correct as-is.
This is an automated analysis result. If this result is incorrect please add a needinfo and feel free to correct the error.
Updated•7 months ago
|
Updated•7 months ago
|
Comment 7•7 months ago
|
||
The test case just replaces serialized data with crafted one, which is never the case in the browser: the serialize+deserialize is a single operation. That's considered as "fuzzing unsafe" scenario. The sec-high is not right rating here.
We may even consider isWasm() MOZ_RELEASE_ASSERT below to enforce this invariant vs throwing the exception.
| Assignee | ||
Comment 8•7 months ago
|
||
(In reply to Yury Delendik (:yury) from comment #7)
The test case just replaces serialized data with crafted one, which is never the case in the browser: the serialize+deserialize is a single operation. That's considered as "fuzzing unsafe" scenario. The sec-high is not right rating here.
The structured clone code has to handle arbitrary input data because it's used for IPC (the parent process can't trust any data coming from a possibly-compromised child process).
If we only care about the same-process case (StructuredCloneScope::SameProcess) for readSharedWasmMemory we should check or release-assert that explicitly.
| Assignee | ||
Comment 9•7 months ago
|
||
Oh I see, the test is cleverly bypassing the check we have for this in the deserialize testing function.
| Assignee | ||
Comment 10•7 months ago
|
||
| Assignee | ||
Updated•7 months ago
|
Comment 11•7 months ago
|
||
Comment 12•7 months ago
|
||
Updated•7 months ago
|
Updated•7 months ago
|
Updated•6 months ago
|
Updated•6 months ago
|
Updated•2 months ago
|
Description
•