Repository navigation
Use overflow-safe multiplication in RuntimeShape::FlatSize - #116206
TristanInSec wants to merge 2 commits into
Conversation
|
Hi @TristanInSec , Can you please resolve the conflicts. Thank you! |
90bd6d8 to
2bcdac8
Compare
|
Hi @veblush @advaitjain, |
2bcdac8 to
30f816f
Compare
|
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
left a comment
There was a problem hiding this comment.
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:
-
Use
TFLITE_CHECK_LEinstead 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 useTFLITE_CHECK_LEinstead ofTFLITE_DCHECK_LE(or debug-only asserts).TFLITE_CHECK_LEwill ensure that the bounds check is preserved in release builds where debug assertions are compiled out. -
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. -
Mirror the MLIR copy:
The MLIR implementation ofRuntimeShapeundertensorflow/compiler/mlir/lite/must be updated identically to maintain consistency and satisfy ourIfThisThenThatlinter checks. -
Add Unit Tests:
Please add test cases (e.g. inruntime_shape_test.cc) asserting that inputs that would cause integer overflow are safely intercepted by the check.
TristanInSec
left a comment
There was a problem hiding this comment.
Thanks for the thorough review! I've addressed your points:
-
TFLITE_CHECK_LE: Changed
TFLITE_CHECK(flat_size <= ...)toTFLITE_CHECK_LE(flat_size, ...)inFlatSize()for idiomatic release-build enforcement. -
Negative dimensions: Already handled --
CheckedMul(size_t, int32_t)explicitly rejectsb < 0(line 38 in runtime_shape.cc), and theTestCheckedFlatSizeRejectsNegativeDimtest verifies this. Added aTestFlatSizeAbortsOnNegativeDimdeath test confirmingFlatSize()itself aborts on negative dims. -
MLIR mirror: Already done in the original PR -- both
runtime_shape.ccandruntime_shape.hundertensorflow/compiler/mlir/lite/are updated identically. -
Unit tests: Already included 13 tests in the original PR (
TestCheckedFlatSize*,TestCheckedNumElementsInRange*,TestCheckedSizeToDimension, etc.). Added 2 new death tests confirmingFlatSize()aborts on overflow and negative dims.
Pushed in d7bb1b8.
dmiltr3
left a comment
There was a problem hiding this comment.
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.
d7bb1b8 to
8144383
Compare
dmiltr3
left a comment
There was a problem hiding this comment.
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
#endifHere 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.
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
|
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. |
Summary
Accumulate the dimension product in
int64_tand check that the resultfits in
intafter each multiplication. Update both the TFLite and MLIRcopies of
RuntimeShape::FlatSize.Test plan