`arguments.length` optimization elides the local `arguments` binding, leaking an enclosing frame's arguments
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
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.
| Reporter | ||
Comment 1•4 months ago
|
||
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.
| Reporter | ||
Updated•4 months ago
|
Comment 2•4 months ago
|
||
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.
Updated•4 months ago
|
Updated•4 months ago
|
| Assignee | ||
Comment 3•4 months ago
|
||
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.
| Assignee | ||
Comment 4•4 months ago
|
||
To be correct, isClosedOver cannot assume that the last scope (.back())
is the only relevant scope. All scopes need to be considered.
Updated•4 months ago
|
| Assignee | ||
Comment 5•4 months ago
|
||
Comment 7•4 months ago
|
||
Just going to point out that NonOptonalPropertyAccessBase has a typo.
Comment 8•4 months ago
|
||
https://hg.mozilla.org/mozilla-central/rev/fa0c788e08f2
https://hg.mozilla.org/mozilla-central/rev/2d358366d4e5
| Assignee | ||
Comment 9•4 months ago
|
||
Whoooops. A typo that compiles.
Updated•3 months ago
|
Updated•3 months ago
|
Updated•3 months ago
|
Updated•3 months ago
|
Updated•21 days ago
|
Description
•