Open Bug 2046891 Opened 5 months ago Updated 2 months ago

Differential behavior: RegExp matchAll iterator next() re-validates OptimizeRegExpPrototypeFuse against the executing realm, bypassing a patched RegExp.prototype.exec across same-compartment realms

Categories

(Core :: JavaScript: Standard Library, defect, P2)

x86_64
Linux
defect

Tracking

()

People

(Reporter: decoder, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: ai-involved, testcase)

Attachments

(1 file)

Summary

The optimized RegExp.prototype[Symbol.matchAll] fast path re-validates its assumptions on every next() using the OptimizeRegExpPrototypeFuse of the currently executing realm instead of the realm that owns the iterator's cached RegExpObject. With two same-compartment realms (in the browser: same-origin iframes; in the shell: newGlobal({sameCompartmentAs: this})), applying the other realm's %RegExpStringIteratorPrototype%.next to an optimized iterator bypasses the deopt that is supposed to restore spec semantics after RegExp.prototype.exec has been modified.

Result: next() returns matches computed by the engine-internal matcher and never calls the monkeypatched exec, violating the specified RegExpExec(R, S) behavior (Get(R, "exec") must observe the patch). V8 follows the spec; SpiderMonkey diverges. Reproduced identically in interpreter, baseline, and Ion.

This defeats membrane/sanitizer-style defensive patching of RegExp.prototype.exec across same-origin realm boundaries and produces spec-violating iteration results.

Root cause

  1. RegExpMatchAll (js/src/builtin/RegExp.js:1200) guards its fast path with IsOptimizableRegExpObject(rx) && C === GetBuiltinConstructor("RegExp"). On the fast path the iterator stores the raw rx plus copies of its source/flags slots in reserved slots of the RegExpStringIteratorObject (CreateRegExpStringIterator, RegExp.js:1261), deliberately skipping the spec's species re-construction. Semantic safety is delegated entirely to a re-validation performed on every next().

  2. RegExpStringIteratorNext (js/src/builtin/RegExp.js:1290) re-validates with IsRegExpPrototypeOptimizable() plus a source/flags slot comparison, then runs RegExpMatcher(regexp, string, lastIndex) which never reads .exec.

  3. IsRegExpPrototypeOptimizable (js/src/builtin/RegExp.cpp:2622) checks cx->realm()->realmFuses.optimizeRegExpPrototypeFuse — the realm of the self-hosted clone being executed, not the realm of regexp (the object whose RegExp.prototype.exec the spec requires reading).

  4. Same-compartment realms exchange objects without wrappers, and GuardToRegExpStringIterator is a pure class check, so realm B's next runs directly against realm A's iterator. Realm A's fuse is popped (its RegExp.prototype.exec was patched), but realm B's fuse is intact, so the check passes and the patched exec is silently ignored.

The same-realm path deopts correctly (see the same-realm line of the output), proving the bypass is precisely the realm confusion. The analogous RegExpSplit/RegExpMatchAll entry guards are realm-bound because IsOptimizableRegExpObject (js/src/builtin/RegExp.cpp:2335) uses shape identity against the executing realm's cached RegExp shape, which a cross-realm object can never satisfy. The next() re-validation has no such realm binding.

Reproduction

Run poc.js (in this directory) — it runs unmodified in both shells:

$ m-c-debug/dist/bin/js --fuzzing-safe poc.js
same-realm  next: done=true value=undefined
cross-realm next: done=false value=["b","b"]
patched exec calls: 1
BUG: cross-realm next() bypassed the patched RegExp.prototype.exec

$ /home/ubuntu/d8 poc.js
same-realm  next: done=true value=undefined
cross-realm next: done=true value=undefined
patched exec calls: 2

The divergence is stable across --no-baseline --no-ion, --baseline-eager, and --ion-eager --no-threads. No special prefs are required.

Impact

Correctness / deopt-bypass with security relevance: it silently disables a documented deoptimization barrier across realm boundaries. The fast path's unsafe slot reads remain type-correct for any genuine RegExpObject, so this specific site does not directly corrupt memory; it demonstrates that the per-realm fuse re-validation model is unsound under same-compartment realm mixing, which becomes memory-unsafe as soon as a fast path guards layout (rather than just semantics) with a realm fuse.

Suggested fix

Make the next() re-validation realm-correct: either check the fuse of regexp's realm (not cx->realm()), or have the fast path additionally require that regexp's realm equals the executing realm (mirroring the shape-identity realm binding used by IsOptimizableRegExpObject), deopting to the reified path otherwise.

Environment

SpiderMonkey JS shell product_version = 20260603-5cffec5992d3 (m-c-asan-debug). Testcase: poc.js.

Blocks: sm-security
Severity: -- → S3
Priority: -- → P2
Group: javascript-core-security
Keywords: sec-audit
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: