Loading...
1/*
2 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
3 *
4 * Fixes from William Schumacher incorporated on 15 March 2001.
5 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
6 */
7
8/*
9 * This file contains generic functions for manipulating
10 * POSIX 1003.1e draft standard 17 ACLs.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/atomic.h>
16#include <linux/fs.h>
17#include <linux/sched.h>
18#include <linux/posix_acl.h>
19#include <linux/posix_acl_xattr.h>
20#include <linux/xattr.h>
21#include <linux/export.h>
22#include <linux/user_namespace.h>
23
24static struct posix_acl **acl_by_type(struct inode *inode, int type)
25{
26 switch (type) {
27 case ACL_TYPE_ACCESS:
28 return &inode->i_acl;
29 case ACL_TYPE_DEFAULT:
30 return &inode->i_default_acl;
31 default:
32 BUG();
33 }
34}
35
36struct posix_acl *get_cached_acl(struct inode *inode, int type)
37{
38 struct posix_acl **p = acl_by_type(inode, type);
39 struct posix_acl *acl;
40
41 for (;;) {
42 rcu_read_lock();
43 acl = rcu_dereference(*p);
44 if (!acl || is_uncached_acl(acl) ||
45 atomic_inc_not_zero(&acl->a_refcount))
46 break;
47 rcu_read_unlock();
48 cpu_relax();
49 }
50 rcu_read_unlock();
51 return acl;
52}
53EXPORT_SYMBOL(get_cached_acl);
54
55struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
56{
57 return rcu_dereference(*acl_by_type(inode, type));
58}
59EXPORT_SYMBOL(get_cached_acl_rcu);
60
61void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
62{
63 struct posix_acl **p = acl_by_type(inode, type);
64 struct posix_acl *old;
65
66 old = xchg(p, posix_acl_dup(acl));
67 if (!is_uncached_acl(old))
68 posix_acl_release(old);
69}
70EXPORT_SYMBOL(set_cached_acl);
71
72static void __forget_cached_acl(struct posix_acl **p)
73{
74 struct posix_acl *old;
75
76 old = xchg(p, ACL_NOT_CACHED);
77 if (!is_uncached_acl(old))
78 posix_acl_release(old);
79}
80
81void forget_cached_acl(struct inode *inode, int type)
82{
83 __forget_cached_acl(acl_by_type(inode, type));
84}
85EXPORT_SYMBOL(forget_cached_acl);
86
87void forget_all_cached_acls(struct inode *inode)
88{
89 __forget_cached_acl(&inode->i_acl);
90 __forget_cached_acl(&inode->i_default_acl);
91}
92EXPORT_SYMBOL(forget_all_cached_acls);
93
94struct posix_acl *get_acl(struct inode *inode, int type)
95{
96 void *sentinel;
97 struct posix_acl **p;
98 struct posix_acl *acl;
99
100 /*
101 * The sentinel is used to detect when another operation like
102 * set_cached_acl() or forget_cached_acl() races with get_acl().
103 * It is guaranteed that is_uncached_acl(sentinel) is true.
104 */
105
106 acl = get_cached_acl(inode, type);
107 if (!is_uncached_acl(acl))
108 return acl;
109
110 if (!IS_POSIXACL(inode))
111 return NULL;
112
113 sentinel = uncached_acl_sentinel(current);
114 p = acl_by_type(inode, type);
115
116 /*
117 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
118 * current value of the ACL will not be ACL_NOT_CACHED and so our own
119 * sentinel will not be set; another task will update the cache. We
120 * could wait for that other task to complete its job, but it's easier
121 * to just call ->get_acl to fetch the ACL ourself. (This is going to
122 * be an unlikely race.)
123 */
124 if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED)
125 /* fall through */ ;
126
127 /*
128 * Normally, the ACL returned by ->get_acl will be cached.
129 * A filesystem can prevent that by calling
130 * forget_cached_acl(inode, type) in ->get_acl.
131 *
132 * If the filesystem doesn't have a get_acl() function at all, we'll
133 * just create the negative cache entry.
134 */
135 if (!inode->i_op->get_acl) {
136 set_cached_acl(inode, type, NULL);
137 return NULL;
138 }
139 acl = inode->i_op->get_acl(inode, type);
140
141 if (IS_ERR(acl)) {
142 /*
143 * Remove our sentinel so that we don't block future attempts
144 * to cache the ACL.
145 */
146 cmpxchg(p, sentinel, ACL_NOT_CACHED);
147 return acl;
148 }
149
150 /*
151 * Cache the result, but only if our sentinel is still in place.
152 */
153 posix_acl_dup(acl);
154 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
155 posix_acl_release(acl);
156 return acl;
157}
158EXPORT_SYMBOL(get_acl);
159
160/*
161 * Init a fresh posix_acl
162 */
163void
164posix_acl_init(struct posix_acl *acl, int count)
165{
166 atomic_set(&acl->a_refcount, 1);
167 acl->a_count = count;
168}
169EXPORT_SYMBOL(posix_acl_init);
170
171/*
172 * Allocate a new ACL with the specified number of entries.
173 */
174struct posix_acl *
175posix_acl_alloc(int count, gfp_t flags)
176{
177 const size_t size = sizeof(struct posix_acl) +
178 count * sizeof(struct posix_acl_entry);
179 struct posix_acl *acl = kmalloc(size, flags);
180 if (acl)
181 posix_acl_init(acl, count);
182 return acl;
183}
184EXPORT_SYMBOL(posix_acl_alloc);
185
186/*
187 * Clone an ACL.
188 */
189static struct posix_acl *
190posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
191{
192 struct posix_acl *clone = NULL;
193
194 if (acl) {
195 int size = sizeof(struct posix_acl) + acl->a_count *
196 sizeof(struct posix_acl_entry);
197 clone = kmemdup(acl, size, flags);
198 if (clone)
199 atomic_set(&clone->a_refcount, 1);
200 }
201 return clone;
202}
203
204/*
205 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
206 */
207int
208posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
209{
210 const struct posix_acl_entry *pa, *pe;
211 int state = ACL_USER_OBJ;
212 int needs_mask = 0;
213
214 FOREACH_ACL_ENTRY(pa, acl, pe) {
215 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
216 return -EINVAL;
217 switch (pa->e_tag) {
218 case ACL_USER_OBJ:
219 if (state == ACL_USER_OBJ) {
220 state = ACL_USER;
221 break;
222 }
223 return -EINVAL;
224
225 case ACL_USER:
226 if (state != ACL_USER)
227 return -EINVAL;
228 if (!kuid_has_mapping(user_ns, pa->e_uid))
229 return -EINVAL;
230 needs_mask = 1;
231 break;
232
233 case ACL_GROUP_OBJ:
234 if (state == ACL_USER) {
235 state = ACL_GROUP;
236 break;
237 }
238 return -EINVAL;
239
240 case ACL_GROUP:
241 if (state != ACL_GROUP)
242 return -EINVAL;
243 if (!kgid_has_mapping(user_ns, pa->e_gid))
244 return -EINVAL;
245 needs_mask = 1;
246 break;
247
248 case ACL_MASK:
249 if (state != ACL_GROUP)
250 return -EINVAL;
251 state = ACL_OTHER;
252 break;
253
254 case ACL_OTHER:
255 if (state == ACL_OTHER ||
256 (state == ACL_GROUP && !needs_mask)) {
257 state = 0;
258 break;
259 }
260 return -EINVAL;
261
262 default:
263 return -EINVAL;
264 }
265 }
266 if (state == 0)
267 return 0;
268 return -EINVAL;
269}
270EXPORT_SYMBOL(posix_acl_valid);
271
272/*
273 * Returns 0 if the acl can be exactly represented in the traditional
274 * file mode permission bits, or else 1. Returns -E... on error.
275 */
276int
277posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
278{
279 const struct posix_acl_entry *pa, *pe;
280 umode_t mode = 0;
281 int not_equiv = 0;
282
283 /*
284 * A null ACL can always be presented as mode bits.
285 */
286 if (!acl)
287 return 0;
288
289 FOREACH_ACL_ENTRY(pa, acl, pe) {
290 switch (pa->e_tag) {
291 case ACL_USER_OBJ:
292 mode |= (pa->e_perm & S_IRWXO) << 6;
293 break;
294 case ACL_GROUP_OBJ:
295 mode |= (pa->e_perm & S_IRWXO) << 3;
296 break;
297 case ACL_OTHER:
298 mode |= pa->e_perm & S_IRWXO;
299 break;
300 case ACL_MASK:
301 mode = (mode & ~S_IRWXG) |
302 ((pa->e_perm & S_IRWXO) << 3);
303 not_equiv = 1;
304 break;
305 case ACL_USER:
306 case ACL_GROUP:
307 not_equiv = 1;
308 break;
309 default:
310 return -EINVAL;
311 }
312 }
313 if (mode_p)
314 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
315 return not_equiv;
316}
317EXPORT_SYMBOL(posix_acl_equiv_mode);
318
319/*
320 * Create an ACL representing the file mode permission bits of an inode.
321 */
322struct posix_acl *
323posix_acl_from_mode(umode_t mode, gfp_t flags)
324{
325 struct posix_acl *acl = posix_acl_alloc(3, flags);
326 if (!acl)
327 return ERR_PTR(-ENOMEM);
328
329 acl->a_entries[0].e_tag = ACL_USER_OBJ;
330 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
331
332 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
333 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
334
335 acl->a_entries[2].e_tag = ACL_OTHER;
336 acl->a_entries[2].e_perm = (mode & S_IRWXO);
337 return acl;
338}
339EXPORT_SYMBOL(posix_acl_from_mode);
340
341/*
342 * Return 0 if current is granted want access to the inode
343 * by the acl. Returns -E... otherwise.
344 */
345int
346posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want)
347{
348 const struct posix_acl_entry *pa, *pe, *mask_obj;
349 int found = 0;
350
351 want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK;
352
353 FOREACH_ACL_ENTRY(pa, acl, pe) {
354 switch(pa->e_tag) {
355 case ACL_USER_OBJ:
356 /* (May have been checked already) */
357 if (uid_eq(inode->i_uid, current_fsuid()))
358 goto check_perm;
359 break;
360 case ACL_USER:
361 if (uid_eq(pa->e_uid, current_fsuid()))
362 goto mask;
363 break;
364 case ACL_GROUP_OBJ:
365 if (in_group_p(inode->i_gid)) {
366 found = 1;
367 if ((pa->e_perm & want) == want)
368 goto mask;
369 }
370 break;
371 case ACL_GROUP:
372 if (in_group_p(pa->e_gid)) {
373 found = 1;
374 if ((pa->e_perm & want) == want)
375 goto mask;
376 }
377 break;
378 case ACL_MASK:
379 break;
380 case ACL_OTHER:
381 if (found)
382 return -EACCES;
383 else
384 goto check_perm;
385 default:
386 return -EIO;
387 }
388 }
389 return -EIO;
390
391mask:
392 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
393 if (mask_obj->e_tag == ACL_MASK) {
394 if ((pa->e_perm & mask_obj->e_perm & want) == want)
395 return 0;
396 return -EACCES;
397 }
398 }
399
400check_perm:
401 if ((pa->e_perm & want) == want)
402 return 0;
403 return -EACCES;
404}
405
406/*
407 * Modify acl when creating a new inode. The caller must ensure the acl is
408 * only referenced once.
409 *
410 * mode_p initially must contain the mode parameter to the open() / creat()
411 * system calls. All permissions that are not granted by the acl are removed.
412 * The permissions in the acl are changed to reflect the mode_p parameter.
413 */
414static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
415{
416 struct posix_acl_entry *pa, *pe;
417 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
418 umode_t mode = *mode_p;
419 int not_equiv = 0;
420
421 /* assert(atomic_read(acl->a_refcount) == 1); */
422
423 FOREACH_ACL_ENTRY(pa, acl, pe) {
424 switch(pa->e_tag) {
425 case ACL_USER_OBJ:
426 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
427 mode &= (pa->e_perm << 6) | ~S_IRWXU;
428 break;
429
430 case ACL_USER:
431 case ACL_GROUP:
432 not_equiv = 1;
433 break;
434
435 case ACL_GROUP_OBJ:
436 group_obj = pa;
437 break;
438
439 case ACL_OTHER:
440 pa->e_perm &= mode | ~S_IRWXO;
441 mode &= pa->e_perm | ~S_IRWXO;
442 break;
443
444 case ACL_MASK:
445 mask_obj = pa;
446 not_equiv = 1;
447 break;
448
449 default:
450 return -EIO;
451 }
452 }
453
454 if (mask_obj) {
455 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
456 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
457 } else {
458 if (!group_obj)
459 return -EIO;
460 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
461 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
462 }
463
464 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
465 return not_equiv;
466}
467
468/*
469 * Modify the ACL for the chmod syscall.
470 */
471static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
472{
473 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
474 struct posix_acl_entry *pa, *pe;
475
476 /* assert(atomic_read(acl->a_refcount) == 1); */
477
478 FOREACH_ACL_ENTRY(pa, acl, pe) {
479 switch(pa->e_tag) {
480 case ACL_USER_OBJ:
481 pa->e_perm = (mode & S_IRWXU) >> 6;
482 break;
483
484 case ACL_USER:
485 case ACL_GROUP:
486 break;
487
488 case ACL_GROUP_OBJ:
489 group_obj = pa;
490 break;
491
492 case ACL_MASK:
493 mask_obj = pa;
494 break;
495
496 case ACL_OTHER:
497 pa->e_perm = (mode & S_IRWXO);
498 break;
499
500 default:
501 return -EIO;
502 }
503 }
504
505 if (mask_obj) {
506 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
507 } else {
508 if (!group_obj)
509 return -EIO;
510 group_obj->e_perm = (mode & S_IRWXG) >> 3;
511 }
512
513 return 0;
514}
515
516int
517__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
518{
519 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
520 int err = -ENOMEM;
521 if (clone) {
522 err = posix_acl_create_masq(clone, mode_p);
523 if (err < 0) {
524 posix_acl_release(clone);
525 clone = NULL;
526 }
527 }
528 posix_acl_release(*acl);
529 *acl = clone;
530 return err;
531}
532EXPORT_SYMBOL(__posix_acl_create);
533
534int
535__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
536{
537 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
538 int err = -ENOMEM;
539 if (clone) {
540 err = __posix_acl_chmod_masq(clone, mode);
541 if (err) {
542 posix_acl_release(clone);
543 clone = NULL;
544 }
545 }
546 posix_acl_release(*acl);
547 *acl = clone;
548 return err;
549}
550EXPORT_SYMBOL(__posix_acl_chmod);
551
552int
553posix_acl_chmod(struct inode *inode, umode_t mode)
554{
555 struct posix_acl *acl;
556 int ret = 0;
557
558 if (!IS_POSIXACL(inode))
559 return 0;
560 if (!inode->i_op->set_acl)
561 return -EOPNOTSUPP;
562
563 acl = get_acl(inode, ACL_TYPE_ACCESS);
564 if (IS_ERR_OR_NULL(acl)) {
565 if (acl == ERR_PTR(-EOPNOTSUPP))
566 return 0;
567 return PTR_ERR(acl);
568 }
569
570 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
571 if (ret)
572 return ret;
573 ret = inode->i_op->set_acl(inode, acl, ACL_TYPE_ACCESS);
574 posix_acl_release(acl);
575 return ret;
576}
577EXPORT_SYMBOL(posix_acl_chmod);
578
579int
580posix_acl_create(struct inode *dir, umode_t *mode,
581 struct posix_acl **default_acl, struct posix_acl **acl)
582{
583 struct posix_acl *p;
584 struct posix_acl *clone;
585 int ret;
586
587 *acl = NULL;
588 *default_acl = NULL;
589
590 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
591 return 0;
592
593 p = get_acl(dir, ACL_TYPE_DEFAULT);
594 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
595 *mode &= ~current_umask();
596 return 0;
597 }
598 if (IS_ERR(p))
599 return PTR_ERR(p);
600
601 ret = -ENOMEM;
602 clone = posix_acl_clone(p, GFP_NOFS);
603 if (!clone)
604 goto err_release;
605
606 ret = posix_acl_create_masq(clone, mode);
607 if (ret < 0)
608 goto err_release_clone;
609
610 if (ret == 0)
611 posix_acl_release(clone);
612 else
613 *acl = clone;
614
615 if (!S_ISDIR(*mode))
616 posix_acl_release(p);
617 else
618 *default_acl = p;
619
620 return 0;
621
622err_release_clone:
623 posix_acl_release(clone);
624err_release:
625 posix_acl_release(p);
626 return ret;
627}
628EXPORT_SYMBOL_GPL(posix_acl_create);
629
630/**
631 * posix_acl_update_mode - update mode in set_acl
632 *
633 * Update the file mode when setting an ACL: compute the new file permission
634 * bits based on the ACL. In addition, if the ACL is equivalent to the new
635 * file mode, set *acl to NULL to indicate that no ACL should be set.
636 *
637 * As with chmod, clear the setgit bit if the caller is not in the owning group
638 * or capable of CAP_FSETID (see inode_change_ok).
639 *
640 * Called from set_acl inode operations.
641 */
642int posix_acl_update_mode(struct inode *inode, umode_t *mode_p,
643 struct posix_acl **acl)
644{
645 umode_t mode = inode->i_mode;
646 int error;
647
648 error = posix_acl_equiv_mode(*acl, &mode);
649 if (error < 0)
650 return error;
651 if (error == 0)
652 *acl = NULL;
653 if (!in_group_p(inode->i_gid) &&
654 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
655 mode &= ~S_ISGID;
656 *mode_p = mode;
657 return 0;
658}
659EXPORT_SYMBOL(posix_acl_update_mode);
660
661/*
662 * Fix up the uids and gids in posix acl extended attributes in place.
663 */
664static void posix_acl_fix_xattr_userns(
665 struct user_namespace *to, struct user_namespace *from,
666 void *value, size_t size)
667{
668 struct posix_acl_xattr_header *header = value;
669 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
670 int count;
671 kuid_t uid;
672 kgid_t gid;
673
674 if (!value)
675 return;
676 if (size < sizeof(struct posix_acl_xattr_header))
677 return;
678 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
679 return;
680
681 count = posix_acl_xattr_count(size);
682 if (count < 0)
683 return;
684 if (count == 0)
685 return;
686
687 for (end = entry + count; entry != end; entry++) {
688 switch(le16_to_cpu(entry->e_tag)) {
689 case ACL_USER:
690 uid = make_kuid(from, le32_to_cpu(entry->e_id));
691 entry->e_id = cpu_to_le32(from_kuid(to, uid));
692 break;
693 case ACL_GROUP:
694 gid = make_kgid(from, le32_to_cpu(entry->e_id));
695 entry->e_id = cpu_to_le32(from_kgid(to, gid));
696 break;
697 default:
698 break;
699 }
700 }
701}
702
703void posix_acl_fix_xattr_from_user(void *value, size_t size)
704{
705 struct user_namespace *user_ns = current_user_ns();
706 if (user_ns == &init_user_ns)
707 return;
708 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
709}
710
711void posix_acl_fix_xattr_to_user(void *value, size_t size)
712{
713 struct user_namespace *user_ns = current_user_ns();
714 if (user_ns == &init_user_ns)
715 return;
716 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
717}
718
719/*
720 * Convert from extended attribute to in-memory representation.
721 */
722struct posix_acl *
723posix_acl_from_xattr(struct user_namespace *user_ns,
724 const void *value, size_t size)
725{
726 const struct posix_acl_xattr_header *header = value;
727 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
728 int count;
729 struct posix_acl *acl;
730 struct posix_acl_entry *acl_e;
731
732 if (!value)
733 return NULL;
734 if (size < sizeof(struct posix_acl_xattr_header))
735 return ERR_PTR(-EINVAL);
736 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
737 return ERR_PTR(-EOPNOTSUPP);
738
739 count = posix_acl_xattr_count(size);
740 if (count < 0)
741 return ERR_PTR(-EINVAL);
742 if (count == 0)
743 return NULL;
744
745 acl = posix_acl_alloc(count, GFP_NOFS);
746 if (!acl)
747 return ERR_PTR(-ENOMEM);
748 acl_e = acl->a_entries;
749
750 for (end = entry + count; entry != end; acl_e++, entry++) {
751 acl_e->e_tag = le16_to_cpu(entry->e_tag);
752 acl_e->e_perm = le16_to_cpu(entry->e_perm);
753
754 switch(acl_e->e_tag) {
755 case ACL_USER_OBJ:
756 case ACL_GROUP_OBJ:
757 case ACL_MASK:
758 case ACL_OTHER:
759 break;
760
761 case ACL_USER:
762 acl_e->e_uid =
763 make_kuid(user_ns,
764 le32_to_cpu(entry->e_id));
765 if (!uid_valid(acl_e->e_uid))
766 goto fail;
767 break;
768 case ACL_GROUP:
769 acl_e->e_gid =
770 make_kgid(user_ns,
771 le32_to_cpu(entry->e_id));
772 if (!gid_valid(acl_e->e_gid))
773 goto fail;
774 break;
775
776 default:
777 goto fail;
778 }
779 }
780 return acl;
781
782fail:
783 posix_acl_release(acl);
784 return ERR_PTR(-EINVAL);
785}
786EXPORT_SYMBOL (posix_acl_from_xattr);
787
788/*
789 * Convert from in-memory to extended attribute representation.
790 */
791int
792posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
793 void *buffer, size_t size)
794{
795 struct posix_acl_xattr_header *ext_acl = buffer;
796 struct posix_acl_xattr_entry *ext_entry;
797 int real_size, n;
798
799 real_size = posix_acl_xattr_size(acl->a_count);
800 if (!buffer)
801 return real_size;
802 if (real_size > size)
803 return -ERANGE;
804
805 ext_entry = (void *)(ext_acl + 1);
806 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
807
808 for (n=0; n < acl->a_count; n++, ext_entry++) {
809 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
810 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
811 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
812 switch(acl_e->e_tag) {
813 case ACL_USER:
814 ext_entry->e_id =
815 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
816 break;
817 case ACL_GROUP:
818 ext_entry->e_id =
819 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
820 break;
821 default:
822 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
823 break;
824 }
825 }
826 return real_size;
827}
828EXPORT_SYMBOL (posix_acl_to_xattr);
829
830static int
831posix_acl_xattr_get(const struct xattr_handler *handler,
832 struct dentry *unused, struct inode *inode,
833 const char *name, void *value, size_t size)
834{
835 struct posix_acl *acl;
836 int error;
837
838 if (!IS_POSIXACL(inode))
839 return -EOPNOTSUPP;
840 if (S_ISLNK(inode->i_mode))
841 return -EOPNOTSUPP;
842
843 acl = get_acl(inode, handler->flags);
844 if (IS_ERR(acl))
845 return PTR_ERR(acl);
846 if (acl == NULL)
847 return -ENODATA;
848
849 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
850 posix_acl_release(acl);
851
852 return error;
853}
854
855int
856set_posix_acl(struct inode *inode, int type, struct posix_acl *acl)
857{
858 if (!IS_POSIXACL(inode))
859 return -EOPNOTSUPP;
860 if (!inode->i_op->set_acl)
861 return -EOPNOTSUPP;
862
863 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
864 return acl ? -EACCES : 0;
865 if (!inode_owner_or_capable(inode))
866 return -EPERM;
867
868 if (acl) {
869 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
870 if (ret)
871 return ret;
872 }
873 return inode->i_op->set_acl(inode, acl, type);
874}
875EXPORT_SYMBOL(set_posix_acl);
876
877static int
878posix_acl_xattr_set(const struct xattr_handler *handler,
879 struct dentry *unused, struct inode *inode,
880 const char *name, const void *value,
881 size_t size, int flags)
882{
883 struct posix_acl *acl = NULL;
884 int ret;
885
886 if (value) {
887 acl = posix_acl_from_xattr(&init_user_ns, value, size);
888 if (IS_ERR(acl))
889 return PTR_ERR(acl);
890 }
891 ret = set_posix_acl(inode, handler->flags, acl);
892 posix_acl_release(acl);
893 return ret;
894}
895
896static bool
897posix_acl_xattr_list(struct dentry *dentry)
898{
899 return IS_POSIXACL(d_backing_inode(dentry));
900}
901
902const struct xattr_handler posix_acl_access_xattr_handler = {
903 .name = XATTR_NAME_POSIX_ACL_ACCESS,
904 .flags = ACL_TYPE_ACCESS,
905 .list = posix_acl_xattr_list,
906 .get = posix_acl_xattr_get,
907 .set = posix_acl_xattr_set,
908};
909EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
910
911const struct xattr_handler posix_acl_default_xattr_handler = {
912 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
913 .flags = ACL_TYPE_DEFAULT,
914 .list = posix_acl_xattr_list,
915 .get = posix_acl_xattr_get,
916 .set = posix_acl_xattr_set,
917};
918EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
919
920int simple_set_acl(struct inode *inode, struct posix_acl *acl, int type)
921{
922 int error;
923
924 if (type == ACL_TYPE_ACCESS) {
925 error = posix_acl_update_mode(inode,
926 &inode->i_mode, &acl);
927 if (error)
928 return error;
929 }
930
931 inode->i_ctime = current_time(inode);
932 set_cached_acl(inode, type, acl);
933 return 0;
934}
935
936int simple_acl_create(struct inode *dir, struct inode *inode)
937{
938 struct posix_acl *default_acl, *acl;
939 int error;
940
941 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
942 if (error)
943 return error;
944
945 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
946 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
947
948 if (default_acl)
949 posix_acl_release(default_acl);
950 if (acl)
951 posix_acl_release(acl);
952 return 0;
953}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
4 *
5 * Fixes from William Schumacher incorporated on 15 March 2001.
6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
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>
16#include <linux/atomic.h>
17#include <linux/fs.h>
18#include <linux/sched.h>
19#include <linux/cred.h>
20#include <linux/posix_acl.h>
21#include <linux/posix_acl_xattr.h>
22#include <linux/xattr.h>
23#include <linux/export.h>
24#include <linux/user_namespace.h>
25#include <linux/namei.h>
26#include <linux/mnt_idmapping.h>
27#include <linux/iversion.h>
28#include <linux/security.h>
29#include <linux/fsnotify.h>
30#include <linux/filelock.h>
31
32#include "internal.h"
33
34static struct posix_acl **acl_by_type(struct inode *inode, int type)
35{
36 switch (type) {
37 case ACL_TYPE_ACCESS:
38 return &inode->i_acl;
39 case ACL_TYPE_DEFAULT:
40 return &inode->i_default_acl;
41 default:
42 BUG();
43 }
44}
45
46struct posix_acl *get_cached_acl(struct inode *inode, int type)
47{
48 struct posix_acl **p = acl_by_type(inode, type);
49 struct posix_acl *acl;
50
51 for (;;) {
52 rcu_read_lock();
53 acl = rcu_dereference(*p);
54 if (!acl || is_uncached_acl(acl) ||
55 refcount_inc_not_zero(&acl->a_refcount))
56 break;
57 rcu_read_unlock();
58 cpu_relax();
59 }
60 rcu_read_unlock();
61 return acl;
62}
63EXPORT_SYMBOL(get_cached_acl);
64
65struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
66{
67 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
68
69 if (acl == ACL_DONT_CACHE) {
70 struct posix_acl *ret;
71
72 ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU);
73 if (!IS_ERR(ret))
74 acl = ret;
75 }
76
77 return acl;
78}
79EXPORT_SYMBOL(get_cached_acl_rcu);
80
81void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
82{
83 struct posix_acl **p = acl_by_type(inode, type);
84 struct posix_acl *old;
85
86 old = xchg(p, posix_acl_dup(acl));
87 if (!is_uncached_acl(old))
88 posix_acl_release(old);
89}
90EXPORT_SYMBOL(set_cached_acl);
91
92static void __forget_cached_acl(struct posix_acl **p)
93{
94 struct posix_acl *old;
95
96 old = xchg(p, ACL_NOT_CACHED);
97 if (!is_uncached_acl(old))
98 posix_acl_release(old);
99}
100
101void forget_cached_acl(struct inode *inode, int type)
102{
103 __forget_cached_acl(acl_by_type(inode, type));
104}
105EXPORT_SYMBOL(forget_cached_acl);
106
107void forget_all_cached_acls(struct inode *inode)
108{
109 __forget_cached_acl(&inode->i_acl);
110 __forget_cached_acl(&inode->i_default_acl);
111}
112EXPORT_SYMBOL(forget_all_cached_acls);
113
114static struct posix_acl *__get_acl(struct mnt_idmap *idmap,
115 struct dentry *dentry, struct inode *inode,
116 int type)
117{
118 struct posix_acl *sentinel;
119 struct posix_acl **p;
120 struct posix_acl *acl;
121
122 /*
123 * The sentinel is used to detect when another operation like
124 * set_cached_acl() or forget_cached_acl() races with get_inode_acl().
125 * It is guaranteed that is_uncached_acl(sentinel) is true.
126 */
127
128 acl = get_cached_acl(inode, type);
129 if (!is_uncached_acl(acl))
130 return acl;
131
132 if (!IS_POSIXACL(inode))
133 return NULL;
134
135 sentinel = uncached_acl_sentinel(current);
136 p = acl_by_type(inode, type);
137
138 /*
139 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
140 * current value of the ACL will not be ACL_NOT_CACHED and so our own
141 * sentinel will not be set; another task will update the cache. We
142 * could wait for that other task to complete its job, but it's easier
143 * to just call ->get_inode_acl to fetch the ACL ourself. (This is
144 * going to be an unlikely race.)
145 */
146 cmpxchg(p, ACL_NOT_CACHED, sentinel);
147
148 /*
149 * Normally, the ACL returned by ->get{_inode}_acl will be cached.
150 * A filesystem can prevent that by calling
151 * forget_cached_acl(inode, type) in ->get{_inode}_acl.
152 *
153 * If the filesystem doesn't have a get{_inode}_ acl() function at all,
154 * we'll just create the negative cache entry.
155 */
156 if (dentry && inode->i_op->get_acl) {
157 acl = inode->i_op->get_acl(idmap, dentry, type);
158 } else if (inode->i_op->get_inode_acl) {
159 acl = inode->i_op->get_inode_acl(inode, type, false);
160 } else {
161 set_cached_acl(inode, type, NULL);
162 return NULL;
163 }
164 if (IS_ERR(acl)) {
165 /*
166 * Remove our sentinel so that we don't block future attempts
167 * to cache the ACL.
168 */
169 cmpxchg(p, sentinel, ACL_NOT_CACHED);
170 return acl;
171 }
172
173 /*
174 * Cache the result, but only if our sentinel is still in place.
175 */
176 posix_acl_dup(acl);
177 if (unlikely(!try_cmpxchg(p, &sentinel, acl)))
178 posix_acl_release(acl);
179 return acl;
180}
181
182struct posix_acl *get_inode_acl(struct inode *inode, int type)
183{
184 return __get_acl(&nop_mnt_idmap, NULL, inode, type);
185}
186EXPORT_SYMBOL(get_inode_acl);
187
188/*
189 * Init a fresh posix_acl
190 */
191void
192posix_acl_init(struct posix_acl *acl, int count)
193{
194 refcount_set(&acl->a_refcount, 1);
195 acl->a_count = count;
196}
197EXPORT_SYMBOL(posix_acl_init);
198
199/*
200 * Allocate a new ACL with the specified number of entries.
201 */
202struct posix_acl *
203posix_acl_alloc(unsigned int count, gfp_t flags)
204{
205 struct posix_acl *acl;
206
207 acl = kmalloc(struct_size(acl, a_entries, count), flags);
208 if (acl)
209 posix_acl_init(acl, count);
210 return acl;
211}
212EXPORT_SYMBOL(posix_acl_alloc);
213
214/*
215 * Clone an ACL.
216 */
217struct posix_acl *
218posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
219{
220 struct posix_acl *clone = NULL;
221
222 if (acl) {
223 clone = kmemdup(acl, struct_size(acl, a_entries, acl->a_count),
224 flags);
225 if (clone)
226 refcount_set(&clone->a_refcount, 1);
227 }
228 return clone;
229}
230EXPORT_SYMBOL_GPL(posix_acl_clone);
231
232/*
233 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
234 */
235int
236posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
237{
238 const struct posix_acl_entry *pa, *pe;
239 int state = ACL_USER_OBJ;
240 int needs_mask = 0;
241
242 FOREACH_ACL_ENTRY(pa, acl, pe) {
243 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
244 return -EINVAL;
245 switch (pa->e_tag) {
246 case ACL_USER_OBJ:
247 if (state == ACL_USER_OBJ) {
248 state = ACL_USER;
249 break;
250 }
251 return -EINVAL;
252
253 case ACL_USER:
254 if (state != ACL_USER)
255 return -EINVAL;
256 if (!kuid_has_mapping(user_ns, pa->e_uid))
257 return -EINVAL;
258 needs_mask = 1;
259 break;
260
261 case ACL_GROUP_OBJ:
262 if (state == ACL_USER) {
263 state = ACL_GROUP;
264 break;
265 }
266 return -EINVAL;
267
268 case ACL_GROUP:
269 if (state != ACL_GROUP)
270 return -EINVAL;
271 if (!kgid_has_mapping(user_ns, pa->e_gid))
272 return -EINVAL;
273 needs_mask = 1;
274 break;
275
276 case ACL_MASK:
277 if (state != ACL_GROUP)
278 return -EINVAL;
279 state = ACL_OTHER;
280 break;
281
282 case ACL_OTHER:
283 if (state == ACL_OTHER ||
284 (state == ACL_GROUP && !needs_mask)) {
285 state = 0;
286 break;
287 }
288 return -EINVAL;
289
290 default:
291 return -EINVAL;
292 }
293 }
294 if (state == 0)
295 return 0;
296 return -EINVAL;
297}
298EXPORT_SYMBOL(posix_acl_valid);
299
300/*
301 * Returns 0 if the acl can be exactly represented in the traditional
302 * file mode permission bits, or else 1. Returns -E... on error.
303 */
304int
305posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
306{
307 const struct posix_acl_entry *pa, *pe;
308 umode_t mode = 0;
309 int not_equiv = 0;
310
311 /*
312 * A null ACL can always be presented as mode bits.
313 */
314 if (!acl)
315 return 0;
316
317 FOREACH_ACL_ENTRY(pa, acl, pe) {
318 switch (pa->e_tag) {
319 case ACL_USER_OBJ:
320 mode |= (pa->e_perm & S_IRWXO) << 6;
321 break;
322 case ACL_GROUP_OBJ:
323 mode |= (pa->e_perm & S_IRWXO) << 3;
324 break;
325 case ACL_OTHER:
326 mode |= pa->e_perm & S_IRWXO;
327 break;
328 case ACL_MASK:
329 mode = (mode & ~S_IRWXG) |
330 ((pa->e_perm & S_IRWXO) << 3);
331 not_equiv = 1;
332 break;
333 case ACL_USER:
334 case ACL_GROUP:
335 not_equiv = 1;
336 break;
337 default:
338 return -EINVAL;
339 }
340 }
341 if (mode_p)
342 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
343 return not_equiv;
344}
345EXPORT_SYMBOL(posix_acl_equiv_mode);
346
347/*
348 * Create an ACL representing the file mode permission bits of an inode.
349 */
350struct posix_acl *
351posix_acl_from_mode(umode_t mode, gfp_t flags)
352{
353 struct posix_acl *acl = posix_acl_alloc(3, flags);
354 if (!acl)
355 return ERR_PTR(-ENOMEM);
356
357 acl->a_entries[0].e_tag = ACL_USER_OBJ;
358 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
359
360 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
361 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
362
363 acl->a_entries[2].e_tag = ACL_OTHER;
364 acl->a_entries[2].e_perm = (mode & S_IRWXO);
365 return acl;
366}
367EXPORT_SYMBOL(posix_acl_from_mode);
368
369/*
370 * Return 0 if current is granted want access to the inode
371 * by the acl. Returns -E... otherwise.
372 */
373int
374posix_acl_permission(struct mnt_idmap *idmap, struct inode *inode,
375 const struct posix_acl *acl, int want)
376{
377 const struct posix_acl_entry *pa, *pe, *mask_obj;
378 struct user_namespace *fs_userns = i_user_ns(inode);
379 int found = 0;
380 vfsuid_t vfsuid;
381 vfsgid_t vfsgid;
382
383 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
384
385 FOREACH_ACL_ENTRY(pa, acl, pe) {
386 switch(pa->e_tag) {
387 case ACL_USER_OBJ:
388 /* (May have been checked already) */
389 vfsuid = i_uid_into_vfsuid(idmap, inode);
390 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
391 goto check_perm;
392 break;
393 case ACL_USER:
394 vfsuid = make_vfsuid(idmap, fs_userns,
395 pa->e_uid);
396 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
397 goto mask;
398 break;
399 case ACL_GROUP_OBJ:
400 vfsgid = i_gid_into_vfsgid(idmap, inode);
401 if (vfsgid_in_group_p(vfsgid)) {
402 found = 1;
403 if ((pa->e_perm & want) == want)
404 goto mask;
405 }
406 break;
407 case ACL_GROUP:
408 vfsgid = make_vfsgid(idmap, fs_userns,
409 pa->e_gid);
410 if (vfsgid_in_group_p(vfsgid)) {
411 found = 1;
412 if ((pa->e_perm & want) == want)
413 goto mask;
414 }
415 break;
416 case ACL_MASK:
417 break;
418 case ACL_OTHER:
419 if (found)
420 return -EACCES;
421 else
422 goto check_perm;
423 default:
424 return -EIO;
425 }
426 }
427 return -EIO;
428
429mask:
430 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
431 if (mask_obj->e_tag == ACL_MASK) {
432 if ((pa->e_perm & mask_obj->e_perm & want) == want)
433 return 0;
434 return -EACCES;
435 }
436 }
437
438check_perm:
439 if ((pa->e_perm & want) == want)
440 return 0;
441 return -EACCES;
442}
443
444/*
445 * Modify acl when creating a new inode. The caller must ensure the acl is
446 * only referenced once.
447 *
448 * mode_p initially must contain the mode parameter to the open() / creat()
449 * system calls. All permissions that are not granted by the acl are removed.
450 * The permissions in the acl are changed to reflect the mode_p parameter.
451 */
452static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
453{
454 struct posix_acl_entry *pa, *pe;
455 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
456 umode_t mode = *mode_p;
457 int not_equiv = 0;
458
459 /* assert(atomic_read(acl->a_refcount) == 1); */
460
461 FOREACH_ACL_ENTRY(pa, acl, pe) {
462 switch(pa->e_tag) {
463 case ACL_USER_OBJ:
464 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
465 mode &= (pa->e_perm << 6) | ~S_IRWXU;
466 break;
467
468 case ACL_USER:
469 case ACL_GROUP:
470 not_equiv = 1;
471 break;
472
473 case ACL_GROUP_OBJ:
474 group_obj = pa;
475 break;
476
477 case ACL_OTHER:
478 pa->e_perm &= mode | ~S_IRWXO;
479 mode &= pa->e_perm | ~S_IRWXO;
480 break;
481
482 case ACL_MASK:
483 mask_obj = pa;
484 not_equiv = 1;
485 break;
486
487 default:
488 return -EIO;
489 }
490 }
491
492 if (mask_obj) {
493 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
494 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
495 } else {
496 if (!group_obj)
497 return -EIO;
498 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
499 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
500 }
501
502 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
503 return not_equiv;
504}
505
506/*
507 * Modify the ACL for the chmod syscall.
508 */
509static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
510{
511 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
512 struct posix_acl_entry *pa, *pe;
513
514 /* assert(atomic_read(acl->a_refcount) == 1); */
515
516 FOREACH_ACL_ENTRY(pa, acl, pe) {
517 switch(pa->e_tag) {
518 case ACL_USER_OBJ:
519 pa->e_perm = (mode & S_IRWXU) >> 6;
520 break;
521
522 case ACL_USER:
523 case ACL_GROUP:
524 break;
525
526 case ACL_GROUP_OBJ:
527 group_obj = pa;
528 break;
529
530 case ACL_MASK:
531 mask_obj = pa;
532 break;
533
534 case ACL_OTHER:
535 pa->e_perm = (mode & S_IRWXO);
536 break;
537
538 default:
539 return -EIO;
540 }
541 }
542
543 if (mask_obj) {
544 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
545 } else {
546 if (!group_obj)
547 return -EIO;
548 group_obj->e_perm = (mode & S_IRWXG) >> 3;
549 }
550
551 return 0;
552}
553
554int
555__posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
556{
557 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
558 int err = -ENOMEM;
559 if (clone) {
560 err = posix_acl_create_masq(clone, mode_p);
561 if (err < 0) {
562 posix_acl_release(clone);
563 clone = NULL;
564 }
565 }
566 posix_acl_release(*acl);
567 *acl = clone;
568 return err;
569}
570EXPORT_SYMBOL(__posix_acl_create);
571
572int
573__posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
574{
575 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
576 int err = -ENOMEM;
577 if (clone) {
578 err = __posix_acl_chmod_masq(clone, mode);
579 if (err) {
580 posix_acl_release(clone);
581 clone = NULL;
582 }
583 }
584 posix_acl_release(*acl);
585 *acl = clone;
586 return err;
587}
588EXPORT_SYMBOL(__posix_acl_chmod);
589
590/**
591 * posix_acl_chmod - chmod a posix acl
592 *
593 * @idmap: idmap of the mount @inode was found from
594 * @dentry: dentry to check permissions on
595 * @mode: the new mode of @inode
596 *
597 * If the dentry has been found through an idmapped mount the idmap of
598 * the vfsmount must be passed through @idmap. This function will then
599 * take care to map the inode according to @idmap before checking
600 * permissions. On non-idmapped mounts or if permission checking is to be
601 * performed on the raw inode simply pass @nop_mnt_idmap.
602 */
603int
604 posix_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry,
605 umode_t mode)
606{
607 struct inode *inode = d_inode(dentry);
608 struct posix_acl *acl;
609 int ret = 0;
610
611 if (!IS_POSIXACL(inode))
612 return 0;
613 if (!inode->i_op->set_acl)
614 return -EOPNOTSUPP;
615
616 acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
617 if (IS_ERR_OR_NULL(acl)) {
618 if (acl == ERR_PTR(-EOPNOTSUPP))
619 return 0;
620 return PTR_ERR(acl);
621 }
622
623 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
624 if (ret)
625 return ret;
626 ret = inode->i_op->set_acl(idmap, dentry, acl, ACL_TYPE_ACCESS);
627 posix_acl_release(acl);
628 return ret;
629}
630EXPORT_SYMBOL(posix_acl_chmod);
631
632int
633posix_acl_create(struct inode *dir, umode_t *mode,
634 struct posix_acl **default_acl, struct posix_acl **acl)
635{
636 struct posix_acl *p;
637 struct posix_acl *clone;
638 int ret;
639
640 *acl = NULL;
641 *default_acl = NULL;
642
643 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
644 return 0;
645
646 p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
647 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
648 *mode &= ~current_umask();
649 return 0;
650 }
651 if (IS_ERR(p))
652 return PTR_ERR(p);
653
654 ret = -ENOMEM;
655 clone = posix_acl_clone(p, GFP_NOFS);
656 if (!clone)
657 goto err_release;
658
659 ret = posix_acl_create_masq(clone, mode);
660 if (ret < 0)
661 goto err_release_clone;
662
663 if (ret == 0)
664 posix_acl_release(clone);
665 else
666 *acl = clone;
667
668 if (!S_ISDIR(*mode))
669 posix_acl_release(p);
670 else
671 *default_acl = p;
672
673 return 0;
674
675err_release_clone:
676 posix_acl_release(clone);
677err_release:
678 posix_acl_release(p);
679 return ret;
680}
681EXPORT_SYMBOL_GPL(posix_acl_create);
682
683/**
684 * posix_acl_update_mode - update mode in set_acl
685 * @idmap: idmap of the mount @inode was found from
686 * @inode: target inode
687 * @mode_p: mode (pointer) for update
688 * @acl: acl pointer
689 *
690 * Update the file mode when setting an ACL: compute the new file permission
691 * bits based on the ACL. In addition, if the ACL is equivalent to the new
692 * file mode, set *@acl to NULL to indicate that no ACL should be set.
693 *
694 * As with chmod, clear the setgid bit if the caller is not in the owning group
695 * or capable of CAP_FSETID (see inode_change_ok).
696 *
697 * If the inode has been found through an idmapped mount the idmap of
698 * the vfsmount must be passed through @idmap. This function will then
699 * take care to map the inode according to @idmap before checking
700 * permissions. On non-idmapped mounts or if permission checking is to be
701 * performed on the raw inode simply pass @nop_mnt_idmap.
702 *
703 * Called from set_acl inode operations.
704 */
705int posix_acl_update_mode(struct mnt_idmap *idmap,
706 struct inode *inode, umode_t *mode_p,
707 struct posix_acl **acl)
708{
709 umode_t mode = inode->i_mode;
710 int error;
711
712 error = posix_acl_equiv_mode(*acl, &mode);
713 if (error < 0)
714 return error;
715 if (error == 0)
716 *acl = NULL;
717 if (!in_group_or_capable(idmap, inode,
718 i_gid_into_vfsgid(idmap, inode)))
719 mode &= ~S_ISGID;
720 *mode_p = mode;
721 return 0;
722}
723EXPORT_SYMBOL(posix_acl_update_mode);
724
725/*
726 * Fix up the uids and gids in posix acl extended attributes in place.
727 */
728static int posix_acl_fix_xattr_common(const void *value, size_t size)
729{
730 const struct posix_acl_xattr_header *header = value;
731 int count;
732
733 if (!header)
734 return -EINVAL;
735 if (size < sizeof(struct posix_acl_xattr_header))
736 return -EINVAL;
737 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
738 return -EOPNOTSUPP;
739
740 count = posix_acl_xattr_count(size);
741 if (count < 0)
742 return -EINVAL;
743 if (count == 0)
744 return 0;
745
746 return count;
747}
748
749/**
750 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
751 * @userns: the filesystem's idmapping
752 * @value: the uapi representation of POSIX ACLs
753 * @size: the size of @void
754 *
755 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
756 * posix_acl_from_xattr() when reading them from the backing store and
757 * converting them into the struct posix_acl VFS format. The helper is
758 * specifically intended to be called from the acl inode operation.
759 *
760 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
761 * in ACL_{GROUP,USER} entries into idmapping in @userns.
762 *
763 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
764 * If it did it calling it from the get acl inode operation would return POSIX
765 * ACLs mapped according to an idmapped mount which would mean that the value
766 * couldn't be cached for the filesystem. Idmapped mounts are taken into
767 * account on the fly during permission checking or right at the VFS -
768 * userspace boundary before reporting them to the user.
769 *
770 * Return: Allocated struct posix_acl on success, NULL for a valid header but
771 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
772 */
773struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns,
774 const void *value, size_t size)
775{
776 const struct posix_acl_xattr_header *header = value;
777 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
778 int count;
779 struct posix_acl *acl;
780 struct posix_acl_entry *acl_e;
781
782 count = posix_acl_fix_xattr_common(value, size);
783 if (count < 0)
784 return ERR_PTR(count);
785 if (count == 0)
786 return NULL;
787
788 acl = posix_acl_alloc(count, GFP_NOFS);
789 if (!acl)
790 return ERR_PTR(-ENOMEM);
791 acl_e = acl->a_entries;
792
793 for (end = entry + count; entry != end; acl_e++, entry++) {
794 acl_e->e_tag = le16_to_cpu(entry->e_tag);
795 acl_e->e_perm = le16_to_cpu(entry->e_perm);
796
797 switch(acl_e->e_tag) {
798 case ACL_USER_OBJ:
799 case ACL_GROUP_OBJ:
800 case ACL_MASK:
801 case ACL_OTHER:
802 break;
803
804 case ACL_USER:
805 acl_e->e_uid = make_kuid(userns,
806 le32_to_cpu(entry->e_id));
807 if (!uid_valid(acl_e->e_uid))
808 goto fail;
809 break;
810 case ACL_GROUP:
811 acl_e->e_gid = make_kgid(userns,
812 le32_to_cpu(entry->e_id));
813 if (!gid_valid(acl_e->e_gid))
814 goto fail;
815 break;
816
817 default:
818 goto fail;
819 }
820 }
821 return acl;
822
823fail:
824 posix_acl_release(acl);
825 return ERR_PTR(-EINVAL);
826}
827EXPORT_SYMBOL (posix_acl_from_xattr);
828
829/*
830 * Convert from in-memory to extended attribute representation.
831 */
832int
833posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
834 void *buffer, size_t size)
835{
836 struct posix_acl_xattr_header *ext_acl = buffer;
837 struct posix_acl_xattr_entry *ext_entry;
838 int real_size, n;
839
840 real_size = posix_acl_xattr_size(acl->a_count);
841 if (!buffer)
842 return real_size;
843 if (real_size > size)
844 return -ERANGE;
845
846 ext_entry = (void *)(ext_acl + 1);
847 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
848
849 for (n=0; n < acl->a_count; n++, ext_entry++) {
850 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
851 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
852 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
853 switch(acl_e->e_tag) {
854 case ACL_USER:
855 ext_entry->e_id =
856 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
857 break;
858 case ACL_GROUP:
859 ext_entry->e_id =
860 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
861 break;
862 default:
863 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
864 break;
865 }
866 }
867 return real_size;
868}
869EXPORT_SYMBOL (posix_acl_to_xattr);
870
871/**
872 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
873 * @idmap: idmap of the mount
874 * @inode: inode the posix acls are set on
875 * @acl: the posix acls as represented by the vfs
876 * @buffer: the buffer into which to convert @acl
877 * @size: size of @buffer
878 *
879 * This converts @acl from the VFS representation in the filesystem idmapping
880 * to the uapi form reportable to userspace. And mount and caller idmappings
881 * are handled appropriately.
882 *
883 * Return: On success, the size of the stored uapi posix acls, on error a
884 * negative errno.
885 */
886static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap,
887 struct inode *inode,
888 const struct posix_acl *acl, void *buffer,
889 size_t size)
890
891{
892 struct posix_acl_xattr_header *ext_acl = buffer;
893 struct posix_acl_xattr_entry *ext_entry;
894 struct user_namespace *fs_userns, *caller_userns;
895 ssize_t real_size, n;
896 vfsuid_t vfsuid;
897 vfsgid_t vfsgid;
898
899 real_size = posix_acl_xattr_size(acl->a_count);
900 if (!buffer)
901 return real_size;
902 if (real_size > size)
903 return -ERANGE;
904
905 ext_entry = (void *)(ext_acl + 1);
906 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
907
908 fs_userns = i_user_ns(inode);
909 caller_userns = current_user_ns();
910 for (n=0; n < acl->a_count; n++, ext_entry++) {
911 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
912 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
913 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
914 switch(acl_e->e_tag) {
915 case ACL_USER:
916 vfsuid = make_vfsuid(idmap, fs_userns, acl_e->e_uid);
917 ext_entry->e_id = cpu_to_le32(from_kuid(
918 caller_userns, vfsuid_into_kuid(vfsuid)));
919 break;
920 case ACL_GROUP:
921 vfsgid = make_vfsgid(idmap, fs_userns, acl_e->e_gid);
922 ext_entry->e_id = cpu_to_le32(from_kgid(
923 caller_userns, vfsgid_into_kgid(vfsgid)));
924 break;
925 default:
926 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
927 break;
928 }
929 }
930 return real_size;
931}
932
933int
934set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry,
935 int type, struct posix_acl *acl)
936{
937 struct inode *inode = d_inode(dentry);
938
939 if (!IS_POSIXACL(inode))
940 return -EOPNOTSUPP;
941 if (!inode->i_op->set_acl)
942 return -EOPNOTSUPP;
943
944 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
945 return acl ? -EACCES : 0;
946 if (!inode_owner_or_capable(idmap, inode))
947 return -EPERM;
948
949 if (acl) {
950 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
951 if (ret)
952 return ret;
953 }
954 return inode->i_op->set_acl(idmap, dentry, acl, type);
955}
956EXPORT_SYMBOL(set_posix_acl);
957
958int posix_acl_listxattr(struct inode *inode, char **buffer,
959 ssize_t *remaining_size)
960{
961 int err;
962
963 if (!IS_POSIXACL(inode))
964 return 0;
965
966 if (inode->i_acl) {
967 err = xattr_list_one(buffer, remaining_size,
968 XATTR_NAME_POSIX_ACL_ACCESS);
969 if (err)
970 return err;
971 }
972
973 if (inode->i_default_acl) {
974 err = xattr_list_one(buffer, remaining_size,
975 XATTR_NAME_POSIX_ACL_DEFAULT);
976 if (err)
977 return err;
978 }
979
980 return 0;
981}
982
983static bool
984posix_acl_xattr_list(struct dentry *dentry)
985{
986 return IS_POSIXACL(d_backing_inode(dentry));
987}
988
989/*
990 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
991 *
992 * This is the legacy POSIX ACL access xattr handler. It is used by some
993 * filesystems to implement their ->listxattr() inode operation. New code
994 * should never use them.
995 */
996const struct xattr_handler nop_posix_acl_access = {
997 .name = XATTR_NAME_POSIX_ACL_ACCESS,
998 .list = posix_acl_xattr_list,
999};
1000EXPORT_SYMBOL_GPL(nop_posix_acl_access);
1001
1002/*
1003 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
1004 *
1005 * This is the legacy POSIX ACL default xattr handler. It is used by some
1006 * filesystems to implement their ->listxattr() inode operation. New code
1007 * should never use them.
1008 */
1009const struct xattr_handler nop_posix_acl_default = {
1010 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1011 .list = posix_acl_xattr_list,
1012};
1013EXPORT_SYMBOL_GPL(nop_posix_acl_default);
1014
1015int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1016 struct posix_acl *acl, int type)
1017{
1018 int error;
1019 struct inode *inode = d_inode(dentry);
1020
1021 if (type == ACL_TYPE_ACCESS) {
1022 error = posix_acl_update_mode(idmap, inode,
1023 &inode->i_mode, &acl);
1024 if (error)
1025 return error;
1026 }
1027
1028 inode_set_ctime_current(inode);
1029 if (IS_I_VERSION(inode))
1030 inode_inc_iversion(inode);
1031 set_cached_acl(inode, type, acl);
1032 return 0;
1033}
1034
1035int simple_acl_create(struct inode *dir, struct inode *inode)
1036{
1037 struct posix_acl *default_acl, *acl;
1038 int error;
1039
1040 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1041 if (error)
1042 return error;
1043
1044 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1045 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1046
1047 if (default_acl)
1048 posix_acl_release(default_acl);
1049 if (acl)
1050 posix_acl_release(acl);
1051 return 0;
1052}
1053
1054static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap,
1055 struct user_namespace *fs_userns,
1056 struct posix_acl *acl)
1057{
1058 for (int n = 0; n < acl->a_count; n++) {
1059 struct posix_acl_entry *acl_e = &acl->a_entries[n];
1060
1061 switch (acl_e->e_tag) {
1062 case ACL_USER:
1063 acl_e->e_uid = from_vfsuid(idmap, fs_userns,
1064 VFSUIDT_INIT(acl_e->e_uid));
1065 break;
1066 case ACL_GROUP:
1067 acl_e->e_gid = from_vfsgid(idmap, fs_userns,
1068 VFSGIDT_INIT(acl_e->e_gid));
1069 break;
1070 }
1071 }
1072
1073 return 0;
1074}
1075
1076/**
1077 * vfs_set_acl - set posix acls
1078 * @idmap: idmap of the mount
1079 * @dentry: the dentry based on which to set the posix acls
1080 * @acl_name: the name of the posix acl
1081 * @kacl: the posix acls in the appropriate VFS format
1082 *
1083 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1084 * afterwards.
1085 *
1086 * Return: On success 0, on error negative errno.
1087 */
1088int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1089 const char *acl_name, struct posix_acl *kacl)
1090{
1091 int acl_type;
1092 int error;
1093 struct inode *inode = d_inode(dentry);
1094 struct inode *delegated_inode = NULL;
1095
1096 acl_type = posix_acl_type(acl_name);
1097 if (acl_type < 0)
1098 return -EINVAL;
1099
1100 if (kacl) {
1101 /*
1102 * If we're on an idmapped mount translate from mount specific
1103 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1104 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1105 * if this is a filesystem with a backing store - ultimately
1106 * translate them to backing store values.
1107 */
1108 error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl);
1109 if (error)
1110 return error;
1111 }
1112
1113retry_deleg:
1114 inode_lock(inode);
1115
1116 /*
1117 * We only care about restrictions the inode struct itself places upon
1118 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1119 */
1120 error = may_write_xattr(idmap, inode);
1121 if (error)
1122 goto out_inode_unlock;
1123
1124 error = security_inode_set_acl(idmap, dentry, acl_name, kacl);
1125 if (error)
1126 goto out_inode_unlock;
1127
1128 error = try_break_deleg(inode, &delegated_inode);
1129 if (error)
1130 goto out_inode_unlock;
1131
1132 if (likely(!is_bad_inode(inode)))
1133 error = set_posix_acl(idmap, dentry, acl_type, kacl);
1134 else
1135 error = -EIO;
1136 if (!error) {
1137 fsnotify_xattr(dentry);
1138 security_inode_post_set_acl(dentry, acl_name, kacl);
1139 }
1140
1141out_inode_unlock:
1142 inode_unlock(inode);
1143
1144 if (delegated_inode) {
1145 error = break_deleg_wait(&delegated_inode);
1146 if (!error)
1147 goto retry_deleg;
1148 }
1149
1150 return error;
1151}
1152EXPORT_SYMBOL_GPL(vfs_set_acl);
1153
1154/**
1155 * vfs_get_acl - get posix acls
1156 * @idmap: idmap of the mount
1157 * @dentry: the dentry based on which to retrieve the posix acls
1158 * @acl_name: the name of the posix acl
1159 *
1160 * This function retrieves @kacl from the filesystem. The caller must all
1161 * posix_acl_release() on @kacl.
1162 *
1163 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1164 */
1165struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap,
1166 struct dentry *dentry, const char *acl_name)
1167{
1168 struct inode *inode = d_inode(dentry);
1169 struct posix_acl *acl;
1170 int acl_type, error;
1171
1172 acl_type = posix_acl_type(acl_name);
1173 if (acl_type < 0)
1174 return ERR_PTR(-EINVAL);
1175
1176 /*
1177 * The VFS has no restrictions on reading POSIX ACLs so calling
1178 * something like xattr_permission() isn't needed. Only LSMs get a say.
1179 */
1180 error = security_inode_get_acl(idmap, dentry, acl_name);
1181 if (error)
1182 return ERR_PTR(error);
1183
1184 if (!IS_POSIXACL(inode))
1185 return ERR_PTR(-EOPNOTSUPP);
1186 if (S_ISLNK(inode->i_mode))
1187 return ERR_PTR(-EOPNOTSUPP);
1188
1189 acl = __get_acl(idmap, dentry, inode, acl_type);
1190 if (IS_ERR(acl))
1191 return acl;
1192 if (!acl)
1193 return ERR_PTR(-ENODATA);
1194
1195 return acl;
1196}
1197EXPORT_SYMBOL_GPL(vfs_get_acl);
1198
1199/**
1200 * vfs_remove_acl - remove posix acls
1201 * @idmap: idmap of the mount
1202 * @dentry: the dentry based on which to retrieve the posix acls
1203 * @acl_name: the name of the posix acl
1204 *
1205 * This function removes posix acls.
1206 *
1207 * Return: On success 0, on error negative errno.
1208 */
1209int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1210 const char *acl_name)
1211{
1212 int acl_type;
1213 int error;
1214 struct inode *inode = d_inode(dentry);
1215 struct inode *delegated_inode = NULL;
1216
1217 acl_type = posix_acl_type(acl_name);
1218 if (acl_type < 0)
1219 return -EINVAL;
1220
1221retry_deleg:
1222 inode_lock(inode);
1223
1224 /*
1225 * We only care about restrictions the inode struct itself places upon
1226 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1227 */
1228 error = may_write_xattr(idmap, inode);
1229 if (error)
1230 goto out_inode_unlock;
1231
1232 error = security_inode_remove_acl(idmap, dentry, acl_name);
1233 if (error)
1234 goto out_inode_unlock;
1235
1236 error = try_break_deleg(inode, &delegated_inode);
1237 if (error)
1238 goto out_inode_unlock;
1239
1240 if (likely(!is_bad_inode(inode)))
1241 error = set_posix_acl(idmap, dentry, acl_type, NULL);
1242 else
1243 error = -EIO;
1244 if (!error) {
1245 fsnotify_xattr(dentry);
1246 security_inode_post_remove_acl(idmap, dentry, acl_name);
1247 }
1248
1249out_inode_unlock:
1250 inode_unlock(inode);
1251
1252 if (delegated_inode) {
1253 error = break_deleg_wait(&delegated_inode);
1254 if (!error)
1255 goto retry_deleg;
1256 }
1257
1258 return error;
1259}
1260EXPORT_SYMBOL_GPL(vfs_remove_acl);
1261
1262int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1263 const char *acl_name, const void *kvalue, size_t size)
1264{
1265 int error;
1266 struct posix_acl *acl = NULL;
1267
1268 if (size) {
1269 /*
1270 * Note that posix_acl_from_xattr() uses GFP_NOFS when it
1271 * probably doesn't need to here.
1272 */
1273 acl = posix_acl_from_xattr(current_user_ns(), kvalue, size);
1274 if (IS_ERR(acl))
1275 return PTR_ERR(acl);
1276 }
1277
1278 error = vfs_set_acl(idmap, dentry, acl_name, acl);
1279 posix_acl_release(acl);
1280 return error;
1281}
1282
1283ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1284 const char *acl_name, void *kvalue, size_t size)
1285{
1286 ssize_t error;
1287 struct posix_acl *acl;
1288
1289 acl = vfs_get_acl(idmap, dentry, acl_name);
1290 if (IS_ERR(acl))
1291 return PTR_ERR(acl);
1292
1293 error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry),
1294 acl, kvalue, size);
1295 posix_acl_release(acl);
1296 return error;
1297}