Skip to content

gh-94518: [_posixsubprocess] Replace variable validity flags with reserved values #94687

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 14 commits into from
Jan 14, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use negative groups_size for "don't call setgroups"
When we query `groups_list` size using `PySequence_Size()`, we get -1
for non-lists. It returns an ability to pass an empty list to
`setgroups()` that is valid but treated as a no-op:

> If `size` is zero, `list` is not modified, but the total number of
> supplementary group IDs for the process is returned. This allows the
> caller to determine the size of a dynamically allocated list to be
> used in a further call to getgroups().
>
> - <https://linux.die.net/man/2/setgroups>
  • Loading branch information
arhadthedev committed Jul 27, 2022
commit aaec16efae3da5bf3d3e78eb7355d34f39e86fb8
18 changes: 11 additions & 7 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ child_exec(char *const exec_array[],
int close_fds, int restore_signals,
int call_setsid, pid_t pgid_to_set,
gid_t gid,
size_t groups_size, const gid_t *groups,
Py_ssize_t groups_size, const gid_t *groups,
uid_t uid, int child_umask,
const void *child_sigmask,
PyObject *py_fds_to_keep,
Expand Down Expand Up @@ -619,7 +619,7 @@ child_exec(char *const exec_array[],
#endif

#ifdef HAVE_SETGROUPS
if (groups)
if (groups_size >= 0)
POSIX_CALL(setgroups(groups_size, groups));
#endif /* HAVE_SETGROUPS */

Expand Down Expand Up @@ -725,7 +725,7 @@ do_fork_exec(char *const exec_array[],
int close_fds, int restore_signals,
int call_setsid, pid_t pgid_to_set,
gid_t gid,
size_t groups_size, const gid_t *groups,
Py_ssize_t groups_size, const gid_t *groups,
uid_t uid, int child_umask,
const void *child_sigmask,
PyObject *py_fds_to_keep,
Expand Down Expand Up @@ -920,10 +920,14 @@ subprocess_fork_exec(PyObject *module, PyObject *args)
goto cleanup;
}

if ((groups = PyMem_RawMalloc(num_groups * sizeof(gid_t))) == NULL) {
PyErr_SetString(PyExc_MemoryError,
"failed to allocate memory for group list");
goto cleanup;
/* Deliberately keep groups == NULL for num_groups == 0 */
if (num_groups > 0) {
groups = PyMem_RawMalloc(num_groups * sizeof(gid_t));
if (groups == NULL) {
PyErr_SetString(PyExc_MemoryError,
"failed to allocate memory for group list");
goto cleanup;
}
}

for (i = 0; i < num_groups; i++) {
Expand Down