File tree Expand file tree Collapse file tree 1 file changed +26
-1
lines changed
x/mongo/driver/auth/internal/gssapi Expand file tree Collapse file tree 1 file changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -9,7 +9,32 @@ static const LPSTR SSPI_PACKAGE_NAME = "kerberos";
9
9
int sspi_init (
10
10
)
11
11
{
12
- sspi_secur32_dll = LoadLibrary ("secur32.dll" );
12
+ // Load the secur32.dll library using its exact path. Passing the exact DLL path rather than allowing LoadLibrary to
13
+ // search in different locations removes the possibility of DLL preloading attacks. We use GetSystemDirectoryA and
14
+ // LoadLibraryA rather than the GetSystemDirectory/LoadLibrary aliases to ensure the ANSI versions are used so we
15
+ // don't have to account for variations in char sizes if UNICODE is enabled.
16
+
17
+ // Passing a 0 size will return the required buffer length to hold the path, including the null terminator.
18
+ int requiredLen = GetSystemDirectoryA (NULL , 0 );
19
+ if (!requiredLen ) {
20
+ return GetLastError ();
21
+ }
22
+
23
+ // Allocate a buffer to hold the system directory + "\secur32.dll" (length 12, not including null terminator).
24
+ int actualLen = requiredLen + 12 ;
25
+ char * directoryBuffer = (char * ) calloc (1 , actualLen );
26
+ int directoryLen = GetSystemDirectoryA (directoryBuffer , actualLen );
27
+ if (!directoryLen ) {
28
+ free (directoryBuffer );
29
+ return GetLastError ();
30
+ }
31
+
32
+ // Append the DLL name to the buffer.
33
+ char * dllName = "\\secur32.dll" ;
34
+ strcpy_s (& (directoryBuffer [directoryLen ]), actualLen - directoryLen , dllName );
35
+
36
+ sspi_secur32_dll = LoadLibraryA (directoryBuffer );
37
+ free (directoryBuffer );
13
38
if (!sspi_secur32_dll ) {
14
39
return GetLastError ();
15
40
}
You can’t perform that action at this time.
0 commit comments