Skip to content

Commit 7964840

Browse files
tonghuarootmiss-islington
authored andcommitted
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 9cbbb07 commit 7964840

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
@@ -1089,8 +1089,14 @@ subprocess_fork_exec_impl(PyObject *module, PyObject *process_args,
10891089
goto cleanup;
10901090
}
10911091
borrowed_arg = PySequence_Fast_GET_ITEM(fast_args, arg_num);
1092-
if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0)
1092+
/* borrowed_arg is only borrowed; its __fspath__() may run Python
1093+
that drops fast_args' last reference to it. */
1094+
Py_INCREF(borrowed_arg);
1095+
if (PyUnicode_FSConverter(borrowed_arg, &converted_arg) == 0) {
1096+
Py_DECREF(borrowed_arg);
10931097
goto cleanup;
1098+
}
1099+
Py_DECREF(borrowed_arg);
10941100
PyTuple_SET_ITEM(converted_args, arg_num, converted_arg);
10951101
}
10961102

0 commit comments

Comments
 (0)