Closed Bug 2072420 Opened 2 months ago Closed 1 month ago

Native regexp incorrectly rejects a Unicode case-insensitive backreference

Categories

(Core :: JavaScript Engine, defect, P2)

defect

Tracking

()

RESOLVED FIXED
160 Branch
Tracking Status
firefox160 --- fixed

People

(Reporter: bugmon, Assigned: iain)

References

Details

(Keywords: testcase)

Attachments

(5 files, 1 external file)

Found by a scan of commit 02fee9a00c60ca16941bea43bbdfca4b940db7e2^..7d09e5a84803feed1c77936f34b1c8f764cb5b5c. 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.

The submitted /(.)(?i:\1)/u returns false for "\u{10400}\u{10428}" with native regexp compilation, while the interpreter returns true. U+10400 folds to U+10428 according to js/src/util/CaseFolding.txt:1374.

The native comparison helper folds individual UTF-16 code units without decoding surrogate pairs. The differing low surrogates remain unequal, causing the false negative. Independent differential evaluations reproduced with and without --differential-testing, and a control with Ion disabled isolated the native regexp backend. The demonstrated impact is incorrect matching.

Build Info

Affected Code

File: js/src/irregexp/RegExpNativeMacroAssembler.cpp, line 1617-1628

  for (size_t i = 0; i < length; i++) {
    char16_t c1 = substring1[i];
    char16_t c2 = substring2[i];
    if (c1 != c2) {
      // Unicode regexps use the common and simple case-folding
      // mappings of the Unicode Character Database.
      c1 = js::unicode::FoldCase(c1);
      c2 = js::unicode::FoldCase(c2);
      if (c1 != c2) {
        return 0;
      }
    }
  }

File: js/src/util/Unicode.h, line 477-480

inline char16_t FoldCase(char16_t ch) {
  const FoldingInfo& info = CaseFoldInfo(ch);
  return uint16_t(ch) + info.folding;
}

SMRegExpMacroAssembler::CaseInsensitiveCompareUnicode processes individual char16_t values. CheckNotBackReferenceImpl treats its zero result as a failed match (js/src/irregexp/RegExpNativeMacroAssembler.cpp:595).

Exploit Chain

  1. Execute the submitted regexp against the fixed Deseret uppercase/lowercase pair.
  2. Native regexp execution compares the captured character and its case-insensitive backreference as separate UTF-16 code units.
  3. The comparison rejects the differing low surrogates, producing false; the testcase detects this incorrect result and throws.

Steps to Reproduce

  1. Build clean /firefox through build_firefox using /app/mozconfigs/mozconfig.js.linux.debug.
  2. Pass /tmp/tmpk8z2fljl/unicode_backref.js as content to js_shell_differential_evaluator, using /firefox/obj-js-debug/dist/bin/js for both shells.
  3. Set flags_a to ["--no-native-regexp", "--no-threads"] and flags_b to ["--regexp-warmup-threshold=0", "--no-threads"], with timeout 20. The evaluator adds --fuzzing-safe.
  4. Observe differs: true: A exits 0; B exits 3 with Error: Unicode backreference mismatch.
  5. Append --differential-testing to both flag sets and repeat. The same discrepancy reproduces.
  6. For isolation, share --regexp-warmup-threshold=0 --no-threads --no-ion --differential-testing between both shells and add only --no-native-regexp to A. The discrepancy persists.

Security Impact

  • Severity: Low
  • Attacker capability: The demonstrated capability is an incorrect regexp match result. Memory corruption, data disclosure, and security-boundary bypass were not demonstrated.
  • Preconditions: Execution reaches the native regexp backend with a Unicode case-insensitive backreference involving the demonstrated supplementary-character case pair. No source patches, browser preferences, or external services were required.

ASAN Report

No ASan report: this is a js_differential finding. Both required evaluations returned differs: true, with interpreter exit 0 and native regexp exit 3; neither timed out. Native stderr under --differential-testing:
/tmp/fx_audit_jsdiff_63gw0lpg/tmp1w3nq22x.js:4:25 Error: Unicode backreference mismatch
Stack:
  @/tmp/fx_audit_jsdiff_63gw0lpg/tmp1w3nq22x.js:4:1

Full evaluator evidence is saved in /app/output/verification-tmpk8z2fljl/initial.json, differential-testing.json, and native-regexp-isolation.json. Cleanup completed with empty staged and unstaged source diffs.
Attached file crash_stack.txt —
Attached file unicode_backref.js —
Attached file replay.json —
Attached file finding.json —
Attached file evaluator-proof.log —

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

This is a correctness issue, not a security issue. I believe Anba has pointed it out in the past; we should just go ahead and fix it.

One annoying thing here is that js::unicode::FoldCase only supports char16_t inputs, which is insufficient for handling the surrogate pairs case. So we need to use something more powerful here.

Group: javascript-core-security
Keywords: sec-other
Severity: -- → S2
Priority: -- → P2
Duplicate of this bug: 2078696

This is fixed by the irregexp update in bug 2083707.

Assignee: nobody → iireland
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Status: ASSIGNED → RESOLVED
Closed: 1 month 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: