You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by yl...@apache.org on 2017/11/04 00:12:45 UTC

svn commit: r1814240 - in /apr/apr/trunk: configure.in misc/unix/rand.c

Author: ylavic
Date: Sat Nov  4 00:12:45 2017
New Revision: 1814240

URL: http://svn.apache.org/viewvc?rev=1814240&view=rev
Log:
rand: add support for getrandom() on Linux as an entropy source.

Use it for apr_generate_random_bytes() when available, reading from the
urandom source, and non-blocking such that the call fails with EINVAL if
there is not enough entropy on the system (which shouldn't be the case
in userspace).


Modified:
    apr/apr/trunk/configure.in
    apr/apr/trunk/misc/unix/rand.c

Modified: apr/apr/trunk/configure.in
URL: http://svn.apache.org/viewvc/apr/apr/trunk/configure.in?rev=1814240&r1=1814239&r2=1814240&view=diff
==============================================================================
--- apr/apr/trunk/configure.in (original)
+++ apr/apr/trunk/configure.in Sat Nov  4 00:12:45 2017
@@ -2453,6 +2453,9 @@ else
 fi
 
 dnl ----------------------------- Checking for /dev/random 
+AC_CHECK_HEADERS(sys/random.h)
+AC_CHECK_FUNCS(getrandom)
+
 AC_CHECK_FUNCS(arc4random_buf)
 
 AC_MSG_CHECKING(for entropy source)
@@ -2473,6 +2476,13 @@ AC_ARG_WITH(egd,
   ])
 
 if test "$rand" != "1"; then
+  if test "$ac_cv_func_getrandom" = yes; then
+    AC_MSG_RESULT(getrandom)
+    rand="1"
+  fi
+fi
+
+if test "$rand" != "1"; then
   if test "$ac_cv_func_arc4random_buf" = yes; then
     AC_MSG_RESULT(arc4random)
     rand="1"

Modified: apr/apr/trunk/misc/unix/rand.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/misc/unix/rand.c?rev=1814240&r1=1814239&r2=1814240&view=diff
==============================================================================
--- apr/apr/trunk/misc/unix/rand.c (original)
+++ apr/apr/trunk/misc/unix/rand.c Sat Nov  4 00:12:45 2017
@@ -42,6 +42,9 @@
 #elif defined(HAVE_SYS_UUID_H)
 #include <sys/uuid.h>
 #endif
+#ifdef HAVE_GETRANDOM
+#include <sys/random.h>
+#endif
 
 #ifndef SHUT_RDWR
 #define SHUT_RDWR 2
@@ -87,7 +90,24 @@ APR_DECLARE(apr_status_t) apr_os_uuid_ge
 APR_DECLARE(apr_status_t) apr_generate_random_bytes(unsigned char *buf, 
                                                     apr_size_t length)
 {
-#if defined(HAVE_ARC4RANDOM)
+#if defined(HAVE_GETRANDOM)
+
+    do {
+        int rc;
+
+        rc = getrandom(buf, length, GRND_NONBLOCK);
+        if (rc == -1) {
+            if (errno == EINTR) {
+                continue;
+            }
+            return errno;
+        }
+
+        buf += rc;
+        length -= rc;
+    } while (length > 0);
+
+#elif defined(HAVE_ARC4RANDOM)
 
     arc4random_buf(buf, length);