Skip to content

Commit 9ff9cbd

Browse files
committed
Merge 3.5 (os.urandom)
2 parents cb3a581 + dddf484 commit 9ff9cbd

File tree

7 files changed

+48
-12
lines changed

7 files changed

+48
-12
lines changed

Doc/library/os.rst

+10-3
Original file line numberDiff line numberDiff line change
@@ -3768,14 +3768,21 @@ Miscellaneous Functions
37683768

37693769
This function returns random bytes from an OS-specific randomness source. The
37703770
returned data should be unpredictable enough for cryptographic applications,
3771-
though its exact quality depends on the OS implementation. On a Unix-like
3772-
system this will query ``/dev/urandom``, and on Windows it will use
3773-
``CryptGenRandom()``. If a randomness source is not found,
3771+
though its exact quality depends on the OS implementation.
3772+
3773+
On Linux, ``getrandom()`` syscall is used if available and the urandom
3774+
entropy pool is initialized (``getrandom()`` does not block).
3775+
On a Unix-like system this will query ``/dev/urandom``. On Windows, it
3776+
will use ``CryptGenRandom()``. If a randomness source is not found,
37743777
:exc:`NotImplementedError` will be raised.
37753778

37763779
For an easy-to-use interface to the random number generator
37773780
provided by your platform, please see :class:`random.SystemRandom`.
37783781

3782+
.. versionchanged:: 3.5.2
3783+
On Linux, if ``getrandom()`` blocks (the urandom entropy pool is not
3784+
initialized yet), fall back on reading ``/dev/urandom``.
3785+
37793786
.. versionchanged:: 3.5
37803787
On Linux 3.17 and newer, the ``getrandom()`` syscall is now used
37813788
when available. On OpenBSD 5.6 and newer, the C ``getentropy()``

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ Ian Bruntlett
201201
Floris Bruynooghe
202202
Matt Bryant
203203
Stan Bubrouski
204+
Colm Buckley
204205
Erik de Bueger
205206
Jan-Hein Bührman
206207
Lars Buitinck

Misc/NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #26839: On Linux, :func:`os.urandom` now calls ``getrandom()`` with
31+
``GRND_NONBLOCK`` to fall back on reading ``/dev/urandom`` if the urandom
32+
entropy pool is not initialized yet. Patch written by Colm Buckley.
33+
3034
- Issue #23883: Added missing APIs to __all__ to match the documented APIs
3135
for the following modules: cgi, mailbox, mimetypes, plistlib and smtpd.
3236
Patches by Jacek Kołodziej.

Python/random.c

+21-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
# ifdef HAVE_SYS_STAT_H
77
# include <sys/stat.h>
88
# endif
9+
# ifdef HAVE_LINUX_RANDOM_H
10+
# include <linux/random.h>
11+
# endif
912
# ifdef HAVE_GETRANDOM
1013
# include <sys/random.h>
1114
# elif defined(HAVE_GETRANDOM_SYSCALL)
@@ -122,9 +125,13 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
122125
/* Is getrandom() supported by the running kernel?
123126
* Need Linux kernel 3.17 or newer, or Solaris 11.3 or newer */
124127
static int getrandom_works = 1;
125-
/* Use non-blocking /dev/urandom device. On Linux at boot, the getrandom()
126-
* syscall blocks until /dev/urandom is initialized with enough entropy. */
127-
const int flags = 0;
128+
129+
/* getrandom() on Linux will block if called before the kernel has
130+
* initialized the urandom entropy pool. This will cause Python
131+
* to hang on startup if called very early in the boot process -
132+
* see https://bugs.python.org/issue26839. To avoid this, use the
133+
* GRND_NONBLOCK flag. */
134+
const int flags = GRND_NONBLOCK;
128135
int n;
129136

130137
if (!getrandom_works)
@@ -168,6 +175,17 @@ py_getrandom(void *buffer, Py_ssize_t size, int raise)
168175
getrandom_works = 0;
169176
return 0;
170177
}
178+
if (errno == EAGAIN) {
179+
/* If we failed with EAGAIN, the entropy pool was
180+
* uninitialized. In this case, we return failure to fall
181+
* back to reading from /dev/urandom.
182+
*
183+
* Note: In this case the data read will not be random so
184+
* should not be used for cryptographic purposes. Retaining
185+
* the existing semantics for practical purposes. */
186+
getrandom_works = 0;
187+
return 0;
188+
}
171189

172190
if (errno == EINTR) {
173191
if (PyErr_CheckSignals()) {

configure

+5-3
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,7 @@ fi
28762876
ac_config_headers="$ac_config_headers pyconfig.h"
28772877

28782878

2879+
28792880
ac_aux_dir=
28802881
for ac_dir in "$srcdir" "$srcdir/.." "$srcdir/../.."; do
28812882
if test -f "$ac_dir/install-sh"; then
@@ -7568,7 +7569,7 @@ sys/param.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
75687569
sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
75697570
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
75707571
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
7571-
bluetooth/bluetooth.h linux/tipc.h spawn.h util.h alloca.h endian.h \
7572+
bluetooth/bluetooth.h linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
75727573
sys/endian.h
75737574
do :
75747575
as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh`
@@ -16431,12 +16432,13 @@ cat confdefs.h - <<_ACEOF >conftest.$ac_ext
1643116432
1643216433
#include <unistd.h>
1643316434
#include <sys/syscall.h>
16435+
#include <linux/random.h>
1643416436
1643516437
int main() {
1643616438
char buffer[1];
1643716439
const size_t buflen = sizeof(buffer);
16438-
const int flags = 0;
16439-
/* ignore the result, Python checks for ENOSYS at runtime */
16440+
const int flags = GRND_NONBLOCK;
16441+
/* ignore the result, Python checks for ENOSYS and EAGAIN at runtime */
1644016442
(void)syscall(SYS_getrandom, buffer, buflen, flags);
1644116443
return 0;
1644216444
}

configure.ac

+4-3
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ sys/param.h sys/select.h sys/sendfile.h sys/socket.h sys/statvfs.h \
18811881
sys/stat.h sys/syscall.h sys/sys_domain.h sys/termio.h sys/time.h \
18821882
sys/times.h sys/types.h sys/uio.h sys/un.h sys/utsname.h sys/wait.h pty.h \
18831883
libutil.h sys/resource.h netpacket/packet.h sysexits.h bluetooth.h \
1884-
bluetooth/bluetooth.h linux/tipc.h spawn.h util.h alloca.h endian.h \
1884+
bluetooth/bluetooth.h linux/tipc.h linux/random.h spawn.h util.h alloca.h endian.h \
18851885
sys/endian.h)
18861886
AC_HEADER_DIRENT
18871887
AC_HEADER_MAJOR
@@ -5246,12 +5246,13 @@ AC_LINK_IFELSE(
52465246
AC_LANG_SOURCE([[
52475247
#include <unistd.h>
52485248
#include <sys/syscall.h>
5249+
#include <linux/random.h>
52495250
52505251
int main() {
52515252
char buffer[1];
52525253
const size_t buflen = sizeof(buffer);
5253-
const int flags = 0;
5254-
/* ignore the result, Python checks for ENOSYS at runtime */
5254+
const int flags = GRND_NONBLOCK;
5255+
/* ignore the result, Python checks for ENOSYS and EAGAIN at runtime */
52555256
(void)syscall(SYS_getrandom, buffer, buflen, flags);
52565257
return 0;
52575258
}

pyconfig.h.in

+3
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@
574574
/* Define to 1 if you have the <linux/tipc.h> header file. */
575575
#undef HAVE_LINUX_TIPC_H
576576

577+
/* Define to 1 if you have the <linux/random.h> header file. */
578+
#undef HAVE_LINUX_RANDOM_H
579+
577580
/* Define to 1 if you have the `lockf' function. */
578581
#undef HAVE_LOCKF
579582

0 commit comments

Comments
 (0)