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