Open Bug 2078781 Opened 1 month ago Updated 1 month ago

JS::ForOfIterator::closeThrow skips IteratorClose for optimized arrays after %ArrayIteratorPrototype%.return is added mid-iteration

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

UNCONFIRMED

People

(Reporter: bugmon, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: testcase)

Attachments

(5 files)

JS::ForOfIterator::closeThrow returns early whenever isOptimizedArray_ is set, assuming %ArrayIteratorPrototype% has no return property. That assumption is only checked once, in ForOfIterator::init, via IsArrayWithDefaultIterator and the realm fuse. User code that runs between init and closeThrow can add a return method, and closeThrow does not re-check the fuse, so the return call required by IteratorClose is not made. With Promise.all on a Promise subclass whose static resolve installs return and throws, SpiderMonkey calls return zero times where the specification requires one call.

A script can make SpiderMonkey skip a specification-required call to an iterator's return method by defining return on %ArrayIteratorPrototype% from a callback that runs while a Promise combinator iterates a plain array, and then throwing. The demonstrated effect is a behavioural divergence from ECMA-262 and from V8: a missing callback. No crash, assertion failure or memory corruption was observed, and the testcase needs no prefs, flags or patches.

Build Info

Affected Code

File: js/src/vm/ForOfIterator.cpp, line 32-39

  if (IsArrayWithDefaultIterator<MustBePacked::No>(iterableObj, cx)) {
    // Array is optimizable.
    isOptimizedArray_ = true;
    arrayIndex_ = 0;
    iteratorOrArray_ = iterableObj;
    nextMethod_.setUndefined();
    return true;
  }

File: js/src/vm/ForOfIterator.cpp, line 155-163

void ForOfIterator::closeThrow() {
  MOZ_ASSERT(iteratorOrArray_);

  if (isOptimizedArray_) {
    // |iteratorOrArray_| is the array object. IsArrayWithDefaultIterator
    // ensured %ArrayIteratorPrototype% does not have a |return| property, so
    // IteratorClose is a no-op.
    return;
  }

File: js/src/vm/Iteration.cpp, line 2379-2383

  // Ensure Array.prototype[@@iterator] and %ArrayIteratorPrototype% haven't
  // been mutated in a way that affects the iterator protocol.
  if (!arr->realm()->realmFuses.optimizeGetIteratorFuse.intact()) {
    return false;
  }

File: js/src/builtin/Promise.cpp, line 4365-4371

    } else {
      // Step {c, d}. Let nextPromise be
      //              ? Call(promiseResolve, constructor, « next »).
      if (!Call(cx, promiseResolve, CVal, nextValue, &nextPromise)) {
        return false;
      }
    }

File: js/src/builtin/Promise.cpp, line 3684-3690

  auto maybeCloseIter = [](PromiseForOfIterator& iter, bool done) {
    // Step 8.a. If iteratorRecord.[[Done]] is false,
    //           set result to IteratorClose(iteratorRecord, result).
    if (!done) {
      iter.closeThrow();
    }
  };

init takes the optimized-array path when the fuse is intact and records that in isOptimizedArray_. closeThrow trusts that flag without re-validating the fuse. In the testcase, P is a Promise subclass, so the combinator calls the user-defined P.resolve (Promise.cpp:4368) after init; that call installs return and throws, leaving *done == false, so maybeCloseIter calls closeThrow, which returns without performing IteratorClose.

Two additional runs support this mechanism. The control testcase, which hides the array behind a wrapper iterable so init does not take the optimized path, exits 0 in both engines. In a separate run, a bytecode for-of loop over an array with the same mid-loop return installation called return once in SpiderMonkey, and a later Promise.race on a subclass in the same realm (fuse already popped before init) also called return once.

Exploit Chain

  1. Script defines a Promise subclass P with a static resolve method, so the combinator does not use the default-Promise fast path and calls the user-defined resolve.
  2. Script calls P.all([1, 2, 3]). ForOfIterator::init sees a plain array with the iterator fuse intact and sets isOptimizedArray_ = true.
  3. For the first element, the combinator calls P.resolve, which assigns a return function to %ArrayIteratorPrototype% and then throws.
  4. The combinator returns failure with done == false and calls ForOfIterator::closeThrow, which returns immediately because isOptimizedArray_ is still set.
  5. The newly installed return method is never called (called === 0), whereas ECMA-262 Promise.all step 8.a / IteratorClose requires one call and V8 makes it.

Steps to Reproduce

  1. Build the SpiderMonkey debug shell from an unmodified tree with /app/mozconfigs/mozconfig.js.linux.debug (output: /firefox/obj-js-debug/dist/bin/js).
  2. Run testcase.js through js_shell_differential_evaluator with shell_a=/firefox/obj-js-debug/dist/bin/js, shell_b=/app/tools/d8/d8, engine_b="v8". Expected: differs: true, SpiderMonkey exit 3 with Error: IteratorClose did not call return: 0, d8 exit 0.
  3. Re-run with flags_a=["--differential-testing"]. Expected: differs: true with the same exit codes.
  4. Optionally run control_nonoptimized.js with the same shells. Expected: differs: false, both exit 0.

Security Impact

  • Severity: Low
  • Attacker capability: A script can cause a specification-required call to an array iterator's return method to be skipped during abrupt completion of a Promise combinator iterating a plain array. Only this behavioural divergence was demonstrated; no crash, assertion failure or memory corruption was observed.
  • Preconditions: The script must run its own code between the combinator's GetIterator and the abrupt completion (here a Promise subclass's static resolve) and use it to define return on %ArrayIteratorPrototype%. No prefs, shell flags or source patches are needed.

ASAN Report

No sanitizer report: this is a js_differential finding, and the proof is the exit-status divergence below (verifier's own runs, debug shell built from a clean tree).

Run 1 (no extra flags): differs: true
  shell_a argv: ["/firefox/obj-js-debug/dist/bin/js","--fuzzing-safe","/tmp/fx_audit_jsdiff_gn4i4jv3/testcase.js"]  exit_code: 3
  shell_b argv: ["/app/tools/d8/d8","/tmp/fx_audit_jsdiff_gn4i4jv3/testcase.js"]  exit_code: 0
  SpiderMonkey stderr:
    /tmp/fx_audit_jsdiff_gn4i4jv3/testcase.js:11:9 Error: IteratorClose did not call return: 0
    Stack:
      @/tmp/fx_audit_jsdiff_gn4i4jv3/testcase.js:11:9
  d8 stderr: (empty)

Run 2 (--differential-testing on the SpiderMonkey side): differs: true
  shell_a argv: ["/firefox/obj-js-debug/dist/bin/js","--fuzzing-safe","--differential-testing","/tmp/fx_audit_jsdiff_9gi6wcys/testcase.js"]  exit_code: 3
  shell_b argv: ["/app/tools/d8/d8","/tmp/fx_audit_jsdiff_9gi6wcys/testcase.js"]  exit_code: 0
  SpiderMonkey stderr:
    /tmp/fx_audit_jsdiff_9gi6wcys/testcase.js:11:9 Error: IteratorClose did not call return: 0
    Stack:
      @/tmp/fx_audit_jsdiff_9gi6wcys/testcase.js:11:1
  d8 stderr: (empty)

Control (control_nonoptimized.js, array wrapped in a custom iterable): differs: false, both shells exit_code 0.
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file testcase.js —
Attached file README.txt —

This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.

Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

This is a correctness issue.

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

Attachment

General

Created:
Updated:
Size: