Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 | 2 | /* |
5c8ebd57 | 3 | * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org> |
1da177e4 | 4 | * |
5c8ebd57 CH |
5 | * Fixes from William Schumacher incorporated on 15 March 2001. |
6 | * (Reported by Charles Bertsch, <CBertsch@microtest.com>). | |
1da177e4 LT |
7 | */ |
8 | ||
9 | /* | |
10 | * This file contains generic functions for manipulating | |
11 | * POSIX 1003.1e draft standard 17 ACLs. | |
12 | */ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/slab.h> | |
60063497 | 16 | #include <linux/atomic.h> |
1da177e4 LT |
17 | #include <linux/fs.h> |
18 | #include <linux/sched.h> | |
5b825c3a | 19 | #include <linux/cred.h> |
1da177e4 | 20 | #include <linux/posix_acl.h> |
5c8ebd57 | 21 | #include <linux/posix_acl_xattr.h> |
2aeccbe9 | 22 | #include <linux/xattr.h> |
630d9c47 | 23 | #include <linux/export.h> |
5c8ebd57 | 24 | #include <linux/user_namespace.h> |
332f606b | 25 | #include <linux/namei.h> |
1da177e4 | 26 | |
04c57f45 | 27 | static struct posix_acl **acl_by_type(struct inode *inode, int type) |
0afaa120 AM |
28 | { |
29 | switch (type) { | |
30 | case ACL_TYPE_ACCESS: | |
31 | return &inode->i_acl; | |
32 | case ACL_TYPE_DEFAULT: | |
33 | return &inode->i_default_acl; | |
34 | default: | |
35 | BUG(); | |
36 | } | |
37 | } | |
0afaa120 AM |
38 | |
39 | struct posix_acl *get_cached_acl(struct inode *inode, int type) | |
40 | { | |
41 | struct posix_acl **p = acl_by_type(inode, type); | |
b8a7a3a6 AG |
42 | struct posix_acl *acl; |
43 | ||
44 | for (;;) { | |
45 | rcu_read_lock(); | |
46 | acl = rcu_dereference(*p); | |
47 | if (!acl || is_uncached_acl(acl) || | |
66717260 | 48 | refcount_inc_not_zero(&acl->a_refcount)) |
b8a7a3a6 AG |
49 | break; |
50 | rcu_read_unlock(); | |
51 | cpu_relax(); | |
0afaa120 | 52 | } |
b8a7a3a6 | 53 | rcu_read_unlock(); |
0afaa120 AM |
54 | return acl; |
55 | } | |
56 | EXPORT_SYMBOL(get_cached_acl); | |
57 | ||
58 | struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type) | |
59 | { | |
332f606b MS |
60 | struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type)); |
61 | ||
62 | if (acl == ACL_DONT_CACHE) { | |
63 | struct posix_acl *ret; | |
64 | ||
65 | ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU); | |
66 | if (!IS_ERR(ret)) | |
67 | acl = ret; | |
68 | } | |
69 | ||
70 | return acl; | |
0afaa120 AM |
71 | } |
72 | EXPORT_SYMBOL(get_cached_acl_rcu); | |
73 | ||
74 | void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl) | |
75 | { | |
76 | struct posix_acl **p = acl_by_type(inode, type); | |
77 | struct posix_acl *old; | |
b8a7a3a6 AG |
78 | |
79 | old = xchg(p, posix_acl_dup(acl)); | |
80 | if (!is_uncached_acl(old)) | |
0afaa120 AM |
81 | posix_acl_release(old); |
82 | } | |
83 | EXPORT_SYMBOL(set_cached_acl); | |
84 | ||
b8a7a3a6 | 85 | static void __forget_cached_acl(struct posix_acl **p) |
0afaa120 | 86 | { |
0afaa120 | 87 | struct posix_acl *old; |
b8a7a3a6 AG |
88 | |
89 | old = xchg(p, ACL_NOT_CACHED); | |
90 | if (!is_uncached_acl(old)) | |
0afaa120 AM |
91 | posix_acl_release(old); |
92 | } | |
b8a7a3a6 AG |
93 | |
94 | void forget_cached_acl(struct inode *inode, int type) | |
95 | { | |
96 | __forget_cached_acl(acl_by_type(inode, type)); | |
97 | } | |
0afaa120 AM |
98 | EXPORT_SYMBOL(forget_cached_acl); |
99 | ||
100 | void forget_all_cached_acls(struct inode *inode) | |
101 | { | |
b8a7a3a6 AG |
102 | __forget_cached_acl(&inode->i_acl); |
103 | __forget_cached_acl(&inode->i_default_acl); | |
0afaa120 AM |
104 | } |
105 | EXPORT_SYMBOL(forget_all_cached_acls); | |
1da177e4 | 106 | |
2982baa2 CH |
107 | struct posix_acl *get_acl(struct inode *inode, int type) |
108 | { | |
b8a7a3a6 AG |
109 | void *sentinel; |
110 | struct posix_acl **p; | |
2982baa2 CH |
111 | struct posix_acl *acl; |
112 | ||
b8a7a3a6 AG |
113 | /* |
114 | * The sentinel is used to detect when another operation like | |
115 | * set_cached_acl() or forget_cached_acl() races with get_acl(). | |
116 | * It is guaranteed that is_uncached_acl(sentinel) is true. | |
117 | */ | |
118 | ||
2982baa2 | 119 | acl = get_cached_acl(inode, type); |
b8a7a3a6 | 120 | if (!is_uncached_acl(acl)) |
2982baa2 CH |
121 | return acl; |
122 | ||
123 | if (!IS_POSIXACL(inode)) | |
124 | return NULL; | |
125 | ||
b8a7a3a6 AG |
126 | sentinel = uncached_acl_sentinel(current); |
127 | p = acl_by_type(inode, type); | |
128 | ||
129 | /* | |
130 | * If the ACL isn't being read yet, set our sentinel. Otherwise, the | |
131 | * current value of the ACL will not be ACL_NOT_CACHED and so our own | |
132 | * sentinel will not be set; another task will update the cache. We | |
133 | * could wait for that other task to complete its job, but it's easier | |
134 | * to just call ->get_acl to fetch the ACL ourself. (This is going to | |
135 | * be an unlikely race.) | |
136 | */ | |
d1cef29a | 137 | cmpxchg(p, ACL_NOT_CACHED, sentinel); |
b8a7a3a6 | 138 | |
2982baa2 | 139 | /* |
b8a7a3a6 AG |
140 | * Normally, the ACL returned by ->get_acl will be cached. |
141 | * A filesystem can prevent that by calling | |
142 | * forget_cached_acl(inode, type) in ->get_acl. | |
2982baa2 CH |
143 | * |
144 | * If the filesystem doesn't have a get_acl() function at all, we'll | |
145 | * just create the negative cache entry. | |
146 | */ | |
147 | if (!inode->i_op->get_acl) { | |
148 | set_cached_acl(inode, type, NULL); | |
149 | return NULL; | |
150 | } | |
0cad6246 | 151 | acl = inode->i_op->get_acl(inode, type, false); |
b8a7a3a6 AG |
152 | |
153 | if (IS_ERR(acl)) { | |
154 | /* | |
155 | * Remove our sentinel so that we don't block future attempts | |
156 | * to cache the ACL. | |
157 | */ | |
158 | cmpxchg(p, sentinel, ACL_NOT_CACHED); | |
159 | return acl; | |
160 | } | |
161 | ||
162 | /* | |
163 | * Cache the result, but only if our sentinel is still in place. | |
164 | */ | |
165 | posix_acl_dup(acl); | |
166 | if (unlikely(cmpxchg(p, sentinel, acl) != sentinel)) | |
167 | posix_acl_release(acl); | |
168 | return acl; | |
2982baa2 CH |
169 | } |
170 | EXPORT_SYMBOL(get_acl); | |
171 | ||
f61f6da0 CL |
172 | /* |
173 | * Init a fresh posix_acl | |
174 | */ | |
175 | void | |
176 | posix_acl_init(struct posix_acl *acl, int count) | |
177 | { | |
66717260 | 178 | refcount_set(&acl->a_refcount, 1); |
f61f6da0 CL |
179 | acl->a_count = count; |
180 | } | |
0afaa120 | 181 | EXPORT_SYMBOL(posix_acl_init); |
f61f6da0 | 182 | |
1da177e4 LT |
183 | /* |
184 | * Allocate a new ACL with the specified number of entries. | |
185 | */ | |
186 | struct posix_acl * | |
dd0fc66f | 187 | posix_acl_alloc(int count, gfp_t flags) |
1da177e4 LT |
188 | { |
189 | const size_t size = sizeof(struct posix_acl) + | |
190 | count * sizeof(struct posix_acl_entry); | |
191 | struct posix_acl *acl = kmalloc(size, flags); | |
f61f6da0 CL |
192 | if (acl) |
193 | posix_acl_init(acl, count); | |
1da177e4 LT |
194 | return acl; |
195 | } | |
0afaa120 | 196 | EXPORT_SYMBOL(posix_acl_alloc); |
1da177e4 LT |
197 | |
198 | /* | |
199 | * Clone an ACL. | |
200 | */ | |
edde854e | 201 | static struct posix_acl * |
dd0fc66f | 202 | posix_acl_clone(const struct posix_acl *acl, gfp_t flags) |
1da177e4 LT |
203 | { |
204 | struct posix_acl *clone = NULL; | |
205 | ||
206 | if (acl) { | |
207 | int size = sizeof(struct posix_acl) + acl->a_count * | |
208 | sizeof(struct posix_acl_entry); | |
52978be6 AD |
209 | clone = kmemdup(acl, size, flags); |
210 | if (clone) | |
66717260 | 211 | refcount_set(&clone->a_refcount, 1); |
1da177e4 LT |
212 | } |
213 | return clone; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Check if an acl is valid. Returns 0 if it is, or -E... otherwise. | |
218 | */ | |
219 | int | |
0d4d717f | 220 | posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl) |
1da177e4 LT |
221 | { |
222 | const struct posix_acl_entry *pa, *pe; | |
223 | int state = ACL_USER_OBJ; | |
1da177e4 LT |
224 | int needs_mask = 0; |
225 | ||
226 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
227 | if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE)) | |
228 | return -EINVAL; | |
229 | switch (pa->e_tag) { | |
230 | case ACL_USER_OBJ: | |
231 | if (state == ACL_USER_OBJ) { | |
1da177e4 LT |
232 | state = ACL_USER; |
233 | break; | |
234 | } | |
235 | return -EINVAL; | |
236 | ||
237 | case ACL_USER: | |
238 | if (state != ACL_USER) | |
239 | return -EINVAL; | |
0d4d717f | 240 | if (!kuid_has_mapping(user_ns, pa->e_uid)) |
1da177e4 | 241 | return -EINVAL; |
1da177e4 LT |
242 | needs_mask = 1; |
243 | break; | |
244 | ||
245 | case ACL_GROUP_OBJ: | |
246 | if (state == ACL_USER) { | |
1da177e4 LT |
247 | state = ACL_GROUP; |
248 | break; | |
249 | } | |
250 | return -EINVAL; | |
251 | ||
252 | case ACL_GROUP: | |
253 | if (state != ACL_GROUP) | |
254 | return -EINVAL; | |
0d4d717f | 255 | if (!kgid_has_mapping(user_ns, pa->e_gid)) |
2f6f0654 | 256 | return -EINVAL; |
1da177e4 LT |
257 | needs_mask = 1; |
258 | break; | |
259 | ||
260 | case ACL_MASK: | |
261 | if (state != ACL_GROUP) | |
262 | return -EINVAL; | |
263 | state = ACL_OTHER; | |
264 | break; | |
265 | ||
266 | case ACL_OTHER: | |
267 | if (state == ACL_OTHER || | |
268 | (state == ACL_GROUP && !needs_mask)) { | |
269 | state = 0; | |
270 | break; | |
271 | } | |
272 | return -EINVAL; | |
273 | ||
274 | default: | |
275 | return -EINVAL; | |
276 | } | |
277 | } | |
278 | if (state == 0) | |
279 | return 0; | |
280 | return -EINVAL; | |
281 | } | |
0afaa120 | 282 | EXPORT_SYMBOL(posix_acl_valid); |
1da177e4 LT |
283 | |
284 | /* | |
285 | * Returns 0 if the acl can be exactly represented in the traditional | |
286 | * file mode permission bits, or else 1. Returns -E... on error. | |
287 | */ | |
288 | int | |
d6952123 | 289 | posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p) |
1da177e4 LT |
290 | { |
291 | const struct posix_acl_entry *pa, *pe; | |
d6952123 | 292 | umode_t mode = 0; |
1da177e4 LT |
293 | int not_equiv = 0; |
294 | ||
50c6e282 CH |
295 | /* |
296 | * A null ACL can always be presented as mode bits. | |
297 | */ | |
298 | if (!acl) | |
299 | return 0; | |
300 | ||
1da177e4 LT |
301 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
302 | switch (pa->e_tag) { | |
303 | case ACL_USER_OBJ: | |
304 | mode |= (pa->e_perm & S_IRWXO) << 6; | |
305 | break; | |
306 | case ACL_GROUP_OBJ: | |
307 | mode |= (pa->e_perm & S_IRWXO) << 3; | |
308 | break; | |
309 | case ACL_OTHER: | |
310 | mode |= pa->e_perm & S_IRWXO; | |
311 | break; | |
312 | case ACL_MASK: | |
313 | mode = (mode & ~S_IRWXG) | | |
314 | ((pa->e_perm & S_IRWXO) << 3); | |
315 | not_equiv = 1; | |
316 | break; | |
317 | case ACL_USER: | |
318 | case ACL_GROUP: | |
319 | not_equiv = 1; | |
320 | break; | |
321 | default: | |
322 | return -EINVAL; | |
323 | } | |
324 | } | |
325 | if (mode_p) | |
326 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; | |
327 | return not_equiv; | |
328 | } | |
0afaa120 | 329 | EXPORT_SYMBOL(posix_acl_equiv_mode); |
1da177e4 LT |
330 | |
331 | /* | |
332 | * Create an ACL representing the file mode permission bits of an inode. | |
333 | */ | |
334 | struct posix_acl * | |
3a5fba19 | 335 | posix_acl_from_mode(umode_t mode, gfp_t flags) |
1da177e4 LT |
336 | { |
337 | struct posix_acl *acl = posix_acl_alloc(3, flags); | |
338 | if (!acl) | |
339 | return ERR_PTR(-ENOMEM); | |
340 | ||
341 | acl->a_entries[0].e_tag = ACL_USER_OBJ; | |
1da177e4 LT |
342 | acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6; |
343 | ||
344 | acl->a_entries[1].e_tag = ACL_GROUP_OBJ; | |
1da177e4 LT |
345 | acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3; |
346 | ||
347 | acl->a_entries[2].e_tag = ACL_OTHER; | |
1da177e4 LT |
348 | acl->a_entries[2].e_perm = (mode & S_IRWXO); |
349 | return acl; | |
350 | } | |
0afaa120 | 351 | EXPORT_SYMBOL(posix_acl_from_mode); |
1da177e4 LT |
352 | |
353 | /* | |
354 | * Return 0 if current is granted want access to the inode | |
355 | * by the acl. Returns -E... otherwise. | |
356 | */ | |
357 | int | |
47291baa CB |
358 | posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode, |
359 | const struct posix_acl *acl, int want) | |
1da177e4 LT |
360 | { |
361 | const struct posix_acl_entry *pa, *pe, *mask_obj; | |
362 | int found = 0; | |
47291baa CB |
363 | kuid_t uid; |
364 | kgid_t gid; | |
1da177e4 | 365 | |
63d72b93 | 366 | want &= MAY_READ | MAY_WRITE | MAY_EXEC; |
d124b60a | 367 | |
1da177e4 LT |
368 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
369 | switch(pa->e_tag) { | |
370 | case ACL_USER_OBJ: | |
371 | /* (May have been checked already) */ | |
47291baa CB |
372 | uid = i_uid_into_mnt(mnt_userns, inode); |
373 | if (uid_eq(uid, current_fsuid())) | |
1da177e4 LT |
374 | goto check_perm; |
375 | break; | |
376 | case ACL_USER: | |
47291baa CB |
377 | uid = kuid_into_mnt(mnt_userns, pa->e_uid); |
378 | if (uid_eq(uid, current_fsuid())) | |
1da177e4 LT |
379 | goto mask; |
380 | break; | |
381 | case ACL_GROUP_OBJ: | |
47291baa CB |
382 | gid = i_gid_into_mnt(mnt_userns, inode); |
383 | if (in_group_p(gid)) { | |
1da177e4 LT |
384 | found = 1; |
385 | if ((pa->e_perm & want) == want) | |
386 | goto mask; | |
387 | } | |
388 | break; | |
389 | case ACL_GROUP: | |
47291baa CB |
390 | gid = kgid_into_mnt(mnt_userns, pa->e_gid); |
391 | if (in_group_p(gid)) { | |
1da177e4 LT |
392 | found = 1; |
393 | if ((pa->e_perm & want) == want) | |
394 | goto mask; | |
395 | } | |
396 | break; | |
397 | case ACL_MASK: | |
398 | break; | |
399 | case ACL_OTHER: | |
400 | if (found) | |
401 | return -EACCES; | |
402 | else | |
403 | goto check_perm; | |
404 | default: | |
405 | return -EIO; | |
406 | } | |
407 | } | |
408 | return -EIO; | |
409 | ||
410 | mask: | |
411 | for (mask_obj = pa+1; mask_obj != pe; mask_obj++) { | |
412 | if (mask_obj->e_tag == ACL_MASK) { | |
413 | if ((pa->e_perm & mask_obj->e_perm & want) == want) | |
414 | return 0; | |
415 | return -EACCES; | |
416 | } | |
417 | } | |
418 | ||
419 | check_perm: | |
420 | if ((pa->e_perm & want) == want) | |
421 | return 0; | |
422 | return -EACCES; | |
423 | } | |
424 | ||
425 | /* | |
426 | * Modify acl when creating a new inode. The caller must ensure the acl is | |
427 | * only referenced once. | |
428 | * | |
429 | * mode_p initially must contain the mode parameter to the open() / creat() | |
430 | * system calls. All permissions that are not granted by the acl are removed. | |
431 | * The permissions in the acl are changed to reflect the mode_p parameter. | |
432 | */ | |
d3fb6120 | 433 | static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p) |
1da177e4 LT |
434 | { |
435 | struct posix_acl_entry *pa, *pe; | |
436 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; | |
d3fb6120 | 437 | umode_t mode = *mode_p; |
1da177e4 LT |
438 | int not_equiv = 0; |
439 | ||
440 | /* assert(atomic_read(acl->a_refcount) == 1); */ | |
441 | ||
442 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
443 | switch(pa->e_tag) { | |
444 | case ACL_USER_OBJ: | |
445 | pa->e_perm &= (mode >> 6) | ~S_IRWXO; | |
446 | mode &= (pa->e_perm << 6) | ~S_IRWXU; | |
447 | break; | |
448 | ||
449 | case ACL_USER: | |
450 | case ACL_GROUP: | |
451 | not_equiv = 1; | |
452 | break; | |
453 | ||
454 | case ACL_GROUP_OBJ: | |
455 | group_obj = pa; | |
456 | break; | |
457 | ||
458 | case ACL_OTHER: | |
459 | pa->e_perm &= mode | ~S_IRWXO; | |
460 | mode &= pa->e_perm | ~S_IRWXO; | |
461 | break; | |
462 | ||
463 | case ACL_MASK: | |
464 | mask_obj = pa; | |
465 | not_equiv = 1; | |
466 | break; | |
467 | ||
468 | default: | |
469 | return -EIO; | |
470 | } | |
471 | } | |
472 | ||
473 | if (mask_obj) { | |
474 | mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO; | |
475 | mode &= (mask_obj->e_perm << 3) | ~S_IRWXG; | |
476 | } else { | |
477 | if (!group_obj) | |
478 | return -EIO; | |
479 | group_obj->e_perm &= (mode >> 3) | ~S_IRWXO; | |
480 | mode &= (group_obj->e_perm << 3) | ~S_IRWXG; | |
481 | } | |
482 | ||
483 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; | |
484 | return not_equiv; | |
485 | } | |
486 | ||
487 | /* | |
488 | * Modify the ACL for the chmod syscall. | |
489 | */ | |
5bf3258f | 490 | static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) |
1da177e4 LT |
491 | { |
492 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; | |
493 | struct posix_acl_entry *pa, *pe; | |
494 | ||
495 | /* assert(atomic_read(acl->a_refcount) == 1); */ | |
496 | ||
497 | FOREACH_ACL_ENTRY(pa, acl, pe) { | |
498 | switch(pa->e_tag) { | |
499 | case ACL_USER_OBJ: | |
500 | pa->e_perm = (mode & S_IRWXU) >> 6; | |
501 | break; | |
502 | ||
503 | case ACL_USER: | |
504 | case ACL_GROUP: | |
505 | break; | |
506 | ||
507 | case ACL_GROUP_OBJ: | |
508 | group_obj = pa; | |
509 | break; | |
510 | ||
511 | case ACL_MASK: | |
512 | mask_obj = pa; | |
513 | break; | |
514 | ||
515 | case ACL_OTHER: | |
516 | pa->e_perm = (mode & S_IRWXO); | |
517 | break; | |
518 | ||
519 | default: | |
520 | return -EIO; | |
521 | } | |
522 | } | |
523 | ||
524 | if (mask_obj) { | |
525 | mask_obj->e_perm = (mode & S_IRWXG) >> 3; | |
526 | } else { | |
527 | if (!group_obj) | |
528 | return -EIO; | |
529 | group_obj->e_perm = (mode & S_IRWXG) >> 3; | |
530 | } | |
531 | ||
532 | return 0; | |
533 | } | |
bc26ab5f | 534 | |
826cae2f | 535 | int |
37bc1539 | 536 | __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) |
826cae2f AV |
537 | { |
538 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); | |
539 | int err = -ENOMEM; | |
540 | if (clone) { | |
541 | err = posix_acl_create_masq(clone, mode_p); | |
542 | if (err < 0) { | |
543 | posix_acl_release(clone); | |
544 | clone = NULL; | |
545 | } | |
546 | } | |
547 | posix_acl_release(*acl); | |
548 | *acl = clone; | |
549 | return err; | |
550 | } | |
37bc1539 | 551 | EXPORT_SYMBOL(__posix_acl_create); |
826cae2f | 552 | |
bc26ab5f | 553 | int |
5bf3258f | 554 | __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) |
bc26ab5f AV |
555 | { |
556 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); | |
557 | int err = -ENOMEM; | |
558 | if (clone) { | |
5bf3258f | 559 | err = __posix_acl_chmod_masq(clone, mode); |
bc26ab5f AV |
560 | if (err) { |
561 | posix_acl_release(clone); | |
562 | clone = NULL; | |
563 | } | |
564 | } | |
565 | posix_acl_release(*acl); | |
566 | *acl = clone; | |
567 | return err; | |
568 | } | |
5bf3258f CH |
569 | EXPORT_SYMBOL(__posix_acl_chmod); |
570 | ||
e65ce2a5 CB |
571 | /** |
572 | * posix_acl_chmod - chmod a posix acl | |
573 | * | |
574 | * @mnt_userns: user namespace of the mount @inode was found from | |
575 | * @inode: inode to check permissions on | |
576 | * @mode: the new mode of @inode | |
577 | * | |
578 | * If the inode has been found through an idmapped mount the user namespace of | |
579 | * the vfsmount must be passed through @mnt_userns. This function will then | |
580 | * take care to map the inode according to @mnt_userns before checking | |
581 | * permissions. On non-idmapped mounts or if permission checking is to be | |
582 | * performed on the raw inode simply passs init_user_ns. | |
583 | */ | |
5bf3258f | 584 | int |
e65ce2a5 CB |
585 | posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode, |
586 | umode_t mode) | |
5bf3258f CH |
587 | { |
588 | struct posix_acl *acl; | |
589 | int ret = 0; | |
590 | ||
591 | if (!IS_POSIXACL(inode)) | |
592 | return 0; | |
593 | if (!inode->i_op->set_acl) | |
594 | return -EOPNOTSUPP; | |
595 | ||
596 | acl = get_acl(inode, ACL_TYPE_ACCESS); | |
789b663a TM |
597 | if (IS_ERR_OR_NULL(acl)) { |
598 | if (acl == ERR_PTR(-EOPNOTSUPP)) | |
599 | return 0; | |
5bf3258f | 600 | return PTR_ERR(acl); |
789b663a | 601 | } |
5bf3258f | 602 | |
37bc1539 | 603 | ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode); |
5bf3258f CH |
604 | if (ret) |
605 | return ret; | |
549c7297 | 606 | ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS); |
5bf3258f CH |
607 | posix_acl_release(acl); |
608 | return ret; | |
609 | } | |
bc26ab5f | 610 | EXPORT_SYMBOL(posix_acl_chmod); |
5c8ebd57 | 611 | |
37bc1539 CH |
612 | int |
613 | posix_acl_create(struct inode *dir, umode_t *mode, | |
614 | struct posix_acl **default_acl, struct posix_acl **acl) | |
615 | { | |
616 | struct posix_acl *p; | |
c0c3a718 | 617 | struct posix_acl *clone; |
37bc1539 CH |
618 | int ret; |
619 | ||
c0c3a718 DC |
620 | *acl = NULL; |
621 | *default_acl = NULL; | |
622 | ||
37bc1539 | 623 | if (S_ISLNK(*mode) || !IS_POSIXACL(dir)) |
c0c3a718 | 624 | return 0; |
37bc1539 CH |
625 | |
626 | p = get_acl(dir, ACL_TYPE_DEFAULT); | |
c0c3a718 DC |
627 | if (!p || p == ERR_PTR(-EOPNOTSUPP)) { |
628 | *mode &= ~current_umask(); | |
629 | return 0; | |
37bc1539 | 630 | } |
c0c3a718 DC |
631 | if (IS_ERR(p)) |
632 | return PTR_ERR(p); | |
37bc1539 | 633 | |
beaf226b | 634 | ret = -ENOMEM; |
c0c3a718 DC |
635 | clone = posix_acl_clone(p, GFP_NOFS); |
636 | if (!clone) | |
beaf226b | 637 | goto err_release; |
37bc1539 | 638 | |
c0c3a718 | 639 | ret = posix_acl_create_masq(clone, mode); |
fed0b588 | 640 | if (ret < 0) |
beaf226b | 641 | goto err_release_clone; |
37bc1539 | 642 | |
c0c3a718 DC |
643 | if (ret == 0) |
644 | posix_acl_release(clone); | |
645 | else | |
646 | *acl = clone; | |
37bc1539 | 647 | |
c0c3a718 | 648 | if (!S_ISDIR(*mode)) |
37bc1539 | 649 | posix_acl_release(p); |
c0c3a718 | 650 | else |
37bc1539 | 651 | *default_acl = p; |
37bc1539 | 652 | |
37bc1539 | 653 | return 0; |
fed0b588 | 654 | |
beaf226b | 655 | err_release_clone: |
c0c3a718 | 656 | posix_acl_release(clone); |
beaf226b | 657 | err_release: |
fed0b588 | 658 | posix_acl_release(p); |
beaf226b | 659 | return ret; |
37bc1539 CH |
660 | } |
661 | EXPORT_SYMBOL_GPL(posix_acl_create); | |
662 | ||
07393101 JK |
663 | /** |
664 | * posix_acl_update_mode - update mode in set_acl | |
e65ce2a5 CB |
665 | * @mnt_userns: user namespace of the mount @inode was found from |
666 | * @inode: target inode | |
667 | * @mode_p: mode (pointer) for update | |
668 | * @acl: acl pointer | |
07393101 JK |
669 | * |
670 | * Update the file mode when setting an ACL: compute the new file permission | |
671 | * bits based on the ACL. In addition, if the ACL is equivalent to the new | |
e39e773a | 672 | * file mode, set *@acl to NULL to indicate that no ACL should be set. |
07393101 | 673 | * |
e39e773a | 674 | * As with chmod, clear the setgid bit if the caller is not in the owning group |
07393101 JK |
675 | * or capable of CAP_FSETID (see inode_change_ok). |
676 | * | |
e65ce2a5 CB |
677 | * If the inode has been found through an idmapped mount the user namespace of |
678 | * the vfsmount must be passed through @mnt_userns. This function will then | |
679 | * take care to map the inode according to @mnt_userns before checking | |
680 | * permissions. On non-idmapped mounts or if permission checking is to be | |
681 | * performed on the raw inode simply passs init_user_ns. | |
682 | * | |
07393101 JK |
683 | * Called from set_acl inode operations. |
684 | */ | |
e65ce2a5 CB |
685 | int posix_acl_update_mode(struct user_namespace *mnt_userns, |
686 | struct inode *inode, umode_t *mode_p, | |
07393101 JK |
687 | struct posix_acl **acl) |
688 | { | |
689 | umode_t mode = inode->i_mode; | |
690 | int error; | |
691 | ||
692 | error = posix_acl_equiv_mode(*acl, &mode); | |
693 | if (error < 0) | |
694 | return error; | |
695 | if (error == 0) | |
696 | *acl = NULL; | |
e65ce2a5 CB |
697 | if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) && |
698 | !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID)) | |
07393101 JK |
699 | mode &= ~S_ISGID; |
700 | *mode_p = mode; | |
701 | return 0; | |
702 | } | |
703 | EXPORT_SYMBOL(posix_acl_update_mode); | |
704 | ||
5c8ebd57 CH |
705 | /* |
706 | * Fix up the uids and gids in posix acl extended attributes in place. | |
707 | */ | |
708 | static void posix_acl_fix_xattr_userns( | |
709 | struct user_namespace *to, struct user_namespace *from, | |
e65ce2a5 CB |
710 | struct user_namespace *mnt_userns, |
711 | void *value, size_t size, bool from_user) | |
5c8ebd57 | 712 | { |
2211d5ba AG |
713 | struct posix_acl_xattr_header *header = value; |
714 | struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end; | |
5c8ebd57 CH |
715 | int count; |
716 | kuid_t uid; | |
717 | kgid_t gid; | |
718 | ||
719 | if (!value) | |
720 | return; | |
2211d5ba | 721 | if (size < sizeof(struct posix_acl_xattr_header)) |
5c8ebd57 CH |
722 | return; |
723 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) | |
724 | return; | |
725 | ||
726 | count = posix_acl_xattr_count(size); | |
727 | if (count < 0) | |
728 | return; | |
729 | if (count == 0) | |
730 | return; | |
731 | ||
732 | for (end = entry + count; entry != end; entry++) { | |
733 | switch(le16_to_cpu(entry->e_tag)) { | |
734 | case ACL_USER: | |
735 | uid = make_kuid(from, le32_to_cpu(entry->e_id)); | |
e65ce2a5 CB |
736 | if (from_user) |
737 | uid = kuid_from_mnt(mnt_userns, uid); | |
738 | else | |
739 | uid = kuid_into_mnt(mnt_userns, uid); | |
5c8ebd57 CH |
740 | entry->e_id = cpu_to_le32(from_kuid(to, uid)); |
741 | break; | |
742 | case ACL_GROUP: | |
743 | gid = make_kgid(from, le32_to_cpu(entry->e_id)); | |
e65ce2a5 CB |
744 | if (from_user) |
745 | gid = kgid_from_mnt(mnt_userns, gid); | |
746 | else | |
747 | gid = kgid_into_mnt(mnt_userns, gid); | |
5c8ebd57 CH |
748 | entry->e_id = cpu_to_le32(from_kgid(to, gid)); |
749 | break; | |
750 | default: | |
751 | break; | |
752 | } | |
753 | } | |
754 | } | |
755 | ||
e65ce2a5 CB |
756 | void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns, |
757 | void *value, size_t size) | |
5c8ebd57 CH |
758 | { |
759 | struct user_namespace *user_ns = current_user_ns(); | |
e65ce2a5 | 760 | if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns)) |
5c8ebd57 | 761 | return; |
e65ce2a5 CB |
762 | posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value, |
763 | size, true); | |
5c8ebd57 CH |
764 | } |
765 | ||
e65ce2a5 CB |
766 | void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns, |
767 | void *value, size_t size) | |
5c8ebd57 CH |
768 | { |
769 | struct user_namespace *user_ns = current_user_ns(); | |
e65ce2a5 | 770 | if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns)) |
5c8ebd57 | 771 | return; |
e65ce2a5 CB |
772 | posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value, |
773 | size, false); | |
5c8ebd57 CH |
774 | } |
775 | ||
776 | /* | |
777 | * Convert from extended attribute to in-memory representation. | |
778 | */ | |
779 | struct posix_acl * | |
780 | posix_acl_from_xattr(struct user_namespace *user_ns, | |
781 | const void *value, size_t size) | |
782 | { | |
2211d5ba AG |
783 | const struct posix_acl_xattr_header *header = value; |
784 | const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end; | |
5c8ebd57 CH |
785 | int count; |
786 | struct posix_acl *acl; | |
787 | struct posix_acl_entry *acl_e; | |
788 | ||
789 | if (!value) | |
790 | return NULL; | |
2211d5ba | 791 | if (size < sizeof(struct posix_acl_xattr_header)) |
5c8ebd57 CH |
792 | return ERR_PTR(-EINVAL); |
793 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) | |
794 | return ERR_PTR(-EOPNOTSUPP); | |
795 | ||
796 | count = posix_acl_xattr_count(size); | |
797 | if (count < 0) | |
798 | return ERR_PTR(-EINVAL); | |
799 | if (count == 0) | |
800 | return NULL; | |
801 | ||
802 | acl = posix_acl_alloc(count, GFP_NOFS); | |
803 | if (!acl) | |
804 | return ERR_PTR(-ENOMEM); | |
805 | acl_e = acl->a_entries; | |
806 | ||
807 | for (end = entry + count; entry != end; acl_e++, entry++) { | |
808 | acl_e->e_tag = le16_to_cpu(entry->e_tag); | |
809 | acl_e->e_perm = le16_to_cpu(entry->e_perm); | |
810 | ||
811 | switch(acl_e->e_tag) { | |
812 | case ACL_USER_OBJ: | |
813 | case ACL_GROUP_OBJ: | |
814 | case ACL_MASK: | |
815 | case ACL_OTHER: | |
816 | break; | |
817 | ||
818 | case ACL_USER: | |
819 | acl_e->e_uid = | |
820 | make_kuid(user_ns, | |
821 | le32_to_cpu(entry->e_id)); | |
822 | if (!uid_valid(acl_e->e_uid)) | |
823 | goto fail; | |
824 | break; | |
825 | case ACL_GROUP: | |
826 | acl_e->e_gid = | |
827 | make_kgid(user_ns, | |
828 | le32_to_cpu(entry->e_id)); | |
829 | if (!gid_valid(acl_e->e_gid)) | |
830 | goto fail; | |
831 | break; | |
832 | ||
833 | default: | |
834 | goto fail; | |
835 | } | |
836 | } | |
837 | return acl; | |
838 | ||
839 | fail: | |
840 | posix_acl_release(acl); | |
841 | return ERR_PTR(-EINVAL); | |
842 | } | |
843 | EXPORT_SYMBOL (posix_acl_from_xattr); | |
844 | ||
845 | /* | |
846 | * Convert from in-memory to extended attribute representation. | |
847 | */ | |
848 | int | |
849 | posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, | |
850 | void *buffer, size_t size) | |
851 | { | |
2211d5ba AG |
852 | struct posix_acl_xattr_header *ext_acl = buffer; |
853 | struct posix_acl_xattr_entry *ext_entry; | |
5c8ebd57 CH |
854 | int real_size, n; |
855 | ||
856 | real_size = posix_acl_xattr_size(acl->a_count); | |
857 | if (!buffer) | |
858 | return real_size; | |
859 | if (real_size > size) | |
860 | return -ERANGE; | |
47ba9734 | 861 | |
2211d5ba | 862 | ext_entry = (void *)(ext_acl + 1); |
5c8ebd57 CH |
863 | ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
864 | ||
865 | for (n=0; n < acl->a_count; n++, ext_entry++) { | |
866 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; | |
867 | ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); | |
868 | ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); | |
869 | switch(acl_e->e_tag) { | |
870 | case ACL_USER: | |
871 | ext_entry->e_id = | |
872 | cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); | |
873 | break; | |
874 | case ACL_GROUP: | |
875 | ext_entry->e_id = | |
876 | cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); | |
877 | break; | |
878 | default: | |
879 | ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); | |
880 | break; | |
881 | } | |
882 | } | |
883 | return real_size; | |
884 | } | |
885 | EXPORT_SYMBOL (posix_acl_to_xattr); | |
2aeccbe9 CH |
886 | |
887 | static int | |
d9a82a04 | 888 | posix_acl_xattr_get(const struct xattr_handler *handler, |
b296821a AV |
889 | struct dentry *unused, struct inode *inode, |
890 | const char *name, void *value, size_t size) | |
2aeccbe9 CH |
891 | { |
892 | struct posix_acl *acl; | |
893 | int error; | |
894 | ||
b296821a | 895 | if (!IS_POSIXACL(inode)) |
2aeccbe9 | 896 | return -EOPNOTSUPP; |
b296821a | 897 | if (S_ISLNK(inode->i_mode)) |
2aeccbe9 CH |
898 | return -EOPNOTSUPP; |
899 | ||
b296821a | 900 | acl = get_acl(inode, handler->flags); |
2aeccbe9 CH |
901 | if (IS_ERR(acl)) |
902 | return PTR_ERR(acl); | |
903 | if (acl == NULL) | |
904 | return -ENODATA; | |
905 | ||
906 | error = posix_acl_to_xattr(&init_user_ns, acl, value, size); | |
907 | posix_acl_release(acl); | |
908 | ||
909 | return error; | |
910 | } | |
911 | ||
485e71e8 | 912 | int |
e65ce2a5 CB |
913 | set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode, |
914 | int type, struct posix_acl *acl) | |
2aeccbe9 | 915 | { |
2aeccbe9 CH |
916 | if (!IS_POSIXACL(inode)) |
917 | return -EOPNOTSUPP; | |
918 | if (!inode->i_op->set_acl) | |
919 | return -EOPNOTSUPP; | |
920 | ||
485e71e8 AG |
921 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) |
922 | return acl ? -EACCES : 0; | |
e65ce2a5 | 923 | if (!inode_owner_or_capable(mnt_userns, inode)) |
2aeccbe9 CH |
924 | return -EPERM; |
925 | ||
485e71e8 | 926 | if (acl) { |
a867d734 | 927 | int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl); |
485e71e8 AG |
928 | if (ret) |
929 | return ret; | |
930 | } | |
549c7297 | 931 | return inode->i_op->set_acl(mnt_userns, inode, acl, type); |
485e71e8 AG |
932 | } |
933 | EXPORT_SYMBOL(set_posix_acl); | |
934 | ||
935 | static int | |
936 | posix_acl_xattr_set(const struct xattr_handler *handler, | |
e65ce2a5 CB |
937 | struct user_namespace *mnt_userns, |
938 | struct dentry *unused, struct inode *inode, | |
939 | const char *name, const void *value, size_t size, | |
940 | int flags) | |
485e71e8 AG |
941 | { |
942 | struct posix_acl *acl = NULL; | |
943 | int ret; | |
944 | ||
2aeccbe9 CH |
945 | if (value) { |
946 | acl = posix_acl_from_xattr(&init_user_ns, value, size); | |
947 | if (IS_ERR(acl)) | |
948 | return PTR_ERR(acl); | |
2aeccbe9 | 949 | } |
e65ce2a5 | 950 | ret = set_posix_acl(mnt_userns, inode, handler->flags, acl); |
2aeccbe9 CH |
951 | posix_acl_release(acl); |
952 | return ret; | |
953 | } | |
954 | ||
764a5c6b AG |
955 | static bool |
956 | posix_acl_xattr_list(struct dentry *dentry) | |
2aeccbe9 | 957 | { |
764a5c6b | 958 | return IS_POSIXACL(d_backing_inode(dentry)); |
2aeccbe9 CH |
959 | } |
960 | ||
961 | const struct xattr_handler posix_acl_access_xattr_handler = { | |
98e9cb57 | 962 | .name = XATTR_NAME_POSIX_ACL_ACCESS, |
2aeccbe9 CH |
963 | .flags = ACL_TYPE_ACCESS, |
964 | .list = posix_acl_xattr_list, | |
965 | .get = posix_acl_xattr_get, | |
966 | .set = posix_acl_xattr_set, | |
967 | }; | |
968 | EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler); | |
969 | ||
970 | const struct xattr_handler posix_acl_default_xattr_handler = { | |
98e9cb57 | 971 | .name = XATTR_NAME_POSIX_ACL_DEFAULT, |
2aeccbe9 CH |
972 | .flags = ACL_TYPE_DEFAULT, |
973 | .list = posix_acl_xattr_list, | |
974 | .get = posix_acl_xattr_get, | |
975 | .set = posix_acl_xattr_set, | |
976 | }; | |
977 | EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler); | |
feda821e | 978 | |
549c7297 CB |
979 | int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
980 | struct posix_acl *acl, int type) | |
feda821e CH |
981 | { |
982 | int error; | |
983 | ||
984 | if (type == ACL_TYPE_ACCESS) { | |
549c7297 | 985 | error = posix_acl_update_mode(mnt_userns, inode, |
497de07d GZ |
986 | &inode->i_mode, &acl); |
987 | if (error) | |
988 | return error; | |
feda821e CH |
989 | } |
990 | ||
078cd827 | 991 | inode->i_ctime = current_time(inode); |
feda821e CH |
992 | set_cached_acl(inode, type, acl); |
993 | return 0; | |
994 | } | |
995 | ||
996 | int simple_acl_create(struct inode *dir, struct inode *inode) | |
997 | { | |
998 | struct posix_acl *default_acl, *acl; | |
999 | int error; | |
1000 | ||
1001 | error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); | |
1002 | if (error) | |
1003 | return error; | |
1004 | ||
1005 | set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl); | |
1006 | set_cached_acl(inode, ACL_TYPE_ACCESS, acl); | |
1007 | ||
1008 | if (default_acl) | |
1009 | posix_acl_release(default_acl); | |
1010 | if (acl) | |
1011 | posix_acl_release(acl); | |
1012 | return 0; | |
1013 | } |