Skip to content

Fix OOB read in Gather kernel via runtime bounds check on coordinate indices - #3638

Open
TristanInSec wants to merge 1 commit into
tensorflow:mainfrom
TristanInSec:fix/gather-oob-bounds-check
Open

Fix OOB read in Gather kernel via runtime bounds check on coordinate indices#3638
TristanInSec wants to merge 1 commit into
tensorflow:mainfrom
TristanInSec:fix/gather-oob-bounds-check

Conversation

@TristanInSec

Copy link
Copy Markdown

Summary

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 cause an out-of-bounds read from heap memory.

Fix

Replace the TFLITE_DCHECK pair at lines 85-86 with a runtime bounds check:

const CoordsT idx = coords_data[batch * coord_size + coord];
if (idx < 0 || idx >= axis_size) {
  return kTfLiteError;
}

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):

"Out-of-bounds Memory Access: If an input tensor contains indices or offsets generated at runtime (e.g., in GATHER, STRIDED_SLICE), these should be bounds-checked at runtime using a raw if (!valid) return kTfLiteError;."

Per Section 4 (Preventing Buffer Overflows in Eval):

"If the overflow comes from invalid control data (like an input tensor providing indices), it should be validated in Eval to prevent memory corruption. Use raw returns."

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 kTfLiteError instead of reading past the input buffer.

Fixes GHSA-cpmh-q338-mv33

…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.
@TristanInSec
TristanInSec requested a review from a team as a code owner July 26, 2026 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant