Skip to content

fix: prevent EXP negative dimension size_t cast out of bounds access#3516

Open
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Sakura-501:fix-exp-negative-dimension-oob
Open

fix: prevent EXP negative dimension size_t cast out of bounds access#3516
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Sakura-501:fix-exp-negative-dimension-oob

Conversation

@Sakura-501
Copy link
Copy Markdown

@Sakura-501 Sakura-501 commented Apr 4, 2026

Vulnerability

This PR fixes an out of bounds memory access in the EXP kernel caused by converting a negative flat_size into size_t without validation.

A malformed tensor shape such as [-1] can make MatchingFlatSize return a negative value. ExpEval then casts that signed value to size_t, which expands it into a huge unsigned element count. reference_ops::Exp trusts that count and iterates far beyond the real input and output buffers, which can cause out of bounds read and write, process crash, and memory corruption.

Root cause

  • MatchingFlatSize can return a negative value for malformed tensor dimensions
  • ExpEval did not reject negative flat_size
  • the unchecked signed to unsigned conversion turned an invalid shape into a huge iteration bound
  • the EXP reference kernel then used that corrupted bound for memory access

Fix

  • add TF_LITE_ENSURE(context, flat_size >= 0) in ExpEval
  • reject malformed negative flat sizes before the size_t cast
  • keep the patch minimal and localized to the vulnerable path
  • add one regression test that verifies a negative dimension now returns kTfLiteError instead of crashing

Why the fix is correct

The bug exists at the conversion boundary between signed and unsigned length handling. Rejecting negative flat_size values prevents the dangerous cast and stops malformed tensor shapes from reaching the out of bounds loop in reference_ops::Exp.

Local verification

  • bazel test //tensorflow/lite/micro/kernels:exp_test
  • xcrun clang-format --dry-run -Werror tensorflow/lite/micro/kernels/exp.cc tensorflow/lite/micro/kernels/exp_test.cc
  • git diff --check
  • bash tensorflow/lite/micro/tools/ci_build/test_code_style.sh

BUG=None

The EXP kernel computed flat_size as a signed int and then cast it to size_t without validating negative values. A malformed tensor shape such as [-1] turns flat_size into a negative number, and the cast expands it into a huge unsigned length that drives reference_ops::Exp into out-of-bounds memory access.

This change adds a TF_LITE_ENSURE check in ExpEval so malformed negative flat sizes are rejected before the cast. It also adds a regression test that demonstrates the kernel now returns kTfLiteError for a negative dimension instead of crashing.
@Sakura-501 Sakura-501 requested a review from a team as a code owner April 4, 2026 17:04
@Sakura-501 Sakura-501 changed the title fix: reject negative EXP flat_size size_t cast OOB fix: prevent EXP negative dimension size_t cast out of bounds access Apr 5, 2026
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