OpenVINO™ is an open source toolkit for optimizing and deploying AI inference
C++ aicomputer-visiondeep-learningdeploy-aidiffusion-modelsgenerative-aigood-first-issueinferencellm-inferencenatural-language-processingnlpopenvinooptimize-aiperformance-boostrecommendation-systemspeech-recognitionstable-diffusiontransformersyolo
Out-of-bounds Write in ONNX Loop Converter body_inputs Access leads to Crash / DoS (#34762)
Szymon Gutaj728dd933de6d
diff --git a/src/frontends/onnx/frontend/src/op/loop.cpp b/src/frontends/onnx/frontend/src/op/loop.cppindex 31570e6c8c1..8c497c1d528 100644--- a/src/frontends/onnx/frontend/src/op/loop.cpp+++ b/src/frontends/onnx/frontend/src/op/loop.cpp@@ -100,7 +100,14 @@ namespace detail {ov::OutputVector loop_legacy(const ov::frontend::onnx::Node& node) {const auto& ng_inputs = node.get_ov_inputs();- const ov::OutputVector loop_carried_dependencies{std::next(ng_inputs.begin(), 2), ng_inputs.end()};+ constexpr size_t control_inputs_count = 2;++ FRONT_END_GENERAL_CHECK(+ ng_inputs.size() >= control_inputs_count,+ "Expecting at least two canonical inputs for Loop op: trip count and termination condition");++ const ov::OutputVector loop_carried_dependencies{std::next(ng_inputs.begin(), control_inputs_count),+ ng_inputs.end()};const auto& subgraphs = node.get_subgraphs();auto body_graph_it = subgraphs.find("body");@@ -109,10 +116,32 @@ ov::OutputVector loop_legacy(const ov::frontend::onnx::Node& node) {auto body_outputs = body_graph->get_ov_outputs();const auto& body_inputs = body_graph->get_ng_parameters();+ // NOTE: We're relaxing the check on body_inputs size here to allow extra parameters that are not loop-carried+ // dependencies and are not control inputs (iteration number and termination condition). These extra parameters are+ // expected to be invariant inputs that are wired from the parent graph via Loop::set_invariant_input, and their+ // presence does not violate ONNX spec as long as the loop body graph is well-formed and can be executed by ONNX+ // Runtime.+ CHECK_VALID_NODE(node,+ body_inputs.size() >= control_inputs_count &&+ body_inputs.size() - control_inputs_count >= loop_carried_dependencies.size(),+ "The provided loop body graph canonical inputs size (",+ body_inputs.size(),+ "), does not match the sum of loop carried dependencies "+ "and two mandatory inputs (",+ loop_carried_dependencies.size() + control_inputs_count,+ ")");++ CHECK_VALID_NODE(node,+ body_outputs.size() >= 1 && body_outputs.size() - 1 >= loop_carried_dependencies.size(),+ "The provided loop body graph outputs size (",+ body_outputs.size(),+ ") is smaller than number of outputs. Required at least: ",+ loop_carried_dependencies.size() + 1);+// Infer loop body inputs' element type based on carried dependenciesfor (size_t i = 0; i < loop_carried_dependencies.size(); i++) {- body_inputs[i + 2]->set_element_type(loop_carried_dependencies[i].get_element_type());- body_inputs[i + 2]->set_partial_shape(loop_carried_dependencies[i].get_partial_shape());+ body_inputs[i + control_inputs_count]->set_element_type(loop_carried_dependencies[i].get_element_type());+ body_inputs[i + control_inputs_count]->set_partial_shape(loop_carried_dependencies[i].get_partial_shape());}// optional inputs@@ -165,30 +194,13 @@ ov::OutputVector loop_legacy(const ov::frontend::onnx::Node& node) {body_outputs[0] = v0::Constant::create(ov::element::boolean, {1}, {true});// Construct body_params without the condition parameter (body_inputs[1])body_params = ov::ParameterVector{body_inputs[0]};- body_params.insert(body_params.end(), body_inputs.begin() + 2, body_inputs.end());+ body_params.insert(body_params.end(), body_inputs.begin() + control_inputs_count, body_inputs.end());needs_condition_param = false;} else {// Construct body_params with all body_inputsbody_params = ov::ParameterVector(body_inputs.begin(), body_inputs.end());}- CHECK_VALID_NODE(node,- body_inputs.size() >= loop_carried_dependencies.size() + 2,- "The provided loop body graph inputs size (",- body_inputs.size(),- "), is not greater than the sum of loop carried dependencies "- "and two mandatory"- " inputs (",- loop_carried_dependencies.size() + 2,- ")");-- CHECK_VALID_NODE(node,- body_outputs.size() >= loop_carried_dependencies.size() + 1,- "The provided loop body graph outputs size (",- body_outputs.size(),- ") is not greater than number of outputs. Required at least: ",- loop_carried_dependencies.size() + 1);-const auto body = std::make_shared<ov::Model>(body_outputs, body_params);auto loop = std::make_shared<v5::Loop>(trip_count, termination_cond);v5::Loop::SpecialBodyPorts spec_ports{0, 0};@@ -200,7 +212,7 @@ ov::OutputVector loop_legacy(const ov::frontend::onnx::Node& node) {}// Setting up other Loop body inputs.// body_inputs[0] is iteration number, body_inputs[1] is termination condition- auto body_inputs_it = std::next(body_inputs.begin(), 2);+ auto body_inputs_it = std::next(body_inputs.begin(), control_inputs_count);// body_outputs[0] is termination condition outputauto body_outputs_it = std::next(body_outputs.begin(), 1);@@ -246,7 +258,14 @@ ov::OutputVector loop_legacy(const ov::frontend::onnx::Node& node) {ov::OutputVector loop(const ov::frontend::onnx::Node& node) {const auto& ng_inputs = node.get_ov_inputs();- const ov::OutputVector loop_carried_dependencies{std::next(ng_inputs.begin(), 2), ng_inputs.end()};+ constexpr size_t control_inputs_count = 2;++ FRONT_END_GENERAL_CHECK(+ ng_inputs.size() >= control_inputs_count,+ "Expecting at least two canonical inputs for Loop op: trip count and termination condition");++ const ov::OutputVector loop_carried_dependencies{std::next(ng_inputs.begin(), control_inputs_count),+ ng_inputs.end()};auto body_graph = node.get_attribute_value<std::shared_ptr<ov::Model>>("body");const auto& body_results = body_graph->get_results();@@ -277,14 +296,18 @@ ov::OutputVector loop(const ov::frontend::onnx::Node& node) {body_outputs = std::move(filtered_body_outputs);CHECK_VALID_NODE(node,- canonical_inputs.size() >= 2,- "The provided loop body graph inputs size (",+ canonical_inputs.size() >= control_inputs_count &&+ canonical_inputs.size() - control_inputs_count == loop_carried_dependencies.size(),+ "The provided loop body graph canonical inputs size (",canonical_inputs.size(),- ") is not greater than the mandatory iteration and condition inputs (2)");+ "), does not match the sum of loop carried dependencies "+ "and two mandatory inputs (",+ loop_carried_dependencies.size() + control_inputs_count,+ ")");auto iteration_param = canonical_inputs[0];auto condition_param = canonical_inputs[1];- ov::ParameterVector state_parameters(canonical_inputs.begin() + 2, canonical_inputs.end());+ ov::ParameterVector state_parameters(canonical_inputs.begin() + control_inputs_count, canonical_inputs.end());const auto default_trip_count = v0::Constant::create(ov::element::i64, {1}, {-1});const auto true_condition = v0::Constant::create(ov::element::boolean, {1}, {true});@@ -338,14 +361,6 @@ ov::OutputVector loop(const ov::frontend::onnx::Node& node) {}}- CHECK_VALID_NODE(node,- state_parameters.size() == loop_carried_dependencies.size(),- "The provided loop body state parameters size (",- state_parameters.size(),- ") must match the number of loop carried dependencies (",- loop_carried_dependencies.size(),- ")");-const auto mapped = loop_carried_dependencies.size();// Infer loop body inputs' element type based on carried dependencies@@ -355,11 +370,11 @@ ov::OutputVector loop(const ov::frontend::onnx::Node& node) {}CHECK_VALID_NODE(node,- body_outputs.size() >= state_parameters.size() + 1,+ body_outputs.size() >= 1 && body_outputs.size() - 1 >= loop_carried_dependencies.size(),"The provided loop body graph outputs size (",body_outputs.size(),- ") is not greater than number of outputs. Required at least: ",- state_parameters.size() + 1);+ ") is smaller than number of outputs. Required at least: ",+ loop_carried_dependencies.size() + 1);ov::ParameterVector body_params;body_params.push_back(iteration_param);diff --git a/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_inputs.prototxt b/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_inputs.prototxtnew file mode 100644index 00000000000..7be0c77bf95--- /dev/null+++ b/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_inputs.prototxt@@ -0,0 +1,138 @@+ir_version: 6+producer_name: "OpenVINO ONNX Frontend"+doc_string: "Malformed Loop: 3 loop-carried deps but body only has 2 params (iter + cond, missing state params)"+graph {+ name: "loop_body_inputs_oob"+ node {+ input: "trip_count"+ input: ""+ input: "dep1"+ input: "dep2"+ input: "dep3"+ output: "res1"+ output: "res2"+ output: "res3"+ op_type: "Loop"+ attribute {+ name: "body"+ g {+ node {+ input: "cond_in"+ output: "cond_out"+ name: "cond_identity"+ op_type: "Identity"+ }+ name: "loop body with too few inputs"+ input {+ name: "i"+ type {+ tensor_type {+ elem_type: 7+ shape {+ }+ }+ }+ }+ input {+ name: "cond_in"+ type {+ tensor_type {+ elem_type: 9+ shape {+ }+ }+ }+ }+ output {+ name: "cond_out"+ type {+ tensor_type {+ elem_type: 9+ shape {+ }+ }+ }+ }+ }+ type: GRAPH+ }+ }+ initializer {+ dims: 1+ data_type: 7+ int64_data: 3+ name: "trip_count"+ }+ input {+ name: "dep1"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "dep2"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ input {+ name: "dep3"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ output {+ name: "res1"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ output {+ name: "res2"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ output {+ name: "res3"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+}+opset_import {+ version: 11+}diff --git a/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_outputs.prototxt b/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_outputs.prototxtnew file mode 100644index 00000000000..01fd168345c--- /dev/null+++ b/src/frontends/onnx/tests/models/controlflow/loop_too_few_body_outputs.prototxt@@ -0,0 +1,112 @@+ir_version: 6+producer_name: "OpenVINO ONNX Frontend"+doc_string: "Malformed Loop: has loop-carried deps but body has too few outputs (only cond_out, missing state outputs)"+graph {+ name: "loop_body_outputs_oob"+ node {+ input: "trip_count"+ input: ""+ input: "a_init"+ output: "a_final"+ output: "a_values"+ op_type: "Loop"+ attribute {+ name: "body"+ g {+ node {+ input: "cond_in"+ output: "cond_out"+ name: "cond_identity"+ op_type: "Identity"+ }+ name: "loop body with too few outputs"+ input {+ name: "i"+ type {+ tensor_type {+ elem_type: 7+ shape {+ }+ }+ }+ }+ input {+ name: "cond_in"+ type {+ tensor_type {+ elem_type: 9+ shape {+ }+ }+ }+ }+ input {+ name: "a_in"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ output {+ name: "cond_out"+ type {+ tensor_type {+ elem_type: 9+ shape {+ }+ }+ }+ }+ }+ type: GRAPH+ }+ }+ initializer {+ dims: 1+ data_type: 7+ int64_data: 3+ name: "trip_count"+ }+ input {+ name: "a_init"+ type {+ tensor_type {+ elem_type: 1+ shape {+ dim {+ dim_value: 1+ }+ }+ }+ }+ }+ output {+ name: "a_final"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+ output {+ name: "a_values"+ type {+ tensor_type {+ elem_type: 1+ shape {+ }+ }+ }+ }+}+opset_import {+ version: 11+}diff --git a/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp b/src/frontends/onnx/tests/onnx_import_controlflow.in.cppindex e8c2c752111..14e3d3f931d 100644--- a/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp+++ b/src/frontends/onnx/tests/onnx_import_controlflow.in.cpp@@ -9,6 +9,7 @@#include "common_test_utils/type_prop.hpp"#include "gtest/gtest.h"#include "onnx_utils.hpp"+#include "openvino/frontend/exception.hpp"#include "openvino/op/loop.hpp"#include "openvino/op/multiply.hpp"@@ -237,6 +238,23 @@ OPENVINO_TEST(${BACKEND_NAME}, onnx_controlflow_loop_add_value_access_to_body_sc}}+// Regression test: Loop body with too few inputs relative to loop-carried+// dependencies must throw instead of accessing body_inputs out of bounds.+OPENVINO_TEST(${BACKEND_NAME}, onnx_controlflow_loop_too_few_body_inputs_exception) {+ OV_EXPECT_THROW(convert_model("controlflow/loop_too_few_body_inputs.onnx"),+ ov::AssertFailure,+ testing::AllOf(testing::HasSubstr("loop body graph canonical inputs size"),+ testing::HasSubstr("does not match the sum of loop carried dependencies")));+}++// Regression test: Loop body with too few outputs relative to loop-carried+// dependencies must throw instead of accessing body_outputs out of bounds.+OPENVINO_TEST(${BACKEND_NAME}, onnx_controlflow_loop_too_few_body_outputs_exception) {+ OV_EXPECT_THROW(convert_model("controlflow/loop_too_few_body_outputs.onnx"),+ ov::AssertFailure,+ testing::HasSubstr("loop body graph outputs size"));+}+OPENVINO_TEST(${BACKEND_NAME}, onnx_controlflow_loop_add_value_the_same_node_from_parent_and_subgraph) {const auto model = convert_model("controlflow/loop_2d_add_the_same_name.onnx");