[Debugger] Type confusion in obj_construct via JS_OPTIMIZED_OUT magic value escape from rematerialized Ion frame
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: bugmon, Assigned: iain)
References
(Blocks 2 open bugs)
Details
(4 keywords, Whiteboard: [bugmon:bisected,confirmed][prefs-checked][sec-auto-downgraded][adv-main151-])
Attachments
(3 files, 2 external files)
Type confusion in obj_construct via JS_OPTIMIZED_OUT magic value escape from rematerialized Ion frame
When the Debugger rematerializes an Ion frame whose strict-mode formal parameters were optimized out, the corresponding slots in the resulting UnmappedArgumentsObject contain MagicValue(JS_OPTIMIZED_OUT). ArgumentsObject::element() does not sanitize this magic value (it only checks for IsMagicScopeSlotValue, which tests magicUint32() > JS_WHY_MAGIC_COUNT — JS_OPTIMIZED_OUT is below this threshold). When code running inside frame.eval() reads arguments[i] and passes it as the |this| argument to Function.prototype.call, the magic value flows into vp[1] of the target native.
The engine invariant is that the only magic value permitted in vp[1] is JS_IS_CONSTRUCTING. Both isMagicNoReleaseCheck(JS_IS_CONSTRUCTING) (used by CallArgsFromVp) and CallArgs::isConstructing() encode this invariant by checking only isMagic() in release builds — they assume any magic |this| means construction. With JS_OPTIMIZED_OUT in vp[1], obj_construct's args.isConstructing() returns true and args.newTarget() reads argv_[argc_], which is one Value slot past the end of the InvokeArgs buffer (GenericArgsBase<NO_CONSTRUCT> allocates exactly 2+argc slots, no newTarget slot).
The OOB-read 8-byte Value is then passed to .toObject() and used as the newTarget JSObject* in CreateThis → GetPrototypeFromConstructor → GetProperty, where it is dereferenced to read its shape. In an ASAN build with assertions disabled, this manifests as a SEGV at JSObject::shape() with rdi=0xfffe000000000000 — the wild pointer derived from interpreting the OOB-read default-initialized Value as an object pointer.
Build Info
- Branch: main
- Revision: 98bf4b92d3f5d7a9855281df4bf333210bcfbbc4
- Timestamp: 2026-04-01T20:06:40+00:00
Affected Code
File: js/src/vm/ArgumentsObject-inl.h, line 18-27
inline const Value& ArgumentsObject::element(uint32_t i) const {
MOZ_ASSERT(isElement(i));
const Value& v = data()->args[i];
if (IsMagicScopeSlotValue(v)) { // Checks magicUint32() > JS_WHY_MAGIC_COUNT
CallObject& callobj =
getFixedSlot(MAYBE_CALL_SLOT).toObject().as<CallObject>();
return callobj.aliasedFormalFromArguments(v);
}
return v; // JS_OPTIMIZED_OUT magic returned unsanitized
}
File: js/public/CallArgs.h, line 163-182
bool isConstructing() const {
if (!argv_[-1].isMagic()) { // Checks ANY magic, not JS_IS_CONSTRUCTING specifically
return false;
}
// ... debug-only check ...
return true;
}
MutableHandleValue newTarget() const {
MOZ_ASSERT(constructing_);
return MutableHandleValue::fromMarkedLocation(&this->argv_[argc_]); // OOB if not actually constructing
}
File: js/public/Value.h, line 842-848
// Like isMagic, but without the release assertion.
bool isMagicNoReleaseCheck(JSWhyMagic why) const {
if (!isMagic()) {
return false;
}
MOZ_ASSERT(whyMagic() == why); // Debug-only canary; no-op in release
return true;
}
File: js/src/vm/Stack.h, line 871-891
bool init(JSContext* cx, uint64_t argc) {
// ...
// callee, this, arguments[, new.target iff constructing]
size_t len = 2 + argc + uint32_t(Construct); // NO_CONSTRUCT => no newTarget slot
MOZ_ASSERT(len > argc); // no overflow
if (!v_.resize(len)) {
return false;
}
// ...
}
File: js/src/builtin/Object.cpp, line 72-93
bool js::obj_construct(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
JSObject* obj;
if (args.isConstructing() && // true: vp[1] is magic (JS_OPTIMIZED_OUT)
(&args.newTarget().toObject() != &args.callee())) { // OOB read + .toObject() on garbage
RootedObject newTarget(cx, &args.newTarget().toObject());
obj = CreateThis(cx, newTarget); // wild JSObject* dereferenced inside
}
// ...
}
The root cause is a magic-value escape: JS_OPTIMIZED_OUT leaks from a rematerialized Ion frame through ArgumentsObject::element() (which only filters scope-slot magic, not optimized-out magic). The engine assumes that any magic value in vp[1] is JS_IS_CONSTRUCTING — this assumption is encoded in both isMagicNoReleaseCheck (via debug-only assert) and isConstructing (via tag-only check). When the wrong magic reaches vp[1], InvokeArgs's 2+argc-slot buffer is read one slot past its end as newTarget, and the resulting wild bits are dereferenced as a JSObject*.
Exploit Chain
- Lower JIT warmup thresholds and disable off-thread compilation to make Ion compilation deterministic.
- Define a strict-mode function g(d, x) where parameter x is unused. In strict mode, formals are not aliased by |arguments|, so Ion marks x's slot as NotObservable and emits MagicValue(JS_OPTIMIZED_OUT) for it in the snapshot.
- Warm up g until Ion-compiled, then call g(true, 42) to enter the Ion frame with x optimized out.
- Inside g, call into a cross-compartment helper that attaches a Debugger to the running global. The Debugger rematerializes the live Ion frame; the snapshot reader writes JS_OPTIMIZED_OUT into the rematerialized frame's slot for x.
- Call frame.eval('Object.call(arguments[1])'). The eval runs inside the debuggee scope; |arguments| resolves to an UnmappedArgumentsObject built from the rematerialized frame, so arguments[1] = MagicValue(JS_OPTIMIZED_OUT). UnmappedArgGetter → ArgumentsObject::element() returns this magic unfiltered.
- Function.prototype.call (fun_call) takes the magic as its first argument and passes it as |thisv| into js::Call, which writes it into vp[1] of an InvokeArgs<NO_CONSTRUCT> buffer sized at exactly 2+argc=2 slots.
- obj_construct reconstructs CallArgs from vp. args.isConstructing() checks vp[1].isMagic() → true. args.newTarget() reads argv_[0] = vp[2] — one Value slot (8 bytes) past the end of the RootedValueVector heap buffer.
- The OOB-read Value is .toObject()'d (NaN-unboxing yields a wild pointer) and passed to CreateThis → GetPrototypeFromConstructor → GetProperty, which dereferences it via JSObject::shape() → JSObject::getClass(). With heap grooming of the slot adjacent to the InvokeArgs buffer, an attacker could place a fake-object pointer there for arbitrary read or further confusion.
Steps to Reproduce
- Build SpiderMonkey shell (debug or ASAN). Both build types crash; debug crashes at the assertion canary, ASAN/release crashes at the downstream wild-pointer dereference.
- Save the testcase as poc.js (gczeal call may be removed for non-debug shells).
- Run: js --fuzzing-safe poc.js
- Debug build: 'Assertion failure: whyMagic() == why, at js/Value.h:846'.
- ASAN build: 'AddressSanitizer: SEGV ... in js::gc::HeaderWord::get()' with rdi=0xfffe000000000000, called from js::obj_construct → CreateThis → GetPrototypeFromConstructor.
Security Impact
- Severity: Moderate
- Attacker capability: Out-of-bounds heap read of one JS::Value (8 bytes) past an InvokeArgs buffer, immediately reinterpreted via .toObject() as a JSObject* and dereferenced (shape read, then virtual-dispatch property lookup). With heap grooming of adjacent memory in the RootedValueVector backing store, this could be leveraged into a controlled fake-object dereference for information disclosure or as a step toward arbitrary read/write. The same primitive is reachable through any native that calls args.newTarget() when args.isConstructing() is true (Object, Array, Map, Set, etc.).
- Preconditions: Requires the Debugger API to attach to a global containing a live Ion-compiled frame and invoke frame.eval(). In Firefox, Debugger is restricted to chrome/privileged code (DevTools, debugger-permission extensions); web content cannot construct a Debugger. The bug therefore does not directly enable a content-process sandbox escape — the attacker must already control privileged JavaScript. This precondition lowers severity from High to Moderate despite the memory-safety impact. However, the underlying invariant violation (JS_OPTIMIZED_OUT escaping a rematerialized frame into general value flow) is a hazard for any code path that materializes Ion frames.
ASAN Report
AddressSanitizer:DEADLYSIGNAL
=================================================================
==408940==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x561a021817b5 bp 0x7ffe649c3e90 sp 0x7ffe649c3e20 T0)
==408940==The signal is caused by a READ memory access.
==408940==Hint: this fault was caused by a dereference of a high value address (see register values below). Disassemble the provided pc to learn which register was used.
#0 0x561a021817b5 in js::gc::HeaderWord::get() const /firefox/js/src/gc/Cell.h:112:23
#1 0x561a021817b5 in js::gc::CellWithTenuredGCPointer<js::gc::Cell, js::Shape>::headerPtr() const /firefox/js/src/gc/Cell.h:845:60
#2 0x561a021817b5 in JSObject::shape() const /firefox/js/src/vm/JSObject.h:94:37
#3 0x561a021817b5 in JSObject::getClass() const /firefox/js/src/vm/JSObject.h:115:44
#4 0x561a021817b5 in JSObject::getOpsGetProperty() const /firefox/js/src/vm/JSObject.h:128:12
#5 0x561a021817b5 in js::GetProperty(JSContext*, JS::Handle<JSObject*>, JS::Handle<JS::Value>, JS::Handle<JS::PropertyKey>, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/ObjectOperations-inl.h:109:31
#6 0x561a021817b5 in js::GetProperty(JSContext*, JS::Handle<JSObject*>, JS::Handle<JS::Value>, js::PropertyName*, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/ObjectOperations-inl.h:120:10
#7 0x561a021817b5 in js::GetProperty(JSContext*, JS::Handle<JSObject*>, JS::Handle<JSObject*>, js::PropertyName*, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/ObjectOperations-inl.h:134:10
#8 0x561a04142d19 in js::GetPrototypeFromConstructor(JSContext*, JS::Handle<JSObject*>, JSProtoKey, JS::MutableHandle<JSObject*>) /firefox/js/src/vm/JSObject.cpp:816:8
#9 0x561a0243b112 in CreateThis(JSContext*, JS::Handle<JSObject*>) /firefox/js/src/builtin/Object.cpp:60:8
#10 0x561a0243b112 in js::obj_construct(JSContext*, unsigned int, JS::Value*) /firefox/js/src/builtin/Object.cpp:79:11
#11 0x561a03ca331a in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:490:13
#12 0x561a03ca331a in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:586:12
#13 0x561a03ca4d44 in InternalCall(JSContext*, js::AnyInvokeArgs const&, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:653:10
#14 0x561a03ca4d44 in js::Call(JSContext*, JS::Handle<JS::Value>, JS::Handle<JS::Value>, js::AnyInvokeArgs const&, JS::MutableHandle<JS::Value>, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:685:8
#15 0x561a04109324 in js::fun_call(JSContext*, unsigned int, JS::Value*) /firefox/js/src/vm/JSFunction.cpp:1105:10
#16 0x561a03ca331a in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:490:13
#17 0x561a03ca331a in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:586:12
#18 0x561a03cbfb41 in InternalCall(JSContext*, js::AnyInvokeArgs const&, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:653:10
#19 0x561a03cbfb41 in js::CallFromStack(JSContext*, JS::CallArgs const&, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:658:10
#20 0x561a03cbfb41 in js::Interpret(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:3272:16
#21 0x561a03ca2206 in MaybeEnterInterpreterTrampoline(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:384:10
#22 0x561a03ca2206 in js::RunScript(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:460:13
#23 0x561a03ca6e47 in js::ExecuteKernel(JSContext*, JS::Handle<JSScript*>, JS::Handle<JSObject*>, js::AbstractFramePtr, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/Interpreter.cpp:850:10
#24 0x561a02694ce6 in EvaluateInEnv(JSContext*, JS::Handle<JSObject*>, js::AbstractFramePtr, mozilla::Range<char16_t const>, js::EvalOptions const&, JS::Handle<JS::StackGCVector<JS::PropertyKey, js::TempAllocPolicy>>, JS::Handle<JS::StackGCVector<JS::Value, js::TempAllocPolicy>>, JS::MutableHandle<JS::Value>) /firefox/js/src/debugger/Frame.cpp:1116:10
#25 0x561a02694ce6 in js::DebuggerGenericEval(JSContext*, mozilla::Range<char16_t const>, JS::Handle<JSObject*>, js::EvalOptions const&, js::Debugger*, JS::Handle<JSObject*>, js::FrameIter*) /firefox/js/src/debugger/Frame.cpp:1207:13
#26 0x561a02695f29 in js::DebuggerFrame::eval(JSContext*, JS::Handle<js::DebuggerFrame*>, mozilla::Range<char16_t const>, JS::Handle<JSObject*>, js::EvalOptions const&) /firefox/js/src/debugger/Frame.cpp:1228:10
#27 0x561a0269c363 in js::DebuggerFrame::CallData::evalMethod() /firefox/js/src/debugger/Frame.cpp:1977:3
#28 0x561a026c524e in bool js::DebuggerFrame::CallData::ToNative<&js::DebuggerFrame::CallData::evalMethod()>(JSContext*, unsigned int, JS::Value*) /firefox/js/src/debugger/Frame.cpp:1432:10
#29 0x561a03ca331a in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:490:13
#30 0x561a03ca331a in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:586:12
#31 0x561a02f35bfb in js::jit::DoCallFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, unsigned int, JS::Value*, JS::MutableHandle<JS::Value>) /firefox/js/src/jit/BaselineIC.cpp:1701:10
#32 0x1adc45e26823 (<unknown module>)
==408940==Register values:
rax = 0x1fffc00000000000 rbx = 0x00007ffe649c3e20 rcx = 0x00000e8ae9aa0920 rdx = 0x000074574d504870
rdi = 0xfffe000000000000 rsi = 0x000074574d504870 rbp = 0x00007ffe649c3e90 rsp = 0x00007ffe649c3e20
r8 = 0x000074574d4c8ab0 r9 = 0x000076374e5e0c80 r10 = 0x000074574d504900 r11 = 0x00000fffcc938830
r12 = 0x000074574d504920 r13 = 0x000074574d504900 r14 = 0x00000e8ae9aa0920 r15 = 0x000074574d504960
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /firefox/js/src/gc/Cell.h:112:23 in js::gc::HeaderWord::get() const
==408940==ABORTING
| Reporter | ||
Comment 1•7 months ago
|
||
| Reporter | ||
Comment 2•7 months ago
|
||
Updated•7 months ago
|
Updated•7 months ago
|
| Reporter | ||
Comment 4•7 months ago
|
||
Verified bug as reproducible on mozilla-central 20260405210219-4e8e90ddd4ee.
Unable to bisect testcase (Testcase reproduces on start build!):
Start: b9e21434cf454a178ce807ed2fd2b891937aea7c (20260107160934)
End: f66c987893d12eb5ce718fbc669e66e68a29ed8a (20260404202145)
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)
Updated•7 months ago
|
Updated•7 months ago
|
Comment 5•7 months ago
|
||
Prefs check: the testcase only sets shell-only setJitCompilerOption knobs (ion/baseline warmup, offthread-compilation) to make Ion compilation deterministic. None of these are Firefox prefs and none gate the vulnerability itself — tagging [prefs-checked].
Sec rating: exploiting this requires new Debugger() (to attach to the debuggee and call frame.eval), which is only reachable from chrome/privileged JavaScript. Web content cannot construct a Debugger. This matches the "requires attacker-controlled ChromeJS in the process being attacked" case in our triage rules, so downgrading to sec-other with [sec-auto-downgraded] and removing the csectype-* keywords. The crash is still a real correctness bug and I'm attaching a fix.
Fix: sanitize magic values in CopyFrameArgs::copyActualArgs (used by createMissingArguments → createUnexpected(AbstractFramePtr) for the off-stack Debugger path) and CopyScriptFrameIterArgs::init. Ion snapshots can yield MagicValue(JS_OPTIMIZED_OUT) for elided formals in a rematerialized frame; these must never be stored in ArgumentsObject element slots, since downstream consumers like CallArgs::isConstructing treat any magic |this| as JS_IS_CONSTRUCTING and then read an OOB newTarget. Normalizing to undefined at the copy boundary closes the escape for both paths without scrubbing the slots on RematerializedFrame itself (which the Debugger.Frame.arguments getter still needs to report "optimized out"). Verified: POC crashes on clean main, no crash after the patch.
This is an automated analysis result. If this result is incorrect please add a needinfo and feel free to correct the error.
Comment 6•7 months ago
|
||
Comment 7•7 months ago
|
||
| Assignee | ||
Comment 8•7 months ago
|
||
The most interesting part of this bug to me is the mechanism it uses for exploiting a leaked magic value. We made a deliberate decision in bug 1859832 to avoid the release assert here, but it does allow for shenanigans.
I think the explanation for why the argument is not marked as observable is that this code says it should: it's a strict function, and it doesn't use arguments/rest, so we don't think the argument can be observed. This is consistent with other places where we return OPTIMIZED_OUT for values that are only observable using the debugger. In those cases, we sanitize the OPTIMIZED_OUT before letting it escape. I don't have a strong opinion about where we should do that sanitizing here, but it does seem like some form of sanitization is the right answer.
Comment 9•7 months ago
|
||
I guess, if we do not care about the correctness of the recovery, then the sanitization would be ok.
However if we do care we would have to change the SlotObservableKind::NotObservable to SlotObservableKind::ObservableRecoverable.
| Assignee | ||
Comment 10•7 months ago
|
||
My understanding is that, to a first approximation, the debugger can always see everything. We've made the decision that it's okay for things that only the debugger can see to be optimized away, and to report that to the user. We could revisit that decision, but it would make performance slightly worse, and I haven't seen a lot of complaints about it. (I don't know how much worse; we could measure.)
Basically I think this comes down to the fact that in a function like function f(unusedArg) { ...}, if you use the debugger to eval unusedArg in an Ion frame, it will throw Error: variable 'x' has been optimized out, and we should probably ensure that arguments[0] does the same thing.
I was briefly concerned that we could have a similar problem with Function.arguments, but that's already covered by the strict mode check.
So I think that somewhere in this stack, we should check for magic values and ... throw? Debugger::wrapDebuggeeValue does what we want, but we don't have a debugger easily available here.
#0 js::ArgumentsObject::create<CopyFrameArgs>
#1 0x0000567006082587 in js::ArgumentsObject::createUnexpected
#2 0x00005670063e58b2 in (anonymous namespace)::DebugEnvironmentProxyHandler::createMissingArguments
#3 0x00005670063e5d71 in (anonymous namespace)::DebugEnvironmentProxyHandler::getMissingArguments
I guess we already call reportOptimizedOut in DebugEnvironmentProxyHandler here, so maybe that's the right place for the check.
| Assignee | ||
Comment 11•7 months ago
|
||
Updated•7 months ago
|
| Assignee | ||
Comment 12•7 months ago
|
||
Comment 13•7 months ago
|
||
Comment 14•7 months ago
|
||
Comment 15•7 months ago
|
||
js/src/jit/shared/CodeGenerator-shared.h:454:17: error: static assertion failed due to requirement 'std::is_void_v<bool>': LambdaOutOfLineCode lambda must return void; use masm.setOOM() to report failure
| Assignee | ||
Comment 16•7 months ago
|
||
I was initially confused when investigating this because the wrong error message was used in comment 15. That's the failure caused by bug 2023217, which was backed out just after this patch landed.
The actual problem is a missing #include of ArgumentsObject-inl.h in EnvironmentObject.cpp.
Comment 17•7 months ago
|
||
Comment 18•7 months ago
|
||
| Reporter | ||
Comment 19•7 months ago
|
||
Testcase crashes using the initial build (mozilla-central 20260404202145-f66c987893d1) but not with tip (mozilla-central 20260411091926-496be9d3e8d5.)
The bug appears to have been fixed in the following build range:
Start: 6afabc1130bf53bf1f140c55bb19929c3ef0e68e (20260410220039)
End: 9c108a4126cf9024ecfc534a13c6b6d098437a56 (20260410220819)
Pushlog: https://hg.mozilla.org/integration/autoland/pushloghtml?fromchange=6afabc1130bf53bf1f140c55bb19929c3ef0e68e&tochange=9c108a4126cf9024ecfc534a13c6b6d098437a56
iain, 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
|
| Assignee | ||
Comment 20•7 months ago
|
||
I confirm that the fix landed in the linked range.
Updated•7 months ago
|
Updated•6 months ago
|
Updated•6 months ago
|
Updated•6 months ago
|
Updated•2 months ago
|
Updated•2 months ago
|
Comment 21•2 months ago
|
||
Comment 22•2 months ago
|
||
| bugherder | ||
Description
•