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>
29 static struct posix_acl **acl_by_type(struct inode *inode, int type)
34 case ACL_TYPE_DEFAULT:
35 return &inode->i_default_acl;
41 struct posix_acl *get_cached_acl(struct inode *inode, int type)
43 struct posix_acl **p = acl_by_type(inode, type);
44 struct posix_acl *acl;
48 acl = rcu_dereference(*p);
49 if (!acl || is_uncached_acl(acl) ||
50 refcount_inc_not_zero(&acl->a_refcount))
58 EXPORT_SYMBOL(get_cached_acl);
60 struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type)
62 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type));
64 if (acl == ACL_DONT_CACHE) {
65 struct posix_acl *ret;
67 ret = inode->i_op->get_acl(inode, type, LOOKUP_RCU);
74 EXPORT_SYMBOL(get_cached_acl_rcu);
76 void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl)
78 struct posix_acl **p = acl_by_type(inode, type);
79 struct posix_acl *old;
81 old = xchg(p, posix_acl_dup(acl));
82 if (!is_uncached_acl(old))
83 posix_acl_release(old);
85 EXPORT_SYMBOL(set_cached_acl);
87 static void __forget_cached_acl(struct posix_acl **p)
89 struct posix_acl *old;
91 old = xchg(p, ACL_NOT_CACHED);
92 if (!is_uncached_acl(old))
93 posix_acl_release(old);
96 void forget_cached_acl(struct inode *inode, int type)
98 __forget_cached_acl(acl_by_type(inode, type));
100 EXPORT_SYMBOL(forget_cached_acl);
102 void forget_all_cached_acls(struct inode *inode)
104 __forget_cached_acl(&inode->i_acl);
105 __forget_cached_acl(&inode->i_default_acl);
107 EXPORT_SYMBOL(forget_all_cached_acls);
109 struct posix_acl *get_acl(struct inode *inode, int type)
112 struct posix_acl **p;
113 struct posix_acl *acl;
116 * The sentinel is used to detect when another operation like
117 * set_cached_acl() or forget_cached_acl() races with get_acl().
118 * It is guaranteed that is_uncached_acl(sentinel) is true.
121 acl = get_cached_acl(inode, type);
122 if (!is_uncached_acl(acl))
125 if (!IS_POSIXACL(inode))
128 sentinel = uncached_acl_sentinel(current);
129 p = acl_by_type(inode, type);
132 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
133 * current value of the ACL will not be ACL_NOT_CACHED and so our own
134 * sentinel will not be set; another task will update the cache. We
135 * could wait for that other task to complete its job, but it's easier
136 * to just call ->get_acl to fetch the ACL ourself. (This is going to
137 * be an unlikely race.)
139 cmpxchg(p, ACL_NOT_CACHED, sentinel);
142 * Normally, the ACL returned by ->get_acl will be cached.
143 * A filesystem can prevent that by calling
144 * forget_cached_acl(inode, type) in ->get_acl.
146 * If the filesystem doesn't have a get_acl() function at all, we'll
147 * just create the negative cache entry.
149 if (!inode->i_op->get_acl) {
150 set_cached_acl(inode, type, NULL);
153 acl = inode->i_op->get_acl(inode, type, false);
157 * Remove our sentinel so that we don't block future attempts
160 cmpxchg(p, sentinel, ACL_NOT_CACHED);
165 * Cache the result, but only if our sentinel is still in place.
168 if (unlikely(cmpxchg(p, sentinel, acl) != sentinel))
169 posix_acl_release(acl);
172 EXPORT_SYMBOL(get_acl);
175 * Init a fresh posix_acl
178 posix_acl_init(struct posix_acl *acl, int count)
180 refcount_set(&acl->a_refcount, 1);
181 acl->a_count = count;
183 EXPORT_SYMBOL(posix_acl_init);
186 * Allocate a new ACL with the specified number of entries.
189 posix_acl_alloc(int count, gfp_t flags)
191 const size_t size = sizeof(struct posix_acl) +
192 count * sizeof(struct posix_acl_entry);
193 struct posix_acl *acl = kmalloc(size, flags);
195 posix_acl_init(acl, count);
198 EXPORT_SYMBOL(posix_acl_alloc);
204 posix_acl_clone(const struct posix_acl *acl, gfp_t flags)
206 struct posix_acl *clone = NULL;
209 int size = sizeof(struct posix_acl) + acl->a_count *
210 sizeof(struct posix_acl_entry);
211 clone = kmemdup(acl, size, flags);
213 refcount_set(&clone->a_refcount, 1);
217 EXPORT_SYMBOL_GPL(posix_acl_clone);
220 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
223 posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl)
225 const struct posix_acl_entry *pa, *pe;
226 int state = ACL_USER_OBJ;
229 FOREACH_ACL_ENTRY(pa, acl, pe) {
230 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE))
234 if (state == ACL_USER_OBJ) {
241 if (state != ACL_USER)
243 if (!kuid_has_mapping(user_ns, pa->e_uid))
249 if (state == ACL_USER) {
256 if (state != ACL_GROUP)
258 if (!kgid_has_mapping(user_ns, pa->e_gid))
264 if (state != ACL_GROUP)
270 if (state == ACL_OTHER ||
271 (state == ACL_GROUP && !needs_mask)) {
285 EXPORT_SYMBOL(posix_acl_valid);
288 * Returns 0 if the acl can be exactly represented in the traditional
289 * file mode permission bits, or else 1. Returns -E... on error.
292 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p)
294 const struct posix_acl_entry *pa, *pe;
299 * A null ACL can always be presented as mode bits.
304 FOREACH_ACL_ENTRY(pa, acl, pe) {
307 mode |= (pa->e_perm & S_IRWXO) << 6;
310 mode |= (pa->e_perm & S_IRWXO) << 3;
313 mode |= pa->e_perm & S_IRWXO;
316 mode = (mode & ~S_IRWXG) |
317 ((pa->e_perm & S_IRWXO) << 3);
329 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
332 EXPORT_SYMBOL(posix_acl_equiv_mode);
335 * Create an ACL representing the file mode permission bits of an inode.
338 posix_acl_from_mode(umode_t mode, gfp_t flags)
340 struct posix_acl *acl = posix_acl_alloc(3, flags);
342 return ERR_PTR(-ENOMEM);
344 acl->a_entries[0].e_tag = ACL_USER_OBJ;
345 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6;
347 acl->a_entries[1].e_tag = ACL_GROUP_OBJ;
348 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3;
350 acl->a_entries[2].e_tag = ACL_OTHER;
351 acl->a_entries[2].e_perm = (mode & S_IRWXO);
354 EXPORT_SYMBOL(posix_acl_from_mode);
357 * Return 0 if current is granted want access to the inode
358 * by the acl. Returns -E... otherwise.
361 posix_acl_permission(struct user_namespace *mnt_userns, struct inode *inode,
362 const struct posix_acl *acl, int want)
364 const struct posix_acl_entry *pa, *pe, *mask_obj;
365 struct user_namespace *fs_userns = i_user_ns(inode);
370 want &= MAY_READ | MAY_WRITE | MAY_EXEC;
372 FOREACH_ACL_ENTRY(pa, acl, pe) {
375 /* (May have been checked already) */
376 vfsuid = i_uid_into_vfsuid(mnt_userns, inode);
377 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
381 vfsuid = make_vfsuid(mnt_userns, fs_userns,
383 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
387 vfsgid = i_gid_into_vfsgid(mnt_userns, inode);
388 if (vfsgid_in_group_p(vfsgid)) {
390 if ((pa->e_perm & want) == want)
395 vfsgid = make_vfsgid(mnt_userns, fs_userns,
397 if (vfsgid_in_group_p(vfsgid)) {
399 if ((pa->e_perm & want) == want)
417 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) {
418 if (mask_obj->e_tag == ACL_MASK) {
419 if ((pa->e_perm & mask_obj->e_perm & want) == want)
426 if ((pa->e_perm & want) == want)
432 * Modify acl when creating a new inode. The caller must ensure the acl is
433 * only referenced once.
435 * mode_p initially must contain the mode parameter to the open() / creat()
436 * system calls. All permissions that are not granted by the acl are removed.
437 * The permissions in the acl are changed to reflect the mode_p parameter.
439 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
441 struct posix_acl_entry *pa, *pe;
442 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
443 umode_t mode = *mode_p;
446 /* assert(atomic_read(acl->a_refcount) == 1); */
448 FOREACH_ACL_ENTRY(pa, acl, pe) {
451 pa->e_perm &= (mode >> 6) | ~S_IRWXO;
452 mode &= (pa->e_perm << 6) | ~S_IRWXU;
465 pa->e_perm &= mode | ~S_IRWXO;
466 mode &= pa->e_perm | ~S_IRWXO;
480 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
481 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
485 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
486 mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
489 *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
494 * Modify the ACL for the chmod syscall.
496 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode)
498 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
499 struct posix_acl_entry *pa, *pe;
501 /* assert(atomic_read(acl->a_refcount) == 1); */
503 FOREACH_ACL_ENTRY(pa, acl, pe) {
506 pa->e_perm = (mode & S_IRWXU) >> 6;
522 pa->e_perm = (mode & S_IRWXO);
531 mask_obj->e_perm = (mode & S_IRWXG) >> 3;
535 group_obj->e_perm = (mode & S_IRWXG) >> 3;
542 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p)
544 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
547 err = posix_acl_create_masq(clone, mode_p);
549 posix_acl_release(clone);
553 posix_acl_release(*acl);
557 EXPORT_SYMBOL(__posix_acl_create);
560 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode)
562 struct posix_acl *clone = posix_acl_clone(*acl, gfp);
565 err = __posix_acl_chmod_masq(clone, mode);
567 posix_acl_release(clone);
571 posix_acl_release(*acl);
575 EXPORT_SYMBOL(__posix_acl_chmod);
578 * posix_acl_chmod - chmod a posix acl
580 * @mnt_userns: user namespace of the mount @inode was found from
581 * @inode: inode to check permissions on
582 * @mode: the new mode of @inode
584 * If the inode has been found through an idmapped mount the user namespace of
585 * the vfsmount must be passed through @mnt_userns. This function will then
586 * take care to map the inode according to @mnt_userns before checking
587 * permissions. On non-idmapped mounts or if permission checking is to be
588 * performed on the raw inode simply passs init_user_ns.
591 posix_acl_chmod(struct user_namespace *mnt_userns, struct inode *inode,
594 struct posix_acl *acl;
597 if (!IS_POSIXACL(inode))
599 if (!inode->i_op->set_acl)
602 acl = get_acl(inode, ACL_TYPE_ACCESS);
603 if (IS_ERR_OR_NULL(acl)) {
604 if (acl == ERR_PTR(-EOPNOTSUPP))
609 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
612 ret = inode->i_op->set_acl(mnt_userns, inode, acl, ACL_TYPE_ACCESS);
613 posix_acl_release(acl);
616 EXPORT_SYMBOL(posix_acl_chmod);
619 posix_acl_create(struct inode *dir, umode_t *mode,
620 struct posix_acl **default_acl, struct posix_acl **acl)
623 struct posix_acl *clone;
629 if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
632 p = get_acl(dir, ACL_TYPE_DEFAULT);
633 if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
634 *mode &= ~current_umask();
641 clone = posix_acl_clone(p, GFP_NOFS);
645 ret = posix_acl_create_masq(clone, mode);
647 goto err_release_clone;
650 posix_acl_release(clone);
655 posix_acl_release(p);
662 posix_acl_release(clone);
664 posix_acl_release(p);
667 EXPORT_SYMBOL_GPL(posix_acl_create);
670 * posix_acl_update_mode - update mode in set_acl
671 * @mnt_userns: user namespace of the mount @inode was found from
672 * @inode: target inode
673 * @mode_p: mode (pointer) for update
676 * Update the file mode when setting an ACL: compute the new file permission
677 * bits based on the ACL. In addition, if the ACL is equivalent to the new
678 * file mode, set *@acl to NULL to indicate that no ACL should be set.
680 * As with chmod, clear the setgid bit if the caller is not in the owning group
681 * or capable of CAP_FSETID (see inode_change_ok).
683 * If the inode has been found through an idmapped mount the user namespace of
684 * the vfsmount must be passed through @mnt_userns. This function will then
685 * take care to map the inode according to @mnt_userns before checking
686 * permissions. On non-idmapped mounts or if permission checking is to be
687 * performed on the raw inode simply passs init_user_ns.
689 * Called from set_acl inode operations.
691 int posix_acl_update_mode(struct user_namespace *mnt_userns,
692 struct inode *inode, umode_t *mode_p,
693 struct posix_acl **acl)
695 umode_t mode = inode->i_mode;
698 error = posix_acl_equiv_mode(*acl, &mode);
703 if (!vfsgid_in_group_p(i_gid_into_vfsgid(mnt_userns, inode)) &&
704 !capable_wrt_inode_uidgid(mnt_userns, inode, CAP_FSETID))
709 EXPORT_SYMBOL(posix_acl_update_mode);
712 * Fix up the uids and gids in posix acl extended attributes in place.
714 static int posix_acl_fix_xattr_common(const void *value, size_t size)
716 const struct posix_acl_xattr_header *header = value;
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);
735 void posix_acl_getxattr_idmapped_mnt(struct user_namespace *mnt_userns,
736 const struct inode *inode,
737 void *value, size_t size)
739 struct posix_acl_xattr_header *header = value;
740 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
741 struct user_namespace *fs_userns = i_user_ns(inode);
748 if (no_idmapping(mnt_userns, i_user_ns(inode)))
751 count = posix_acl_fix_xattr_common(value, size);
755 for (end = entry + count; entry != end; entry++) {
756 switch (le16_to_cpu(entry->e_tag)) {
758 uid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
759 vfsuid = make_vfsuid(mnt_userns, fs_userns, uid);
760 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns,
761 vfsuid_into_kuid(vfsuid)));
764 gid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
765 vfsgid = make_vfsgid(mnt_userns, fs_userns, gid);
766 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns,
767 vfsgid_into_kgid(vfsgid)));
775 static void posix_acl_fix_xattr_userns(
776 struct user_namespace *to, struct user_namespace *from,
777 void *value, size_t size)
779 struct posix_acl_xattr_header *header = value;
780 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
785 count = posix_acl_fix_xattr_common(value, size);
789 for (end = entry + count; entry != end; entry++) {
790 switch(le16_to_cpu(entry->e_tag)) {
792 uid = make_kuid(from, le32_to_cpu(entry->e_id));
793 entry->e_id = cpu_to_le32(from_kuid(to, uid));
796 gid = make_kgid(from, le32_to_cpu(entry->e_id));
797 entry->e_id = cpu_to_le32(from_kgid(to, gid));
805 void posix_acl_fix_xattr_from_user(void *value, size_t size)
807 struct user_namespace *user_ns = current_user_ns();
808 if (user_ns == &init_user_ns)
810 posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size);
813 void posix_acl_fix_xattr_to_user(void *value, size_t size)
815 struct user_namespace *user_ns = current_user_ns();
816 if (user_ns == &init_user_ns)
818 posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size);
822 * make_posix_acl - convert POSIX ACLs from uapi to VFS format using the
823 * provided callbacks to map ACL_{GROUP,USER} entries into the
825 * @mnt_userns: the mount's idmapping
826 * @fs_userns: the filesystem's idmapping
827 * @value: the uapi representation of POSIX ACLs
828 * @size: the size of @void
829 * @uid_cb: callback to use for mapping the uid stored in ACL_USER entries
830 * @gid_cb: callback to use for mapping the gid stored in ACL_GROUP entries
832 * The make_posix_acl() helper is an abstraction to translate from uapi format
833 * into the VFS format allowing the caller to specific callbacks to map
834 * ACL_{GROUP,USER} entries into the expected format. This is used in
835 * posix_acl_from_xattr() and vfs_set_acl_prepare() and avoids pointless code
838 * Return: Allocated struct posix_acl on success, NULL for a valid header but
839 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
841 static struct posix_acl *make_posix_acl(struct user_namespace *mnt_userns,
842 struct user_namespace *fs_userns, const void *value, size_t size,
843 kuid_t (*uid_cb)(struct user_namespace *, struct user_namespace *,
844 const struct posix_acl_xattr_entry *),
845 kgid_t (*gid_cb)(struct user_namespace *, struct user_namespace *,
846 const struct posix_acl_xattr_entry *))
848 const struct posix_acl_xattr_header *header = value;
849 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
851 struct posix_acl *acl;
852 struct posix_acl_entry *acl_e;
854 count = posix_acl_fix_xattr_common(value, size);
856 return ERR_PTR(count);
860 acl = posix_acl_alloc(count, GFP_NOFS);
862 return ERR_PTR(-ENOMEM);
863 acl_e = acl->a_entries;
865 for (end = entry + count; entry != end; acl_e++, entry++) {
866 acl_e->e_tag = le16_to_cpu(entry->e_tag);
867 acl_e->e_perm = le16_to_cpu(entry->e_perm);
869 switch(acl_e->e_tag) {
877 acl_e->e_uid = uid_cb(mnt_userns, fs_userns, entry);
878 if (!uid_valid(acl_e->e_uid))
882 acl_e->e_gid = gid_cb(mnt_userns, fs_userns, entry);
883 if (!gid_valid(acl_e->e_gid))
894 posix_acl_release(acl);
895 return ERR_PTR(-EINVAL);
899 * vfs_set_acl_prepare_kuid - map ACL_USER uid according to mount- and
900 * filesystem idmapping
901 * @mnt_userns: the mount's idmapping
902 * @fs_userns: the filesystem's idmapping
903 * @e: a ACL_USER entry in POSIX ACL uapi format
905 * The uid stored as ACL_USER entry in @e is a kuid_t stored as a raw {g,u}id
906 * value. The vfs_set_acl_prepare_kuid() will recover the kuid_t through
907 * KUIDT_INIT() and then map it according to the idmapped mount. The resulting
908 * kuid_t is the value which the filesystem can map up into a raw backing store
909 * id in the filesystem's idmapping.
911 * This is used in vfs_set_acl_prepare() to generate the proper VFS
912 * representation of POSIX ACLs with ACL_USER entries during setxattr().
914 * Return: A kuid in @fs_userns for the uid stored in @e.
917 vfs_set_acl_prepare_kuid(struct user_namespace *mnt_userns,
918 struct user_namespace *fs_userns,
919 const struct posix_acl_xattr_entry *e)
921 kuid_t kuid = KUIDT_INIT(le32_to_cpu(e->e_id));
922 return from_vfsuid(mnt_userns, fs_userns, VFSUIDT_INIT(kuid));
926 * vfs_set_acl_prepare_kgid - map ACL_GROUP gid according to mount- and
927 * filesystem idmapping
928 * @mnt_userns: the mount's idmapping
929 * @fs_userns: the filesystem's idmapping
930 * @e: a ACL_GROUP entry in POSIX ACL uapi format
932 * The gid stored as ACL_GROUP entry in @e is a kgid_t stored as a raw {g,u}id
933 * value. The vfs_set_acl_prepare_kgid() will recover the kgid_t through
934 * KGIDT_INIT() and then map it according to the idmapped mount. The resulting
935 * kgid_t is the value which the filesystem can map up into a raw backing store
936 * id in the filesystem's idmapping.
938 * This is used in vfs_set_acl_prepare() to generate the proper VFS
939 * representation of POSIX ACLs with ACL_GROUP entries during setxattr().
941 * Return: A kgid in @fs_userns for the gid stored in @e.
944 vfs_set_acl_prepare_kgid(struct user_namespace *mnt_userns,
945 struct user_namespace *fs_userns,
946 const struct posix_acl_xattr_entry *e)
948 kgid_t kgid = KGIDT_INIT(le32_to_cpu(e->e_id));
949 return from_vfsgid(mnt_userns, fs_userns, VFSGIDT_INIT(kgid));
953 * vfs_set_acl_prepare - convert POSIX ACLs from uapi to VFS format taking
954 * mount and filesystem idmappings into account
955 * @mnt_userns: the mount's idmapping
956 * @fs_userns: the filesystem's idmapping
957 * @value: the uapi representation of POSIX ACLs
958 * @size: the size of @void
960 * When setting POSIX ACLs with ACL_{GROUP,USER} entries they need to be
961 * mapped according to the relevant mount- and filesystem idmapping. It is
962 * important that the ACL_{GROUP,USER} entries in struct posix_acl will be
963 * mapped into k{g,u}id_t that are supposed to be mapped up in the filesystem
964 * idmapping. This is crucial since the resulting struct posix_acl might be
965 * cached filesystem wide. The vfs_set_acl_prepare() function will take care to
966 * perform all necessary idmappings.
968 * Note, that since basically forever the {g,u}id values encoded as
969 * ACL_{GROUP,USER} entries in the uapi POSIX ACLs passed via @value contain
970 * values that have been mapped according to the caller's idmapping. In other
971 * words, POSIX ACLs passed in uapi format as @value during setxattr() contain
972 * {g,u}id values in their ACL_{GROUP,USER} entries that should actually have
973 * been stored as k{g,u}id_t.
975 * This means, vfs_set_acl_prepare() needs to first recover the k{g,u}id_t by
976 * calling K{G,U}IDT_INIT(). Afterwards they can be interpreted as vfs{g,u}id_t
977 * through from_vfs{g,u}id() to account for any idmapped mounts. The
978 * vfs_set_acl_prepare_k{g,u}id() helpers will take care to generate the
979 * correct k{g,u}id_t.
981 * The filesystem will then receive the POSIX ACLs ready to be cached
982 * filesystem wide and ready to be written to the backing store taking the
983 * filesystem's idmapping into account.
985 * Return: Allocated struct posix_acl on success, NULL for a valid header but
986 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
988 struct posix_acl *vfs_set_acl_prepare(struct user_namespace *mnt_userns,
989 struct user_namespace *fs_userns,
990 const void *value, size_t size)
992 return make_posix_acl(mnt_userns, fs_userns, value, size,
993 vfs_set_acl_prepare_kuid,
994 vfs_set_acl_prepare_kgid);
996 EXPORT_SYMBOL(vfs_set_acl_prepare);
999 * posix_acl_from_xattr_kuid - map ACL_USER uid into filesystem idmapping
1000 * @mnt_userns: unused
1001 * @fs_userns: the filesystem's idmapping
1002 * @e: a ACL_USER entry in POSIX ACL uapi format
1004 * Map the uid stored as ACL_USER entry in @e into the filesystem's idmapping.
1005 * This is used in posix_acl_from_xattr() to generate the proper VFS
1006 * representation of POSIX ACLs with ACL_USER entries.
1008 * Return: A kuid in @fs_userns for the uid stored in @e.
1010 static inline kuid_t
1011 posix_acl_from_xattr_kuid(struct user_namespace *mnt_userns,
1012 struct user_namespace *fs_userns,
1013 const struct posix_acl_xattr_entry *e)
1015 return make_kuid(fs_userns, le32_to_cpu(e->e_id));
1019 * posix_acl_from_xattr_kgid - map ACL_GROUP gid into filesystem idmapping
1020 * @mnt_userns: unused
1021 * @fs_userns: the filesystem's idmapping
1022 * @e: a ACL_GROUP entry in POSIX ACL uapi format
1024 * Map the gid stored as ACL_GROUP entry in @e into the filesystem's idmapping.
1025 * This is used in posix_acl_from_xattr() to generate the proper VFS
1026 * representation of POSIX ACLs with ACL_GROUP entries.
1028 * Return: A kgid in @fs_userns for the gid stored in @e.
1030 static inline kgid_t
1031 posix_acl_from_xattr_kgid(struct user_namespace *mnt_userns,
1032 struct user_namespace *fs_userns,
1033 const struct posix_acl_xattr_entry *e)
1035 return make_kgid(fs_userns, le32_to_cpu(e->e_id));
1039 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
1040 * @fs_userns: the filesystem's idmapping
1041 * @value: the uapi representation of POSIX ACLs
1042 * @size: the size of @void
1044 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
1045 * posix_acl_from_xattr() when reading them from the backing store and
1046 * converting them into the struct posix_acl VFS format. The helper is
1047 * specifically intended to be called from the ->get_acl() inode operation.
1049 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
1050 * in ACL_{GROUP,USER} entries into the filesystem idmapping in @fs_userns. The
1051 * posix_acl_from_xattr_k{g,u}id() helpers will take care to generate the
1052 * correct k{g,u}id_t. The returned struct posix_acl can be cached.
1054 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
1055 * If it did it calling is from the ->get_acl() inode operation would return
1056 * POSIX ACLs mapped according to an idmapped mount which would mean that the
1057 * value couldn't be cached for the filesystem. Idmapped mounts are taken into
1058 * account on the fly during permission checking or right at the VFS -
1059 * userspace boundary before reporting them to the user.
1061 * Return: Allocated struct posix_acl on success, NULL for a valid header but
1062 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
1065 posix_acl_from_xattr(struct user_namespace *fs_userns,
1066 const void *value, size_t size)
1068 return make_posix_acl(&init_user_ns, fs_userns, value, size,
1069 posix_acl_from_xattr_kuid,
1070 posix_acl_from_xattr_kgid);
1072 EXPORT_SYMBOL (posix_acl_from_xattr);
1075 * Convert from in-memory to extended attribute representation.
1078 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
1079 void *buffer, size_t size)
1081 struct posix_acl_xattr_header *ext_acl = buffer;
1082 struct posix_acl_xattr_entry *ext_entry;
1085 real_size = posix_acl_xattr_size(acl->a_count);
1088 if (real_size > size)
1091 ext_entry = (void *)(ext_acl + 1);
1092 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
1094 for (n=0; n < acl->a_count; n++, ext_entry++) {
1095 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
1096 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
1097 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
1098 switch(acl_e->e_tag) {
1101 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
1105 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
1108 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
1114 EXPORT_SYMBOL (posix_acl_to_xattr);
1117 posix_acl_xattr_get(const struct xattr_handler *handler,
1118 struct dentry *unused, struct inode *inode,
1119 const char *name, void *value, size_t size)
1121 struct posix_acl *acl;
1124 if (!IS_POSIXACL(inode))
1126 if (S_ISLNK(inode->i_mode))
1129 acl = get_acl(inode, handler->flags);
1131 return PTR_ERR(acl);
1135 error = posix_acl_to_xattr(&init_user_ns, acl, value, size);
1136 posix_acl_release(acl);
1142 set_posix_acl(struct user_namespace *mnt_userns, struct inode *inode,
1143 int type, struct posix_acl *acl)
1145 if (!IS_POSIXACL(inode))
1147 if (!inode->i_op->set_acl)
1150 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
1151 return acl ? -EACCES : 0;
1152 if (!inode_owner_or_capable(mnt_userns, inode))
1156 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
1160 return inode->i_op->set_acl(mnt_userns, inode, acl, type);
1162 EXPORT_SYMBOL(set_posix_acl);
1165 posix_acl_xattr_set(const struct xattr_handler *handler,
1166 struct user_namespace *mnt_userns,
1167 struct dentry *unused, struct inode *inode,
1168 const char *name, const void *value, size_t size,
1171 struct posix_acl *acl = NULL;
1176 * By the time we end up here the {g,u}ids stored in
1177 * ACL_{GROUP,USER} have already been mapped according to the
1178 * caller's idmapping. The vfs_set_acl_prepare() helper will
1179 * recover them and take idmapped mounts into account. The
1180 * filesystem will receive the POSIX ACLs in the correct
1181 * format ready to be cached or written to the backing store
1182 * taking the filesystem idmapping into account.
1184 acl = vfs_set_acl_prepare(mnt_userns, i_user_ns(inode),
1187 return PTR_ERR(acl);
1189 ret = set_posix_acl(mnt_userns, inode, handler->flags, acl);
1190 posix_acl_release(acl);
1195 posix_acl_xattr_list(struct dentry *dentry)
1197 return IS_POSIXACL(d_backing_inode(dentry));
1200 const struct xattr_handler posix_acl_access_xattr_handler = {
1201 .name = XATTR_NAME_POSIX_ACL_ACCESS,
1202 .flags = ACL_TYPE_ACCESS,
1203 .list = posix_acl_xattr_list,
1204 .get = posix_acl_xattr_get,
1205 .set = posix_acl_xattr_set,
1207 EXPORT_SYMBOL_GPL(posix_acl_access_xattr_handler);
1209 const struct xattr_handler posix_acl_default_xattr_handler = {
1210 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1211 .flags = ACL_TYPE_DEFAULT,
1212 .list = posix_acl_xattr_list,
1213 .get = posix_acl_xattr_get,
1214 .set = posix_acl_xattr_set,
1216 EXPORT_SYMBOL_GPL(posix_acl_default_xattr_handler);
1218 int simple_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
1219 struct posix_acl *acl, int type)
1223 if (type == ACL_TYPE_ACCESS) {
1224 error = posix_acl_update_mode(mnt_userns, inode,
1225 &inode->i_mode, &acl);
1230 inode->i_ctime = current_time(inode);
1231 if (IS_I_VERSION(inode))
1232 inode_inc_iversion(inode);
1233 set_cached_acl(inode, type, acl);
1237 int simple_acl_create(struct inode *dir, struct inode *inode)
1239 struct posix_acl *default_acl, *acl;
1242 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1246 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1247 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1250 posix_acl_release(default_acl);
1252 posix_acl_release(acl);