mirror of
https://github.com/opencv/opencv.git
synced 2026-11-17 14:58:10 -06:00
dnn: validate TFLite tensor buffer bounds 🤖🤖🤖 - #30261 OpenCV Extra: https://github.com/opencv/opencv_extra/pull/1430 ### What this changes `TFLiteImporter::parseTensor` now checks the buffer index and required tensor bytes before constructing a `Mat` over model data. It rejects negative or overflowing shapes and unsupported sparse tensors. Absent or empty data remains a runtime tensor. ### Why A TFLite model controls tensor shape, type, and buffer index. On 4.x at `802cf95aa3d42816995695bf68a86b3532f12760`, a tensor can claim more bytes than its selected buffer contains. The `tfliteShortBuffer` model included in this PR caused a stock Release SIGSEGV in three observed runs. The new check rejects it before the `Mat` wraps short data. Associated upstream issue: none. Related DNN parser work includes #29371 and the closed, unmerged #29336; neither fixes this shape-to-buffer check. ### Tests and reproduction This PR includes 15 small inline control and malformed TFLite models, importer tests, and `modules/dnn/test/generate_tflite_tensor_bounds_models.py`. The generator uses OpenCV's in-tree TFLite schema and `flatc` 25.12.19. The focused tests need no `opencv_extra` download. To reproduce with the bytes in this PR, copy `modules/dnn/test/test_tflite_importer.cpp` and `modules/dnn/test/tflite_tensor_bounds_models.inc` from this PR into an otherwise unmodified 4.x checkout at the commit above. Build `opencv_test_dnn`, then run `./build/bin/opencv_test_dnn --gtest_filter=Test_TFLite_Importer.ShortInt32Buffer`. Apply the full PR and run `./build/bin/opencv_test_dnn --gtest_filter='Test_TFLite_Importer.*'`. On Linux, an earlier tree that combined this patch with a separate ONNX importer fix passed all 15 focused TFLite tests and the 245-test `*Test_TFLite.*:*Test_ONNX_layers.*` filter in Release and GCC ASan builds. The compiled source and test files in this PR are byte-identical to that tree; only a generator comment changed afterwards, and rerunning the generator reproduced the include byte-for-byte. This branch has not been built on its own. Clang, MSVC, MinGW, and 5.x runtime tests have not run. I used an AI coding tool to help prepare this change and will answer reviewer questions and address requested fixes within a day, following OpenCV's AI usage guideline. A separate reproducer archive is available on request; no files are attached to this PR. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable The test models are inline in this PR; no opencv_extra change is proposed. - [ ] The feature is well documented and sample code can be built with the project CMake