diff --git a/cmdline/mingw.c b/cmdline/mingw.c index 8d41c81e..1175041e 100644 --- a/cmdline/mingw.c +++ b/cmdline/mingw.c @@ -109,6 +109,12 @@ static BOOL (WINAPI* ptr_GetFileInformationByHandleEx)(HANDLE, DWORD, LPVOID, DW */ static BOOLEAN (WINAPI* ptr_CreateSymbolicLinkW)(LPWSTR, LPWSTR, DWORD); +/** + * Direct access to RtlGenRandom(). + * This function is accessible only with LoadLibrary() and it's availble from Windows XP. + */ +static BOOLEAN (WINAPI* ptr_RtlGenRandom)(PVOID, ULONG); + /** * Description of the last error. */ @@ -124,29 +130,49 @@ static int is_wine; */ static int is_scan_winfind; +/** + * Loaded ADVAPI32.DLL. + */ +static HMODULE advapi32; + void os_init(int opt) { - HMODULE kernel32, ntdll; + HMODULE ntdll, kernel32; is_scan_winfind = opt != 0; - kernel32 = GetModuleHandle("KERNEL32.DLL"); - if (!kernel32) { - fprintf(stderr, "Error loading the KERNEL32 module.\n"); - exit(EXIT_FAILURE); - } - ntdll = GetModuleHandle("NTDLL.DLL"); if (!ntdll) { fprintf(stderr, "Error loading the NTDLL module.\n"); exit(EXIT_FAILURE); } + kernel32 = GetModuleHandle("KERNEL32.DLL"); + if (!kernel32) { + fprintf(stderr, "Error loading the KERNEL32 module.\n"); + exit(EXIT_FAILURE); + } + + advapi32 = LoadLibrary("ADVAPI32.DLL"); + if (!advapi32) { + fprintf(stderr, "Error loading the ADVAPI32 module.\n"); + exit(EXIT_FAILURE); + } + + /* check for Wine presence */ + is_wine = GetProcAddress(ntdll, "wine_get_version") != 0; + /* load functions not always available */ ptr_SetThreadExecutionState = (void*)GetProcAddress(kernel32, "SetThreadExecutionState"); ptr_GetFileInformationByHandleEx = (void*)GetProcAddress(kernel32, "GetFileInformationByHandleEx"); ptr_CreateSymbolicLinkW = (void*)GetProcAddress(kernel32, "CreateSymbolicLinkW"); + ptr_RtlGenRandom = (void*)GetProcAddress(advapi32, "SystemFunction036"); + if (!ptr_RtlGenRandom) { + fprintf(stderr, "Error loading RtlGenRandom() from the ADVAPI32 module.\n"); + exit(EXIT_FAILURE); + } + /* set the thread execution level to avoid sleep */ if (ptr_SetThreadExecutionState) { /* first try for Windows 7 */ @@ -156,8 +182,6 @@ void os_init(int opt) } } - is_wine = GetProcAddress(ntdll, "wine_get_version") != 0; - last_error = 0; } @@ -169,6 +193,8 @@ void os_done(void) if (ptr_SetThreadExecutionState) { ptr_SetThreadExecutionState(WIN32_ES_CONTINUOUS); } + + FreeLibrary(advapi32); } /** @@ -1718,5 +1744,15 @@ uint64_t tick(void) return t.QuadPart; } +int windows_randomize(void* ptr, size_t size) +{ + if (!ptr_RtlGenRandom(ptr, size)) { + windows_errno(GetLastError()); + return -1; + } + + return 0; +} + #endif diff --git a/cmdline/mingw.h b/cmdline/mingw.h index b08cc33a..f1d120b4 100644 --- a/cmdline/mingw.h +++ b/cmdline/mingw.h @@ -115,6 +115,8 @@ #define pread windows_pread #undef pwrite #define pwrite windows_pwrite +#undef randomize +#define randomize windows_randomize /** * If nanoseconds are not supported, we report the special STAT_NSEC_INVALID value, @@ -352,6 +354,11 @@ ssize_t windows_pread(int f, void* buffer, size_t size, off_t offset); */ ssize_t windows_pwrite(int f, const void* buffer, size_t size, off_t offset); +/** + * Like randomize(). + */ +int windows_randomize(void* ptr, size_t size); + #endif #endif diff --git a/cmdline/rehash.c b/cmdline/rehash.c index 672c89ec..2116d572 100644 --- a/cmdline/rehash.c +++ b/cmdline/rehash.c @@ -56,7 +56,12 @@ void state_rehash(struct snapraid_state* state) /* set the new hash and seed */ state->hash = state->besthash; - randomize(state->hashseed, HASH_SIZE); + if (randomize(state->hashseed, HASH_SIZE) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Failed to get random values.\n"); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } /* mark all the block for rehashing */ for (i = 0; i < blockmax; ++i) { diff --git a/cmdline/snapraid.c b/cmdline/snapraid.c index c41953fb..ace113be 100644 --- a/cmdline/snapraid.c +++ b/cmdline/snapraid.c @@ -412,9 +412,6 @@ int main(int argc, char* argv[]) struct tm* tm; int i; - /* always different random numbers */ - srand(time(0)); - /* defaults */ config(conf, sizeof(conf), argv[0]); memset(&opt, 0, sizeof(opt)); diff --git a/cmdline/state.c b/cmdline/state.c index 26316a7a..eac77fdc 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -896,7 +896,12 @@ void state_config(struct snapraid_state* state, const char* path, const char* co state->hash = state->besthash; /* by default use a random hash seed */ - randomize(state->hashseed, HASH_SIZE); + if (randomize(state->hashseed, HASH_SIZE) != 0) { + /* LCOV_EXCL_START */ + fprintf(stderr, "Failed to get random values.\n"); + exit(EXIT_FAILURE); + /* LCOV_EXCL_STOP */ + } /* no previous hash by default */ state->prevhash = HASH_UNDEFINED; diff --git a/cmdline/unix.c b/cmdline/unix.c index 3a71c1e3..0ed89e35 100644 --- a/cmdline/unix.c +++ b/cmdline/unix.c @@ -300,6 +300,27 @@ uint64_t tick(void) #endif } +int randomize(void* ptr, size_t size) +{ + int f; + ssize_t ret; + + f = open("/dev/urandom", O_RDONLY); + if (f == -1) + return -1; + + ret = read(f, ptr, size); + if (ret < 0 || (size_t)ret != size) { + close(f); + return -1; + } + + if (close(f) != 0) + return -1; + + return 0; +} + void os_init(int opt) { (void)opt; diff --git a/cmdline/unix.h b/cmdline/unix.h index 210ea384..bd35e1f7 100644 --- a/cmdline/unix.h +++ b/cmdline/unix.h @@ -53,5 +53,10 @@ int dirent_hidden(struct dirent* dd); */ const char* stat_desc(struct stat* st); +/** + * Fill memory with psudeo-random values. + */ +int randomize(void* ptr, size_t size); + #endif diff --git a/cmdline/util.c b/cmdline/util.c index ecee1887..15708a84 100644 --- a/cmdline/util.c +++ b/cmdline/util.c @@ -1685,18 +1685,6 @@ const char* hash_config_name(unsigned kind) } } -/****************************************************************************/ -/* random */ - -void randomize(void* void_ptr, unsigned size) -{ - unsigned char* ptr = void_ptr; - unsigned i; - - for (i = 0; i < size; ++i) - ptr[i] = rand(); -} - /****************************************************************************/ /* lock */ diff --git a/cmdline/util.h b/cmdline/util.h index 23552efb..b6b952fb 100644 --- a/cmdline/util.h +++ b/cmdline/util.h @@ -526,14 +526,6 @@ void memhash(unsigned kind, const unsigned char* seed, void* digest, const void* */ const char* hash_config_name(unsigned kind); -/****************************************************************************/ -/* random */ - -/** - * Write random values. - */ -void randomize(void* ptr, unsigned size); - /****************************************************************************/ /* crc */