Closed Bug 2042991 Opened 5 months ago Closed 5 months ago

Assertion failure: index < tarray->length().valueOr(0), at js/src/vm/TypedArrayObject.cpp:873

Categories

(Core :: JavaScript: Debugger API, defect, P3)

x86_64
Linux
defect

Tracking

()

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

People

(Reporter: decoder, Assigned: jandem)

Details

(Keywords: ai-involved, sec-audit, testcase, Whiteboard: [adv-main153-])

Attachments

(1 file, 1 external file)

Summary

%TypedArray%.prototype.join (js/src/vm/TypedArrayObject.cpp) reads the typed array's elements through a raw, cached data pointer inside TypedArrayJoinKernel without re-validating that the backing buffer is still attached. The length is re-validated exactly once, before the kernel is entered. The kernel's per-iteration CheckForInterrupt(cx) call can re-enter JavaScript; if that JS detaches (or shrinks) the buffer, the loop keeps calling getIndex() on the now-freed/NULL data pointer, producing an out-of-bounds / use-after-free read at an attacker-influenced offset.

Root cause

TypedArray_join captures the length, runs ToString(separator) (which may run user JS), and then re-validates the length once with the actualLength clamp:

// TypedArrayObject.cpp:2401-2403
// Reacquire the length because side-effects may have detached or resized the
// array buffer.
size_t actualLength = std::min(len, tarray->length().valueOr(0));

actualLength is then handed to the kernel (TypedArrayObject.cpp:2426-2434). The kernel loops over [0, actualLength) and on every iteration calls CheckForInterrupt(cx) and then reads the element with getIndex():

// TypedArrayObject.cpp:2315-2326
for (size_t k = 0; k < len; k++) {            // len == actualLength
  if (!CheckForInterrupt(cx)) {               // <-- can run arbitrary JS
    return false;
  }
  if (k > 0 && sep->length() > 0 && !sb.append(sep)) { ... }
  auto element = TypedArrayObjectTemplate<NativeType>::getIndex(tarray, k);  // <-- raw read
  ...
}

getIndex performs no runtime bounds/detach check — it only debug-asserts and then dereferences the cached data pointer:

// TypedArrayObject.cpp:872-876
static const NativeType getIndex(TypedArrayObject* tarray, size_t index) {
  MOZ_ASSERT(index < tarray->length().valueOr(0));
  return jit::AtomicOperations::loadSafeWhenRacy(
      tarray->dataPointerEither().cast<NativeType*>() + index);
}

The actualLength clamp only covers the side effects of ToString(separator). It does not cover side effects that occur inside the kernel loop. CheckForInterrupt(cx) can run arbitrary JS via the engine's interrupt machinery; if that JS detaches the buffer, the data pointer is freed and reset to NULL, but the loop continues calling getIndex(tarray, k) for the remaining indices, reading through the dangling/NULL pointer. Classic TOCTOU: the length is checked once, but the buffer is read many times across a point at which JS can run. TypedArray_join is the same C++ path regardless of JIT tier, so the bug is tier-independent.

Reproduction

Testcase: poc.js (attached / in the finding artifacts). Run with the JS shell:

./js --fuzzing-safe poc.js

The PoC reaches the interrupt path entirely under --fuzzing-safe:

  1. A large Int32Array (128 MiB buffer) is created in a debuggee global so the native join loop runs long enough to be interrupted.
  2. A Debugger sets frame.onStep. While the native join is executing, the debuggee frame is parked at the ta.join(...) call-site bytecode, so the interrupt-driven onSingleStep re-enters the handler at the same offset repeatedly. The handler detects this (same offset twice in a row ⇒ inside the native) and calls ab.transfer() to detach the buffer mid-kernel.
  3. timeout(0.1, "true") schedules the watchdog interrupt. A string interrupt callback is allowed under --fuzzing-safe and returns true, so HandleInterrupt does not abort and instead runs the Debugger's onSingleStep hook while we are inside TypedArrayJoinKernel.
  4. After the detach, the next getIndex() reads through the freed/NULL data pointer and crashes.

Crash signature

Debug ASan build (js --fuzzing-safe poc.js), reliably:

Assertion failure: index < tarray->length().valueOr(0), at js/src/vm/TypedArrayObject.cpp:873
==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000
  #1 TypedArrayObjectTemplate<unsigned int>::getIndex(...)   TypedArrayObject.cpp:873
  #2 TypedArrayJoinKernel<int, int>(...)                     TypedArrayObject.cpp:2326
  #3 TypedArray_join(JSContext*, JS::CallArgs const&)        TypedArrayObject.cpp:2434
  #5 TypedArray_join(JSContext*, unsigned int, JS::Value*)   TypedArrayObject.cpp:2471

Opt ASan build:

js/src/vm/SharedMem.h:93:27: runtime error: applying non-zero offset 4899508 to null pointer
  #1 TypedArrayObjectTemplate<int>::getIndex(...)            TypedArrayObject.cpp:875
  #2 TypedArrayJoinKernel<int, int>(...)                     TypedArrayObject.cpp:2326
  #3 TypedArray_join(JSContext*, JS::CallArgs const&)        TypedArrayObject.cpp:2434

The register/offset values show the read uses a NULL base plus the loop index times the element size — a read at an attacker-influenced offset off the freed buffer.

Suggested fix

Inside TypedArrayJoinKernel, after CheckForInterrupt(cx) (which may run JS), re-acquire and re-clamp the length before each getIndex, e.g. break out of the loop when k >= tarray->length().valueOr(0) (treating the remaining indices as absent / empty per the spec) instead of trusting the pre-loop actualLength.

Notes on attack surface / severity

The underlying defect in TypedArrayJoinKernel is a real memory-safety issue (read after free / OOB read). However, the only demonstrated way to run JavaScript at the kernel's CheckForInterrupt point uses shell-only mechanisms: the SpiderMonkey Debugger's onStep/onSingleStep hook and the --fuzzing-safe-gated string interrupt callback (timeout(0.1, "true")). Neither of these is reachable from ordinary web content, and no content-JS path that runs script at this interrupt point has been demonstrated. Whether this is exploitable from the browser is therefore unproven; the bug is filed as sec-audit so triage can determine the real-world security impact. Severity intentionally left unset.

Found by mozscan automated analysis.

This looks like an interrupt-handling debugger loophole.

Flags: needinfo?(jdemooij)
Attachment #9590663 - Attachment mime type: application/javascript → text/plain

I think the key part of this loophole is this code in HandleInterrupt that invokes the Debugger's single step handler when the interrupt callback fires. It looks like this was added in bug 1016523 to assist with slow script debugging. One option would be to disable that path for --fuzzing-safe.

Severity: -- → S3
Component: JavaScript Engine → JavaScript: Debugger API
Priority: -- → P3
Flags: needinfo?(jdemooij)
Attached file (secure) —
Assignee: nobody → jdemooij
Status: NEW → ASSIGNED
Pushed by jdemooij@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/857927cdc60d https://hg.mozilla.org/integration/autoland/rev/2368b58c8d5c Don't invoke Debugger onStep handlers in HandleInterrupt in --fuzzing-safe mode. r=iain
Group: javascript-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 5 months ago
Flags: in-testsuite+
Resolution: --- → FIXED
Target Milestone: --- → 153 Branch
Whiteboard: [adv-main153-]
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: