Skip to content
Memory corruption Disclosed report Advanced

Write beyond bounds caused by overlarge offset in WASM assembler

The WASM assembler in Firefox could generate an out-of-range offset for a far jump, causing a write beyond bounds when patching the jump. An attacker could craft a large WASM program to trigger this, leading to a crash or potential heap overwrite.

mozillabugs Follow

Originally disclosed on Mozilla

  • Fixed in 31 days
  • Paid in 16 days
  • Disclosed after 31 days
Text
The WASM assembler can generate an out-of-range value for |ModuleGenerator::callFarJumps_.jump.offset_|. If it does, it causes SetInt32() (Patching-x86-shared.h) to attempt to write beyond bounds. When this occurs, SetInt32() is called via the following chain:

   SetInt32()
   ^--- SetRel32() (Patching-x86-shared.h) 
        ^--- patchFarJump() (Assembler-x86-shared.h)
		     ^--- ModuleGenerator::finishCode() (WasmGenerator.cpp)

Attached is a POC that causes this write.


The out-of-range value for |offset_| (0xffffffff820206cb in the attached POC) appears to occur because the POC's WASM program is large enough to caused js::jit::X86Encoding::BaseAssembler::m_formatter.m_buffer to contain more than 0x7fffffff bytes.

When ModuleGenerator::finishCode() attempts to link the code, it eventually calls X86Encoding::BaseAssembler::X86InstructionFormatter::immediateRel32(), which contains this code:

   5093: m_buffer.putIntUnchecked(0);
   5094: return JmpSrc(m_buffer.size());

However, m_buffer.size() returns a |size_t| (64 bits on 64 bit builds) while JmpSrc() takes an |int32_t|. The code here should (but does not) catch this out-of-range value. (Probably some other code should catch it earlier, but I don't understand the assembler well enough to identify that code).


In any case, the bad value propagates back to the caller farJumpWithPatch() (Assembler_x86_shared.h), which calls CodeOffset() (Assembler_shared.h), which puts it, along with the relevant function index, into |callFarJumps_.jump.offset_| (seer ModuleGenerator::linkCallSites()).

Later, when ModuleGenerator::finishCode() calls masm_.patchFarJump(), pathFarJump() computes

    code + farJump.offset()

which points well before the start of |code| because offset() is negative.

The result is an attempt to write beyond bounds. This crashes FF 58.0.1 and 58.0.2 x64 release. On at least the debug version of 58.0.2, however, it successfully overwrites something else on the heap.


I found this bug while attempting to force an overflow elsewhere in the WASM assembler. I will report that bug separately.


Use the PoC by starting FF 58.0.1/.2 x64, attaching a debugger to it, and setting a BP in ModuleGenerator::finishCode() where it processes |callFarJumps_|. Put the POC files on a webserver. Load test40.html in FF (you might need to attach the debugger again if a new content process appears), and wait for the BP to fire. When it does, examine |callFarJumps_| and notice that at least one entry contains a negative offset. Trace the code until you hit the

   mov   dword ptr [r10-4], edx

and watch it either write far beyond bounds or crash with an access violation.

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

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 4 min read.

What the report shows

The target is the JavaScript engine's JIT, specifically the WebAssembly assembler in Firefox. The researcher was working on the WASM assembler and, while trying to force an overflow elsewhere, found that the assembler can generate an out-of-range value for ModuleGenerator::callFarJumps_.jump.offset_. When that happens, SetInt32() in Patching-x86-shared.h attempts to write beyond bounds.

The report gives the call chain that reaches the bad write:

Text
   SetInt32()
   ^--- SetRel32() (Patching-x86-shared.h)
        ^--- patchFarJump() (Assembler-x86-shared.h)
		     ^--- ModuleGenerator::finishCode() (WasmGenerator.cpp)

The researcher attached a proof of concept that triggers the write. The out-of-range value for offset_ in that PoC is 0xffffffff820206cb, and it appears because the PoC's WASM program is large enough to make js::jit::X86Encoding::BaseAssembler::m_formatter.m_buffer hold more than 0x7fffffff bytes.

When ModuleGenerator::finishCode() links the code, it eventually calls X86Encoding::BaseAssembler::X86InstructionFormatter::immediateRel32(), which contains:

Text
   5093: m_buffer.putIntUnchecked(0);
   5094: return JmpSrc(m_buffer.size());

m_buffer.size() returns a size_t (64 bits on 64-bit builds) while JmpSrc() takes an int32_t. The report says this code should catch the out-of-range value but does not, and that some other code probably should catch it earlier, though the researcher could not identify it.

The bad value propagates back to farJumpWithPatch() (Assembler_x86_shared.h), which calls CodeOffset() (Assembler_shared.h), which stores it together with the relevant function index into callFarJumps_.jump.offset_ (see ModuleGenerator::linkCallSites()). Later, when ModuleGenerator::finishCode() calls masm_.patchFarJump(), that function computes:

Text
    code + farJump.offset()

Because offset() is negative, the result points well before the start of code, and the write goes beyond bounds. The report states that this crashes Firefox 58.0.1 and 58.0.2 x64 release, and that on at least the debug version of 58.0.2 it successfully overwrites something else on the heap.

To reproduce, the researcher starts Firefox 58.0.1 or 58.0.2 x64, attaches a debugger, and sets a breakpoint in ModuleGenerator::finishCode() where it processes callFarJumps_. The PoC files go on a web server, test40.html is loaded in Firefox (the debugger may need reattaching if a new content process appears), and the breakpoint is awaited. At that point callFarJumps_ should show at least one entry with a negative offset. Tracing further reaches:

Text
   mov   dword ptr [r10-4], edx

which either writes far beyond bounds or crashes with an access violation.

The report does not give a bounty amount or a fix.

Root cause

The root cause is a narrowing conversion that is not checked. A 64-bit size_t buffer size is passed into a 32-bit int32_t jump source, so once the buffer exceeds 0x7fffffff bytes the stored offset becomes negative. That negative offset is then used as a displacement when patching a far jump, and the computed target lands before the start of the code buffer.

Impact

The report describes a write beyond bounds in the assembler, crashing release builds and overwriting heap memory on a debug build. It does not demonstrate control over what is written or where, so the practical exploitation path is not established in the text.

How this kind of bug works

Assemblers and JIT compilers build machine code in a growable buffer and record positions inside it as offsets. Those offsets are frequently stored in narrower integer types than the buffer size, because in normal operation code is small. When a buffer grows past the range of the narrower type, the offset wraps to a negative value. Any later patch operation that adds such an offset to the buffer base computes an address outside the buffer, and the write lands in unrelated memory.

The general defences are range checks at every narrowing conversion, assertions that offsets stay non-negative and within the buffer, and limits on generated code size so the buffer cannot reach the problematic range. Fuzzing with very large inputs, and builds with integer sanitizers, help surface these cases.

How to test for it

  1. Identify components that generate or patch code: JIT compilers, WASM assemblers, template engines, serialisers that emit bytecode.
  2. Look for places where a buffer size or position is converted to a smaller integer type, or where a signed offset is added to a base pointer.
  3. Craft inputs that make the generated output very large, for example a module with a very large number of functions or call sites, and observe behaviour under a debugger or sanitizer build.
  4. A positive result is a crash, an assertion failure, or a negative offset visible in internal state at a patch site.
  5. Common false positives are ordinary allocation failures or timeouts on huge inputs; confirm that the failure is an out-of-range offset rather than resource exhaustion.

How it is fixed

The report does not describe a fix. Assertions in debug builds help catch regressions.

Hunting notes

This class of bug tends to hide in code generators, assemblers and any component that stores positions as 32-bit values while buffers can exceed 2 GB. Recon signals include large generated artefacts, patch or relocation logic, and mixed signed and unsigned arithmetic around buffer sizes. To make a report accepted, show the exact conversion, the input that drives the buffer past the limit, and the resulting out-of-range access, ideally with a debugger trace or sanitizer output.

The original is short, so this covers everything it reports, then explains this kind of bug in general: how it works, how to test for it and how it is fixed. The general sections are about the bug class, not this target. Written with AI and checked against the original; the original is the record. 7 min read.

What happened, in plain words

Firefox runs JavaScript, the language that makes web pages interactive. To make JavaScript run faster, Firefox contains a component called a JIT compiler. JIT stands for "just in time": instead of slowly interpreting the script line by line, it translates the script into machine code (the raw instructions the processor understands) while the page is running. WebAssembly, or WASM, is a related format that lets pages run compiled programs in the browser at near-native speed, and Firefox has a WASM assembler that builds that machine code.

This report is about a bug in that assembler. The researcher was actually trying to force a different overflow elsewhere in the WASM assembler, and found this one along the way. The report says the other bug would be reported separately.

The assembler builds machine code inside a growable memory buffer. As it writes instructions, it records positions inside that buffer as offsets, meaning "this instruction sits this many bytes from the start". Later it goes back and patches some of those positions, for example to fill in the address of a jump target that was not known when the instruction was first written.

The problem: the buffer can grow very large, and one place in the code stores the buffer size in a narrower number format than the one the buffer actually uses. When the buffer grows past what that narrower format can hold, the stored offset turns negative. A negative offset added to the start of the buffer points before the buffer, into unrelated memory, and the patch write lands there instead.

The report gives the chain of functions that reaches the bad write:

Text
   SetInt32()
   ^--- SetRel32() (Patching-x86-shared.h)
        ^--- patchFarJump() (Assembler-x86-shared.h)
		     ^--- ModuleGenerator::finishCode() (WasmGenerator.cpp)

Read it from the bottom up: ModuleGenerator::finishCode() is the step that finishes and links the generated code; it calls patchFarJump(), which calls SetRel32(), which calls SetInt32(), and that last function is where the out-of-bounds write happens.

The researcher attached a proof of concept (PoC), a small set of files that triggers the bug. In that PoC the bad offset value is 0xffffffff820206cb. It appears because the PoC's WASM program is large enough to make js::jit::X86Encoding::BaseAssembler::m_formatter.m_buffer hold more than 0x7fffffff bytes.

The exact lines where the size is mishandled are these:

Text
   5093: m_buffer.putIntUnchecked(0);
   5094: return JmpSrc(m_buffer.size());

Line 5093 writes a placeholder value into the buffer. Line 5094 asks the buffer how big it is and wraps that answer in a JmpSrc, which represents a jump position. The catch is that m_buffer.size() returns a size_t, a type that is 64 bits wide on 64-bit builds, while JmpSrc() takes an int32_t, a signed 32-bit type. A number too big for 32 bits gets squeezed into it and comes out negative. The report says this code should catch the out-of-range value but does not, and that some other code probably should catch it earlier, though the researcher could not identify which.

The bad value then travels back through farJumpWithPatch() and CodeOffset(), and is stored, together with the relevant function index, in callFarJumps_.jump.offset_. Later, ModuleGenerator::finishCode() calls masm_.patchFarJump(), which computes:

Text
    code + farJump.offset()

Since offset() is negative, this address points well before the start of code. That is the out-of-bounds write. The report states it crashes Firefox 58.0.1 and 58.0.2 x64 release, and that on at least the debug version of 58.0.2 it successfully overwrites something else on the heap (the general pool of memory the program allocates from).

To reproduce, the researcher describes these steps:

  1. Start Firefox 58.0.1 or 58.0.2 x64 and attach a debugger, a tool that lets you pause a program and inspect its memory and variables.
  2. Set a breakpoint (a deliberate pause point) in ModuleGenerator::finishCode() where it processes callFarJumps_.
  3. Put the PoC files on a web server and load test40.html in Firefox. If a new content process appears, the debugger may need attaching again.
  4. Wait for the breakpoint to fire, then examine callFarJumps_: at least one entry should contain a negative offset.
  5. Trace the code until it reaches:
Text
   mov   dword ptr [r10-4], edx

This is a machine instruction that writes a value to memory. Watching it either shows a write far beyond bounds or a crash with an access violation.

Each step matters because the bug only shows itself deep inside the engine: you need the debugger to see the negative offset before the write happens, and the breakpoint is what lets you catch it in the act rather than just seeing a crash.

The report does not give a bounty amount or a fix.

In general, how this kind of bug works

Assemblers and JIT compilers build machine code in a growable buffer and record positions inside it as offsets. Those offsets are often stored in narrower integer types than the buffer size, because in normal use generated code is small. When a buffer grows past the range of the narrower type, the offset wraps around to a negative value. Any later patch operation that adds such an offset to the buffer's starting address computes an address outside the buffer, and the write lands in unrelated memory. A useful comparison: imagine numbering houses on a street with a two-digit counter. Once you pass house 99, the counter rolls over to 00, and anyone following your directions ends up back at the start of the street instead of the house you meant.

The general defences are range checks at every narrowing conversion, assertions that offsets stay non-negative and inside the buffer, and limits on generated code size so the buffer cannot reach the problematic range. Fuzzing with very large inputs, and builds with integer sanitizers (tools that flag suspicious integer arithmetic), help surface these cases.

In general, how to test for it

  1. Identify components that generate or patch code: JIT compilers, WASM assemblers, template engines, serialisers that emit bytecode.
  2. Look for places where a buffer size or position is converted to a smaller integer type, or where a signed offset is added to a base pointer.
  3. Craft inputs that make the generated output very large, for example a module with a very large number of functions or call sites, and observe behaviour under a debugger or sanitizer build.
  4. A positive result is a crash, an assertion failure, or a negative offset visible in internal state at a patch site.
  5. Common false positives are ordinary allocation failures or timeouts on huge inputs; confirm that the failure is an out-of-range offset rather than resource exhaustion.

In general, how it is fixed

The report does not describe a fix. Assertions in debug builds help catch regressions.

In general, where to look

This class of bug tends to hide in code generators, assemblers and any component that stores positions as 32-bit values while buffers can exceed 2 GB. Signals worth watching include large generated artefacts, patch or relocation logic, and mixed signed and unsigned arithmetic around buffer sizes. To make a report accepted, show the exact conversion, the input that drives the buffer past the limit, and the resulting out-of-range access, ideally with a debugger trace or sanitizer output.

Glossary

  • JIT (just-in-time compiler): a component that translates code into machine code while a program runs, to make it faster.
  • WebAssembly (WASM): a binary format that lets browsers run compiled programs at near-native speed.
  • Assembler: the part of the engine that turns instructions into machine code bytes.
  • Machine code: the raw instructions a processor executes.
  • Buffer: a block of memory used to hold data, here the generated machine code.
  • Offset: a distance, in bytes, from a known starting point such as the beginning of a buffer.
  • Patch: going back to an earlier position in generated code to fill in a value that was not known at the time.
  • Out-of-bounds access: reading or writing memory outside the intended buffer.
  • Heap: the general pool of memory a program allocates from at runtime.
  • size_t: an unsigned integer type used for sizes, 64 bits wide on 64-bit builds.
  • int32_t: a signed 32-bit integer type, which can hold positive and negative values up to about 2.1 billion in magnitude.
  • Narrowing conversion: storing a value from a wider type into a narrower one, which can lose information.
  • Breakpoint: a deliberate pause point set in a debugger.
  • Debugger: a tool that lets you pause a running program and inspect its memory and variables.
  • Access violation: a crash caused by touching memory the program is not allowed to access.
  • Proof of concept (PoC): files or steps that reliably demonstrate a bug.
  • Fuzzing: feeding a program many varied or malformed inputs to find crashes and bugs.
  • Sanitizer: a tool built into or alongside a program that flags suspicious behaviour such as bad integer arithmetic.

More by mozillabugs

View all

41 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.