Closed Bug 2053659 Opened 4 months ago Closed 4 months ago

`arguments.length` optimization elides the local `arguments` binding, leaking an enclosing frame's arguments

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

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

People

(Reporter: jandem, Assigned: mgaudet)

References

(Blocks 2 open bugs, Regression)

Details

(Keywords: regression, reporter-external, sec-other, Whiteboard: [adv-main154-])

Attachments

(2 external files)

This is a combination of two reports from OpenAI's Firefox bug database. Probably not security sensitive.


Summary

SpiderMonkey lowers a plain arguments.length read to the JSOp::ArgumentsLength intrinsic and, believing the function's own arguments binding is then unneeded, omits that binding. Two independent frontend defects break that optimization's safety condition, each leaving a live arguments reference that resolves to an enclosing / farther function's arguments instead of the current function's — a script-observable wrong-scope read (and via the same path call / update / delete / mapped-formal write). Both are parser/emitter bugs: deterministic on first execution, no JIT warmup, reproduce under --fuzzing-safe. The leaked value is a valid heap Value, so impact is info-leak + integrity, not memory-unsafety.

Violated invariant: if arguments is discounted for the ArgumentsLength intrinsic and the local binding is omitted, then (a) every emitted use of that name must lower to JSOp::ArgumentsLength, never an ordinary lookup, and (b) the binding must not be omitted if any nested function closes over arguments. Defect A breaks (a); Defect B breaks (b).

Defect A — emitPropLHS flattens an arguments.length.<prop> chain (FF-0415)

memberPropertyAccess (frontend/Parser.cpp) turns arguments.length into ParseNodeKind::ArgumentsLength and drops the arguments name, so declareFunctionArgumentsObject omits the local binding. That is sound only if every use lowers to JSOp::ArgumentsLength. But when ArgumentsLength is the base of a longer chain, BytecodeEmitter::emitPropLHS treats it as an ordinary PropertyAccess, flattens to the innermost base, and emits the raw arguments name (then GetProp "length" / GetProp "<prop>") — never emitArgumentsLength. With the local binding gone, that name resolves to an enclosing arguments.

function run() {
  const enclosing = { length: { secret: "LEAK-FROM-ENCLOSING" } };
  let arguments = enclosing;            // enclosing lexical `arguments`
  function inner(a, b, c) {
    return arguments.length.secret;     // inner's own arguments.length is 3 -> .secret should be undefined
  }
  return inner(1, 2, 3);
}
print(run());
// SpiderMonkey: "LEAK-FROM-ENCLOSING"   (WRONG - resolved to enclosing `let arguments`)
// per spec:     undefined

Fix: in emitPropLHS, when the innermost base is an ArgumentsLength node, emit JSOp::ArgumentsLength for it instead of flattening through to a raw arguments lookup.

Defect B — isClosedOver() last-entry-only elision (FF-0430)

UsedNameTracker records uses as Use{scriptId, scopeId} and only appends when scopeId is newer than uses_.back(); UsedNameInfo::isClosedOver() examines only that final entry. When an arrow's arguments use (nested script, recorded first) is followed by a later arguments.length use in a newer lexical scope of the current script, the later entry hides the arrow's. isClosedOver() returns false, declareFunctionArgumentsObject omits the local binding, and the arrow binds to the next farther function's arguments.

function factory() {
  return function outer() {
    const capture = () => arguments;    // arrow closes over outer's arguments (recorded first)
    {                                    // newer lexical scope
      const count = arguments.length;    // later current-script use hides the arrow use
      return capture;
    }
  };
}
const outer = factory(10, 20, 30, 40, 50);   // farther frame: [10,20,30,40,50]
const a = outer(1, 2, 3)();                   // outer's own args: [1,2,3]
print(a[0], a.length);
// SpiderMonkey: 10 5    (WRONG - leaks factory's args)
// per spec:     1 3

Fix: isClosedOver() must consider all outstanding uses (any use from a nested script?), not just uses_.back() — e.g. a "used in nested script" flag set on insertion.

We've had a number of correctness bugs for the arguments.length optimization. I don't know how hard it would be, but it would be nice if we could catch these with assertions somehow.

Summary: `arguments.length` intrinsic optimization elides the local `arguments` binding, leaking an enclosing frame's arguments → `arguments.length` optimization elides the local `arguments` binding, leaking an enclosing frame's arguments

Set release status flags based on info from the regressing bug 1825722

:mgaudet, since you are the author of the regressor, bug 1825722, could you take a look? Also, could you set the severity field?

For more information, please visit BugBot documentation.

Severity: -- → S3
Priority: -- → P3

These are very good defects. I'm really impressed by the UsedNamesTracker one. That's a clear semantic failure on my part, and something i should have tested appreciably more.

I'm working on patches.

Flags: needinfo?(mgaudet)
Attached file (secure) —

To be correct, isClosedOver cannot assume that the last scope (.back())
is the only relevant scope. All scopes need to be considered.

Assignee: nobody → mgaudet
Status: NEW → ASSIGNED
Attached file (secure) —

Just going to point out that NonOptonalPropertyAccessBase has a typo.

Group: javascript-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 4 months ago
Resolution: --- → FIXED
Target Milestone: --- → 154 Branch

Whoooops. A typo that compiles.

QA Whiteboard: [sec][qa-triage-done-c155/b154]
Whiteboard: [adv-main154-]
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: