Skip to content

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

[Core] Fix potential OOB read in WeightlessCacheAttribute parsing (#37261)

Alexis Menardbc413b52410a
diff --git a/src/core/src/weight_sharing_util.cpp b/src/core/src/weight_sharing_util.cpp
index cf35aa54065..c4c145fb2d3 100644
--- a/src/core/src/weight_sharing_util.cpp
+++ b/src/core/src/weight_sharing_util.cpp
@@ -124,6 +124,9 @@ std::shared_ptr<ov::AlignedBuffer> get_buffer(const Context& shared_context,
source_it != shared_context.m_cache_sources.end()) {
if (auto wt_buffer = source_it->second.m_weights.lock()) {
if (const auto meta = get_constant_meta(shared_context.m_weight_registry, source_id, constant_id)) {
+ OPENVINO_ASSERT(
+ meta->m_offset <= wt_buffer->size() && meta->m_size <= wt_buffer->size() - meta->m_offset,
+ "WeightlessCacheAttribute offset/size is out of bounds for source buffer");
return std::make_shared<ov::SharedBuffer<WeightBuffer>>(wt_buffer->get_ptr<char>() + meta->m_offset,
meta->m_size,
wt_buffer);
@@ -140,6 +143,9 @@ std::shared_ptr<ov::AlignedBuffer> get_buffer(const Context& shared_context,
const auto& desc = source_buffer->get_descriptor();
const auto wt_id = desc ? desc->get_id() : ov::wsh::invalid_source_id;
if (const auto meta_data = get_constant_meta(shared_context.m_weight_registry, wt_id, constant_id)) {
+ OPENVINO_ASSERT(meta_data->m_offset <= source_buffer->size() &&
+ meta_data->m_size <= source_buffer->size() - meta_data->m_offset,
+ "WeightlessCacheAttribute offset/size is out of bounds for source buffer");
return std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(
source_buffer->get_ptr<char>() + meta_data->m_offset,
meta_data->m_size,
@@ -155,6 +161,9 @@ std::shared_ptr<ov::AlignedBuffer> get_buffer(const Context& shared_context,
if (source_buffer) {
if (const auto meta_data =
get_constant_meta(shared_context.m_weight_registry, source_buffer->get_id(), constant_id)) {
+ OPENVINO_ASSERT(meta_data->m_offset <= source_buffer->size() &&
+ meta_data->m_size <= source_buffer->size() - meta_data->m_offset,
+ "WeightlessCacheAttribute offset/size is out of bounds for source buffer");
return std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::MappedMemory>>>(
source_buffer->data() + meta_data->m_offset,
meta_data->m_size,
diff --git a/src/core/tests/weight_sharing_util_test.cpp b/src/core/tests/weight_sharing_util_test.cpp
index 008aed8ae87..dae38afcaa6 100644
--- a/src/core/tests/weight_sharing_util_test.cpp
+++ b/src/core/tests/weight_sharing_util_test.cpp
@@ -381,4 +381,51 @@ TEST_F(WeightShareExtensionTest, rebuild_constant_from_shared_context) {
EXPECT_EQ(ov::wsh::Extension::get_constant_id(*c), const_id_from_blob);
}
}
+
+TEST_F(WeightShareExtensionTest, get_buffer_by_source_id_oob_throws) {
+ weight_sharing::Context ctx;
+ const weight_sharing::DataID source_id = 42;
+ const weight_sharing::DataID const_id = 0;
+
+ auto raw = std::make_shared<ov::AlignedBuffer>(20);
+ auto src_buf = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(
+ raw->get_ptr<char>(),
+ raw->size(),
+ raw,
+ ov::create_base_descriptor(source_id, 0, raw));
+ ctx.m_cache_sources[source_id] = {"test", src_buf};
+ ctx.m_weight_registry[source_id][const_id] = {0, 0x1000000000ULL, element::f32};
+
+ OV_EXPECT_THROW(weight_sharing::get_buffer(ctx, source_id, const_id), ov::Exception, testing::_);
+}
+
+TEST_F(WeightShareExtensionTest, get_buffer_by_aligned_buffer_oob_throws) {
+ weight_sharing::Context ctx;
+ const weight_sharing::DataID source_id = 43;
+ const weight_sharing::DataID const_id = 0;
+
+ auto raw = std::make_shared<ov::AlignedBuffer>(20);
+ auto src_buf = std::make_shared<ov::SharedBuffer<std::shared_ptr<ov::AlignedBuffer>>>(
+ raw->get_ptr<char>(),
+ raw->size(),
+ raw,
+ ov::create_base_descriptor(source_id, 0, raw));
+ ctx.m_weight_registry[source_id][const_id] = {0, 0x1000000000ULL, element::f32};
+
+ OV_EXPECT_THROW(weight_sharing::get_buffer(ctx, src_buf, const_id), ov::Exception, testing::_);
+}
+
+TEST_F(WeightShareExtensionTest, get_buffer_by_mmap_oob_throws) {
+ const auto weights_path = test_dir / "weights.bin";
+ create_test_weights_file(weights_path, /*size=*/5);
+
+ weight_sharing::Context ctx;
+ auto mmap = ov::load_mmap_object(weights_path);
+ ASSERT_TRUE(mmap);
+ const weight_sharing::DataID const_id = 0;
+ ctx.m_weight_registry[mmap->get_id()][const_id] = {0, 0x1000000000ULL, element::f32};
+
+ OV_EXPECT_THROW(weight_sharing::get_buffer(ctx, mmap, const_id), ov::Exception, testing::_);
+}
+
} // namespace ov::test
diff --git a/src/core/tests/xml_util/custom_ir.cpp b/src/core/tests/xml_util/custom_ir.cpp
index cffc4da46a0..e89e34001fa 100644
--- a/src/core/tests/xml_util/custom_ir.cpp
+++ b/src/core/tests/xml_util/custom_ir.cpp
@@ -424,6 +424,34 @@ TEST_F(CustomIRTest, modified_serialization_deserialization) {
EXPECT_TRUE(is_valid) << error_msg;
}
+/**
+ * @brief An inflated size in a Const's <data> element must be rejected before
+ * any weights buffer dereference;
+ */
+TEST_F(CustomIRTest, parse_weightless_cache_attribute_oob_throws) {
+ {
+ auto c = std::make_shared<Constant>(element::f32, Shape{5}, std::vector<float>(5, 1.0f));
+ auto input = std::make_shared<Parameter>(element::f32, Shape{5});
+ ov::serialize(std::make_shared<Model>(OutputVector{std::make_shared<Add>(input, c)}, ParameterVector{input}),
+ m_out_xml_path,
+ m_out_bin_path);
+ }
+
+ // Inflate the Const's <data size> — that is what parse_weightless_cache_attribute
+ // reads to populate WeightlessCacheAttribute.original_size
+ pugi::xml_document doc;
+ ASSERT_EQ(doc.load_file(m_out_xml_path.string().c_str()).status, pugi::status_ok);
+ for (auto& layer : doc.child("net").child("layers").children("layer")) {
+ if (std::string(layer.attribute("type").value()) == "Const") {
+ layer.child("data").attribute("size").set_value(uint64_t{0x1000000000ULL});
+ }
+ }
+ doc.save_file(m_out_xml_path.string().c_str());
+
+ // .bin is ~20 bytes; injected size (~68 GB) must be rejected before any dereference
+ EXPECT_THROW(ov::Core().read_model(m_out_xml_path, m_out_bin_path), ov::Exception);
+}
+
TEST(StrToContainer, FloatVectorWithInfAndNan) {
{
std::vector<float> result;
diff --git a/src/core/xml_util/src/xml_deserialize_util.cpp b/src/core/xml_util/src/xml_deserialize_util.cpp
index facfe5c3005..465e0bfeba4 100644
--- a/src/core/xml_util/src/xml_deserialize_util.cpp
+++ b/src/core/xml_util/src/xml_deserialize_util.cpp
@@ -426,8 +426,10 @@ ov::Any XmlDeserializer::parse_weightless_cache_attribute(const pugi::xml_node&
const auto offset = data_node.attribute("offset");
const auto element_type = data_node.attribute("element_type");
if (size && offset && element_type) {
- wl_attr = ov::WeightlessCacheAttribute(static_cast<size_t>(pugixml::get_uint64_attr(data_node, "size")),
- static_cast<size_t>(pugixml::get_uint64_attr(data_node, "offset")),
+ const auto original_size = static_cast<size_t>(pugixml::get_uint64_attr(data_node, "size"));
+ const auto bin_offset = static_cast<size_t>(pugixml::get_uint64_attr(data_node, "offset"));
+ wl_attr = ov::WeightlessCacheAttribute(original_size,
+ bin_offset,
ov::element::Type(pugixml::get_str_attr(data_node, "element_type")));
}
}
@@ -892,7 +894,8 @@ void XmlDeserializer::set_constant_num_buffer(ov::AttributeAdapter<std::shared_p
const auto size = static_cast<size_t>(pugixml::get_uint64_attr(dn, "size"));
const auto offset = static_cast<size_t>(pugixml::get_uint64_attr(dn, "offset"));
- OPENVINO_ASSERT(m_weights->size() >= offset + size, "Incorrect weights in bin file!");
+ OPENVINO_ASSERT(offset <= m_weights->size() && size <= m_weights->size() - offset,
+ "Incorrect weights in bin file!");
char* data = m_weights->get_ptr<char>() + offset;