]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
random: improve random logic
authorVictor Julien <victor@inliniac.net>
Sat, 15 Apr 2017 21:20:48 +0000 (23:20 +0200)
committerVictor Julien <victor@inliniac.net>
Tue, 18 Apr 2017 14:03:32 +0000 (16:03 +0200)
Improve random logic for hash tables.

Implement Windows random API if it is available.

configure.ac
src/defrag-hash.c
src/flow.c
src/host.c
src/ippair.c
src/util-random.c
src/util-random.h

index b833991b7b4c260048f571ea04f835974fef05e6..ef0aee1514d95934235e26c4dd15e9294ff1fbfe 100644 (file)
         #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_
     # 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=""
index 54206b2c73cc197877bb4d18813d4b068173d974..6203baa28d9397a84147e6b79581a032c0a193cf 100644 (file)
@@ -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;
index 728145552b85855dc2632830fc89e05a52a40088..8512e954ec4cb631eca06b2b6fd1e35a651cd18a 100644 (file)
@@ -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;
index 276d1b42ee176348ddfff84147263b3981956ffe..2e9061cc7d485a825f21e900995ff30d515aa646 100644 (file)
@@ -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;
index 29335996d044a58cebc6d2658e93e338c107db7b..66fce1e5a56127da7eb39d3e0c634db0e1b8a613 100644 (file)
@@ -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;
index 3cf5e0756f0e2358a4558d14957b13a31391d35d..17bd74baedc2a2137868a4d4efa79795fed8f499 100644 (file)
@@ -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
 /**
  * \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 "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 <wincrypt.h>
+
+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
index 9adddd01a5e5139a54b9b6673a2288e528ec826f..6376c05a3aaa8ef7d8bc190d8d530730fafcce61 100644 (file)
@@ -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
 /**
  * \file
  *
- * \author Pablo Rincon <pablo.rincon.crespo@gmail.com>
+ * \author Victor Julien <victor@inliniac.net>
  */
 
 #ifndef __UTIL_RANDOM_H__
 #define __UTIL_RANDOM_H__
 
-unsigned int RandomTimePreseed(void);
+long int RandomGet(void);
 
 #endif /* __UTIL_RANDOM_H__ */