gh-151403: Fix use-after-free when an argv item's __fspath__ mutates args#151404
Open
tonghuaroot wants to merge 1 commit into
Open
gh-151403: Fix use-after-free when an argv item's __fspath__ mutates args#151404tonghuaroot wants to merge 1 commit into
tonghuaroot wants to merge 1 commit into
Conversation
gpshead
reviewed
Jun 12, 2026
| gc.disable() | ||
|
|
||
| @support.cpython_only | ||
| def test_fork_exec_args_concurrent_mutation(self): |
Member
There was a problem hiding this comment.
lets not add this test. it is perhaps useful for illustration purposes, but this is not specific to subprocess. it's C API misuse. FWIW, thanks! overall the rest of the PR looks good.
I've pointed claude fable at the antipattern to find and fixup others and improve the docs as a future prevention matter: #151416
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
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.
_posixsubprocess.fork_exec()converts eachargsitem withPyUnicode_FSConverter(), which runs the item's__fspath__(). The item is onlyborrowed from the (incref'd)
argssequence. If__fspath__()drops thesequence's last reference to the item and then returns a non-
str/bytesobject, the error path in
PyOS_FSPath()readsPy_TYPE()of the now-freed item— a use-after-free.
It is reachable from
subprocess.Popenwhen a laterargvitem is aPathLikewhose
__fspath__()mutates theargslist, e.g.subprocess.Popen(["/bin/true", Evil()]).The between-iteration length recheck only catches a shrink between iterations;
the free here happens inside the current iteration's
PyUnicode_FSConverter()call. The fix keeps the borrowed item alive with an
Py_INCREFacross theconversion.
Same family as gh-151295 (
bytes.join, GH-151296) and gh-151370(
marshal.dumps, GH-151371). Triggering requires a custom__fspath__, so thisis a robustness / crash-hardening fix with no security impact; the NEWS entry is
under
Library/.