Skip to content

Fix stack buffer overflow in TFLite Select reference kernel for rank > 8 tensors - #125223

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
prasanna8585:fix/tflite-select-stack-overflow
Aug 14, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
prasanna8585:fix/tflite-select-stack-overflow

Conversation

@prasanna8585

Copy link
Copy Markdown
Contributor

BroadcastSelectSimple in tensorflow/lite/kernels/internal/reference/select.h sized its cond_strides/x_strides/y_strides/o_strides/o_shape metadata arrays to a fixed kMaxRank=8, guarded only by a TFLITE_DCHECK_LE that compiles out entirely in release builds. dims_count is derived directly from the condition/x/y/output tensor shapes of a Select/SelectV2 op -- which come from the .tflite model file itself. A model declaring any of these tensors with rank > 8 writes past the end of all five fixed-size stack arrays.

Confirmed with AddressSanitizer against a standalone, faithful extraction of the real, unmodified function: a rank-9 input using real broadcasting (alternating size-1 dimensions, so the loop-fusion optimization can't collapse the effective rank) reliably triggers a stack-buffer-overflow abort. The same harness, patched with this fix, runs clean.

This is the identical bug class as broadcast_loop.h's BroadcastBinaryOpSimple, fixed earlier the same day. This PR applies the same fix (absl::InlinedVector sized to the real rank, instead of a fixed-size C array). No BUILD dependency changes needed -- select.h and broadcast_loop.h share both enclosing cc_library targets (reference_base, legacy_reference_base), which already picked up absl/container:inlined_vector from that fix. Also drops the now-redundant DCHECKs in BroadcastSelect5DSlow, which only forwards to the now-safe BroadcastSelectSimple.

Added a rank-9 alternating-broadcast regression test (select_test.cc) matching broadcast_loop_test.cc's style, including full correctness verification via an independently-computed per-dimension index decomposition (not just an absence-of-crash check).

…> 8 tensors

BroadcastSelectSimple in select.h sized its cond_strides/x_strides/
y_strides/o_strides/o_shape metadata arrays to a fixed kMaxRank=8,
guarded only by a TFLITE_DCHECK_LE that compiles out entirely in
release builds. dims_count is derived directly from the condition/x/y/
output tensor shapes of a Select/SelectV2 op, which come from the
.tflite model file itself. A model declaring any of these tensors with
rank > 8 writes past the end of all five fixed-size stack arrays.

Confirmed with AddressSanitizer against a standalone, faithful
extraction of the real, unmodified function: a rank-9 input (using
real broadcasting -- alternating size-1 dimensions -- so the loop-
fusion optimization can't collapse the rank down) reliably triggers a
stack-buffer-overflow abort. The same standalone harness, patched with
this fix, runs clean.

Fix mirrors the identical bug class and fix (absl::InlinedVector sized
to the real rank, instead of a fixed-size C array) that
broadcast_loop.h received earlier the same day for its own
BroadcastBinaryOpSimple. No BUILD dependency changes needed: select.h
and broadcast_loop.h share both enclosing cc_library targets
(reference_base, legacy_reference_base), which already picked up
absl/container:inlined_vector from that fix. Also drops the now-
redundant DCHECKs in BroadcastSelect5DSlow, which only forwards to the
now-safe BroadcastSelectSimple.

Adds a rank-9 alternating-broadcast regression test (select_test.cc)
matching broadcast_loop_test.cc's style, including full correctness
verification via an independently-computed per-dimension index
decomposition, not just an absence-of-crash check.
@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Aug 13, 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 addresses a potential stack buffer overflow vulnerability in the BroadcastSelectSimple reference kernel by replacing fixed-size C arrays (which were limited to a rank of 8) with absl::InlinedVector dynamically sized to the actual tensor rank. This allows the kernel to safely support inputs with a rank greater than 8. A regression test has been added in select_test.cc to verify this fix, along with its corresponding BUILD target. I have no feedback to provide as there are no review comments to evaluate.

@nithyak0204
nithyak0204 requested a review from a team August 13, 2026 10:15
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Aug 13, 2026
@nithyak0204 nithyak0204 added the comp:lite TF Lite related issues label Aug 13, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Aug 13, 2026
@dmiltr3
dmiltr3 self-requested a review August 13, 2026 15:12

@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.

LGTM! Thanks for fixing the stack buffer overflow in TFLite Select reference kernel for rank > 8 tensors and for adding the regression test.

@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 13, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Aug 13, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Aug 13, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Aug 14, 2026
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Aug 14, 2026
…el for rank > 8 tensors

Imported from GitHub PR tensorflow/tensorflow#125223

`BroadcastSelectSimple` in `tensorflow/lite/kernels/internal/reference/select.h` sized its `cond_strides`/`x_strides`/`y_strides`/`o_strides`/`o_shape` metadata arrays to a fixed `kMaxRank=8`, guarded only by a `TFLITE_DCHECK_LE` that compiles out entirely in release builds. `dims_count` is derived directly from the condition/x/y/output tensor shapes of a `Select`/`SelectV2` op -- which come from the `.tflite` model file itself. A model declaring any of these tensors with rank > 8 writes past the end of all five fixed-size stack arrays.

Confirmed with AddressSanitizer against a standalone, faithful extraction of the real, unmodified function: a rank-9 input using real broadcasting (alternating size-1 dimensions, so the loop-fusion optimization can't collapse the effective rank) reliably triggers a stack-buffer-overflow abort. The same harness, patched with this fix, runs clean.

This is the identical bug class as `broadcast_loop.h`'s `BroadcastBinaryOpSimple`, fixed earlier the same day. This PR applies the same fix (`absl::InlinedVector` sized to the real rank, instead of a fixed-size C array). No `BUILD` dependency changes needed -- `select.h` and `broadcast_loop.h` share both enclosing `cc_library` targets (`reference_base`, `legacy_reference_base`), which already picked up `absl/container:inlined_vector` from that fix. Also drops the now-redundant `DCHECK`s in `BroadcastSelect5DSlow`, which only forwards to the now-safe `BroadcastSelectSimple`.

Added a rank-9 alternating-broadcast regression test (`select_test.cc`) matching `broadcast_loop_test.cc`'s style, including full correctness verification via an independently-computed per-dimension index decomposition (not just an absence-of-crash check).
Copybara import of the project:

--
7187a3181353728d53b0b5d236f1fff0c9775bc3 by prasanna8585 <[email protected]>:

Fix stack buffer overflow in TFLite Select reference kernel for rank > 8 tensors

BroadcastSelectSimple in select.h sized its cond_strides/x_strides/
y_strides/o_strides/o_shape metadata arrays to a fixed kMaxRank=8,
guarded only by a TFLITE_DCHECK_LE that compiles out entirely in
release builds. dims_count is derived directly from the condition/x/y/
output tensor shapes of a Select/SelectV2 op, which come from the
.tflite model file itself. A model declaring any of these tensors with
rank > 8 writes past the end of all five fixed-size stack arrays.

Confirmed with AddressSanitizer against a standalone, faithful
extraction of the real, unmodified function: a rank-9 input (using
real broadcasting -- alternating size-1 dimensions -- so the loop-
fusion optimization can't collapse the rank down) reliably triggers a
stack-buffer-overflow abort. The same standalone harness, patched with
this fix, runs clean.

Fix mirrors the identical bug class and fix (absl::InlinedVector sized
to the real rank, instead of a fixed-size C array) that
broadcast_loop.h received earlier the same day for its own
BroadcastBinaryOpSimple. No BUILD dependency changes needed: select.h
and broadcast_loop.h share both enclosing cc_library targets
(reference_base, legacy_reference_base), which already picked up
absl/container:inlined_vector from that fix. Also drops the now-
redundant DCHECKs in BroadcastSelect5DSlow, which only forwards to the
now-safe BroadcastSelectSimple.

Adds a rank-9 alternating-broadcast regression test (select_test.cc)
matching broadcast_loop_test.cc's style, including full correctness
verification via an independently-computed per-dimension index
decomposition, not just an absence-of-crash check.

Merging this change closes #125223

PiperOrigin-RevId: 964675852
copybara-service Bot pushed a commit that referenced this pull request Aug 14, 2026
Extract CanUseEigenConvolution (the Eigen predicate plus the layout
checks EmitConvolutionThunk applied inline) and use it from both the
thunk emitter and FusionWrapper, which now wraps any convolution Eigen
cannot take so it lowers through the MLIR elemental convolution. This
covers non-canonical dimension numbers (including inside while bodies),
window reversal, more than three spatial dims, and non-monotonic layouts.
Also fixes convolution indexing map composition for window padding and
bounds checking for padded spatial operand accesses in the elemental loop
emitter.

Reverts a65bb0c

FUTURE_COPYBARA_INTEGRATE_REVIEW=#125223 from prasanna8585:fix/tflite-select-stack-overflow 7187a31
PiperOrigin-RevId: 958406955
copybara-service Bot pushed a commit that referenced this pull request Aug 14, 2026
… settings in favor of the EffortLevel enum.

##### Why?
Simplifies flag input handling by removing multiple ways to specify the same thing.

FUTURE_COPYBARA_INTEGRATE_REVIEW=#125223 from prasanna8585:fix/tflite-select-stack-overflow 7187a31
PiperOrigin-RevId: 821862460
@copybara-service
copybara-service Bot merged commit 3f7600c into tensorflow:master Aug 14, 2026
20 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Aug 14, 2026
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:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

5 participants