[PATCHv3 2/2] groups: Allow unprivileged processes to use setgroups to drop groups

From: Josh Triplett
Date: Sat Nov 15 2014 - 18:50:34 EST


Currently, unprivileged processes (without CAP_SETGID) cannot call
setgroups at all. In particular, processes with a set of supplementary
groups cannot further drop permissions without obtaining elevated
permissions first.

Allow unprivileged processes to call setgroups with a subset of their
current groups; only require CAP_SETGID to add a group the process does
not currently have (either as a supplementary group, or as its gid,
egid, or sgid).

Since some privileged programs (such as sudo) allow tests for
non-membership in a group, require no_new_privs to drop group
privileges.

The kernel already maintains the list of supplementary group IDs in
sorted order, and setgroups already needs to sort the new list, so this
just requires a linear comparison of the two sorted lists.

This moves the CAP_SETGID test from setgroups into set_current_groups.

Tested via the following test program:

void run_id(void)
{
pid_t p = fork();
switch (p) {
case -1:
err(1, "fork");
case 0:
execl("/usr/bin/id", "id", NULL);
err(1, "exec");
default:
if (waitpid(p, NULL, 0) < 0)
err(1, "waitpid");
}
}

int main(void)
{
gid_t list1[] = { 1, 2, 3, 4, 5 };
gid_t list2[] = { 2, 3, 4 };
run_id();
if (setgroups(5, list1) < 0)
err(1, "setgroups 1");
run_id();
if (setresgid(1, 1, 1) < 0)
err(1, "setresgid");
if (setresuid(1, 1, 1) < 0)
err(1, "setresuid");
run_id();
if (setgroups(3, list2) < 0)
err(1, "setgroups 2");
run_id();
if (setgroups(5, list1) < 0)
err(1, "setgroups 3");
run_id();

return 0;
}

Without this patch, the test program gets EPERM from the second
setgroups call, after dropping root privileges. With this patch, the
test program successfully drops groups 1 and 5, but then gets EPERM from
the third setgroups call, since that call attempts to add groups the
process does not currently have.

Signed-off-by: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
---
v3: Allow gid, egid, or sgid.
v2: Require no_new_privs.

kernel/groups.c | 42 +++++++++++++++++++++++++++++++++++++++---
kernel/uid16.c | 2 --
2 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/kernel/groups.c b/kernel/groups.c
index f0667e7..5114155 100644
--- a/kernel/groups.c
+++ b/kernel/groups.c
@@ -153,6 +153,37 @@ int groups_search(const struct group_info *group_info, kgid_t grp)
return 0;
}

+/* Return true if the group_info is a subset of the group_info of the specified
+ * credentials. Also allow the first group_info to contain the gid, egid, or
+ * sgid of the credentials.
+ */
+static bool group_subset(const struct group_info *g1,
+ const struct cred *cred2)
+{
+ const struct group_info *g2 = cred2->group_info;
+ unsigned int i, j;
+
+ for (i = 0, j = 0; i < g1->ngroups; i++) {
+ kgid_t gid1 = GROUP_AT(g1, i);
+ kgid_t gid2;
+ for (; j < g2->ngroups; j++) {
+ gid2 = GROUP_AT(g2, j);
+ if (gid_lte(gid1, gid2))
+ break;
+ }
+ if (j >= g2->ngroups || !gid_eq(gid1, gid2)) {
+ if (!gid_eq(gid1, cred2->gid)
+ && !gid_eq(gid1, cred2->egid)
+ && !gid_eq(gid1, cred2->sgid))
+ return false;
+ } else {
+ j++;
+ }
+ }
+
+ return true;
+}
+
/**
* set_groups_sorted - Change a group subscription in a set of credentials
* @new: The newly prepared set of credentials to alter
@@ -189,11 +220,18 @@ int set_current_groups(struct group_info *group_info)
{
struct cred *new;

+ groups_sort(group_info);
new = prepare_creds();
if (!new)
return -ENOMEM;
+ if (!(ns_capable(current_user_ns(), CAP_SETGID)
+ || (task_no_new_privs(current)
+ && group_subset(group_info, new)))) {
+ abort_creds(new);
+ return -EPERM;
+ }

- set_groups(new, group_info);
+ set_groups_sorted(new, group_info);
return commit_creds(new);
}

@@ -233,8 +271,6 @@ SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
struct group_info *group_info;
int retval;

- if (!ns_capable(current_user_ns(), CAP_SETGID))
- return -EPERM;
if ((unsigned)gidsetsize > NGROUPS_MAX)
return -EINVAL;

diff --git a/kernel/uid16.c b/kernel/uid16.c
index 602e5bb..b27e167 100644
--- a/kernel/uid16.c
+++ b/kernel/uid16.c
@@ -176,8 +176,6 @@ SYSCALL_DEFINE2(setgroups16, int, gidsetsize, old_gid_t __user *, grouplist)
struct group_info *group_info;
int retval;

- if (!ns_capable(current_user_ns(), CAP_SETGID))
- return -EPERM;
if ((unsigned)gidsetsize > NGROUPS_MAX)
return -EINVAL;

--
2.1.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/