gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone

Blame SOURCES/libgcrypt-1.8.3-getrandom.patch

66e42d
diff -up libgcrypt-1.8.3/random/random.c.getrandom libgcrypt-1.8.3/random/random.c
66e42d
--- libgcrypt-1.8.3/random/random.c.getrandom	2017-11-23 19:16:58.000000000 +0100
66e42d
+++ libgcrypt-1.8.3/random/random.c	2018-07-10 15:38:34.303855808 +0200
66e42d
@@ -110,8 +110,8 @@ _gcry_random_read_conf (void)
66e42d
   unsigned int result = 0;
66e42d
 
66e42d
   fp = fopen (fname, "r");
66e42d
-  if (!fp)
66e42d
-    return result;
66e42d
+  if (!fp) /* We make only_urandom the default. */
66e42d
+    return RANDOM_CONF_ONLY_URANDOM;
66e42d
 
66e42d
   for (;;)
66e42d
     {
66e42d
diff -up libgcrypt-1.8.3/random/random-csprng.c.getrandom libgcrypt-1.8.3/random/random-csprng.c
66e42d
--- libgcrypt-1.8.3/random/random-csprng.c.getrandom	2017-11-23 19:16:58.000000000 +0100
66e42d
+++ libgcrypt-1.8.3/random/random-csprng.c	2018-06-14 16:31:04.731179208 +0200
66e42d
@@ -55,6 +55,10 @@
66e42d
 #ifdef __MINGW32__
66e42d
 #include <process.h>
66e42d
 #endif
66e42d
+#if defined(__linux__) && defined(HAVE_SYSCALL)
66e42d
+# include <sys/syscall.h>
66e42d
+# include <linux/random.h>
66e42d
+#endif
66e42d
 #include "g10lib.h"
66e42d
 #include "random.h"
66e42d
 #include "rand-internal.h"
66e42d
@@ -1116,6 +1120,22 @@ getfnc_gather_random (void))(void (*)(co
66e42d
              enum random_origins, size_t, int);
66e42d
 
66e42d
 #if USE_RNDLINUX
66e42d
+#if defined(__linux__) && defined(HAVE_SYSCALL) && defined(__NR_getrandom)
66e42d
+  long ret;
66e42d
+  char buffer[1];
66e42d
+
66e42d
+  _gcry_pre_syscall ();
66e42d
+  ret = syscall (__NR_getrandom,
66e42d
+                 (void*)buffer, (size_t)1, (unsigned int)GRND_NONBLOCK);
66e42d
+  _gcry_post_syscall ();
66e42d
+  if (ret != -1 || errno != ENOSYS)
66e42d
+    {
66e42d
+      fnc = _gcry_rndlinux_gather_random;
66e42d
+      return fnc;
66e42d
+    }
66e42d
+  else
66e42d
+          /* The syscall is not supported - fallback to /dev/urandom. */
66e42d
+#endif
66e42d
   if ( !access (NAME_OF_DEV_RANDOM, R_OK)
66e42d
        && !access (NAME_OF_DEV_URANDOM, R_OK))
66e42d
     {
66e42d
diff -up libgcrypt-1.8.3/random/rndlinux.c.getrandom libgcrypt-1.8.3/random/rndlinux.c
66e42d
--- libgcrypt-1.8.3/random/rndlinux.c.getrandom	2018-06-14 16:31:04.722178971 +0200
66e42d
+++ libgcrypt-1.8.3/random/rndlinux.c	2018-07-10 15:55:03.301075155 +0200
66e42d
@@ -35,6 +35,7 @@
66e42d
 #include <poll.h>
66e42d
 #if defined(__linux__) && defined(HAVE_SYSCALL)
66e42d
 # include <sys/syscall.h>
66e42d
+# include <linux/random.h>
66e42d
 #endif
66e42d
 
66e42d
 #include "types.h"
66e42d
@@ -204,6 +205,18 @@ _gcry_rndlinux_gather_random (void (*add
66e42d
     {
66e42d
       if (fd_urandom == -1)
66e42d
         {
66e42d
+#if defined(__linux__) && defined(HAVE_SYSCALL) && defined(__NR_getrandom)
66e42d
+          long ret;
66e42d
+
66e42d
+          _gcry_pre_syscall ();
66e42d
+          ret = syscall (__NR_getrandom,
66e42d
+                         (void*)buffer, (size_t)1, (unsigned int)GRND_NONBLOCK);
66e42d
+          _gcry_post_syscall ();
66e42d
+          if (ret > -1 || errno == EAGAIN || errno == EINTR)
66e42d
+            fd_urandom = -2;
66e42d
+	  else
66e42d
+            /* The syscall is not supported - fallback to /dev/urandom. */
66e42d
+#endif
66e42d
           fd_urandom = open_device (NAME_OF_DEV_URANDOM, (ever_opened & 2));
66e42d
           ever_opened |= 2;
66e42d
         }
66e42d
@@ -230,7 +243,7 @@ _gcry_rndlinux_gather_random (void (*add
66e42d
        * syscall and not a new device and thus we are not able to use
66e42d
        * select(2) to have a timeout. */
66e42d
 #if defined(__linux__) && defined(HAVE_SYSCALL) && defined(__NR_getrandom)
66e42d
-      if (fd == fd_urandom)
66e42d
+      if (fd == -2)
66e42d
         {
66e42d
           long ret;
66e42d
           size_t nbytes;
66e42d
@@ -246,9 +259,7 @@ _gcry_rndlinux_gather_random (void (*add
66e42d
               _gcry_post_syscall ();
66e42d
             }
66e42d
           while (ret == -1 && errno == EINTR);
66e42d
-          if (ret == -1 && errno == ENOSYS)
66e42d
-            ; /* The syscall is not supported - fallback to /dev/urandom.  */
66e42d
-          else
66e42d
+          if (1)
66e42d
             { /* The syscall is supported.  Some sanity checks.  */
66e42d
               if (ret == -1)
66e42d
                 log_fatal ("unexpected error from getrandom: %s\n",