Skip to content

Use overflow-safe multiplication in RuntimeShape::FlatSize - #116206

Closed
TristanInSec wants to merge 2 commits into
tensorflow:masterfrom
TristanInSec:fix-flatsize-overflow
Closed

TristanInSec wants to merge 2 commits into
tensorflow:masterfrom
TristanInSec:fix-flatsize-overflow

Conversation

@TristanInSec

Copy link
Copy Markdown
Contributor

Summary

Accumulate the dimension product in int64_t and check that the result
fits in int after each multiplication. Update both the TFLite and MLIR
copies of RuntimeShape::FlatSize.

Test plan

  • Existing TFLite and MLIR tests pass
  • Shapes with large dimensions that would overflow int32 are handled safely

@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Apr 17, 2026
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Apr 21, 2026
@keerthanakadiri keerthanakadiri modified the milestone: assigned Apr 21, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Apr 21, 2026
@keerthanakadiri keerthanakadiri added the comp:lite TF Lite related issues label Apr 21, 2026
@keerthanakadiri

Copy link
Copy Markdown
Contributor

Hi @TristanInSec , Can you please resolve the conflicts. Thank you!

@TristanInSec
TristanInSec force-pushed the fix-flatsize-overflow branch from 90bd6d8 to 2bcdac8 Compare May 11, 2026 21:49
@TristanInSec

Copy link
Copy Markdown
Contributor Author

Hi @veblush @advaitjain,
Can you please review?
Thanks.
Best,
Tristan

@TristanInSec
TristanInSec force-pushed the fix-flatsize-overflow branch from 2bcdac8 to 30f816f Compare May 18, 2026 20:49
@keerthanakadiri

Copy link
Copy Markdown
Contributor

Hi @TristanInSec , A review request has been submitted for this PR. Once approved, it will be ready for the next stage of the process. Thank you!

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

Thank you for the contribution to fix this integer overflow issue in FlatSize!

We reviewed the PR and have a few requests before this is ready to be merged:

  1. Use TFLITE_CHECK_LE instead of debug assertions:
    Because this PR addresses a security vulnerability (where a wrapped-around integer value can cause downstream heap buffer allocations to be sized incorrectly, leading to heap buffer overflows), we must use TFLITE_CHECK_LE instead of TFLITE_DCHECK_LE (or debug-only asserts). TFLITE_CHECK_LE will ensure that the bounds check is preserved in release builds where debug assertions are compiled out.

  2. Negative dimensions safety:
    Please add a check to verify that all dimensions are non-negative (dims_data[i] >= 0) before multiplying, so negative values cannot wrap the product back under the limits.

  3. Mirror the MLIR copy:
    The MLIR implementation of RuntimeShape under tensorflow/compiler/mlir/lite/ must be updated identically to maintain consistency and satisfy our IfThisThenThat linter checks.

  4. Add Unit Tests:
    Please add test cases (e.g. in runtime_shape_test.cc) asserting that inputs that would cause integer overflow are safely intercepted by the check.

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Jun 24, 2026

@TristanInSec TristanInSec left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough review! I've addressed your points:

  1. TFLITE_CHECK_LE: Changed TFLITE_CHECK(flat_size <= ...) to TFLITE_CHECK_LE(flat_size, ...) in FlatSize() for idiomatic release-build enforcement.

  2. Negative dimensions: Already handled -- CheckedMul(size_t, int32_t) explicitly rejects b < 0 (line 38 in runtime_shape.cc), and the TestCheckedFlatSizeRejectsNegativeDim test verifies this. Added a TestFlatSizeAbortsOnNegativeDim death test confirming FlatSize() itself aborts on negative dims.

  3. MLIR mirror: Already done in the original PR -- both runtime_shape.cc and runtime_shape.h under tensorflow/compiler/mlir/lite/ are updated identically.

  4. Unit tests: Already included 13 tests in the original PR (TestCheckedFlatSize*, TestCheckedNumElementsInRange*, TestCheckedSizeToDimension, etc.). Added 2 new death tests confirming FlatSize() aborts on overflow and negative dims.

Pushed in d7bb1b8.

@dmiltr3
dmiltr3 self-requested a review June 25, 2026 23:40

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

Hi @TristanInSec,

Your code changes look solid, secure, and address all concerns (including correct production TFLITE_CHECK verification, negative shape validation, and unit tests).

To move to the next stage, could you please resolve the merge conflicts on this branch? Once they are resolved, we'll run the import checks and merge it.

Thank you!

Replace unchecked integer multiplication loop in FlatSize() with
CheckedFlatSize() + TFLITE_CHECK, preventing silent integer overflow
on large tensor shapes. Add TFLITE_CHECK_LE to reject shapes exceeding
INT_MAX. Add death tests for overflow and negative dimension cases.
@TristanInSec
TristanInSec force-pushed the fix-flatsize-overflow branch from d7bb1b8 to 8144383 Compare June 25, 2026 23:59
@nithyak0204
nithyak0204 requested a review from dmiltr3 June 26, 2026 07:30

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

The compilation fails when building the MLIR copy of RuntimeShape because TFLITE_CHECK_LE is only defined in the TFLite-internal compatibility.h and is not defined in the MLIR compatibility header.

How to Fix The Error
To fix this, the contributor needs to define TFLITE_CHECK_LE in
tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h
.

Add the following block to
tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h
(just below the definition they added for TFLITE_CHECK):

  #ifndef TFLITE_CHECK_LE
  #define TFLITE_CHECK_LE(x, y) ((x) <= (y)) ? (void)0 : TFLITE_ABORT
  #endif

Here is a diff showing the exact fix:

  diff --git a/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h b/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h
  index 9f7f298283cbbd..a0be2e724a3501 100644
  --- a/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h
  +++ b/tensorflow/compiler/mlir/lite/kernels/internal/compatibility_macros.h
  @@ -62,6 +62,10 @@ limitations under the License.
   #define TFLITE_CHECK(condition) (condition) ? (void)0 : TFLITE_ABORT
   #endif
   
  +#ifndef TFLITE_CHECK_LE
  +#define TFLITE_CHECK_LE(x, y) ((x) <= (y)) ? (void)0 : TFLITE_ABORT
  +#endif
  +
   // LINT.ThenChange(//tensorflow/lite/kernels/internal/compatibility.h)
   
   #endif  // TENSORFLOW_COMPILER_MLIR_LITE_KERNELS_INTERNAL_COMPATIBILITY_MACROS_H_

The MLIR copy of RuntimeShape includes TFLITE_CHECK_LE via
runtime_shape.h but only the TFLite-internal compatibility.h
defined it.  Mirror the macro here so the MLIR build succeeds.
@dmiltr3
dmiltr3 self-requested a review June 26, 2026 20:55
@google-ml-butler google-ml-butler Bot added the kokoro:force-run Tests on submitted change label Jun 26, 2026
@google-ml-butler google-ml-butler Bot added the ready to pull PR ready for merge process label Jun 26, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Jun 26, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jun 26, 2026
copybara-service Bot pushed a commit to google-ai-edge/LiteRT that referenced this pull request Jun 26, 2026
Imported from GitHub PR tensorflow/tensorflow#116206

## Summary

Accumulate the dimension product in `int64_t` and check that the result
fits in `int` after each multiplication. Update both the TFLite and MLIR
copies of `RuntimeShape::FlatSize`.

## Test plan

- [ ] Existing TFLite and MLIR tests pass
- [ ] Shapes with large dimensions that would overflow int32 are handled safely
Copybara import of the project:

--
814438394ec6ddd1fa3da29c63da3f76ecd1d753 by TristanInSec <[email protected]>:

Use overflow-safe multiplication in RuntimeShape::FlatSize

Replace unchecked integer multiplication loop in FlatSize() with
CheckedFlatSize() + TFLITE_CHECK, preventing silent integer overflow
on large tensor shapes. Add TFLITE_CHECK_LE to reject shapes exceeding
INT_MAX. Add death tests for overflow and negative dimension cases.

--
aa9a5c687c95b01f9baff6b145c6309689731842 by TristanInSec <[email protected]>:

Add TFLITE_CHECK_LE to MLIR compatibility_macros.h

The MLIR copy of RuntimeShape includes TFLITE_CHECK_LE via
runtime_shape.h but only the TFLite-internal compatibility.h
defined it.  Mirror the macro here so the MLIR build succeeds.

Merging this change closes #116206

FUTURE_COPYBARA_INTEGRATE_REVIEW=tensorflow/tensorflow#116206 from TristanInSec:fix-flatsize-overflow aa9a5c687c95b01f9baff6b145c6309689731842
PiperOrigin-RevId: 938758630
@dmiltr3 dmiltr3 closed this Jun 26, 2026
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Closed/Rejected in PR Queue Jun 26, 2026
@google-ml-butler google-ml-butler Bot removed awaiting review Pull request awaiting review ready to pull PR ready for merge process labels Jun 26, 2026
@dmiltr3

dmiltr3 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Hi @TristanInSec,,

Thanks so much for your patch to address potential overflows in RuntimeShape::FlatSize()! We really appreciate you looking out for the stability of the library.

After review, we've decided not to merge this particular change. The rationale is that FlatSize() is intended to be a highly optimized, fast path, while the CheckedFlatSize() function exists for scenarios where overflow protection is explicitly required. The comments in runtime_shape.h aim to clarify this distinction. Introducing checks into FlatSize() would unfortunately negate its performance advantage.

Also, it's worth noting that per TensorFlow's security policy (https://github.com/tensorflow/tensorflow/security), this type of integer overflow is currently not treated as a security bug.

Thank you again for your contribution. We value your input and encourage you to continue participating in the TensorFlow community.

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 size:S CL Change Size: Small

Projects

Status: Closed/Rejected

Development

Successfully merging this pull request may close these issues.

5 participants