All rules
CA5393Security Enabled by default: No
Do not use unsafe DllImportSearchPath value
Do not use unsafe DllImportSearchPath value
Microsoft docsDescription
There could be a malicious DLL in the default DLL search directories and assembly directories. Or, depending on where your application is run from, there could be a malicious DLL in the application's directory.
For more information, see Load Library Safely.
Cause
Using one of the unsafe values of System.Runtime.InteropServices.DllImportSearchPath
How to fix violations
Use safe values of System.Runtime.InteropServices.DllImportSearchPath to specify an explicit search path instead:
SafeDirectoriesSystem32UserDirectories
Example
using System;
using System.Runtime.InteropServices;
class ExampleClass
{
[DllImport("The3rdAssembly.dll")]
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
public void ExampleMethod()
{
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}
using System;
using System.Runtime.InteropServices;
class ExampleClass
{
[DllImport("The3rdAssembly.dll")]
[DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
public void ExampleMethod()
{
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}When to suppress
It's safe to suppress this rule if:
- You're sure the loaded assembly is what you want.
- The imported assembly is a commonly used system assembly, like user32.dll, and the search path strategy follows the Known DLLs mechanism.
Your vote
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0