Closed Bug 2046928 Opened 5 months ago Closed 5 months ago

Assertion failure: obj == cacheResult, at js/src/vm/Compartment-inl.h:104 (nuked-realm bypass: AllowNewWrapper checked against Window's realm, not the reified WindowProxy)

Categories

(Core :: JavaScript Engine, defect, P1)

x86_64
Linux
defect

Tracking

()

RESOLVED FIXED
154 Branch
Tracking Status
firefox-esr115 --- wontfix
firefox-esr140 --- wontfix
firefox152 --- wontfix
firefox153 + fixed
firefox154 + fixed

People

(Reporter: decoder, Assigned: jandem)

References

(Blocks 1 open bug)

Details

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

Attachments

(3 files, 2 external files)

Summary

Compartment::getNonWrapperObjectForCurrentCompartment checks the nukedIncomingWrappers guard (AllowNewWrapper) against the object that is passed in, but then performs a Window→WindowProxy identity switch and creates the wrapper for a different object. For a bare Window, the check tests the Window's realm, while the wrapper that is actually minted targets the WindowProxy. After a same-compartment navigation the WindowProxy lives in a different realm of the same compartment, and if that realm's incoming wrappers were nuked the function still mints a fresh, fully-live cross-compartment wrapper into the nuked realm — reviving read/write/inject access that nuking was supposed to sever.

This is the same nuked-realm-isolation invariant class Mozilla fixed in bug 2031152 (WeakRef.deref bypassing nukeCCW) and the bug 2032512/2032328 series; this instance is unfixed in the tested snapshot (the file is identical to current mozilla-central).

Root cause

js/src/vm/Compartment.cpp, Compartment::getNonWrapperObjectForCurrentCompartment (lines 212-292):

// js/src/vm/Compartment.cpp:237-242
// Disallow creating new wrappers if we nuked the object's realm ...
if (!AllowNewWrapper(this, obj)) {            // checks obj's CURRENT realm
  obj.set(NewDeadProxyObject(cx, obj));
  return !!obj;
}
...
// js/src/vm/Compartment.cpp:244-263
if (IsWindow(obj)) {
  obj.set(ToWindowProxyIfWindow(obj));        // IDENTITY SWITCH
  obj.set(UncheckedUnwrap(obj));
  ...
}

AllowNewWrapper (js/src/proxy/CrossCompartmentWrapper.cpp:482) tests obj->nonCCWRealm()->nukedIncomingWrappers. For a bare Window this tests the Window's realm. The code then reifies obj into the Window's current WindowProxy. After a same-compartment navigation (browser: nsGlobalWindowOuter::SetNewDocument; shell: newGlobal({sameCompartmentAs, transplantWindowProxy})) the brain-transplanted WindowProxy belongs to a different realm of the same compartment. Neither the rest of getNonWrapperObjectForCurrentCompartment nor Compartment::getOrCreateWrapper (lines 294-342) re-checks AllowNewWrapper against the new identity object, so a fresh live CCW into the nuked realm is created and cached.

The wrapper map is then inconsistent with a recomputed wrap: a direct value-wrap of the same WindowProxy takes AllowNewWrapper → false → NewDeadProxyObject, while the debug cache-consistency check in JS::Compartment::wrap(JSContext*, MutableHandleValue) finds the revived live CCW:

// js/src/vm/Compartment-inl.h:104
MOZ_ASSERT_IF(cacheResult, obj == cacheResult);   // FAILS

Reproduction

SpiderMonkey JS shell, debug ASan build, product_version = 20260603-5cffec5992d3.

Run the included testcase:

js --fuzzing-safe poc.js

poc.js (full step-by-step in the comment header):

  1. g1 = newGlobal({newCompartment:true, useWindowProxy:true}) — Window W1 / realm R1 / WindowProxy P; keep e1 (surviving CCW to an R1 eval function) and wp1 (CCW to P).
  2. g2 = newGlobal({sameCompartmentAs:g1, useWindowProxy:true, transplantWindowProxy:wp1}) — same-compartment navigation: P is brain-swapped in place into new realm R2.
  3. g2.eval("nukeAllCCWs()") — sets R2.nukedIncomingWrappers; R1 untouched.
  4. Wrap the bare Window W1 into the main compartment via Debugger.Object.prototype.unsafeDereference(): AllowNewWrapper(main, W1) passes (W1 in R1), the IsWindow block switches to P (R2, nuked), and a fresh live CCW main→P is minted.
  5. e1("globalThis") returns P as a value → JS::Compartment::wrap(Value): wrapper-map cache hits the revived live CCW while the recompute produces a dead proxy → assert fails.

In a debug build this trips the assert (poc.js). In a release/opt build the assert is compiled out and the revived wrapper stays live: demo-release.js demonstrates read/write/inject through it after the nuke (read through revived wrapper: sensitive-value-in-g2, write through revived wrapper: 42).

Crash signature

Assertion failure: obj == cacheResult, at js/src/vm/Compartment-inl.h:104
==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (WRITE)
  #0 MOZ_CrashSequence  mozilla/Assertions.h:261
  #1 JS::Compartment::wrap(JSContext*, JS::MutableHandle<JS::Value>)  js/src/vm/Compartment-inl.h:104
  #2 js::CrossCompartmentWrapper::call(...)  js/src/proxy/CrossCompartmentWrapper.cpp:233
  #3 js::Proxy::call(...)  js/src/proxy/Proxy.cpp:700
  #4 js::InternalCallOrConstruct(...)  js/src/vm/Interpreter.cpp:574

Full output in crash-output.txt.

Browser relevance

Same-compartment, multi-realm windows model same-origin navigations that reuse the outer window; transplantWindowProxy (bug 2037348) was added to the shell to model nsGlobalWindowOuter::SetNewDocument. Realm-targeted nuking runs on window/document teardown (WindowDestroyedEvent::Run, xpc::NukeAllWrappersForRealm), and the browser commonly nukes with DontNukeWindowReferences, keeping window-proxy CCWs alive across the flag-setting nuke — making the missed re-check more reachable there than in the shell. Wrapping bare Window objects is routine in Gecko (the IsWindow path exists for it); the Debugger route in the PoC is just the shell's way of producing such a wrap.

Suggested fix

Re-evaluate AllowNewWrapper(this, obj) against the reified identity object — after the IsWindow/ToWindowProxyIfWindow/UncheckedUnwrap sequence, and again in getOrCreateWrapper before creating a wrapper — returning a dead proxy when the WindowProxy's current realm has nukedIncomingWrappers set.

Artifacts

  • poc.js — debug-build assertion testcase.
  • crash-output.txt — full crash output (debug ASan shell).
  • demo-release.js — opt-build demonstration of the nuke-isolation bypass.

This bugs severity seems to have been set during creation. I am resetting the severity to --- so the SpiderMonkey team will see this as un-triaged and go through our triage process. Otherwise we might miss this bug.

Severity: S2 → --

Jan, could help me look at who might be the right person to look at this bug?

Blocks: sm-security
Flags: needinfo?(jdemooij)
Severity: -- → S2
Priority: -- → P1
Assignee: nobody → jdemooij
Status: NEW → ASSIGNED

After a same-compartment navigation the WindowProxy lives in a different realm of the same compartment, and if that realm's incoming wrappers were nuked the function still mints a fresh, fully-live cross-compartment wrapper into the nuked realm — reviving read/write/inject access that nuking was supposed to sever.

Nuking is used to avoid memory leaks for chrome => content wrappers; it's not really a security boundary.

Keywords: sec-high → sec-low
Severity: S2 → S3
Attached file (secure) —
Flags: needinfo?(jdemooij)
Group: javascript-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 5 months ago
Resolution: --- → FIXED
Target Milestone: --- → 154 Branch

Please nominate this for Beta uplift when you get a chance.

Flags: needinfo?(jdemooij)
Flags: in-testsuite+
Flags: needinfo?(jdemooij)

firefox-beta Uplift Approval Request

  • User impact if declined/Reason for urgency: Fixes an issue in the wrapping code. Uplifting only to have the fix in ESR153.
  • Code covered by automated testing?: yes
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: Just adds an extra check to handle a corner case.
  • String changes made/needed?: N/A
  • Is Android affected?: yes
Attachment #9599680 - Flags: approval-mozilla-beta?
Attached file (secure) —
Attachment #9599680 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
QA Whiteboard: [sec] [uplift] [qa-triage-done-c154/b153]
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: