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

Out of bounds memory access in V8 (CVE-2024-6779)

A type confusion in V8's WebAssembly memory caching allowed an out-of-bounds memory access. An attacker could craft a WebAssembly module to read or write outside the intended memory bounds, potentially leading to code execution.

Seunghyun Lee Follow

Originally disclosed on Chrome VRP

  • Fix shipped in 11 days

VULNERABILITY DETAILS

Summary

WebAssembly OOB memory access due to cached memory index confusion with multi-memory.

Details

To speed up WebAssembly memory access, it maintains one cached memory in its instance cache. It heuristically finds the first memory access and caches it.

C++
  void StartFunction(FullDecoder* decoder) {
    // ...
    size_t num_memories =
        decoder->module_ == nullptr ? 0 : decoder->module_->memories.size();
    if (num_memories == 1) {
      builder_->set_cached_memory_index(0);
    } else if (num_memories > 1) {
      int first_used_mem_index = FindFirstUsedMemoryIndex(
          base::VectorOf(decoder->start(), decoder->end() - decoder->start()),
          decoder->zone());
      if (first_used_mem_index >= 0) {
        builder_->set_cached_memory_index(first_used_mem_index);
      }
    }
    LoadInstanceCacheIntoSsa(ssa_env);
    // ...
  }

  // Load the instance cache entries into the SSA Environment.
  void LoadInstanceCacheIntoSsa(SsaEnv* ssa_env) {
    builder_->InitInstanceCache(&ssa_env->instance_cache);
  }

void WasmGraphBuilder::InitInstanceCache(
    WasmInstanceCacheNodes* instance_cache) {
  // We handle caching of the instance cache nodes manually, and we may reload
  // them in contexts where load elimination would eliminate the reload.
  // Therefore, we use plain Load nodes which are not subject to load
  // elimination.

  // Only cache memory start and size if there is a memory (the nodes would be
  // dead otherwise, but we can avoid creating them in the first place).
  if (!has_cached_memory()) return;

  instance_cache->mem_start = LoadMemStart(cached_memory_index_);

  // TODO(13957): Clamp the loaded memory size to a safe value.
  instance_cache->mem_size = LoadMemSize(cached_memory_index_);
}

On following memory accesses, the compiler re-uses it when the same memory is accessed (with some caveats around reloading on memory growth, irrelevant with this report).

C++
Node* WasmGraphBuilder::MemStart(uint32_t mem_index) {
  DCHECK_NOT_NULL(instance_cache_);
  V8_ASSUME(cached_memory_index_ == kNoCachedMemoryIndex ||
            cached_memory_index_ >= 0);
  if (mem_index == static_cast<uint8_t>(cached_memory_index_)) {
    return instance_cache_->mem_start;
  }
  return LoadMemStart(mem_index);
}

Node* WasmGraphBuilder::MemSize(uint32_t mem_index) {
  DCHECK_NOT_NULL(instance_cache_);
  V8_ASSUME(cached_memory_index_ == kNoCachedMemoryIndex ||
            cached_memory_index_ >= 0);
  if (mem_index == static_cast<uint8_t>(cached_memory_index_)) {
    return instance_cache_->mem_size;
  }

  return LoadMemSize(mem_index);
}

Note the static_cast<uint8_t>(cached_memory_index_) - this results in truncating the integer memory index into a uint8_t, resulting in confusion between different memories. This results in using memory start pointer and length of another memory (and also allows confusion of kNoCachedMemoryIndex with 0xff).

Also, if a memory access is known to be statically in-bounds, the compiler omits runtime bound checks:

C++
// Insert code to bounds check a memory access if necessary. Return the
// bounds-checked index, which is guaranteed to have (the equivalent of)
// {uintptr_t} representation.
std::pair<Node*, BoundsCheckResult> WasmGraphBuilder::BoundsCheckMem(
    const wasm::WasmMemory* memory, uint8_t access_size, Node* index,
    uintptr_t offset, wasm::WasmCodePosition position,
    EnforceBoundsCheck enforce_check, AlignmentCheck alignment_check) {
  // ...
  // Convert the index to uintptr.
  Node* converted_index = index;
  if (!memory->is_memory64) {
    converted_index = gasm_->BuildChangeUint32ToUintPtr(index);
  } else if (kSystemPointerSize == kInt32Size) {
    // Only use the low word for the following bounds check.
    converted_index = gasm_->TruncateInt64ToInt32(index);
  }

  UintPtrMatcher constant_index(converted_index);
  // ..
  uintptr_t end_offset = offset + access_size - 1u;

  if (constant_index.HasResolvedValue() &&
      end_offset <= memory->min_memory_size &&
      constant_index.ResolvedValue() < memory->min_memory_size - end_offset) {
    // The input index is a constant and everything is statically within
    // bounds of the smallest possible memory.
    return {converted_index, BoundsCheckResult::kInBounds};
  }
  // ..
}

Thus we can create/import memories as below:

  • Index 0, length larger than N+8 (+8 for i64 access)
  • Index 1 ~ 0xff, placeholders
  • Index 0x100, length N

And do the following memory accesses:

  • i64.load {index 0x100, offset 0}, stack [index 0, value X]
    • This caches memory index 0x100 into instance cache.
  • i64.load {index 0, offset N}, stack [index 0, value Y]
    • The access is statically in-bounds in terms of WasmMemory index 0.
    • This confuses index 0x100 with index 0 and uses the cached pointer, leading to OOB

Bisect

This existed since the introduction of multi-memory caching: https://chromium.googlesource.com/v8/v8.git/+/1137222a33cfce2e6fcc45c7395c98b496a0762f

VERSION

See bisect commit release info in Chromium Dash for more info: https://chromiumdash.appspot.com/commit/1137222a33cfce2e6fcc45c7395c98b496a0762f

Chrome Version: 118.0.5993.54 stable ~ latest (126.0.6478.127 stable)
Operating System: All

Note that on environments with wasm OOB trap handling enabled (generally speaking, 64bit platforms), it should prevent exploitation of this vulnerability assuming that its implementation is correct. 32bit chrome is exploitable in the same way as b/40061453.

REPRODUCTION CASE

Attached as wasm-ic-memory-oob.html which crashes the renderer. Note that DevTools must not be opened as it prevents wasm tier-up. wasm-module-builder.js is inlined for easier repro.

FOR CRASHES, PLEASE INCLUDE THE FOLLOWING ADDITIONAL INFORMATION

Type of crash: Renderer
Crash State: Crashes on JITed wasm function, explained in depth above in vuln details

CREDIT INFORMATION

Reporter credit: Seunghyun Lee

The original report as published on the Chromium issue tracker, by Seunghyun Lee (@0x10n). Images and videos are our own copies; raw HTML is shown as text. View it on Chromium issue tracker.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The cached memory index was truncated to a uint8_t, causing different memory indices to be confused and leading to the use of an incorrect memory start pointer and size.

Impact

On 64-bit platforms with Wasm OOB trap handling enabled, exploitation is prevented assuming correct implementation; on 32-bit Chrome, the vulnerability is exploitable in the same way as b/40061453, potentially leading to code execution.

What the attacker needed

Victim runs a crafted WebAssembly module; 32-bit Chrome or OOB trap handling disabled

How it was found

  1. Create or import a WebAssembly module with multiple memories: memory 0 with length larger than N+8, memories 1 to 0xff as placeholders, and memory 0x100 with length N.
  2. Perform an i64.load from memory index 0x100 with offset 0, using a stack index from memory 0 and value X. This caches memory index 0x100.
  3. Perform an i64.load from memory index 0 with offset N, using a stack index from memory 0 and value Y. This access is statically in-bounds for memory 0.
  4. The compiler confuses index 0x100 with index 0 and uses the cached pointer, resulting in an out-of-bounds access.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

WebAssembly (Wasm) is a fast, low-level code format that runs in the browser. V8, Chrome's JavaScript engine, optimises Wasm memory access by caching the start and size of one memory. A bug caused it to mix up different memories when their indices were truncated, so a program could access memory outside its allowed area, which is dangerous.

What is Memory corruption?

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

More by Seunghyun Lee

View all

14 more disclosed reports, best first.

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.