Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: add FlatBuffer verification in BuildFromAllocation to prevent heap OOB read#120175

Open
Ashutosh0x wants to merge 2 commits into
tensorflow:masterfrom
Ashutosh0x:fix/flatbuffer-oob-read-model-builder
Open

fix: add FlatBuffer verification in BuildFromAllocation to prevent heap OOB read#120175
Ashutosh0x wants to merge 2 commits into
tensorflow:masterfrom
Ashutosh0x:fix/flatbuffer-oob-read-model-builder

Conversation

@Ashutosh0x
Copy link
Copy Markdown

Summary

Add flatbuffers::Verifier check in BuildFromAllocation() before calling ValidateModelBuffers() to prevent heap out-of-bounds read on crafted .tflite files.

Vulnerability (#115308)

FlatBufferModelBase::BuildFromAllocation() calls ValidateModelBuffers() which accesses model_->buffers() — this follows the FlatBuffer root table offset without any bounds checking. A crafted 8-byte .tflite file with an attacker-controlled root table offset causes ReadScalar<int>() in GetVTable() to read out-of-bounds heap memory.

ASan confirms heap-buffer-overflow with steerable OOB distance (0 to 64KB+).

Reproduction

\\python
import struct
with open('/tmp/crash.tflite', 'wb') as f:
f.write(struct.pack('<I', 0x18) + b'TFL3')
import tensorflow as tf
tf.lite.Interpreter(model_path='/tmp/crash.tflite') # SIGSEGV
\\

Fix

Add flatbuffers::Verifier validation in BuildFromAllocation() before ValidateModelBuffers(), consistent with how VerifyAndBuildFromAllocation() already verifies. Returns nullptr with error on invalid buffers instead of crashing.

File Changed

  • tensorflow/compiler/mlir/lite/core/model_builder_base.h

Fixes #115308

…rleaveDatasetOp

InterleaveDatasetOp::MakeDataset only checks that cycle_length > 0
but enforces no upper bound. When cycle_length is INT64_MAX, the
Iterator constructor allocates a vector of that size, causing either
an immediate crash (OOM/SIGSEGV) or integer overflow in the modulo
operation at line 171: (cycle_index_ + 1) % cycle_length_.

Add a reasonable upper bound (1M) for both cycle_length and
block_length with separate constants (kMaxCycleLength, kMaxBlockLength).
Values above this threshold are rejected with InvalidArgument error.

Added unit tests for both overflow cases.

Fixes tensorflow#116198
…ap OOB read

BuildFromAllocation() calls ValidateModelBuffers() which follows
FlatBuffer offsets (model_->buffers()) without bounds checking.
A crafted .tflite file with an attacker-controlled root table offset
causes heap-buffer-overflow via ReadScalar<int>() in GetVTable().

An 8-byte file is sufficient to trigger the crash, and the OOB read
distance is steerable from 0 to 64KB+.

Add flatbuffers::Verifier check before calling ValidateModelBuffers(),
consistent with how VerifyAndBuildFromAllocation already verifies.
Returns nullptr with error on invalid buffers instead of crashing.

Fixes tensorflow#115308
@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Jun 3, 2026
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances security and robustness across TensorFlow components. It adds flatbuffer structure verification in model_builder_base.h to prevent heap-buffer-overflows on crafted inputs. Additionally, it introduces a maximum limit of 1M for both cycle_length and block_length in interleave_dataset_op.cc to guard against integer overflows and excessive memory allocations, supported by new unit tests in interleave_dataset_op_test.cc. No review comments were provided, and the implementation is clean, robust, and well-tested, so I have no further feedback.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@keerthanakadiri keerthanakadiri requested a review from cantonios June 3, 2026 14:24
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Jun 3, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Jun 3, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Jun 3, 2026
@Ashutosh0x
Copy link
Copy Markdown
Author

@cantonios This PR fixes a heap OOB read in BuildFromAllocation() — a crafted 8-byte .tflite file can trigger it since there's no FlatBuffer verification before ValidateModelBuffers() accesses the root table offset. ASan confirms OOB up to 64KB+. The fix just adds flatbuffers::Verifier the same way VerifyAndBuildFromAllocation() already does it. All checks pass. Can you take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review Pull request awaiting review prtype:bugfix PR to fix a bug size:M CL Change Size: Medium

Projects

Status: Assigned Reviewer

Development

Successfully merging this pull request may close these issues.

FlatBufferModel::BuildFromBuffer + ValidateModelBuffers crash on small crafted .tflite (heap OOB read via unchecked root table offset)

3 participants