From 3f1c180d5fbbb5015d692b3235bf33ec067789e5 Mon Sep 17 00:00:00 2001 From: Artur Gainullin Date: Tue, 9 Jan 2024 09:18:38 -0800 Subject: [PATCH] [SYCL] Limit the directories that are searched when loading plugins Currently the default search order is used when loading dependencies of the plugins (these depencies include the Level Zero loader and the ICD loader) and that list includes current directory and some other directories which are not considered safe. This patch limits the list of directories when loading the dependencies of the plugins. See: https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa for reference. --- .../pi_win_proxy_loader.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp b/sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp index f687e811cfe09..718373588835a 100644 --- a/sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp +++ b/sycl/pi_win_proxy_loader/pi_win_proxy_loader.cpp @@ -134,11 +134,22 @@ void preloadLibraries() { MapT &dllMap = getDllMap(); auto ocl_path = LibSYCLDir / __SYCL_OPENCL_PLUGIN_NAME; - dllMap.emplace(ocl_path, - LoadLibraryEx(ocl_path.wstring().c_str(), NULL, NULL)); - + // When searching for dependencies of the OpenCL plugin (which includes ICD + // loader) limit the list of directories to %windows%\system32 and the + // directory that contains the loaded DLL (OpenCL plugin). ICD loader is + // located in the same directory as OpenCL plugin. Standard library + // dependencies are in the system directory. + dllMap.emplace(ocl_path, LoadLibraryEx(ocl_path.wstring().c_str(), NULL, + LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | + LOAD_LIBRARY_SEARCH_SYSTEM32)); + + // When searching for dependencies of the Level Zero plugin (which includes + // level zero loader) limit the list of directories to %windows%\system32. + // In this case we know that the Level Zero Loader is located in the system + // directory. Standard library dependencies are in the system directory. auto l0_path = LibSYCLDir / __SYCL_LEVEL_ZERO_PLUGIN_NAME; - dllMap.emplace(l0_path, LoadLibraryEx(l0_path.wstring().c_str(), NULL, NULL)); + dllMap.emplace(l0_path, LoadLibraryEx(l0_path.wstring().c_str(), NULL, + LOAD_LIBRARY_SEARCH_SYSTEM32)); auto cuda_path = LibSYCLDir / __SYCL_CUDA_PLUGIN_NAME; dllMap.emplace(cuda_path,