Skip to content

security: prevent integer overflows in shape multiplication and quantization allocation#3583

Open
deezsecc wants to merge 1 commit into
tensorflow:mainfrom
deezsecc:fix-allocator-integer-overflows
Open

security: prevent integer overflows in shape multiplication and quantization allocation#3583
deezsecc wants to merge 1 commit into
tensorflow:mainfrom
deezsecc:fix-allocator-integer-overflows

Conversation

@deezsecc

Copy link
Copy Markdown

Summary

This PR addresses the bug reported in #3582

Changes

memory_helpers.cc — Guard against shape dimension overflow

  • Added <limits> include.
  • Before multiplying element_count by each dimension, validates that the dimension is non-negative and that the multiplication would not exceed std::numeric_limits<int>::max(). Returns kTfLiteError immediately if either check fails.

micro_allocator.cc — Guard against quantization channel count overflow

  • Added <limits> and <new> includes.
  • Before computing the allocation size for per-channel quantization params (TfLiteIntArray), validates that channels is positive and that sizeof(TfLiteIntArray) + channels * sizeof(int) would not overflow size_t. Returns kTfLiteError with a diagnostic message if the check fails.

Testing

Two new tests added to micro_allocator_test.cc:

  • TestTensorShapeIntegerOverflow — constructs a flatbuffer tensor with shape [65536, 65536] (product overflows int) and asserts that InitializeTfLiteTensorFromFlatbuffer returns kTfLiteError.
  • TestQuantizationChannelsIntegerOverflow — on 32-bit targets (sizeof(size_t) == 4), crafts a malformed flatbuffer with a fake channel count of 0x3fffffff (triggers TfLiteIntArrayGetSizeInBytes overflow) and asserts that InitializeTfLiteTensorFromFlatbuffer returns kTfLiteError.

BUG=#3582

@deezsecc deezsecc requested a review from a team as a code owner May 31, 2026 05:28
@deezsecc deezsecc changed the title security: prevent integer overflows in shape multiplication and quat…ization allocation security: prevent integer overflows in shape multiplication and quantization allocation May 31, 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.

2 participants