Closed Bug 2050681 Opened 4 months ago Closed 4 months ago

Forged JS::Value via uncanonicalized float NaN in XPCConvert::NativeData2JS — content→parent sandbox escape

Categories

(Core :: XPConnect, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2050657

People

(Reporter: bugmon, Unassigned)

References

Details

(6 keywords)

Attachments

(5 files)

XPCConvert::NativeData2JS handles nsXPTType::T_FLOAT by calling d.setNumber(*static_cast<const float*>(s)), which routes through JS::Value::setNumber(float) → setDouble(double(f)) → bitsFromDouble(). On every supported tier-1 platform (JS_NONCANONICAL_HARDWARE_NAN is only defined on SPARC), bitsFromDouble stores the raw IEEE-754 bit pattern with no NaN canonicalization. The sibling T_DOUBLE case correctly uses JS_NumberValue, which canonicalizes; only T_FLOAT was missed.

A compromised content process can deliver an arbitrary 32-bit float bit pattern to the parent over IPC: PContent::SynchronizeLayoutHistoryState carries an nsILayoutHistoryState whose PresState.resolution is a raw float. The parent stores it on the active SessionHistoryEntry. A subsequent PSessionStore::SessionStoreUpdate(aNeedCollectSHistory=true) causes parent-process chrome JS (SessionHistory.sys.mjs::_getSerializablePresState) to call the XPCOM method nsILayoutHistoryState::getPresState(key, out float x, out float y, out bool, out float res). XPConnect's GatherAndConvertResults then runs NativeData2JS(T_FLOAT) on the attacker's float and passes the resulting JS::Value to JS_SetPropertyById.

A negative quiet-NaN float such as 0xFFF04141 (sign=1, exp=0xFF, mantissa top-5 = 11100) widens via cvtss2sd to the 64-bit pattern 0xFFFE082820000000. Under PUNBOX64 the top-17 bits 0x1FFFC equal JSVAL_TAG_OBJECT and the low-47 payload 0x82820000000 becomes a forged JSObject*. The attacker controls the low 18 bits of the float mantissa, giving a forged pointer of the form (m & 0x3FFFF) << 29 — any 512 MiB-aligned user-space address up to 0x7FFFE0000000 — and by varying the top-5 mantissa bits can also forge String/Symbol/BigInt tags. This is a full fakeobj primitive in the privileged parent process: with a heap spray it yields arbitrary read/write and a complete sandbox escape.

Build Info

Affected Code

File: js/xpconnect/src/XPCConvert.cpp, line 100-105

case nsXPTType::T_FLOAT:
  d.setNumber(*static_cast<const float*>(s));   // BUG: not canonicalized
  return true;
case nsXPTType::T_DOUBLE:
  d.set(JS_NumberValue(*static_cast<const double*>(s)));  // safe: JS_NumberValue canonicalizes
  return true;

File: js/public/Value.h, line 645-653

void setNumber(float f) {
  int32_t i;
  if (mozilla::NumberIsInt32(f, &i)) {
    setInt32(i);
    return;
  }
  setDouble(double(f));   // cvtss2sd preserves NaN sign + payload
}

File: js/public/Value.h, line 511-516

static uint64_t bitsFromDouble(double d) {
#if defined(JS_NONCANONICAL_HARDWARE_NAN)   // only defined on __sparc__
  d = CanonicalizeNaN(d);
#endif
  return mozilla::BitwiseCast<uint64_t>(d); // raw bits stored as JS::Value
}

File: layout/base/nsILayoutHistoryState.idl, line 48-51

void getPresState(in ACString aKey,
                  out float aScrollX, out float aScrollY,
                  out boolean aAllowScrollOriginDowngrade,
                  out float aRes);   // T_FLOAT out-params reflected via NativeData2JS

File: toolkit/modules/sessionstore/SessionHistory.sys.mjs, line 306-324

_getSerializablePresState(layoutHistoryState, stateKey) {
  let presState = { stateKey };
  let x = {}, y = {}, scrollOriginDowngrade = {}, res = {};
  layoutHistoryState.getPresState(stateKey, x, y, scrollOriginDowngrade, res);
  // XPConnect writes the forged Object-tagged Value into res.value here
  ...
}

File: js/xpconnect/src/XPCWrappedNative.cpp, line 1321-1336

if (!XPCConvert::NativeData2JS(mCallContext, &v, &dp->val, type, &param_iid,
                               array_count, &err)) { ... }
...
RootedObject obj(mCallContext, &mArgv[i].toObject());
if (!JS_SetPropertyById(mCallContext, obj, mIdxValueId, v)) {  // dereferences forged JSObject*

T_FLOAT path stores the raw float→double bit pattern into a JS::Value with no NaN canonicalization, allowing the upper 17 bits to collide with JSVAL_TAG_OBJECT and forge a GC-thing pointer.

Exploit Chain

  1. Attacker achieves arbitrary code execution in a sandboxed content process (precondition).
  2. Content crafts a PresState with resolution set to the raw float bit pattern 0xFFF04141 (sign=1, exp=all-ones, mantissa top-5=11100, low-18 bits encode the target address >> 29).
  3. Content sends PContent::SynchronizeLayoutHistoryState(bc, layoutHistoryState) carrying that PresState; ContentParent::RecvSynchronizeLayoutHistoryState stores it on the active SessionHistoryEntry of the BrowsingContext in the parent.
  4. Content sends PSessionStore::SessionStoreUpdate with aNeedCollectSHistory=true.
  5. Parent SessionStoreParent::RecvSessionStoreUpdate → nsISessionStoreFunctions::UpdateSessionStore → SessionStore.sys.mjs → SessionHistory.sys.mjs::collectFromParent → serializeEntry → _getSerializablePresState calls XPCOM nsILayoutHistoryState::getPresState(key, out float x, out float y, out bool, out float res).
  6. nsLayoutHistoryState::GetPresState writes the attacker's float into *aRes verbatim.
  7. CallMethodHelper::GatherAndConvertResults invokes XPCConvert::NativeData2JS(T_FLOAT) → d.setNumber(float) → setDouble(double(f)) → bitsFromDouble stores 0xFFFE082820000000 as the raw JS::Value bits — tag 0x1FFFC = JSVAL_TAG_OBJECT, payload 0x82820000000.
  8. JS_SetPropertyById(cx, outParamObj, "value", forgedValue) treats the value as a JSObject* and dereferences it (compartment check / shape read / post-barrier), giving a wild read at the attacker-chosen 512 MiB-aligned address.
  9. With a parent-process heap spray placing a fake JSObject/Shape at that address (e.g. via large IPC-backed allocations), the attacker obtains a fakeobj primitive in chrome-privileged JS, escalating to arbitrary read/write and full sandbox escape.

IPC Path

  • Install malicious float in parent SH

    • Protocol: PContent
    • Parent process: Main
    • Child process: Content
    • Message flow: Content -> Main: SynchronizeLayoutHistoryState(bc, nsILayoutHistoryState{PresState.resolution=0xFFF04141})
  • Trigger parent-side SH collection

    • Protocol: PSessionStore
    • Parent process: Main
    • Child process: Content
    • Message flow: Content -> Main: SessionStoreUpdate(aNeedCollectSHistory=true)

Steps to Reproduce

  1. Build Firefox with /app/mozconfigs/mozconfig.linux.asan.fuzzing on x86-64 Linux.
  2. Apply exploit.patch (guarded by XRE_IsContentProcess()): it overrides ParamTraits<nsILayoutHistoryState*>::Write in the content process to serialize a PresState whose resolution float has bit pattern 0xFFF04141, and makes SessionStoreChild::SessionStoreUpdate first send PContent::SynchronizeLayoutHistoryState then PSessionStore::SessionStoreUpdate with aNeedCollectSHistory=true.
  3. Rebuild Firefox.
  4. Set prefs: browser.sessionstore.debug.no_auto_updates=true, browser.sessionstore.interval=100, fission.sessionHistoryInParent=true (timing/reliability only — SHIP is default).
  5. Load testcase.html, which repeatedly calls history.pushState/scrollTo/document.title to drive SessionStore flushes.
  6. Observe the parent process crash (crashed_parent=true) with ASAN SEGV READ at a high address; rax=0x0000082820000000 is the forged JSObject* and the stack tops out in JSObject::shape() under JS_SetPropertyById ← CallMethodHelper::GatherAndConvertResults ← XPCWrappedNative::CallMethod.

Security Impact

  • Severity: High
  • Attacker capability: A compromised content process can place an arbitrary-bit-pattern float into a PresState delivered to the parent over existing, default-enabled IPC. When parent chrome JS reflects that float via XPConnect, the missing NaN canonicalization in the T_FLOAT path yields a JS::Value whose tag is JSVAL_TAG_OBJECT (or String/Symbol/BigInt) and whose 47-bit payload is (float_mantissa & 0x3FFFF) << 29 — a forged GC-cell pointer at any attacker-chosen 512 MiB-aligned user-mode address. This is a fakeobj primitive in the privileged parent process; combined with a heap spray it gives arbitrary read/write in the parent and therefore a full sandbox escape to the user's OS privileges.
  • Preconditions: Renderer (content-process) code execution is required to craft the IPC payloads. No non-default prefs or user interaction are required; Session-History-In-Parent and the SynchronizeLayoutHistoryState / SessionStoreUpdate IPC paths are enabled in shipping Firefox. Affects PUNBOX64 platforms where JS_NONCANONICAL_HARDWARE_NAN is not defined (all tier-1: x86-64, ARM64).

ASAN Report

=================================================================
==14926==ERROR: AddressSanitizer: SEGV on unknown address 0x010583ff8000 (pc 0x76919ec82e6c bp 0x7ffcc0386ad0 sp 0x7ffcc0386ac0 T0)
==14926==The signal is caused by a READ memory access.
    #0 0x76919ec82e6c in js::gc::HeaderWord::get() const /firefox/js/src/gc/Cell.h:113:23
    #1 0x76919ec82e6c in js::gc::CellWithTenuredGCPointer<js::gc::Cell, js::Shape>::headerPtr() const /firefox/js/src/gc/Cell.h:846:60
    #2 0x76919ec82e6c in JSObject::shape() const /firefox/js/src/vm/JSObject.h:92:37
    #3 0x76919ec82e6c in JSObject::compartment() const /firefox/js/src/vm/JSObject.h:144:49
    #4 0x76919ec82e6c in js::ContextChecks::check(JSObject*, int) /firefox/js/src/vm/JSContext-inl.h:82:18
    #5 0x76919ec82e6c in js::ContextChecks::check(JS::Value const&, int) /firefox/js/src/vm/JSContext-inl.h:132:7
    #6 0x7691a0794c09 in void JSContext::checkImpl<JS::Handle<JSObject*>, JS::Handle<JS::PropertyKey>, JS::Handle<JS::Value>>(JS::Handle<JSObject*> const&, JS::Handle<JS::PropertyKey> const&, JS::Handle<JS::Value> const&) /firefox/js/src/vm/JSContext-inl.h:214:33
    #7 0x7691a076e9ab in void JSContext::check<JS::Handle<JSObject*>, JS::Handle<JS::PropertyKey>, JS::Handle<JS::Value>>(JS::Handle<JSObject*> const&, JS::Handle<JS::PropertyKey> const&, JS::Handle<JS::Value> const&) /firefox/js/src/vm/JSContext-inl.h:221:5
    #8 0x7691a076e9ab in JS_SetPropertyById(JSContext*, JS::Handle<JSObject*>, JS::Handle<JS::PropertyKey>, JS::Handle<JS::Value>) /firefox/obj-firefox-asan/js/src/vm/./../../../../js/src/vm/PropertyAndElement.cpp:664:7
    #9 0x769190f1931c in CallMethodHelper::GatherAndConvertResults() /firefox/obj-firefox-asan/js/xpconnect/src/./../../../../js/xpconnect/src/XPCWrappedNative.cpp:1333:12
    #10 0x769190f1931c in CallMethodHelper::Call() /firefox/obj-firefox-asan/js/xpconnect/src/./../../../../js/xpconnect/src/XPCWrappedNative.cpp:1182:10
    #11 0x769190f1931c in XPCWrappedNative::CallMethod(XPCCallContext&, XPCWrappedNative::CallMode) /firefox/obj-firefox-asan/js/xpconnect/src/./../../../../js/xpconnect/src/XPCWrappedNative.cpp:1117:23
    #12 0x769190f1d083 in XPC_WN_CallMethod(JSContext*, unsigned int, JS::Value*) /firefox/obj-firefox-asan/js/xpconnect/src/./../../../../js/xpconnect/src/XPCWrappedNativeJSOps.cpp:960:10
    #13 0x7691a066157a in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:498:13
    #14 0x7691a066157a in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:594:12
    #15 0x76919f8c887b in js::jit::DoCallFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, unsigned int, JS::Value*, JS::MutableHandle<JS::Value>) /firefox/obj-firefox-asan/js/src/jit/./../../../../js/src/jit/BaselineIC.cpp:1713:10
    #16 0x7690d5a24803  (<unknown module>)

==14926==Register values:
rax = 0x0000082820000000  rbx = 0x00007ffcc0386b00  rcx = 0x0000000000000002  rdx = 0x0000010504000000
rdi = 0x00007ffcc0386ae0  rsi = 0x00007ffcc0386e30  rbp = 0x00007ffcc0386ad0  rsp = 0x00007ffcc0386ac0
 r8 = 0x0000000000000000   r9 = 0x00007fffffffff01  r10 = 0x00007fffffffff01  r11 = 0x00000f043784e301
r12 = 0x00007691bae4f680  r13 = 0x00000ed2375c9ed0  r14 = 0x00007691bae4f680  r15 = 0x00007871bbfec080
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/firefox/obj-firefox-asan/dist/bin/libxul.so+0x22eb5e6c) (BuildId: edc8c007b7761c30c1dec00f40366cf1)
==14926==ABORTING
Attached file trace.jsonl —
Attached file testcase.html —
Attached file prefs.js —
Attached patch fix.patch — — Splinter Review
Attached patch exploit.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → dom-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 4 months ago
Duplicate of bug: 2050657
Resolution: --- → DUPLICATE

For anybody looking this over, this report is in fact a double filing of the other bug, not a variant.

Group: dom-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: