Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #ifdef MS_WINDOWS |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 3 | # include <windows.h> |
Martin Panter | d2f8747 | 2016-07-29 04:00:44 +0000 | [diff] [blame] | 4 | /* All sample MSDN wincrypt programs include the header below. It is at least |
| 5 | * required with Min GW. */ |
| 6 | # include <wincrypt.h> |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 7 | #else |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 8 | # include <fcntl.h> |
| 9 | # ifdef HAVE_SYS_STAT_H |
| 10 | # include <sys/stat.h> |
| 11 | # endif |
Victor Stinner | dddf484 | 2016-06-07 11:21:42 +0200 | [diff] [blame] | 12 | # ifdef HAVE_LINUX_RANDOM_H |
| 13 | # include <linux/random.h> |
| 14 | # endif |
Benjamin Peterson | 493ac1b | 2017-01-01 22:29:36 -0600 | [diff] [blame] | 15 | # if defined(HAVE_SYS_RANDOM_H) && (defined(HAVE_GETRANDOM) || defined(HAVE_GETENTROPY)) |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 16 | # include <sys/random.h> |
Ned Deily | 7ae4112 | 2016-11-12 16:35:48 -0500 | [diff] [blame] | 17 | # endif |
| 18 | # if !defined(HAVE_GETRANDOM) && defined(HAVE_GETRANDOM_SYSCALL) |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 19 | # include <sys/syscall.h> |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 20 | # endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 21 | #endif |
| 22 | |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 23 | #ifdef Py_DEBUG |
| 24 | int _Py_HashSecret_Initialized = 0; |
| 25 | #else |
| 26 | static int _Py_HashSecret_Initialized = 0; |
| 27 | #endif |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 28 | |
| 29 | #ifdef MS_WINDOWS |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 30 | static HCRYPTPROV hCryptProv = 0; |
| 31 | |
| 32 | static int |
| 33 | win32_urandom_init(int raise) |
| 34 | { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 35 | /* Acquire context */ |
Martin v. Löwis | 3f50bf6 | 2013-01-25 14:06:18 +0100 | [diff] [blame] | 36 | if (!CryptAcquireContext(&hCryptProv, NULL, NULL, |
| 37 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 38 | goto error; |
| 39 | |
| 40 | return 0; |
| 41 | |
| 42 | error: |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 43 | if (raise) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 44 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 45 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 46 | return -1; |
| 47 | } |
| 48 | |
| 49 | /* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen |
Victor Stinner | 4d6a3d6 | 2014-12-21 01:16:38 +0100 | [diff] [blame] | 50 | API. Return 0 on success, or raise an exception and return -1 on error. */ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 51 | static int |
| 52 | win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise) |
| 53 | { |
| 54 | Py_ssize_t chunk; |
| 55 | |
| 56 | if (hCryptProv == 0) |
| 57 | { |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 58 | if (win32_urandom_init(raise) == -1) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 59 | return -1; |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 60 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | while (size > 0) |
| 64 | { |
| 65 | chunk = size > INT_MAX ? INT_MAX : size; |
Victor Stinner | 0c08346 | 2013-11-15 23:26:25 +0100 | [diff] [blame] | 66 | if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer)) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 67 | { |
| 68 | /* CryptGenRandom() failed */ |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 69 | if (raise) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 70 | PyErr_SetFromWindowsErr(0); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 71 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 72 | return -1; |
| 73 | } |
| 74 | buffer += chunk; |
| 75 | size -= chunk; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 79 | |
Victor Stinner | dcdb60e | 2017-01-06 11:16:20 +0100 | [diff] [blame] | 80 | #else /* !MS_WINDOWS */ |
| 81 | |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 82 | #if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL) |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 83 | #define PY_GETRANDOM 1 |
| 84 | |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 85 | /* Call getrandom() to get random bytes: |
| 86 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 87 | - Return 1 on success |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 88 | - Return 0 if getrandom() is not available (failed with ENOSYS or EPERM), |
| 89 | or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom not |
| 90 | initialized yet) and raise=0. |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 91 | - Raise an exception (if raise is non-zero) and return -1 on error: |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 92 | if getrandom() failed with EINTR, raise is non-zero and the Python signal |
| 93 | handler raised an exception, or if getrandom() failed with a different |
| 94 | error. |
| 95 | |
| 96 | getrandom() is retried if it failed with EINTR: interrupted by a signal. */ |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 97 | static int |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 98 | py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise) |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 99 | { |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 100 | /* Is getrandom() supported by the running kernel? Set to 0 if getrandom() |
Victor Stinner | 6d8bc46 | 2016-09-20 22:46:02 +0200 | [diff] [blame] | 101 | failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris |
Victor Stinner | af59732 | 2016-09-20 22:26:18 +0200 | [diff] [blame] | 102 | 11.3 or newer */ |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 103 | static int getrandom_works = 1; |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 104 | int flags; |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 105 | char *dest; |
Victor Stinner | ec721f3 | 2016-06-16 23:53:47 +0200 | [diff] [blame] | 106 | long n; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 107 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 108 | if (!getrandom_works) { |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 109 | return 0; |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 110 | } |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 111 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 112 | flags = blocking ? 0 : GRND_NONBLOCK; |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 113 | dest = buffer; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 114 | while (0 < size) { |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 115 | #ifdef sun |
| 116 | /* Issue #26735: On Solaris, getrandom() is limited to returning up |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 117 | to 1024 bytes. Call it multiple times if more bytes are |
| 118 | requested. */ |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 119 | n = Py_MIN(size, 1024); |
| 120 | #else |
Victor Stinner | ec721f3 | 2016-06-16 23:53:47 +0200 | [diff] [blame] | 121 | n = Py_MIN(size, LONG_MAX); |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 122 | #endif |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 123 | |
Victor Stinner | 9d24271 | 2016-04-12 22:28:49 +0200 | [diff] [blame] | 124 | errno = 0; |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 125 | #ifdef HAVE_GETRANDOM |
| 126 | if (raise) { |
| 127 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 128 | n = getrandom(dest, n, flags); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 129 | Py_END_ALLOW_THREADS |
| 130 | } |
| 131 | else { |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 132 | n = getrandom(dest, n, flags); |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 133 | } |
| 134 | #else |
| 135 | /* On Linux, use the syscall() function because the GNU libc doesn't |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 136 | expose the Linux getrandom() syscall yet. See: |
| 137 | https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */ |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 138 | if (raise) { |
| 139 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 140 | n = syscall(SYS_getrandom, dest, n, flags); |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 141 | Py_END_ALLOW_THREADS |
| 142 | } |
| 143 | else { |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 144 | n = syscall(SYS_getrandom, dest, n, flags); |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 145 | } |
Victor Stinner | bae2d62 | 2015-10-01 09:47:30 +0200 | [diff] [blame] | 146 | #endif |
Victor Stinner | 79b74ae | 2015-03-30 11:16:40 +0200 | [diff] [blame] | 147 | |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 148 | if (n < 0) { |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 149 | /* ENOSYS: the syscall is not supported by the kernel. |
| 150 | EPERM: the syscall is blocked by a security policy (ex: SECCOMP) |
| 151 | or something else. */ |
Victor Stinner | 6d8bc46 | 2016-09-20 22:46:02 +0200 | [diff] [blame] | 152 | if (errno == ENOSYS || errno == EPERM) { |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 153 | getrandom_works = 0; |
| 154 | return 0; |
| 155 | } |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 156 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 157 | /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system urandom |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 158 | is not initialiazed yet. For _PyRandom_Init(), we ignore the |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 159 | error and fall back on reading /dev/urandom which never blocks, |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 160 | even if the system urandom is not initialized yet: |
| 161 | see the PEP 524. */ |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 162 | if (errno == EAGAIN && !raise && !blocking) { |
Victor Stinner | dddf484 | 2016-06-07 11:21:42 +0200 | [diff] [blame] | 163 | return 0; |
| 164 | } |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 165 | |
| 166 | if (errno == EINTR) { |
Victor Stinner | cecdd96 | 2016-08-16 15:19:09 +0200 | [diff] [blame] | 167 | if (raise) { |
| 168 | if (PyErr_CheckSignals()) { |
| 169 | return -1; |
| 170 | } |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 171 | } |
Victor Stinner | cecdd96 | 2016-08-16 15:19:09 +0200 | [diff] [blame] | 172 | |
| 173 | /* retry getrandom() if it was interrupted by a signal */ |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 174 | continue; |
| 175 | } |
| 176 | |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 177 | if (raise) { |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 178 | PyErr_SetFromErrno(PyExc_OSError); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 179 | } |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
Victor Stinner | cfb1961 | 2016-06-08 10:16:50 +0200 | [diff] [blame] | 183 | dest += n; |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 184 | size -= n; |
| 185 | } |
| 186 | return 1; |
| 187 | } |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 188 | |
| 189 | #elif defined(HAVE_GETENTROPY) |
| 190 | #define PY_GETENTROPY 1 |
| 191 | |
Victor Stinner | de2f1ea | 2017-01-06 11:33:18 +0100 | [diff] [blame] | 192 | /* Fill buffer with size pseudo-random bytes generated by getentropy(): |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 193 | |
Victor Stinner | de2f1ea | 2017-01-06 11:33:18 +0100 | [diff] [blame] | 194 | - Return 1 on success |
| 195 | - Return 0 if getentropy() syscall is not available (failed with ENOSYS or |
| 196 | EPERM). |
| 197 | - Raise an exception (if raise is non-zero) and return -1 on error: |
| 198 | if getentropy() failed with EINTR, raise is non-zero and the Python signal |
| 199 | handler raised an exception, or if getentropy() failed with a different |
| 200 | error. |
| 201 | |
| 202 | getentropy() is retried if it failed with EINTR: interrupted by a signal. */ |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 203 | static int |
| 204 | py_getentropy(char *buffer, Py_ssize_t size, int raise) |
| 205 | { |
Victor Stinner | de2f1ea | 2017-01-06 11:33:18 +0100 | [diff] [blame] | 206 | /* Is getentropy() supported by the running kernel? Set to 0 if |
| 207 | getentropy() failed with ENOSYS or EPERM. */ |
| 208 | static int getentropy_works = 1; |
| 209 | |
| 210 | if (!getentropy_works) { |
| 211 | return 0; |
| 212 | } |
| 213 | |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 214 | while (size > 0) { |
Victor Stinner | de2f1ea | 2017-01-06 11:33:18 +0100 | [diff] [blame] | 215 | /* getentropy() is limited to returning up to 256 bytes. Call it |
| 216 | multiple times if more bytes are requested. */ |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 217 | Py_ssize_t len = Py_MIN(size, 256); |
| 218 | int res; |
| 219 | |
| 220 | if (raise) { |
| 221 | Py_BEGIN_ALLOW_THREADS |
| 222 | res = getentropy(buffer, len); |
| 223 | Py_END_ALLOW_THREADS |
| 224 | } |
| 225 | else { |
| 226 | res = getentropy(buffer, len); |
| 227 | } |
| 228 | |
| 229 | if (res < 0) { |
Victor Stinner | de2f1ea | 2017-01-06 11:33:18 +0100 | [diff] [blame] | 230 | /* ENOSYS: the syscall is not supported by the running kernel. |
| 231 | EPERM: the syscall is blocked by a security policy (ex: SECCOMP) |
| 232 | or something else. */ |
| 233 | if (errno == ENOSYS || errno == EPERM) { |
| 234 | getentropy_works = 0; |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | if (errno == EINTR) { |
| 239 | if (raise) { |
| 240 | if (PyErr_CheckSignals()) { |
| 241 | return -1; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* retry getentropy() if it was interrupted by a signal */ |
| 246 | continue; |
| 247 | } |
| 248 | |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 249 | if (raise) { |
| 250 | PyErr_SetFromErrno(PyExc_OSError); |
| 251 | } |
| 252 | return -1; |
| 253 | } |
| 254 | |
| 255 | buffer += len; |
| 256 | size -= len; |
| 257 | } |
| 258 | return 1; |
| 259 | } |
| 260 | #endif /* defined(HAVE_GETENTROPY) && !defined(sun) */ |
Victor Stinner | dcdb60e | 2017-01-06 11:16:20 +0100 | [diff] [blame] | 261 | |
Victor Stinner | 59f7fb2 | 2015-03-18 14:39:33 +0100 | [diff] [blame] | 262 | |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 263 | static struct { |
| 264 | int fd; |
| 265 | dev_t st_dev; |
| 266 | ino_t st_ino; |
| 267 | } urandom_cache = { -1 }; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 268 | |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 269 | /* Read random bytes from the /dev/urandom device: |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 270 | |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 271 | - Return 0 on success |
| 272 | - Raise an exception (if raise is non-zero) and return -1 on error |
| 273 | |
| 274 | Possible causes of errors: |
| 275 | |
| 276 | - open() failed with ENOENT, ENXIO, ENODEV, EACCES: the /dev/urandom device |
| 277 | was not found. For example, it was removed manually or not exposed in a |
| 278 | chroot or container. |
| 279 | - open() failed with a different error |
| 280 | - fstat() failed |
| 281 | - read() failed or returned 0 |
| 282 | |
| 283 | read() is retried if it failed with EINTR: interrupted by a signal. |
| 284 | |
| 285 | The file descriptor of the device is kept open between calls to avoid using |
| 286 | many file descriptors when run in parallel from multiple threads: |
| 287 | see the issue #18756. |
| 288 | |
| 289 | st_dev and st_ino fields of the file descriptor (from fstat()) are cached to |
| 290 | check if the file descriptor was replaced by a different file (which is |
| 291 | likely a bug in the application): see the issue #21207. |
| 292 | |
| 293 | If the file descriptor was closed or replaced, open a new file descriptor |
| 294 | but don't close the old file descriptor: it probably points to something |
| 295 | important for some third-party code. */ |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 296 | static int |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 297 | dev_urandom(char *buffer, Py_ssize_t size, int raise) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 298 | { |
| 299 | int fd; |
| 300 | Py_ssize_t n; |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 301 | |
| 302 | if (raise) { |
| 303 | struct _Py_stat_struct st; |
Nir Soffer | 4484f9d | 2018-03-12 01:39:22 +0200 | [diff] [blame] | 304 | int fstat_result; |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 305 | |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 306 | if (urandom_cache.fd >= 0) { |
Nir Soffer | 4484f9d | 2018-03-12 01:39:22 +0200 | [diff] [blame] | 307 | Py_BEGIN_ALLOW_THREADS |
| 308 | fstat_result = _Py_fstat_noraise(urandom_cache.fd, &st); |
| 309 | Py_END_ALLOW_THREADS |
| 310 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 311 | /* Does the fd point to the same thing as before? (issue #21207) */ |
Nir Soffer | 4484f9d | 2018-03-12 01:39:22 +0200 | [diff] [blame] | 312 | if (fstat_result |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 313 | || st.st_dev != urandom_cache.st_dev |
| 314 | || st.st_ino != urandom_cache.st_ino) { |
| 315 | /* Something changed: forget the cached fd (but don't close it, |
| 316 | since it probably points to something important for some |
| 317 | third-party code). */ |
| 318 | urandom_cache.fd = -1; |
| 319 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 320 | } |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 321 | if (urandom_cache.fd >= 0) |
| 322 | fd = urandom_cache.fd; |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 323 | else { |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 324 | fd = _Py_open("/dev/urandom", O_RDONLY); |
| 325 | if (fd < 0) { |
| 326 | if (errno == ENOENT || errno == ENXIO || |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 327 | errno == ENODEV || errno == EACCES) { |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 328 | PyErr_SetString(PyExc_NotImplementedError, |
| 329 | "/dev/urandom (or equivalent) not found"); |
Victor Stinner | b27df6f | 2017-01-06 11:39:15 +0100 | [diff] [blame] | 330 | } |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 331 | /* otherwise, keep the OSError exception raised by _Py_open() */ |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 332 | return -1; |
| 333 | } |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 334 | if (urandom_cache.fd >= 0) { |
| 335 | /* urandom_fd was initialized by another thread while we were |
| 336 | not holding the GIL, keep it. */ |
| 337 | close(fd); |
| 338 | fd = urandom_cache.fd; |
| 339 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 340 | else { |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 341 | if (_Py_fstat(fd, &st)) { |
| 342 | close(fd); |
| 343 | return -1; |
| 344 | } |
| 345 | else { |
| 346 | urandom_cache.fd = fd; |
| 347 | urandom_cache.st_dev = st.st_dev; |
| 348 | urandom_cache.st_ino = st.st_ino; |
| 349 | } |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 350 | } |
| 351 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 352 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 353 | do { |
| 354 | n = _Py_read(fd, buffer, (size_t)size); |
| 355 | if (n == -1) |
| 356 | return -1; |
| 357 | if (n == 0) { |
| 358 | PyErr_Format(PyExc_RuntimeError, |
| 359 | "Failed to read %zi bytes from /dev/urandom", |
| 360 | size); |
| 361 | return -1; |
| 362 | } |
| 363 | |
| 364 | buffer += n; |
| 365 | size -= n; |
| 366 | } while (0 < size); |
| 367 | } |
| 368 | else { |
| 369 | fd = _Py_open_noraise("/dev/urandom", O_RDONLY); |
| 370 | if (fd < 0) { |
Victor Stinner | c9382eb | 2015-03-19 23:36:33 +0100 | [diff] [blame] | 371 | return -1; |
| 372 | } |
| 373 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 374 | while (0 < size) |
| 375 | { |
| 376 | do { |
| 377 | n = read(fd, buffer, (size_t)size); |
| 378 | } while (n < 0 && errno == EINTR); |
Victor Stinner | c9382eb | 2015-03-19 23:36:33 +0100 | [diff] [blame] | 379 | |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 380 | if (n <= 0) { |
| 381 | /* stop on error or if read(size) returned 0 */ |
Victor Stinner | 3ee933f | 2016-08-16 18:27:44 +0200 | [diff] [blame] | 382 | close(fd); |
Victor Stinner | 6974cf2 | 2016-08-16 18:46:38 +0200 | [diff] [blame] | 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | buffer += n; |
| 387 | size -= n; |
| 388 | } |
| 389 | close(fd); |
| 390 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 391 | return 0; |
| 392 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 393 | |
| 394 | static void |
| 395 | dev_urandom_close(void) |
| 396 | { |
Antoine Pitrou | e472aea | 2014-04-26 14:33:03 +0200 | [diff] [blame] | 397 | if (urandom_cache.fd >= 0) { |
| 398 | close(urandom_cache.fd); |
| 399 | urandom_cache.fd = -1; |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 400 | } |
| 401 | } |
Victor Stinner | dcdb60e | 2017-01-06 11:16:20 +0100 | [diff] [blame] | 402 | #endif /* !MS_WINDOWS */ |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 403 | |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 404 | |
| 405 | /* Fill buffer with pseudo-random bytes generated by a linear congruent |
| 406 | generator (LCG): |
| 407 | |
| 408 | x(n+1) = (x(n) * 214013 + 2531011) % 2^32 |
| 409 | |
| 410 | Use bits 23..16 of x(n) to generate a byte. */ |
| 411 | static void |
| 412 | lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size) |
| 413 | { |
| 414 | size_t index; |
| 415 | unsigned int x; |
| 416 | |
| 417 | x = x0; |
| 418 | for (index=0; index < size; index++) { |
| 419 | x *= 214013; |
| 420 | x += 2531011; |
| 421 | /* modulo 2 ^ (8 * sizeof(int)) */ |
| 422 | buffer[index] = (x >> 16) & 0xff; |
| 423 | } |
| 424 | } |
| 425 | |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 426 | /* Read random bytes: |
| 427 | |
| 428 | - Return 0 on success |
| 429 | - Raise an exception (if raise is non-zero) and return -1 on error |
| 430 | |
| 431 | Used sources of entropy ordered by preference, preferred source first: |
| 432 | |
| 433 | - CryptGenRandom() on Windows |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 434 | - getrandom() function (ex: Linux and Solaris): call py_getrandom() |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 435 | - getentropy() function (ex: OpenBSD): call py_getentropy() |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 436 | - /dev/urandom device |
| 437 | |
| 438 | Read from the /dev/urandom device if getrandom() or getentropy() function |
| 439 | is not available or does not work. |
| 440 | |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 441 | Prefer getrandom() over getentropy() because getrandom() supports blocking |
| 442 | and non-blocking mode: see the PEP 524. Python requires non-blocking RNG at |
| 443 | startup to initialize its hash secret, but os.urandom() must block until the |
| 444 | system urandom is initialized (at least on Linux 3.17 and newer). |
| 445 | |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 446 | Prefer getrandom() and getentropy() over reading directly /dev/urandom |
| 447 | because these functions don't need file descriptors and so avoid ENFILE or |
| 448 | EMFILE errors (too many open files): see the issue #18756. |
| 449 | |
| 450 | Only the getrandom() function supports non-blocking mode. |
| 451 | |
| 452 | Only use RNG running in the kernel. They are more secure because it is |
| 453 | harder to get the internal state of a RNG running in the kernel land than a |
| 454 | RNG running in the user land. The kernel has a direct access to the hardware |
| 455 | and has access to hardware RNG, they are used as entropy sources. |
| 456 | |
| 457 | Note: the OpenSSL RAND_pseudo_bytes() function does not automatically reseed |
| 458 | its RNG on fork(), two child processes (with the same pid) generate the same |
| 459 | random numbers: see issue #18747. Kernel RNGs don't have this issue, |
| 460 | they have access to good quality entropy sources. |
| 461 | |
| 462 | If raise is zero: |
| 463 | |
| 464 | - Don't raise an exception on error |
| 465 | - Don't call the Python signal handler (don't call PyErr_CheckSignals()) if |
| 466 | a function fails with EINTR: retry directly the interrupted function |
| 467 | - Don't release the GIL to call functions. |
| 468 | */ |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 469 | static int |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 470 | pyurandom(void *buffer, Py_ssize_t size, int blocking, int raise) |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 471 | { |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 472 | #if defined(PY_GETRANDOM) || defined(PY_GETENTROPY) |
| 473 | int res; |
| 474 | #endif |
| 475 | |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 476 | if (size < 0) { |
| 477 | if (raise) { |
| 478 | PyErr_Format(PyExc_ValueError, |
| 479 | "negative argument not allowed"); |
| 480 | } |
| 481 | return -1; |
| 482 | } |
| 483 | |
| 484 | if (size == 0) { |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | #ifdef MS_WINDOWS |
| 489 | return win32_urandom((unsigned char *)buffer, size, raise); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 490 | #else |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 491 | |
| 492 | #if defined(PY_GETRANDOM) || defined(PY_GETENTROPY) |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 493 | #ifdef PY_GETRANDOM |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 494 | res = py_getrandom(buffer, size, blocking, raise); |
Victor Stinner | 2f79643 | 2017-01-06 11:26:01 +0100 | [diff] [blame] | 495 | #else |
| 496 | res = py_getentropy(buffer, size, raise); |
Victor Stinner | a49a207 | 2017-01-06 11:17:52 +0100 | [diff] [blame] | 497 | #endif |
| 498 | if (res < 0) { |
| 499 | return -1; |
| 500 | } |
| 501 | if (res == 1) { |
| 502 | return 0; |
| 503 | } |
| 504 | /* getrandom() or getentropy() function is not available: failed with |
| 505 | ENOSYS or EPERM. Fall back on reading from /dev/urandom. */ |
| 506 | #endif |
| 507 | |
| 508 | return dev_urandom(buffer, size, raise); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 509 | #endif |
| 510 | } |
| 511 | |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 512 | /* Fill buffer with size pseudo-random bytes from the operating system random |
Serhiy Storchaka | 56a6d85 | 2014-12-01 18:28:43 +0200 | [diff] [blame] | 513 | number generator (RNG). It is suitable for most cryptographic purposes |
Georg Brandl | c6a2c9b | 2013-10-06 18:43:19 +0200 | [diff] [blame] | 514 | except long living private keys for asymmetric encryption. |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 515 | |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 516 | On Linux 3.17 and newer, the getrandom() syscall is used in blocking mode: |
| 517 | block until the system urandom entropy pool is initialized (128 bits are |
| 518 | collected by the kernel). |
| 519 | |
| 520 | Return 0 on success. Raise an exception and return -1 on error. */ |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 521 | int |
| 522 | _PyOS_URandom(void *buffer, Py_ssize_t size) |
| 523 | { |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 524 | return pyurandom(buffer, size, 1, 1); |
| 525 | } |
| 526 | |
| 527 | /* Fill buffer with size pseudo-random bytes from the operating system random |
| 528 | number generator (RNG). It is not suitable for cryptographic purpose. |
| 529 | |
| 530 | On Linux 3.17 and newer (when getrandom() syscall is used), if the system |
| 531 | urandom is not initialized yet, the function returns "weak" entropy read |
| 532 | from /dev/urandom. |
| 533 | |
| 534 | Return 0 on success. Raise an exception and return -1 on error. */ |
| 535 | int |
| 536 | _PyOS_URandomNonblock(void *buffer, Py_ssize_t size) |
| 537 | { |
| 538 | return pyurandom(buffer, size, 0, 1); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 539 | } |
| 540 | |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 541 | int |
| 542 | _Py_ReadHashSeed(const char *seed_text, |
| 543 | int *use_hash_seed, |
| 544 | unsigned long *hash_seed) |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 545 | { |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 546 | Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc)); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 547 | /* Convert a text seed to a numeric one */ |
| 548 | if (seed_text && *seed_text != '\0' && strcmp(seed_text, "random") != 0) { |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 549 | const char *endptr = seed_text; |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 550 | unsigned long seed; |
Serhiy Storchaka | 4ae06c5 | 2017-12-12 13:55:04 +0200 | [diff] [blame] | 551 | seed = strtoul(seed_text, (char **)&endptr, 10); |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 552 | if (*endptr != '\0' |
| 553 | || seed > 4294967295UL |
| 554 | || (errno == ERANGE && seed == ULONG_MAX)) |
| 555 | { |
| 556 | return -1; |
| 557 | } |
| 558 | /* Use a specific hash */ |
| 559 | *use_hash_seed = 1; |
| 560 | *hash_seed = seed; |
| 561 | } |
| 562 | else { |
| 563 | /* Use a random hash */ |
| 564 | *use_hash_seed = 0; |
| 565 | *hash_seed = 0; |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 570 | |
| 571 | _PyInitError |
| 572 | _Py_HashRandomization_Init(const _PyCoreConfig *config) |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 573 | { |
| 574 | void *secret = &_Py_HashSecret; |
| 575 | Py_ssize_t secret_size = sizeof(_Py_HashSecret_t); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 576 | |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 577 | if (_Py_HashSecret_Initialized) { |
| 578 | return _Py_INIT_OK(); |
| 579 | } |
Benjamin Peterson | 69e9727 | 2012-02-21 11:08:50 -0500 | [diff] [blame] | 580 | _Py_HashSecret_Initialized = 1; |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 581 | |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 582 | if (config->use_hash_seed) { |
| 583 | if (config->hash_seed == 0) { |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 584 | /* disable the randomized hash */ |
| 585 | memset(secret, 0, secret_size); |
| 586 | } |
| 587 | else { |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 588 | /* use the specified hash seed */ |
Victor Stinner | 358e5e1 | 2017-12-15 00:51:22 +0100 | [diff] [blame] | 589 | lcg_urandom(config->hash_seed, secret, secret_size); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | else { |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 593 | /* use a random hash seed */ |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 594 | int res; |
| 595 | |
| 596 | /* _PyRandom_Init() is called very early in the Python initialization |
Victor Stinner | e66987e | 2016-09-06 16:33:52 -0700 | [diff] [blame] | 597 | and so exceptions cannot be used (use raise=0). |
| 598 | |
| 599 | _PyRandom_Init() must not block Python initialization: call |
| 600 | pyurandom() is non-blocking mode (blocking=0): see the PEP 524. */ |
| 601 | res = pyurandom(secret, secret_size, 0, 0); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 602 | if (res < 0) { |
Victor Stinner | a7368ac | 2017-11-15 18:11:45 -0800 | [diff] [blame] | 603 | return _Py_INIT_USER_ERR("failed to get random numbers " |
| 604 | "to initialize Python"); |
Victor Stinner | 4bad3b6 | 2016-08-16 15:23:58 +0200 | [diff] [blame] | 605 | } |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 606 | } |
Victor Stinner | f7e5b56 | 2017-11-15 15:48:08 -0800 | [diff] [blame] | 607 | return _Py_INIT_OK(); |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 608 | } |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 609 | |
Eric Snow | 6b4be19 | 2017-05-22 21:36:03 -0700 | [diff] [blame] | 610 | |
| 611 | void |
| 612 | _Py_HashRandomization_Fini(void) |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 613 | { |
Victor Stinner | d50c3f3 | 2014-05-02 22:06:44 +0200 | [diff] [blame] | 614 | #ifdef MS_WINDOWS |
| 615 | if (hCryptProv) { |
Tim Golden | b8ac3e1 | 2014-05-06 13:29:45 +0100 | [diff] [blame] | 616 | CryptReleaseContext(hCryptProv, 0); |
Victor Stinner | d50c3f3 | 2014-05-02 22:06:44 +0200 | [diff] [blame] | 617 | hCryptProv = 0; |
| 618 | } |
| 619 | #else |
Antoine Pitrou | 4879a96 | 2013-08-31 00:26:02 +0200 | [diff] [blame] | 620 | dev_urandom_close(); |
| 621 | #endif |
| 622 | } |