[Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available

Jonathan Gray jsg at jsg.id.au
Thu Mar 23 04:24:16 UTC 2017


Instead of using using /dev/urandom on Linux and time(NULL) elsewhere
for a seed first use getentropy() for systems that have a kernel
interface to get a seed such as OpenBSD.  This interface is also
present in other systems such as Solaris and even Linux with a recent
version of glibc.

v2: check for/use the different header Solaris and glibc use

Signed-off-by: Jonathan Gray <jsg at jsg.id.au>
---
 configure.ac        |  4 ++++
 src/util/rand_xor.c | 17 +++++++++++++++--
 2 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/configure.ac b/configure.ac
index bc9c304c8d..c27646ca4c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -756,6 +756,7 @@ fi
 AC_HEADER_MAJOR
 AC_CHECK_HEADER([xlocale.h], [DEFINES="$DEFINES -DHAVE_XLOCALE_H"])
 AC_CHECK_HEADER([sys/sysctl.h], [DEFINES="$DEFINES -DHAVE_SYS_SYSCTL_H"])
+AC_CHECK_HEADER([sys/random.h], [DEFINES="$DEFINES -DHAVE_SYS_RANDOM_H"])
 AC_CHECK_FUNC([strtof], [DEFINES="$DEFINES -DHAVE_STRTOF"])
 AC_CHECK_FUNC([mkostemp], [DEFINES="$DEFINES -DHAVE_MKOSTEMP"])
 
@@ -788,6 +789,9 @@ esac
 dnl See if posix_memalign is available
 AC_CHECK_FUNC([posix_memalign], [DEFINES="$DEFINES -DHAVE_POSIX_MEMALIGN"])
 
+dnl See if getentropy is available
+AC_CHECK_FUNC([getentropy], [DEFINES="$DEFINES -DHAVE_GETENTROPY"])
+
 dnl Check for zlib
 PKG_CHECK_MODULES([ZLIB], [zlib >= $ZLIB_REQUIRED])
 
diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
index de05fa64b3..c613371f67 100644
--- a/src/util/rand_xor.c
+++ b/src/util/rand_xor.c
@@ -22,7 +22,15 @@
  *
  */
 
-#if defined(__linux__)
+#ifdef HAVE_GETENTROPY
+
+#ifdef HAVE_SYS_RANDOM_H
+#include <sys/random.h>
+#else
+#include <unistd.h>
+#endif
+
+#elif defined(__linux__)
 #include <sys/file.h>
 #include <unistd.h>
 #else
@@ -56,7 +64,12 @@ s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
    if (!randomised_seed)
       goto fixed_seed;
 
-#if defined(__linux__)
+#ifdef HAVE_GETENTROPY
+   size_t seed_size = sizeof(uint64_t) * 2;
+   if (getentropy(seed, seed_size) == -1)
+      goto fixed_seed;
+   return;
+#elif defined(__linux__)
    int fd = open("/dev/urandom", O_RDONLY);
    if (fd < 0)
       goto fixed_seed;
-- 
2.12.0



More information about the mesa-dev mailing list