random: improve random logic

Improve random logic for hash tables.

Implement Windows random API if it is available.
pull/2666/head
Victor Julien 9 years ago
parent ec964ebf84
commit dd70b3fda0

@ -142,7 +142,7 @@
#endif #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_ #ifndef _X86_
#define _X86_ #define _X86_
@ -170,7 +170,7 @@
# Checks for library functions. # Checks for library functions.
AC_FUNC_MALLOC AC_FUNC_MALLOC
AC_FUNC_REALLOC 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 OCFLAGS=$CFLAGS
CFLAGS="" CFLAGS=""

@ -134,9 +134,8 @@ void DefragInitConfig(char quiet)
DefragTrackerQueueInit(&defragtracker_spare_q); DefragTrackerQueueInit(&defragtracker_spare_q);
#ifndef AFLFUZZ_NO_RANDOM #ifndef AFLFUZZ_NO_RANDOM
unsigned int seed = RandomTimePreseed();
/* set defaults */ /* 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 #endif
defrag_config.hash_size = DEFRAG_DEFAULT_HASHSIZE; defrag_config.hash_size = DEFRAG_DEFAULT_HASHSIZE;
defrag_config.memcap = DEFRAG_DEFAULT_MEMCAP; defrag_config.memcap = DEFRAG_DEFAULT_MEMCAP;

@ -350,9 +350,8 @@ void FlowInitConfig(char quiet)
FlowQueueInit(&flow_recycle_q); FlowQueueInit(&flow_recycle_q);
#ifndef AFLFUZZ_NO_RANDOM #ifndef AFLFUZZ_NO_RANDOM
unsigned int seed = RandomTimePreseed();
/* set defaults */ /* 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 #endif
flow_config.hash_size = FLOW_DEFAULT_HASHSIZE; flow_config.hash_size = FLOW_DEFAULT_HASHSIZE;
flow_config.memcap = FLOW_DEFAULT_MEMCAP; flow_config.memcap = FLOW_DEFAULT_MEMCAP;

@ -142,9 +142,8 @@ void HostInitConfig(char quiet)
HostQueueInit(&host_spare_q); HostQueueInit(&host_spare_q);
#ifndef AFLFUZZ_NO_RANDOM #ifndef AFLFUZZ_NO_RANDOM
unsigned int seed = RandomTimePreseed();
/* set defaults */ /* 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 #endif
host_config.hash_size = HOST_DEFAULT_HASHSIZE; host_config.hash_size = HOST_DEFAULT_HASHSIZE;
host_config.memcap = HOST_DEFAULT_MEMCAP; host_config.memcap = HOST_DEFAULT_MEMCAP;

@ -138,9 +138,8 @@ void IPPairInitConfig(char quiet)
IPPairQueueInit(&ippair_spare_q); IPPairQueueInit(&ippair_spare_q);
#ifndef AFLFUZZ_NO_RANDOM #ifndef AFLFUZZ_NO_RANDOM
unsigned int seed = RandomTimePreseed();
/* set defaults */ /* 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 #endif
ippair_config.hash_size = IPPAIR_DEFAULT_HASHSIZE; ippair_config.hash_size = IPPAIR_DEFAULT_HASHSIZE;
ippair_config.memcap = IPPAIR_DEFAULT_MEMCAP; ippair_config.memcap = IPPAIR_DEFAULT_MEMCAP;

@ -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 * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -18,31 +18,54 @@
/** /**
* \file * \file
* *
* \author Pablo Rincon <pablo.rincon.crespo@gmail.com> * \author Victor Julien <victor@inliniac.net>
* *
* 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 "suricata-common.h"
#include "detect.h"
#include "threads.h"
#include "util-debug.h"
/** #if defined(HAVE_WINCRYPT_H) && defined(OS_WIN32)
* \brief create a seed number to pass to rand() , rand_r(), and similars #include <wincrypt.h>
* \retval seed for rand()
*/ long int RandomGet(void)
unsigned int RandomTimePreseed(void)
{ {
/* preseed rand() */ HCRYPTPROV p;
time_t now = time ( 0 ); if (!(CryptAcquireContext(&p, NULL, NULL,
unsigned char *p = (unsigned char *)&now; PROV_RSA_FULL, 0))) {
unsigned seed = 0; return -1;
size_t ind; }
for ( ind = 0; ind < sizeof now; ind++ ) long int value = 0;
seed = seed * ( UCHAR_MAX + 2U ) + p[ind]; 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

@ -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 * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -18,13 +18,13 @@
/** /**
* \file * \file
* *
* \author Pablo Rincon <pablo.rincon.crespo@gmail.com> * \author Victor Julien <victor@inliniac.net>
*/ */
#ifndef __UTIL_RANDOM_H__ #ifndef __UTIL_RANDOM_H__
#define __UTIL_RANDOM_H__ #define __UTIL_RANDOM_H__
unsigned int RandomTimePreseed(void); long int RandomGet(void);
#endif /* __UTIL_RANDOM_H__ */ #endif /* __UTIL_RANDOM_H__ */

Loading…
Cancel
Save