Closed Bug 2042732 Opened 5 months ago Closed 5 months ago

Heap buffer overflow read in [@ AutoMessageArgs::init] via non-NUL-terminated wasm ES-module name

Categories

(Core :: JavaScript Engine, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2040428

People

(Reporter: bugmon, Assigned: dminor)

Details

(5 keywords)

Attachments

(5 files)

js/src/vm/ErrorReporting.cpp builds error messages by treating each variadic argument as a NUL-terminated C string. In AutoMessageArgs::init (line 269) it computes lengths_[i] = strlen(args_[i]) and later copies that many bytes. This is safe only when every argument the engine passes is actually NUL-terminated.

The WebAssembly ES-module source-phase integration violates that invariant. When a wasm module is compiled as an ES-module source (js::wasm::CompileForESM in js/src/wasm/WasmJS.cpp), it validates import/export names against reserved prefixes. wasm names are decoded by DecodeName (js/src/wasm/WasmValidate.cpp:2919) into a CacheableName backed by a mozilla::Vector<char> (UTF8Bytes) sized EXACTLY to the name length with NO trailing NUL. CacheableName::utf8Bytes() returns a mozilla::Span<const char> directly over that storage. CompileForESM passes name.data() (the raw, non-NUL-terminated pointer) to JS_ReportErrorNumberUTF8 for JSMSG_WASM_ESM_RESERVED_EXPORT_NAME / _FIELD_NAME / reserved module name. Because that message has a {0} placeholder, the pointer reaches AutoMessageArgs::init, where strlen reads past the end of the exact-sized heap allocation.

Result: a heap-buffer-overflow READ. The over-read bytes (adjacent heap data) also get copied into the resulting error-message string, which is observable to the attacker (the module author) as the thrown error's message — an information-disclosure primitive — and can crash when the read runs into an unmapped page.

Build Info

Affected Code

File: js/src/vm/ErrorReporting.cpp, line 260-270

case ArgumentsAreASCII:
case ArgumentsAreUTF8: {
  const char* c = argsArg ? static_cast<const char**>(argsArg)[i]
                          : va_arg(ap, const char*);
  args_[i] = c;
  MOZ_ASSERT_IF(typeArg == ArgumentsAreASCII,
                JS::StringIsASCII(args_[i]));
  lengths_[i] = strlen(args_[i]);   // <-- OOB READ: args_[i] is not NUL-terminated
  break;
}

File: js/src/wasm/WasmJS.cpp, line 721-731

for (const Export& exp : moduleMeta.exports) {
  Span<const char> name = exp.fieldName().utf8Bytes();   // Span over a Vector<char>, no NUL
  if (CharsStartsWith(name, MakeStringSpan("wasm:")) ||
      CharsStartsWith(name, MakeStringSpan("wasm-js:"))) {
    JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr,
                             JSMSG_WASM_ESM_RESERVED_EXPORT_NAME,
                             name.data());   // <-- non-NUL-terminated pointer as %s-style arg
    return false;
  }
}

File: js/src/wasm/WasmValidate.cpp, line 2919

// DecodeName: name bytes are read into a Vector<char> sized exactly to the
// byte count; there is NO terminating NUL byte appended.
if (!bytes.resizeUninitialized(numBytes)) { ... }   // capacity == numBytes
... memcpy/copy of numBytes ...

The crashing strlen in the analyzed file, and the caller that supplies the non-NUL-terminated pointer plus the allocation that lacks a terminator.

Exploit Chain

  1. Attacker authors a WebAssembly module that is structurally valid and whose export (or import-field/import-module) name begins with the reserved prefix "wasm:" (or "wasm-js:").
  2. The module is imported as a source-phase ES module: import.source("./evil.wasm") (requires the experimental prefs source-phase-imports and wasm-esm-integration).
  3. The shell module loader reads the .wasm bytes and calls JS::CompileWasmModuleAsSource -> js::wasm::CompileForESM.
  4. The wasm decoder (DecodeName) stores the export name in a CacheableName whose Vector<char> buffer is exactly the name length, with no NUL terminator.
  5. CompileForESM detects the reserved prefix and calls JS_ReportErrorNumberUTF8(JSMSG_WASM_ESM_RESERVED_EXPORT_NAME, name.data()), passing the non-NUL-terminated pointer.
  6. ExpandErrorArgumentsHelper -> AutoMessageArgs::init runs strlen(name.data()) at ErrorReporting.cpp:269, reading past the heap allocation (heap-buffer-overflow READ); the over-read bytes are copied into the produced error-message string.

Steps to Reproduce

  1. Generate the malicious module: python3 gen_evil_wasm.py (produces evil.wasm next to poc.js).
  2. Run the ASAN shell from that directory: /firefox/obj-firefox-asan/dist/bin/js --fuzzing-safe --enable-source-phase-imports --enable-wasm-esm-integration poc.js.
  3. AddressSanitizer reports a heap-buffer-overflow READ with SUMMARY at js/src/vm/ErrorReporting.cpp:269 in AutoMessageArgs::init.
  4. Equivalent confirmation via mcp__clauditor__js_shell_evaluator (forces --fuzzing-safe): flags ["--enable-source-phase-imports","--enable-wasm-esm-integration"], content import.source("/ABS/PATH/evil.wasm").then(()=>{},()=>{}); with evil.wasm pre-placed at that absolute path.

Security Impact

  • Severity: Moderate
  • Attacker capability: Out-of-bounds heap READ in the content/JS process: the strlen runs off the end of an exact-sized heap allocation, leaking adjacent heap bytes into the thrown error message (information disclosure of heap memory) and potentially crashing (DoS) if the read reaches an unmapped page. It is a read primitive, not a write.
  • Preconditions: Requires the experimental WebAssembly ES-module source-phase integration to be enabled (build flag ENABLE_SOURCE_PHASE_IMPORTS plus runtime prefs experimental.source_phase_imports and experimental.wasm_esm_integration), and the ability to have a crafted .wasm module imported via import.source. These prefs are off by default in shipping Firefox; the bug is reachable in the JS shell with the corresponding flags (and under --fuzzing-safe).

ASAN Report

==1506==ERROR: AddressSanitizer: heap-buffer-overflow READ of size 143, 0 bytes after a 128-byte region.
  #0 strlen
  #1 AutoMessageArgs::init<js::FrontendAllocator>(...) js/src/vm/ErrorReporting.cpp:269:25
  #2 ExpandErrorArgumentsHelper<JSErrorReport>(...) js/src/vm/ErrorReporting.cpp:366:19
  #3 js::ExpandErrorArgumentsVA(...) js/src/vm/ErrorReporting.cpp:456:10
  #4 js::ReportErrorNumberVA(...) js/src/vm/ErrorReporting.cpp:479:8
  #5/#6 JS_ReportErrorNumberUTF8VA / JS_ReportErrorNumberUTF8 js/src/jsapi.cpp:3960/3950
  #7 js::wasm::CompileForESM(...) js/src/wasm/WasmJS.cpp
  #8 JS::CompileWasmModuleAsSource(...) js/src/vm/Modules.cpp:350
  #9 js::shell::ModuleLoader::loadAndParse(...) js/src/shell/ModuleLoader.cpp:685
allocated by: DecodeName(...) js/src/wasm/WasmValidate.cpp:2919 (CacheableName Vector<char>, exact-sized, no NUL)
SUMMARY: AddressSanitizer: heap-buffer-overflow js/src/vm/ErrorReporting.cpp:269:25 in AutoMessageArgs::init
Attached file poc.js —
Attached file gen_evil_wasm.py —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file RUN.txt —
Group: core-security → javascript-core-security

NI Dan because this requires Wasm ESM integration.

Flags: needinfo?(dminor)

This looks like a dup of Bug 2040428, but I'll double check.

Assignee: nobody → dminor
Flags: needinfo?(dminor)

Confirmed this was fixed by Bug 2040428.

Status: UNCONFIRMED → RESOLVED
Closed: 5 months ago
Duplicate of bug: 2040428
Resolution: --- → DUPLICATE
Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: