Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Supplementary group IDs |
| 4 | */ |
| 5 | #include <linux/cred.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 6 | #include <linux/export.h> |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 7 | #include <linux/slab.h> |
| 8 | #include <linux/security.h> |
Rasmus Villemoes | b7b2562 | 2017-07-10 15:51:17 -0700 | [diff] [blame] | 9 | #include <linux/sort.h> |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 10 | #include <linux/syscalls.h> |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 11 | #include <linux/user_namespace.h> |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 12 | #include <linux/vmalloc.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 13 | #include <linux/uaccess.h> |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 14 | |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 15 | struct group_info *groups_alloc(int gidsetsize) |
| 16 | { |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 17 | struct group_info *gi; |
Hubert Jasudowicz | e1e0141 | 2021-02-25 17:21:07 -0800 | [diff] [blame] | 18 | gi = kvmalloc(struct_size(gi, gid, gidsetsize), GFP_KERNEL_ACCOUNT); |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 19 | if (!gi) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 20 | return NULL; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 21 | |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 22 | atomic_set(&gi->usage, 1); |
| 23 | gi->ngroups = gidsetsize; |
| 24 | return gi; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | EXPORT_SYMBOL(groups_alloc); |
| 28 | |
| 29 | void groups_free(struct group_info *group_info) |
| 30 | { |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 31 | kvfree(group_info); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | EXPORT_SYMBOL(groups_free); |
| 35 | |
| 36 | /* export the group_info to a user-space array */ |
| 37 | static int groups_to_user(gid_t __user *grouplist, |
| 38 | const struct group_info *group_info) |
| 39 | { |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 40 | struct user_namespace *user_ns = current_user_ns(); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 41 | int i; |
| 42 | unsigned int count = group_info->ngroups; |
| 43 | |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 44 | for (i = 0; i < count; i++) { |
| 45 | gid_t gid; |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 46 | gid = from_kgid_munged(user_ns, group_info->gid[i]); |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 47 | if (put_user(gid, grouplist+i)) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 48 | return -EFAULT; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | /* fill a group_info from a user-space array - it must be allocated already */ |
| 54 | static int groups_from_user(struct group_info *group_info, |
| 55 | gid_t __user *grouplist) |
| 56 | { |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 57 | struct user_namespace *user_ns = current_user_ns(); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 58 | int i; |
| 59 | unsigned int count = group_info->ngroups; |
| 60 | |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 61 | for (i = 0; i < count; i++) { |
| 62 | gid_t gid; |
| 63 | kgid_t kgid; |
| 64 | if (get_user(gid, grouplist+i)) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 65 | return -EFAULT; |
| 66 | |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 67 | kgid = make_kgid(user_ns, gid); |
| 68 | if (!gid_valid(kgid)) |
| 69 | return -EINVAL; |
| 70 | |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 71 | group_info->gid[i] = kgid; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 72 | } |
| 73 | return 0; |
| 74 | } |
| 75 | |
Rasmus Villemoes | b7b2562 | 2017-07-10 15:51:17 -0700 | [diff] [blame] | 76 | static int gid_cmp(const void *_a, const void *_b) |
| 77 | { |
| 78 | kgid_t a = *(kgid_t *)_a; |
| 79 | kgid_t b = *(kgid_t *)_b; |
| 80 | |
| 81 | return gid_gt(a, b) - gid_lt(a, b); |
| 82 | } |
| 83 | |
Thiago Rafael Becker | bdcf0a4 | 2017-12-14 15:33:12 -0800 | [diff] [blame] | 84 | void groups_sort(struct group_info *group_info) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 85 | { |
Rasmus Villemoes | b7b2562 | 2017-07-10 15:51:17 -0700 | [diff] [blame] | 86 | sort(group_info->gid, group_info->ngroups, sizeof(*group_info->gid), |
| 87 | gid_cmp, NULL); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 88 | } |
Thiago Rafael Becker | bdcf0a4 | 2017-12-14 15:33:12 -0800 | [diff] [blame] | 89 | EXPORT_SYMBOL(groups_sort); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 90 | |
| 91 | /* a simple bsearch */ |
Eric W. Biederman | ae2975b | 2011-11-14 15:56:38 -0800 | [diff] [blame] | 92 | int groups_search(const struct group_info *group_info, kgid_t grp) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 93 | { |
| 94 | unsigned int left, right; |
| 95 | |
| 96 | if (!group_info) |
| 97 | return 0; |
| 98 | |
| 99 | left = 0; |
| 100 | right = group_info->ngroups; |
| 101 | while (left < right) { |
| 102 | unsigned int mid = (left+right)/2; |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 103 | if (gid_gt(grp, group_info->gid[mid])) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 104 | left = mid + 1; |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 105 | else if (gid_lt(grp, group_info->gid[mid])) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 106 | right = mid; |
| 107 | else |
| 108 | return 1; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * set_groups - Change a group subscription in a set of credentials |
| 115 | * @new: The newly prepared set of credentials to alter |
| 116 | * @group_info: The group list to install |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 117 | */ |
Wang YanQing | 8f6c5ff | 2014-04-03 14:48:26 -0700 | [diff] [blame] | 118 | void set_groups(struct cred *new, struct group_info *group_info) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 119 | { |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 120 | put_group_info(new->group_info); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 121 | get_group_info(group_info); |
| 122 | new->group_info = group_info; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | EXPORT_SYMBOL(set_groups); |
| 126 | |
| 127 | /** |
| 128 | * set_current_groups - Change current's group subscription |
| 129 | * @group_info: The group list to impose |
| 130 | * |
| 131 | * Validate a group subscription and, if valid, impose it upon current's task |
| 132 | * security record. |
| 133 | */ |
| 134 | int set_current_groups(struct group_info *group_info) |
| 135 | { |
| 136 | struct cred *new; |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 137 | |
| 138 | new = prepare_creds(); |
| 139 | if (!new) |
| 140 | return -ENOMEM; |
| 141 | |
Wang YanQing | 8f6c5ff | 2014-04-03 14:48:26 -0700 | [diff] [blame] | 142 | set_groups(new, group_info); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 143 | return commit_creds(new); |
| 144 | } |
| 145 | |
| 146 | EXPORT_SYMBOL(set_current_groups); |
| 147 | |
| 148 | SYSCALL_DEFINE2(getgroups, int, gidsetsize, gid_t __user *, grouplist) |
| 149 | { |
| 150 | const struct cred *cred = current_cred(); |
| 151 | int i; |
| 152 | |
| 153 | if (gidsetsize < 0) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | /* no need to grab task_lock here; it cannot change */ |
| 157 | i = cred->group_info->ngroups; |
| 158 | if (gidsetsize) { |
| 159 | if (i > gidsetsize) { |
| 160 | i = -EINVAL; |
| 161 | goto out; |
| 162 | } |
| 163 | if (groups_to_user(grouplist, cred->group_info)) { |
| 164 | i = -EFAULT; |
| 165 | goto out; |
| 166 | } |
| 167 | } |
| 168 | out: |
| 169 | return i; |
| 170 | } |
| 171 | |
Eric W. Biederman | 7ff4d90 | 2014-12-05 17:19:27 -0600 | [diff] [blame] | 172 | bool may_setgroups(void) |
| 173 | { |
| 174 | struct user_namespace *user_ns = current_user_ns(); |
| 175 | |
Thomas Cedeno | 111767c | 2020-07-16 19:13:57 +0000 | [diff] [blame] | 176 | return ns_capable_setid(user_ns, CAP_SETGID) && |
Eric W. Biederman | 273d2c6 | 2014-12-05 18:01:11 -0600 | [diff] [blame] | 177 | userns_may_setgroups(user_ns); |
Eric W. Biederman | 7ff4d90 | 2014-12-05 17:19:27 -0600 | [diff] [blame] | 178 | } |
| 179 | |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 180 | /* |
| 181 | * SMP: Our groups are copy-on-write. We can set them safely |
| 182 | * without another task interfering. |
| 183 | */ |
| 184 | |
| 185 | SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist) |
| 186 | { |
| 187 | struct group_info *group_info; |
| 188 | int retval; |
| 189 | |
Eric W. Biederman | 7ff4d90 | 2014-12-05 17:19:27 -0600 | [diff] [blame] | 190 | if (!may_setgroups()) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 191 | return -EPERM; |
| 192 | if ((unsigned)gidsetsize > NGROUPS_MAX) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | group_info = groups_alloc(gidsetsize); |
| 196 | if (!group_info) |
| 197 | return -ENOMEM; |
| 198 | retval = groups_from_user(group_info, grouplist); |
| 199 | if (retval) { |
| 200 | put_group_info(group_info); |
| 201 | return retval; |
| 202 | } |
| 203 | |
Thiago Rafael Becker | bdcf0a4 | 2017-12-14 15:33:12 -0800 | [diff] [blame] | 204 | groups_sort(group_info); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 205 | retval = set_current_groups(group_info); |
| 206 | put_group_info(group_info); |
| 207 | |
| 208 | return retval; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Check whether we're fsgid/egid or in the supplemental group.. |
| 213 | */ |
Eric W. Biederman | 72cda3d | 2012-02-09 09:09:39 -0800 | [diff] [blame] | 214 | int in_group_p(kgid_t grp) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 215 | { |
| 216 | const struct cred *cred = current_cred(); |
| 217 | int retval = 1; |
| 218 | |
Eric W. Biederman | 72cda3d | 2012-02-09 09:09:39 -0800 | [diff] [blame] | 219 | if (!gid_eq(grp, cred->fsgid)) |
| 220 | retval = groups_search(cred->group_info, grp); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 221 | return retval; |
| 222 | } |
| 223 | |
| 224 | EXPORT_SYMBOL(in_group_p); |
| 225 | |
Eric W. Biederman | 72cda3d | 2012-02-09 09:09:39 -0800 | [diff] [blame] | 226 | int in_egroup_p(kgid_t grp) |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 227 | { |
| 228 | const struct cred *cred = current_cred(); |
| 229 | int retval = 1; |
| 230 | |
Eric W. Biederman | 72cda3d | 2012-02-09 09:09:39 -0800 | [diff] [blame] | 231 | if (!gid_eq(grp, cred->egid)) |
| 232 | retval = groups_search(cred->group_info, grp); |
Alexey Dobriyan | 30639b6a | 2009-06-16 15:33:40 -0700 | [diff] [blame] | 233 | return retval; |
| 234 | } |
| 235 | |
| 236 | EXPORT_SYMBOL(in_egroup_p); |