Skip to content

Fix arithmetic overflow in TFLite buffer bounds checks (re-apply #116204) - #123910

Merged
copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
prasanna8585:fix-interpreter-builder-bounds-check
Aug 7, 2026
Merged

copybara-service[bot] merged 2 commits into
tensorflow:masterfrom
prasanna8585:fix-interpreter-builder-bounds-check

Conversation

@prasanna8585

Copy link
Copy Markdown
Contributor

This re-applies the fix from #116204 (commit 5cf3899), which corrected an overflow-prone bounds check pattern (offset + size > bytes) in ParseNodes()/ParseTensors() to use the overflow-safe subtraction form (size > bytes || offset > bytes - size).

The original fix appears to have been unintentionally reverted by a subsequent sync commit shortly after merging. This PR re-applies the same reviewed diff with no functional changes from the original.

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Jul 24, 2026

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

Copy link
Copy Markdown
Contributor

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 improves security and robustness in tensorflow/lite/core/interpreter_builder.cc by refactoring bounds checks to prevent potential integer overflows. Specifically, in both ParseNodes and ParseTensors, addition-based offset checks are replaced with overflow-safe subtraction-based checks when validating custom options and constant buffer offsets against the allocation size. There are no review comments, and I have no additional feedback to provide.

@keerthanakadiri keerthanakadiri added the comp:lite TF Lite related issues label Jul 24, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Jul 24, 2026
@keerthanakadiri
keerthanakadiri requested a review from a team July 24, 2026 11:37
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Jul 24, 2026
@prasanna8585

Copy link
Copy Markdown
Contributor Author

Hi Team,
Is there any changes or update require from my side on this PR?

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please re-implement this solution as Ingress Validation in tflite::Verify (tensorflow/lite/core/tools/verifier.cc) rather than adding checks to InterpreterBuilder.

Rationale

To adhere to the LiteRT/TFLite security model, we aim to avoid redundant checks and code bloat in the core runtime execution path when they can be effectively caught at ingress. Malformed FlatBuffers should be rejected early during model verification before they reach InterpreterBuilder.

Requested Changes

  • Remove the added checks from tensorflow/lite/core/interpreter_builder.cc.
  • Implement equivalent overflow-safe range checks in tensorflow/lite/core/tools/verifier.cc.
  • Ensure that file offsets and sizes (such as CustomOption and Constant buffer offsets) are validated against the total allocation bounds within tflite::Verify (e.g., using size > limit || offset > limit - size to prevent overflow in the check itself).

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Aug 5, 2026
TristanInSec and others added 2 commits August 6, 2026 09:55
…116204)

* Avoid arithmetic overflow in TFLite buffer bounds checks

Rewrite bounds checks in ParseNodes and ParseTensors to use
subtraction instead of addition, preventing potential wraparound
when summing two uint64 values.

* Trigger CI re-run (flaky while_op_test failure)
Per review: re-implement the overflow-safe bounds checks as ingress
validation in tflite::Verify (tensorflow/lite/core/tools/verifier.cc)
instead of tensorflow/lite/core/interpreter_builder.cc, so malformed
FlatBuffers are rejected during model verification before they reach
InterpreterBuilder.

- Reverts this PR's prior changes to interpreter_builder.cc entirely;
  that file is no longer touched.
- Adds an overflow-safe range check for Operator.large_custom_options_offset/
  size (custom options stored outside the flatbuffer, for models > 2GB) in
  VerifyOperators.
- Adds an overflow-safe range check for Buffer.offset/size (tensor buffer
  data stored outside the flatbuffer, for models > 2GB) in VerifyTensors,
  a case the verifier did not previously validate at all.
- Both checks use the subtraction form (size > len || offset > len - size)
  so the bounds check itself cannot overflow, threading the total
  allocation length (len) down from Verify() through VerifyModel(),
  VerifySubGraphs(), VerifyOperators(), and VerifyTensors().
@prasanna8585
prasanna8585 force-pushed the fix-interpreter-builder-bounds-check branch from e7e6421 to aa6431c Compare August 6, 2026 04:31
@prasanna8585
prasanna8585 requested a review from dmiltr3 August 6, 2026 04:32

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you for moving the ingress validation overflow checks into tflite::Verify (verifier.cc) and removing them from InterpreterBuilder. Validating both large_custom_options and external buffer offsets/sizes against len using the overflow-safe subtraction form keeps the runtime path lean and secures ingress. Looks great to merge.

@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Aug 6, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Aug 6, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 6, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 7, 2026
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Aug 7, 2026
…e-apply #116204)

Imported from GitHub PR tensorflow/tensorflow#123910

This re-applies the fix from #116204 (commit 5cf3899), which corrected an overflow-prone bounds check pattern (`offset + size > bytes`) in ParseNodes()/ParseTensors() to use the overflow-safe subtraction form (`size > bytes || offset > bytes - size`).

The original fix appears to have been unintentionally reverted by a subsequent sync commit shortly after merging. This PR re-applies the same reviewed diff with no functional changes from the original.
Copybara import of the project:

--
dc8862dbe6e73a9df337492f91825626501f42b7 by Tristan <[email protected]>:

Avoid arithmetic overflow in TFLite buffer bounds checks (#116204)

* Avoid arithmetic overflow in TFLite buffer bounds checks

Rewrite bounds checks in ParseNodes and ParseTensors to use
subtraction instead of addition, preventing potential wraparound
when summing two uint64 values.

* Trigger CI re-run (flaky while_op_test failure)
--
aa6431c59686dc1929ff45117c01c966729c2804 by prasanna8585 <[email protected]>:

Move TFLite buffer bounds overflow checks to ingress verification

Per review: re-implement the overflow-safe bounds checks as ingress
validation in tflite::Verify (tensorflow/lite/core/tools/verifier.cc)
instead of tensorflow/lite/core/interpreter_builder.cc, so malformed
FlatBuffers are rejected during model verification before they reach
InterpreterBuilder.

- Reverts this PR's prior changes to interpreter_builder.cc entirely;
  that file is no longer touched.
- Adds an overflow-safe range check for Operator.large_custom_options_offset/
  size (custom options stored outside the flatbuffer, for models > 2GB) in
  VerifyOperators.
- Adds an overflow-safe range check for Buffer.offset/size (tensor buffer
  data stored outside the flatbuffer, for models > 2GB) in VerifyTensors,
  a case the verifier did not previously validate at all.
- Both checks use the subtraction form (size > len || offset > len - size)
  so the bounds check itself cannot overflow, threading the total
  allocation length (len) down from Verify() through VerifyModel(),
  VerifySubGraphs(), VerifyOperators(), and VerifyTensors().

Merging this change closes #123910

PiperOrigin-RevId: 960838590
@copybara-service
copybara-service Bot merged commit 64fca1f into tensorflow:master Aug 7, 2026
14 of 15 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 7, 2026
cybrdude added a commit to cybrdude/tensorflow that referenced this pull request Sep 30, 2026
…uzz target

Puts the harness on the supported ingress path for untrusted models: call
tflite::Verify() first and stop on rejection, then build and invoke. The
verifier owns the bounds checks on model metadata (tensorflow/tensorflow
tensorflow#116631 review, Sep 18; checks landed in tensorflow#123910), so inputs like the
uint64 external-offset wrap are rejected before ParseTensors, and the
verifier itself gets fuzz coverage. No change to interpreter_builder.cc.

VerifyAndBuildFromBuffer alone would not do this: without an extra_verifier
it runs only the flatbuffers structural check.
cybrdude added a commit to cybrdude/tensorflow that referenced this pull request Oct 3, 2026
…lder

Both sites compared offset + size against allocation_->bytes() with two
uint64_t operands; a pair whose sum wraps passed the check and the pointer
computed from base + offset wrapped with it. Compare in the subtraction
form instead, so the check cannot overflow. Same predicate for every valid
(offset, size); only the wrapping pairs change outcome.

Requested on this PR on Sep 16 and again on Oct 3 so the fuzz target in
the same change does not report a known crash. Re-applies the change from
tensorflow#116631 that was reverted on Sep 18; the verifier-side check from tensorflow#123910
stays, so both layers reject the wrap.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:lite TF Lite related issues ready to pull PR ready for merge process size:S CL Change Size: Small

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

7 participants