Uh oh!
There was an error while loading. Please reload this page.
Make Windows system-DLL P/Invokes use only DllImportSearchPath.System32 - #129588
Conversation
Add [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] to every P/Invoke targeting a Windows system DLL under Common/src/Interop/Windows (advapi32, bcrypt, crypt32, kernel32, ntdll, ole32, oleaut32, secur32, shell32, user32, normaliz, ucrtbase, ws2_32, winhttp, etc., plus api-set forwarders). By default these resolve via the assembly/app directory before System32, which lets a DLL planted next to the application win the search (DLL hijacking). Restricting the search to System32 (LOAD_LIBRARY_SEARCH_SYSTEM32) closes that vector. All targeted DLLs are OS components resident in %windir%\System32. App-local native libraries (hostpolicy, System.Globalization.Native, System.IO.Compression.Native, msquic) live outside these folders and are intentionally left unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…okes Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tagging subscribers to this area: @dotnet/interop-contrib |
DllImportSearchPath.System32tannergooding
commented
Jun 18, 2026
I wonder if it is worth discussion of adding some feature for this that doesn't require annotating every p/invoke. Some thoughts are:
CC. @AaronRobinsonMSFT, @dotnet/interop-contrib -- I don't think it's worth blocking this PR over, but this does seem like a more general problem that would be worth tackling. It would also reduce risk for future P/Invokes added and reduce burden on user binding libraries. |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Uh oh!
There was an error while loading. Please reload this page.
…m32` (#129588) Adds `[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]` to every P/Invoke that targets a Windows operating-system library under `src/libraries/Common/src/Interop/Windows/` (advapi32, bcrypt, crypt32, kernel32, ntdll, ole32, oleaut32, secur32, shell32, user32, normaliz, ucrtbase, ws2_32, winhttp, the api-set forwarders, etc.). SPCL and libraries specify `DllImportSearchPath.Assembly` and `DllImportSearchPath.System32`. The assembly directory (application directory for single-file) is always searched first. For correctness, we can restrict Windows system DLL p/invokes to System32. > [!NOTE] > This pull request was authored with the assistance of GitHub Copilot. --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Note
This pull request was authored with the assistance of GitHub Copilot.
Adds
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]to every P/Invoke that targets a Windows operating-system library undersrc/libraries/Common/src/Interop/Windows/(advapi32, bcrypt, crypt32, kernel32, ntdll, ole32, oleaut32, secur32, shell32, user32, normaliz, ucrtbase, ws2_32, winhttp, the api-set forwarders, etc.).SPCL and libraries specify
DllImportSearchPath.AssemblyandDllImportSearchPath.System32. The assembly directory (application directory for single-file) is always searched first. For correctness, we can restrict Windows system DLL p/invokes to System32.