ksmbd: return STATUS_BAD_NETWORK_NAME error status if share is not configured
[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         int found = 0;
365         vfsuid_t vfsuid;
366         vfsgid_t vfsgid;
367
368         want &= MAY_READ | MAY_WRITE | MAY_EXEC;
369
370         FOREACH_ACL_ENTRY(pa, acl, pe) {
371                 switch(pa->e_tag) {
372                         case ACL_USER_OBJ:
373                                 /* (May have been checked already) */
374                                 vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
375                                 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
376                                         goto check_perm;
377                                 break;
378                         case ACL_USER:
379                                 vfsuid = make_vfsuid(mnt_userns, &init_user_ns,
380                                                      pa->e_uid);
381                                 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
382                                         goto mask;
383                                 break;
384                         case ACL_GROUP_OBJ:
385                                 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
386                                 if (vfsgid_in_group_p(vfsgid)) {
387                                         found = 1;
388                                         if ((pa->e_perm & want) == want)
389                                                 goto mask;
390                                 }
391                                 break;
392                         case ACL_GROUP:
393                                 vfsgid = make_vfsgid(mnt_userns, &init_user_ns,
394                                                      pa->e_gid);
395                                 if (vfsgid_in_group_p(vfsgid)) {
396                                         found = 1;
397                                         if ((pa->e_perm & want) == want)
398                                                 goto mask;
399                                 }
400                                 break;
401                         case ACL_MASK:
402                                 break;
403                         case ACL_OTHER:
404                                 if (found)
405                                         return -EACCES;
406                                 else
407                                         goto check_perm;
408                         default:
409                                 return -EIO;
410                 }
411         }
412         return -EIO;
413
414 mask:
415         for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
416                 if (mask_obj->e_tag == ACL_MASK) {
417                         if ((pa->e_perm & mask_obj->e_perm & want) == want)
418                                 return 0;
419                         return -EACCES;
420                 }
421         }
422
423 check_perm:
424         if ((pa->e_perm & want) == want)
425                 return 0;
426         return -EACCES;
427 }
428
429 /*
430  * Modify acl when creating a new inode. The caller must ensure the acl is
431  * only referenced once.
432  *
433  * mode_p initially must contain the mode parameter to the open() / creat()
434  * system calls. All permissions that are not granted by the acl are removed.
435  * The permissions in the acl are changed to reflect the mode_p parameter.
436  */
437 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
438 {
439         struct posix_acl_entry *pa, *pe;
440         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
441         umode_t mode = *mode_p;
442         int not_equiv = 0;
443
444         /* assert(atomic_read(acl->a_refcount) == 1); */
445
446         FOREACH_ACL_ENTRY(pa, acl, pe) {
447                 switch(pa->e_tag) {
448                         case ACL_USER_OBJ:
449                                 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
450                                 mode &= (pa->e_perm << 6) | ~S_IRWXU;
451                                 break;
452
453                         case ACL_USER:
454                         case ACL_GROUP:
455                                 not_equiv = 1;
456                                 break;
457
458                         case ACL_GROUP_OBJ:
459                                 group_obj = pa;
460                                 break;
461
462                         case ACL_OTHER:
463                                 pa->e_perm &= mode | ~S_IRWXO;
464                                 mode &= pa->e_perm | ~S_IRWXO;
465                                 break;
466
467                         case ACL_MASK:
468                                 mask_obj = pa;
469                                 not_equiv = 1;
470                                 break;
471
472                         default:
473                                 return -EIO;
474                 }
475         }
476
477         if (mask_obj) {
478                 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
479                 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
480         } else {
481                 if (!group_obj)
482                         return -EIO;
483                 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
484                 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
485         }
486
487         *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
488         return not_equiv;
489 }
490
491 /*
492  * Modify the ACL for the chmod syscall.
493  */
494 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
495 {
496         struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
497         struct posix_acl_entry *pa, *pe;
498
499         /* assert(atomic_read(acl->a_refcount) == 1); */
500
501         FOREACH_ACL_ENTRY(pa, acl, pe) {
502                 switch(pa->e_tag) {
503                         case ACL_USER_OBJ:
504                                 pa->e_perm = (mode & S_IRWXU) >> 6;
505                                 break;
506
507                         case ACL_USER:
508                         case ACL_GROUP:
509                                 break;
510
511                         case ACL_GROUP_OBJ:
512                                 group_obj = pa;
513                                 break;
514
515                         case ACL_MASK:
516                                 mask_obj = pa;
517                                 break;
518
519                         case ACL_OTHER:
520                                 pa->e_perm = (mode & S_IRWXO);
521                                 break;
522
523                         default:
524                                 return -EIO;
525                 }
526         }
527
528         if (mask_obj) {
529                 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
530         } else {
531                 if (!group_obj)
532                         return -EIO;
533                 group_obj->e_perm = (mode & S_IRWXG) >> 3;
534         }
535
536         return 0;
537 }
538
539 int
540 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
541 {
542         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
543         int err = -ENOMEM;
544         if (clone) {
545                 err = posix_acl_create_masq(clone, mode_p);
546                 if (err < 0) {
547                         posix_acl_release(clone);
548                         clone = NULL;
549                 }
550         }
551         posix_acl_release(*acl);
552         *acl = clone;
553         return err;
554 }
555 EXPORT_SYMBOL(__posix_acl_create);
556
557 int
558 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
559 {
560         struct posix_acl *clone = posix_acl_clone(*acl, gfp);
561         int err = -ENOMEM;
562         if (clone) {
563                 err = __posix_acl_chmod_masq(clone, mode);
564                 if (err) {
565                         posix_acl_release(clone);
566                         clone = NULL;
567                 }
568         }
569         posix_acl_release(*acl);
570         *acl = clone;
571         return err;
572 }
573 EXPORT_SYMBOL(__posix_acl_chmod);
574
575 /**
576  * posix_acl_chmod - chmod a posix acl
577  *
578  * @mnt_userns: user namespace of the mount @inode was found from
579  * @inode:      inode to check permissions on
580  * @mode:       the new mode of @inode
581  *
582  * If the inode has been found through an idmapped mount the user namespace of
583  * the vfsmount must be passed through @mnt_userns. This function will then
584  * take care to map the inode according to @mnt_userns before checking
585  * permissions. On non-idmapped mounts or if permission checking is to be
586  * performed on the raw inode simply passs init_user_ns.
587  */
588 int
589  posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
590                     umode_t mode)
591 {
592         struct posix_acl *acl;
593         int ret = 0;
594
595         if (!IS_POSIXACL(inode))
596                 return 0;
597         if (!inode->i_op->set_acl)
598                 return -EOPNOTSUPP;
599
600         acl = get_acl(inode, ACL_TYPE_ACCESS);
601         if (IS_ERR_OR_NULL(acl)) {
602                 if (acl == ERR_PTR(-EOPNOTSUPP))
603                         return 0;
604                 return PTR_ERR(acl);
605         }
606
607         ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
608         if (ret)
609                 return ret;
610         ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
611         posix_acl_release(acl);
612         return ret;
613 }
614 EXPORT_SYMBOL(posix_acl_chmod);
615
616 int
617 posix_acl_create(struct inode *dir, umode_t *mode,
618                 struct posix_acl **default_acl, struct posix_acl **acl)
619 {
620         struct posix_acl *p;
621         struct posix_acl *clone;
622         int ret;
623
624         *acl = NULL;
625         *default_acl = NULL;
626
627         if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
628                 return 0;
629
630         p = get_acl(dir, ACL_TYPE_DEFAULT);
631         if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
632                 *mode &= ~current_umask();
633                 return 0;
634         }
635         if (IS_ERR(p))
636                 return PTR_ERR(p);
637
638         ret = -ENOMEM;
639         clone = posix_acl_clone(p, GFP_NOFS);
640         if (!clone)
641                 goto err_release;
642
643         ret = posix_acl_create_masq(clone, mode);
644         if (ret < 0)
645                 goto err_release_clone;
646
647         if (ret == 0)
648                 posix_acl_release(clone);
649         else
650                 *acl = clone;
651
652         if (!S_ISDIR(*mode))
653                 posix_acl_release(p);
654         else
655                 *default_acl = p;
656
657         return 0;
658
659 err_release_clone:
660         posix_acl_release(clone);
661 err_release:
662         posix_acl_release(p);
663         return ret;
664 }
665 EXPORT_SYMBOL_GPL(posix_acl_create);
666
667 /**
668  * posix_acl_update_mode  -  update mode in set_acl
669  * @mnt_userns: user namespace of the mount @inode was found from
670  * @inode:      target inode
671  * @mode_p:     mode (pointer) for update
672  * @acl:        acl pointer
673  *
674  * Update the file mode when setting an ACL: compute the new file permission
675  * bits based on the ACL.  In addition, if the ACL is equivalent to the new
676  * file mode, set *@acl to NULL to indicate that no ACL should be set.
677  *
678  * As with chmod, clear the setgid bit if the caller is not in the owning group
679  * or capable of CAP_FSETID (see inode_change_ok).
680  *
681  * If the inode has been found through an idmapped mount the user namespace of
682  * the vfsmount must be passed through @mnt_userns. This function will then
683  * take care to map the inode according to @mnt_userns before checking
684  * permissions. On non-idmapped mounts or if permission checking is to be
685  * performed on the raw inode simply passs init_user_ns.
686  *
687  * Called from set_acl inode operations.
688  */
689 int posix_acl_update_mode(struct user_namespace *mnt_userns,
690                           struct inode *inode, umode_t *mode_p,
691                           struct posix_acl **acl)
692 {
693         umode_t mode = inode->i_mode;
694         int error;
695
696         error = posix_acl_equiv_mode(*acl, &mode);
697         if (error < 0)
698                 return error;
699         if (error == 0)
700                 *acl = NULL;
701         if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
702             !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
703                 mode &= ~S_ISGID;
704         *mode_p = mode;
705         return 0;
706 }
707 EXPORT_SYMBOL(posix_acl_update_mode);
708
709 /*
710  * Fix up the uids and gids in posix acl extended attributes in place.
711  */
712 static int posix_acl_fix_xattr_common(void *value, size_t size)
713 {
714         struct posix_acl_xattr_header *header = value;
715         int count;
716
717         if (!header)
718                 return -EINVAL;
719         if (size < sizeof(struct posix_acl_xattr_header))
720                 return -EINVAL;
721         if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
722                 return -EINVAL;
723
724         count = posix_acl_xattr_count(size);
725         if (count < 0)
726                 return -EINVAL;
727         if (count == 0)
728                 return -EINVAL;
729
730         return count;
731 }
732
733 void posix_acl_getxattr_idmapped_mnt(struct user_namespace *mnt_userns,
734                                      const struct inode *inode,
735                                      void *value, size_t size)
736 {
737         struct posix_acl_xattr_header *header = value;
738         struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
739         int count;
740         vfsuid_t vfsuid;
741         vfsgid_t vfsgid;
742         kuid_t uid;
743         kgid_t gid;
744
745         if (no_idmapping(mnt_userns, i_user_ns(inode)))
746                 return;
747
748         count = posix_acl_fix_xattr_common(value, size);
749         if (count < 0)
750                 return;
751
752         for (end = entry + count; entry != end; entry++) {
753                 switch (le16_to_cpu(entry->e_tag)) {
754                 case ACL_USER:
755                         uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
756                         vfsuid = make_vfsuid(mnt_userns, &init_user_ns, uid);
757                         entry->e_id = cpu_to_le32(from_kuid(&init_user_ns,
758                                                 vfsuid_into_kuid(vfsuid)));
759                         break;
760                 case ACL_GROUP:
761                         gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
762                         vfsgid = make_vfsgid(mnt_userns, &init_user_ns, gid);
763                         entry->e_id = cpu_to_le32(from_kgid(&init_user_ns,
764                                                 vfsgid_into_kgid(vfsgid)));
765                         break;
766                 default:
767                         break;
768                 }
769         }
770 }
771
772 void posix_acl_setxattr_idmapped_mnt(struct user_namespace *mnt_userns,
773                                      const struct inode *inode,
774                                      void *value, size_t size)
775 {
776         struct posix_acl_xattr_header *header = value;
777         struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
778         int count;
779         vfsuid_t vfsuid;
780         vfsgid_t vfsgid;
781         kuid_t uid;
782         kgid_t gid;
783
784         if (no_idmapping(mnt_userns, i_user_ns(inode)))
785                 return;
786
787         count = posix_acl_fix_xattr_common(value, size);
788         if (count < 0)
789                 return;
790
791         for (end = entry + count; entry != end; entry++) {
792                 switch (le16_to_cpu(entry->e_tag)) {
793                 case ACL_USER:
794                         uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
795                         vfsuid = VFSUIDT_INIT(uid);
796                         uid = from_vfsuid(mnt_userns, &init_user_ns, vfsuid);
797                         entry->e_id = cpu_to_le32(from_kuid(&init_user_ns, uid));
798                         break;
799                 case ACL_GROUP:
800                         gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
801                         vfsgid = VFSGIDT_INIT(gid);
802                         gid = from_vfsgid(mnt_userns, &init_user_ns, vfsgid);
803                         entry->e_id = cpu_to_le32(from_kgid(&init_user_ns, gid));
804                         break;
805                 default:
806                         break;
807                 }
808         }
809 }
810
811 static void posix_acl_fix_xattr_userns(
812         struct user_namespace *to, struct user_namespace *from,
813         void *value, size_t size)
814 {
815         struct posix_acl_xattr_header *header = value;
816         struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
817         int count;
818         kuid_t uid;
819         kgid_t gid;
820
821         count = posix_acl_fix_xattr_common(value, size);
822         if (count < 0)
823                 return;
824
825         for (end = entry + count; entry != end; entry++) {
826                 switch(le16_to_cpu(entry->e_tag)) {
827                 case ACL_USER:
828                         uid = make_kuid(from, le32_to_cpu(entry->e_id));
829                         entry->e_id = cpu_to_le32(from_kuid(to, uid));
830                         break;
831                 case ACL_GROUP:
832                         gid = make_kgid(from, le32_to_cpu(entry->e_id));
833                         entry->e_id = cpu_to_le32(from_kgid(to, gid));
834                         break;
835                 default:
836                         break;
837                 }
838         }
839 }
840
841 void posix_acl_fix_xattr_from_user(void *value, size_t size)
842 {
843         struct user_namespace *user_ns = current_user_ns();
844         if (user_ns == &init_user_ns)
845                 return;
846         posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
847 }
848
849 void posix_acl_fix_xattr_to_user(void *value, size_t size)
850 {
851         struct user_namespace *user_ns = current_user_ns();
852         if (user_ns == &init_user_ns)
853                 return;
854         posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
855 }
856
857 /*
858  * Convert from extended attribute to in-memory representation.
859  */
860 struct posix_acl *
861 posix_acl_from_xattr(struct user_namespace *user_ns,
862                      const void *value, size_t size)
863 {
864         const struct posix_acl_xattr_header *header = value;
865         const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
866         int count;
867         struct posix_acl *acl;
868         struct posix_acl_entry *acl_e;
869
870         if (!value)
871                 return NULL;
872         if (size < sizeof(struct posix_acl_xattr_header))
873                  return ERR_PTR(-EINVAL);
874         if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
875                 return ERR_PTR(-EOPNOTSUPP);
876
877         count = posix_acl_xattr_count(size);
878         if (count < 0)
879                 return ERR_PTR(-EINVAL);
880         if (count == 0)
881                 return NULL;
882         
883         acl = posix_acl_alloc(count, GFP_NOFS);
884         if (!acl)
885                 return ERR_PTR(-ENOMEM);
886         acl_e = acl->a_entries;
887         
888         for (end = entry + count; entry != end; acl_e++, entry++) {
889                 acl_e->e_tag  = le16_to_cpu(entry->e_tag);
890                 acl_e->e_perm = le16_to_cpu(entry->e_perm);
891
892                 switch(acl_e->e_tag) {
893                         case ACL_USER_OBJ:
894                         case ACL_GROUP_OBJ:
895                         case ACL_MASK:
896                         case ACL_OTHER:
897                                 break;
898
899                         case ACL_USER:
900                                 acl_e->e_uid =
901                                         make_kuid(user_ns,
902                                                   le32_to_cpu(entry->e_id));
903                                 if (!uid_valid(acl_e->e_uid))
904                                         goto fail;
905                                 break;
906                         case ACL_GROUP:
907                                 acl_e->e_gid =
908                                         make_kgid(user_ns,
909                                                   le32_to_cpu(entry->e_id));
910                                 if (!gid_valid(acl_e->e_gid))
911                                         goto fail;
912                                 break;
913
914                         default:
915                                 goto fail;
916                 }
917         }
918         return acl;
919
920 fail:
921         posix_acl_release(acl);
922         return ERR_PTR(-EINVAL);
923 }
924 EXPORT_SYMBOL (posix_acl_from_xattr);
925
926 /*
927  * Convert from in-memory to extended attribute representation.
928  */
929 int
930 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
931                    void *buffer, size_t size)
932 {
933         struct posix_acl_xattr_header *ext_acl = buffer;
934         struct posix_acl_xattr_entry *ext_entry;
935         int real_size, n;
936
937         real_size = posix_acl_xattr_size(acl->a_count);
938         if (!buffer)
939                 return real_size;
940         if (real_size > size)
941                 return -ERANGE;
942
943         ext_entry = (void *)(ext_acl + 1);
944         ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
945
946         for (n=0; n < acl->a_count; n++, ext_entry++) {
947                 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
948                 ext_entry->e_tag  = cpu_to_le16(acl_e->e_tag);
949                 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
950                 switch(acl_e->e_tag) {
951                 case ACL_USER:
952                         ext_entry->e_id =
953                                 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
954                         break;
955                 case ACL_GROUP:
956                         ext_entry->e_id =
957                                 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
958                         break;
959                 default:
960                         ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
961                         break;
962                 }
963         }
964         return real_size;
965 }
966 EXPORT_SYMBOL (posix_acl_to_xattr);
967
968 static int
969 posix_acl_xattr_get(const struct xattr_handler *handler,
970                     struct dentry *unused, struct inode *inode,
971                     const char *name, void *value, size_t size)
972 {
973         struct posix_acl *acl;
974         int error;
975
976         if (!IS_POSIXACL(inode))
977                 return -EOPNOTSUPP;
978         if (S_ISLNK(inode->i_mode))
979                 return -EOPNOTSUPP;
980
981         acl = get_acl(inode, handler->flags);
982         if (IS_ERR(acl))
983                 return PTR_ERR(acl);
984         if (acl == NULL)
985                 return -ENODATA;
986
987         error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
988         posix_acl_release(acl);
989
990         return error;
991 }
992
993 int
994 set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
995               int type, struct posix_acl *acl)
996 {
997         if (!IS_POSIXACL(inode))
998                 return -EOPNOTSUPP;
999         if (!inode->i_op->set_acl)
1000                 return -EOPNOTSUPP;
1001
1002         if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
1003                 return acl ? -EACCES : 0;
1004         if (!inode_owner_or_capable(mnt_userns, inode))
1005                 return -EPERM;
1006
1007         if (acl) {
1008                 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
1009                 if (ret)
1010                         return ret;
1011         }
1012         return inode->i_op->set_acl(mnt_userns, inode, acl, type);
1013 }
1014 EXPORT_SYMBOL(set_posix_acl);
1015
1016 static int
1017 posix_acl_xattr_set(const struct xattr_handler *handler,
1018                            struct user_namespace *mnt_userns,
1019                            struct dentry *unused, struct inode *inode,
1020                            const char *name, const void *value, size_t size,
1021                            int flags)
1022 {
1023         struct posix_acl *acl = NULL;
1024         int ret;
1025
1026         if (value) {
1027                 acl = posix_acl_from_xattr(&init_user_ns, value, size);
1028                 if (IS_ERR(acl))
1029                         return PTR_ERR(acl);
1030         }
1031         ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
1032         posix_acl_release(acl);
1033         return ret;
1034 }
1035
1036 static bool
1037 posix_acl_xattr_list(struct dentry *dentry)
1038 {
1039         return IS_POSIXACL(d_backing_inode(dentry));
1040 }
1041
1042 const struct xattr_handler posix_acl_access_xattr_handler = {
1043         .name = XATTR_NAME_POSIX_ACL_ACCESS,
1044         .flags = ACL_TYPE_ACCESS,
1045         .list = posix_acl_xattr_list,
1046         .get = posix_acl_xattr_get,
1047         .set = posix_acl_xattr_set,
1048 };
1049 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
1050
1051 const struct xattr_handler posix_acl_default_xattr_handler = {
1052         .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1053         .flags = ACL_TYPE_DEFAULT,
1054         .list = posix_acl_xattr_list,
1055         .get = posix_acl_xattr_get,
1056         .set = posix_acl_xattr_set,
1057 };
1058 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
1059
1060 int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1061                    struct posix_acl *acl, int type)
1062 {
1063         int error;
1064
1065         if (type == ACL_TYPE_ACCESS) {
1066                 error = posix_acl_update_mode(mnt_userns, inode,
1067                                 &inode->i_mode, &acl);
1068                 if (error)
1069                         return error;
1070         }
1071
1072         inode->i_ctime = current_time(inode);
1073         set_cached_acl(inode, type, acl);
1074         return 0;
1075 }
1076
1077 int simple_acl_create(struct inode *dir, struct inode *inode)
1078 {
1079         struct posix_acl *default_acl, *acl;
1080         int error;
1081
1082         error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1083         if (error)
1084                 return error;
1085
1086         set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1087         set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1088
1089         if (default_acl)
1090                 posix_acl_release(default_acl);
1091         if (acl)
1092                 posix_acl_release(acl);
1093         return 0;
1094 }