Skip to content

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

[coverity][samples] Fix filesystem path filename manipulation (#30582)

Pawel Raasz25ac325ed6f8
diff --git a/samples/c/common/CMakeLists.txt b/samples/c/common/CMakeLists.txt
index 330478f9e5c..4e87556bf24 100644
--- a/samples/c/common/CMakeLists.txt
+++ b/samples/c/common/CMakeLists.txt
@@ -3,3 +3,4 @@
#
add_subdirectory(opencv_c_wrapper)
+add_subdirectory(sample_util)
diff --git a/samples/c/common/sample_util/CMakeLists.txt b/samples/c/common/sample_util/CMakeLists.txt
new file mode 100644
index 00000000000..a3fb92087b4
--- /dev/null
+++ b/samples/c/common/sample_util/CMakeLists.txt
@@ -0,0 +1,17 @@
+# Copyright (C) 2018-2025 Intel Corporation
+# SPDX-License-Identifier: Apache-2.0
+#
+
+set(TARGET_NAME "ie_samples_util_c")
+
+file(GLOB_RECURSE SOURCES "*.c" "*.h")
+source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES})
+
+add_library(${TARGET_NAME} STATIC EXCLUDE_FROM_ALL ${SOURCES})
+set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "src")
+
+target_include_directories(${TARGET_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
+
+if(COMMAND ov_add_clang_format_target)
+ ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})
+endif()
diff --git a/samples/c/common/sample_util/include/samples_util/path_util.h b/samples/c/common/sample_util/include/samples_util/path_util.h
new file mode 100644
index 00000000000..33fa3ec41b5
--- /dev/null
+++ b/samples/c/common/sample_util/include/samples_util/path_util.h
@@ -0,0 +1,34 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+
+#ifdef _WIN32
+# include <windows.h>
+# define PATH_MAX MAX_PATH
+typedef BOOLEAN WIN_BOOLEAN;
+#else
+# include <limits.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Sanitizes and validate a file path.
+ * @param filename The file path to be sanitized.
+ * @param sanitized_path The buffer to store the sanitized path.
+ * @param size The size of the buffer.
+ * @return true if the path is valid, false otherwise.
+ */
+bool sanitize_path(const char* filename, char* sanitized_path, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/samples/c/common/sample_util/src/path_util.c b/samples/c/common/sample_util/src/path_util.c
new file mode 100644
index 00000000000..eeb3714370b
--- /dev/null
+++ b/samples/c/common/sample_util/src/path_util.c
@@ -0,0 +1,51 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "samples_util/path_util.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+bool sanitize_path(const char* filename, char* sanitized_path, size_t max_len) {
+ if (filename == NULL) {
+ printf("[ERROR] File path is NULL.\n");
+ return false;
+ }
+
+#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)
+ if (GetFullPathNameA(filename, (DWORD)max_len, sanitized_path, NULL) == 0) {
+ printf("[ERROR] Invalid file path: %lu\n", GetLastError());
+ return false;
+ }
+#else
+ if (realpath(filename, sanitized_path) == NULL) {
+ printf("[ERROR] Invalid file path: %s\n", strerror(errno));
+ return false;
+ }
+#endif
+
+ // Check if the path length exceeds the maximum allowed length
+ if (strlen(sanitized_path) >= max_len) {
+ printf("[ERROR] File path is too long.\n");
+ return false;
+ }
+
+ // Check for directory traversal patterns
+ if (strstr(sanitized_path, "..") != NULL) {
+ printf("[ERROR] Path contains directory traversal patterns: %s\n", sanitized_path);
+ return false;
+ }
+
+ // Check can access the file
+ FILE* file = fopen(sanitized_path, "r");
+ if (file != NULL) {
+ fclose(file);
+ return true;
+ } else {
+ printf("[ERROR] File does not exist or cannot be accessed: %s\n", sanitized_path);
+ return false;
+ }
+}
diff --git a/samples/c/hello_classification/CMakeLists.txt b/samples/c/hello_classification/CMakeLists.txt
index 97e9cd6ac87..9548a59e8f2 100644
--- a/samples/c/hello_classification/CMakeLists.txt
+++ b/samples/c/hello_classification/CMakeLists.txt
@@ -5,4 +5,4 @@
ov_add_sample(NAME hello_classification_c
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.c;${CMAKE_CURRENT_SOURCE_DIR}/infer_result_util.c"
HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/infer_result_util.h"
- DEPENDENCIES opencv_c_wrapper)
+ DEPENDENCIES opencv_c_wrapper ie_samples_util_c)
diff --git a/samples/c/hello_classification/main.c b/samples/c/hello_classification/main.c
index 6b44c29527d..418dbcf8afe 100644
--- a/samples/c/hello_classification/main.c
+++ b/samples/c/hello_classification/main.c
@@ -2,13 +2,15 @@
// SPDX-License-Identifier: Apache-2.0
//
-#include <opencv_c_wrapper.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include "samples_util/path_util.h"
+// Uses windows.h must be before openvino/c/openvino.h
#include "infer_result_util.h"
+#include "opencv_c_wrapper.h"
#include "openvino/c/openvino.h"
void print_model_input_output_info(ov_model_t* model) {
@@ -49,9 +51,10 @@ int main(int argc, char** argv) {
struct infer_result* results = NULL;
ov_layout_t* input_layout = NULL;
ov_layout_t* model_layout = NULL;
- ov_shape_t input_shape;
+ ov_shape_t input_shape = {.rank = 0, .dims = NULL};
ov_output_const_port_t* output_port = NULL;
ov_output_const_port_t* input_port = NULL;
+ c_mat_t img = {0};
// -------- Get OpenVINO runtime version --------
ov_version_t version;
@@ -63,7 +66,10 @@ int main(int argc, char** argv) {
// -------- Parsing and validation of input arguments --------
const char* input_model = argv[1];
- const char* input_image_path = argv[2];
+ char input_image_path[PATH_MAX];
+ if (!sanitize_path(argv[2], input_image_path, sizeof(input_image_path))) {
+ goto err;
+ }
const char* device_name = argv[3];
// -------- Step 1. Initialize OpenVINO Runtime Core --------
@@ -87,7 +93,7 @@ int main(int argc, char** argv) {
}
// -------- Step 3. Set up input
- c_mat_t img;
+
image_read(input_image_path, &img);
ov_element_type_e input_type = U8;
int64_t dims[4] = {1, (size_t)img.mat_height, (size_t)img.mat_width, 3};
diff --git a/samples/c/hello_nv12_input_classification/CMakeLists.txt b/samples/c/hello_nv12_input_classification/CMakeLists.txt
index 34fe07a2d02..2c383e97267 100644
--- a/samples/c/hello_nv12_input_classification/CMakeLists.txt
+++ b/samples/c/hello_nv12_input_classification/CMakeLists.txt
@@ -3,4 +3,5 @@
#
ov_add_sample(NAME hello_nv12_input_classification_c
- SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.c")
+ SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/main.c"
+ DEPENDENCIES ie_samples_util_c)
diff --git a/samples/c/hello_nv12_input_classification/main.c b/samples/c/hello_nv12_input_classification/main.c
index 0e0d08ad95e..4f5b04987d9 100644
--- a/samples/c/hello_nv12_input_classification/main.c
+++ b/samples/c/hello_nv12_input_classification/main.c
@@ -7,6 +7,8 @@
#include <stdlib.h>
#include <string.h>
+#include "samples_util/path_util.h"
+// Uses windows.h must be before openvino/c/openvino.h
#include "openvino/c/openvino.h"
/**
@@ -198,7 +200,7 @@ int main(int argc, char** argv) {
ov_output_const_port_t* input_port = NULL;
ov_output_const_port_t* output_port = NULL;
ov_layout_t* model_layout = NULL;
- ov_shape_t input_shape;
+ ov_shape_t input_shape = {.rank = 0, .dims = NULL};
// -------- Get OpenVINO runtime version --------
ov_version_t version = {.description = NULL, .buildNumber = NULL};
@@ -210,7 +212,10 @@ int main(int argc, char** argv) {
// -------- Parsing and validation of input arguments --------
const char* input_model = argv[1];
- const char* input_image_path = argv[2];
+ char input_image_path[PATH_MAX];
+ if (!sanitize_path(argv[2], input_image_path, sizeof(input_image_path))) {
+ goto err;
+ }
const char* device_name = argv[4];
// -------- Step 1. Initialize OpenVINO Runtime Core --------