Closed Bug 1991309 Opened 1 year ago Closed 1 year ago

Assertion failure: HasBuiltinTypedArraySpecies(obj, cx), at js/src/vm/TypedArrayObject.cpp:4307

Categories

(Core :: JavaScript Engine: JIT, defect)

defect

Tracking

()

RESOLVED FIXED
145 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 --- unaffected
firefox143 --- wontfix
firefox144 --- wontfix
firefox145 --- fixed

People

(Reporter: sakura, Assigned: anba)

References

(Regression)

Details

(Keywords: regression, reporter-external, Whiteboard: [client-bounty-form])

Attachments

(1 file, 1 external file)

Attached file poc.js —

Reproduce

  1. Clone the Firefox mirror from https://github.com/mozilla-firefox/firefox
  2. Run build command in the js/src directory of the firefox checkout:
    mkdir fuzzbuild_OPT.OBJ && cd fuzzbuild_OPT.OBJ && ../configure --enable-address-sanitizer --disable-jemalloc --enable-debug --enable-optimize --disable-shared-js --enable-application=js --enable-gczeal && make -j64
    
  3. Run poc: fuzzbuild_OPT.OBJ/dist/bin/js poc.js

Bisect

https://hg.mozilla.org/integration/autoland/pushloghtml?fromchange=50fc56fdf66b5dcfd8f398c100439f1cafd3cfa1&tochange=68d37f9acbd49fbe5b7ad759935475128bcf26e1

Root Cause

The issue occurs when the JIT compiler makes optimization assumptions about TypedArray objects that are invalidated during exception handling and bailout recovery.

The JIT compiler optimizes TypedArray.prototype.subarray() calls by assuming the object maintains its built-in Species behavior throughout its lifetime. This optimization allows for faster execution by bypassing runtime checks that would normally verify the object's prototype chain and constructor properties.

The HasBuiltinTypedArraySpecies function (TypedArrayObject.cpp:3782-3804) validates that a TypedArray object still maintains its built-in Species behavior:

static bool HasBuiltinTypedArraySpecies(TypedArrayObject* obj, JSContext* cx) {
  // Check if TypedArray species fuse is intact
  if (!cx->realm()->realmFuses.optimizeTypedArraySpeciesFuse.intact()) {
    return false;
  }

  // Ensure object's prototype is the actual concrete TypedArray.prototype
  auto* proto = cx->global()->maybeGetPrototype(protoKey);
  if (!proto || obj->staticPrototype() != proto) {
    return false;
  }

  // Fail if object has an own 'constructor' property
  if (obj->containsPure(cx->names().constructor)) {
    return false;
  }

  return true;
}

This function performs three critical checks: ensuring the TypedArray species optimization fuse is intact, verifying the object's prototype hasn't been modified, and confirming the object doesn't have a custom constructor property.

During JIT bailout recovery, TypedArraySubarrayRecover (TypedArrayObject.cpp:4307) asserts that the Species assumption still holds:

TypedArrayObject* js::TypedArraySubarrayRecover(JSContext* cx,
                                                Handle<TypedArrayObject*> obj,
                                                intptr_t start,
                                                intptr_t length) {
  MOZ_ASSERT(!obj->is<ResizableTypedArrayObject>());
  MOZ_ASSERT(HasBuiltinTypedArraySpecies(obj, cx));  // FAILS HERE
  MOZ_ASSERT(start >= 0);
  MOZ_ASSERT(length >= 0);
  // ...
}

The assertion at line 4307 expects that the object still satisfies the Species requirements that were valid during the initial JIT compilation phase.

The POC trigger this vulnerability through a carefully orchestrated sequence:

function triggerTypedArraySpeciesBug() {
  var typedArray = new Int8Array();

  function recursiveSubarray() {
    try {
      var subarrayResult = typedArray.subarray();
      recursiveSubarray(); // Trigger stack overflow exception
      Object.setPrototypeOf(typedArray, Object.create(RegExp.prototype)); // Corrupt prototype during bailout
    } catch (e) {}
  }

  for (var i = 0; i < 100; ++i) {
    recursiveSubarray();
  }
}

try {
  triggerTypedArraySpeciesBug();
} catch (e) {}

First, it creates a TypedArray and calls subarray() to trigger JIT compilation with Species optimization enabled. Then it uses recursive function calls to force a stack overflow exception and subsequent bailout from optimized code. Crucially, during the exception handling process, it calls Object.setPrototypeOf() to modify the TypedArray's prototype chain, violating the Species assumption. When the JIT system attempts to recover the TypedArraySubarray instruction during bailout, the assertion fails because the object no longer satisfies the Species requirements that were assumed during compilation.

Crash Stack Trace

Assertion message: Assertion failure: HasBuiltinTypedArraySpecies(obj, cx), at /home/sakura/gecko-dev/js/src/vm/TypedArrayObject.cpp:4307

[2945798] Assertion failure: HasBuiltinTypedArraySpecies(obj, cx), at /home/sakura/gecko-dev/js/src/vm/TypedArrayObject.cpp:4307
#01: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x248a0b1]
#02: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x30977e7]
#03: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x3757a8d]
#04: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x3755a21]
#05: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x2fee57a]
#06: ???[/home/sakura/gecko-dev/js/src/fuzzbuild_OPT.OBJ/dist/bin/js +0x2fedbe5]
#07: ??? (???:???)
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==2945798==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55a0c81c60c9 bp 0x7ffc10688e80 sp 0x7ffc10688e20 T2945798)
==2945798==The signal is caused by a WRITE memory access.
==2945798==Hint: address points to the zero page.
    #0 0x55a0c81c60c9 in MOZ_CrashSequence(void*, long) js/src/fuzzbuild_OPT.OBJ/dist/include/mozilla/Assertions.h:248:3
    #1 0x55a0c81c60c9 in js::TypedArraySubarrayRecover(JSContext*, JS::Handle<js::TypedArrayObject*>, long, long) js/src/vm/TypedArrayObject.cpp:4307:3
    #2 0x55a0c8dd37e6 in js::jit::RTypedArraySubarray::recover(JSContext*, js::jit::SnapshotIterator&) const js/src/jit/Recover.cpp:2590:18
    #3 0x55a0c9493a8c in js::jit::SnapshotIterator::computeInstructionResults(JSContext*, js::jit::RInstructionResults*) const js/src/jit/JitFrames.cpp:2246:29
    #4 0x55a0c9491a20 in js::jit::SnapshotIterator::initInstructionResults(js::jit::MaybeReadFallback&) js/src/jit/JitFrames.cpp:2199:12
    #5 0x55a0c8d2a579 in js::jit::BailoutIonToBaseline(JSContext*, js::jit::JitActivation*, js::jit::JSJitFrameIter const&, js::jit::BaselineBailoutInfo**, js::jit::ExceptionBailoutInfo const*, js::jit::BailoutReason) js/src/jit/BaselineBailouts.cpp:1647:17
    #6 0x55a0c8d29be4 in js::jit::Bailout(js::jit::BailoutStack*, js::jit::BaselineBailoutInfo**) js/src/jit/Bailouts.cpp:146:7
    #7 0x06a2fd4df3e1  ([anon:js-executable-memory]+0x6a2fd4df3e1)

==2945798==Register values:
rax = 0x0000000000000000  rbx = 0x00007f0651a3de00  rcx = 0x00000000000010d3  rdx = 0x00007f065381b723
rdi = 0x00007f065381ca60  rsi = 0x0000000000000000  rbp = 0x00007ffc10688e80  rsp = 0x7ffc10688e20
 r8 = 0x0000000000000000   r9 = 0x0000000000000003  r10 = 0x0000000000000039  r11 = 0x0000000000000293
r12 = 0x0000000000000000  r13 = 0x000055a0ca116100  r14 = 0x00007ffc10688ea8  r15 = 0x0000000000000000
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV js/src/fuzzbuild_OPT.OBJ/dist/include/mozilla/Assertions.h:248:3 in MOZ_CrashSequence(void*, long)
==2945798==ABORTING
Flags: sec-bounty?
Group: firefox-core-security → javascript-core-security
Component: Security → JavaScript Engine: JIT
Product: Firefox → Core

Below is a test case that doesn't use recursion or try-catch. We bail out and then recover the subarray result, but by that point the species state may no longer be valid.

anba, is this just a bogus assertion in TypedArraySubarrayRecover (and in TypedArraySubarrayWithLength for that caller)?

const len = 2000;
function mutate(i) {
  with ({});
  if (i === len - 10) {
    Int8Array.prototype.__proto__.constructor = {};
  }
}
function createSubarray(typedArray, i) {
  var res = typedArray.subarray(i);
  mutate(i);
  return res.length;
}
function test() {
  with ({});
  var typedArray = new Int8Array(len);
  for (var i = 0; i < len; i++) {
    assertEq(createSubarray(typedArray, i), len - i);
  }
}
test();
Flags: needinfo?(andrebargull)
Keywords: regression
Regressed by: 1980613

Set release status flags based on info from the regressing bug 1980613

(In reply to Jan de Mooij [:jandem] from comment #1)

anba, is this just a bogus assertion in TypedArraySubarrayRecover (and in TypedArraySubarrayWithLength for that caller)?

Yes, these are just overzealous assertions, which can both be safely removed.

Flags: needinfo?(andrebargull)

The assertion in TypedArraySubarrayWithLength was only used to ensure we emit
the necessary species guards to allow inlining subarray. These guards can be
violated when recovering subarray, though.

Assignee: nobody → andrebargull
Status: NEW → ASSIGNED

So is this actually a security issue or not :)

No, it's not a security issue. (But I don't have the necessary rights to unhide this bug report.)

Group: javascript-core-security
Keywords: csectype-jit
Attachment #9517694 - Attachment description: (secure) → Bug 1991309: Remove overzealous assertions when recovering subarray. r=jandem!
Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 145 Branch

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

For more information, please visit BugBot documentation.

Flags: needinfo?(andrebargull)

This is a debug-only assertion, so probably not worth it to uplift to beta.

Flags: needinfo?(andrebargull)
Flags: sec-bounty? → sec-bounty-
QA Whiteboard: [qa-triage-done-c146/b145]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: