]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
sysdep: Add wrapper to get random bytes - update
authorOndrej Zajicek (work) <santiago@crfreenet.org>
Wed, 7 Apr 2021 23:15:17 +0000 (01:15 +0200)
committerOndrej Zajicek (work) <santiago@crfreenet.org>
Sun, 6 Jun 2021 14:26:06 +0000 (16:26 +0200)
Simplify the code and fix an issue with getentropy() return value.

conf/conf.c
configure.ac
lib/birdlib.h
sysdep/unix/random.c

index 0c355be42abe77649b4923ce7555868b08a48ea8..58abcde13dd42f6c05902c3736374a9b338cbcf6 100644 (file)
@@ -524,7 +524,6 @@ order_shutdown(int gr)
   c->gr_down = gr;
 
   config_commit(c, RECONFIG_HARD, 0);
-  random_close();
   shutting_down = 1;
 }
 
index ded258d34580cc405697fae49340a1fd59363bde..64181d2972cad2b6dbe6e997b5562639f5487d86 100644 (file)
@@ -376,7 +376,6 @@ fi
 
 AC_CHECK_FUNCS(getrandom)
 AC_CHECK_FUNCS(getentropy)
-AC_CHECK_HEADERS(sys/random.h)
 
 if test "$enable_debug" = yes ; then
   AC_DEFINE([DEBUGGING], [1], [Define to 1 if debugging is enabled])
index 61098f9290314ec921285c056f2cf8716e02f53f..fd20ef3a82c6f21073521ece0e05459a7dcee835 100644 (file)
@@ -192,8 +192,7 @@ asm(
 /* Pseudorandom numbers */
 
 u32 random_u32(void);
-int random_bytes(char *buf, size_t size);
-void random_close(void);
 void random_init(void);
+void random_bytes(void *buf, size_t size);
 
 #endif
index de81f3ca254b68de543f73d32c9144b4a264d411..4e64e56b8a9c0a2468196ca709cc073d1e6ef3af 100644 (file)
@@ -8,22 +8,18 @@
 
 #include <stdlib.h>
 #include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
 #include <errno.h>
 
 #include "sysdep/config.h"
+#include "nest/bird.h"
 
-#ifdef HAVE_SYS_STAT_H
-#  include <sys/stat.h>
-#endif
-#ifdef HAVE_LINUX_RANDOM_H
-#  include <linux/random.h>
-#endif
-#if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY))
-#    include <sys/random.h>
+#ifdef HAVE_GETRANDOM
+#include <sys/random.h>
 #endif
 
-#include "nest/bird.h"
 
 u32
 random_u32(void)
@@ -35,75 +31,65 @@ random_u32(void)
   return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
 }
 
-void
-random_init()
-{
-  char buf;
-  /* get a single random byte to trip any errors early */
-  random_bytes(&buf, sizeof(buf));
-}
 
-#if defined(HAVE_GETRANDOM) || defined(HAVE_GENTROPY)
+/* If there is no getrandom() / getentropy(), use /dev/urandom */
+#if !defined(HAVE_GETRANDOM) && !defined(HAVE_GETENTROPY)
+
+#define HAVE_URANDOM_FD 1
+static int urandom_fd = -1;
+
 int
-random_bytes(char *buf, size_t size)
+read_urandom_fd(void *buf, uint count)
 {
-  int n;
-  int flags = 0;
-  while (0 < size) {
-#if defined(HAVE_GETRANDOM)
-    n = getrandom(buf, size, flags);
-#else
-    n = getentropy(buf, size);
-#endif
-    if (n < 0) {
-      if (errno == EINTR)
-        continue;
-      die("Couldn't get random bytes: %m");
-    }
-    buf += n;
-    size -= n;
+  if (urandom_fd < 0)
+  {
+    urandom_fd = open("/dev/urandom", O_RDONLY);
+    if (urandom_fd < 0)
+      die("Cannot open /dev/urandom: %m");
   }
 
-  return 0;
+  return read(urandom_fd, buf, count);
 }
+#endif
 
-void random_close(void) {}
 
-#else
+void
+random_init(void)
+{
+  uint seed;
 
-static int urandom_fd = -1;
-int random_bytes(char *buf, size_t size)
+  /* Get random bytes to trip any errors early and to seed random() */
+  random_bytes(&seed, sizeof(seed));
+
+  srandom(seed);
+}
+
+void
+random_bytes(void *buf, size_t count)
 {
-  int n;
+  ASSERT(count <= 256);
 
-  if (urandom_fd < 0)
+  while (count > 0)
   {
-    urandom_fd = open("/dev/urandom", O_RDONLY);
-    if (urandom_fd < 0)
-      die("Couldn't open /dev/urandom: %m");
-  }
+    int n = -1;
 
-  do
-  {
-    n = read(urandom_fd, buf, size);
-    if (n <= 0) {
+#if defined(HAVE_GETRANDOM)
+    n = getrandom(buf, count, 0);
+#elif defined(HAVE_GETENTROPY)
+    n = getentropy(buf, count);
+    n = !n ? (int) count : n;
+#elif defined(HAVE_URANDOM_FD)
+    n = read_urandom_fd(buf, count);
+#endif
+
+    if (n < 0)
+    {
       if (errno == EINTR)
         continue;
-      die("Couldn't read from /dev/urandom: %m");
+      die("Cannot get random bytes: %m");
     }
-    buf += n;
-    size -= n;
-  } while (size > 0);
-
-  return 0;
-}
 
-void
-random_close(void)
-{
-  if (urandom_fd >= 0) {
-    close(urandom_fd);
-    urandom_fd = -1;
+    buf += n;
+    count -= n;
   }
 }
-#endif