8000 ensure no one tries to hash things before the random seed is found · python/cpython@69e9727 · GitHub
[go: up one dir, main page]

Skip to content

Commit 69e9727

Browse files
committed
ensure no one tries to hash things before the random seed is found
1 parent 9571155 commit 69e9727

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

Include/object.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ typedef struct {
479479
} _Py_HashSecret_t;
480480
PyAPI_DATA(_Py_HashSecret_t) _Py_HashSecret;
481481

482+
#ifdef Py_DEBUG
483+
PyAPI_DATA(int) _Py_HashSecret_Initialized;
484+
#endif
485+
482486
/* Helper for passing objects to printf and the like */
483487
#define PyObject_REPR(obj) _PyUnicode_AsString(PyObject_Repr(obj))
484488

Modules/datetimemodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,7 @@ generic_hash(unsigned char *data, int len)
25652565
register unsigned char *p;
25662566
register long x;
25672567

2568+
assert(_Py_HashSecret_Initialized);
25682569
p = (unsigned char *) data;
25692570
x = _Py_HashSecret.prefix;
25702571
x ^= *p << 7;

Objects/bytesobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ bytes_hash(PyBytesObject *a)
896896
register unsigned char *p;
897897
register long x;
898898

899+
assert(_Py_HashSecret_Initialized);
899900
if (a->ob_shash != -1)
900901
return a->ob_shash;
901902
len = Py_SIZE(a);

Objects/unicodeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7341,6 +7341,7 @@ unicode_hash(PyUnicodeObject *self)
73417341
Py_UNICODE *p;
73427342
long x;
73437343

7344+
assert(_Py_HashSecret_Initialized);
73447345
if (self->hash != -1)
73457346
return self->hash;
73467347
len = Py_SIZE(self);

Python/random.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
#include <fcntl.h>
66
#endif
77

8-
static int random_initialized = 0;
8+
#ifdef Py_DEBUG
9+
int _Py_HashSecret_Initialized = 0;
10+
#else
11+
static int _Py_HashSecret_Initialized = 0;
12+
#endif
913

1014
#ifdef MS_WINDOWS
1115
typedef BOOL (WINAPI *CRYPTACQUIRECONTEXTA)(HCRYPTPROV *phProv,\
@@ -246,11 +250,11 @@ _PyRandom_Init(void)
246250
{
247251
char *env;
248252
void *secret = &_Py_HashSecret;
249-
Py_ssize_t secret_size = sizeof(_Py_HashSecret);
253+
Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);
250254

251-
if (random_initialized)
255+
if (_Py_HashSecret_Initialized)
252256
return;
253-
random_initialized = 1;
257+
_Py_HashSecret_Initialized = 1;
254258

255259
/*
256260
By default, hash randomization is disabled, and only

0 commit comments

Comments
 (0)
0