win: use RtlGenRandom from advapi32.dll directly

At least two people have reported that `LoadLibrary("advapi32.dll")`
fails in some configurations.

Libuv already links against advapi32.dll so let's sidestep the issue
by linking to `RtlGenRandom()` directly instead of looking it up at
runtime.

Fixes: https://github.com/libuv/libuv/issues/2759
PR-URL: https://github.com/libuv/libuv/pull/2762
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jameson Nash <vtjnash@gmail.com>
This commit is contained in:
Ben Noordhuis 2020-04-06 12:36:34 +02:00
parent e15a3c45d8
commit 335e8a6d12
3 changed files with 4 additions and 24 deletions

View File

@ -63,6 +63,9 @@
/* Maximum environment variable size, including the terminating null */
#define MAX_ENV_VAR_LENGTH 32767
/* A RtlGenRandom() by any other name... */
extern BOOLEAN NTAPI SystemFunction036(PVOID Buffer, ULONG BufferLength);
/* Cached copy of the process title, plus a mutex guarding it. */
static char *process_title;
static CRITICAL_SECTION process_title_lock;
@ -1862,13 +1865,10 @@ int uv_gettimeofday(uv_timeval64_t* tv) {
}
int uv__random_rtlgenrandom(void* buf, size_t buflen) {
if (pRtlGenRandom == NULL)
return UV_ENOSYS;
if (buflen == 0)
return 0;
if (pRtlGenRandom(buf, buflen) == FALSE)
if (SystemFunction036(buf, buflen) == FALSE)
return UV_EIO;
return 0;

View File

@ -36,9 +36,6 @@ sNtQueryDirectoryFile pNtQueryDirectoryFile;
sNtQuerySystemInformation pNtQuerySystemInformation;
sNtQueryInformationProcess pNtQueryInformationProcess;
/* Advapi32 function pointers */
sRtlGenRandom pRtlGenRandom;
/* Kernel32 function pointers */
sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;
@ -54,7 +51,6 @@ void uv_winapi_init(void) {
HMODULE powrprof_module;
HMODULE user32_module;
HMODULE kernel32_module;
HMODULE advapi32_module;
ntdll_module = GetModuleHandleA("ntdll.dll");
if (ntdll_module == NULL) {
@ -138,12 +134,4 @@ void uv_winapi_init(void) {
pSetWinEventHook = (sSetWinEventHook)
GetProcAddress(user32_module, "SetWinEventHook");
}
advapi32_module = GetModuleHandleA("advapi32.dll");
if (advapi32_module == NULL) {
uv_fatal_error(GetLastError(), "GetModuleHandleA");
}
pRtlGenRandom =
(sRtlGenRandom) GetProcAddress(advapi32_module, "SystemFunction036");
}

View File

@ -4589,11 +4589,6 @@ typedef NTSTATUS (NTAPI *sNtQueryInformationProcess)
ULONG Length,
PULONG ReturnLength);
/*
* Advapi32 headers
*/
typedef BOOLEAN (WINAPI *sRtlGenRandom)(PVOID Buffer, ULONG BufferLength);
/*
* Kernel32 headers
*/
@ -4736,9 +4731,6 @@ extern sNtQueryDirectoryFile pNtQueryDirectoryFile;
extern sNtQuerySystemInformation pNtQuerySystemInformation;
extern sNtQueryInformationProcess pNtQueryInformationProcess;
/* Advapi32 function pointers */
extern sRtlGenRandom pRtlGenRandom;
/* Kernel32 function pointers */
extern sGetQueuedCompletionStatusEx pGetQueuedCompletionStatusEx;