gentleknife / rpms / libgcrypt

Forked from rpms/libgcrypt 4 years ago
Clone

Blame SOURCES/libgcrypt-1.8.3-getrandom.patch

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