TOCTOU in [@ Deserialize] allows synthetic clone data to be parsed with SameProcess scope, creating ArrayBuffer at arbitrary address
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
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
- Script calls
serialize({x: 1})to obtain a legitimate (non-synthetic)CloneBufferObject. - Script crafts a 56-byte
ArrayBufferencoding a structured-clone stream withSCTAG_HEADER,SCTAG_TRANSFER_MAP_HEADER, oneSCTAG_TRANSFER_MAP_ARRAY_BUFFERentry carrying ownershipSCTAG_TMO_ALLOC_DATA, raw pointer0x4141414141414141, length0x100, and anSCTAG_NULLterminator. - Script calls
deserialize(cb, opts)whereoptshas aSharedArrayBufferaccessor. Deserializereadsobj->isSynthetic()→false→ setsscope = SameProcess.JS_GetProperty(cx, opts, "SharedArrayBuffer", &v)triggers the getter.- Getter executes
cb.clonebuffer = ab→setCloneBuffer_implswaps in the crafted bytes and marks the buffer synthetic. - Control returns to
Deserialize, which callsJS_ReadStructuredCloneon the crafted bytes withSameProcessscope. readTransferMapseesSCTAG_TRANSFER_MAP_ARRAY_BUFFER, skips theDifferentProcessrejection, reads raw pointer0x4141414141414141viain.readPtr(&content).JS::NewArrayBufferWithContents(cx, 0x100, 0x4141414141414141, ...)creates an ArrayBuffer whose data pointer is attacker-controlled.- 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
ArrayBufferbacked 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.
| Reporter | ||
Comment 1•7 months ago
|
||
| Reporter | ||
Comment 2•7 months ago
|
||
Updated•7 months ago
|
Updated•7 months ago
|
Comment 4•7 months ago
|
||
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 onlyjs/src/builtin/Object.cpp:2442— gated by#ifdef FUZZING(compile-time), not present in release/beta/ESRCu.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.
| Reporter | ||
Comment 5•7 months ago
|
||
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)
Comment 6•7 months ago
|
||
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.
| Reporter | ||
Comment 7•7 months ago
|
||
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.
Updated•7 months ago
|
Comment 8•7 months ago
|
||
This was fixed in bug 2029299, as confirmed by bugmon's bisection fix range.
Updated•7 months ago
|
Updated•2 months ago
|
Description
•