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