GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-149816: Fix a RC in `_random.Random.__init__` method by sobolevn · Pull Request #149824 · python/cpython

Conversation

@sobolevn

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

Uh oh!

There was an error while loading. Please reload this page.

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);

  • Issue: 22 free-threading race conditions #149816

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sobolevn
sobolevn requested a review from colesbury May 14, 2026 12:58
@sobolevn
sobolevn requested a review from rhettinger as a code owner May 14, 2026 12:58
@bedevere-app bedevere-app Bot mentioned this pull request May 14, 2026
22 free-threading race conditions #149816
Open
13 of 24 tasks
@bedevere-app bedevere-app Bot added the awaiting core review label May 14, 2026
@sobolevn
pythongh-149816: Fix a RC in _random.Random.__init__ method
6632a25
@sobolevn
sobolevn force-pushed the issue-149816-17 branch from ff2d5ad to 6632a25 Compare May 14, 2026 13:28
@lunixbochs

lunixbochs commented May 14, 2026

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)

sobolevn reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

picnixz
picnixz reviewed May 15, 2026
View reviewed changes

@picnixz picnixz left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Do we test this private constructor actually?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Modules/_randommodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_randommodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Modules/_randommodule.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sobolevn
Address review
de0b35d
@sobolevn

sobolevn commented May 15, 2026

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@colesbury colesbury added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels May 18, 2026
colesbury
colesbury approved these changes May 18, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 18, 2026
@sobolevn
sobolevn merged commit 14af19e into python:main May 18, 2026
63 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@miss-islington-app

miss-islington-app Bot commented May 18, 2026

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot removed the awaiting merge label May 18, 2026
@bedevere-app

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app 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
@miss-islington @sobolevn
[3.15] gh-149816: Fix a RC in _random.Random.__init__ method (GH-14…
01b07df
…9824) (#149997)

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

Co-authored-by: sobolevn <[email protected]>
sobolevn added a commit that referenced this pull request May 18, 2026
@miss-islington @sobolevn
[3.14] gh-149816: Fix a RC in _random.Random.__init__ method (GH-14…
21a7236
…9824) (#149998)

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

Co-authored-by: sobolevn <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@picnixz picnixz picnixz left review comments
@colesbury colesbury colesbury approved these changes
@rhettinger rhettinger Awaiting requested review from rhettinger rhettinger is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@sobolevn @lunixbochs @colesbury @picnixz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.