Skip to content

Commit bbaaebd

Browse files
[3.14] gh-151403: Fix use-after-free when an argv item's __fspath__ mutates args (GH-151404) (#151446)
gh-151403: Fix use-after-free when an argv item's __fspath__ mutates args (GH-151404) --------- (cherry picked from commit 6679ac0) Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com> Co-authored-by: tonghuaroot <23011166+tonghuaroot@users.noreply.github.com>
1 parent c8a843c commit bbaaebd

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed a crash in :class:`subprocess.Popen` (and ``_posixsubprocess.fork_exec``)
2+
when an ``argv`` item's :meth:`~os.PathLike.__fspath__` concurrently mutates the
3+
``args`` sequence being converted.

Modules/_posixsubprocess.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1090,8 +1090,14 @@ subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,
10901090
goto cleanup;
10911091
}
10921092
borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
1093-
if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
1093+
/* borrowed_arg is only borrowed; its __fspath__() may run Python
1094+
that drops fast_args' last reference to it. */
1095+
Py_INCREF(borrowed_arg);
1096+
if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) {
1097+
Py_DECREF(borrowed_arg);
10941098
goto cleanup;
1099+
}
1100+
Py_DECREF(borrowed_arg);
10951101
PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
10961102
}
10971103

0 commit comments

Comments
 (0)