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 Level Zero graph argument names and dimension values (#38420)
Alexis Menard2802ac433e05
diff --git a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cppindex 7d45a09cda1..467233f6183 100644--- a/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp+++ b/src/plugins/intel_npu/src/compiler_adapter/src/ze_graph_ext_wrappers.cpp@@ -4,6 +4,7 @@#include "ze_graph_ext_wrappers.hpp"+#include <cstring>#include <string_view>#include "intel_npu/prefix.hpp"@@ -16,6 +17,27 @@#include "openvino/core/dimension.hpp"#include "openvino/core/partial_shape.hpp"+namespace {+// Level Zero fixed-width name buffers are not guaranteed to be NUL-terminated by a malicious/compromised+// driver or compiler; scanning them as C strings (e.g. via std::string's implicit constructor) can read+// past the end of the buffer. Require an in-array terminator before building the string.+std::string safeStringFromFixedBuffer(const char* buffer,+ size_t bufferSize,+ const char* fieldName,+ uint32_t indexUsedByDriver) {+ if (std::memchr(buffer, '\0', bufferSize) == nullptr) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": ",+ fieldName,+ " is not NUL-terminated within its ",+ bufferSize,+ "-byte buffer");+ }+ return std::string(buffer);+}+} // namespace+namespace intel_npu {IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,@@ -27,8 +49,19 @@ IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,ov::PartialShape shapeFromIRModel;std::unordered_set<std::string> outputTensorNames;+ if (arg.associated_tensor_names_count > ZE_MAX_GRAPH_TENSOR_NAMES_SIZE) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": associated_tensor_names_count ",+ arg.associated_tensor_names_count,+ " exceeds ABI limit ",+ ZE_MAX_GRAPH_TENSOR_NAMES_SIZE);+ }for (uint32_t id = 0; id < arg.associated_tensor_names_count; id++) {- outputTensorNames.insert(arg.associated_tensor_names[id]);+ outputTensorNames.insert(safeStringFromFixedBuffer(arg.associated_tensor_names[id],+ ZE_MAX_GRAPH_ARGUMENT_NAME,+ "associated_tensor_names",+ indexUsedByDriver));}if (arg.dims_count > ZE_MAX_GRAPH_ARGUMENT_DIMENSIONS_SIZE) {OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",@@ -62,6 +95,21 @@ IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,shapeFromIRModel.reserve(metadata->shape_size);for (uint32_t id = 0; id < metadata->shape_size; id++) {if (metadata->shape[id] != dynamicDim) {+ // static metadata dimension must match the driver's true argument span; otherwise the+ // tensor could be undersized relative to what Level Zero actually reads/writes+ const bool isPluginBatchingDimension =+ id == utils::BATCH_AXIS && shapeFromCompiler[id] == utils::DEFAULT_BATCH_SIZE;+ if ((metadata->shape[id] != shapeFromCompiler[id] && !isPluginBatchingDimension) ||+ (isPluginBatchingDimension && metadata->shape[id] < shapeFromCompiler[id])) {+ OPENVINO_THROW("Invalid Level Zero graph argument metadata for argument index ",+ indexUsedByDriver,+ ": static metadata dimension ",+ id,+ " value ",+ metadata->shape[id],+ " does not match driver argument dimension ",+ shapeFromCompiler[id]);+ }shapeFromIRModel.push_back(metadata->shape[id]);} else {// lower bound is ignored, so we set it to 1 just to satisfy the Dimension constructor,@@ -80,7 +128,8 @@ IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,}// Flags will be used instead of indices for informing the type of the current entry- std::string nameFromCompiler = arg.name;+ std::string nameFromCompiler =+ safeStringFromFixedBuffer(arg.name, ZE_MAX_GRAPH_ARGUMENT_NAME, "name", indexUsedByDriver);const bool isInput = (arg.type == ZE_GRAPH_ARGUMENT_TYPE_INPUT);bool isStateInput = false;bool isStateOutput = false;@@ -117,6 +166,11 @@ IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,}}+ std::string debugFriendlyName = safeStringFromFixedBuffer(arg.debug_friendly_name,+ ZE_MAX_GRAPH_ARGUMENT_NAME,+ "debug_friendly_name",+ indexUsedByDriver);+return {std::move(nameFromCompiler),precision,shapeFromCompiler,@@ -127,7 +181,7 @@ IODescriptor createIODescriptorFromLevelZero(const uint32_t indexUsedByDriver,isInitOutputWeights,isMainInputWeights,std::nullopt,- arg.debug_friendly_name,+ std::move(debugFriendlyName),std::move(outputTensorNames),metadata.has_value() ? std::optional(shapeFromIRModel) : std::nullopt,indexUsedByDriver,@@ -541,6 +595,10 @@ void ZeGraphExtWrappers::getMetadata(ze_graph_handle_t graphHandle,std::optional<ze_graph_argument_metadata_t> optionalMetadata = std::nullopt;+ // arg.name is scanned as a string_view by the prefix predicates below, ahead of the+ // NUL-termination check that createIODescriptorFromLevelZero performs further down.+ safeStringFromFixedBuffer(arg.name, ZE_MAX_GRAPH_ARGUMENT_NAME, "name", indexUsedByDriver);+if (!isStateInputName(arg.name) && !isStateOutputName(arg.name) && !isShapeTensorName(arg.name) &&!isInitInputWeightsName(arg.name) && !isInitOutputWeightsName(arg.name) &&!isMainInputWeightsName(arg.name)) {diff --git a/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cpp b/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cppindex b7015624613..6ef950803a3 100644--- a/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cpp+++ b/src/plugins/intel_npu/tests/unit/npu/ze_graph_ext_wrappers_test.cpp@@ -97,3 +97,106 @@ TEST(ZeGraphExtWrappersTest, AcceptsMatchingDynamicMetadataRank) {EXPECT_EQ(descriptor.shapeFromCompiler, ov::PartialShape({4, 32}));EXPECT_EQ(*descriptor.shapeFromIRModel, ov::PartialShape({4, ov::Dimension(1, 32)}));}++TEST(ZeGraphExtWrappersTest, AcceptsPluginBatchingMetadata) {+ auto arg = makeArgument(2);+ arg.dims[0] = 1;+ auto metadata = makeMetadata(2);+ metadata.shape[0] = 4;++ const auto descriptor = intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata);++ EXPECT_EQ(descriptor.shapeFromCompiler, ov::PartialShape({1, 32}));+ ASSERT_TRUE(descriptor.shapeFromIRModel.has_value());+ EXPECT_EQ(*descriptor.shapeFromIRModel, ov::PartialShape({4, 32}));+}++TEST(ZeGraphExtWrappersTest, RejectsUndersizedPluginBatchingMetadata) {+ auto arg = makeArgument(2);+ arg.dims[0] = 1;+ auto metadata = makeMetadata(2);+ metadata.shape[0] = 0;++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata),+ ov::Exception,+ "static metadata dimension 0 value 0 does not match driver argument dimension 1");+}++TEST(ZeGraphExtWrappersTest, RejectsStaticMetadataDriverSpanMismatch) {+ // Same rank on both sides, but the static metadata shape ([1]) undersizes the real+ // driver argument span ([1024]); a naive tensor sized from metadata alone would let the+ // driver write/read out of bounds relative to the (smaller) OpenVINO-visible allocation.+ auto arg = makeArgument(1);+ arg.dims[0] = 1024;+ auto metadata = makeMetadata(1);+ metadata.shape[0] = 1;++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata),+ ov::Exception,+ "static metadata dimension 0 value 1 does not match driver argument dimension 1024");+}++TEST(ZeGraphExtWrappersTest, RejectsCompilerAndIrRankMismatchZeroRank) {+ // Compiler shape rank 0 (empty shapeFromCompiler) paired with a one-axis dynamic IR shape:+ // without the rank-equality guard, indexing shapeFromCompiler[0] to resolve the dynamic+ // upper bound would read out of bounds of the empty vector.+ auto arg = makeArgument(0);+ auto metadata = makeMetadata(1);+ metadata.shape[0] = std::numeric_limits<uint64_t>::max();++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, metadata),+ ov::Exception,+ "metadata shape_size 1 does not match dims_count 0");+}++TEST(ZeGraphExtWrappersTest, RejectsUnterminatedName) {+ // A compromised driver/parser can fill arg.name with exactly ZE_MAX_GRAPH_ARGUMENT_NAME+ // non-NUL bytes; constructing a std::string from it would scan past the buffer.+ auto arg = makeArgument(0);+ std::memset(arg.name, 'A', sizeof(arg.name));++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt),+ ov::Exception,+ "name is not NUL-terminated");+}++TEST(ZeGraphExtWrappersTest, RejectsUnterminatedDebugFriendlyName) {+ auto arg = makeArgument(0);+ std::memset(arg.debug_friendly_name, 'A', sizeof(arg.debug_friendly_name));++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt),+ ov::Exception,+ "debug_friendly_name is not NUL-terminated");+}++TEST(ZeGraphExtWrappersTest, RejectsUnterminatedAssociatedTensorName) {+ auto arg = makeArgument(0);+ arg.associated_tensor_names_count = 1;+ std::memset(arg.associated_tensor_names[0], 'A', sizeof(arg.associated_tensor_names[0]));++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt),+ ov::Exception,+ "associated_tensor_names is not NUL-terminated");+}++TEST(ZeGraphExtWrappersTest, RejectsAssociatedTensorNamesCountAboveAbiLimit) {+ auto arg = makeArgument(0);+ const auto aboveAbiLimit = ZE_MAX_GRAPH_TENSOR_NAMES_SIZE + 1;+ arg.associated_tensor_names_count = aboveAbiLimit;++ OV_EXPECT_THROW_HAS_SUBSTRING(intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt),+ ov::Exception,+ "associated_tensor_names_count " + std::to_string(aboveAbiLimit) ++ " exceeds ABI limit " + std::to_string(ZE_MAX_GRAPH_TENSOR_NAMES_SIZE));+}++TEST(ZeGraphExtWrappersTest, AcceptsFullCapacityNameWithTrailingNul) {+ // Negative control from the report: 255 non-NUL bytes followed by the parser-initialized+ // final NUL byte is a legitimate full-length terminated name and must still be accepted.+ auto arg = makeArgument(0);+ std::memset(arg.name, 'A', sizeof(arg.name) - 1);+ arg.name[sizeof(arg.name) - 1] = '\0';++ const auto descriptor = intel_npu::createIODescriptorFromLevelZero(kArgumentIndex, arg, std::nullopt);+ EXPECT_EQ(descriptor.nameFromCompiler.size(), sizeof(arg.name) - 1);+}