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)
Tracking
()
People
(Reporter: decoder, Unassigned)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved, testcase)
Attachments
(1 file)
|
1.97 KB,
text/plain
|
Details |
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
-
RegExpMatchAll(js/src/builtin/RegExp.js:1200) guards its fast path withIsOptimizableRegExpObject(rx) && C === GetBuiltinConstructor("RegExp"). On the fast path the iterator stores the rawrxplus copies of its source/flags slots in reserved slots of theRegExpStringIteratorObject(CreateRegExpStringIterator,RegExp.js:1261), deliberately skipping the spec's species re-construction. Semantic safety is delegated entirely to a re-validation performed on everynext(). -
RegExpStringIteratorNext(js/src/builtin/RegExp.js:1290) re-validates withIsRegExpPrototypeOptimizable()plus a source/flags slot comparison, then runsRegExpMatcher(regexp, string, lastIndex)which never reads.exec. -
IsRegExpPrototypeOptimizable(js/src/builtin/RegExp.cpp:2622) checkscx->realm()->realmFuses.optimizeRegExpPrototypeFuse— the realm of the self-hosted clone being executed, not the realm ofregexp(the object whoseRegExp.prototype.execthe spec requires reading). -
Same-compartment realms exchange objects without wrappers, and
GuardToRegExpStringIteratoris a pure class check, so realm B'snextruns directly against realm A's iterator. Realm A's fuse is popped (itsRegExp.prototype.execwas patched), but realm B's fuse is intact, so the check passes and the patchedexecis 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.
| Reporter | ||
Comment 1•5 months ago
|
||
Updated•5 months ago
|
Description
•