Closed Bug 2042761 Opened 5 months ago Closed 5 months ago

OOB read in WASM baseline compiler inline memory.copy [@ BaseCompiler::memCopyInlineM32] on custom tiny-page memories

Categories

(Core :: JavaScript: WebAssembly, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2026383

People

(Reporter: bugmon, Unassigned)

Details

(5 keywords)

Attachments

(4 files)

BaseCompiler::memCopyInlineM32() in js/src/wasm/WasmBCMemory.cpp implements an inlined wasm memory.copy by first loading all source bytes onto the value stack, then storing them to the destination. To avoid redundant checks, the load phase emits a bounds check only for the FIRST (lowest-address) load and sets check.omitBoundsCheck = true for every subsequent higher-address load. This optimization is sound only because of the invariant static_assert(MaxInlineMemoryCopyLength < MinOffsetGuardLimit) (WasmMemory.cpp:330-331): every wasm memory is assumed to have a guard region large enough that the un-checked tail reads (length <= 64) always land inside the guard.

That invariant does not hold for custom tiny-page-size memories ((memory ... (pagesize 1))). GetMaxOffsetGuardLimit() returns 0 for non-Standard page sizes (WasmMemory.cpp:320), ComputeMappedSize() explicitly omits GuardSize for PageSize::Tiny (WasmMemory.cpp:421-431), and WasmMemoryObject::boundsCheckLimit() returns the exact byte length with no slack for tiny pages (WasmJS.cpp:2896-2901). Yet emitMemCopy() (WasmBaselineCompile.cpp:6818-6824) selects memCopyInlineM32() for ANY mem32 memory-0 copy whose length is a constant in (0, 64], with no page-size check. Because enabling custom page sizes also disables Ion (IonDisabledByFeatures, WasmCompile.cpp:210), the function is guaranteed to be baseline-compiled, i.e. this exact code runs.

Consequently an inline memory.copy whose source is near the end of a tiny-page memory reads up to ~48 bytes past the memory's exact mapping with no bounds check. When the page after the mapping is mapped, this discloses adjacent heap memory (leaked heap pointers were observed) into wasm-readable space; when it is unmapped it is a wild out-of-bounds read that crashes (SIGSEGV).

Build Info

Affected Code

File: js/src/wasm/WasmBCMemory.cpp, line 2497-2517

  // Load all source bytes onto the value stack from low to high using the
  // widest transfer width we can for the system. We will trap without writing
  // anything if any source byte is out-of-bounds.
  bool omitBoundsCheck = false;
  size_t offset = 0;

#ifdef ENABLE_WASM_SIMD
  for (uint32_t i = 0; i < numCopies16; i++) {
    RegI32 temp = needI32();
    moveI32(src, temp);
    pushI32(temp);

    MemoryAccessDesc access(memoryIndex, Scalar::Simd128, 1, offset,
                            trapSiteDesc(), hugeMemoryEnabled(memoryIndex));
    AccessCheck check;
    check.omitBoundsCheck = omitBoundsCheck;   // false only for the FIRST load
    loadCommon(&access, check, ValType::V128);

    offset += sizeof(V128);
    omitBoundsCheck = true;   // every subsequent (higher-address) load is unchecked
  }
#endif

File: js/src/wasm/WasmBaselineCompile.cpp, line 6818-6824

  if (dstMemIndex == 0 && srcMemIndex == 0 && isMem32(dstMemIndex)) {
    int32_t signedLength;
    if (peekConst(&signedLength) && signedLength != 0 &&
        uint32_t(signedLength) <= MaxInlineMemoryCopyLength) {
      memCopyInlineM32();   // selected with NO check on the memory's page size
      return true;
    }
  }

File: js/src/wasm/WasmMemory.cpp, line 320-332

  uint64_t guardLimit = sz == PageSize::Standard ? OffsetGuardLimit : 0; // 0 for Tiny
  ...
// Tiny pages get NO guard region (ComputeMappedSize omits GuardSize for Tiny),
// so the inline-copy invariant below does not actually hold for them:
static const size_t MinOffsetGuardLimit = OffsetGuardLimit;
static_assert(MaxInlineMemoryCopyLength < MinOffsetGuardLimit, "precondition");

The inline copy elides bounds checks on all but the first source load, relying on a guard region that tiny-page memories do not have. emitMemCopy selects this path with no page-size guard, and ComputeMappedSize/GetMaxOffsetGuardLimit confirm tiny pages have zero guard.

Exploit Chain

  1. Enable the compiled-in custom-page-sizes feature (--setpref=wasm_custom_page_sizes=true); this also disables Ion, forcing baseline compilation of all wasm functions.
  2. Create wasm mem32 memories with a 1-byte page size: (memory i32 N (pagesize 1)). These are mapped by ComputeMappedSize() WITHOUT a guard region; the byte just past the mapping may be unmapped.
  3. Define a function that performs memory.copy(dest=0, src=N-16, len=64) where len is a constant <= MaxInlineMemoryCopyLength (64); emitMemCopy() therefore lowers it via memCopyInlineM32().
  4. Arrange for a tiny-page memory's mapping to be immediately followed by an unmapped page (allocate many 2 MiB tiny-page memories, then free every other one with gc() to leave unmapped 2 MiB holes adjacent to the survivors).
  5. Call run(): the inline copy bounds-checks only the first source load ([N-16, N)); the subsequent elided tail loads read [N, N+48) past the guard-less mapping. With the neighbor mapped this leaks adjacent heap memory; with the neighbor unmapped it triggers a SIGSEGV out-of-bounds read in the baseline-generated wasm code.

Steps to Reproduce

  1. Build the SpiderMonkey shell with ASAN (already at /firefox/obj-firefox-asan/dist/bin/js).
  2. Run: /firefox/obj-firefox-asan/dist/bin/js --fuzzing-safe --setpref=wasm_custom_page_sizes=true wasm_bcmemory_oob.js
  3. Observe AddressSanitizer: SEGV on a READ memory access at a page-aligned address, inside wasm JIT code reached via js::wasm::Instance::callExport -> EnterBaseline, with rax=0x1ffff0 (src=N-16) and rdx=0x200000 (N=2 MiB).

Security Impact

  • Severity: High
  • Attacker capability: Untrusted WebAssembly can perform an out-of-bounds read up to ~48 bytes past the end of a custom tiny-page-size linear memory, with no bounds check. Depending on the adjacent mapping it is either a heap information disclosure (leaking adjacent heap contents, including pointers, into wasm-readable memory and thereby defeating ASLR / aiding further exploitation) or a remotely triggerable crash (SIGSEGV) when the neighboring page is unmapped.
  • Preconditions: The custom-page-sizes wasm feature must be enabled (javascript.options.wasm_custom_page_sizes). It is compiled into the build but off by default; in Firefox it is a Nightly/experimental pref. No other privileges are required — a single wasm module with a tiny-page memory and a constant-length memory.copy suffices, and enabling the feature forces the (vulnerable) baseline compiler.

ASAN Report

==1218==ERROR: AddressSanitizer: SEGV on unknown address 0x7bd203efe000 (pc 0x7bd20ae240c8 bp 0x7ffffa3436d0 sp 0x7ffffa3436c0 T0)
==1218==The signal is caused by a READ memory access.
    #0 0x7bd20ae240c8  (<unknown module>)   <- wasm JIT: inline memory.copy elided tail load
    #2 js::wasm::Instance::callExport(...) js/src/wasm/WasmInstance.cpp:4135
    #3 WasmCall(...) js/src/wasm/WasmInstance.cpp:3918
    #11 EnterBaseline(JSContext*, EnterJitData&) js/src/jit/BaselineJIT.cpp:146
    #12 js::jit::EnterBaselineInterpreterAtBranch(...) js/src/jit/BaselineJIT.cpp:201
Register values: rax = 0x00000000001ffff0 (src index N-16)  rdx = 0x0000000000200000 (N = 2 MiB)
SUMMARY: AddressSanitizer: SEGV (<unknown module>)

The faulting address is page-aligned and rax/rdx match the testcase's source index and memory size, confirming the fault is the inline-copy tail load reading just past the tiny-page memory's guard-less mapping.
Attached file wasm_bcmemory_oob.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file RUN_FLAGS.txt —
Group: core-security → javascript-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 5 months ago
Duplicate of bug: 2040120
Resolution: --- → DUPLICATE

Forwarding the dupe.

Duplicate of bug: 2026383
No longer duplicate of bug: 2040120
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: