From 2c6c6c4c86d922d45773594317522ae8e80bebe5 Mon Sep 17 00:00:00 2001 From: Uwez Khan Date: Fri, 16 Oct 2026 06:55:39 +0530 Subject: [PATCH 1/2] dnn: bound operator tensor indices in tflite populateNet --- modules/dnn/src/tflite/tflite_importer.cpp | 23 +++++++++++ modules/dnn/test/test_tflite_importer.cpp | 44 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/modules/dnn/src/tflite/tflite_importer.cpp b/modules/dnn/src/tflite/tflite_importer.cpp index 8e9daffaed..cd82079e3e 100644 --- a/modules/dnn/src/tflite/tflite_importer.cpp +++ b/modules/dnn/src/tflite/tflite_importer.cpp @@ -272,6 +272,28 @@ void TFLiteImporter::populateNet() CV_Assert(modelTensors); layouts.resize(modelTensors->size(), DNN_LAYOUT_UNKNOWN); + + // Tensor and operator code indices are read from the model as is and used + // to index modelTensors, opCodes and layouts, so bound them all up front. + const int numTensors = (int)modelTensors->size(); + for (int idx : *subgraph_inputs) + CV_Check(idx, 0 <= idx && idx < numTensors, "DNN/TFLite: subgraph input tensor index is out of range"); + if (subgraph->outputs()) + { + for (int idx : *subgraph->outputs()) + CV_Check(idx, 0 <= idx && idx < numTensors, "DNN/TFLite: subgraph output tensor index is out of range"); + } + for (const auto op : *subgraph_operators) + { + CV_Assert(op && op->inputs() && op->outputs()); + CV_CheckLT((size_t)op->opcode_index(), (size_t)opCodes->size(), "DNN/TFLite: operator code index is out of range"); + CV_CheckGT((int)op->outputs()->size(), 0, "DNN/TFLite: operator has no outputs"); + // -1 marks an omitted optional input + for (int idx : *op->inputs()) + CV_Check(idx, -1 <= idx && idx < numTensors, "DNN/TFLite: operator input tensor index is out of range"); + for (int idx : *op->outputs()) + CV_Check(idx, 0 <= idx && idx < numTensors, "DNN/TFLite: operator output tensor index is out of range"); + } size_t subgraph_inputs_size = subgraph_inputs->size(); std::vector inputsNames(subgraph_inputs_size); std::vector inputsShapes(subgraph_inputs_size); @@ -322,6 +344,7 @@ void TFLiteImporter::populateNet() if (newEngine) { + CV_CheckGT(all_operators_size, (size_t)0, "DNN/TFLite: subgraph has no operators"); const auto last_op = all_operators[all_operators_size - 1]; std::string type = EnumNameBuiltinOperator(getBuiltinCode(opCodes->Get(last_op->opcode_index()))); if (type == "CUSTOM") diff --git a/modules/dnn/test/test_tflite_importer.cpp b/modules/dnn/test/test_tflite_importer.cpp index 0b22a96155..4524ed9198 100644 --- a/modules/dnn/test/test_tflite_importer.cpp +++ b/modules/dnn/test/test_tflite_importer.cpp @@ -564,6 +564,50 @@ TEST_P(Test_TFLite, pose_landmark) } } +// x -> RELU -> y; byte 212 holds the operator's outputs[0] tensor index. +static const uchar tfliteReluModel[] = { + 0x08, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0xa6, 0xff, 0xff, 0xff, + 0x03, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, + 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x18, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x0c, 0x00, 0x0e, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, + 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x13, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x0e, 0x00, 0x18, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, + 0x10, 0x00, 0x14, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, + 0x2c, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, + 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, + 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, + 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x79, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, + 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, +}; + +TEST(Test_TFLite_Importer, reject_out_of_range_tensor_index) +{ + std::vector model(tfliteReluModel, tfliteReluModel + sizeof(tfliteReluModel)); + ASSERT_EQ(model[212], 1); + EXPECT_NO_THROW(readNetFromTFLite(model)); + + // The subgraph has 2 tensors, so 2 is the first out-of-range index. + for (int idx : {2, 1000}) + { + model[212] = (uchar)(idx & 0xff); + model[213] = (uchar)(idx >> 8); + EXPECT_THROW(readNetFromTFLite(model), cv::Exception) << "output tensor index " << idx; + } +} + INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets()); }} // namespace From 104c5af4eadba3149a20b8d927ee23dd51b2b817 Mon Sep 17 00:00:00 2001 From: Uwez Khan Date: Sat, 17 Oct 2026 12:43:48 +0530 Subject: [PATCH 2/2] dnn: load tflite tensor index test models from opencv_extra --- modules/dnn/test/test_tflite_importer.cpp | 39 ++--------------------- 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/modules/dnn/test/test_tflite_importer.cpp b/modules/dnn/test/test_tflite_importer.cpp index 4524ed9198..28e1c3678d 100644 --- a/modules/dnn/test/test_tflite_importer.cpp +++ b/modules/dnn/test/test_tflite_importer.cpp @@ -564,46 +564,13 @@ TEST_P(Test_TFLite, pose_landmark) } } -// x -> RELU -> y; byte 212 holds the operator's outputs[0] tensor index. -static const uchar tfliteReluModel[] = { - 0x08, 0x00, 0x00, 0x00, 0x54, 0x46, 0x4c, 0x33, 0xa6, 0xff, 0xff, 0xff, - 0x03, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, - 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x18, 0x00, 0x00, 0x00, 0x04, 0x00, 0x04, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x0c, 0x00, 0x0e, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, - 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x13, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x0e, 0x00, 0x18, 0x00, 0x04, 0x00, 0x08, 0x00, 0x0c, 0x00, - 0x10, 0x00, 0x14, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, - 0x2c, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x6d, 0x61, 0x69, 0x6e, - 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x5c, 0x00, 0x00, 0x00, - 0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, - 0x10, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x79, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x0c, 0x00, 0x04, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x08, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, - 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, -}; - +// x -> RELU -> y, where the operator's outputs[0] tensor index is out of range. +// The subgraph has 2 tensors, so 2 is the first out-of-range index. TEST(Test_TFLite_Importer, reject_out_of_range_tensor_index) { - std::vector model(tfliteReluModel, tfliteReluModel + sizeof(tfliteReluModel)); - ASSERT_EQ(model[212], 1); - EXPECT_NO_THROW(readNetFromTFLite(model)); - - // The subgraph has 2 tensors, so 2 is the first out-of-range index. for (int idx : {2, 1000}) { - model[212] = (uchar)(idx & 0xff); - model[213] = (uchar)(idx >> 8); + std::string model = findDataFile(format("dnn/tflite/invalid_tensor_index_%d.tflite", idx)); EXPECT_THROW(readNetFromTFLite(model), cv::Exception) << "output tensor index " << idx; } }