Skip to content

Commit c0590c0

Browse files
author
Alexey Izbyshev
authored
bpo-42146: Fix memory leak in subprocess.Popen() in case of uid/gid overflow (GH-22966)
Fix memory leak in subprocess.Popen() in case of uid/gid overflow Also add a test that would catch this leak with `--huntrleaks`. Alas, the test for `extra_groups` also exposes an inconsistency in our error reporting: we use a custom ValueError for `extra_groups`, but propagate OverflowError for `user` and `group`.
1 parent e68c678 commit c0590c0

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Lib/test/test_subprocess.py

+13
Original file line numberDiff line numberDiff line change
@@ -1895,6 +1895,10 @@ def test_user(self):
18951895
with self.assertRaises(ValueError):
18961896
subprocess.check_call(ZERO_RETURN_CMD, user=-1)
18971897

1898+
with self.assertRaises(OverflowError):
1899+
subprocess.check_call(ZERO_RETURN_CMD,
1900+
cwd=os.curdir, env=os.environ, user=2**64)
1901+
18981902
if pwd is None and name_uid is not None:
18991903
with self.assertRaises(ValueError):
19001904
subprocess.check_call(ZERO_RETURN_CMD, user=name_uid)
@@ -1938,6 +1942,10 @@ def test_group(self):
19381942
with self.assertRaises(ValueError):
19391943
subprocess.check_call(ZERO_RETURN_CMD, group=-1)
19401944

1945+
with self.assertRaises(OverflowError):
1946+
subprocess.check_call(ZERO_RETURN_CMD,
1947+
cwd=os.curdir, env=os.environ, group=2**64)
1948+
19411949
if grp is None:
19421950
with self.assertRaises(ValueError):
19431951
subprocess.check_call(ZERO_RETURN_CMD, group=name_group)
@@ -1986,6 +1994,11 @@ def test_extra_groups(self):
19861994
with self.assertRaises(ValueError):
19871995
subprocess.check_call(ZERO_RETURN_CMD, extra_groups=[-1])
19881996

1997+
with self.assertRaises(ValueError):
1998+
subprocess.check_call(ZERO_RETURN_CMD,
1999+
cwd=os.curdir, env=os.environ,
2000+
extra_groups=[2**64])
2001+
19892002
if grp is None:
19902003
with self.assertRaises(ValueError):
19912004
subprocess.check_call(ZERO_RETURN_CMD,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix memory leak in :func:`subprocess.Popen` in case an uid (gid) specified in
2+
`user` (`group`, `extra_groups`) overflows `uid_t` (`gid_t`).

Modules/_posixsubprocess.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
772772
uid_t uid;
773773
gid_t gid, *groups = NULL;
774774
int child_umask;
775-
PyObject *cwd_obj, *cwd_obj2;
775+
PyObject *cwd_obj, *cwd_obj2 = NULL;
776776
const char *cwd;
777777
pid_t pid;
778778
int need_to_reenable_gc = 0;
@@ -894,7 +894,6 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
894894
cwd = PyBytes_AsString(cwd_obj2);
895895
} else {
896896
cwd = NULL;
897-
cwd_obj2 = NULL;
898897
}
899898

900899
if (groups_list != Py_None) {
@@ -1080,6 +1079,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
10801079
return PyLong_FromPid(pid);
10811080

10821081
cleanup:
1082+
Py_XDECREF(cwd_obj2);
10831083
if (envp)
10841084
_Py_FreeCharPArray(envp);
10851085
if (argv)

0 commit comments

Comments
 (0)