Skip to content
Memory corruption Disclosed report Advanced CVE-2021-43539

Crash in Wasm Ion code when gczeal is used with reference types and builtin instance method calls

A bug in the Wasm Ion JIT compiler caused stack maps for instance method calls to be installed at the wrong offset, so during garbage collection the GC missed roots in the stack frame. An attacker could potentially trigger a crash or memory corruption by crafting Wasm code that forces a GC during such a call, though the reporter notes it is difficult to trigger without gczeal.

Asumu Takikawa Follow

Originally disclosed on Mozilla

  • Fixed in 10 days
  • Paid in 18 days
  • Disclosed after 10 days
Text
The following Wasm jit-test test case triggers a crash in Ion:

'''
// crash.js
gczeal(2, 1); // Collect on every allocation.

let exports = wasmEvalText(
  `(module
     (table $tab (export "tab") 5 externref)
     (elem declare funcref (ref.func $g))
     (func $g)
     (func $f (export "f") (param externref) (result)
       (ref.func $g) ;; force a collection via allocation in instance call
                     ;; stack map is not at the right offset as failure mode is FailOnInvalidRef
       (ref.is_null)
       (if
         (then)
         ;; crashes here due to GC happening above
         ;; at this point the externref on stack is a poisoned value
         (else (i32.const 0) (local.get 0) (table.set $tab)))
       )
     )`,
  {}
).exports;
exports.f("foo");
'''

Running this produces a crash like the following

'''
$ mach jit-test --ion crash.js
Assertion failure: ThreadId::ThisThreadId() == owningThread_, at /home/asumu/mozilla-unified/js/src/threading/Mutex.cpp:63                                                                   
UndefinedBehaviorSanitizer:DEADLYSIGNAL                                                                                                                                                      
==28940==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x557b685d175c bp 0x7ffec0503ba0 sp 0x7ffec0503b70 T28940)                                            
==28940==The signal is caused by a WRITE memory access.                                                                                                                                      
==28940==Hint: address points to the zero page.     
'''

This is reduced from another test case (in exception handling Ion code) where it was more obvious that the `externref` parameter to the function `$f` is getting collected and overwritten with a poisoned value, but you can see in gdb that the externref is overwritten with `0x2F` (`JS_FRESH_NURSERY_PATTERN`) if you break in the `tableSet` method.

What appears to be happening is that stack maps for Ion calls to Wasm instance method calls are not being installed correctly (at the right offset), so they are not actually looked up during GC. The GC then misses roots in the stack frame. This happens on methods where the failure mode is not `Infallible`, as there are some error handling instructions inserted between the call return point and the location of the `LSafepoint`.

I'll attach a patch that should fix the offsets later.

I think it is difficult to trigger this bug without gczeal turned on, but I've marked this as a security bug just in case.

The original report as published on Mozilla's Bugzilla, by Asumu Takikawa. 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 the stack map for Ion calls to Wasm instance methods was installed at an incorrect offset because error-handling instructions were inserted between the call return point and the safepoint, so the GC did not look up the stack map and missed roots.

Impact

The bug could cause a crash or memory corruption, but the reporter notes it is difficult to trigger without gczeal turned on.

What the attacker needed

Gczeal enabled (debug build); ability to run Wasm code

The fix

A patch that fixes the offsets was to be attached later.

How it was found

  1. Enable frequent garbage collection with gczeal(2, 1).
  2. Create a Wasm module with an exported function that takes an externref parameter and calls a builtin instance method that allocates, forcing a collection.
  3. Call the exported function with a string argument.
  4. Observe the crash due to the externref being overwritten with a poison value.

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.

This is a bug in a JavaScript engine's optimising compiler for WebAssembly (Wasm). When the compiler generates code for a function call, it also creates a map that tells the garbage collector where live references are on the stack. Here that map was placed at the wrong spot, so the garbage collector missed a reference and overwrote it with a poison value, leading to a crash. The bug is hard to hit without a special debugging tool that forces garbage collection frequently.

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