Repository navigation
Fix arithmetic overflow in TFLite buffer bounds checks (re-apply #116204) - #123910
copybara-service[bot] merged 2 commits into
Conversation
There was a problem hiding this comment.
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.
|
Hi Team, |
dmiltr3
left a comment
There was a problem hiding this comment.
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
CustomOptionandConstantbuffer offsets) are validated against the total allocation bounds withintflite::Verify(e.g., usingsize > limit || offset > limit - sizeto prevent overflow in the check itself).
…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().
e7e6421 to
aa6431c
Compare
dmiltr3
left a comment
There was a problem hiding this comment.
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.
…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
64fca1f
into
tensorflow:master
…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.
…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.
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.