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
[NPU] Validate the stream offset and size when deserializing weights … (#36743)
Alexis Menard8e855e999c91
diff --git a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp b/src/plugins/intel_npu/src/plugin/npuw/serialization.cppindex b388262ab1d..4814b2ef2a8 100644--- a/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp+++ b/src/plugins/intel_npu/src/plugin/npuw/serialization.cpp@@ -361,7 +361,12 @@ void ov::npuw::orc::serialize_weightless(Stream& stream,serialize(stream, offset);ov::Tensor t(type, shape);+ OPENVINO_ASSERT(byte_size == t.get_byte_size(), "[NPU] ORC weight byte_size does not match tensor shape");+if (ctx.weights) {+ const auto wsz = ctx.weights->size();+ OPENVINO_ASSERT(offset <= wsz && byte_size <= wsz - offset,+ "[NPU] ORC weight offset/size out of range");if (ctx.bf16_consts.find({offset, byte_size}) != ctx.bf16_consts.end()) {NPUW_ASSERT(type == ov::element::f16);// Read original bf16 weightdiff --git a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp b/src/plugins/intel_npu/tests/unit/npuw/serialization.cppindex 14d7b523ef7..ceda43a6a4b 100644--- a/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp+++ b/src/plugins/intel_npu/tests/unit/npuw/serialization.cpp@@ -802,6 +802,61 @@ TEST(SerializationTest, OVTypes_Tensor_weightless_bf16_to_f16) {std::filesystem::remove(file_path);}+// The weightless-import mmap path in serialize_weightless() reads `byte_size` and `offset`+// straight from the (untrusted) stream and then does+// std::memcpy(t.data(), ctx.weights->get_ptr(offset), byte_size);+// without checking either+// (a) offset + byte_size <= ctx.weights->size() (source / mmap over-read), or+// (b) byte_size <= t.get_byte_size() (destination heap over-write).+// Unlike the sibling consts_cache path (asserts t.get_byte_size() == byte_size) and the+// bf16 path (asserts the bf16 buffer size), this branch validates neither, so a crafted+// blob yields out-of-bounds read AND write -> memory corruption.+//+// This test crafts such a blob: a 4-byte destination tensor, an 8-byte backing weights+// file, but byte_size = 64 KiB. Without importer-side validation this may corrupt the heap and crash+// nondeterministically; with validation in place, read_weightless() is expected to throw (ov::AssertFailure)+// instead of reaching the memcpy.+TEST(SerializationTest, OVTypes_Tensor_weightless_mmap_oob_overflow) {+ using namespace ov::npuw::s11n;++ // Hand-craft the serialized weightless entry in the exact field order the importer+ // reads it (serialize_weightless, is_weightless branch):+ // size, is_initialized, is_weightless, type_str, shape, byte_size, offset+ std::stringstream ss;+ write(ss, std::size_t(1)); // one tensor in the vector+ write(ss, true); // is_initialized+ write(ss, true); // is_weightless+ write(ss, std::string("u8")); // element type -> 1 byte / element+ write(ss, ov::Shape{4}); // destination tensor: 4 bytes only+ const std::size_t attacker_byte_size = 64 * 1024; // >> dest (4) and >> weights file (8)+ write(ss, attacker_byte_size); // byte_size (attacker-controlled, unchecked)+ write(ss, std::size_t(0)); // offset (attacker-controlled, unchecked)++ // Tiny backing "weights" file: only 8 bytes get mmapped.+ std::filesystem::path file_path = ov::test::utils::generateTestFilePrefix() + "_npuw_oob_weights.bin";+ {+ std::ofstream os(file_path, std::ios::binary);+ const std::vector<uint8_t> tiny(8, 0xAB);+ os.write(reinterpret_cast<const char*>(tiny.data()), static_cast<std::streamsize>(tiny.size()));+ }++ std::vector<ov::Tensor> res;+ {+ auto mapped = ov::load_mmap_object(file_path);+ ASSERT_NE(mapped, nullptr);+ auto weights = std::make_shared<Weights>(reinterpret_cast<char*>(mapped->data()), mapped->size(), mapped);++ // No bf16 entry and empty consts_cache -> forces the unguarded mmap memcpy branch.+ WeightsContext import_ctx(weights, file_path.string(), {}, {});++ // memcpy reads 64 KiB from an 8-byte mmap region into a 4-byte tensor:+ // out-of-bounds READ past the mapping AND out-of-bounds WRITE past the heap tensor.+ ASSERT_THROW(read_weightless(ss, res, import_ctx), ov::AssertFailure);+ }++ std::filesystem::remove(file_path);+}+TEST(SerializationTest, OVTypes_OutputPort_roundtrips_into_parameter_pointer) {using namespace ov::npuw::s11n;