Closed Bug 2055687 Opened 4 months ago Closed 3 months ago

Type confusion in [@ js::jit::DoGetNameFallback] via trial-inlined ICScript mismatch after resetFallbackStubStates

Categories

(Core :: JavaScript Engine: JIT, defect, P3)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2044837

People

(Reporter: bugmon, Unassigned)

References

(Blocks 1 open bug)

Details

(4 keywords)

Attachments

(4 files)

A trial-inlining ICScript confusion in SpiderMonkey's bailout machinery lets a callee's baseline frame resume execution against a different callee's ICScript, causing IC-kind confusion and ultimately a type confusion where boxed JS::Value bits are dereferenced as a raw JSObject*.

When Warp/Ion compiles a script whose call site was trial-inlined, the compiled code is tied to a specific inlined ICScript child for that call site. The shell testing function resetFallbackStubStates() (registered in the fuzzing-safe TestingFunctions[] array, so available under --fuzzing-safe) discards all fallback stubs, removes inlined ICScript children, and resets the normally one-way trialInliningState machine — but unlike every production purge path it neither invalidates the dependent IonScript nor sets IonScript::purgedICScripts_ (only jit::MarkActiveICScriptsAndCopyStubs does that, JitScript.cpp:820). If the call site is then re-warmed and re-trial-inlined with a different callee (f2), a new inlined ICScript child is installed at the same pcOffset while the stale IonScript (compiled for f1) survives. On the next bailout inside the inlined f1 frame, BaselineStackBuilder::setNextCallee (BaselineBailouts.cpp:508-539) sees trialInliningState == Inlined, finds purgedICScripts() false, and fetches findInlinedChild(pcOff) — f2's ICScript — while nextCallee is f1. The two MOZ_RELEASE_ASSERT defenses only compare numICEntries and bytecodeSize, which f1 and f2 satisfy by construction despite having different IC kinds at each offset.

f1's JSOp::GetProp bytecode then executes f2's GetName IC entry. The GetName fallback (DoGetNameFallback) expects a raw JSObject* environment-chain argument, but the GetProp site passes a boxed JS::Value; GetNameIRGenerator::tryAttachEnvironmentName dereferences the tagged Value bits (0xfffe... visible in r13) as a JSObject* in JSObject::shape() — an attacker-influenced wild pointer dereference. On a debug build the same testcase fails 'Assertion failure: op == JSOp::GetName || op == JSOp::GetGName, BaselineIC.cpp:1166', directly confirming the IC-kind confusion.

Build Info

Affected Code

File: js/src/jit/BaselineBailouts.cpp, line 508-538

void BaselineStackBuilder::setNextCallee(
    JSFunction* nextCallee, TrialInliningState trialInliningState) {
  nextCallee_ = nextCallee;

  if (trialInliningState == TrialInliningState::Inlined &&
      !iter_.ionScript()->purgedICScripts() && canUseTrialInlinedICScripts_) {
    // Update icScript_ to point to the icScript of nextCallee
    const uint32_t pcOff = script_->pcToOffset(pc_);
    icScript_ = icScript_->findInlinedChild(pcOff);   // <-- fetches f2's ICScript while nextCallee is f1
  } else {
    ...
  }

  // Assert the ICScript matches nextCallee.
  JSScript* calleeScript = nextCallee->nonLazyScript();
  MOZ_RELEASE_ASSERT(icScript_->numICEntries() == calleeScript->numICEntries());   // 4 == 4: passes
  MOZ_RELEASE_ASSERT(icScript_->bytecodeSize() == calleeScript->length());          // 20 == 20: passes
}

File: js/src/builtin/TestingFunctions.cpp, line 9917-9930

  jit::ICScript* icScript = script->jitScript()->icScript();
  uint32_t numEntries = script->jitScript()->numICEntries();
  JS::Zone* zone = script->zone();
  for (uint32_t i = 0; i < numEntries; i++) {
    jit::ICFallbackStub* stub = script->jitScript()->fallbackStub(i);
    stub->discardStubs(zone, &icScript->icEntry(i));

    if (icScript->hasInlinedChild(stub->pcOffset())) {
      icScript->removeInlinedChild(stub->pcOffset());   // removes inlined child...
    }

    stub->state().reset();                              // ...and resets the one-way trialInliningState
  }
  script->jitScript()->notePurgedStubs();
  // NOTE: no jit::Invalidate of the dependent IonScript and no
  // IonScript::notePurgedICScripts() -- unlike the production purge path
  // (jit::MarkActiveICScriptsAndCopyStubs, JitScript.cpp:820).

File: js/src/jit/BaselineIC.cpp, line 1155-1170

bool DoGetNameFallback(JSContext* cx, BaselineFrame* frame,
                       ICFallbackStub* stub, HandleObject envChain,   // <-- raw JSObject*, but the
                       MutableHandleValue res) {                      //     GetProp site pushed a boxed Value
  ...
  mozilla::DebugOnly<JSOp> op = JSOp(*pc);
  ...
  MOZ_ASSERT(op == JSOp::GetName || op == JSOp::GetGName);   // fails on debug: op is GetProp

  Rooted<PropertyName*> name(cx, script->getName(pc));

  TryAttachStub<GetNameIRGenerator>("GetName", cx, frame, stub, envChain, name);

File: js/src/jit/CacheIR.cpp, line 3554-3560

  JSObject* env = env_;   // env_ is actually JS::Value bits (0xfffe...) from the GetProp site
  Maybe<PropertyInfo> prop;
  NativeObject* holder = nullptr;

  while (env) {
    if (env->is<GlobalObject>()) {   // JSObject::shape() dereferences the tagged bits -> SEGV

setNextCallee trusts the current (mutable) trial-inlining state to pick the inlined ICScript for the frame being rebuilt, guarded only by IonScript::purgedICScripts() and two size-equality release asserts. resetFallbackStubStates resets that state and swaps inlined children without invalidating the IonScript or setting purgedICScripts_, breaking setNextCallee's assumption that the inlined child at pcOff still corresponds to the callee baked into the Ion code. The confused ICScript then routes a GetProp op into DoGetNameFallback, which treats the boxed Value argument as a raw JSObject* env chain.

Exploit Chain

  1. Warm f1 and f2 standalone so both have JitScripts/baseline data; define them with identical bytecode length (20 bytes) and IC-entry count (4) but different IC kinds per offset (f1: GetProp at pc 3; f2: GetElem/GetName layout swapped).
  2. Warm outer(obj, f1) in a loop; trialInline() inside outer forces DoTrialInlining to create inlined ICScript C1 (for f1) at the call-site pcOffset, and Warp/Ion compiles outer with f1 inlined via C1 (trialInliningState -> Inlined).
  3. Call resetFallbackStubStates(outer): discards stubs, removes inlined child C1, and resets trialInliningState — without invalidating outer's IonScript and without setting IonScript::purgedICScripts_.
  4. Call outer(obj, f2): the baked GuardSpecificFunction(f1) fails -> bailout to baseline; the rest of the invocation re-warms the call site with f2 and trialInline() installs a new inlined ICScript C2 (for f2) at the same pcOffset. The stale IonScript for outer(f1) still exists.
  5. Call outer(objB, f1): the surviving IonScript runs, the inlined f1 body hits a shape-guard failure on objB (--ion-licm=off keeps the guard inside the inline frame) and bails out inside the inlined frame.
  6. BaselineStackBuilder::setNextCallee sees trialIniningState == Inlined, purgedICScripts() == false, and fetches findInlinedChild(pcOff) == C2 (f2's ICScript) while nextCallee is f1; the numICEntries/bytecodeSize release asserts pass by construction.
  7. f1's baseline frame resumes with f2's ICScript: f1's JSOp::GetProp executes f2's GetName IC entry; DoGetNameFallback receives the GetProp site's boxed JS::Value as its HandleObject envChain.
  8. GetNameIRGenerator::tryAttachEnvironmentName dereferences the tagged Value bits (0xfffe...) as JSObject* in JSObject::shape() -> attacker-influenced wild read; controlling the boxed Value's payload gives control over the fake JSObject pointer, a strong primitive for further memory corruption.

Steps to Reproduce

  1. Build the SpiderMonkey ASAN fuzzing shell: build_firefox(firefox_dir=/firefox, mozconfig_path=/app/mozconfigs/mozconfig.linux.asan.fuzzing) -> /firefox/obj-firefox-asan
  2. Run testcase.js via js_shell_evaluator with js_binary=/firefox/obj-firefox-asan/dist/bin/js and flags --fast-warmup --no-threads --monomorphic-inlining=never --ion-licm=off (the evaluator adds --fuzzing-safe)
  3. Observe AddressSanitizer SEGV (wild read) in js::gc::HeaderWord::get() under js::jit::DoGetNameFallback -> GetNameIRGenerator::tryAttachEnvironmentName, with r13 = 0xfffe... (boxed Value bits used as JSObject*)
  4. Optionally run the same testcase on the debug shell (/firefox/obj-js-debug/dist/bin/js, mozconfig.js.linux.debug) to observe 'Assertion failure: op == JSOp::GetName || op == JSOp::GetGName, at js/src/jit/BaselineIC.cpp:1166' confirming the IC-kind confusion

Security Impact

  • Severity: High
  • Attacker capability: Type confusion between a boxed JS::Value and a raw JSObject* inside the baseline IC fallback path. The attacker controls the Value flowing into the confused GetProp/GetName IC slot, so the bits dereferenced as a JSObject* are attacker-influenced — a wild/controlled pointer read on JSObject::shape() that is a classic starting primitive for crafting fake objects and escalating to arbitrary read/write and code execution in the JS engine process. More broadly, any bytecode-op-vs-IC-kind mismatch between two same-sized scripts is reachable through this ICScript swap.
  • Preconditions: Requires the JS shell testing functions trialInline() and resetFallbackStubStates(), both of which are exposed under --fuzzing-safe (they are in the fuzzing-safe TestingFunctions[] array). Not directly reachable from web content, since no production purge path resets trialInliningState without first invalidating Ion code (Zone::forceDiscardJitCode runs jit::InvalidateAll, and on-stack Ion frames get notePurgedICScripts()); the underlying weakness is setNextCallee trusting mutable IC state that the release asserts only weakly validate.

ASAN Report

AddressSanitizer:DEADLYSIGNAL
=================================================================
==5144==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x5b4059c30987 bp 0x7ffdbe3d8b50 sp 0x7ffdbe3d8ad0 T0)
==5144==The signal is caused by a READ memory access.
==5144==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 0x5b4059c30987 in js::gc::HeaderWord::get() const /firefox/js/src/gc/Cell.h:113:23
    #1 0x5b4059c30987 in js::gc::CellWithTenuredGCPointer<js::gc::Cell, js::Shape>::headerPtr() const /firefox/js/src/gc/Cell.h:876:60
    #2 0x5b4059c30987 in JSObject::shape() const /firefox/js/src/vm/JSObject.h:100:37
    #3 0x5b4059c30987 in JSObject::getClass() const /firefox/js/src/vm/JSObject.h:121:44
    #4 0x5b4059c30987 in bool JSObject::is<js::GlobalObject>() const /firefox/js/src/vm/GlobalObject.h:1224:13
    #5 0x5b4059c30987 in js::jit::GetNameIRGenerator::tryAttachEnvironmentName(js::jit::ObjOperandId, JS::Handle<JS::PropertyKey>) /firefox/obj-firefox-asan/js/src/jit/./../../../../js/src/jit/CacheIR.cpp:3559:14
    #6 0x5b4059c2ec4d in js::jit::GetNameIRGenerator::tryAttachStub() /firefox/obj-firefox-asan/js/src/jit/./../../../../js/src/jit/CacheIR.cpp:3325:3
    #7 0x5b40597318d3 in void js::jit::TryAttachStub<js::jit::GetNameIRGenerator, JS::Handle<JSObject*>&, JS::Rooted<js::PropertyName*>&>(char const*, JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, JS::Handle<JSObject*>&, JS::Rooted<js::PropertyName*>&) /firefox/obj-firefox-asan/js/src/jit/./../../../../js/src/jit/BaselineIC.cpp:520:17
    #8 0x5b40597318d3 in js::jit::DoGetNameFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICFallbackStub*, JS::Handle<JSObject*>, JS::MutableHandle<JS::Value>) /firefox/obj-firefox-asan/js/src/jit/./../../../../js/src/jit/BaselineIC.cpp:1170:3
    #9 0x28df88a17b2f  (<unknown module>)

==5144==Register values:
rax = 0x00000f5684f640b6  rbx = 0x0000000000000000  rcx = 0x0000000000000000  rdx = 0x00005b405dbe3400  
rdi = 0x00007ffdbe3d8d50  rsi = 0x0000000000000001  rbp = 0x00007ffdbe3d8b50  rsp = 0x00007ffdbe3d8ad0  
 r8 = 0x0000000000000001   r9 = 0x0000000000000037  r10 = 0x00007fffffffff01  r11 = 0x00007eb428e25001  
r12 = 0x00007ab427e97c60  r13 = 0xfffe7eb428e26060  r14 = 0x00005b4057040620  r15 = 0x1fffcfd6851c4c0c  
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /firefox/js/src/gc/Cell.h:113:23 in js::gc::HeaderWord::get() const
==5144==ABORTING
Attached file testcase.js —
Attached file run-flags.txt —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

This is only a bug in a testing function. It was fixed in bug 2044837.

Group: javascript-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 3 months ago
Duplicate of bug: 2044837
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: