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-134144: Fix use-after-free in zapthreads() by b-pass · Pull Request #134145 · python/cpython

Conversation

@b-pass

@b-pass b-pass commented May 17, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

Fixes #134144. This problem was introduced by #127077 which added _Py_FOR_EACH_TSTATE_UNLOCKED.

_Py_FOR_EACH_TSTATE_UNLOCKED reads the tstate at the end of each loop iteration to get the next value. When the loop body frees the tstate, the result is always a use-after-free. It doesn't always crash (seems rare, actually), I guess it just depends what the allocator does with those pages during the rest of the tstate deletion.

  • Issue: Cannot safely Py_EndInterpreter in 3.14b1 #134144

Sorry, something went wrong.

Uh oh!

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

All reactions
@b-pass
Fix use-after-free in zapthreads
16628e8
Can't use the _Py_FOR_EACH_TSTATE_UNLOCKED macro because it will reference the just-deleted tstate in order to get the next value.
@b-pass
b-pass requested a review from ericsnowcurrently as a code owner May 17, 2025 15:02
@python-cla-bot

python-cla-bot Bot commented May 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app

bedevere-app Bot commented May 17, 2025

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot mentioned this pull request May 17, 2025
Cannot safely Py_EndInterpreter in 3.14b1 #134144
Closed
@bedevere-app bedevere-app Bot added the awaiting review label May 17, 2025
@b-pass
Fix trailing whitespace
1c85317
@bedevere-app

bedevere-app Bot commented May 17, 2025

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ericsnowcurrently ericsnowcurrently added the needs backport to 3.14 bugs and security fixes label May 17, 2025
ZeroIntensity
ZeroIntensity requested changes May 17, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity 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.

I'm pretty sure we can drop zapthreads entirely, because right now the assumption is that the interpreter doesn't have any threads left. Let's hold off on this until we come to a decision on what the actual behavior should be for dangling threads.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels May 17, 2025
ericsnowcurrently
ericsnowcurrently approved these changes May 17, 2025
View reviewed changes

@ericsnowcurrently ericsnowcurrently 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.

LGTM

We just need a NEWS entry.

@ZeroIntensity, regarding gh-128640, I expect we'll do that too since it solves an additional problem. However, this PR is a good idea regardless.

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels May 17, 2025
@ZeroIntensity

ZeroIntensity commented May 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

We need a test as well, but I'm more worried about the use-case as given in the issue. zapthreads will arbitrarily destroy a thread state (one created by PyGILState_Ensure in the repro), which is a recipe for crashes if the thread is still running.

All reactions

Sorry, something went wrong.

Uh oh!

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

@b-pass
Add NEWS blurb
89082fa
@b-pass

b-pass commented May 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor Author

We need a test as well, but I'm more worried about the use-case as given in the issue. zapthreads will arbitrarily destroy a thread state (one created by PyGILState_Ensure in the repro), which is a recipe for crashes if the thread is still running.

Sorry, the issue description isn't great. The thread wasn't alive, but Py_EndInterpreter takes a PyThreadState* as the argument (not a PyInterpreterState*), so there must always been one on the interpreter. But the fact is this code ALWAYS does a use-after-free (even if there was only ever one thread state). Here is some pseudo code of what is going on:

for (PyThreadState *tstate = interp->threads.head; 
       tstate != NULL; 
       tstate = tstate->next)
{
   free(tstate);
}
All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented May 17, 2025

Copy link
Copy Markdown
Member

Right, I've noticed this before. In most cases, we get lucky because the initial thread is statically allocated on the interpreter state, so it doesn't actually cause UAF. It becomes an issue with something like PyGILState_Ensure because we have more than one thread state, but the problem is that's unsupported anyway.

All reactions

Sorry, something went wrong.

Uh oh!

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

@b-pass

b-pass commented May 17, 2025

Copy link
Copy Markdown
Contributor Author

It becomes an issue with something like PyGILState_Ensure because we have more than one thread state, but the problem is that's unsupported anyway.

It happens without the Ensure. I've updated the linked issue with a complete program (without an Ensure). So are you saying it's not supported to use a different PyThreadState* to end the interpreter than the one returned from its creation?

All reactions

Sorry, something went wrong.

Uh oh!

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

@ZeroIntensity

ZeroIntensity commented May 17, 2025 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

So are you saying it's not supported to use a different PyThreadState* to end the interpreter than the one returned from its creation?

Yes, and you can't have any additional threads when you call Py_EndInterpreter.

I'm fine with this fix, but my point is that it's not enough. Let's add a test and then I'll approve.

All reactions

Sorry, something went wrong.

Uh oh!

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

@ericsnowcurrently

ericsnowcurrently commented May 17, 2025

Copy link
Copy Markdown
Member

Yeah, a test would be nice.

All reactions

Sorry, something went wrong.

Uh oh!

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

ZeroIntensity
ZeroIntensity reviewed May 17, 2025
View reviewed changes
Comment thread Misc/NEWS.d/next/C_API/2025-05-17-14-41-21.gh-issue-134144.xVpZik.rst Resolved

Uh oh!

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

b-pass and others added 2 commits May 17, 2025 17:47
@b-pass @ZeroIntensity
Update Misc/NEWS.d/next/C_API/2025-05-17-14-41-21.gh-issue-134144.xVp…
985eeda
…Zik.rst

Co-authored-by: Peter Bierma <[email protected]>
@b-pass
Add a test which ends an interpreter with a different PyThreadState t…
6db6f3d
…han normal
kumaraditya303
kumaraditya303 approved these changes May 18, 2025
View reviewed changes
ZeroIntensity
ZeroIntensity approved these changes May 18, 2025
View reviewed changes

@ZeroIntensity ZeroIntensity 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.

Ok, let's go with this.

Sorry, something went wrong.

Uh oh!

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

All reactions
@kumaraditya303
kumaraditya303 merged commit f2de1e6 into python:main May 18, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label May 18, 2025
@miss-islington-app

miss-islington-app Bot commented May 18, 2025

Copy link
Copy Markdown

Thanks @b-pass for the PR, and @kumaraditya303 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 18, 2025
@b-pass @miss-islington
pythongh-134144: Fix use-after-free in zapthreads() (pythonGH-134145)
91712d2
(cherry picked from commit f2de1e6)

Co-authored-by: b-pass <[email protected]>
@bedevere-app

bedevere-app Bot commented May 18, 2025

Copy link
Copy Markdown

GH-134182 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, 2025
kumaraditya303 pushed a commit that referenced this pull request May 18, 2025
@miss-islington @b-pass
[3.14] gh-134144: Fix use-after-free in zapthreads() (GH-134145) (#13…
bb5f92a
…4182)

gh-134144: Fix use-after-free in zapthreads() (GH-134145)
(cherry picked from commit f2de1e6)

Co-authored-by: b-pass <[email protected]>
Pranjal095 pushed a commit to Pranjal095/cpython that referenced this pull request Jul 12, 2025
@b-pass @Pranjal095
pythongh-134144: Fix use-after-free in zapthreads() (python#134145)
2604776
taegyunkim pushed a commit to taegyunkim/cpython that referenced this pull request Aug 4, 2025
@b-pass @taegyunkim
pythongh-134144: Fix use-after-free in zapthreads() (python#134145)
f5ef465
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericsnowcurrently ericsnowcurrently ericsnowcurrently approved these changes
@ZeroIntensity ZeroIntensity ZeroIntensity approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes

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.

Cannot safely Py_EndInterpreter in 3.14b1

Uh oh!

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

4 participants

@b-pass @ZeroIntensity @ericsnowcurrently @kumaraditya303
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 01402520 — 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.