blob: 787b381c7c0026c31793ab1ebd0c2383ced820f0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -07002/*
3 * Supplementary group IDs
4 */
5#include <linux/cred.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -04006#include <linux/export.h>
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -07007#include <linux/slab.h>
8#include <linux/security.h>
Rasmus Villemoesb7b25622017-07-10 15:51:17 -07009#include <linux/sort.h>
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070010#include <linux/syscalls.h>
Eric W. Biederman273d2c62014-12-05 18:01:11 -060011#include <linux/user_namespace.h>
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070012#include <linux/vmalloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080013#include <linux/uaccess.h>
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070014
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070015struct group_info *groups_alloc(int gidsetsize)
16{
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070017 struct group_info *gi;
Hubert Jasudowicze1e01412021-02-25 17:21:07 -080018 gi = kvmalloc(struct_size(gi, gid, gidsetsize), GFP_KERNEL_ACCOUNT);
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070019 if (!gi)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070020 return NULL;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070021
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070022 atomic_set(&gi->usage, 1);
23 gi->ngroups = gidsetsize;
24 return gi;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070025}
26
27EXPORT_SYMBOL(groups_alloc);
28
29void groups_free(struct group_info *group_info)
30{
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070031 kvfree(group_info);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070032}
33
34EXPORT_SYMBOL(groups_free);
35
36/* export the group_info to a user-space array */
37static int groups_to_user(gid_t __user *grouplist,
38 const struct group_info *group_info)
39{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080040 struct user_namespace *user_ns = current_user_ns();
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070041 int i;
42 unsigned int count = group_info->ngroups;
43
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080044 for (i = 0; i < count; i++) {
45 gid_t gid;
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070046 gid = from_kgid_munged(user_ns, group_info->gid[i]);
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080047 if (put_user(gid, grouplist+i))
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070048 return -EFAULT;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070049 }
50 return 0;
51}
52
53/* fill a group_info from a user-space array - it must be allocated already */
54static int groups_from_user(struct group_info *group_info,
55 gid_t __user *grouplist)
56{
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080057 struct user_namespace *user_ns = current_user_ns();
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070058 int i;
59 unsigned int count = group_info->ngroups;
60
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080061 for (i = 0; i < count; i++) {
62 gid_t gid;
63 kgid_t kgid;
64 if (get_user(gid, grouplist+i))
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070065 return -EFAULT;
66
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080067 kgid = make_kgid(user_ns, gid);
68 if (!gid_valid(kgid))
69 return -EINVAL;
70
Alexey Dobriyan81243ea2016-10-07 17:03:12 -070071 group_info->gid[i] = kgid;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070072 }
73 return 0;
74}
75
Rasmus Villemoesb7b25622017-07-10 15:51:17 -070076static 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 Beckerbdcf0a42017-12-14 15:33:12 -080084void groups_sort(struct group_info *group_info)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070085{
Rasmus Villemoesb7b25622017-07-10 15:51:17 -070086 sort(group_info->gid, group_info->ngroups, sizeof(*group_info->gid),
87 gid_cmp, NULL);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070088}
Thiago Rafael Beckerbdcf0a42017-12-14 15:33:12 -080089EXPORT_SYMBOL(groups_sort);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070090
91/* a simple bsearch */
Eric W. Biedermanae2975b2011-11-14 15:56:38 -080092int groups_search(const struct group_info *group_info, kgid_t grp)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -070093{
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 Dobriyan81243ea2016-10-07 17:03:12 -0700103 if (gid_gt(grp, group_info->gid[mid]))
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700104 left = mid + 1;
Alexey Dobriyan81243ea2016-10-07 17:03:12 -0700105 else if (gid_lt(grp, group_info->gid[mid]))
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700106 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 Dobriyan30639b6a2009-06-16 15:33:40 -0700117 */
Wang YanQing8f6c5ff2014-04-03 14:48:26 -0700118void set_groups(struct cred *new, struct group_info *group_info)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700119{
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700120 put_group_info(new->group_info);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700121 get_group_info(group_info);
122 new->group_info = group_info;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700123}
124
125EXPORT_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 */
134int set_current_groups(struct group_info *group_info)
135{
136 struct cred *new;
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700137
138 new = prepare_creds();
139 if (!new)
140 return -ENOMEM;
141
Wang YanQing8f6c5ff2014-04-03 14:48:26 -0700142 set_groups(new, group_info);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700143 return commit_creds(new);
144}
145
146EXPORT_SYMBOL(set_current_groups);
147
148SYSCALL_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 }
168out:
169 return i;
170}
171
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600172bool may_setgroups(void)
173{
174 struct user_namespace *user_ns = current_user_ns();
175
Thomas Cedeno111767c2020-07-16 19:13:57 +0000176 return ns_capable_setid(user_ns, CAP_SETGID) &&
Eric W. Biederman273d2c62014-12-05 18:01:11 -0600177 userns_may_setgroups(user_ns);
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600178}
179
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700180/*
181 * SMP: Our groups are copy-on-write. We can set them safely
182 * without another task interfering.
183 */
184
185SYSCALL_DEFINE2(setgroups, int, gidsetsize, gid_t __user *, grouplist)
186{
187 struct group_info *group_info;
188 int retval;
189
Eric W. Biederman7ff4d902014-12-05 17:19:27 -0600190 if (!may_setgroups())
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700191 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 Beckerbdcf0a42017-12-14 15:33:12 -0800204 groups_sort(group_info);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700205 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. Biederman72cda3d2012-02-09 09:09:39 -0800214int in_group_p(kgid_t grp)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700215{
216 const struct cred *cred = current_cred();
217 int retval = 1;
218
Eric W. Biederman72cda3d2012-02-09 09:09:39 -0800219 if (!gid_eq(grp, cred->fsgid))
220 retval = groups_search(cred->group_info, grp);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700221 return retval;
222}
223
224EXPORT_SYMBOL(in_group_p);
225
Eric W. Biederman72cda3d2012-02-09 09:09:39 -0800226int in_egroup_p(kgid_t grp)
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700227{
228 const struct cred *cred = current_cred();
229 int retval = 1;
230
Eric W. Biederman72cda3d2012-02-09 09:09:39 -0800231 if (!gid_eq(grp, cred->egid))
232 retval = groups_search(cred->group_info, grp);
Alexey Dobriyan30639b6a2009-06-16 15:33:40 -0700233 return retval;
234}
235
236EXPORT_SYMBOL(in_egroup_p);