Fix OOB read in Gather kernel via runtime bounds check on coordinate indices - #3638
Open
TristanInSec wants to merge 1 commit into
Open
Fix OOB read in Gather kernel via runtime bounds check on coordinate indices#3638TristanInSec wants to merge 1 commit into
TristanInSec wants to merge 1 commit into
Conversation
…indices The Gather kernel uses TFLITE_DCHECK_GE/TFLITE_DCHECK_LT to validate coordinate indices, but these compile to no-ops in release builds (NDEBUG). This allows attacker-controlled indices from an input tensor to read arbitrary heap memory past the input buffer. Per the Error Handling Guide, control data from input tensors evaluated at runtime should use raw if/return validation in Eval to prevent memory corruption. This matches the pattern in gather_nd.cc which already validates from_pos at runtime. Replace the TFLITE_DCHECK pair with a runtime bounds check that returns kTfLiteError for out-of-range indices, consistent with Section 4 (Preventing Buffer Overflows in Eval) of the guide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Gather kernel uses
TFLITE_DCHECK_GE/TFLITE_DCHECK_LTto validate coordinate indices, but these compile to no-ops in release builds (NDEBUG). This allows attacker-controlled indices from an input tensor to cause an out-of-bounds read from heap memory.Fix
Replace the TFLITE_DCHECK pair at lines 85-86 with a runtime bounds check:
This replaces the previous PR #3534 which was closed for not following the Error Handling Guide.
Alignment with Error Handling Guide
Per Section 2 (Phase 2: Execution - Eval):
Per Section 4 (Preventing Buffer Overflows in Eval):
The guide explicitly names GATHER as an example. The sibling
gather_nd.cc(line 155) already validates bounds at runtime with a proper error return.Testing
Verified with ASan-enabled build: crafted model with out-of-range gather index now returns
kTfLiteErrorinstead of reading past the input buffer.Fixes GHSA-cpmh-q338-mv33