Skip to content

Commit 68d5da1

Browse files
author
Divjot Arora
committed
GODRIVER-1757 Specify exact path for LoadLibrary call (#522)
1 parent 35df1be commit 68d5da1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

x/mongo/driver/auth/internal/gssapi/sspi_wrapper.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,32 @@ static const LPSTR SSPI_PACKAGE_NAME = "kerberos";
99
int sspi_init(
1010
)
1111
{
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);
1338
if (!sspi_secur32_dll) {
1439
return GetLastError();
1540
}

0 commit comments

Comments
 (0)