This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Move hash randomisation initialisation out of Python/random.c
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: brett.cannon, christian.heimes, eric.snow, ncoghlan, rhettinger, serhiy.storchaka, steve.dower, vstinner
Priority: normal Keywords:

Created on 2016-12-31 11:36 by ncoghlan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (10)
msg284383 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-12-31 11:36
This is the last major structural refactoring proposal to come out of the prep work on the PEP 432 modular bootstrapping feature development branch: 

* rename Python/random.c -> Python/hash_randomization.c
* rename _PyRandom_Init -> __Py_HashRandomization_Init
* rename _PyRandom_Fini -> __Py_HashRandomization_Fini

The current naming is confusing when working on the interpreter startup sequence as the file and API naming looks like it relates to initialising the random module, when it has nothing to do with that.
msg284388 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-31 12:11
This is not just about the hash randomization. The file also contains implementations of _PyOS_URandom() and _PyOS_URandomNonblock().
msg284390 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-12-31 12:32
You're right, I forgot about that part (I was mainly looking at the diffs today).

Spelling out the *problem*, rather than just jumping to a specific proposed solution, the confusing code layout we currently have is:

* Modules/_randommodule.c provides _random.Random
* Include/pylifecycle.c declares (amongst other things):
  * _PyRandom_Init
  * _PyRandom_Fini
  * _PyOS_URandom
  * _PyOS_URandomNonBlock
* Python/random.c implements all four of those APIs

I don't believe the latter two APIs existed back when I started the PEP 432 feature branch, which is why I had forgotten about them in this context.

Having the OS independent _PyOS_URandom APIs in a file called "Python/random.c" is reasonable, and there's even a legitimate connection to the random module there by way of random.SystemRandom.

So the only confusing aspect is also having the hash randomisation initialisation functions in there, rather than in their own file, with more appropriate API names.
msg284391 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-31 13:09
Actually _PyRandom_Fini() has a relation to _PyOS_URandom APIs. It releases resources acquired by _PyOS_URandom APIs. It shouldn't be renamed to _Py_HashRandomization_Fini.

_PyRandom_Init() just initializes _Py_HashSecret. But it can't be implemented via _PyOS_URandom APIs. It uses a static function from this file. If move it to other place, we should add new _PyOS_URandom API function. I don't think this would make the code cleaner.
msg284394 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-12-31 14:52
Nick: As Serhiy explained, IMHO you misunderstood _PyRandom_Init() and _PyRandom_Fini(), I suggest to close the issue.
msg284409 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-12-31 19:00
FWIW, I concur with Nick's initial observation that the current "API naming looks like it relates to initialising the random module, when it has nothing to do with that."

It would be nice if Python/random.c were renamed to something else that indicated its relation to urandom and to hash secrets.
msg284417 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-01-01 00:12
On 1 Jan 2017 05:00, "Raymond Hettinger" <report@bugs.python.org> wrote:

It would be nice if Python/random.c were renamed to something else that
indicated its relation to urandom and to hash secrets.

Right, based on Victor & Serhiy's feedback, the split I would now propose
 is:

- Include/pyurandom.h and Python/pyurandom.c for the OS urandom APIs (with
related init/fini functions)
- Python/hash_randomization.c for the hash seeding functions  (while this
process *uses* pyurandom, it isn't part of *providing* that interface)
msg310909 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-01-28 03:53
As part of the startup sequence refactoring, this file has been renamed to Python/bootstrap_hash.c, and _PyRandom_Init/Fini have been renamed to _Py_HashRandomization_Init/Fini.

Relevant commit: https://github.com/python/cpython/commit/6b4be195cd8868b76eb6fbe166acc39beee8ce36#diff-5c57849694577deb1e10475ae796f7dc
msg310927 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-01-28 07:57
I prefered random.c name since hash initialization is only a small part of
the file, most of file is the implementation of os.urandom() and
os.getrandom() which is used a runtime.
msg310935 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2018-01-28 11:01
Aye, but the reason they're here rather than in the os module where you might otherwise expect to find them is because the hash randomization bootstrapping needs them.
History
Date User Action Args
2022-04-11 14:58:41adminsetgithub: 73306
2018-01-28 11:01:57ncoghlansetmessages: + msg310935
2018-01-28 07:57:12vstinnersetmessages: + msg310927
2018-01-28 03:53:46ncoghlansetstatus: open -> closed
resolution: fixed
messages: + msg310909

stage: needs patch -> resolved
2017-01-01 00:12:32ncoghlansetmessages: + msg284417
2016-12-31 19:00:46rhettingersetnosy: + rhettinger
messages: + msg284409
2016-12-31 14:52:10vstinnersetmessages: + msg284394
2016-12-31 14:51:04vstinnersetnosy: + vstinner
2016-12-31 13:09:25serhiy.storchakasetmessages: + msg284391
2016-12-31 12:32:45ncoghlansetmessages: + msg284390
title: Rename Python/random.c to Python/bootstrap_hash.c -> Move hash randomisation initialisation out of Python/random.c
2016-12-31 12:11:21serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg284388
2016-12-31 11:36:34ncoghlancreate