Open Bug 2078863 Opened 1 month ago Updated 1 month ago

Wild write to fixed address 0x1000 in [@ gluesmith] via mismatched FFI declaration in shell getWasmSmithModule()

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

UNCONFIRMED

People

(Reporter: bugmon, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: crash, testcase)

Attachments

(3 files)

GetWasmSmithModule in js/src/shell/js.cpp declares the Rust function gluesmith as size_t gluesmith(uint8_t* data, size_t size, uint8_t* out, size_t maxsize), but the definition in js/src/fuzz-tests/gluesmith/src/lib.rs takes (*const u8, usize, *mut *mut u8, *mut usize) -> bool. The caller therefore passes a 4096-byte stack array where a pointer-to-pointer is expected and the integer constant 4096 where a *mut usize is expected. When wasm-smith produces a module, gluesmith stores a heap pointer into the first 8 bytes of the stack array and then writes the module length through the address 0x1000, which faults.

A person who can run a script in a JS shell built with fuzzing interfaces can crash that shell by calling getWasmSmithModule with an ArrayBuffer that wasm-smith accepts, including an empty one. The helper is compiled only under FUZZING_INTERFACES and is registered as a shell function, so the demonstrated reach is fuzzing builds of the standalone shell, not Firefox web content. The fault address is the fixed constant 4096 and is not derived from the input; the only demonstrated effect is a deterministic crash.

Build Info

Affected Code

File: js/src/shell/js.cpp, line 9442-9472

#ifdef FUZZING_INTERFACES
extern "C" {
size_t gluesmith(uint8_t* data, size_t size, uint8_t* out, size_t maxsize);
}

static bool GetWasmSmithModule(JSContext* cx, unsigned argc, Value* vp) {
  ...
  ArrayBufferObject* arrayBuffer = &args[0].toObject().as<ArrayBufferObject>();
  size_t length = arrayBuffer->byteLength();
  uint8_t* data = arrayBuffer->dataPointer();

  const size_t maxModuleSize = 4096;
  uint8_t tmp[maxModuleSize];

  size_t outSize = gluesmith(data, length, tmp, maxModuleSize);
  if (!outSize) {
    JS_ReportErrorASCII(cx, "Generated module is too large.");
    return false;
  }

File: js/src/fuzz-tests/gluesmith/src/lib.rs, line 22-55

#[no_mangle]
pub unsafe extern "C" fn gluesmith(
    data: *const u8,
    len: usize,
    out_bytes: *mut *mut u8,
    out_bytes_len: *mut usize,
) -> bool {
    ...
    let module = match Module::new(config, &mut u) {
        Ok(m) => m,
        Err(_e) => return false,
    };

    let bytes_slice = Box::leak(module.to_bytes().into_boxed_slice());
    out_bytes.write(bytes_slice.as_mut_ptr());
    out_bytes_len.write(bytes_slice.len());
    true
}

File: js/src/fuzz-tests/testWasm.cpp, line 34-37

extern "C" {
bool gluesmith(const uint8_t* data, size_t size, uint8_t** out_bytes,
               size_t* out_bytes_len);
}

The C++ declaration and call site disagree with the Rust definition on the types of the third and fourth arguments and on the return type. The fuzz target in js/src/fuzz-tests/testWasm.cpp:35-36 declares the same function with the matching bool (const uint8_t*, size_t, uint8_t**, size_t*) form. The shell function is listed in shell_functions (js.cpp:10737-10741), which is defined on the global regardless of fuzzingSafe (js.cpp:12090); FUZZING_INTERFACES is set only when fuzzing is enabled with AFL or libFuzzer and without Fuzzilli (build/moz.configure/toolchain.configure:3514-3531). In the reproduced crash the faulting instruction is movq %r14, (%r12) with r12 = 0x1000 and r14 = 8, i.e. the store of the module length (8 bytes) through the maxModuleSize argument. ASAN labels the access READ; the instruction is a store.

Exploit Chain

  1. A script running in a JS shell built with FUZZING_INTERFACES calls getWasmSmithModule(new ArrayBuffer(64)).
  2. GetWasmSmithModule (js/src/shell/js.cpp:9468) calls gluesmith(data, length, tmp, 4096) through a declaration whose third and fourth parameter types do not match the Rust definition.
  3. wasm-smith generates a module from the input (an all-zero or empty buffer yields an 8-byte module), so gluesmith takes its success path.
  4. gluesmith writes the leaked heap pointer into tmp[0..8] via out_bytes.write (lib.rs:52); this store is within the 4096-byte stack array.
  5. gluesmith writes the module length through out_bytes_len, which holds the integer 4096 (lib.rs:53). The store to address 0x1000 faults and the shell aborts with SEGV. No behaviour past this fault was observed.

Steps to Reproduce

  1. Build the JS shell from an unmodified tree with /app/mozconfigs/mozconfig.linux.asan.fuzzing (provides --enable-fuzzing and ASAN).
  2. Save getWasmSmithModule(new ArrayBuffer(64)); as testcase.js.
  3. Run it with js_shell_evaluator against obj-firefox-asan/dist/bin/js (the evaluator adds --fuzzing-safe; no other flags, prefs or patches).
  4. Observe AddressSanitizer: SEGV on unknown address 0x000000001000 with frames gluesmith (lib.rs:53) <- GetWasmSmithModule (js.cpp:9468).
  5. The same fault occurs for ArrayBuffer lengths 0 and 64, and on the existing debug shell (obj-js-debug) where it is reported by UndefinedBehaviorSanitizer with the same top frames.

Security Impact

  • Severity: Low
  • Attacker capability: Deterministic crash of a fuzzing-enabled JS shell. The faulting store targets the fixed address 0x1000 (the constant 4096 passed as an argument) and the stored value is the generated module length; neither the address nor any out-of-bounds memory was shown to be influenced by the input. No memory corruption beyond the faulting store was demonstrated, and no path to this code from Firefox web content was identified.
  • Preconditions: JS shell built with FUZZING_INTERFACES (--enable-fuzzing with libFuzzer or AFL, without Fuzzilli) and the ability to run a script in it that calls the shell-only function getWasmSmithModule. Address 0x1000 must be unmapped for the crash, which was the case in the reproduction.

ASAN Report

AddressSanitizer:DEADLYSIGNAL
=================================================================
==7623==ERROR: AddressSanitizer: SEGV on unknown address 0x000000001000 (pc 0x5650ced2f467 bp 0x7ea5ab8595b0 sp 0x7ea5ab8594e0 T0)
==7623==The signal is caused by a READ memory access.
    #0 0x5650ced2f467 in core::ptr::write::<usize> /builds/worker/fetches/rust/library/core/src/ptr/mod.rs:1917:41
    #1 0x5650ced2f467 in <*mut usize>::write /builds/worker/fetches/rust/library/core/src/ptr/mut_ptr.rs:1418:18
    #2 0x5650ced2f467 in gluesmith /firefox/js/src/fuzz-tests/gluesmith/src/lib.rs:53:19
    #3 0x5650cb58dea2 in GetWasmSmithModule(JSContext*, unsigned int, JS::Value*) /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp:9468:20
    #4 0x5650cd34e494 in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:528:13
    #5 0x5650cd34e494 in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:624:12
    #6 0x5650cd36a3c7 in InternalCall(JSContext*, js::AnyInvokeArgs const&, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:691:10
    #7 0x5650cd36a3c7 in js::CallFromStack(JSContext*, JS::CallArgs const&, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:696:10
    #8 0x5650cd36a3c7 in js::Interpret(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:3278:16
    #9 0x5650cd34d548 in MaybeEnterInterpreterTrampoline(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:420:10
    #10 0x5650cd34d548 in js::RunScript(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:498:13
    #11 0x5650cd351ff7 in js::ExecuteKernel(JSContext*, JS::Handle<JSScript*>, JS::Handle<JSObject*>, js::AbstractFramePtr, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/Interpreter.cpp:888:10
    #12 0x5650cd6e4b58 in ExecuteScript(JSContext*, JS::Handle<JSObject*>, JS::Handle<JSScript*>, JS::MutableHandle<JS::Value>) /firefox/obj-firefox-asan/js/src/vm/./../../../../js/src/vm/CompilationAndEvaluation.cpp:544:10
    #13 0x5650cd6e4e52 in JS_ExecuteScript(JSContext*, JS::Handle<JSScript*>) /firefox/obj-firefox-asan/js/src/vm/./../../../../js/src/vm/CompilationAndEvaluation.cpp:568:10
    #14 0x5650cb5aa216 in RunFile(JSContext*, char const*, _IO_FILE*, CompileUtf8, bool, bool) /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp:1430:10
    #15 0x5650cb5a98c7 in Process(JSContext*, char const*, bool, FileKind) /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp
    #16 0x5650cb551f11 in ProcessArgs(JSContext*, js::cli::OptionParser*) /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp:12385:10
    #17 0x5650cb551f11 in Shell(JSContext*, js::cli::OptionParser*) /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp:12636:12
    #18 0x5650cb53e6dd in main /firefox/obj-firefox-asan/js/src/shell/./../../../../js/src/shell/js.cpp:13056:12
    #19 0x7eab5c6051c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #20 0x7eab5c60528a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #21 0x5650cb442b88 in _start (/firefox/obj-firefox-asan/dist/bin/js+0x2f12b88) (BuildId: eb4617fcbaa3c8d9984171f386522f57)

==7623==Register values:
rax = 0x0000000000000000  rbx = 0x00007ea5ab8594e0  rcx = 0x0000000000000000  rdx = 0x0000000000000001  
rdi = 0x0000000000000001  rsi = 0x00000000000005e8  rbp = 0x00007ea5ab8595b0  rsp = 0x00007ea5ab8594e0  
 r8 = 0x00005650d0af3a00   r9 = 0x0000000000000001  r10 = 0x00000fd56b55ea06  r11 = 0x0000000000000000  
r12 = 0x0000000000001000  r13 = 0x00007ea5ab859640  r14 = 0x0000000000000008  r15 = 0x0000000000000000  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /builds/worker/fetches/rust/library/core/src/ptr/mod.rs:1917:41 in core::ptr::write::<usize>
==7623==ABORTING
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file testcase.js —

This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.

Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

This is a bug in a shell-only testing function that is only included in --enable-fuzzing builds, and even if you jump through all those hoops, it's a deterministic crash. It's not security-sensitive.

Group: javascript-core-security
Keywords: sec-other
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: