Skip to content

[3.9] bpo-42146: Fix memory leak in subprocess.Popen() in case of uid/gid overflow (GH-22966) #22980

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Lib/test/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -1826,6 +1826,10 @@ def test_user(self):
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD, user=-1)

with self.assertRaises(OverflowError):
subprocess.check_call(ZERO_RETURN_CMD,
cwd=os.curdir, env=os.environ, user=2**64)

if pwd is None and name_uid is not None:
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD, user=name_uid)
Expand Down Expand Up @@ -1869,6 +1873,10 @@ def test_group(self):
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD, group=-1)

with self.assertRaises(OverflowError):
subprocess.check_call(ZERO_RETURN_CMD,
cwd=os.curdir, env=os.environ, group=2**64)

if grp is None:
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD, group=name_group)
Expand Down Expand Up @@ -1917,6 +1925,11 @@ def test_extra_groups(self):
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD, extra_groups=[-1])

with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD,
cwd=os.curdir, env=os.environ,
extra_groups=[2**64])

if grp is None:
with self.assertRaises(ValueError):
subprocess.check_call(ZERO_RETURN_CMD,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix memory leak in :func:`subprocess.Popen` in case an uid (gid) specified in
`user` (`group`, `extra_groups`) overflows `uid_t` (`gid_t`).
4 changes: 2 additions & 2 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
uid_t uid;
gid_t gid, *groups = NULL;
int child_umask;
PyObject *cwd_obj, *cwd_obj2;
PyObject *cwd_obj, *cwd_obj2 = NULL;
const char *cwd;
pid_t pid;
int need_to_reenable_gc = 0;
Expand Down Expand Up @@ -748,7 +748,6 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
cwd = PyBytes_AsString(cwd_obj2);
} else {
cwd = NULL;
cwd_obj2 = NULL;
}

if (groups_list != Py_None) {
Expand Down Expand Up @@ -908,6 +907,7 @@ subprocess_fork_exec(PyObject* self, PyObject *args)
return PyLong_FromPid(pid);

cleanup:
Py_XDECREF(cwd_obj2);
if (envp)
_Py_FreeCharPArray(envp);
if (argv)
Expand Down