diff --git a/configure.ac b/configure.ac index b833991b7b..ef0aee1514 100644 --- a/configure.ac +++ b/configure.ac @@ -142,7 +142,7 @@ #endif ]]) - AC_CHECK_HEADERS([windows.h winsock2.h ws2tcpip.h w32api/wtypes.h], [], [], + AC_CHECK_HEADERS([windows.h winsock2.h ws2tcpip.h w32api/wtypes.h wincrypt.h], [], [], [[ #ifndef _X86_ #define _X86_ @@ -170,7 +170,7 @@ # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC - AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strerror strncasecmp strtol strtoul memchr memrchr]) + AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strerror strncasecmp strtol strtoul memchr memrchr clock_gettime]) OCFLAGS=$CFLAGS CFLAGS="" diff --git a/src/defrag-hash.c b/src/defrag-hash.c index 54206b2c73..6203baa28d 100644 --- a/src/defrag-hash.c +++ b/src/defrag-hash.c @@ -134,9 +134,8 @@ void DefragInitConfig(char quiet) DefragTrackerQueueInit(&defragtracker_spare_q); #ifndef AFLFUZZ_NO_RANDOM - unsigned int seed = RandomTimePreseed(); /* set defaults */ - defrag_config.hash_rand = (int)(DEFRAG_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)); + defrag_config.hash_rand = (uint32_t)RandomGet(); #endif defrag_config.hash_size = DEFRAG_DEFAULT_HASHSIZE; defrag_config.memcap = DEFRAG_DEFAULT_MEMCAP; diff --git a/src/flow.c b/src/flow.c index 728145552b..8512e954ec 100644 --- a/src/flow.c +++ b/src/flow.c @@ -350,9 +350,8 @@ void FlowInitConfig(char quiet) FlowQueueInit(&flow_recycle_q); #ifndef AFLFUZZ_NO_RANDOM - unsigned int seed = RandomTimePreseed(); /* set defaults */ - flow_config.hash_rand = (int)( FLOW_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)); + flow_config.hash_rand = (uint32_t)RandomGet(); #endif flow_config.hash_size = FLOW_DEFAULT_HASHSIZE; flow_config.memcap = FLOW_DEFAULT_MEMCAP; diff --git a/src/host.c b/src/host.c index 276d1b42ee..2e9061cc7d 100644 --- a/src/host.c +++ b/src/host.c @@ -142,9 +142,8 @@ void HostInitConfig(char quiet) HostQueueInit(&host_spare_q); #ifndef AFLFUZZ_NO_RANDOM - unsigned int seed = RandomTimePreseed(); /* set defaults */ - host_config.hash_rand = (int)( HOST_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)); + host_config.hash_rand = (uint32_t)RandomGet(); #endif host_config.hash_size = HOST_DEFAULT_HASHSIZE; host_config.memcap = HOST_DEFAULT_MEMCAP; diff --git a/src/ippair.c b/src/ippair.c index 29335996d0..66fce1e5a5 100644 --- a/src/ippair.c +++ b/src/ippair.c @@ -138,9 +138,8 @@ void IPPairInitConfig(char quiet) IPPairQueueInit(&ippair_spare_q); #ifndef AFLFUZZ_NO_RANDOM - unsigned int seed = RandomTimePreseed(); /* set defaults */ - ippair_config.hash_rand = (int)( IPPAIR_DEFAULT_HASHSIZE * (rand_r(&seed) / RAND_MAX + 1.0)); + ippair_config.hash_rand = (uint32_t)RandomGet(); #endif ippair_config.hash_size = IPPAIR_DEFAULT_HASHSIZE; ippair_config.memcap = IPPAIR_DEFAULT_MEMCAP; diff --git a/src/util-random.c b/src/util-random.c index 3cf5e0756f..17bd74baed 100644 --- a/src/util-random.c +++ b/src/util-random.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2017 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -18,31 +18,54 @@ /** * \file * - * \author Pablo Rincon + * \author Victor Julien * - * Utility function for seeding rand + * Functions for getting a random value based on + * SEI CERT C Coding Standard MSC30-C */ #include "suricata-common.h" -#include "detect.h" -#include "threads.h" -#include "util-debug.h" -/** - * \brief create a seed number to pass to rand() , rand_r(), and similars - * \retval seed for rand() - */ -unsigned int RandomTimePreseed(void) +#if defined(HAVE_WINCRYPT_H) && defined(OS_WIN32) +#include + +long int RandomGet(void) { - /* preseed rand() */ - time_t now = time ( 0 ); - unsigned char *p = (unsigned char *)&now; - unsigned seed = 0; - size_t ind; + HCRYPTPROV p; + if (!(CryptAcquireContext(&p, NULL, NULL, + PROV_RSA_FULL, 0))) { + return -1; + } - for ( ind = 0; ind < sizeof now; ind++ ) - seed = seed * ( UCHAR_MAX + 2U ) + p[ind]; + long int value = 0; + if (!CryptGenRandom(p, sizeof(value), (BYTE *)&value)) { + (void)CryptReleaseContext(p, 0); + return -1; + } - return seed; + (void)CryptReleaseContext(prov, 0); + + return value; } +#elif defined(HAVE_CLOCK_GETTIME) +long int RandomGet(void) +{ + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + srandom(ts.tv_nsec ^ ts.tv_sec); + long int value = random(); + return value; +} +#else +long int RandomGet(void) +{ + struct timeval tv; + memset(&tv, 0, sizeof(tv)); + gettimeofday(&tv, NULL); + + srandom(tv.tv_usec ^ tv.tv_sec); + long int value = random(); + return value; +} +#endif diff --git a/src/util-random.h b/src/util-random.h index 9adddd01a5..6376c05a3a 100644 --- a/src/util-random.h +++ b/src/util-random.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2010 Open Information Security Foundation +/* Copyright (C) 2007-2017 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -18,13 +18,13 @@ /** * \file * - * \author Pablo Rincon + * \author Victor Julien */ #ifndef __UTIL_RANDOM_H__ #define __UTIL_RANDOM_H__ -unsigned int RandomTimePreseed(void); +long int RandomGet(void); #endif /* __UTIL_RANDOM_H__ */