random.c 13.3 KB
Newer Older
1 2
#include "Python.h"
#ifdef MS_WINDOWS
3
#  include <windows.h>
4 5 6
/* All sample MSDN wincrypt programs include the header below. It is at least
 * required with Min GW. */
#  include <wincrypt.h>
7
#else
8 9 10 11
#  include <fcntl.h>
#  ifdef HAVE_SYS_STAT_H
#    include <sys/stat.h>
#  endif
12 13 14
#  ifdef HAVE_LINUX_RANDOM_H
#    include <linux/random.h>
#  endif
15 16 17
#  ifdef HAVE_GETRANDOM
#    include <sys/random.h>
#  elif defined(HAVE_GETRANDOM_SYSCALL)
18 19
#    include <sys/syscall.h>
#  endif
20 21
#endif

22 23 24 25 26
#ifdef Py_DEBUG
int _Py_HashSecret_Initialized = 0;
#else
static int _Py_HashSecret_Initialized = 0;
#endif
27 28 29 30 31 32 33 34

#ifdef MS_WINDOWS
static HCRYPTPROV hCryptProv = 0;

static int
win32_urandom_init(int raise)
{
    /* Acquire context */
35 36
    if (!CryptAcquireContext(&hCryptProv, NULL, NULL,
                             PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
37 38 39 40 41
        goto error;

    return 0;

error:
42
    if (raise) {
43
        PyErr_SetFromWindowsErr(0);
44
    }
45 46 47 48
    return -1;
}

/* Fill buffer with size pseudo-random bytes generated by the Windows CryptoGen
49
   API. Return 0 on success, or raise an exception and return -1 on error. */
50 51 52 53 54 55 56
static int
win32_urandom(unsigned char *buffer, Py_ssize_t size, int raise)
{
    Py_ssize_t chunk;

    if (hCryptProv == 0)
    {
57
        if (win32_urandom_init(raise) == -1) {
58
            return -1;
59
        }
60 61 62 63 64
    }

    while (size > 0)
    {
        chunk = size > INT_MAX ? INT_MAX : size;
65
        if (!CryptGenRandom(hCryptProv, (DWORD)chunk, buffer))
66 67
        {
            /* CryptGenRandom() failed */
68
            if (raise) {
69
                PyErr_SetFromWindowsErr(0);
70
            }
71 72 73 74 75 76 77 78
            return -1;
        }
        buffer += chunk;
        size -= chunk;
    }
    return 0;
}

79
/* Issue #25003: Don't use getentropy() on Solaris (available since
80
 * Solaris 11.3), it is blocking whereas os.urandom() should not block. */
81 82 83
#elif defined(HAVE_GETENTROPY) && !defined(sun)
#define PY_GETENTROPY 1

84 85
/* Fill buffer with size pseudo-random bytes generated by getentropy().
   Return 0 on success, or raise an exception and return -1 on error.
86

87
   If raise is zero, don't raise an exception on error. */
88
static int
89
py_getentropy(char *buffer, Py_ssize_t size, int raise)
90 91 92
{
    while (size > 0) {
        Py_ssize_t len = Py_MIN(size, 256);
93 94
        int res;

95
        if (raise) {
96 97 98
            Py_BEGIN_ALLOW_THREADS
            res = getentropy(buffer, len);
            Py_END_ALLOW_THREADS
99
        }
100 101
        else {
            res = getentropy(buffer, len);
102 103 104 105 106 107 108
        }

        if (res < 0) {
            if (raise) {
                PyErr_SetFromErrno(PyExc_OSError);
            }
            return -1;
109 110
        }

111 112 113 114 115 116
        buffer += len;
        size -= len;
    }
    return 0;
}

117 118 119 120
#else

#if defined(HAVE_GETRANDOM) || defined(HAVE_GETRANDOM_SYSCALL)
#define PY_GETRANDOM 1
121

122 123
/* Call getrandom()
   - Return 1 on success
124 125
   - Return 0 if getrandom() syscall is not available (failed with ENOSYS or
     EPERM) or if getrandom(GRND_NONBLOCK) failed with EAGAIN (system urandom
Victor Stinner's avatar
Victor Stinner committed
126
     not initialized yet) and raise=0.
127 128 129
   - Raise an exception (if raise is non-zero) and return -1 on error:
     getrandom() failed with EINTR and the Python signal handler raised an
     exception, or getrandom() failed with a different error. */
130
static int
131
py_getrandom(void *buffer, Py_ssize_t size, int blocking, int raise)
132
{
133
    /* Is getrandom() supported by the running kernel? Set to 0 if getrandom()
134
       failed with ENOSYS or EPERM. Need Linux kernel 3.17 or newer, or Solaris
Victor Stinner's avatar
Victor Stinner committed
135
       11.3 or newer */
136
    static int getrandom_works = 1;
137
    int flags;
138
    char *dest;
139
    long n;
140

141
    if (!getrandom_works) {
142
        return 0;
143
    }
144

145
    flags = blocking ? 0 : GRND_NONBLOCK;
146
    dest = buffer;
147
    while (0 < size) {
148 149 150 151 152
#ifdef sun
        /* Issue #26735: On Solaris, getrandom() is limited to returning up
           to 1024 bytes */
        n = Py_MIN(size, 1024);
#else
153
        n = Py_MIN(size, LONG_MAX);
154
#endif
155

156
        errno = 0;
157 158 159
#ifdef HAVE_GETRANDOM
        if (raise) {
            Py_BEGIN_ALLOW_THREADS
160
            n = getrandom(dest, n, flags);
161 162 163
            Py_END_ALLOW_THREADS
        }
        else {
164
            n = getrandom(dest, n, flags);
165 166 167
        }
#else
        /* On Linux, use the syscall() function because the GNU libc doesn't
168 169
           expose the Linux getrandom() syscall yet. See:
           https://sourceware.org/bugzilla/show_bug.cgi?id=17252 */
170 171
        if (raise) {
            Py_BEGIN_ALLOW_THREADS
172
            n = syscall(SYS_getrandom, dest, n, flags);
173 174 175
            Py_END_ALLOW_THREADS
        }
        else {
176
            n = syscall(SYS_getrandom, dest, n, flags);
177
        }
178
#endif
179

180
        if (n < 0) {
Victor Stinner's avatar
Victor Stinner committed
181
            /* ENOSYS: getrandom() syscall not supported by the kernel (but
182 183 184
             * maybe supported by the host which built Python). EPERM:
             * getrandom() syscall blocked by SECCOMP or something else. */
            if (errno == ENOSYS || errno == EPERM) {
185 186 187
                getrandom_works = 0;
                return 0;
            }
188 189 190 191 192 193

            /* getrandom(GRND_NONBLOCK) fails with EAGAIN if the system urandom
               is not initialiazed yet. For _PyRandom_Init(), we ignore their
               error and fall back on reading /dev/urandom which never blocks,
               even if the system urandom is not initialized yet. */
            if (errno == EAGAIN && !raise && !blocking) {
194 195
                return 0;
            }
196 197

            if (errno == EINTR) {
198 199 200 201
                if (raise) {
                    if (PyErr_CheckSignals()) {
                        return -1;
                    }
202
                }
203 204

                /* retry getrandom() if it was interrupted by a signal */
205 206 207
                continue;
            }

208
            if (raise) {
209
                PyErr_SetFromErrno(PyExc_OSError);
210
            }
211 212 213
            return -1;
        }

214
        dest += n;
215 216 217 218 219 220
        size -= n;
    }
    return 1;
}
#endif

221 222 223 224 225
static struct {
    int fd;
    dev_t st_dev;
    ino_t st_ino;
} urandom_cache = { -1 };
226

227

228
/* Read 'size' random bytes from py_getrandom(). Fall back on reading from
229
   /dev/urandom if getrandom() is not available.
230

231 232
   Return 0 on success. Raise an exception (if raise is non-zero) and return -1
   on error. */
233
static int
234
dev_urandom(char *buffer, Py_ssize_t size, int blocking, int raise)
235 236 237
{
    int fd;
    Py_ssize_t n;
238
#ifdef PY_GETRANDOM
239 240
    int res;
#endif
241

242
    assert(size > 0);
243

244
#ifdef PY_GETRANDOM
245
    res = py_getrandom(buffer, size, blocking, raise);
246
    if (res < 0) {
247
        return -1;
248 249
    }
    if (res == 1) {
250
        return 0;
251
    }
252
    /* getrandom() failed with ENOSYS or EPERM,
Victor Stinner's avatar
Victor Stinner committed
253
       fall back on reading /dev/urandom */
254 255
#endif

256 257 258 259

    if (raise) {
        struct _Py_stat_struct st;

260
        if (urandom_cache.fd >= 0) {
261 262 263 264 265 266 267 268 269
            /* Does the fd point to the same thing as before? (issue #21207) */
            if (_Py_fstat_noraise(urandom_cache.fd, &st)
                || st.st_dev != urandom_cache.st_dev
                || st.st_ino != urandom_cache.st_ino) {
                /* Something changed: forget the cached fd (but don't close it,
                   since it probably points to something important for some
                   third-party code). */
                urandom_cache.fd = -1;
            }
270
        }
271 272
        if (urandom_cache.fd >= 0)
            fd = urandom_cache.fd;
273
        else {
274 275 276 277 278 279 280
            fd = _Py_open("/dev/urandom", O_RDONLY);
            if (fd < 0) {
                if (errno == ENOENT || errno == ENXIO ||
                    errno == ENODEV || errno == EACCES)
                    PyErr_SetString(PyExc_NotImplementedError,
                                    "/dev/urandom (or equivalent) not found");
                /* otherwise, keep the OSError exception raised by _Py_open() */
281 282
                return -1;
            }
283 284 285 286 287 288
            if (urandom_cache.fd >= 0) {
                /* urandom_fd was initialized by another thread while we were
                   not holding the GIL, keep it. */
                close(fd);
                fd = urandom_cache.fd;
            }
289
            else {
290 291 292 293 294 295 296 297 298
                if (_Py_fstat(fd, &st)) {
                    close(fd);
                    return -1;
                }
                else {
                    urandom_cache.fd = fd;
                    urandom_cache.st_dev = st.st_dev;
                    urandom_cache.st_ino = st.st_ino;
                }
299
            }
300
        }
301

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
        do {
            n = _Py_read(fd, buffer, (size_t)size);
            if (n == -1)
                return -1;
            if (n == 0) {
                PyErr_Format(PyExc_RuntimeError,
                        "Failed to read %zi bytes from /dev/urandom",
                        size);
                return -1;
            }

            buffer += n;
            size -= n;
        } while (0 < size);
    }
    else {
        fd = _Py_open_noraise("/dev/urandom", O_RDONLY);
        if (fd < 0) {
320 321 322
            return -1;
        }

323 324 325 326 327
        while (0 < size)
        {
            do {
                n = read(fd, buffer, (size_t)size);
            } while (n < 0 && errno == EINTR);
328

329 330
            if (n <= 0) {
                /* stop on error or if read(size) returned 0 */
331
                close(fd);
332 333 334 335 336 337 338 339
                return -1;
            }

            buffer += n;
            size -= n;
        }
        close(fd);
    }
340 341
    return 0;
}
342 343 344 345

static void
dev_urandom_close(void)
{
346 347 348
    if (urandom_cache.fd >= 0) {
        close(urandom_cache.fd);
        urandom_cache.fd = -1;
349 350 351
    }
}

352
#endif
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374

/* Fill buffer with pseudo-random bytes generated by a linear congruent
   generator (LCG):

       x(n+1) = (x(n) * 214013 + 2531011) % 2^32

   Use bits 23..16 of x(n) to generate a byte. */
static void
lcg_urandom(unsigned int x0, unsigned char *buffer, size_t size)
{
    size_t index;
    unsigned int x;

    x = x0;
    for (index=0; index < size; index++) {
        x *= 214013;
        x += 2531011;
        /* modulo 2 ^ (8 * sizeof(int)) */
        buffer[index] = (x >> 16) & 0xff;
    }
}

375
/* If raise is zero:
376 377 378 379
   - Don't raise exceptions on error
   - Don't call PyErr_CheckSignals() on EINTR (retry directly the interrupted
     syscall)
   - Don't release the GIL to call syscalls. */
380
static int
381
pyurandom(void *buffer, Py_ssize_t size, int blocking, int raise)
382 383
{
    if (size < 0) {
384 385 386 387
        if (raise) {
            PyErr_Format(PyExc_ValueError,
                         "negative argument not allowed");
        }
388 389
        return -1;
    }
390 391

    if (size == 0) {
392
        return 0;
393
    }
394 395

#ifdef MS_WINDOWS
396
    return win32_urandom((unsigned char *)buffer, size, raise);
397
#elif defined(PY_GETENTROPY)
398
    return py_getentropy(buffer, size, raise);
399
#else
400
    return dev_urandom(buffer, size, blocking, raise);
401 402 403
#endif
}

404 405 406 407
/* Fill buffer with size pseudo-random bytes from the operating system random
   number generator (RNG). It is suitable for most cryptographic purposes
   except long living private keys for asymmetric encryption.

408 409 410 411 412
   On Linux 3.17 and newer, the getrandom() syscall is used in blocking mode:
   block until the system urandom entropy pool is initialized (128 bits are
   collected by the kernel).

   Return 0 on success. Raise an exception and return -1 on error. */
413 414 415
int
_PyOS_URandom(void *buffer, Py_ssize_t size)
{
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
    return pyurandom(buffer, size, 1, 1);
}

/* Fill buffer with size pseudo-random bytes from the operating system random
   number generator (RNG). It is not suitable for cryptographic purpose.

   On Linux 3.17 and newer (when getrandom() syscall is used), if the system
   urandom is not initialized yet, the function returns "weak" entropy read
   from /dev/urandom.

   Return 0 on success. Raise an exception and return -1 on error. */
int
_PyOS_URandomNonblock(void *buffer, Py_ssize_t size)
{
    return pyurandom(buffer, size, 0, 1);
431 432
}

433 434 435 436
void
_PyRandom_Init(void)
{
    char *env;
437
    unsigned char *secret = (unsigned char *)&_Py_HashSecret.uc;
438
    Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
439
    Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));
440

441
    if (_Py_HashSecret_Initialized)
442
        return;
443
    _Py_HashSecret_Initialized = 1;
444 445 446 447 448 449 450

    /*
      Hash randomization is enabled.  Generate a per-process secret,
      using PYTHONHASHSEED if provided.
    */

    env = Py_GETENV("PYTHONHASHSEED");
Georg Brandl's avatar
Georg Brandl committed
451
    if (env && *env != '\0' && strcmp(env, "random") != 0) {
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
        char *endptr = env;
        unsigned long seed;
        seed = strtoul(env, &endptr, 10);
        if (*endptr != '\0'
            || seed > 4294967295UL
            || (errno == ERANGE && seed == ULONG_MAX))
        {
            Py_FatalError("PYTHONHASHSEED must be \"random\" or an integer "
                          "in range [0; 4294967295]");
        }
        if (seed == 0) {
            /* disable the randomized hash */
            memset(secret, 0, secret_size);
        }
        else {
467
            lcg_urandom(seed, secret, secret_size);
468 469 470
        }
    }
    else {
471 472 473
        int res;

        /* _PyRandom_Init() is called very early in the Python initialization
474 475 476 477 478
           and so exceptions cannot be used (use raise=0).

           _PyRandom_Init() must not block Python initialization: call
           pyurandom() is non-blocking mode (blocking=0): see the PEP 524. */
        res = pyurandom(secret, secret_size, 0, 0);
479 480 481
        if (res < 0) {
            Py_FatalError("failed to get random numbers to initialize Python");
        }
482 483
    }
}
484 485 486 487

void
_PyRandom_Fini(void)
{
488 489
#ifdef MS_WINDOWS
    if (hCryptProv) {
490
        CryptReleaseContext(hCryptProv, 0);
491 492
        hCryptProv = 0;
    }
493
#elif defined(PY_GETENTROPY)
494
    /* nothing to clean */
495
#else
496 497 498
    dev_urandom_close();
#endif
}