Skip to content

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

[NPU] Fix OOB read in blob_format_importer_factory tensor overload (#37315)

Alexis Menard714640b4d1f9
diff --git a/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp b/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp
index 36b5b9d6ac8..24171833edb 100644
--- a/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp
+++ b/src/plugins/intel_npu/src/plugin/src/blob_format_importers.cpp
@@ -546,10 +546,14 @@ std::unique_ptr<IBlobFormatImporter> create(const ov::Tensor& npu_formatted_blob
return std::make_unique<RawBlobImporter>(npu_formatted_blob, original_model, config);
}
- size_t magic_bytes_size = MAGIC_BYTES.size();
- std::string_view blob_magic_bytes(
- npu_formatted_blob.data<const char>() + npu_formatted_blob.get_byte_size() - magic_bytes_size,
- magic_bytes_size);
+ const size_t input_size = npu_formatted_blob.get_byte_size();
+ OPENVINO_ASSERT(input_size > 0, EMPTY_BLOB_MESSAGE);
+
+ const size_t magic_bytes_size = MAGIC_BYTES.size();
+ OPENVINO_ASSERT(input_size >= magic_bytes_size, BLOB_SIZE_SMALLER_THAN_MAGIC);
+
+ std::string_view blob_magic_bytes(npu_formatted_blob.data<const char>() + input_size - magic_bytes_size,
+ magic_bytes_size);
OPENVINO_ASSERT(MAGIC_BYTES == blob_magic_bytes, MISSING_METADATA_MESSAGE);
diff --git a/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp b/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp
index 02938868f91..5ab493b3287 100644
--- a/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp
+++ b/src/plugins/intel_npu/tests/unit/npu/blob_format_importers.cpp
@@ -70,6 +70,21 @@ TEST_F(BlobFormatImportersTest, FactoryEmptyInputFails) {
OV_EXPECT_THROW(blob_format_importer_factory::create(input_tensor, false, nullptr, config), ov::Exception, _);
}
+/**
+ * @brief A non-raw tensor with 1..(MAGIC_BYTES.size()-1) bytes must be rejected without reading OOB.
+ * The unsigned subtraction get_byte_size() - MAGIC_BYTES.size() underflows otherwise.
+ */
+TEST_F(BlobFormatImportersTest, FactoryTensorSmallerThanMagicFails) {
+ ASSERT_GT(MAGIC_BYTES.size(), 1u);
+ for (size_t sz = 1; sz < MAGIC_BYTES.size(); ++sz) {
+ const std::string blob(sz, '\x00');
+ const ov::Tensor input_tensor(ov::element::Type_t::u8,
+ ov::Shape({blob.size()}),
+ const_cast<char*>(blob.data()));
+ OV_EXPECT_THROW(blob_format_importer_factory::create(input_tensor, false, nullptr, config), ov::Exception, _);
+ }
+}
+
/**
* @brief Non-raw blobs must contain the magic bytes
*/