Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fe02e39

Browse files
committed
Issue #22585: On OpenBSD 5.6 and newer, os.urandom() now calls getentropy(),
instead of reading /dev/urandom, to get pseudo-random bytes.
1 parent 94cb7a2 commit fe02e39

6 files changed

Lines changed: 56 additions & 9 deletions

File tree

Lib/test/test_os.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import decimal
2828
import fractions
2929
import pickle
30+
import sysconfig
3031
try:
3132
import threading
3233
except ImportError:
@@ -1053,6 +1054,12 @@ def test_urandom_subprocess(self):
10531054
data2 = self.get_urandom_subprocess(16)
10541055
self.assertNotEqual(data1, data2)
10551056

1057+
1058+
HAVE_GETENTROPY = (sysconfig.get_config_var('HAVE_GETENTROPY') == 1)
1059+
1060+
@unittest.skipIf(HAVE_GETENTROPY,
1061+
"getentropy() does not use a file descriptor")
1062+
class URandomFDTests(unittest.TestCase):
10561063
@unittest.skipUnless(resource, "test requires the resource module")
10571064
def test_urandom_failure(self):
10581065
# Check urandom() failing when it is not able to open /dev/random.

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ Core and Builtins
4141
Library
4242
-------
4343

44+
- Issue #22585: On OpenBSD 5.6 and newer, os.urandom() now calls getentropy(),
45+
instead of reading /dev/urandom, to get pseudo-random bytes.
46+
4447
- Issue #23112: Fix SimpleHTTPServer to correctly carry the query string and
4548
fragment when it redirects to add a trailing slash.
4649

Python/random.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ static int _Py_HashSecret_Initialized = 0;
1515
#endif
1616

1717
#ifdef MS_WINDOWS
18-
/* This handle is never explicitly released. Instead, the operating
19-
system will release it when the process terminates. */
2018
static HCRYPTPROV hCryptProv = 0;
2119

2220
static int
@@ -38,7 +36,7 @@ win32_urandom_init(int raise)
3836
}
3937

4038
/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
41-
API. Return 0 on success, or -1 on error. */
39+
API. Return 0 on success, or raise an exception and return -1 on error. */
4240
static int
4341
win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
4442
{
@@ -68,10 +66,35 @@ win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
6866
}
6967
return 0;
7068
}
71-
#endif /* MS_WINDOWS */
7269

70+
#elif HAVE_GETENTROPY
71+
/* Fill buffer with size pseudo-random bytes generated by getentropy().
72+
Return 0 on success, or raise an exception and return -1 on error.
7373
74-
#ifndef MS_WINDOWS
74+
If fatal is nonzero, call Py_FatalError() instead of raising an exception
75+
on error. */
76+
static int
77+
py_getentropy(unsigned char *buffer, Py_ssize_t size, int fatal)
78+
{
79+
while (size > 0) {
80+
Py_ssize_t len = Py_MIN(size, 256);
81+
int res = getentropy(buffer, len);
82+
if (res < 0) {
83+
if (fatal) {
84+
Py_FatalError("getentropy() failed");
85+
}
86+
else {
87+
PyErr_SetFromErrno(PyExc_OSError);
88+
return -1;
89+
}
90+
}
91+
buffer += len;
92+
size -= len;
93+
}
94+
return 0;
95+
}
96+
97+
#else
7598
static struct {
7699
int fd;
77100
dev_t st_dev;
@@ -203,7 +226,7 @@ dev_urandom_close(void)
203226
}
204227
}
205228

206-
#endif /* MS_WINDOWS */
229+
#endif /* HAVE_GETENTROPY */
207230

208231
/* Fill buffer with pseudo-random bytes generated by a linear congruent
209232
generator (LCG):
@@ -244,6 +267,8 @@ _PyOS_URandom(void *buffer, Py_ssize_t size)
244267

245268
#ifdef MS_WINDOWS
246269
return win32_urandom((unsigned char *)buffer, size, 1);
270+
#elif HAVE_GETENTROPY
271+
return py_getentropy(buffer, size, 0);
247272
#else
248273
return dev_urandom_python((char*)buffer, size);
249274
#endif
@@ -289,6 +314,8 @@ _PyRandom_Init(void)
289314
else {
290315
#ifdef MS_WINDOWS
291316
(void)win32_urandom(secret, secret_size, 0);
317+
#elif HAVE_GETENTROPY
318+
(void)py_getentropy(secret, secret_size, 1);
292319
#else
293320
dev_urandom_noraise(secret, secret_size);
294321
#endif
@@ -298,7 +325,14 @@ _PyRandom_Init(void)
298325
void
299326
_PyRandom_Fini(void)
300327
{
301-
#ifndef MS_WINDOWS
328+
#ifdef MS_WINDOWS
329+
if (hCryptProv) {
330+
CryptReleaseContext(hCryptProv, 0);
331+
hCryptProv = 0;
332+
}
333+
#elif HAVE_GETENTROPY
334+
/* nothing to clean */
335+
#else
302336
dev_urandom_close();
303337
#endif
304338
}

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10495,7 +10495,7 @@ fi
1049510495
for ac_func in alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
1049610496
clock confstr ctermid dup3 execv faccessat fchmod fchmodat fchown fchownat \
1049710497
fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
10498-
futimens futimes gai_strerror \
10498+
futimens futimes gai_strerror getentropy \
1049910499
getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \
1050010500
getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
1050110501
if_nameindex \

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,7 @@ fi
29352935
AC_CHECK_FUNCS(alarm accept4 setitimer getitimer bind_textdomain_codeset chown \
29362936
clock confstr ctermid dup3 execv faccessat fchmod fchmodat fchown fchownat \
29372937
fexecve fdopendir fork fpathconf fstatat ftime ftruncate futimesat \
2938-
futimens futimes gai_strerror \
2938+
futimens futimes gai_strerror getentropy \
29392939
getgrouplist getgroups getlogin getloadavg getpeername getpgid getpid \
29402940
getpriority getresuid getresgid getpwent getspnam getspent getsid getwd \
29412941
if_nameindex \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@
329329
/* Define this if you have flockfile(), getc_unlocked(), and funlockfile() */
330330
#undef HAVE_GETC_UNLOCKED
331331

332+
/* Define to 1 if you have the `getentropy' function. */
333+
#undef HAVE_GETENTROPY
334+
332335
/* Define to 1 if you have the `getgrouplist' function. */
333336
#undef HAVE_GETGROUPLIST
334337

0 commit comments

Comments
 (0)