1 // SPDX-License-Identifier: GPL-2.0-only
5 * Fixes from William Schumacher incorporated on 15 March 2001.
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 cmpxchg(p, ACL_NOT_CACHED, sentinel);
140 * Normally, the ACL returned by ->get_acl will be cached.
141 * A filesystem can prevent that by calling
142 * forget_cached_acl(inode, type) in ->get_acl.
144 * If the filesystem doesn't have a get_acl() function at all, we'll
145 * just create the negative cache entry.
147 if (!inode->i_op->get_acl) {
148 set_cached_acl(inode, type, NULL);
151 acl = inode->i_op->get_acl(inode, type, false);
155 * Remove our sentinel so that we don't block future attempts
158 cmpxchg(p, sentinel, ACL_NOT_CACHED);
163 * Cache the result, but only if our sentinel is still in place.
166 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
167 posix_acl_release(acl);
170 EXPORT_SYMBOL(get_acl);
173 * Init a fresh posix_acl
176 posix_acl_init(struct posix_acl *acl, int count)
178 refcount_set(&acl->a_refcount, 1);
179 acl->a_count = count;
181 EXPORT_SYMBOL(posix_acl_init);
184 * Allocate a new ACL with the specified number of entries.
187 posix_acl_alloc(int count, gfp_t flags)
189 const size_t size = sizeof(struct posix_acl) +
190 count * sizeof(struct posix_acl_entry);
191 struct posix_acl *acl = kmalloc(size, flags);
193 posix_acl_init(acl, count);
196 EXPORT_SYMBOL(posix_acl_alloc);
201 static struct posix_acl *
202 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
204 struct posix_acl *clone = NULL;
207 int size = sizeof(struct posix_acl) + acl->a_count *
208 sizeof(struct posix_acl_entry);
209 clone = kmemdup(acl, size, flags);
211 refcount_set(&clone->a_refcount, 1);
217 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
220 posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
222 const struct posix_acl_entry *pa, *pe;
223 int state = ACL_USER_OBJ;
226 FOREACH_ACL_ENTRY(pa, acl, pe) {
227 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
231 if (state == ACL_USER_OBJ) {
238 if (state != ACL_USER)
240 if (!kuid_has_mapping(user_ns, pa->e_uid))
246 if (state == ACL_USER) {
253 if (state != ACL_GROUP)
255 if (!kgid_has_mapping(user_ns, pa->e_gid))
261 if (state != ACL_GROUP)
267 if (state == ACL_OTHER ||
268 (state == ACL_GROUP && !needs_mask)) {
282 EXPORT_SYMBOL(posix_acl_valid);
285 * Returns 0 if the acl can be exactly represented in the traditional
286 * file mode permission bits, or else 1. Returns -E... on error.
289 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
291 const struct posix_acl_entry *pa, *pe;
296 * A null ACL can always be presented as mode bits.
301 FOREACH_ACL_ENTRY(pa, acl, pe) {
304 mode |= (pa->e_perm & S_IRWXO) << 6;
307 mode |= (pa->e_perm & S_IRWXO) << 3;
310 mode |= pa->e_perm & S_IRWXO;
313 mode = (mode & ~S_IRWXG) |
314 ((pa->e_perm & S_IRWXO) << 3);
326 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
329 EXPORT_SYMBOL(posix_acl_equiv_mode);
332 * Create an ACL representing the file mode permission bits of an inode.
335 posix_acl_from_mode(umode_t mode, gfp_t flags)
337 struct posix_acl *acl = posix_acl_alloc(3, flags);
339 return ERR_PTR(-ENOMEM);
341 acl->a_entries[0].e_tag = ACL_USER_OBJ;
342 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
344 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
345 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
347 acl->a_entries[2].e_tag = ACL_OTHER;
348 acl->a_entries[2].e_perm = (mode & S_IRWXO);
351 EXPORT_SYMBOL(posix_acl_from_mode);
354 * Return 0 if current is granted want access to the inode
355 * by the acl. Returns -E... otherwise.
358 posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
359 const struct posix_acl *acl, int want)
361 const struct posix_acl_entry *pa, *pe, *mask_obj;
366 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
368 FOREACH_ACL_ENTRY(pa, acl, pe) {
371 /* (May have been checked already) */
372 uid = i_uid_into_mnt(mnt_userns, inode);
373 if (uid_eq(uid, current_fsuid()))
377 uid = kuid_into_mnt(mnt_userns, pa->e_uid);
378 if (uid_eq(uid, current_fsuid()))
382 gid = i_gid_into_mnt(mnt_userns, inode);
383 if (in_group_p(gid)) {
385 if ((pa->e_perm & want) == want)
390 gid = kgid_into_mnt(mnt_userns, pa->e_gid);
391 if (in_group_p(gid)) {
393 if ((pa->e_perm & want) == want)
411 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
412 if (mask_obj->e_tag == ACL_MASK) {
413 if ((pa->e_perm & mask_obj->e_perm & want) == want)
420 if ((pa->e_perm & want) == want)
426 * Modify acl when creating a new inode. The caller must ensure the acl is
427 * only referenced once.
429 * mode_p initially must contain the mode parameter to the open() / creat()
430 * system calls. All permissions that are not granted by the acl are removed.
431 * The permissions in the acl are changed to reflect the mode_p parameter.
433 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
435 struct posix_acl_entry *pa, *pe;
436 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
437 umode_t mode = *mode_p;
440 /* assert(atomic_read(acl->a_refcount) == 1); */
442 FOREACH_ACL_ENTRY(pa, acl, pe) {
445 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
446 mode &= (pa->e_perm << 6) | ~S_IRWXU;
459 pa->e_perm &= mode | ~S_IRWXO;
460 mode &= pa->e_perm | ~S_IRWXO;
474 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
475 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
479 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
480 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
483 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
488 * Modify the ACL for the chmod syscall.
490 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
492 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
493 struct posix_acl_entry *pa, *pe;
495 /* assert(atomic_read(acl->a_refcount) == 1); */
497 FOREACH_ACL_ENTRY(pa, acl, pe) {
500 pa->e_perm = (mode & S_IRWXU) >> 6;
516 pa->e_perm = (mode & S_IRWXO);
525 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
529 group_obj->e_perm = (mode & S_IRWXG) >> 3;
536 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
538 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
541 err = posix_acl_create_masq(clone, mode_p);
543 posix_acl_release(clone);
547 posix_acl_release(*acl);
551 EXPORT_SYMBOL(__posix_acl_create);
554 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
556 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
559 err = __posix_acl_chmod_masq(clone, mode);
561 posix_acl_release(clone);
565 posix_acl_release(*acl);
569 EXPORT_SYMBOL(__posix_acl_chmod);
572 * posix_acl_chmod - chmod a posix acl
574 * @mnt_userns: user namespace of the mount @inode was found from
575 * @inode: inode to check permissions on
576 * @mode: the new mode of @inode
578 * If the inode has been found through an idmapped mount the user namespace of
579 * the vfsmount must be passed through @mnt_userns. This function will then
580 * take care to map the inode according to @mnt_userns before checking
581 * permissions. On non-idmapped mounts or if permission checking is to be
582 * performed on the raw inode simply passs init_user_ns.
585 posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
588 struct posix_acl *acl;
591 if (!IS_POSIXACL(inode))
593 if (!inode->i_op->set_acl)
596 acl = get_acl(inode, ACL_TYPE_ACCESS);
597 if (IS_ERR_OR_NULL(acl)) {
598 if (acl == ERR_PTR(-EOPNOTSUPP))
603 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
606 ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
607 posix_acl_release(acl);
610 EXPORT_SYMBOL(posix_acl_chmod);
613 posix_acl_create(struct inode *dir, umode_t *mode,
614 struct posix_acl **default_acl, struct posix_acl **acl)
617 struct posix_acl *clone;
623 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
626 p = get_acl(dir, ACL_TYPE_DEFAULT);
627 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
628 *mode &= ~current_umask();
635 clone = posix_acl_clone(p, GFP_NOFS);
639 ret = posix_acl_create_masq(clone, mode);
641 goto err_release_clone;
644 posix_acl_release(clone);
649 posix_acl_release(p);
656 posix_acl_release(clone);
658 posix_acl_release(p);
661 EXPORT_SYMBOL_GPL(posix_acl_create);
664 * posix_acl_update_mode - update mode in set_acl
665 * @mnt_userns: user namespace of the mount @inode was found from
666 * @inode: target inode
667 * @mode_p: mode (pointer) for update
670 * Update the file mode when setting an ACL: compute the new file permission
671 * bits based on the ACL. In addition, if the ACL is equivalent to the new
672 * file mode, set *@acl to NULL to indicate that no ACL should be set.
674 * As with chmod, clear the setgid bit if the caller is not in the owning group
675 * or capable of CAP_FSETID (see inode_change_ok).
677 * If the inode has been found through an idmapped mount the user namespace of
678 * the vfsmount must be passed through @mnt_userns. This function will then
679 * take care to map the inode according to @mnt_userns before checking
680 * permissions. On non-idmapped mounts or if permission checking is to be
681 * performed on the raw inode simply passs init_user_ns.
683 * Called from set_acl inode operations.
685 int posix_acl_update_mode(struct user_namespace *mnt_userns,
686 struct inode *inode, umode_t *mode_p,
687 struct posix_acl **acl)
689 umode_t mode = inode->i_mode;
692 error = posix_acl_equiv_mode(*acl, &mode);
697 if (!in_group_p(i_gid_into_mnt(mnt_userns, inode)) &&
698 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
703 EXPORT_SYMBOL(posix_acl_update_mode);
706 * Fix up the uids and gids in posix acl extended attributes in place.
708 static void posix_acl_fix_xattr_userns(
709 struct user_namespace *to, struct user_namespace *from,
710 struct user_namespace *mnt_userns,
711 void *value, size_t size, bool from_user)
713 struct posix_acl_xattr_header *header = value;
714 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
721 if (size < sizeof(struct posix_acl_xattr_header))
723 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
726 count = posix_acl_xattr_count(size);
732 for (end = entry + count; entry != end; entry++) {
733 switch(le16_to_cpu(entry->e_tag)) {
735 uid = make_kuid(from, le32_to_cpu(entry->e_id));
737 uid = kuid_from_mnt(mnt_userns, uid);
739 uid = kuid_into_mnt(mnt_userns, uid);
740 entry->e_id = cpu_to_le32(from_kuid(to, uid));
743 gid = make_kgid(from, le32_to_cpu(entry->e_id));
745 gid = kgid_from_mnt(mnt_userns, gid);
747 gid = kgid_into_mnt(mnt_userns, gid);
748 entry->e_id = cpu_to_le32(from_kgid(to, gid));
756 void posix_acl_fix_xattr_from_user(struct user_namespace *mnt_userns,
757 void *value, size_t size)
759 struct user_namespace *user_ns = current_user_ns();
760 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
762 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, mnt_userns, value,
766 void posix_acl_fix_xattr_to_user(struct user_namespace *mnt_userns,
767 void *value, size_t size)
769 struct user_namespace *user_ns = current_user_ns();
770 if ((user_ns == &init_user_ns) && (mnt_userns == &init_user_ns))
772 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, mnt_userns, value,
777 * Convert from extended attribute to in-memory representation.
780 posix_acl_from_xattr(struct user_namespace *user_ns,
781 const void *value, size_t size)
783 const struct posix_acl_xattr_header *header = value;
784 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
786 struct posix_acl *acl;
787 struct posix_acl_entry *acl_e;
791 if (size < sizeof(struct posix_acl_xattr_header))
792 return ERR_PTR(-EINVAL);
793 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
794 return ERR_PTR(-EOPNOTSUPP);
796 count = posix_acl_xattr_count(size);
798 return ERR_PTR(-EINVAL);
802 acl = posix_acl_alloc(count, GFP_NOFS);
804 return ERR_PTR(-ENOMEM);
805 acl_e = acl->a_entries;
807 for (end = entry + count; entry != end; acl_e++, entry++) {
808 acl_e->e_tag = le16_to_cpu(entry->e_tag);
809 acl_e->e_perm = le16_to_cpu(entry->e_perm);
811 switch(acl_e->e_tag) {
821 le32_to_cpu(entry->e_id));
822 if (!uid_valid(acl_e->e_uid))
828 le32_to_cpu(entry->e_id));
829 if (!gid_valid(acl_e->e_gid))
840 posix_acl_release(acl);
841 return ERR_PTR(-EINVAL);
843 EXPORT_SYMBOL (posix_acl_from_xattr);
846 * Convert from in-memory to extended attribute representation.
849 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
850 void *buffer, size_t size)
852 struct posix_acl_xattr_header *ext_acl = buffer;
853 struct posix_acl_xattr_entry *ext_entry;
856 real_size = posix_acl_xattr_size(acl->a_count);
859 if (real_size > size)
862 ext_entry = (void *)(ext_acl + 1);
863 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
865 for (n=0; n < acl->a_count; n++, ext_entry++) {
866 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
867 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
868 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
869 switch(acl_e->e_tag) {
872 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
876 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
879 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
885 EXPORT_SYMBOL (posix_acl_to_xattr);
888 posix_acl_xattr_get(const struct xattr_handler *handler,
889 struct dentry *unused, struct inode *inode,
890 const char *name, void *value, size_t size)
892 struct posix_acl *acl;
895 if (!IS_POSIXACL(inode))
897 if (S_ISLNK(inode->i_mode))
900 acl = get_acl(inode, handler->flags);
906 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
907 posix_acl_release(acl);
913 set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
914 int type, struct posix_acl *acl)
916 if (!IS_POSIXACL(inode))
918 if (!inode->i_op->set_acl)
921 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
922 return acl ? -EACCES : 0;
923 if (!inode_owner_or_capable(mnt_userns, inode))
927 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
931 return inode->i_op->set_acl(mnt_userns, inode, acl, type);
933 EXPORT_SYMBOL(set_posix_acl);
936 posix_acl_xattr_set(const struct xattr_handler *handler,
937 struct user_namespace *mnt_userns,
938 struct dentry *unused, struct inode *inode,
939 const char *name, const void *value, size_t size,
942 struct posix_acl *acl = NULL;
946 acl = posix_acl_from_xattr(&init_user_ns, value, size);
950 ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
951 posix_acl_release(acl);
956 posix_acl_xattr_list(struct dentry *dentry)
958 return IS_POSIXACL(d_backing_inode(dentry));
961 const struct xattr_handler posix_acl_access_xattr_handler = {
962 .name = XATTR_NAME_POSIX_ACL_ACCESS,
963 .flags = ACL_TYPE_ACCESS,
964 .list = posix_acl_xattr_list,
965 .get = posix_acl_xattr_get,
966 .set = posix_acl_xattr_set,
968 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
970 const struct xattr_handler posix_acl_default_xattr_handler = {
971 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
972 .flags = ACL_TYPE_DEFAULT,
973 .list = posix_acl_xattr_list,
974 .get = posix_acl_xattr_get,
975 .set = posix_acl_xattr_set,
977 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
979 int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
980 struct posix_acl *acl, int type)
984 if (type == ACL_TYPE_ACCESS) {
985 error = posix_acl_update_mode(mnt_userns, inode,
986 &inode->i_mode, &acl);
991 inode->i_ctime = current_time(inode);
992 set_cached_acl(inode, type, acl);
996 int simple_acl_create(struct inode *dir, struct inode *inode)
998 struct posix_acl *default_acl, *acl;
1001 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1005 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1006 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1009 posix_acl_release(default_acl);
1011 posix_acl_release(acl);