1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org>
5 * Fixes from William Schumacher incorporated on 15 March 2001.
6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>).
10 * This file contains generic functions for manipulating
11 * POSIX 1003.1e draft standard 17 ACLs.
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/atomic.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>
27 static struct posix_acl **acl_by_type(struct inode *inode, int type)
32 case ACL_TYPE_DEFAULT:
33 return &inode->i_default_acl;
39 struct posix_acl *get_cached_acl(struct inode *inode, int type)
41 struct posix_acl **p = acl_by_type(inode, type);
42 struct posix_acl *acl;
46 acl = rcu_dereference(*p);
47 if (!acl || is_uncached_acl(acl) ||
48 refcount_inc_not_zero(&acl->a_refcount))
56 EXPORT_SYMBOL(get_cached_acl);
58 struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
60 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
62 if (acl == ACL_DONT_CACHE) {
63 struct posix_acl *ret;
65 ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
72 EXPORT_SYMBOL(get_cached_acl_rcu);
74 void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
76 struct posix_acl **p = acl_by_type(inode, type);
77 struct posix_acl *old;
79 old = xchg(p, posix_acl_dup(acl));
80 if (!is_uncached_acl(old))
81 posix_acl_release(old);
83 EXPORT_SYMBOL(set_cached_acl);
85 static void __forget_cached_acl(struct posix_acl **p)
87 struct posix_acl *old;
89 old = xchg(p, ACL_NOT_CACHED);
90 if (!is_uncached_acl(old))
91 posix_acl_release(old);
94 void forget_cached_acl(struct inode *inode, int type)
96 __forget_cached_acl(acl_by_type(inode, type));
98 EXPORT_SYMBOL(forget_cached_acl);
100 void forget_all_cached_acls(struct inode *inode)
102 __forget_cached_acl(&inode->i_acl);
103 __forget_cached_acl(&inode->i_default_acl);
105 EXPORT_SYMBOL(forget_all_cached_acls);
107 struct posix_acl *get_acl(struct inode *inode, int type)
110 struct posix_acl **p;
111 struct posix_acl *acl;
114 * The sentinel is used to detect when another operation like
115 * set_cached_acl() or forget_cached_acl() races with get_acl().
116 * It is guaranteed that is_uncached_acl(sentinel) is true.
119 acl = get_cached_acl(inode, type);
120 if (!is_uncached_acl(acl))
123 if (!IS_POSIXACL(inode))
126 sentinel = uncached_acl_sentinel(current);
127 p = acl_by_type(inode, type);
130 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
131 * current value of the ACL will not be ACL_NOT_CACHED and so our own
132 * sentinel will not be set; another task will update the cache. We
133 * could wait for that other task to complete its job, but it's easier
134 * to just call ->get_acl to fetch the ACL ourself. (This is going to
135 * be an unlikely race.)
137 if (cmpxchg(p, ACL_NOT_CACHED, sentinel) != ACL_NOT_CACHED)
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.
145 * If the filesystem doesn't have a get_acl() function at all, we'll
146 * just create the negative cache entry.
148 if (!inode->i_op->get_acl) {
149 set_cached_acl(inode, type, NULL);
152 acl = inode->i_op->get_acl(inode, type, false);
156 * Remove our sentinel so that we don't block future attempts
159 cmpxchg(p, sentinel, ACL_NOT_CACHED);
164 * Cache the result, but only if our sentinel is still in place.
167 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
168 posix_acl_release(acl);
171 EXPORT_SYMBOL(get_acl);
174 * Init a fresh posix_acl
177 posix_acl_init(struct posix_acl *acl, int count)
179 refcount_set(&acl->a_refcount, 1);
180 acl->a_count = count;
182 EXPORT_SYMBOL(posix_acl_init);
185 * Allocate a new ACL with the specified number of entries.
188 posix_acl_alloc(int count, gfp_t flags)
190 const size_t size = sizeof(struct posix_acl) +
191 count * sizeof(struct posix_acl_entry);
192 struct posix_acl *acl = kmalloc(size, flags);
194 posix_acl_init(acl, count);
197 EXPORT_SYMBOL(posix_acl_alloc);
202 static struct posix_acl *
203 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
205 struct posix_acl *clone = NULL;
208 int size = sizeof(struct posix_acl) + acl->a_count *
209 sizeof(struct posix_acl_entry);
210 clone = kmemdup(acl, size, flags);
212 refcount_set(&clone->a_refcount, 1);
218 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
221 posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
223 const struct posix_acl_entry *pa, *pe;
224 int state = ACL_USER_OBJ;
227 FOREACH_ACL_ENTRY(pa, acl, pe) {
228 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
232 if (state == ACL_USER_OBJ) {
239 if (state != ACL_USER)
241 if (!kuid_has_mapping(user_ns, pa->e_uid))
247 if (state == ACL_USER) {
254 if (state != ACL_GROUP)
256 if (!kgid_has_mapping(user_ns, pa->e_gid))
262 if (state != ACL_GROUP)
268 if (state == ACL_OTHER ||
269 (state == ACL_GROUP && !needs_mask)) {
283 EXPORT_SYMBOL(posix_acl_valid);
286 * Returns 0 if the acl can be exactly represented in the traditional
287 * file mode permission bits, or else 1. Returns -E... on error.
290 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
292 const struct posix_acl_entry *pa, *pe;
297 * A null ACL can always be presented as mode bits.
302 FOREACH_ACL_ENTRY(pa, acl, pe) {
305 mode |= (pa->e_perm & S_IRWXO) << 6;
308 mode |= (pa->e_perm & S_IRWXO) << 3;
311 mode |= pa->e_perm & S_IRWXO;
314 mode = (mode & ~S_IRWXG) |
315 ((pa->e_perm & S_IRWXO) << 3);
327 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
330 EXPORT_SYMBOL(posix_acl_equiv_mode);
333 * Create an ACL representing the file mode permission bits of an inode.
336 posix_acl_from_mode(umode_t mode, gfp_t flags)
338 struct posix_acl *acl = posix_acl_alloc(3, flags);
340 return ERR_PTR(-ENOMEM);
342 acl->a_entries[0].e_tag = ACL_USER_OBJ;
343 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
345 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
346 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
348 acl->a_entries[2].e_tag = ACL_OTHER;
349 acl->a_entries[2].e_perm = (mode & S_IRWXO);
352 EXPORT_SYMBOL(posix_acl_from_mode);
355 * Return 0 if current is granted want access to the inode
356 * by the acl. Returns -E... otherwise.
359 posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
360 const struct posix_acl *acl, int want)
362 const struct posix_acl_entry *pa, *pe, *mask_obj;
367 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
369 FOREACH_ACL_ENTRY(pa, acl, pe) {
372 /* (May have been checked already) */
373 uid = i_uid_into_mnt(mnt_userns, inode);
374 if (uid_eq(uid, current_fsuid()))
378 uid = kuid_into_mnt(mnt_userns, pa->e_uid);
379 if (uid_eq(uid, current_fsuid()))
383 gid = i_gid_into_mnt(mnt_userns, inode);
384 if (in_group_p(gid)) {
386 if ((pa->e_perm & want) == want)
391 gid = kgid_into_mnt(mnt_userns, pa->e_gid);
392 if (in_group_p(gid)) {
394 if ((pa->e_perm & want) == want)
412 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
413 if (mask_obj->e_tag == ACL_MASK) {
414 if ((pa->e_perm & mask_obj->e_perm & want) == want)
421 if ((pa->e_perm & want) == want)
427 * Modify acl when creating a new inode. The caller must ensure the acl is
428 * only referenced once.
430 * mode_p initially must contain the mode parameter to the open() / creat()
431 * system calls. All permissions that are not granted by the acl are removed.
432 * The permissions in the acl are changed to reflect the mode_p parameter.
434 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
436 struct posix_acl_entry *pa, *pe;
437 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
438 umode_t mode = *mode_p;
441 /* assert(atomic_read(acl->a_refcount) == 1); */
443 FOREACH_ACL_ENTRY(pa, acl, pe) {
446 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
447 mode &= (pa->e_perm << 6) | ~S_IRWXU;
460 pa->e_perm &= mode | ~S_IRWXO;
461 mode &= pa->e_perm | ~S_IRWXO;
475 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
476 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
480 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
481 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
484 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
489 * Modify the ACL for the chmod syscall.
491 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
493 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
494 struct posix_acl_entry *pa, *pe;
496 /* assert(atomic_read(acl->a_refcount) == 1); */
498 FOREACH_ACL_ENTRY(pa, acl, pe) {
501 pa->e_perm = (mode & S_IRWXU) >> 6;
517 pa->e_perm = (mode & S_IRWXO);
526 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
530 group_obj->e_perm = (mode & S_IRWXG) >> 3;
537 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
539 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
542 err = posix_acl_create_masq(clone, mode_p);
544 posix_acl_release(clone);
548 posix_acl_release(*acl);
552 EXPORT_SYMBOL(__posix_acl_create);
555 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
557 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
560 err = __posix_acl_chmod_masq(clone, mode);
562 posix_acl_release(clone);
566 posix_acl_release(*acl);
570 EXPORT_SYMBOL(__posix_acl_chmod);
573 * posix_acl_chmod - chmod a posix acl
575 * @mnt_userns: user namespace of the mount @inode was found from
576 * @inode: inode to check permissions on
577 * @mode: the new mode of @inode
579 * If the inode has been found through an idmapped mount the user namespace of
580 * the vfsmount must be passed through @mnt_userns. This function will then
581 * take care to map the inode according to @mnt_userns before checking
582 * permissions. On non-idmapped mounts or if permission checking is to be
583 * performed on the raw inode simply passs init_user_ns.
586 posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
589 struct posix_acl *acl;
592 if (!IS_POSIXACL(inode))
594 if (!inode->i_op->set_acl)
597 acl = get_acl(inode, ACL_TYPE_ACCESS);
598 if (IS_ERR_OR_NULL(acl)) {
599 if (acl == ERR_PTR(-EOPNOTSUPP))
604 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
607 ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
608 posix_acl_release(acl);
611 EXPORT_SYMBOL(posix_acl_chmod);
614 posix_acl_create(struct inode *dir, umode_t *mode,
615 struct posix_acl **default_acl, struct posix_acl **acl)
618 struct posix_acl *clone;
624 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
627 p = get_acl(dir, ACL_TYPE_DEFAULT);
628 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
629 *mode &= ~current_umask();
636 clone = posix_acl_clone(p, GFP_NOFS);
640 ret = posix_acl_create_masq(clone, mode);
642 goto err_release_clone;
645 posix_acl_release(clone);
650 posix_acl_release(p);
657 posix_acl_release(clone);
659 posix_acl_release(p);
662 EXPORT_SYMBOL_GPL(posix_acl_create);
665 * posix_acl_update_mode - update mode in set_acl
666 * @mnt_userns: user namespace of the mount @inode was found from
667 * @inode: target inode
668 * @mode_p: mode (pointer) for update
671 * Update the file mode when setting an ACL: compute the new file permission
672 * bits based on the ACL. In addition, if the ACL is equivalent to the new
673 * file mode, set *@acl to NULL to indicate that no ACL should be set.
675 * As with chmod, clear the setgid bit if the caller is not in the owning group
676 * or capable of CAP_FSETID (see inode_change_ok).
678 * If the inode has been found through an idmapped mount the user namespace of
679 * the vfsmount must be passed through @mnt_userns. This function will then
680 * take care to map the inode according to @mnt_userns before checking
681 * permissions. On non-idmapped mounts or if permission checking is to be
682 * performed on the raw inode simply passs init_user_ns.
684 * Called from set_acl inode operations.
686 int posix_acl_update_mode(struct user_namespace *mnt_userns,
687 struct inode *inode, umode_t *mode_p,
688 struct posix_acl **acl)
690 umode_t mode = inode->i_mode;
693 error = posix_acl_equiv_mode(*acl, &mode);
698 if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) &&
699 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
704 EXPORT_SYMBOL(posix_acl_update_mode);
707 * Fix up the uids and gids in posix acl extended attributes in place.
709 static void posix_acl_fix_xattr_userns(
710 struct user_namespace *to, struct user_namespace *from,
711 struct user_namespace *mnt_userns,
712 void *value, size_t size, bool from_user)
714 struct posix_acl_xattr_header *header = value;
715 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
722 if (size < sizeof(struct posix_acl_xattr_header))
724 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
727 count = posix_acl_xattr_count(size);
733 for (end = entry + count; entry != end; entry++) {
734 switch(le16_to_cpu(entry->e_tag)) {
736 uid = make_kuid(from, le32_to_cpu(entry->e_id));
738 uid = kuid_from_mnt(mnt_userns, uid);
740 uid = kuid_into_mnt(mnt_userns, uid);
741 entry->e_id = cpu_to_le32(from_kuid(to, uid));
744 gid = make_kgid(from, le32_to_cpu(entry->e_id));
746 gid = kgid_from_mnt(mnt_userns, gid);
748 gid = kgid_into_mnt(mnt_userns, gid);
749 entry->e_id = cpu_to_le32(from_kgid(to, gid));
757 void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns,
758 void *value, size_t size)
760 struct user_namespace *user_ns = current_user_ns();
761 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
763 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value,
767 void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns,
768 void *value, size_t size)
770 struct user_namespace *user_ns = current_user_ns();
771 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
773 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value,
778 * Convert from extended attribute to in-memory representation.
781 posix_acl_from_xattr(struct user_namespace *user_ns,
782 const void *value, size_t size)
784 const struct posix_acl_xattr_header *header = value;
785 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
787 struct posix_acl *acl;
788 struct posix_acl_entry *acl_e;
792 if (size < sizeof(struct posix_acl_xattr_header))
793 return ERR_PTR(-EINVAL);
794 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
795 return ERR_PTR(-EOPNOTSUPP);
797 count = posix_acl_xattr_count(size);
799 return ERR_PTR(-EINVAL);
803 acl = posix_acl_alloc(count, GFP_NOFS);
805 return ERR_PTR(-ENOMEM);
806 acl_e = acl->a_entries;
808 for (end = entry + count; entry != end; acl_e++, entry++) {
809 acl_e->e_tag = le16_to_cpu(entry->e_tag);
810 acl_e->e_perm = le16_to_cpu(entry->e_perm);
812 switch(acl_e->e_tag) {
822 le32_to_cpu(entry->e_id));
823 if (!uid_valid(acl_e->e_uid))
829 le32_to_cpu(entry->e_id));
830 if (!gid_valid(acl_e->e_gid))
841 posix_acl_release(acl);
842 return ERR_PTR(-EINVAL);
844 EXPORT_SYMBOL (posix_acl_from_xattr);
847 * Convert from in-memory to extended attribute representation.
850 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
851 void *buffer, size_t size)
853 struct posix_acl_xattr_header *ext_acl = buffer;
854 struct posix_acl_xattr_entry *ext_entry;
857 real_size = posix_acl_xattr_size(acl->a_count);
860 if (real_size > size)
863 ext_entry = (void *)(ext_acl + 1);
864 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
866 for (n=0; n < acl->a_count; n++, ext_entry++) {
867 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
868 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
869 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
870 switch(acl_e->e_tag) {
873 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
877 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
880 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
886 EXPORT_SYMBOL (posix_acl_to_xattr);
889 posix_acl_xattr_get(const struct xattr_handler *handler,
890 struct dentry *unused, struct inode *inode,
891 const char *name, void *value, size_t size)
893 struct posix_acl *acl;
896 if (!IS_POSIXACL(inode))
898 if (S_ISLNK(inode->i_mode))
901 acl = get_acl(inode, handler->flags);
907 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
908 posix_acl_release(acl);
914 set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
915 int type, struct posix_acl *acl)
917 if (!IS_POSIXACL(inode))
919 if (!inode->i_op->set_acl)
922 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
923 return acl ? -EACCES : 0;
924 if (!inode_owner_or_capable(mnt_userns, inode))
928 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
932 return inode->i_op->set_acl(mnt_userns, inode, acl, type);
934 EXPORT_SYMBOL(set_posix_acl);
937 posix_acl_xattr_set(const struct xattr_handler *handler,
938 struct user_namespace *mnt_userns,
939 struct dentry *unused, struct inode *inode,
940 const char *name, const void *value, size_t size,
943 struct posix_acl *acl = NULL;
947 acl = posix_acl_from_xattr(&init_user_ns, value, size);
951 ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
952 posix_acl_release(acl);
957 posix_acl_xattr_list(struct dentry *dentry)
959 return IS_POSIXACL(d_backing_inode(dentry));
962 const struct xattr_handler posix_acl_access_xattr_handler = {
963 .name = XATTR_NAME_POSIX_ACL_ACCESS,
964 .flags = ACL_TYPE_ACCESS,
965 .list = posix_acl_xattr_list,
966 .get = posix_acl_xattr_get,
967 .set = posix_acl_xattr_set,
969 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
971 const struct xattr_handler posix_acl_default_xattr_handler = {
972 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
973 .flags = ACL_TYPE_DEFAULT,
974 .list = posix_acl_xattr_list,
975 .get = posix_acl_xattr_get,
976 .set = posix_acl_xattr_set,
978 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
980 int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
981 struct posix_acl *acl, int type)
985 if (type == ACL_TYPE_ACCESS) {
986 error = posix_acl_update_mode(mnt_userns, inode,
987 &inode->i_mode, &acl);
992 inode->i_ctime = current_time(inode);
993 set_cached_acl(inode, type, acl);
997 int simple_acl_create(struct inode *dir, struct inode *inode)
999 struct posix_acl *default_acl, *acl;
1002 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1006 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1007 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1010 posix_acl_release(default_acl);
1012 posix_acl_release(acl);