From 43bfe2940275ef7fa5bc362814883d5252715122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sun, 2 Aug 2026 22:09:38 -0700 Subject: [PATCH] Fixed DLL search order hijacking in dlopen. (#413) --- src/os.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index be93dd0b..0a68b5ed 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -153,7 +153,12 @@ namespace bx void* dlopen(const FilePath& _filePath) { #if BX_PLATFORM_WINDOWS - return (void*)::LoadLibraryA(_filePath.getCPtr() ); + // LOAD_LIBRARY_SEARCH_DEFAULT_DIRS excludes CWD and %PATH% from the search order. + HMODULE handle = ::LoadLibraryExA(_filePath.getCPtr(), NULL, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); + + BX_WARN(NULL != handle, "dlopen failed: \"%s\" 0x%x.", _filePath.getCPtr(), ::GetLastError() ); + + return (void*)handle; #elif BX_PLATFORM_EMSCRIPTEN \ || BX_PLATFORM_PS4 \ || BX_PLATFORM_XBOXONE \ @@ -164,7 +169,7 @@ namespace bx return NULL; #else void* so = ::dlopen(_filePath.getCPtr(), RTLD_LOCAL|RTLD_LAZY); - BX_WARN(NULL != so, "dlopen failed: \"%s\".", ::dlerror() ); + BX_WARN(NULL != so, "dlopen failed: \"%s\" %s.", _filePath.getCPtr(), ::dlerror() ); return so; #endif // BX_PLATFORM_ }