Closed Bug 2027358 Opened 7 months ago Closed 7 months ago

TOCTOU in [@ Deserialize] allows synthetic clone data to be parsed with SameProcess scope, creating ArrayBuffer at arbitrary address

Categories

(Core :: JavaScript Engine, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2029299

People

(Reporter: bugmon, Unassigned)

Details

(4 keywords, Whiteboard: [prefs-checked][sec-auto-downgraded][bugmon:bisected,confirmed])

Attachments

(2 files)

Summary

A time-of-check-to-time-of-use vulnerability exists in the Deserialize testing function in js/src/builtin/TestingFunctions.cpp:6336. The function computes the deserialization scope based on whether the clone buffer is synthetic before invoking user-controlled getters on the options object. A malicious getter can replace the clone buffer's backing data with attacker-crafted bytes via the clonebuffer setter, causing synthetic (untrusted) data to be deserialized with SameProcess scope. This bypasses the safety check in JSStructuredCloneReader::readTransferMap and results in JS::NewArrayBufferWithContents being called with an attacker-controlled raw pointer, yielding an ArrayBuffer backed by arbitrary memory and an arbitrary free() during GC finalization.

Affected Code

File: js/src/builtin/TestingFunctions.cpp, line 6336

JS::StructuredCloneScope scope =
    obj->isSynthetic() ? JS::StructuredCloneScope::DifferentProcess
                       : JS::StructuredCloneScope::SameProcess;
if (args.get(1).isObject()) {
  RootedObject opts(cx, &args[1].toObject());
  ...
  RootedValue v(cx);
  if (!JS_GetProperty(cx, opts, "SharedArrayBuffer", &v)) {  // <-- runs user JS
    return false;
  }
  ...
  if (!JS_GetProperty(cx, opts, "scope", &v)) {  // <-- runs user JS
    return false;
  }
  ...
}
...
if (!JS_ReadStructuredClone(cx, *obj->data(), JS_STRUCTURED_CLONE_VERSION,
                            scope, &deserialized, policy, &gCloneCallbacks,
                            nullptr)) {

The scope variable is latched from obj->isSynthetic() at line 6336-6338. The subsequent JS_GetProperty calls on lines 6346 and 6371 can invoke arbitrary JS accessors. Such an accessor can invoke CloneBufferObject::setCloneBuffer_impl via cb.clonebuffer = arraybuffer, which calls obj->setData(buf.release(), true) — marking the buffer synthetic and replacing its contents. However, the already-computed scope still reads SameProcess, so the now-untrusted bytes are fed to JS_ReadStructuredClone with full trust.

In JSStructuredCloneReader::readTransferMap, the check that should reject raw-pointer transferables is skipped:

if (tag == SCTAG_TRANSFER_MAP_ARRAY_BUFFER) {
  if (allowedScope == JS::StructuredCloneScope::DifferentProcess) {
    // Rejected — but scope is SameProcess, so this branch is NOT taken
    ReportDataCloneError(cx, callbacks, JS_SCERR_TRANSFERABLE, closure);
    return false;
  }
  ...
  if (data == JS::SCTAG_TMO_ALLOC_DATA) {
    obj = JS::NewArrayBufferWithContents(
        cx, nbytes, content,  // <-- content = 0x4141414141414141
        JS::NewArrayBufferOutOfMemory::CallerMustFreeMemory);
  }

Correct Pattern

Re-check isSynthetic() after all user JS has had a chance to run:

// Process options first...
if (args.get(1).isObject()) {
  ... // JS_GetProperty calls here
}

// Compute scope AFTER all user JS has executed
JS::StructuredCloneScope scope =
    obj->isSynthetic() ? JS::StructuredCloneScope::DifferentProcess
                       : JS::StructuredCloneScope::SameProcess;

// Apply user-requested scope widening only if safe
if (userScope) {
  if (*userScope < scope) {
    JS_ReportErrorASCII(cx, "Cannot use less restrictive scope...");
    return false;
  }
  scope = *userScope;
}

Exploit Chain

  1. Script calls serialize({x: 1}) to obtain a legitimate (non-synthetic) CloneBufferObject.
  2. Script crafts a 56-byte ArrayBuffer encoding a structured-clone stream with SCTAG_HEADER, SCTAG_TRANSFER_MAP_HEADER, one SCTAG_TRANSFER_MAP_ARRAY_BUFFER entry carrying ownership SCTAG_TMO_ALLOC_DATA, raw pointer 0x4141414141414141, length 0x100, and an SCTAG_NULL terminator.
  3. Script calls deserialize(cb, opts) where opts has a SharedArrayBuffer accessor.
  4. Deserialize reads obj->isSynthetic() → false → sets scope = SameProcess.
  5. JS_GetProperty(cx, opts, "SharedArrayBuffer", &v) triggers the getter.
  6. Getter executes cb.clonebuffer = ab → setCloneBuffer_impl swaps in the crafted bytes and marks the buffer synthetic.
  7. Control returns to Deserialize, which calls JS_ReadStructuredClone on the crafted bytes with SameProcess scope.
  8. readTransferMap sees SCTAG_TRANSFER_MAP_ARRAY_BUFFER, skips the DifferentProcess rejection, reads raw pointer 0x4141414141414141 via in.readPtr(&content).
  9. JS::NewArrayBufferWithContents(cx, 0x100, 0x4141414141414141, ...) creates an ArrayBuffer whose data pointer is attacker-controlled.
  10. During GC finalization, the ArrayBuffer's finalizer calls free(0x4141414141414141) → crash with attacker-controlled registers.

Security Impact

Severity: High (shell-only testing function, but fuzzing-safe accessible)

Attacker capability:

  • Create an ArrayBuffer backed by an arbitrary memory address → arbitrary read/write primitive over the process address space.
  • Trigger free() on an arbitrary pointer during GC → heap corruption / exploitable free.

Preconditions: Access to the JS shell deserialize / serialize testing functions. These are shell-only and not exposed to web content, but are enabled under --fuzzing-safe and therefore constitute valid fuzz-blocker / shell-hardening bugs.

ASAN Report

AddressSanitizer:DEADLYSIGNAL
=================================================================
==53138==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x5771c04178b7 bp 0x000000000001 sp 0x7fff0c2c1030 T0)
==53138==The signal is caused by a READ memory access.
==53138==Hint: this fault was caused by a dereference of a high value address (see register values below).
    #0 0x5771c04178b7 in atomic_compare_exchange_strong<__sanitizer::atomic_uint8_t> compiler-rt/lib/asan/../sanitizer_common/sanitizer_atomic_clang.h:84:10
    #1 0x5771c04178b7 in AtomicallySetQuarantineFlagIfAllocated compiler-rt/lib/asan/asan_allocator.cpp:674:10
    #2 0x5771c04178b7 in __asan::Allocator::Deallocate compiler-rt/lib/asan/asan_allocator.cpp:738:10
    #3 0x5771c04b4c1b in free compiler-rt/lib/asan/asan_malloc_linux.cpp:52:3
    #4 0x5771c114a751 in JSClass::doFinalize(JS::GCContext*, JSObject*) const dist/include/js/Class.h:663:5
    #5 0x5771c114a751 in JSObject::finalize(JS::GCContext*) js/src/vm/JSObject-inl.h:90:10
    #6 0x5771c114a751 in js::gc::Arena::finalize<JSObject, (js::gc::FinalizeKind)2> js/src/gc/Sweeping.cpp:163:12
    ...
    #23 0x5771c21f6015 in JSRuntime::destroyRuntime() js/src/vm/Runtime.cpp:256:8
    #24 0x5771c250a4fc in js::DestroyContext(JSContext*) js/src/vm/JSContext.cpp:234:7
    #25 0x5771c050a7f4 in main js/src/shell/js.cpp:12763:1

==53138==Register values:
rdi = 0x4141414141414141  rsi = 0x4141414141414141  r13 = 0x4141414141414131  r14 = 0x4141414141414141
SUMMARY: AddressSanitizer: SEGV dist/include/js/Class.h:663:5 in JSClass::doFinalize(JS::GCContext*, JSObject*) const
==53138==ABORTING

Suggested Fix

Move the isSynthetic() check to after all option-object property reads, or re-validate before calling JS_ReadStructuredClone:

// In Deserialize, after processing all options:
if (obj->isSynthetic() && scope < JS::StructuredCloneScope::DifferentProcess) {
  scope = JS::StructuredCloneScope::DifferentProcess;
}

Alternatively, compute scope from the JSStructuredCloneData's own stored scope rather than the wrapper object's flag, since setCloneBuffer_impl already creates the data with DifferentProcess scope.

Attached file testcase.js —
Attached file crash_stack.txt —
Group: core-security → dom-core-security
Whiteboard: [prefs-checked][sec-auto-downgraded]

Downgrading sec-high → sec-other.

deserialize() is a testing function that is not reachable from web content in any shipping build:

  • js/src/shell/js.cpp — JS shell binary only
  • js/src/builtin/Object.cpp:2442 — gated by #ifdef FUZZING (compile-time), not present in release/beta/ESR
  • Cu.getJSTestingFunctions() (XPCComponents.cpp) — requires Chrome JS privileges

The only browser path requires attacker-controlled Chrome JS in the same process, at which point the attacker already has everything this primitive would give them. No about:config pref exposes these functions to content.

Still worth fixing as a shell-hardening / fuzz-blocker, just not a shipping security bug.

This is an automated analysis result. If this result is incorrect please add a needinfo and feel free to correct the error.

Verified bug as reproducible on mozilla-central 20260329205327-086d14f42154.
Unable to bisect testcase (Testcase reproduces on start build!):

Start: 810549a5947f5ae5d8cf1f11c15c841e1a6dc64c (20251231225543)
End: 8e941630524a230f9342db0dad8061dcabcbad12 (20260328200338)
BuildFlags: BuildFlags(asan=True, tsan=False, debug=False, fuzzing=True, coverage=False, valgrind=False, no_opt=False, fuzzilli=False, nyx=False, searchfox=False, afl=False)

Whiteboard: [prefs-checked][sec-auto-downgraded] → [prefs-checked][sec-auto-downgraded][bugmon:bisected,confirmed]

I think this can be triaged to S4 given comment #4, but I also think I'm not the right triage owner to decide if that's correct.

Component: IPC → JavaScript Engine

Testcase crashes using the initial build (mozilla-central 20260328200338-8e941630524a) but not with tip (mozilla-central 20260410211335-be5d6cb74c0a.)

The bug appears to have been fixed in the following build range:

Start: 6bed8a4e5e54f320d8b3245d460515bb4f874f83 (20260410063520)
End: 543680059c158e8f66bd842ea2747bf5ebe85937 (20260410080634)
Pushlog: https://hg.mozilla.org/integration/autoland/pushloghtml?fromchange=6bed8a4e5e54f320d8b3245d460515bb4f874f83&tochange=543680059c158e8f66bd842ea2747bf5ebe85937

bugmon, can you confirm that the above bisection range is responsible for fixing this issue?
Removing bugmon keyword as no further action possible. Please review the bug and re-add the keyword for further analysis.

Flags: needinfo?(bugmon)
Keywords: bugmon
Group: dom-core-security → javascript-core-security

This was fixed in bug 2029299, as confirmed by bugmon's bisection fix range.

Status: UNCONFIRMED → RESOLVED
Closed: 7 months ago
Duplicate of bug: 2029299
Resolution: --- → DUPLICATE
Flags: needinfo?(bugmon)
Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: