Repository navigation
Fix stack buffer overflow in TFLite Select reference kernel for rank > 8 tensors - #125223
Merged
copybara-service[bot] merged 1 commit intoAug 14, 2026
Conversation
…> 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.
Contributor
There was a problem hiding this comment.
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.
dmiltr3
approved these changes
Aug 13, 2026
dmiltr3
left a comment
Contributor
There was a problem hiding this comment.
LGTM! Thanks for fixing the stack buffer overflow in TFLite Select reference kernel for rank > 8 tensors and for adding the regression test.
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
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.
BroadcastSelectSimpleintensorflow/lite/kernels/internal/reference/select.hsized itscond_strides/x_strides/y_strides/o_strides/o_shapemetadata arrays to a fixedkMaxRank=8, guarded only by aTFLITE_DCHECK_LEthat compiles out entirely in release builds.dims_countis derived directly from the condition/x/y/output tensor shapes of aSelect/SelectV2op -- which come from the.tflitemodel 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'sBroadcastBinaryOpSimple, fixed earlier the same day. This PR applies the same fix (absl::InlinedVectorsized to the real rank, instead of a fixed-size C array). NoBUILDdependency changes needed --select.handbroadcast_loop.hshare both enclosingcc_librarytargets (reference_base,legacy_reference_base), which already picked upabsl/container:inlined_vectorfrom that fix. Also drops the now-redundantDCHECKs inBroadcastSelect5DSlow, which only forwards to the now-safeBroadcastSelectSimple.Added a rank-9 alternating-broadcast regression test (
select_test.cc) matchingbroadcast_loop_test.cc's style, including full correctness verification via an independently-computed per-dimension index decomposition (not just an absence-of-crash check).