]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Add getrandom to k5_get_os_entropy() using syscall
authorRobbie Harwood <rharwood@redhat.com>
Wed, 14 Sep 2016 20:10:34 +0000 (16:10 -0400)
committerGreg Hudson <ghudson@mit.edu>
Thu, 22 Sep 2016 17:40:30 +0000 (13:40 -0400)
ticket: 8499

src/lib/crypto/krb/prng.c

index 9ad24c1bdf19691131a76c0e6b2fdbedd3fddfcc..22948a4971e47c8ba52f4f1fa97b9c2f020e55b6 100644 (file)
@@ -58,6 +58,9 @@ k5_get_os_entropy(unsigned char *buf, size_t len, int strong)
 #ifdef HAVE_SYS_STAT_H
 #include <sys/stat.h>
 #endif
+#ifdef __linux__
+#include <sys/syscall.h>
+#endif /* __linux__ */
 
 /* Open device, ensure that it is not a regular file, and read entropy.  Return
  * true on success, false on failure. */
@@ -96,6 +99,33 @@ krb5_boolean
 k5_get_os_entropy(unsigned char *buf, size_t len, int strong)
 {
     const char *device;
+#if defined(__linux__) && defined(SYS_getrandom)
+    int r;
+
+    while (len > 0) {
+        /*
+         * Pull from the /dev/urandom pool, but it to have been seeded.  This
+         * ensures strong randomness while only blocking during first system
+         * boot.
+         *
+         * glibc does not currently provide a binding for getrandom:
+         * https://sourceware.org/bugzilla/show_bug.cgi?id=17252
+         */
+        errno = 0;
+        r = syscall(SYS_getrandom, buf, len, 0);
+        if (r <= 0) {
+            if (errno == EINTR)
+                continue;
+
+            /* ENOSYS or other unrecoverable failure */
+            break;
+        }
+        len -= r;
+        buf += r;
+    }
+    if (len == 0)
+        return TRUE;
+#endif /* defined(__linux__) && defined(SYS_getrandom) */
 
     device = strong ? "/dev/random" : "/dev/urandom";
     return read_entropy_from_device(device, buf, len);