diff -ru python3.5-3.5.1-orig/configure.ac python3.5-3.5.1/configure.ac --- python3.5-3.5.1-orig/configure.ac 2015-12-07 01:39:11.000000000 +0000 +++ python3.5-3.5.1/configure.ac 2016-05-13 09:00:55.356227833 +0100 @@ -1799,7 +1799,7 @@ sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \ sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \ libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \ -bluetooth/bluetooth.h linux/tipc.h spawn.h util.h alloca.h endian.h \ +bluetooth/bluetooth.h linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \ sys/endian.h) AC_HEADER_DIRENT AC_HEADER_MAJOR diff -ru python3.5-3.5.1-orig/pyconfig.h.in python3.5-3.5.1/pyconfig.h.in --- python3.5-3.5.1-orig/pyconfig.h.in 2015-12-07 01:39:11.000000000 +0000 +++ python3.5-3.5.1/pyconfig.h.in 2016-05-13 09:00:33.689556635 +0100 @@ -546,6 +546,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_LINUX_TIPC_H +/* Define to 1 if you have the header file. */ +#undef HAVE_LINUX_RANDOM_H + /* Define to 1 if you have the `lockf' function. */ #undef HAVE_LOCKF diff -ru python3.5-3.5.1-orig/Python/random.c python3.5-3.5.1/Python/random.c --- python3.5-3.5.1-orig/Python/random.c 2015-12-07 01:39:11.000000000 +0000 +++ python3.5-3.5.1/Python/random.c 2016-05-14 02:49:35.898574571 +0100 @@ -6,6 +6,9 @@ # ifdef HAVE_SYS_STAT_H # include # endif +# ifdef HAVE_LINUX_RANDOM_H +# include +# endif # ifdef HAVE_GETRANDOM # include # elif defined(HAVE_GETRANDOM_SYSCALL) @@ -122,9 +125,14 @@ /* Is getrandom() supported by the running kernel? * Need Linux kernel 3.17 or newer, or Solaris 11.3 or newer */ static int getrandom_works = 1; - /* Use non-blocking /dev/urandom device. On Linux at boot, the getrandom() - * syscall blocks until /dev/urandom is initialized with enough entropy. */ - const int flags = 0; + + /* getrandom() on Linux will block if called before the kernel has + * initialized the urandom entropy pool. This will cause Python + * to hang on startup if called very early in the boot process - + * see https://bugs.python.org/issue26839. To avoid this, use the + * GRND_NONBLOCK flag. */ + + const int flags = GRND_NONBLOCK; int n; if (!getrandom_works) @@ -161,6 +169,17 @@ getrandom_works = 0; return 0; } + if (errno == EAGAIN) { + /* If we failed with EAGAIN, the entropy pool was + * uninitialized. In this case, we return failure to fall + * back to reading from /dev/urandom. + * Note: in this case the data read will not be random so + * should not be used for cryptographic purposes. Retaining + * the existing semantics for practical purposes. */ + /* Alternative might be to return all-zeroes as a strong + * signal that these are not random data. */ + return 0; + } if (errno == EINTR) { if (PyErr_CheckSignals()) {