Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-134144: Fix use-after-free in zapthreads()#134145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
kumaraditya303 merged 5 commits intopython:mainfromb-pass:fix-gh-134144
May 18, 2025

Conversation

b-pass
Copy link
Contributor

@b-passb-pass commentedMay 17, 2025
edited by bedevere-appbot
Loading

Fixes#134144. This problem was introduced by#127077 which added_Py_FOR_EACH_TSTATE_UNLOCKED.

_Py_FOR_EACH_TSTATE_UNLOCKED reads thetstate at the end of each loop iteration to get thenext value. When the loop body frees thetstate, 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.

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.
@python-cla-bot
Copy link

python-cla-botbot commentedMay 17, 2025
edited
Loading

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

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

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

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

@ericsnowcurrentlyericsnowcurrently added the needs backport to 3.14bugs and security fixes labelMay 17, 2025
Copy link
Member

@ZeroIntensityZeroIntensity left a 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 dropzapthreads 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.

Copy link
Member

@ericsnowcurrentlyericsnowcurrently left a 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, regardinggh-128640, I expect we'll do that too since it solves an additional problem. However, this PR is a good idea regardless.

@ZeroIntensity
Copy link
Member

ZeroIntensity commentedMay 17, 2025
edited
Loading

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 byPyGILState_Ensure in the repro), which is a recipe for crashes if the thread is still running.

@b-pass
Copy link
ContributorAuthor

b-pass commentedMay 17, 2025
edited
Loading

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 byPyGILState_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, butPy_EndInterpreter takes aPyThreadState* as the argument (not aPyInterpreterState*), 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 somepseudo code of what is going on:

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

@ZeroIntensity
Copy link
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 likePyGILState_Ensure because we have more than one thread state, but the problem is that's unsupported anyway.

@b-pass
Copy link
ContributorAuthor

It becomes an issue with something likePyGILState_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 differentPyThreadState* to end the interpreter than the one returned from its creation?

@ZeroIntensity
Copy link
Member

ZeroIntensity commentedMay 17, 2025
edited
Loading

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 callPy_EndInterpreter.

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

@ericsnowcurrently
Copy link
Member

Yeah, a test would be nice.

Copy link
Member

@ZeroIntensityZeroIntensity left a 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.

@kumaraditya303kumaraditya303 merged commitf2de1e6 intopython:mainMay 18, 2025
39 checks passed
@miss-islington-app
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestMay 18, 2025
(cherry picked from commitf2de1e6)Co-authored-by: b-pass <b-pass@users.noreply.github.com>
@bedevere-app
Copy link

GH-134182 is a backport of this pull request to the3.14 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.14bugs and security fixes labelMay 18, 2025
kumaraditya303 pushed a commit that referenced this pull requestMay 18, 2025
…4182)gh-134144: Fix use-after-free in zapthreads() (GH-134145)(cherry picked from commitf2de1e6)Co-authored-by: b-pass <b-pass@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ericsnowcurrentlyericsnowcurrentlyericsnowcurrently approved these changes

@ZeroIntensityZeroIntensityZeroIntensity approved these changes

@kumaraditya303kumaraditya303kumaraditya303 approved these changes

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
4 participants
@b-pass@ZeroIntensity@ericsnowcurrently@kumaraditya303

[8]ページ先頭

©2009-2025 Movatter.jp