Skip to content

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

[Reference] Fix potential OOB write in ScatterNDUpdate OP (#37129)

Enrico Galli1de08f9b0f7c
diff --git a/src/core/reference/include/openvino/reference/scatter_nd_update.hpp b/src/core/reference/include/openvino/reference/scatter_nd_update.hpp
index 90988521f9e..8240e90dbe8 100644
--- a/src/core/reference/include/openvino/reference/scatter_nd_update.hpp
+++ b/src/core/reference/include/openvino/reference/scatter_nd_update.hpp
@@ -99,6 +99,7 @@ void scatterNdUpdate(
if (c < 0) {
c += static_cast<indicesType>(dataShape[j]);
}
+ OPENVINO_ASSERT(c >= 0 && static_cast<size_t>(c) < dataShape[j], "Index is out of bounds");
j++;
}
diff --git a/src/plugins/template/tests/functional/op_reference/scatter_nd_update.cpp b/src/plugins/template/tests/functional/op_reference/scatter_nd_update.cpp
index c788db901d1..ca141e0c0a2 100644
--- a/src/plugins/template/tests/functional/op_reference/scatter_nd_update.cpp
+++ b/src/plugins/template/tests/functional/op_reference/scatter_nd_update.cpp
@@ -657,6 +657,16 @@ std::vector<ScatterNDUpdateParams> generateScatterNDUpdateNegativeParams() {
reference_tests::Tensor({1}, IN_ET, std::vector<T>{10}),
reference_tests::Tensor({2, 2}, IN_ET, std::vector<T>{1, 2, 3, 4, 5, 6}),
"scatter_nd_update_2x3_with_out_of_bounds_index"),
+ // scatter_nd_update_4x4_with_out_of_range_negative_index
+ // Regression test for CWE-190 -> CWE-787: an index whose magnitude exceeds
+ // its dimension (dim=4, idx=-5) stays negative after the single-pass negative
+ // index fixup. Casting it to the unsigned accumulator used to wrap the bounds
+ // check and allow an out-of-bounds write; it must now be rejected.
+ ScatterNDUpdateParams(reference_tests::Tensor({4, 4}, IN_ET, std::vector<T>(16, static_cast<T>(0))),
+ reference_tests::Tensor({1, 2}, IU_ET, std::vector<U>{0, -5}),
+ reference_tests::Tensor({1}, IN_ET, std::vector<T>{1}),
+ reference_tests::Tensor({4, 4}, IN_ET, std::vector<T>(16, static_cast<T>(0))),
+ "scatter_nd_update_4x4_with_out_of_range_negative_index"),
};
return scatterParams;
}