Closed Bug 2061316 Opened 4 months ago Closed 3 months ago

Controlled write-what-where via Nursery::forwardBufferPointer misclassifying a tenured one-past-the-end elements pointer that aliases a nursery chunk base

Categories

(Core :: JavaScript: GC, defect, P1)

defect

Tracking

()

RESOLVED FIXED
156 Branch
Tracking Status
firefox-esr115 155+ fixed
firefox-esr140 155+ fixed
firefox-esr153 155+ fixed
firefox154 --- wontfix
firefox155 + fixed
firefox156 + fixed

People

(Reporter: tjr, Assigned: sfink)

References

(Blocks 1 open bug)

Details

(5 keywords, Whiteboard: [adv-main155+r][adv-esr153.2+r][adv-esr140.15+r][adv-esr115.40+r])

Attachments

(3 files, 5 external files)

Nursery::Space::isInside (js/src/gc/Nursery.h:745-751) considers a pointer inside the nursery iff uintptr_t(p) - uintptr_t(chunk) < ChunkSize for some nursery chunk, which classifies a pointer numerically equal to a nursery chunk base as nursery-resident. Nursery::forwardBufferPointer (js/src/gc/Nursery.cpp:964-993), used during every minor GC to fix up slots/elements pointers spilled in Ion frames (js/src/jit/JitFrames.cpp:1164,1183), blindly dereferences any 'inside' pointer not found in the forwardedBuffers table as a BufferRelocationOverlay and writes the read value back into the frame slot; its IsWriteableAddress check is DEBUG-only.

A zero-capacity tenured ArrayObject (e.g. from deserialize(serialize(new Array(100))), AllocKind OBJECT2) placed in the last cell of a tenured chunk has its elements_ pointer exactly at the chunk end; when that tenured chunk sits 1 MiB below a nursery chunk, elements_ equals the nursery chunk base. This layout is acknowledged as real in js/src/vm/ArrayBufferObject.cpp:3236-3242. The earlier bug-2045451 mitigation (Space::setCurrentEnd reserving CellAlignBytes at nursery chunk ends, js/src/gc/Nursery.cpp:2146-2155) only handles the nursery-side direction and does not prevent this tenured-side aliasing.

When an Ion frame holds such an elements pointer live across a minor GC, forwardBufferPointer dereferences the nursery chunk base — whose first word is the ChunkBase::storeBuffer field (js/public/HeapAPI.h:141-148), i.e. &rt->gc.storeBuffer_ — and patches the spilled elements pointer to that address. The subsequent Ion store-element-hole then performs an attacker-indexed 8-byte write of a raw double into the runtime's StoreBuffer object (index chosen via the array's length). The next minor GC traces the corrupted store buffer and dereferences the attacker value; the verified run crashed with rdi = 0x4141414141414141 in StoreBuffer::ValueEdge::trace. This is a controlled write-what-where in the JS engine reachable from untrusted JavaScript.

Build Info

Affected Code

File: js/src/gc/Nursery.h, line 745-751

MOZ_ALWAYS_INLINE bool Nursery::Space::isInside(const void* p) const {
  for (auto* chunk : chunks_) {
    if (uintptr_t(p) - uintptr_t(chunk) < gc::ChunkSize) {  // p == chunk base => 'inside'
      return true;
    }
  }
  return false;
}

File: js/src/gc/Nursery.cpp, line 964-993

void js::Nursery::forwardBufferPointer(uintptr_t* pSlotsElems) {
  auto* buffer = reinterpret_cast<void*>(*pSlotsElems);

  if (!isInside(buffer)) {   // one-past-the-end tenured pointer aliasing a
    return;                  // nursery chunk base passes this check
  }

  if (ForwardedBufferMap::Ptr p = forwardedBuffers.lookup(buffer)) {
    buffer = p->value();
  } else {
    BufferRelocationOverlay* reloc =
        static_cast<BufferRelocationOverlay*>(buffer);
    buffer = *reloc;                        // reads ChunkBase::storeBuffer
    MOZ_ASSERT(IsWriteableAddress(buffer)); // DEBUG-only
  }

  *pSlotsElems = reinterpret_cast<uintptr_t>(buffer);  // frame's elements ptr
}                                                      // becomes &gc.storeBuffer_

File: js/public/HeapAPI.h, line 141-148

  // The store buffer for pointers from tenured things to things in this
  // chunk. Will be non-null if and only if this is a nursery chunk.
  StoreBuffer* storeBuffer;   // first word of every nursery chunk

  JSRuntime* runtime;
  ChunkKind kind;
  uint8_t nurseryChunkIndex;

File: js/src/vm/ArrayBufferObject.cpp, line 3236-3242

// Check the data pointer is not inside the nursery, but take account of the
// fact that inline data pointers for zero length buffers can point to the end
// of a chunk which can abut the start of the nursery.
if (src.byteLength() != 0 || (uintptr_t(src.dataPointer()) & gc::ChunkMask)) {
  Nursery& nursery = obj->runtimeFromMainThread()->gc.nursery();
  MOZ_ASSERT(!nursery.isInside(src.dataPointer()));
}

The reader (forwardBufferPointer/isInside) and the writers disagree about one-past-the-end pointers: a tenured zero-capacity array whose elements_ pointer equals an adjacent nursery chunk's base address is misclassified as a nursery buffer, and its 'relocation overlay' — actually the nursery chunk header's StoreBuffer* — is written into the Ion frame's spilled elements pointer. The debug comment in ArrayBufferObject.cpp confirms this chunk-end/nursery-base aliasing occurs in practice; setCurrentEnd's CellAlignBytes reservation (Nursery.cpp:2146-2155) only protects the opposite (nursery-side) direction.

Exploit Chain

  1. Grow the nursery to 64 MiB and record nursery chunk base addresses; find a pair where a later chunk sits exactly 1 MiB below an earlier one (verified run: aliasBase=0x7abcc0e00000).
  2. Shrink the nursery so the lower chunk is freed to the empty-chunk pool while the upper chunk remains a nursery chunk; spray tenured zero-capacity arrays (deserialize(serialize(new Array(100)))) until one lands in the recycled chunk's last OBJECT2 cell (verified: victim at 0x7abcc0dfffd8; elements_ = victim+40 = nursery chunk base).
  3. Warm f(a,v){var len=a.length; var t={x:v}; a[len]=v; return t;} in Ion so the elements pointer stays live across the {x:v} allocation; fill the nursery to within 64 bytes of the end and call f(victim, evil) so the allocation's out-of-line path triggers a minor GC with the elements pointer spilled at the safepoint.
  4. During the minor GC, UpdateIonJSFrameForMinorGC calls Nursery::forwardBufferPointer on the spilled pointer; isInside(nursery chunk base) is true, the forwardedBuffers lookup misses, and the chunk header's first word (&rt->gc.storeBuffer_) is written into the frame slot.
  5. After the GC, the Ion store-element-hole executes with elements == &rt->gc.storeBuffer_: it passes its bounds check against out-of-bounds 'initializedLength' data and writes the raw double 0x4141414141414141 at &storeBuffer_ + 8*victim.length — an attacker-indexed 8-byte controlled write (verified: attack L=2..4).
  6. The next minor GC traces the corrupted store buffer; StoreBuffer::MonoTypeBuffer<ValueEdge>::trace dereferences the attacker value, crashing with rdi = 0x4141414141414141 in JS::Value::isGCThing.

Steps to Reproduce

  1. Build Firefox/SpiderMonkey with /app/mozconfigs/mozconfig.linux.asan.fuzzing on an unmodified tree.
  2. Run the provided testcase.js in the JS shell: obj-firefox-asan/dist/bin/js --fuzzing-safe testcase.js (no extra flags, prefs, patches, or servers).
  3. The run prints the groomed layout stages (pair/aliasBase, victim at 0x...fffd8, attack L=2..4) and crashes in the following minorgc() with SEGV on the controlled address 0x4141414141414141 in js::gc::StoreBuffer::ValueEdge::trace. The heap-layout steps are probabilistic but include retries; it reproduced on the first attempt in this verification (~3 minutes under ASAN).

Security Impact

  • Severity: High
  • Attacker capability: An attacker running untrusted JavaScript obtains an 8-byte controlled write at an attacker-chosen index off the runtime's StoreBuffer object (write-what-where into JS runtime state), demonstrated by the subsequent minor GC dereferencing the attacker value 0x4141414141414141. This is a memory-corruption primitive in the content-process JS engine suitable as the basis for content-process code execution.
  • Preconditions: Attacker-controlled JavaScript with Ion compilation enabled (default). The demonstrated testcase uses fuzzing-safe shell testing functions (objectAddress, gcparam, minorgc, serialize/deserialize) to make the required heap layout — a tenured chunk directly below a nursery chunk with a zero-capacity array in the tenured chunk's last cell — deterministic; the underlying aliasing layout is acknowledged as occurring naturally in ArrayBufferObject.cpp:3236-3242, so exploitation from web content requires heap grooming without those helpers.

ASAN Report

AddressSanitizer:DEADLYSIGNAL
=================================================================
==25297==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x5b89a62e6e54 bp 0x7ffc6c2b9ef0 sp 0x7ffc6c2b9e90 T0)
==25297==The signal is caused by a READ memory access.
==25297==Hint: this fault was caused by a dereference of a high value address (see register values below).
    #0 0x5b89a62e6e54 in JS::Value::isGCThing() const /firefox/obj-firefox-asan/dist/include/js/Value.h:861:12
    #1 0x5b89a62e6e54 in js::gc::StoreBuffer::ValueEdge::isGCThing() const /firefox/js/src/gc/StoreBuffer.h:274:43
    #2 0x5b89a62e6e54 in js::gc::StoreBuffer::ValueEdge::trace(js::gc::TenuringTracer&) const /firefox/js/src/gc/Tenuring.cpp:847:8
    #3 0x5b89a62e6e54 in js::gc::StoreBuffer::MonoTypeBuffer<js::gc::StoreBuffer::ValueEdge>::trace(js::gc::TenuringTracer&, js::gc::StoreBuffer*) /firefox/js/src/gc/Tenuring.cpp:389:11
    #4 0x5b89a6279c1d in js::Nursery::traceRoots(js::gc::AutoGCSession&, js::gc::TenuringTracer&) /firefox/js/src/gc/Nursery.cpp:1685:8
    #5 0x5b89a6276dac in js::Nursery::doCollection(js::gc::AutoGCSession&, JS::GCOptions, JS::GCReason) /firefox/js/src/gc/Nursery.cpp:1549:3
    #6 0x5b89a627596a in js::Nursery::collect(JS::GCOptions, JS::GCReason) /firefox/js/src/gc/Nursery.cpp:1408:31
    #7 0x5b89a61ed4dc in js::gc::GCRuntime::collectNursery(JS::GCOptions, JS::GCReason, js::gcstats::PhaseKind) /firefox/js/src/gc/GC.cpp:5507:13
    #8 0x5b89a61cf9b0 in js::gc::GCRuntime::minorGC(JS::GCReason, js::gcstats::PhaseKind) /firefox/js/src/gc/GC.cpp:5476:3
    #9 0x5b89a5b26f2e in JSContext::minorGC(JS::GCReason) /firefox/js/src/vm/JSContext-inl.h:286:17
    #10 0x5b89a5b26f2e in MinorGC(JSContext*, unsigned int, JS::Value*) /firefox/js/src/builtin/TestingFunctions.cpp:823:7
    #11 0x3a0d7ea848fe  (<unknown module>)

==25297==Register values:
rax = 0x0828282828282828  rbx = 0x00007abcc8e60040  rcx = 0x00007abcc8adaa9f  rdx = 0x00007abcc8fc51f0  
rdi = 0x4141414141414141  rsi = 0x00007abcc8e60040  rbp = 0x00007ffc6c2b9ef0  rsp = 0x00007ffc6c2b9e90  
 r8 = 0x0000000000000000   r9 = 0xf2f20000f1f1f100  r10 = 0x00007ffc6c3080b0  r11 = 0x00007ffc6c308080  
r12 = 0x00007abcc8e60040  r13 = 0x00007abcc8fc5210  r14 = 0x00007abcc8fc51f0  r15 = 0x00000f57991f8a42  
SUMMARY: AddressSanitizer: SEGV /firefox/obj-firefox-asan/dist/include/js/Value.h:861:12 in JS::Value::isGCThing() const
==25297==ABORTING

(Preceding stdout: pair keep=2 free=3 aliasBase=0x7abcc0e00000 / victim at 0x7abcc0dfffd8 / attack L=2 / attack L=3 / attack L=4)
Attached file crash_stack.txt —
Attached patch fix.patch — — Splinter Review
Attached file testcase.js —

This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.

Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S2
Priority: -- → P1
Assignee: nobody → sphink
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Duplicate of this bug: 2061420

:sfink, please update all release status flags to reflect the affected/unaffected/disabled state correctly. Which branches (beta, release, and/or ESR) are affected by this flaw?

Flags: needinfo?(sphink)
Group: javascript-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 3 months ago
Resolution: --- → FIXED
Target Milestone: --- → 156 Branch

The patch landed in nightly and beta is affected, along with ESR.
:sfink, is this bug important enough to require an uplift?

For more information, please visit BugBot documentation.

Flags: needinfo?(sphink)

Sorry, I got a little lost when trying to dig into this to understand when it began. I thought it was probably the buffer allocator, but it seems like it predates that. It's probably from pretty close to the introduction of generational GC. I think it's worth some uplifts.

Flags: needinfo?(sphink)
Attached file (secure) —
Attachment #9630877 - Flags: approval-mozilla-esr115?

firefox-esr115 Uplift Approval Request

  • User impact if declined/Reason for urgency: It's a UAF that is reachable with some heap grooming. It's pretty unlikely to accidentally trigger, but it does seem useful to an attacker. The AI described how to make use of it in the bug. It more ends up as an attacker-controlled read than a write, but there is an attacker-controlled write as part of the setup, so it seems pretty concerning.
  • Code covered by automated testing?: no
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: The fix is pretty straightforward, and applies narrowly to pointers that point to a place where they wouldn't be pointing to in the first place if they are actual valid pointers. Specifically, it only changes behavior in the case where a pointer points to the beginning of a 1MB nursery chunk, and there is no real data at the beginning of a nursery chunk, so it cannot be a valid pointer into a nursery chunk. It could be a valid pointer into something else, but this patch causes it to fall back to the path where it is assumed to be something else (it does nothing, as there is nothing to fix up).
  • String changes made/needed?: none
  • Is Android affected?: yes

firefox-esr140 Uplift Approval Request

  • User impact if declined/Reason for urgency: It's a UAF that is reachable with some heap grooming. It's pretty unlikely to accidentally trigger, but it does seem useful to an attacker. The AI described how to make use of it in the bug. It more ends up as an attacker-controlled read than a write, but there is an attacker-controlled write as part of the setup, so it seems pretty concerning.
  • Code covered by automated testing?: no
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: The fix is pretty straightforward, and applies narrowly to pointers that point to a place where they wouldn't be pointing to in the first place if they are actual valid pointers. Specifically, it only changes behavior in the case where a pointer points to the beginning of a 1MB nursery chunk, and there is no real data at the beginning of a nursery chunk, so it cannot be a valid pointer into a nursery chunk. It could be a valid pointer into something else, but this patch causes it to fall back to the path where it is assumed to be something else (it does nothing, as there is nothing to fix up).
  • String changes made/needed?: none
  • Is Android affected?: yes
Attachment #9630878 - Flags: approval-mozilla-esr140?
Attached file (secure) —
Attached file (secure) —
Attachment #9630879 - Flags: approval-mozilla-esr153?

firefox-esr153 Uplift Approval Request

  • User impact if declined/Reason for urgency: It's a UAF that is reachable with some heap grooming. It's pretty unlikely to accidentally trigger, but it does seem useful to an attacker. The AI described how to make use of it in the bug. It more ends up as an attacker-controlled read than a write, but there is an attacker-controlled write as part of the setup, so it seems pretty concerning.
  • Code covered by automated testing?: no
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: The fix is pretty straightforward, and applies narrowly to pointers that point to a place where they wouldn't be pointing to in the first place if they are actual valid pointers. Specifically, it only changes behavior in the case where a pointer points to the beginning of a 1MB nursery chunk, and there is no real data at the beginning of a nursery chunk, so it cannot be a valid pointer into a nursery chunk. It could be a valid pointer into something else, but this patch causes it to fall back to the path where it is assumed to be something else (it does nothing, as there is nothing to fix up).
  • String changes made/needed?: none
  • Is Android affected?: yes
Attached file (secure) —
Attachment #9630887 - Flags: approval-mozilla-beta?

firefox-beta Uplift Approval Request

  • User impact if declined/Reason for urgency: UAF, controlled write, pretty bad. The AIs can figure out how to use it.
  • Code covered by automated testing?: no
  • Fix verified in Nightly?: yes
  • Needs manual QE testing?: no
  • Steps to reproduce for manual QE testing:
  • Risk associated with taking this patch: low
  • Explanation of risk level: Straightforward fix that avoids doing an access in a situation where the access only makes sense in an impossible situation.
  • String changes made/needed?: none
  • Is Android affected?: yes
Attachment #9630887 - Flags: approval-mozilla-beta? → approval-mozilla-beta+
Attachment #9630879 - Flags: approval-mozilla-esr153? → approval-mozilla-esr153+
Attachment #9630878 - Flags: approval-mozilla-esr140? → approval-mozilla-esr140+
Attachment #9630877 - Flags: approval-mozilla-esr115? → approval-mozilla-esr115+
QA Whiteboard: [sec] [uplift] [qa-triage-done-c156/b155]
Whiteboard: [adv-main155+r][adv-esr153.2+r][adv-esr140.15+r][adv-esr115.40+r]
See Also: → 2068302
Group: core-security-release
Attachment #9626453 - Attachment description: (secure) → Bug 2061316 - Handle zero-length allocation chunk test, r=jonco
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: