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>
26 #include <linux/mnt_idmapping.h>
27 #include <linux/iversion.h>
28 #include <linux/security.h>
29 #include <linux/fsnotify.h>
30 #include <linux/filelock.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 mnt_idmap *idmap,
115 struct dentry *dentry, struct inode *inode,
118 struct posix_acl *sentinel;
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(idmap, 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(!try_cmpxchg(p, &sentinel, acl)))
178 posix_acl_release(acl);
182 struct posix_acl *get_inode_acl(struct inode *inode, int type)
184 return __get_acl(&nop_mnt_idmap, 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 mnt_idmap *idmap, 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(idmap, inode);
391 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
395 vfsuid = make_vfsuid(idmap, fs_userns,
397 if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
401 vfsgid = i_gid_into_vfsgid(idmap, inode);
402 if (vfsgid_in_group_p(vfsgid)) {
404 if ((pa->e_perm & want) == want)
409 vfsgid = make_vfsgid(idmap, 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 * @idmap: idmap 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 idmap of
599 * the vfsmount must be passed through @idmap. This function will then
600 * take care to map the inode according to @idmap before checking
601 * permissions. On non-idmapped mounts or if permission checking is to be
602 * performed on the raw inode simply pass @nop_mnt_idmap.
605 posix_acl_chmod(struct mnt_idmap *idmap, 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(idmap, 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 * @idmap: idmap 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 idmap of
699 * the vfsmount must be passed through @idmap. This function will then
700 * take care to map the inode according to @idmap before checking
701 * permissions. On non-idmapped mounts or if permission checking is to be
702 * performed on the raw inode simply pass @nop_mnt_idmap.
704 * Called from set_acl inode operations.
706 int posix_acl_update_mode(struct mnt_idmap *idmap,
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(idmap, inode)) &&
719 !capable_wrt_inode_uidgid(idmap, 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);
751 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
752 * @userns: the filesystem's idmapping
753 * @value: the uapi representation of POSIX ACLs
754 * @size: the size of @void
756 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
757 * posix_acl_from_xattr() when reading them from the backing store and
758 * converting them into the struct posix_acl VFS format. The helper is
759 * specifically intended to be called from the acl inode operation.
761 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
762 * in ACL_{GROUP,USER} entries into idmapping in @userns.
764 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
765 * If it did it calling it from the get acl inode operation would return POSIX
766 * ACLs mapped according to an idmapped mount which would mean that the value
767 * couldn't be cached for the filesystem. Idmapped mounts are taken into
768 * account on the fly during permission checking or right at the VFS -
769 * userspace boundary before reporting them to the user.
771 * Return: Allocated struct posix_acl on success, NULL for a valid header but
772 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
774 struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns,
775 const void *value, size_t size)
777 const struct posix_acl_xattr_header *header = value;
778 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end;
780 struct posix_acl *acl;
781 struct posix_acl_entry *acl_e;
783 count = posix_acl_fix_xattr_common(value, size);
785 return ERR_PTR(count);
789 acl = posix_acl_alloc(count, GFP_NOFS);
791 return ERR_PTR(-ENOMEM);
792 acl_e = acl->a_entries;
794 for (end = entry + count; entry != end; acl_e++, entry++) {
795 acl_e->e_tag = le16_to_cpu(entry->e_tag);
796 acl_e->e_perm = le16_to_cpu(entry->e_perm);
798 switch(acl_e->e_tag) {
806 acl_e->e_uid = make_kuid(userns,
807 le32_to_cpu(entry->e_id));
808 if (!uid_valid(acl_e->e_uid))
812 acl_e->e_gid = make_kgid(userns,
813 le32_to_cpu(entry->e_id));
814 if (!gid_valid(acl_e->e_gid))
825 posix_acl_release(acl);
826 return ERR_PTR(-EINVAL);
828 EXPORT_SYMBOL (posix_acl_from_xattr);
831 * Convert from in-memory to extended attribute representation.
834 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl,
835 void *buffer, size_t size)
837 struct posix_acl_xattr_header *ext_acl = buffer;
838 struct posix_acl_xattr_entry *ext_entry;
841 real_size = posix_acl_xattr_size(acl->a_count);
844 if (real_size > size)
847 ext_entry = (void *)(ext_acl + 1);
848 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
850 for (n=0; n < acl->a_count; n++, ext_entry++) {
851 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
852 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
853 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
854 switch(acl_e->e_tag) {
857 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid));
861 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid));
864 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
870 EXPORT_SYMBOL (posix_acl_to_xattr);
873 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
874 * @idmap: idmap of the mount
875 * @inode: inode the posix acls are set on
876 * @acl: the posix acls as represented by the vfs
877 * @buffer: the buffer into which to convert @acl
878 * @size: size of @buffer
880 * This converts @acl from the VFS representation in the filesystem idmapping
881 * to the uapi form reportable to userspace. And mount and caller idmappings
882 * are handled appropriately.
884 * Return: On success, the size of the stored uapi posix acls, on error a
887 static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap,
889 const struct posix_acl *acl, void *buffer,
893 struct posix_acl_xattr_header *ext_acl = buffer;
894 struct posix_acl_xattr_entry *ext_entry;
895 struct user_namespace *fs_userns, *caller_userns;
896 ssize_t real_size, n;
900 real_size = posix_acl_xattr_size(acl->a_count);
903 if (real_size > size)
906 ext_entry = (void *)(ext_acl + 1);
907 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION);
909 fs_userns = i_user_ns(inode);
910 caller_userns = current_user_ns();
911 for (n=0; n < acl->a_count; n++, ext_entry++) {
912 const struct posix_acl_entry *acl_e = &acl->a_entries[n];
913 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag);
914 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm);
915 switch(acl_e->e_tag) {
917 vfsuid = make_vfsuid(idmap, fs_userns, acl_e->e_uid);
918 ext_entry->e_id = cpu_to_le32(from_kuid(
919 caller_userns, vfsuid_into_kuid(vfsuid)));
922 vfsgid = make_vfsgid(idmap, fs_userns, acl_e->e_gid);
923 ext_entry->e_id = cpu_to_le32(from_kgid(
924 caller_userns, vfsgid_into_kgid(vfsgid)));
927 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
935 set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry,
936 int type, struct posix_acl *acl)
938 struct inode *inode = d_inode(dentry);
940 if (!IS_POSIXACL(inode))
942 if (!inode->i_op->set_acl)
945 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
946 return acl ? -EACCES : 0;
947 if (!inode_owner_or_capable(idmap, inode))
951 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl);
955 return inode->i_op->set_acl(idmap, dentry, acl, type);
957 EXPORT_SYMBOL(set_posix_acl);
959 int posix_acl_listxattr(struct inode *inode, char **buffer,
960 ssize_t *remaining_size)
964 if (!IS_POSIXACL(inode))
968 err = xattr_list_one(buffer, remaining_size,
969 XATTR_NAME_POSIX_ACL_ACCESS);
974 if (inode->i_default_acl) {
975 err = xattr_list_one(buffer, remaining_size,
976 XATTR_NAME_POSIX_ACL_DEFAULT);
985 posix_acl_xattr_list(struct dentry *dentry)
987 return IS_POSIXACL(d_backing_inode(dentry));
991 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
993 * This is the legacy POSIX ACL access xattr handler. It is used by some
994 * filesystems to implement their ->listxattr() inode operation. New code
995 * should never use them.
997 const struct xattr_handler nop_posix_acl_access = {
998 .name = XATTR_NAME_POSIX_ACL_ACCESS,
999 .list = posix_acl_xattr_list,
1001 EXPORT_SYMBOL_GPL(nop_posix_acl_access);
1004 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
1006 * This is the legacy POSIX ACL default xattr handler. It is used by some
1007 * filesystems to implement their ->listxattr() inode operation. New code
1008 * should never use them.
1010 const struct xattr_handler nop_posix_acl_default = {
1011 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
1012 .list = posix_acl_xattr_list,
1014 EXPORT_SYMBOL_GPL(nop_posix_acl_default);
1016 int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1017 struct posix_acl *acl, int type)
1020 struct inode *inode = d_inode(dentry);
1022 if (type == ACL_TYPE_ACCESS) {
1023 error = posix_acl_update_mode(idmap, inode,
1024 &inode->i_mode, &acl);
1029 inode_set_ctime_current(inode);
1030 if (IS_I_VERSION(inode))
1031 inode_inc_iversion(inode);
1032 set_cached_acl(inode, type, acl);
1036 int simple_acl_create(struct inode *dir, struct inode *inode)
1038 struct posix_acl *default_acl, *acl;
1041 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
1045 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl);
1046 set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
1049 posix_acl_release(default_acl);
1051 posix_acl_release(acl);
1055 static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap,
1056 struct user_namespace *fs_userns,
1057 struct posix_acl *acl)
1059 for (int n = 0; n < acl->a_count; n++) {
1060 struct posix_acl_entry *acl_e = &acl->a_entries[n];
1062 switch (acl_e->e_tag) {
1064 acl_e->e_uid = from_vfsuid(idmap, fs_userns,
1065 VFSUIDT_INIT(acl_e->e_uid));
1068 acl_e->e_gid = from_vfsgid(idmap, fs_userns,
1069 VFSGIDT_INIT(acl_e->e_gid));
1078 * vfs_set_acl - set posix acls
1079 * @idmap: idmap of the mount
1080 * @dentry: the dentry based on which to set the posix acls
1081 * @acl_name: the name of the posix acl
1082 * @kacl: the posix acls in the appropriate VFS format
1084 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1087 * Return: On success 0, on error negative errno.
1089 int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1090 const char *acl_name, struct posix_acl *kacl)
1094 struct inode *inode = d_inode(dentry);
1095 struct inode *delegated_inode = NULL;
1097 acl_type = posix_acl_type(acl_name);
1103 * If we're on an idmapped mount translate from mount specific
1104 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1105 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1106 * if this is a filesystem with a backing store - ultimately
1107 * translate them to backing store values.
1109 error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl);
1118 * We only care about restrictions the inode struct itself places upon
1119 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1121 error = may_write_xattr(idmap, inode);
1123 goto out_inode_unlock;
1125 error = security_inode_set_acl(idmap, dentry, acl_name, kacl);
1127 goto out_inode_unlock;
1129 error = try_break_deleg(inode, &delegated_inode);
1131 goto out_inode_unlock;
1133 if (likely(!is_bad_inode(inode)))
1134 error = set_posix_acl(idmap, dentry, acl_type, kacl);
1138 fsnotify_xattr(dentry);
1139 security_inode_post_set_acl(dentry, acl_name, kacl);
1143 inode_unlock(inode);
1145 if (delegated_inode) {
1146 error = break_deleg_wait(&delegated_inode);
1153 EXPORT_SYMBOL_GPL(vfs_set_acl);
1156 * vfs_get_acl - get posix acls
1157 * @idmap: idmap of the mount
1158 * @dentry: the dentry based on which to retrieve the posix acls
1159 * @acl_name: the name of the posix acl
1161 * This function retrieves @kacl from the filesystem. The caller must all
1162 * posix_acl_release() on @kacl.
1164 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1166 struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap,
1167 struct dentry *dentry, const char *acl_name)
1169 struct inode *inode = d_inode(dentry);
1170 struct posix_acl *acl;
1171 int acl_type, error;
1173 acl_type = posix_acl_type(acl_name);
1175 return ERR_PTR(-EINVAL);
1178 * The VFS has no restrictions on reading POSIX ACLs so calling
1179 * something like xattr_permission() isn't needed. Only LSMs get a say.
1181 error = security_inode_get_acl(idmap, dentry, acl_name);
1183 return ERR_PTR(error);
1185 if (!IS_POSIXACL(inode))
1186 return ERR_PTR(-EOPNOTSUPP);
1187 if (S_ISLNK(inode->i_mode))
1188 return ERR_PTR(-EOPNOTSUPP);
1190 acl = __get_acl(idmap, dentry, inode, acl_type);
1194 return ERR_PTR(-ENODATA);
1198 EXPORT_SYMBOL_GPL(vfs_get_acl);
1201 * vfs_remove_acl - remove posix acls
1202 * @idmap: idmap of the mount
1203 * @dentry: the dentry based on which to retrieve the posix acls
1204 * @acl_name: the name of the posix acl
1206 * This function removes posix acls.
1208 * Return: On success 0, on error negative errno.
1210 int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1211 const char *acl_name)
1215 struct inode *inode = d_inode(dentry);
1216 struct inode *delegated_inode = NULL;
1218 acl_type = posix_acl_type(acl_name);
1226 * We only care about restrictions the inode struct itself places upon
1227 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1229 error = may_write_xattr(idmap, inode);
1231 goto out_inode_unlock;
1233 error = security_inode_remove_acl(idmap, dentry, acl_name);
1235 goto out_inode_unlock;
1237 error = try_break_deleg(inode, &delegated_inode);
1239 goto out_inode_unlock;
1241 if (likely(!is_bad_inode(inode)))
1242 error = set_posix_acl(idmap, dentry, acl_type, NULL);
1246 fsnotify_xattr(dentry);
1247 security_inode_post_remove_acl(idmap, dentry, acl_name);
1251 inode_unlock(inode);
1253 if (delegated_inode) {
1254 error = break_deleg_wait(&delegated_inode);
1261 EXPORT_SYMBOL_GPL(vfs_remove_acl);
1263 int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1264 const char *acl_name, const void *kvalue, size_t size)
1267 struct posix_acl *acl = NULL;
1271 * Note that posix_acl_from_xattr() uses GFP_NOFS when it
1272 * probably doesn't need to here.
1274 acl = posix_acl_from_xattr(current_user_ns(), kvalue, size);
1276 return PTR_ERR(acl);
1279 error = vfs_set_acl(idmap, dentry, acl_name, acl);
1280 posix_acl_release(acl);
1284 ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry,
1285 const char *acl_name, void *kvalue, size_t size)
1288 struct posix_acl *acl;
1290 acl = vfs_get_acl(idmap, dentry, acl_name);
1292 return PTR_ERR(acl);
1294 error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry),
1296 posix_acl_release(acl);