Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[ONNX FE] Unguarded border[1]/border[0] read before length check in crop (#37915)

Xu Xingcbcea7a3f6a3
diff --git a/src/frontends/onnx/frontend/src/op/crop.cpp b/src/frontends/onnx/frontend/src/op/crop.cpp
index e8bc90e7bb7..56af291b007 100644
--- a/src/frontends/onnx/frontend/src/op/crop.cpp
+++ b/src/frontends/onnx/frontend/src/op/crop.cpp
@@ -26,6 +26,12 @@ ov::OutputVector crop(const ov::frontend::onnx::Node& node) {
// Spec defines four borders (left, top, right, bottom); when scale is present only left/top are used.
const auto border = node.get_attribute_value<std::vector<std::int64_t>>("border");
+ // border[0] and border[1] are read below to build `begin`, regardless of whether 'scale' is set.
+ CHECK_VALID_NODE(node,
+ border.size() >= 2,
+ "ONNX Crop expects at least 2 values in 'border' attribute, found: ",
+ border.size());
+
std::shared_ptr<ov::Node> end;
// Set slice begin values to border values (note order of indexes)
@@ -35,10 +41,6 @@ ov::OutputVector crop(const ov::frontend::onnx::Node& node) {
// If scale is given, then start crop at left/top `border`
// and end on left/top `border` + `scale`.
if (node.has_attribute("scale")) {
- CHECK_VALID_NODE(node,
- border.size() >= 2,
- "ONNX Crop with 'scale' expects at least 2 values in 'border' attribute, found: ",
- border.size());
// List of ints height, width
const auto scale = node.get_attribute_value<std::vector<std::int64_t>>("scale");
diff --git a/src/frontends/onnx/tests/convert_tests.cpp b/src/frontends/onnx/tests/convert_tests.cpp
index 0b04f8ab16b..936896fc366 100644
--- a/src/frontends/onnx/tests/convert_tests.cpp
+++ b/src/frontends/onnx/tests/convert_tests.cpp
@@ -113,3 +113,17 @@ TEST(ONNXFeConvertException, exception_if_scan_body_fewer_outputs_than_initial_v
ov::frontend::OpConversionFailure,
testing::HasSubstr("num_scan_outputs can't be negative"));
}
+
+/// Tests that Crop rejects a 'border' attribute with fewer than 2 values, since border[0] and
+/// border[1] are read to build the slice 'begin' constant regardless of the 'scale' attribute.
+TEST(ONNXFeConvertException, exception_if_crop_border_too_short) {
+ OV_EXPECT_THROW(convert_model("crop_border_too_short.onnx"),
+ ov::AssertFailure,
+ testing::HasSubstr("expects at least 2 values in 'border' attribute, found: 1"));
+}
+
+TEST(ONNXFeConvertException, exception_if_crop_border_empty) {
+ OV_EXPECT_THROW(convert_model("crop_border_empty.onnx"),
+ ov::AssertFailure,
+ testing::HasSubstr("expects at least 2 values in 'border' attribute, found: 0"));
+}
diff --git a/src/frontends/onnx/tests/models/crop_border_empty.prototxt b/src/frontends/onnx/tests/models/crop_border_empty.prototxt
new file mode 100644
index 00000000000..effba75bff4
--- /dev/null
+++ b/src/frontends/onnx/tests/models/crop_border_empty.prototxt
@@ -0,0 +1,61 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Crop"
+ attribute {
+ name: "border"
+ type: INTS
+ }
+ }
+ name: "test_model"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 1
+}
diff --git a/src/frontends/onnx/tests/models/crop_border_too_short.prototxt b/src/frontends/onnx/tests/models/crop_border_too_short.prototxt
new file mode 100644
index 00000000000..d0b08e297c5
--- /dev/null
+++ b/src/frontends/onnx/tests/models/crop_border_too_short.prototxt
@@ -0,0 +1,62 @@
+ir_version: 3
+producer_name: "OpenVINO ONNX Frontend"
+graph {
+ node {
+ input: "x"
+ output: "y"
+ op_type: "Crop"
+ attribute {
+ name: "border"
+ ints: 1
+ type: INTS
+ }
+ }
+ name: "test_model"
+ input {
+ name: "x"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 4
+ }
+ dim {
+ dim_value: 4
+ }
+ }
+ }
+ }
+ }
+ output {
+ name: "y"
+ type {
+ tensor_type {
+ elem_type: 1
+ shape {
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 1
+ }
+ dim {
+ dim_value: 2
+ }
+ dim {
+ dim_value: 2
+ }
+ }
+ }
+ }
+ }
+}
+opset_import {
+ version: 1
+}