diff --git a/Doc/library/os.rst b/Doc/library/os.rst index dfe5ef0726ff7d..d0c9c053fb0576 100644 --- a/Doc/library/os.rst +++ b/Doc/library/os.rst @@ -5732,8 +5732,8 @@ Random numbers ``/dev/urandom`` devices. The flags argument is a bit mask that can contain zero or more of the - following values ORed together: :py:const:`os.GRND_RANDOM` and - :py:data:`GRND_NONBLOCK`. + following values ORed together: :py:const:`os.GRND_RANDOM`, + :py:data:`GRND_NONBLOCK` and :py:data:`GRND_INSECURE`. See also the `Linux getrandom() manual page `_. @@ -5803,3 +5803,15 @@ Random numbers ``/dev/random`` pool instead of the ``/dev/urandom`` pool. .. versionadded:: 3.6 + +.. data:: GRND_INSECURE + + If this flag is set, then :func:`getrandom` will return pseudo-random data + even if the entropy pool has not yet been initialized. + (It cannot be used with :py:const:`os.GRND_RANDOM`.) + + .. note:: + + It is not suitable for scenarios requiring secure cryptography. + + .. versionadded:: next diff --git a/Misc/NEWS.d/next/Library/2024-12-10-13-09-46.gh-issue-127776.7T-Yxn.rst b/Misc/NEWS.d/next/Library/2024-12-10-13-09-46.gh-issue-127776.7T-Yxn.rst new file mode 100644 index 00000000000000..d4685e01ad9a1c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-12-10-13-09-46.gh-issue-127776.7T-Yxn.rst @@ -0,0 +1 @@ +Add the :data:`~os.GRND_INSECURE` constant to the :mod:`os` module. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 6eb7054b566e3f..bf7459f4a3afc0 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -17613,6 +17613,8 @@ all_ins(PyObject *m) #ifdef HAVE_GETRANDOM_SYSCALL if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1; if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1; + /* Linux 5.6+ */ + if (PyModule_AddIntMacro(m, GRND_INSECURE)) return -1; #endif #ifdef HAVE_MEMFD_CREATE if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;