Closed Bug 2027987 Opened 7 months ago Closed 7 months ago

Type confusion in [@ js::jit::HasNativeElementPure] due to MGuardIsNativeObject having AliasSet::None allowing GVN elimination across JSObject::swap

Categories

(Core :: JavaScript Engine: JIT, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2024919

People

(Reporter: bugmon, Unassigned)

Details

(5 keywords)

Summary

MGuardIsNativeObject in js/src/jit/MIROps.yaml line 2608 is declared with alias_set: none, which tells GVN that the guard does not depend on any memory state. This allows GVN to eliminate a redundant MGuardIsNativeObject even when an intervening call performs JSObject::swap (brain transplant), which mutates the object's shape/class in-place. After the guard is eliminated, MCallObjectHasSparseElement calls HasNativeElementPure on what has become a CrossCompartmentWrapper proxy, treating the proxy's handler pointer as NativeObject::elements_ and reading out-of-bounds.

Affected Code

File: js/src/jit/MIROps.yaml, line 2608 (local tree)

# Guard the object is a native object.
- name: GuardIsNativeObject
  operands:
    object: Object
  result_type: Object
  guard: true
  movable: true
  congruent_to: if_operands_equal
  alias_set: none          # <-- BUG: should be load: ObjectFields
  generate_lir: true
  lir_result_type: none
  lir_temps: 1

With alias_set: none and congruent_to: if_operands_equal, two MGuardIsNativeObject(obj) instructions on the same SSA operand are considered congruent and the second has no dependency edge on any store. GVN therefore replaces the second guard with the first, even when a call with AliasSet::Store(Any) sits between them.

The guard reads obj->shape()->getObjectClass()->flags & NON_NATIVE, which is mutable via JSObject::swap. JSObject::swap replaces the object's shape in-place when a DOM node is transplanted to a new compartment, turning a NativeObject into a CrossCompartmentWrapper (ProxyObject).

Correct Pattern

- name: GuardIsNativeObject
  operands:
    object: Object
  result_type: Object
  guard: true
  movable: true
  congruent_to: if_operands_equal
  alias_set:
    load: ObjectFields
  generate_lir: true
  lir_result_type: none
  lir_temps: 1

Declaring load: ObjectFields gives the guard a dependency on AliasSet::ObjectFields, so GVN's AliasAnalysis will not hoist or merge it past a Store(Any) call. This matches other shape-reading guards like MGuardShape and the adjacent guard at lines 2590–2593 in the same file.

Exploit Chain

  1. Script creates a transplantable NativeObject with sparse indexed properties (obj[1000000]=1; obj[2000000]=2).
  2. Object.hasOwn(obj, 1000000) attaches CacheIR stub via HasPropIRGenerator::tryAttachSparse, emitting GuardIsNativeObject(obj) + CallObjectHasSparseElementResult(obj, idx).
  3. Warp compiles f(). The transpiler emits two MGuardIsNativeObject(obj) instructions, one per Object.hasOwn call, with MCall(cb) between them.
  4. GVN sees both guards as congruent (if_operands_equal) and, because alias_set: none means no memory dependency, eliminates the second guard.
  5. At runtime, the first guard passes (object is native). cb() calls transplant(g2) which invokes JSObject::swap, turning the object into a CrossCompartmentWrapper proxy at the same address.
  6. The second MCallObjectHasSparseElement executes without re-guarding, calling HasNativeElementPure(cx, obj, index, vp) with a proxy cast as NativeObject*.
  7. obj->containsDenseElement(index) reads elements_. In ProxyObject layout, the same offset holds the proxy handler pointer (&CrossCompartmentWrapper::singleton).
  8. getElementsHeader()->initializedLength dereferences (ObjectElements*)(handler) - 1 → read at &singleton - 12 → ASAN global-buffer-overflow.

Security Impact

Severity: High (sec-high). Type confusion between NativeObject and ProxyObject in Ion-compiled code.

Attacker capability: An attacker controlling web content can trigger this from JavaScript. While transplantableObject() is a shell-only testing function, the underlying primitive — JSObject::swap — is reachable from web content via document.adoptNode() / cross-compartment DOM node adoption, which performs brain transplants on DOM reflector objects. The type confusion reads attacker-influenced memory as ObjectElements fields; depending on the confused layout, this can leak memory contents via the returned boolean and, if initializedLength reads as a large value, the subsequent elements_[idx].isMagic() dereference reads arbitrary offsets from the proxy handler pointer. Further code paths (shape()->lookup) would operate on corrupted shape data, potentially escalating to memory corruption.

Preconditions: Ion compilation must fire (--fast-warmup accelerates this in the testcase; in the browser, a warm loop suffices). The object must have sparse indexed properties to attach the HasProp.Sparse IC stub.

ASAN Report

==220716==ERROR: AddressSanitizer: global-buffer-overflow on address 0x59b50e2afe14 at pc 0x59b50b9497ca bp 0x7ffd8a2933f0 sp 0x7ffd8a2933e8
READ of size 4 at 0x59b50e2afe14 thread T0
    #0 0x59b50b9497c9 in js::NativeObject::getDenseInitializedLength() const /firefox/js/src/vm/NativeObject.h:734:33
    #1 0x59b50b9497c9 in js::NativeObject::containsDenseElement(unsigned int) const /firefox/js/src/vm/NativeObject.h:730:18
    #2 0x59b50b9497c9 in js::jit::HasNativeElementPure(JSContext*, js::NativeObject*, int, JS::Value*) /firefox/js/src/jit/VMFunctions.cpp:2258:12
    #3 0x34f9465f84d4  (<unknown module>)
    #4 0x34f9465e63dc  (<unknown module>)
    #5 0x59b50c1591dd in EnterJit(JSContext*, js::RunState&, unsigned char*) /firefox/js/src/jit/Jit.cpp:107:5
    #6 0x59b50c1591dd in js::jit::MaybeEnterJit(JSContext*, js::RunState&) /firefox/js/src/jit/Jit.cpp:255:10
    #7 0x59b50c38a7f3 in js::RunScript(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:450:32
    #8 0x59b50c38bcef in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:618:13
    #9 0x59b50b61e86b 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
    ...

0x59b50e2afe14 is located 12 bytes before global variable 'js::CrossCompartmentWrapper::singleton' defined in '/firefox/obj-x86_64-pc-linux-gnu/dist/include/js/Wrapper.h:262' (0x59b50e2afe20) of size 24
0x59b50e2afe14 is located 52 bytes after global variable 'vtable for js::CrossCompartmentWrapper' defined in 'Unified_cpp_js_src_proxy0.cpp' (0x59b50e2afca0) of size 320
SUMMARY: AddressSanitizer: global-buffer-overflow /firefox/js/src/vm/NativeObject.h:734:33 in js::NativeObject::getDenseInitializedLength() const

Suggested Fix

Change alias_set: none to alias_set: load: ObjectFields in js/src/jit/MIROps.yaml:

 - name: GuardIsNativeObject
   operands:
     object: Object
   result_type: Object
   guard: true
   movable: true
   congruent_to: if_operands_equal
-  alias_set: none
+  alias_set:
+    load: ObjectFields
   generate_lir: true
   lir_result_type: none
   lir_temps: 1

This ensures the guard is not reordered or eliminated past stores to ObjectFields, including calls that may perform JSObject::swap.

Note: This same incorrect alias_set: none pattern affects multiple related MIR instructions in MIROps.yaml and MIR.h: GuardIsProxy, GuardIsNotProxy, GuardIsNotDOMProxy, GuardHasProxyHandler, LoadScriptedProxyHandler, IsConstructor, MIsCallable, MHasClass, and MGuardToClass — all of which read the object's class and should have Load(ObjectFields).

Status: UNCONFIRMED → RESOLVED
Closed: 7 months ago
Duplicate of bug: 2024919
Resolution: --- → DUPLICATE
Group: core-security → javascript-core-security

No valid actions for resolution (DUPLICATE).
Removing bugmon keyword as no further action possible. Please review the bug and re-add the keyword for further analysis.

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