René's URL Explorer Experiment


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

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

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

Description: 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: Create r = _random.Random(0), then advance to a near-boundary index (e.g., call r.getrandbits(32) 623 times so index becomes 623). Thread A calls r.getrandbits(32) and enters genrand_uint32; it evaluates self->index >= N as false at cpython/Modules/_randommodule.c:143. 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. 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 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. 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. 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). 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 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. 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. 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). Keep both operations running at high iteration counts; if nothing appears immediately, increase contention (more CPU load, more iterations, pinning threads, repeated retries). 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: gh-149816

Open Graph Description: 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 ...

X Description: 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 ...

Opengraph URL: https://github.com/python/cpython/pull/149824

X: @github

direct link

Domain: github.com

route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:0ff00bb0-1be8-0851-ae07-65725b7e3738
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idC7A0:2D56E3:E187CC:12D4E62:6A533079
html-safe-nonce4aa42d0ccd2b5145bf5d88f31311ec2f69668365c076a5b84fd8e484162d44b4
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0EwOjJENTZFMzpFMTg3Q0M6MTJENEU2Mjo2QTUzMzA3OSIsInZpc2l0b3JfaWQiOiI0NTA4NzM0MTU1MTgyNTE4Mzk0IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmaca15baae5f26765f99b377e878ca77cb091c94e22b1c4397224502c157cfd6b92
hovercard-subject-tagpull_request:3683006416
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/python/cpython/pull/149824/files
twitter:imagehttps://avatars.githubusercontent.com/u/4660275?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/4660275?s=400&v=4
og:image:altI 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 ...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
Noneb9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release07a982c1d40157c619b364352b704c3ce66bb332
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/cpython/pull/149824/files#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F149824%2Ffiles
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fpull%2F149824%2Ffiles
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/pull/149824/files
Reloadhttps://github.com/python/cpython/pull/149824/files
Reloadhttps://github.com/python/cpython/pull/149824/files
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
python https://github.com/python
cpythonhttps://github.com/python/cpython
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
Notifications https://github.com/login?return_to=%2Fpython%2Fcpython
Fork 35k https://github.com/login?return_to=%2Fpython%2Fcpython
Star 73.8k https://github.com/login?return_to=%2Fpython%2Fcpython
Code https://github.com/python/cpython
Issues 5k+ https://github.com/python/cpython/issues
Pull requests 2.3k https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality 0 https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Code https://github.com/python/cpython
Issues https://github.com/python/cpython/issues
Pull requests https://github.com/python/cpython/pulls
Actions https://github.com/python/cpython/actions
Projects https://github.com/python/cpython/projects
Security and quality https://github.com/python/cpython/security
Insights https://github.com/python/cpython/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fpython%2Fcpython%2Fissues%2Fnew%2Fchoose
sobolevnhttps://github.com/sobolevn
python:mainhttps://github.com/python/cpython/tree/main
sobolevn:issue-149816-17https://github.com/sobolevn/cpython/tree/issue-149816-17
Conversation 9 https://github.com/python/cpython/pull/149824
Commits 2 https://github.com/python/cpython/pull/149824/commits
Checks 65 https://github.com/python/cpython/pull/149824/checks
Files changed https://github.com/python/cpython/pull/149824/files
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
gh-149816: Fix a RC in _random.Random.__init__ method https://github.com/python/cpython/pull/149824/files#top
Show all changes 2 commits https://github.com/python/cpython/pull/149824/files
6632a25 gh-149816: Fix a RC in `_random.Random.__init__` method sobolevn May 14, 2026 https://github.com/python/cpython/pull/149824/commits/6632a253d4dc9793729df55b3ae7a901d2d74c8a
de0b35d Address review sobolevn May 15, 2026 https://github.com/python/cpython/pull/149824/commits/de0b35d830b0c9cfc99a330ee9964ff5a1b85384
Clear filters https://github.com/python/cpython/pull/149824/files
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst https://github.com/python/cpython/pull/149824/files#diff-eba476464e09157d308f1c1ad05e96b20ebd9f30be2ee06342d25de6f965818a
_randommodule.c https://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
_randommodule.c.h https://github.com/python/cpython/pull/149824/files#diff-f2bc9bc8f92c7ecb7ed10916d2c5ff9de9748e53ecd76ce027de797ca281bba5
Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rsthttps://github.com/python/cpython/pull/149824/files#diff-eba476464e09157d308f1c1ad05e96b20ebd9f30be2ee06342d25de6f965818a
View file https://github.com/sobolevn/cpython/blob/de0b35d830b0c9cfc99a330ee9964ff5a1b85384/Misc/NEWS.d/next/Library/2026-05-14-15-55-28.gh-issue-149816.ZaXQ0q.rst
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/149824/{{ revealButtonHref }}
https://github.com/python/cpython/blob/main/.github/CODEOWNERS#L568
Modules/_randommodule.chttps://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
View file https://github.com/sobolevn/cpython/blob/de0b35d830b0c9cfc99a330ee9964ff5a1b85384/Modules/_randommodule.c
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/python/cpython/pull/149824/{{ revealButtonHref }}
https://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
https://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
https://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
https://github.com/python/cpython/pull/149824/files#diff-c2608f08e5410416046fb2fc953730740d0b29b26c148c2cb4a40e87212012fa
Modules/clinic/_randommodule.c.hhttps://github.com/python/cpython/pull/149824/files#diff-f2bc9bc8f92c7ecb7ed10916d2c5ff9de9748e53ecd76ce027de797ca281bba5
View file https://github.com/sobolevn/cpython/blob/de0b35d830b0c9cfc99a330ee9964ff5a1b85384/Modules/clinic/_randommodule.c.h
Open in desktop https://desktop.github.com
how customized files appear on GitHubhttps://docs.github.com/github/administering-a-repository/customizing-how-changed-files-appear-on-github
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
Please reload this pagehttps://github.com/python/cpython/pull/149824/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.