JIT type confusion: MGuardToFunction::getAliasSet() returns AliasSet::None() allowing GVN to eliminate guard
Categories
(Core :: JavaScript Engine: JIT, defect)
Tracking
()
People
(Reporter: lexu, Unassigned)
References
Details
(Keywords: reporter-external)
Attachments
(1 file)
|
372 bytes,
text/html
|
Details |
User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:149.0) Gecko/20100101 Firefox/149.0
Steps to reproduce:
. Save the attached poc.html
2. Open in Firefox (or run: firefox --headless --screenshot out.png file:///path/to/poc.html)
3. Read the output
The PoC trains IonMonkey with a real Function (50K iterations), then passes a plain object {name:"PWNED", length:57005}. The JIT-compiled code treats it as a Function.
Actual results:
The object's .name and .length were read through Function-specific IC stubs with no type check, no deopt, no bailout. The MGuardToFunction guard was eliminated by GVN because getAliasSet() returns AliasSet::None().
Root Cause
// js/src/jit/MIR.h, MGuardToFunction class:
AliasSet getAliasSet() const override { return AliasSet::None(); }
Should be:
AliasSet getAliasSet() const override {
return AliasSet::Load(AliasSet::ObjectFields);
}
MGuardToFunction reads the object's class via loadObjClassUnsafe at runtime. AliasSet::None() tells GVN it reads nothing, so GVN eliminates the second guard when two exist on the same object. The IC stubs after the eliminated guard execute Function-specific property access on whatever object is passed.
Why This Works (and similar bugs don't)
MGuardToFunction is a guard ? its elimination removes the type check entirely. The JIT code after it runs unguarded. This is different from MTypeOf or MIsTypedArray which produce values ? even if GVN eliminates a duplicate, the first computation returns the correct result and downstream branches take the right path.
Only guard elimination leads to unguarded specialized code on a wrong type.
Impact
- Attacker controls
.length(integer) and.name(string) that JIT code trusts as Function metadata .lengthused as bounds/count ? potential OOB- Call dispatch on non-function ? potential control flow hijack
- Web-triggerable, page load, no user interaction
Expected results:
The function should have deoptimized (bailed out) when receiving a non-Function object, since MGuardToFunction is supposed to verify the object is a JSFunction.
ADDITIONAL DETAILS:
Root cause: js/src/jit/MIR.h, MGuardToFunction class — getAliasSet() returns AliasSet::None() but the instruction reads the object's class pointer at runtime via loadObjClassUnsafe. GVN treats it as pure and eliminates duplicate guards.
Fix: change AliasSet::None() to AliasSet::Load(AliasSet::ObjectFields). One-line fix included in the attached patch.
MIsTypedArray (non-wrapped path) has the same pattern
Tested on Firefox ESR 140.8.0
Updated•7 months ago
|
Updated•7 months ago
|
Comment 1•7 months ago
|
||
Thanks for the report.
MGuardToFunction is marked as guard so won't be eliminated, and function objects can't be transplanted/swapped so bug 2024919 is unrelated. It might make sense to fix up the alias set here for consistency but I think this particular bug report and the test case are invalid.
I'll take a closer look soon.
Thanks for looking at this, Jan.
You're right that our original PoC doesn't demonstrate a security issue reading .name and .length from a plain object
works through normal polymorphic IC stubs, not through a bypassed guard. We confirmed this with additional tests:
calling the object (TypeError), .bind() (TypeError), new (TypeError), and slot offset probes (all correct values). The
guard catches every case.
We did look at whether GVN could eliminate a congruent MGuardToFunction ValueNumbering.cpp line 916 does call
setNotGuardUnchecked() on redundant guards with matching operands. But in practice this only removes a second guard on
the same SSA value where the first guard already validated the type, so there's no window for exploitation since
objects can't change class between the two checks.
We also checked LICM (the instruction has setMovable() + AliasSet::None() so it looks loop-invariant), but
HasOperandInLoop prevents hoisting when the guarded value is defined inside the loop, and when defined outside, the
object is stable across iterations.
Fair to say this is a correctness issue (loadObjClassUnsafe is a memory read, so AliasSet::None() is technically wrong)
but not a security bug with current SpiderMonkey semantics. Same applies to the MIsTypedArray case we mentioned.
Thanks for the time we'll focus on valid findings going forward.
Comment 3•7 months ago
|
||
Thanks. Let's close this but for consistency I'll change MGuardToFunction::getAliasSet to match MGuardToClass as part of cleaning up some other things.
Updated•7 months ago
|
Updated•2 months ago
|
Description
•