Closed Bug 2068429 Opened 2 months ago Closed 27 days ago

irregexp parked-position drain optimization skips start positions when a backreference follows a captured greedy loop; /(\w+)\s+\1/.exec("hello ello") returns null

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

RESOLVED FIXED
160 Branch
Tracking Status
firefox160 --- fixed

People

(Reporter: bugmon, Assigned: iain)

References

(Blocks 1 open bug)

Details

(Keywords: testcase)

Attachments

(3 files, 1 external file)

Found by a scan of commit 3806ed99457a0c47c9a4b68a08778a1d19c67983^..359ab0323379b0f997a21bb2703c0282f78ce739. The scan that produced this finding was directed at the changes landed by that commit, which may or may not be its actual cause. Please judge independently whether they are really connected.

LoopChoiceNode::ComputeSearchBodyParkedGrant (js/src/irregexp/imported/regexp-compiler.cc:4337) treats STORE_POSITION as a register-only action (ActionNode::IsRegisterOnlyAction, regexp-nodes.h:527), so a search-loop body whose greedy character-class run is wrapped in a capture group still receives the kParkedUniformPrefix grant even though the continuation's behavior depends on where the run started — breaking the uniform-prefix soundness argument at regexp-nodes.h:269 whenever a backreference to that capture follows. Under that grant Trace::Flush (regexp-compiler.cc:745) omits the PushCurrentPosition save of the attempt start, the drain restores only to the loop entry and the loop-exit backtrack lands at parked_reentry, so start positions inside the consumed run are never retried and /(\w+)\s+\1/.exec("hello ello") returns null instead of the spec-mandated ["ello ello","ello"] at index 1 (V8's d8 returns the correct result); the wrong answer reproduces in the interpreter, the regexp JIT, and debug and ASAN shells, with no memory-safety effect observed.

Exploit Chain

  1. A page executes a RegExp whose pattern begins with a captured greedy + loop over a character class, whose continuation begins with a disjoint class and later contains a backreference, e.g. /(\w+)\s+\1/ or /(\d+)x\1/.
  2. irregexp classifies the search-loop body as kParkedUniformPrefix (ComputeSearchBodyParkedGrant) because STORE_POSITION is treated as register-only and the trailing \w* loop is atomic (kDisjoint).
  3. Trace::Flush skips the PushCurrentPosition save; on continuation failure at start position 0 the drain restores only to the loop entry (position 1) and the loop-exit backtrack lands at parked_reentry, which advances and retries from position 2.
  4. Start position 1, where (\w+)="ello", \s+=" ", \1="ello" would match, is never attempted; exec returns null (observed) instead of the spec-mandated ["ello ello","ello"] at index 1 (returned by V8).
  5. Web content observing RegExp results gets wrong answers: missed matches (null) or matches at a later index than required, in every regexp execution tier.

Affected Code

Steps to Reproduce

  1. Build the SpiderMonkey debug shell from an unmodified tree with /app/mozconfigs/mozconfig.js.linux.debug (build_dir /firefox/obj-js-debug).
  2. Run js_shell_differential_evaluator with shell_a=/firefox/obj-js-debug/dist/bin/js (spidermonkey), shell_b=/app/tools/d8/d8 (v8), content=testcase.js.
  3. Observe differs=true: A=FAIL (exit 3, stderr 'Error: wrong result: null'), B=OK (exit 0).
  4. Re-run with flags_a=["--differential-testing"]; observe differs=true again with identical exit codes.
  5. Optionally confirm tier-independence: the shell prints null for /(\w+)\s+\1/.exec("hello ello") under --no-native-regexp, --regexp-warmup-threshold=0, and in /firefox/obj-firefox-asan/dist/bin/js; d8 prints ["ello ello","ello"] at index 1.

Security Impact

  • Severity: Low
  • Attacker capability: None demonstrated beyond incorrect RegExp results: RegExp.prototype.exec (and by extension test/match/replace/split) can return null or a later-index match where ECMAScript 22.2.7.2 RegExpBuiltinExec requires a match at an earlier index, for patterns with a captured greedy loop followed by a backreference. This is a correctness defect in the regexp compiler affecting all execution tiers; no memory-safety effect was observed (no ASAN report in the ASAN shell).
  • Preconditions: None; any web content evaluating an affected RegExp in an unmodified default-configuration build triggers it.

The full analysis report is attached (analysis.md).

Attached file crash_stack.txt —
Attached file testcase.js —
Attached file analysis.md —

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 bug in upstream V8. The report claims that V8 gets the right result, but I assume the model was testing with an older version where the bug hadn't been introduced. I opened this upstream issue.

Duplicate of this bug: 2078689
See Also: → 2078729
Group: javascript-core-security
Keywords: sec-other

This is fixed by the irregexp update in bug 2083707.

Assignee: nobody → iireland
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Status: ASSIGNED → RESOLVED
Closed: 27 days ago
Resolution: --- → FIXED
Target Milestone: --- → 160 Branch
QA Whiteboard: [qa-triage-done-c161/b160]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: