]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
random: support getrandom(2) if available 2944/head
authorVictor Julien <victor@inliniac.net>
Thu, 19 Oct 2017 07:51:18 +0000 (09:51 +0200)
committerVictor Julien <victor@inliniac.net>
Thu, 19 Oct 2017 08:44:27 +0000 (10:44 +0200)
Ticket: #2193

configure.ac
src/suricata-common.h
src/util-random.c

index b31307a1aede831289ab2fc05914d1d2144c73b7..9e7b8c9209c27440dd9893ff44a2a90f631d437b 100644 (file)
     AC_CHECK_HEADER(glob.h,,[AC_ERROR(glob.h not found ...)])
     AC_CHECK_HEADERS([dirent.h fnmatch.h])
     AC_CHECK_HEADERS([sys/resource.h sys/types.h sys/un.h])
+    AC_CHECK_HEADERS([sys/random.h])
 
     AC_CHECK_HEADERS([sys/socket.h net/if.h sys/mman.h linux/if_arp.h], [], [],
     [[#ifdef HAVE_SYS_SOCKET_H
     AC_FUNC_REALLOC
     AC_CHECK_FUNCS([gettimeofday memset strcasecmp strchr strdup strerror strncasecmp strtol strtoul memchr memrchr clock_gettime])
 
+    AC_CHECK_DECL([getrandom],
+        AC_DEFINE([HAVE_GETRANDOM], [1], [Use getrandom]),
+        [], [
+            #include <sys/random.h> 
+            ])
+
     OCFLAGS=$CFLAGS
     CFLAGS=""
     AC_CHECK_FUNCS([strlcpy strlcat])
index 41e969abd8aee56d9776bf49997e2b73cde4ef3d..ec34a8c6cea2191a0befee183c501f3d0f7b409b 100644 (file)
 #include <sys/mman.h>
 #endif
 
+#if HAVE_SYS_RANDOM_H
+#include <sys/random.h>
+#endif
+
 #if HAVE_NETINET_IN_H
 #include <netinet/in.h>
 #endif
index 35a29056ab1a9eaa5f515abc67112ab0a46a1b32..1abc036d335da79f5cc72a18f2e40a5a100446d0 100644 (file)
@@ -51,6 +51,21 @@ long int RandomGet(void)
 
     return value;
 }
+#elif defined(HAVE_GETRANDOM)
+long int RandomGet(void)
+{
+    if (g_disable_randomness)
+        return 0;
+
+    long int value = 0;
+    int ret = getrandom(&value, sizeof(value), 0);
+    /* ret should be sizeof(value), but if it is > 0 and < sizeof(value)
+     * it's still better than nothing so we return what we have */
+    if (ret <= 0) {
+        return -1;
+    }
+    return value;
+}
 #elif defined(HAVE_CLOCK_GETTIME)
 long int RandomGet(void)
 {