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>
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>
34 static struct posix_acl **acl_by_type(struct inode *inode, int type)
39 case ACL_TYPE_DEFAULT:
40 return &inode->i_default_acl;
46 struct posix_acl *get_cached_acl(struct inode *inode, int type)
48 struct posix_acl **p = acl_by_type(inode, type);
49 struct posix_acl *acl;
53 acl = rcu_dereference(*p);
54 if (!acl || is_uncached_acl(acl) ||
55 refcount_inc_not_zero(&acl->a_refcount))
63 EXPORT_SYMBOL(get_cached_acl);
65 struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
67 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
69 if (acl == ACL_DONT_CACHE) {
70 struct posix_acl *ret;
72 ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU);
79 EXPORT_SYMBOL(get_cached_acl_rcu);
81 void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
83 struct posix_acl **p = acl_by_type(inode, type);
84 struct posix_acl *old;
86 old = xchg(p, posix_acl_dup(acl));
87 if (!is_uncached_acl(old))
88 posix_acl_release(old);
90 EXPORT_SYMBOL(set_cached_acl);
92 static void __forget_cached_acl(struct posix_acl **p)
94 struct posix_acl *old;
96 old = xchg(p, ACL_NOT_CACHED);
97 if (!is_uncached_acl(old))
98 posix_acl_release(old);
101 void forget_cached_acl(struct inode *inode, int type)
103 __forget_cached_acl(acl_by_type(inode, type));
105 EXPORT_SYMBOL(forget_cached_acl);
107 void forget_all_cached_acls(struct inode *inode)
109 __forget_cached_acl(&inode->i_acl);
110 __forget_cached_acl(&inode->i_default_acl);
112 EXPORT_SYMBOL(forget_all_cached_acls);
114 static struct posix_acl *__get_acl(struct user_namespace *mnt_userns,
115 struct dentry *dentry, struct inode *inode,
119 struct posix_acl **p;
120 struct posix_acl *acl;
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.
128 acl = get_cached_acl(inode, type);
129 if (!is_uncached_acl(acl))
132 if (!IS_POSIXACL(inode))
135 sentinel = uncached_acl_sentinel(current);
136 p = acl_by_type(inode, type);
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.)
146 cmpxchg(p, ACL_NOT_CACHED, sentinel);
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.
153 * If the filesystem doesn't have a get{_inode}_ acl() function at all,
154 * we'll just create the negative cache entry.
156 if (dentry && inode->i_op->get_acl) {
157 acl = inode->i_op->get_acl(mnt_userns, dentry, type);
158 } else if (inode->i_op->get_inode_acl) {
159 acl = inode->i_op->get_inode_acl(inode, type, false);
161 set_cached_acl(inode, type, NULL);
166 * Remove our sentinel so that we don't block future attempts
169 cmpxchg(p, sentinel, ACL_NOT_CACHED);
174 * Cache the result, but only if our sentinel is still in place.
177 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
178 posix_acl_release(acl);
182 struct posix_acl *get_inode_acl(struct inode *inode, int type)
184 return __get_acl(&init_user_ns, NULL, inode, type);
186 EXPORT_SYMBOL(get_inode_acl);
189 * Init a fresh posix_acl
192 posix_acl_init(struct posix_acl *acl, int count)
194 refcount_set(&acl->a_refcount, 1);
195 acl->a_count = count;
197 EXPORT_SYMBOL(posix_acl_init);
200 * Allocate a new ACL with the specified number of entries.
203 posix_acl_alloc(int count, gfp_t flags)
205 const size_t size = sizeof(struct posix_acl) +
206 count * sizeof(struct posix_acl_entry);
207 struct posix_acl *acl = kmalloc(size, flags);
209 posix_acl_init(acl, count);
212 EXPORT_SYMBOL(posix_acl_alloc);
218 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
220 struct posix_acl *clone = NULL;
223 int size = sizeof(struct posix_acl) + acl->a_count *
224 sizeof(struct posix_acl_entry);
225 clone = kmemdup(acl, size, flags);
227 refcount_set(&clone->a_refcount, 1);
231 EXPORT_SYMBOL_GPL(posix_acl_clone);
234 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
237 posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
239 const struct posix_acl_entry *pa, *pe;
240 int state = ACL_USER_OBJ;
243 FOREACH_ACL_ENTRY(pa, acl, pe) {
244 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
248 if (state == ACL_USER_OBJ) {
255 if (state != ACL_USER)
257 if (!kuid_has_mapping(user_ns, pa->e_uid))
263 if (state == ACL_USER) {
270 if (state != ACL_GROUP)
272 if (!kgid_has_mapping(user_ns, pa->e_gid))
278 if (state != ACL_GROUP)
284 if (state == ACL_OTHER ||
285 (state == ACL_GROUP && !needs_mask)) {
299 EXPORT_SYMBOL(posix_acl_valid);
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.
306 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
308 const struct posix_acl_entry *pa, *pe;
313 * A null ACL can always be presented as mode bits.
318 FOREACH_ACL_ENTRY(pa, acl, pe) {
321 mode |= (pa->e_perm & S_IRWXO) << 6;
324 mode |= (pa->e_perm & S_IRWXO) << 3;
327 mode |= pa->e_perm & S_IRWXO;
330 mode = (mode & ~S_IRWXG) |
331 ((pa->e_perm & S_IRWXO) << 3);
343 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
346 EXPORT_SYMBOL(posix_acl_equiv_mode);
349 * Create an ACL representing the file mode permission bits of an inode.
352 posix_acl_from_mode(umode_t mode, gfp_t flags)
354 struct posix_acl *acl = posix_acl_alloc(3, flags);
356 return ERR_PTR(-ENOMEM);
358 acl->a_entries[0].e_tag = ACL_USER_OBJ;
359 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
361 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
362 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
364 acl->a_entries[2].e_tag = ACL_OTHER;
365 acl->a_entries[2].e_perm = (mode & S_IRWXO);
368 EXPORT_SYMBOL(posix_acl_from_mode);
371 * Return 0 if current is granted want access to the inode
372 * by the acl. Returns -E... otherwise.
375 posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
376 const struct posix_acl *acl, int want)
378 const struct posix_acl_entry *pa, *pe, *mask_obj;
379 struct user_namespace *fs_userns = i_user_ns(inode);
384 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
386 FOREACH_ACL_ENTRY(pa, acl, pe) {
389 /* (May have been checked already) */
390 vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
391 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
395 vfsuid = make_vfsuid(mnt_userns, fs_userns,
397 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
401 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
402 if (vfsgid_in_group_p(vfsgid)) {
404 if ((pa->e_perm & want) == want)
409 vfsgid = make_vfsgid(mnt_userns, fs_userns,
411 if (vfsgid_in_group_p(vfsgid)) {
413 if ((pa->e_perm & want) == want)
431 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
432 if (mask_obj->e_tag == ACL_MASK) {
433 if ((pa->e_perm & mask_obj->e_perm & want) == want)
440 if ((pa->e_perm & want) == want)
446 * Modify acl when creating a new inode. The caller must ensure the acl is
447 * only referenced once.
449 * mode_p initially must contain the mode parameter to the open() / creat()
450 * system calls. All permissions that are not granted by the acl are removed.
451 * The permissions in the acl are changed to reflect the mode_p parameter.
453 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
455 struct posix_acl_entry *pa, *pe;
456 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
457 umode_t mode = *mode_p;
460 /* assert(atomic_read(acl->a_refcount) == 1); */
462 FOREACH_ACL_ENTRY(pa, acl, pe) {
465 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
466 mode &= (pa->e_perm << 6) | ~S_IRWXU;
479 pa->e_perm &= mode | ~S_IRWXO;
480 mode &= pa->e_perm | ~S_IRWXO;
494 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
495 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
499 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
500 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
503 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
508 * Modify the ACL for the chmod syscall.
510 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
512 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
513 struct posix_acl_entry *pa, *pe;
515 /* assert(atomic_read(acl->a_refcount) == 1); */
517 FOREACH_ACL_ENTRY(pa, acl, pe) {
520 pa->e_perm = (mode & S_IRWXU) >> 6;
536 pa->e_perm = (mode & S_IRWXO);
545 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
549 group_obj->e_perm = (mode & S_IRWXG) >> 3;
556 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
558 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
561 err = posix_acl_create_masq(clone, mode_p);
563 posix_acl_release(clone);
567 posix_acl_release(*acl);
571 EXPORT_SYMBOL(__posix_acl_create);
574 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
576 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
579 err = __posix_acl_chmod_masq(clone, mode);
581 posix_acl_release(clone);
585 posix_acl_release(*acl);
589 EXPORT_SYMBOL(__posix_acl_chmod);
592 * posix_acl_chmod - chmod a posix acl
594 * @mnt_userns: user namespace of the mount @inode was found from
595 * @dentry: dentry to check permissions on
596 * @mode: the new mode of @inode
598 * If the dentry has been found through an idmapped mount the user namespace of
599 * the vfsmount must be passed through @mnt_userns. This function will then
600 * take care to map the inode according to @mnt_userns before checking
601 * permissions. On non-idmapped mounts or if permission checking is to be
602 * performed on the raw inode simply passs init_user_ns.
605 posix_acl_chmod(struct user_namespace *mnt_userns, struct dentry *dentry,
608 struct inode *inode = d_inode(dentry);
609 struct posix_acl *acl;
612 if (!IS_POSIXACL(inode))
614 if (!inode->i_op->set_acl)
617 acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
618 if (IS_ERR_OR_NULL(acl)) {
619 if (acl == ERR_PTR(-EOPNOTSUPP))
624 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
627 ret = inode->i_op->set_acl(mnt_userns, dentry, acl, ACL_TYPE_ACCESS);
628 posix_acl_release(acl);
631 EXPORT_SYMBOL(posix_acl_chmod);
634 posix_acl_create(struct inode *dir, umode_t *mode,
635 struct posix_acl **default_acl, struct posix_acl **acl)
638 struct posix_acl *clone;
644 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
647 p = get_inode_acl(dir, ACL_TYPE_DEFAULT);
648 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
649 *mode &= ~current_umask();
656 clone = posix_acl_clone(p, GFP_NOFS);
660 ret = posix_acl_create_masq(clone, mode);
662 goto err_release_clone;
665 posix_acl_release(clone);
670 posix_acl_release(p);
677 posix_acl_release(clone);
679 posix_acl_release(p);
682 EXPORT_SYMBOL_GPL(posix_acl_create);
685 * posix_acl_update_mode - update mode in set_acl
686 * @mnt_userns: user namespace of the mount @inode was found from
687 * @inode: target inode
688 * @mode_p: mode (pointer) for update
691 * Update the file mode when setting an ACL: compute the new file permission
692 * bits based on the ACL. In addition, if the ACL is equivalent to the new
693 * file mode, set *@acl to NULL to indicate that no ACL should be set.
695 * As with chmod, clear the setgid bit if the caller is not in the owning group
696 * or capable of CAP_FSETID (see inode_change_ok).
698 * If the inode has been found through an idmapped mount the user namespace of
699 * the vfsmount must be passed through @mnt_userns. This function will then
700 * take care to map the inode according to @mnt_userns before checking
701 * permissions. On non-idmapped mounts or if permission checking is to be
702 * performed on the raw inode simply passs init_user_ns.
704 * Called from set_acl inode operations.
706 int posix_acl_update_mode(struct user_namespace *mnt_userns,
707 struct inode *inode, umode_t *mode_p,
708 struct posix_acl **acl)
710 umode_t mode = inode->i_mode;
713 error = posix_acl_equiv_mode(*acl, &mode);
718 if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
719 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
724 EXPORT_SYMBOL(posix_acl_update_mode);
727 * Fix up the uids and gids in posix acl extended attributes in place.
729 static int posix_acl_fix_xattr_common(const void *value, size_t size)
731 const struct posix_acl_xattr_header *header = value;
736 if (size < sizeof(struct posix_acl_xattr_header))
738 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
741 count = posix_acl_xattr_count(size);
750 void posix_acl_getxattr_idmapped_mnt(struct user_namespace *mnt_userns,
751 const struct inode *inode,
752 void *value, size_t size)
754 struct posix_acl_xattr_header *header = value;
755 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
756 struct user_namespace *fs_userns = i_user_ns(inode);
763 if (no_idmapping(mnt_userns, i_user_ns(inode)))
766 count = posix_acl_fix_xattr_common(value, size);
770 for (end = entry + count; entry != end; entry++) {
771 switch (le16_to_cpu(entry->e_tag)) {
773 uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
774 vfsuid = make_vfsuid(mnt_userns, fs_userns, uid);
775 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns,
776 vfsuid_into_kuid(vfsuid)));
779 gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
780 vfsgid = make_vfsgid(mnt_userns, fs_userns, gid);
781 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns,
782 vfsgid_into_kgid(vfsgid)));
790 static void posix_acl_fix_xattr_userns(
791 struct user_namespace *to, struct user_namespace *from,
792 void *value, size_t size)
794 struct posix_acl_xattr_header *header = value;
795 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
800 count = posix_acl_fix_xattr_common(value, size);
804 for (end = entry + count; entry != end; entry++) {
805 switch(le16_to_cpu(entry->e_tag)) {
807 uid = make_kuid(from, le32_to_cpu(entry->e_id));
808 entry->e_id = cpu_to_le32(from_kuid(to, uid));
811 gid = make_kgid(from, le32_to_cpu(entry->e_id));
812 entry->e_id = cpu_to_le32(from_kgid(to, gid));
820 void posix_acl_fix_xattr_from_user(void *value, size_t size)
822 struct user_namespace *user_ns = current_user_ns();
823 if (user_ns == &init_user_ns)
825 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
828 void posix_acl_fix_xattr_to_user(void *value, size_t size)
830 struct user_namespace *user_ns = current_user_ns();
831 if (user_ns == &init_user_ns)
833 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
837 * make_posix_acl - convert POSIX ACLs from uapi to VFS format using the
838 * provided callbacks to map ACL_{GROUP,USER} entries into the
840 * @mnt_userns: the mount's idmapping
841 * @fs_userns: the filesystem's idmapping
842 * @value: the uapi representation of POSIX ACLs
843 * @size: the size of @void
844 * @uid_cb: callback to use for mapping the uid stored in ACL_USER entries
845 * @gid_cb: callback to use for mapping the gid stored in ACL_GROUP entries
847 * The make_posix_acl() helper is an abstraction to translate from uapi format
848 * into the VFS format allowing the caller to specific callbacks to map
849 * ACL_{GROUP,USER} entries into the expected format. This is used in
850 * posix_acl_from_xattr() and vfs_set_acl_prepare() and avoids pointless code
853 * Return: Allocated struct posix_acl on success, NULL for a valid header but
854 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
856 static struct posix_acl *make_posix_acl(struct user_namespace *mnt_userns,
857 struct user_namespace *fs_userns, const void *value, size_t size,
858 kuid_t (*uid_cb)(struct user_namespace *, struct user_namespace *,
859 const struct posix_acl_xattr_entry *),
860 kgid_t (*gid_cb)(struct user_namespace *, struct user_namespace *,
861 const struct posix_acl_xattr_entry *))
863 const struct posix_acl_xattr_header *header = value;
864 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
866 struct posix_acl *acl;
867 struct posix_acl_entry *acl_e;
869 count = posix_acl_fix_xattr_common(value, size);
871 return ERR_PTR(count);
875 acl = posix_acl_alloc(count, GFP_NOFS);
877 return ERR_PTR(-ENOMEM);
878 acl_e = acl->a_entries;
880 for (end = entry + count; entry != end; acl_e++, entry++) {
881 acl_e->e_tag = le16_to_cpu(entry->e_tag);
882 acl_e->e_perm = le16_to_cpu(entry->e_perm);
884 switch(acl_e->e_tag) {
892 acl_e->e_uid = uid_cb(mnt_userns, fs_userns, entry);
893 if (!uid_valid(acl_e->e_uid))
897 acl_e->e_gid = gid_cb(mnt_userns, fs_userns, entry);
898 if (!gid_valid(acl_e->e_gid))
909 posix_acl_release(acl);
910 return ERR_PTR(-EINVAL);
914 * vfs_set_acl_prepare_kuid - map ACL_USER uid according to mount- and
915 * filesystem idmapping
916 * @mnt_userns: the mount's idmapping
917 * @fs_userns: the filesystem's idmapping
918 * @e: a ACL_USER entry in POSIX ACL uapi format
920 * The uid stored as ACL_USER entry in @e is a kuid_t stored as a raw {g,u}id
921 * value. The vfs_set_acl_prepare_kuid() will recover the kuid_t through
922 * KUIDT_INIT() and then map it according to the idmapped mount. The resulting
923 * kuid_t is the value which the filesystem can map up into a raw backing store
924 * id in the filesystem's idmapping.
926 * This is used in vfs_set_acl_prepare() to generate the proper VFS
927 * representation of POSIX ACLs with ACL_USER entries during setxattr().
929 * Return: A kuid in @fs_userns for the uid stored in @e.
932 vfs_set_acl_prepare_kuid(struct user_namespace *mnt_userns,
933 struct user_namespace *fs_userns,
934 const struct posix_acl_xattr_entry *e)
936 kuid_t kuid = KUIDT_INIT(le32_to_cpu(e->e_id));
937 return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
941 * vfs_set_acl_prepare_kgid - map ACL_GROUP gid according to mount- and
942 * filesystem idmapping
943 * @mnt_userns: the mount's idmapping
944 * @fs_userns: the filesystem's idmapping
945 * @e: a ACL_GROUP entry in POSIX ACL uapi format
947 * The gid stored as ACL_GROUP entry in @e is a kgid_t stored as a raw {g,u}id
948 * value. The vfs_set_acl_prepare_kgid() will recover the kgid_t through
949 * KGIDT_INIT() and then map it according to the idmapped mount. The resulting
950 * kgid_t is the value which the filesystem can map up into a raw backing store
951 * id in the filesystem's idmapping.
953 * This is used in vfs_set_acl_prepare() to generate the proper VFS
954 * representation of POSIX ACLs with ACL_GROUP entries during setxattr().
956 * Return: A kgid in @fs_userns for the gid stored in @e.
959 vfs_set_acl_prepare_kgid(struct user_namespace *mnt_userns,
960 struct user_namespace *fs_userns,
961 const struct posix_acl_xattr_entry *e)
963 kgid_t kgid = KGIDT_INIT(le32_to_cpu(e->e_id));
964 return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
968 * vfs_set_acl_prepare - convert POSIX ACLs from uapi to VFS format taking
969 * mount and filesystem idmappings into account
970 * @mnt_userns: the mount's idmapping
971 * @fs_userns: the filesystem's idmapping
972 * @value: the uapi representation of POSIX ACLs
973 * @size: the size of @void
975 * When setting POSIX ACLs with ACL_{GROUP,USER} entries they need to be
976 * mapped according to the relevant mount- and filesystem idmapping. It is
977 * important that the ACL_{GROUP,USER} entries in struct posix_acl will be
978 * mapped into k{g,u}id_t that are supposed to be mapped up in the filesystem
979 * idmapping. This is crucial since the resulting struct posix_acl might be
980 * cached filesystem wide. The vfs_set_acl_prepare() function will take care to
981 * perform all necessary idmappings.
983 * Note, that since basically forever the {g,u}id values encoded as
984 * ACL_{GROUP,USER} entries in the uapi POSIX ACLs passed via @value contain
985 * values that have been mapped according to the caller's idmapping. In other
986 * words, POSIX ACLs passed in uapi format as @value during setxattr() contain
987 * {g,u}id values in their ACL_{GROUP,USER} entries that should actually have
988 * been stored as k{g,u}id_t.
990 * This means, vfs_set_acl_prepare() needs to first recover the k{g,u}id_t by
991 * calling K{G,U}IDT_INIT(). Afterwards they can be interpreted as vfs{g,u}id_t
992 * through from_vfs{g,u}id() to account for any idmapped mounts. The
993 * vfs_set_acl_prepare_k{g,u}id() helpers will take care to generate the
994 * correct k{g,u}id_t.
996 * The filesystem will then receive the POSIX ACLs ready to be cached
997 * filesystem wide and ready to be written to the backing store taking the
998 * filesystem's idmapping into account.
1000 * Return: Allocated struct posix_acl on success, NULL for a valid header but
1001 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
1003 struct posix_acl *vfs_set_acl_prepare(struct user_namespace *mnt_userns,
1004 struct user_namespace *fs_userns,
1005 const void *value, size_t size)
1007 return make_posix_acl(mnt_userns, fs_userns, value, size,
1008 vfs_set_acl_prepare_kuid,
1009 vfs_set_acl_prepare_kgid);
1011 EXPORT_SYMBOL(vfs_set_acl_prepare);
1014 * posix_acl_from_xattr_kuid - map ACL_USER uid into filesystem idmapping
1015 * @mnt_userns: unused
1016 * @fs_userns: the filesystem's idmapping
1017 * @e: a ACL_USER entry in POSIX ACL uapi format
1019 * Map the uid stored as ACL_USER entry in @e into the filesystem's idmapping.
1020 * This is used in posix_acl_from_xattr() to generate the proper VFS
1021 * representation of POSIX ACLs with ACL_USER entries.
1023 * Return: A kuid in @fs_userns for the uid stored in @e.
1025 static inline kuid_t
1026 posix_acl_from_xattr_kuid(struct user_namespace *mnt_userns,
1027 struct user_namespace *fs_userns,
1028 const struct posix_acl_xattr_entry *e)
1030 return make_kuid(fs_userns, le32_to_cpu(e->e_id));
1034 * posix_acl_from_xattr_kgid - map ACL_GROUP gid into filesystem idmapping
1035 * @mnt_userns: unused
1036 * @fs_userns: the filesystem's idmapping
1037 * @e: a ACL_GROUP entry in POSIX ACL uapi format
1039 * Map the gid stored as ACL_GROUP entry in @e into the filesystem's idmapping.
1040 * This is used in posix_acl_from_xattr() to generate the proper VFS
1041 * representation of POSIX ACLs with ACL_GROUP entries.
1043 * Return: A kgid in @fs_userns for the gid stored in @e.
1045 static inline kgid_t
1046 posix_acl_from_xattr_kgid(struct user_namespace *mnt_userns,
1047 struct user_namespace *fs_userns,
1048 const struct posix_acl_xattr_entry *e)
1050 return make_kgid(fs_userns, le32_to_cpu(e->e_id));
1054 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
1055 * @fs_userns: the filesystem's idmapping
1056 * @value: the uapi representation of POSIX ACLs
1057 * @size: the size of @void
1059 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
1060 * posix_acl_from_xattr() when reading them from the backing store and
1061 * converting them into the struct posix_acl VFS format. The helper is
1062 * specifically intended to be called from the ->get_inode_acl() inode
1065 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
1066 * in ACL_{GROUP,USER} entries into the filesystem idmapping in @fs_userns. The
1067 * posix_acl_from_xattr_k{g,u}id() helpers will take care to generate the
1068 * correct k{g,u}id_t. The returned struct posix_acl can be cached.
1070 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
1071 * If it did it calling is from the ->get_inode_acl() inode operation would
1072 * return POSIX ACLs mapped according to an idmapped mount which would mean
1073 * that the value couldn't be cached for the filesystem. Idmapped mounts are
1074 * taken into account on the fly during permission checking or right at the VFS
1075 * - userspace boundary before reporting them to the user.
1077 * Return: Allocated struct posix_acl on success, NULL for a valid header but
1078 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
1081 posix_acl_from_xattr(struct user_namespace *fs_userns,
1082 const void *value, size_t size)
1084 return make_posix_acl(&init_user_ns, fs_userns, value, size,
1085 posix_acl_from_xattr_kuid,
1086 posix_acl_from_xattr_kgid);
1088 EXPORT_SYMBOL (posix_acl_from_xattr);
1091 * Convert from in-memory to extended attribute representation.
1094 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
1095 void *buffer, size_t size)
1097 struct posix_acl_xattr_header *ext_acl = buffer;
1098 struct posix_acl_xattr_entry *ext_entry;
1101 real_size = posix_acl_xattr_size(acl->a_count);
1104 if (real_size > size)
1107 ext_entry = (void *)(ext_acl + 1);
1108 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
1110 for (n=0; n < acl->a_count; n++, ext_entry++) {
1111 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
1112 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
1113 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
1114 switch(acl_e->e_tag) {
1117 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
1121 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
1124 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1130 EXPORT_SYMBOL (posix_acl_to_xattr);
1133 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
1134 * @mnt_userns: user namespace of the mount
1135 * @inode: inode the posix acls are set on
1136 * @acl: the posix acls as represented by the vfs
1137 * @buffer: the buffer into which to convert @acl
1138 * @size: size of @buffer
1140 * This converts @acl from the VFS representation in the filesystem idmapping
1141 * to the uapi form reportable to userspace. And mount and caller idmappings
1142 * are handled appropriately.
1144 * Return: On success, the size of the stored uapi posix acls, on error a
1147 ssize_t vfs_posix_acl_to_xattr(struct user_namespace *mnt_userns,
1148 struct inode *inode, const struct posix_acl *acl,
1149 void *buffer, size_t size)
1152 struct posix_acl_xattr_header *ext_acl = buffer;
1153 struct posix_acl_xattr_entry *ext_entry;
1154 struct user_namespace *fs_userns, *caller_userns;
1155 ssize_t real_size, n;
1159 real_size = posix_acl_xattr_size(acl->a_count);
1162 if (real_size > size)
1165 ext_entry = (void *)(ext_acl + 1);
1166 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
1168 fs_userns = i_user_ns(inode);
1169 caller_userns = current_user_ns();
1170 for (n=0; n < acl->a_count; n++, ext_entry++) {
1171 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
1172 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
1173 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
1174 switch(acl_e->e_tag) {
1176 vfsuid = make_vfsuid(mnt_userns, fs_userns, acl_e->e_uid);
1177 ext_entry->e_id = cpu_to_le32(from_kuid(
1178 caller_userns, vfsuid_into_kuid(vfsuid)));
1181 vfsgid = make_vfsgid(mnt_userns, fs_userns, acl_e->e_gid);
1182 ext_entry->e_id = cpu_to_le32(from_kgid(
1183 caller_userns, vfsgid_into_kgid(vfsgid)));
1186 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1194 posix_acl_xattr_get(const struct xattr_handler *handler,
1195 struct dentry *unused, struct inode *inode,
1196 const char *name, void *value, size_t size)
1198 struct posix_acl *acl;
1201 if (!IS_POSIXACL(inode))
1203 if (S_ISLNK(inode->i_mode))
1206 acl = get_inode_acl(inode, handler->flags);
1208 return PTR_ERR(acl);
1212 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
1213 posix_acl_release(acl);
1219 set_posix_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
1220 int type, struct posix_acl *acl)
1222 struct inode *inode = d_inode(dentry);
1224 if (!IS_POSIXACL(inode))
1226 if (!inode->i_op->set_acl)
1229 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
1230 return acl ? -EACCES : 0;
1231 if (!inode_owner_or_capable(mnt_userns, inode))
1235 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
1239 return inode->i_op->set_acl(mnt_userns, dentry, acl, type);
1241 EXPORT_SYMBOL(set_posix_acl);
1244 posix_acl_xattr_set(const struct xattr_handler *handler,
1245 struct user_namespace *mnt_userns,
1246 struct dentry *dentry, struct inode *inode,
1247 const char *name, const void *value, size_t size,
1250 struct posix_acl *acl = NULL;
1255 * By the time we end up here the {g,u}ids stored in
1256 * ACL_{GROUP,USER} have already been mapped according to the
1257 * caller's idmapping. The vfs_set_acl_prepare() helper will
1258 * recover them and take idmapped mounts into account. The
1259 * filesystem will receive the POSIX ACLs in the correct
1260 * format ready to be cached or written to the backing store
1261 * taking the filesystem idmapping into account.
1263 acl = vfs_set_acl_prepare(mnt_userns, i_user_ns(inode),
1266 return PTR_ERR(acl);
1268 ret = set_posix_acl(mnt_userns, dentry, handler->flags, acl);
1269 posix_acl_release(acl);
1274 posix_acl_xattr_list(struct dentry *dentry)
1276 return IS_POSIXACL(d_backing_inode(dentry));
1279 const struct xattr_handler posix_acl_access_xattr_handler = {
1280 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1281 .flags = ACL_TYPE_ACCESS,
1282 .list = posix_acl_xattr_list,
1283 .get = posix_acl_xattr_get,
1284 .set = posix_acl_xattr_set,
1286 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
1288 const struct xattr_handler posix_acl_default_xattr_handler = {
1289 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1290 .flags = ACL_TYPE_DEFAULT,
1291 .list = posix_acl_xattr_list,
1292 .get = posix_acl_xattr_get,
1293 .set = posix_acl_xattr_set,
1295 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
1297 int simple_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
1298 struct posix_acl *acl, int type)
1301 struct inode *inode = d_inode(dentry);
1303 if (type == ACL_TYPE_ACCESS) {
1304 error = posix_acl_update_mode(mnt_userns, inode,
1305 &inode->i_mode, &acl);
1310 inode->i_ctime = current_time(inode);
1311 if (IS_I_VERSION(inode))
1312 inode_inc_iversion(inode);
1313 set_cached_acl(inode, type, acl);
1317 int simple_acl_create(struct inode *dir, struct inode *inode)
1319 struct posix_acl *default_acl, *acl;
1322 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1326 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1327 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1330 posix_acl_release(default_acl);
1332 posix_acl_release(acl);
1336 static int vfs_set_acl_idmapped_mnt(struct user_namespace *mnt_userns,
1337 struct user_namespace *fs_userns,
1338 struct posix_acl *acl)
1340 for (int n = 0; n < acl->a_count; n++) {
1341 struct posix_acl_entry *acl_e = &acl->a_entries[n];
1343 switch (acl_e->e_tag) {
1345 acl_e->e_uid = from_vfsuid(mnt_userns, fs_userns,
1346 VFSUIDT_INIT(acl_e->e_uid));
1349 acl_e->e_gid = from_vfsgid(mnt_userns, fs_userns,
1350 VFSGIDT_INIT(acl_e->e_gid));
1359 * vfs_set_acl - set posix acls
1360 * @mnt_userns: user namespace of the mount
1361 * @dentry: the dentry based on which to set the posix acls
1362 * @acl_name: the name of the posix acl
1363 * @kacl: the posix acls in the appropriate VFS format
1365 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1368 * Return: On success 0, on error negative errno.
1370 int vfs_set_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
1371 const char *acl_name, struct posix_acl *kacl)
1375 struct inode *inode = d_inode(dentry);
1376 struct inode *delegated_inode = NULL;
1378 acl_type = posix_acl_type(acl_name);
1384 * If we're on an idmapped mount translate from mount specific
1385 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1386 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1387 * if this is a filesystem with a backing store - ultimately
1388 * translate them to backing store values.
1390 error = vfs_set_acl_idmapped_mnt(mnt_userns, i_user_ns(inode), kacl);
1399 * We only care about restrictions the inode struct itself places upon
1400 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1402 error = may_write_xattr(mnt_userns, inode);
1404 goto out_inode_unlock;
1406 error = security_inode_set_acl(mnt_userns, dentry, acl_name, kacl);
1408 goto out_inode_unlock;
1410 error = try_break_deleg(inode, &delegated_inode);
1412 goto out_inode_unlock;
1414 if (inode->i_opflags & IOP_XATTR)
1415 error = set_posix_acl(mnt_userns, dentry, acl_type, kacl);
1416 else if (unlikely(is_bad_inode(inode)))
1419 error = -EOPNOTSUPP;
1421 fsnotify_xattr(dentry);
1422 evm_inode_post_set_acl(dentry, acl_name, kacl);
1426 inode_unlock(inode);
1428 if (delegated_inode) {
1429 error = break_deleg_wait(&delegated_inode);
1436 EXPORT_SYMBOL_GPL(vfs_set_acl);
1439 * vfs_get_acl - get posix acls
1440 * @mnt_userns: user namespace of the mount
1441 * @dentry: the dentry based on which to retrieve the posix acls
1442 * @acl_name: the name of the posix acl
1444 * This function retrieves @kacl from the filesystem. The caller must all
1445 * posix_acl_release() on @kacl.
1447 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1449 struct posix_acl *vfs_get_acl(struct user_namespace *mnt_userns,
1450 struct dentry *dentry, const char *acl_name)
1452 struct inode *inode = d_inode(dentry);
1453 struct posix_acl *acl;
1454 int acl_type, error;
1456 acl_type = posix_acl_type(acl_name);
1458 return ERR_PTR(-EINVAL);
1461 * The VFS has no restrictions on reading POSIX ACLs so calling
1462 * something like xattr_permission() isn't needed. Only LSMs get a say.
1464 error = security_inode_get_acl(mnt_userns, dentry, acl_name);
1466 return ERR_PTR(error);
1468 if (!IS_POSIXACL(inode))
1469 return ERR_PTR(-EOPNOTSUPP);
1470 if (S_ISLNK(inode->i_mode))
1471 return ERR_PTR(-EOPNOTSUPP);
1473 acl = __get_acl(mnt_userns, dentry, inode, acl_type);
1477 return ERR_PTR(-ENODATA);
1481 EXPORT_SYMBOL_GPL(vfs_get_acl);
1484 * vfs_remove_acl - remove posix acls
1485 * @mnt_userns: user namespace of the mount
1486 * @dentry: the dentry based on which to retrieve the posix acls
1487 * @acl_name: the name of the posix acl
1489 * This function removes posix acls.
1491 * Return: On success 0, on error negative errno.
1493 int vfs_remove_acl(struct user_namespace *mnt_userns, struct dentry *dentry,
1494 const char *acl_name)
1498 struct inode *inode = d_inode(dentry);
1499 struct inode *delegated_inode = NULL;
1501 acl_type = posix_acl_type(acl_name);
1509 * We only care about restrictions the inode struct itself places upon
1510 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1512 error = may_write_xattr(mnt_userns, inode);
1514 goto out_inode_unlock;
1516 error = security_inode_remove_acl(mnt_userns, dentry, acl_name);
1518 goto out_inode_unlock;
1520 error = try_break_deleg(inode, &delegated_inode);
1522 goto out_inode_unlock;
1524 if (inode->i_opflags & IOP_XATTR)
1525 error = set_posix_acl(mnt_userns, dentry, acl_type, NULL);
1526 else if (unlikely(is_bad_inode(inode)))
1529 error = -EOPNOTSUPP;
1531 fsnotify_xattr(dentry);
1532 evm_inode_post_remove_acl(mnt_userns, dentry, acl_name);
1536 inode_unlock(inode);
1538 if (delegated_inode) {
1539 error = break_deleg_wait(&delegated_inode);
1546 EXPORT_SYMBOL_GPL(vfs_remove_acl);