Differential behavior: MapIteratorObject::create / SetIteratorObject::create initialize a foreign realm's %MapIteratorPrototype% without entering its realm (realm pollution + iterator-fuse desync)
Categories
(Core :: JavaScript: Standard Library, defect, P3)
Tracking
()
People
(Reporter: decoder, Unassigned)
References
(Blocks 1 open bug)
Details
(Keywords: ai-involved, testcase)
Attachments
(3 files)
Summary
MapIteratorObject::create and SetIteratorObject::create in js/src/builtin/MapObject.cpp operate on a Map/Set object that can belong to a different realm than the current context, without entering that object's realm. When the current realm's Map.prototype.entries/keys/values (or Set.prototype.values/entries) is invoked on a same-compartment, different-realm receiver whose realm has not yet lazily created its iterator prototype, the foreign realm's canonical %MapIteratorPrototype% (and its self-hosted next clone, @@toStringTag accessor, and possibly its %IteratorPrototype%) are allocated in the current (wrong) realm and then installed as the foreign realm's canonical builtin protos.
This is observable as a SpiderMonkey-vs-V8 differential, and — more importantly — it desynchronizes the realm iterator fuse so that the C++ table-copy fast path in new Map(m) / new Set(s) silently bypasses a user-installed next hook that the spec requires to be called.
Root cause
js/src/builtin/MapObject.cpp:187-215 (Map) and js/src/builtin/MapObject.cpp:1074-1104 (Set):
MapIteratorObject* MapIteratorObject::create(JSContext* cx,
Handle<MapObject*> mapobj,
Kind kind) {
Rooted<GlobalObject*> global(cx, &mapobj->global()); // foreign global
Rooted<JSObject*> proto(
cx, GlobalObject::getOrCreateMapIteratorPrototype(cx, global)); // lazy init, no AutoRealm
...
MapIteratorObject* iterobj =
NewObjectWithGivenProto<MapIteratorObject>(cx, proto); // allocated in cx's realm
getOrCreateMapIteratorPrototype(cx, global) runs GlobalObject::initMapIteratorProto via GlobalObject::createBuiltinProto (js/src/vm/GlobalObject.cpp:526), which has no realm guard. Contrast GlobalObject::resolveConstructor (js/src/vm/GlobalObject.cpp:281-285) handling the identical situation correctly:
MOZ_ASSERT(cx->compartment() == global->compartment());
AutoRealm ar(cx, global);
That AutoRealm is missing on the MapIteratorObject::create / SetIteratorObject::create path.
CallNonGenericMethod only redispatches CCW proxies; a naked same-compartment foreign-realm MapObject passes MapObject::is directly, so entries_impl runs with cx->realm() == A while mapobj->global() is realm B.
Consequences
- Realm pollution. Realm B's canonical
%MapIteratorPrototype%and its builtinnextbecome realm-A objects (realm-pollution-demo.js). - Spec violation / V8 divergence. Per
CreateMapIterator, the iterator must use the active function's realm's%MapIteratorPrototype%; SpiderMonkey hands out the polluted foreign-realm proto. - Realm-fuse desynchronization → silent iteration-protocol bypass (security relevant). Watchtower fuse-popping (
js/src/vm/Watchtower.cpp:362-372,MaybePopMapIteratorPrototypeFuses) keys off the mutated object's own global:obj != obj->global().maybeBuiltinProto(MapIteratorProto). For the polluted proto,obj->global()is realm A's global, whose registered proto is a different object, so redefiningnexton realm B's registered%MapIteratorPrototype%pops no fuse.IsMapObjectWithDefaultIterator(js/src/vm/Iteration.cpp:2403) keeps approving the C++ table-copy fast path inMapObject::tryOptimizeCtorWithIterable(js/src/builtin/MapObject.cpp:743), sonew Map(m)silently ignores the usernexthook. Any sandbox/membrane relying on hooking the iteration protocol can be bypassed. The broken fuse invariant (OptimizeMapObjectIteratorFuse::checkInvariant,js/src/vm/RealmFuses.cpp:575) also tripsMOZ_ASSERTviaRealmFuses::assertInvariants(reachable throughgetFuseState(), disabled under--fuzzing-safe). The identical bug exists for Sets (SetIteratorObject::create,js/src/builtin/MapObject.cpp:1079-1081, consumed by thenew Set(set)fast path atjs/src/builtin/MapObject.cpp:1449).
Reproduction
SpiderMonkey JS shell, mozilla-central product_version = 20260603-5cffec5992d3 (ASan debug build).
Run the differential testcase (poc.js) in both shells — realms are a web-platform feature (same-origin iframes), modeled with newGlobal({sameCompartmentAs: this}) in the SM shell and Realm.createAllowCrossRealmAccess in d8:
$ js --fuzzing-safe poc.js
TEST1 iterator-uses-current-realm-proto: false
TEST2 size-after-hooked-iteration (spec: 0): 2
$ d8 poc.js
TEST1 iterator-uses-current-realm-proto: true
TEST2 size-after-hooked-iteration (spec: 0): 0
Both output lines differ. TEST2 is the security-relevant one: the hooked next is never invoked in SpiderMonkey and both entries are copied anyway.
control.js (SpiderMonkey) shows the divergence is specifically the wrong-realm lazy initialization: when realm B's iterator proto is created from within realm B, the same cross-realm hook correctly pops the fuse and is honored (size: 0).
realm-pollution-demo.js shows realm B's builtin next is a realm-A function and demonstrates the Set variant.
Suggested fix
In MapIteratorObject::create (js/src/builtin/MapObject.cpp:187) and SetIteratorObject::create (js/src/builtin/MapObject.cpp:1074), do not run the foreign global's lazy builtin-proto initialization from the wrong realm. Either:
- enter the table object's realm first (
AutoRealm ar(cx, mapobj);, the pattern fromGlobalObject::resolveConstructor), or - use the current realm's proto (
getOrCreateMapIteratorPrototype(cx, cx->global())), which is whatCreateMapIteratormandates and what V8 implements, or - make
GlobalObject::createBuiltinProtoassert/enter the target global's realm so all such callers are caught.
Files
poc.js— differential testcase (SpiderMonkey vs V8; both output lines differ)control.js— controls showing spec-compliant behavior on non-buggy pathsrealm-pollution-demo.js— realm B's builtinnextis a realm-A function; Set variant of the bypass
| Reporter | ||
Comment 1•5 months ago
|
||
| Reporter | ||
Comment 2•5 months ago
|
||
| Reporter | ||
Comment 3•5 months ago
|
||
Updated•5 months ago
|
Updated•5 months ago
|
Description
•