From 9427f6240dd7f0b1f752c1799ffbb62b6a41697c Mon Sep 17 00:00:00 2001 From: TristanInSec Date: Sun, 26 Jul 2026 09:51:54 -0400 Subject: [PATCH] Add TFLITE_DCHECK for buffer index bounds in GetFlatbufferTensorBuffer GetFlatbufferTensorBuffer uses flatbuffer_tensor.buffer() as a direct index into the buffers vector without any validation. FlatBuffers Vector::operator[] does not perform bounds checking. A structurally malformed model with a buffer index exceeding the vector size causes an OOB read. Per the Error Handling Guide, FlatBuffer structural bounds checks should use TFLITE_DCHECK exclusively (zero-cost debug aid, compiled out in release). This is consistent with the project's trust model where structurally valid FlatBuffers are the application layer's responsibility. Note: the compression path (line 391) already validates buffer_index against buffers->size() with a runtime check. This TFLITE_DCHECK brings the main path in line for debug builds. --- tensorflow/lite/micro/micro_allocator.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow/lite/micro/micro_allocator.cc b/tensorflow/lite/micro/micro_allocator.cc index ecb1651c71e..5ba5028de79 100644 --- a/tensorflow/lite/micro/micro_allocator.cc +++ b/tensorflow/lite/micro/micro_allocator.cc @@ -203,6 +203,7 @@ void* GetFlatbufferTensorBuffer( // First see if there's any buffer information in the serialized tensor. // TODO(b/170379532): Add better unit tests to validate flatbuffer values. void* out_buffer = nullptr; + TFLITE_DCHECK(flatbuffer_tensor.buffer() < buffers->size()); if (auto* buffer = (*buffers)[flatbuffer_tensor.buffer()]) { // If we've found a buffer, does it have any data? if (auto* array = buffer->data()) {