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