bpo-32329: Fix -R option for hash randomization (#4873) · python/cpython@358e5e1 (original) (raw)

`@@ -533,9 +533,10 @@ _PyOS_URandomNonblock(void *buffer, Py_ssize_t size)

`

533

533

`return pyurandom(buffer, size, 0, 1);

`

534

534

`}

`

535

535

``

536

``

`-

int Py_ReadHashSeed(const char *seed_text,

`

537

``

`-

int *use_hash_seed,

`

538

``

`-

unsigned long *hash_seed)

`

``

536

`+

int

`

``

537

`+

_Py_ReadHashSeed(const char *seed_text,

`

``

538

`+

int *use_hash_seed,

`

``

539

`+

unsigned long *hash_seed)

`

539

540

`{

`

540

541

`Py_BUILD_ASSERT(sizeof(_Py_HashSecret_t) == sizeof(_Py_HashSecret.uc));

`

541

542

`/* Convert a text seed to a numeric one */

`

`@@ -561,9 +562,9 @@ int Py_ReadHashSeed(const char *seed_text,

`

561

562

`return 0;

`

562

563

`}

`

563

564

``

564

``

`-

static _PyInitError

`

565

``

`-

init_hash_secret(int use_hash_seed,

`

566

``

`-

unsigned long hash_seed)

`

``

565

+

``

566

`+

_PyInitError

`

``

567

`+

_Py_HashRandomization_Init(const _PyCoreConfig *config)

`

567

568

`{

`

568

569

`void *secret = &_Py_HashSecret;

`

569

570

`Py_ssize_t secret_size = sizeof(_Py_HashSecret_t);

`

`@@ -573,14 +574,14 @@ init_hash_secret(int use_hash_seed,

`

573

574

` }

`

574

575

`_Py_HashSecret_Initialized = 1;

`

575

576

``

576

``

`-

if (use_hash_seed) {

`

577

``

`-

if (hash_seed == 0) {

`

``

577

`+

if (config->use_hash_seed) {

`

``

578

`+

if (config->hash_seed == 0) {

`

578

579

`/* disable the randomized hash */

`

579

580

`memset(secret, 0, secret_size);

`

580

581

` }

`

581

582

`else {

`

582

583

`/* use the specified hash seed */

`

583

``

`-

lcg_urandom(hash_seed, secret, secret_size);

`

``

584

`+

lcg_urandom(config->hash_seed, secret, secret_size);

`

584

585

` }

`

585

586

` }

`

586

587

`else {

`

`@@ -601,24 +602,6 @@ init_hash_secret(int use_hash_seed,

`

601

602

`return _Py_INIT_OK();

`

602

603

`}

`

603

604

``

604

``

`-

_PyInitError

`

605

``

`-

_Py_HashRandomization_Init(_PyCoreConfig *core_config)

`

606

``

`-

{

`

607

``

`-

const char *seed_text;

`

608

``

`-

int use_hash_seed = core_config->use_hash_seed;

`

609

``

`-

unsigned long hash_seed = core_config->hash_seed;

`

610

``

-

611

``

`-

if (use_hash_seed < 0) {

`

612

``

`-

seed_text = Py_GETENV("PYTHONHASHSEED");

`

613

``

`-

if (Py_ReadHashSeed(seed_text, &use_hash_seed, &hash_seed) < 0) {

`

614

``

`-

return _Py_INIT_USER_ERR("PYTHONHASHSEED must be "random" "

`

615

``

`-

"or an integer in range [0; 4294967295]");

`

616

``

`-

}

`

617

``

`-

core_config->use_hash_seed = use_hash_seed;

`

618

``

`-

core_config->hash_seed = hash_seed;

`

619

``

`-

}

`

620

``

`-

return init_hash_secret(use_hash_seed, hash_seed);

`

621

``

`-

}

`

622

605

``

623

606

`void

`

624

607

`_Py_HashRandomization_Fini(void)

`