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
Don't allow to load library linking to another folder (#25958)
Xuejun Zhai2bfb63046103
diff --git a/src/common/util/CMakeLists.txt b/src/common/util/CMakeLists.txtindex 6b5cf4b7a64..efdae4b2683 100644--- a/src/common/util/CMakeLists.txt+++ b/src/common/util/CMakeLists.txt@@ -41,6 +41,10 @@ target_link_libraries(${TARGET_NAME} PRIVATE ${CMAKE_DL_LIBS} PUBLIC openvino::pif (WIN32)target_link_libraries(${TARGET_NAME} PRIVATE Shlwapi)endif()++if(LINUX)+ target_link_libraries(${TARGET_NAME} PRIVATE stdc++fs)+endif()target_include_directories(${TARGET_NAME} PUBLIC $<BUILD_INTERFACE:${UTIL_INCLUDE_DIR}>)ov_add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME})diff --git a/src/common/util/include/openvino/util/file_util.hpp b/src/common/util/include/openvino/util/file_util.hppindex 88b9110abec..7b28bda704a 100644--- a/src/common/util/include/openvino/util/file_util.hpp+++ b/src/common/util/include/openvino/util/file_util.hpp@@ -12,6 +12,20 @@#include "openvino/util/util.hpp"+#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)+# if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L) && (_MSC_VER >= 1913))+# if __has_include(<filesystem>)+# include <filesystem>+namespace fs = std::filesystem;+# endif+# else+# define _SILENCE_EXPERIMENTAL_FILESYSTEM_DEPRECATION_WARNING+# define _LIBCPP_NO_EXPERIMENTAL_DEPRECATION_WARNING_FILESYSTEM+# include <experimental/filesystem>+namespace fs = std::experimental::filesystem;+# endif+#endif+namespace ov {namespace util {@@ -397,5 +411,12 @@ inline std::basic_string<C> make_path(const std::basic_string<C>& folder, constreturn folder + ov::util::FileTraits<C>::file_separator + file;}+/**+ * @brief If the library path is symlink & the symlink file location is not in the real file folder, return true. Others+ * return false.+ * @param path - Library file path to check+ * @return Bool value+ */+bool is_symlink_in_different_path(std::string library_path);} // namespace util} // namespace ovdiff --git a/src/common/util/src/file_util.cpp b/src/common/util/src/file_util.cppindex 2846355e9ae..8fa9e5e06b1 100644--- a/src/common/util/src/file_util.cpp+++ b/src/common/util/src/file_util.cpp@@ -704,3 +704,14 @@ const char* ov::util::trim_file_name(const char* const fname) {#endifreturn fname_trim_ptr;}++bool ov::util::is_symlink_in_different_path(std::string library_path) {+#if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)+ if (fs::is_symlink(library_path) && ov::util::get_absolute_file_path(ov::util::get_directory(library_path)) !=+ ov::util::get_directory(ov::util::get_absolute_file_path(library_path)))+ return true;+ return false;+#else+ return false;+#endif+}diff --git a/src/core/dev_api/openvino/core/so_extension.hpp b/src/core/dev_api/openvino/core/so_extension.hppindex 931344b5fc4..5db9879dc1c 100644--- a/src/core/dev_api/openvino/core/so_extension.hpp+++ b/src/core/dev_api/openvino/core/so_extension.hpp@@ -3,6 +3,7 @@//#pragma once+#include "openvino/core/except.hpp"#include "openvino/core/extension.hpp"#include "openvino/core/visibility.hpp"#include "openvino/util/file_util.hpp"@@ -40,6 +41,8 @@ inline std::string resolve_extension_path(const std::string& path) {}inline std::vector<Extension::Ptr> load_extensions(const std::string& path) {+ OPENVINO_ASSERT(!ov::util::is_symlink_in_different_path(path),+ "Cannot load extension with symlink path: \"" + path + "\".");const std::string resolved_path = resolve_extension_path(path);auto so = ov::util::load_shared_object(resolved_path.c_str());using CreateFunction = void(std::vector<Extension::Ptr>&);diff --git a/src/frontends/common/src/manager.cpp b/src/frontends/common/src/manager.cppindex 0a31f9a2c42..30bfc0a46c2 100644--- a/src/frontends/common/src/manager.cpp+++ b/src/frontends/common/src/manager.cpp@@ -106,6 +106,8 @@ public:}void register_front_end(const std::string& name, const std::string& library_path) {+ OPENVINO_ASSERT(!ov::util::is_symlink_in_different_path(library_path),+ "Cannot register plugin with symlink path: \"" + library_path + "\".");auto lib_path = ov::util::from_file_path(ov::util::get_plugin_path(library_path));PluginInfo plugin;plugin.m_file_path = lib_path;diff --git a/src/inference/src/dev/core_impl.cpp b/src/inference/src/dev/core_impl.cppindex 237c246ab38..f8fcf26b006 100644--- a/src/inference/src/dev/core_impl.cpp+++ b/src/inference/src/dev/core_impl.cpp@@ -1189,7 +1189,8 @@ void ov::CoreImpl::register_plugin(const std::string& plugin,if (device_name.find('.') != std::string::npos) {OPENVINO_THROW("Device name must not contain dot '.' symbol");}-+ OPENVINO_ASSERT(!ov::util::is_symlink_in_different_path(plugin),+ "Cannot registe plugin with symlink path: \"" + plugin + "\".");PluginDescriptor desc{ov::util::get_plugin_path(plugin), properties};register_plugin_in_registry_unsafe(device_name, desc);}diff --git a/src/inference/tests/functional/ov_core_test.cpp b/src/inference/tests/functional/ov_core_test.cppindex 26eb38e3fd1..acba5cc4912 100644--- a/src/inference/tests/functional/ov_core_test.cpp+++ b/src/inference/tests/functional/ov_core_test.cpp@@ -95,4 +95,22 @@ TEST(CoreBaseTest, LoadOVFolderOverCWPathPluginXML) {remove_plugin_xml(ov_file_path);}+# if !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)+TEST(CoreBaseTest, AddExtensionwithSymlinkInDiffPlace) {+ std::string openvino_template_extension =+ ov::util::make_plugin_library_name<char>(ov::test::utils::getExecutableDirectory(),+ std::string("openvino_template_extension") + OV_BUILD_POSTFIX);++ // Symlink file & the real file doesn't locale in the diff place. Will throw+ fs::create_directory("test_link");+ std::string symlink_for_extension_copy_file = "test_link/symlink_for_extension_copy_file";++ fs::create_symlink(openvino_template_extension, symlink_for_extension_copy_file);+ ov::Core core;+ EXPECT_NO_THROW(core.add_extension(openvino_template_extension));+ EXPECT_THROW(core.add_extension(symlink_for_extension_copy_file), std::runtime_error);++ ASSERT_TRUE(fs::remove_all("test_link"));+}+# endif#endif