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