FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-149816: Fix a RC in `_random.Random.__init__` method by sobolevn · Pull Request #149824 · python/cpython · GitHub

Repository navigation

gh-149816: Fix a RC in _random.Random.__init__ method - #149824

Merged
sobolevn merged 2 commits into
python:mainfrom
sobolevn:issue-149816-17
May 18, 2026
Merged

sobolevn merged 2 commits into
python:mainfrom
sobolevn:issue-149816-17

Conversation

sobolevn commented May 14, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

I am not sure that it would be easy to repro / test in a real env, but it can be a potential problem.

Original description from 17.md:


Vulnerability #17

Title: Unlocked init races with PRNG state access

Category: Concurrency and Race Conditions

Tags: race,read,dos

CWEs: CWE-125, CWE-367

CVSS: CVSS:4.0/AV:L/AC:L/AT:P/PR:N/UI:N/VC:L/VI:N/VA:L/SC:N/SI:N/SA:N

Severity: Low (2.1)

Location: cpython/Modules/_randommodule.c:572:572 in function random_init

Description

random_init reseeds internal MT state by calling random_seed without acquiring the object critical section at cpython/Modules/_randommodule.c:572, while normal methods use Py_BEGIN_CRITICAL_SECTION(self) (e.g. cpython/Modules/clinic/_randommodule.c.h:26, cpython/Modules/clinic/_randommodule.c.h:139). During reseed, init_genrand writes self->index = N at cpython/Modules/_randommodule.c:212. A concurrent reader in genrand_uint32 can pass the bounds check self->index >= N at cpython/Modules/_randommodule.c:143 and then use a raced value at cpython/Modules/_randommodule.c:160, causing mt[624] access.

Trigger Conditions

Pre-conditions:

  • CPython is built in free-threaded mode (Py_GIL_DISABLED); with the GIL enabled, the race is serialized.
  • The same _random.Random instance is shared across threads.
  • The attacker can cause concurrent calls to __init__ and random()/getrandbits() on that same object (directly or through an exposed application API).

Data flow:

  1. Create r = _random.Random(0), then advance to a near-boundary index (e.g., call r.getrandbits(32) 623 times so index becomes 623).
  2. Thread A calls r.getrandbits(32) and enters genrand_uint32; it evaluates self->index >= N as false at cpython/Modules/_randommodule.c:143.
  3. Thread B concurrently calls _random.Random.__init__(r, 1) (or r.__init__(1)), reaching unlocked random_init at cpython/Modules/_randommodule.c:572 and then init_genrand setting self->index = 624 at cpython/Modules/_randommodule.c:212.
  4. Thread A resumes and executes y = mt[self->index++] at cpython/Modules/_randommodule.c:160, reading out-of-bounds element state[624].

Impact

In the free-threaded runtime, this race introduces undefined behavior and a concrete out-of-bounds read on PRNG object state. Most severe outcomes are process crash (availability impact) or unintended memory disclosure via corrupted/random output, depending on allocator/layout and timing. Exploitability requires precise concurrency and access to concurrent method invocation on the same object, which lowers reliability but does not remove the bug.

Remediation

  1. In random_init() (cpython/Modules/_randommodule.c), wrap the random_seed(...) call in the same object critical section used by other mutating/accessor methods, so __init__ cannot race with random(), getrandbits(), getstate(), setstate(), or seed() on the same instance.
  2. Ensure the lock/unlock structure in random_init() is exception-safe on all return paths (including failures), matching existing critical-section patterns used in generated wrappers.
  3. Keep locking semantics consistent for inherited tp_init use as well (the fix should apply regardless of whether Random is used directly or via subclasses reusing this tp_init).
  4. Add a free-threaded regression test in Lib/test (random module tests) that repeatedly races r.__init__(...) against r.random() / r.getrandbits(...) on the same object and asserts no crash/UB under stress.

Reproduction

  1. Build a free-threaded CPython (Py_GIL_DISABLED) with a sanitizer-enabled/debug configuration (ASAN or UBSAN strongly recommended) so races and out-of-bounds reads are visible instead of silently ignored.
  2. In one process, create a single shared random.Random instance and drive it close to the MT boundary state (consume enough outputs so index is near 623), then keep using that same object from multiple threads.
  3. Run two concurrent activities against that same object:
    • one thread repeatedly calling random() or getrandbits(32),
    • another thread repeatedly calling obj.__init__(seed_value) (re-initializing the existing object, not creating a new one).
  4. Keep both operations running at high iteration counts; if nothing appears immediately, increase contention (more CPU load, more iterations, pinning threads, repeated retries).
  5. Confirm the issue by observing at least one of:
    • sanitizer report indicating a race or out-of-bounds access in _randommodule.c (notably around genrand_uint32 / seed-init paths),
    • intermittent crash/abort in free-threaded builds during this concurrent __init__ + random/getrandbits workload.

Code Context

    return random_seed(RandomObject_CAST(self), arg);

sobolevn requested a review from colesbury May 14, 2026 12:58
sobolevn requested a review from rhettinger as a code owner May 14, 2026 12:58
bedevere-app Bot mentioned this pull request May 14, 2026
13 of 24 tasks

Copy link
Copy Markdown
Contributor

looks fixed - confirmed 17.py does not reproduce the issue for me on 6632a25 but does on your previous commit (31d1a72)

picnixz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do we test this private constructor actually?

Comment thread Modules/_randommodule.c Outdated
Comment thread Modules/_randommodule.c
Comment thread Modules/_randommodule.c Outdated

Copy link
Copy Markdown
Member Author

Yes, we do test it in TestRawMersenneTwister in Lib/test/test_random.py :)

colesbury added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels May 18, 2026
sobolevn merged commit 14af19e into python:main May 18, 2026
63 checks passed

Copy link
Copy Markdown

Thanks @sobolevn for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

bedevere-app Bot commented May 18, 2026

Copy link
Copy Markdown

GH-149997 is a backport of this pull request to the 3.15 branch.

bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label May 18, 2026

bedevere-app Bot commented May 18, 2026

Copy link
Copy Markdown

GH-149998 is a backport of this pull request to the 3.14 branch.

bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label May 18, 2026
sobolevn added a commit that referenced this pull request May 18, 2026
…9824) (#149997)

gh-149816: Fix a RC in `_random.Random.__init__` method (GH-149824)
(cherry picked from commit 14af19e)

Co-authored-by: sobolevn <mail@sobolevn.me>
sobolevn added a commit that referenced this pull request May 18, 2026
…9824) (#149998)

gh-149816: Fix a RC in `_random.Random.__init__` method (GH-149824)
(cherry picked from commit 14af19e)

Co-authored-by: sobolevn <mail@sobolevn.me>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL