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>
31 #include <linux/filelock.h>
35 static struct posix_acl
**acl_by_type(struct inode
*inode
, int type
)
40 case ACL_TYPE_DEFAULT
:
41 return &inode
->i_default_acl
;
47 struct posix_acl
*get_cached_acl(struct inode
*inode
, int type
)
49 struct posix_acl
**p
= acl_by_type(inode
, type
);
50 struct posix_acl
*acl
;
54 acl
= rcu_dereference(*p
);
55 if (!acl
|| is_uncached_acl(acl
) ||
56 refcount_inc_not_zero(&acl
->a_refcount
))
64 EXPORT_SYMBOL(get_cached_acl
);
66 struct posix_acl
*get_cached_acl_rcu(struct inode
*inode
, int type
)
68 struct posix_acl
*acl
= rcu_dereference(*acl_by_type(inode
, type
));
70 if (acl
== ACL_DONT_CACHE
) {
71 struct posix_acl
*ret
;
73 ret
= inode
->i_op
->get_inode_acl(inode
, type
, LOOKUP_RCU
);
80 EXPORT_SYMBOL(get_cached_acl_rcu
);
82 void set_cached_acl(struct inode
*inode
, int type
, struct posix_acl
*acl
)
84 struct posix_acl
**p
= acl_by_type(inode
, type
);
85 struct posix_acl
*old
;
87 old
= xchg(p
, posix_acl_dup(acl
));
88 if (!is_uncached_acl(old
))
89 posix_acl_release(old
);
91 EXPORT_SYMBOL(set_cached_acl
);
93 static void __forget_cached_acl(struct posix_acl
**p
)
95 struct posix_acl
*old
;
97 old
= xchg(p
, ACL_NOT_CACHED
);
98 if (!is_uncached_acl(old
))
99 posix_acl_release(old
);
102 void forget_cached_acl(struct inode
*inode
, int type
)
104 __forget_cached_acl(acl_by_type(inode
, type
));
106 EXPORT_SYMBOL(forget_cached_acl
);
108 void forget_all_cached_acls(struct inode
*inode
)
110 __forget_cached_acl(&inode
->i_acl
);
111 __forget_cached_acl(&inode
->i_default_acl
);
113 EXPORT_SYMBOL(forget_all_cached_acls
);
115 static struct posix_acl
*__get_acl(struct mnt_idmap
*idmap
,
116 struct dentry
*dentry
, struct inode
*inode
,
119 struct posix_acl
*sentinel
;
120 struct posix_acl
**p
;
121 struct posix_acl
*acl
;
124 * The sentinel is used to detect when another operation like
125 * set_cached_acl() or forget_cached_acl() races with get_inode_acl().
126 * It is guaranteed that is_uncached_acl(sentinel) is true.
129 acl
= get_cached_acl(inode
, type
);
130 if (!is_uncached_acl(acl
))
133 if (!IS_POSIXACL(inode
))
136 sentinel
= uncached_acl_sentinel(current
);
137 p
= acl_by_type(inode
, type
);
140 * If the ACL isn't being read yet, set our sentinel. Otherwise, the
141 * current value of the ACL will not be ACL_NOT_CACHED and so our own
142 * sentinel will not be set; another task will update the cache. We
143 * could wait for that other task to complete its job, but it's easier
144 * to just call ->get_inode_acl to fetch the ACL ourself. (This is
145 * going to be an unlikely race.)
147 cmpxchg(p
, ACL_NOT_CACHED
, sentinel
);
150 * Normally, the ACL returned by ->get{_inode}_acl will be cached.
151 * A filesystem can prevent that by calling
152 * forget_cached_acl(inode, type) in ->get{_inode}_acl.
154 * If the filesystem doesn't have a get{_inode}_ acl() function at all,
155 * we'll just create the negative cache entry.
157 if (dentry
&& inode
->i_op
->get_acl
) {
158 acl
= inode
->i_op
->get_acl(idmap
, dentry
, type
);
159 } else if (inode
->i_op
->get_inode_acl
) {
160 acl
= inode
->i_op
->get_inode_acl(inode
, type
, false);
162 set_cached_acl(inode
, type
, NULL
);
167 * Remove our sentinel so that we don't block future attempts
170 cmpxchg(p
, sentinel
, ACL_NOT_CACHED
);
175 * Cache the result, but only if our sentinel is still in place.
178 if (unlikely(!try_cmpxchg(p
, &sentinel
, acl
)))
179 posix_acl_release(acl
);
183 struct posix_acl
*get_inode_acl(struct inode
*inode
, int type
)
185 return __get_acl(&nop_mnt_idmap
, NULL
, inode
, type
);
187 EXPORT_SYMBOL(get_inode_acl
);
190 * Init a fresh posix_acl
193 posix_acl_init(struct posix_acl
*acl
, int count
)
195 refcount_set(&acl
->a_refcount
, 1);
196 acl
->a_count
= count
;
198 EXPORT_SYMBOL(posix_acl_init
);
201 * Allocate a new ACL with the specified number of entries.
204 posix_acl_alloc(int count
, gfp_t flags
)
206 const size_t size
= sizeof(struct posix_acl
) +
207 count
* sizeof(struct posix_acl_entry
);
208 struct posix_acl
*acl
= kmalloc(size
, flags
);
210 posix_acl_init(acl
, count
);
213 EXPORT_SYMBOL(posix_acl_alloc
);
219 posix_acl_clone(const struct posix_acl
*acl
, gfp_t flags
)
221 struct posix_acl
*clone
= NULL
;
224 int size
= sizeof(struct posix_acl
) + acl
->a_count
*
225 sizeof(struct posix_acl_entry
);
226 clone
= kmemdup(acl
, size
, flags
);
228 refcount_set(&clone
->a_refcount
, 1);
232 EXPORT_SYMBOL_GPL(posix_acl_clone
);
235 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
238 posix_acl_valid(struct user_namespace
*user_ns
, const struct posix_acl
*acl
)
240 const struct posix_acl_entry
*pa
, *pe
;
241 int state
= ACL_USER_OBJ
;
244 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
245 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
249 if (state
== ACL_USER_OBJ
) {
256 if (state
!= ACL_USER
)
258 if (!kuid_has_mapping(user_ns
, pa
->e_uid
))
264 if (state
== ACL_USER
) {
271 if (state
!= ACL_GROUP
)
273 if (!kgid_has_mapping(user_ns
, pa
->e_gid
))
279 if (state
!= ACL_GROUP
)
285 if (state
== ACL_OTHER
||
286 (state
== ACL_GROUP
&& !needs_mask
)) {
300 EXPORT_SYMBOL(posix_acl_valid
);
303 * Returns 0 if the acl can be exactly represented in the traditional
304 * file mode permission bits, or else 1. Returns -E... on error.
307 posix_acl_equiv_mode(const struct posix_acl
*acl
, umode_t
*mode_p
)
309 const struct posix_acl_entry
*pa
, *pe
;
314 * A null ACL can always be presented as mode bits.
319 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
322 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
325 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
328 mode
|= pa
->e_perm
& S_IRWXO
;
331 mode
= (mode
& ~S_IRWXG
) |
332 ((pa
->e_perm
& S_IRWXO
) << 3);
344 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
347 EXPORT_SYMBOL(posix_acl_equiv_mode
);
350 * Create an ACL representing the file mode permission bits of an inode.
353 posix_acl_from_mode(umode_t mode
, gfp_t flags
)
355 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
357 return ERR_PTR(-ENOMEM
);
359 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
360 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
362 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
363 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
365 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
366 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
369 EXPORT_SYMBOL(posix_acl_from_mode
);
372 * Return 0 if current is granted want access to the inode
373 * by the acl. Returns -E... otherwise.
376 posix_acl_permission(struct mnt_idmap
*idmap
, struct inode
*inode
,
377 const struct posix_acl
*acl
, int want
)
379 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
380 struct user_namespace
*fs_userns
= i_user_ns(inode
);
385 want
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
387 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
390 /* (May have been checked already) */
391 vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
392 if (vfsuid_eq_kuid(vfsuid
, current_fsuid()))
396 vfsuid
= make_vfsuid(idmap
, fs_userns
,
398 if (vfsuid_eq_kuid(vfsuid
, current_fsuid()))
402 vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
403 if (vfsgid_in_group_p(vfsgid
)) {
405 if ((pa
->e_perm
& want
) == want
)
410 vfsgid
= make_vfsgid(idmap
, fs_userns
,
412 if (vfsgid_in_group_p(vfsgid
)) {
414 if ((pa
->e_perm
& want
) == want
)
432 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
433 if (mask_obj
->e_tag
== ACL_MASK
) {
434 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
441 if ((pa
->e_perm
& want
) == want
)
447 * Modify acl when creating a new inode. The caller must ensure the acl is
448 * only referenced once.
450 * mode_p initially must contain the mode parameter to the open() / creat()
451 * system calls. All permissions that are not granted by the acl are removed.
452 * The permissions in the acl are changed to reflect the mode_p parameter.
454 static int posix_acl_create_masq(struct posix_acl
*acl
, umode_t
*mode_p
)
456 struct posix_acl_entry
*pa
, *pe
;
457 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
458 umode_t mode
= *mode_p
;
461 /* assert(atomic_read(acl->a_refcount) == 1); */
463 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
466 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
467 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
480 pa
->e_perm
&= mode
| ~S_IRWXO
;
481 mode
&= pa
->e_perm
| ~S_IRWXO
;
495 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
496 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
500 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
501 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
504 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
509 * Modify the ACL for the chmod syscall.
511 static int __posix_acl_chmod_masq(struct posix_acl
*acl
, umode_t mode
)
513 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
514 struct posix_acl_entry
*pa
, *pe
;
516 /* assert(atomic_read(acl->a_refcount) == 1); */
518 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
521 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
537 pa
->e_perm
= (mode
& S_IRWXO
);
546 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
550 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
557 __posix_acl_create(struct posix_acl
**acl
, gfp_t gfp
, umode_t
*mode_p
)
559 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
562 err
= posix_acl_create_masq(clone
, mode_p
);
564 posix_acl_release(clone
);
568 posix_acl_release(*acl
);
572 EXPORT_SYMBOL(__posix_acl_create
);
575 __posix_acl_chmod(struct posix_acl
**acl
, gfp_t gfp
, umode_t mode
)
577 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
580 err
= __posix_acl_chmod_masq(clone
, mode
);
582 posix_acl_release(clone
);
586 posix_acl_release(*acl
);
590 EXPORT_SYMBOL(__posix_acl_chmod
);
593 * posix_acl_chmod - chmod a posix acl
595 * @idmap: idmap of the mount @inode was found from
596 * @dentry: dentry to check permissions on
597 * @mode: the new mode of @inode
599 * If the dentry has been found through an idmapped mount the idmap of
600 * the vfsmount must be passed through @idmap. This function will then
601 * take care to map the inode according to @idmap before checking
602 * permissions. On non-idmapped mounts or if permission checking is to be
603 * performed on the raw inode simply passs @nop_mnt_idmap.
606 posix_acl_chmod(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
609 struct inode
*inode
= d_inode(dentry
);
610 struct posix_acl
*acl
;
613 if (!IS_POSIXACL(inode
))
615 if (!inode
->i_op
->set_acl
)
618 acl
= get_inode_acl(inode
, ACL_TYPE_ACCESS
);
619 if (IS_ERR_OR_NULL(acl
)) {
620 if (acl
== ERR_PTR(-EOPNOTSUPP
))
625 ret
= __posix_acl_chmod(&acl
, GFP_KERNEL
, mode
);
628 ret
= inode
->i_op
->set_acl(idmap
, dentry
, acl
, ACL_TYPE_ACCESS
);
629 posix_acl_release(acl
);
632 EXPORT_SYMBOL(posix_acl_chmod
);
635 posix_acl_create(struct inode
*dir
, umode_t
*mode
,
636 struct posix_acl
**default_acl
, struct posix_acl
**acl
)
639 struct posix_acl
*clone
;
645 if (S_ISLNK(*mode
) || !IS_POSIXACL(dir
))
648 p
= get_inode_acl(dir
, ACL_TYPE_DEFAULT
);
649 if (!p
|| p
== ERR_PTR(-EOPNOTSUPP
)) {
650 *mode
&= ~current_umask();
657 clone
= posix_acl_clone(p
, GFP_NOFS
);
661 ret
= posix_acl_create_masq(clone
, mode
);
663 goto err_release_clone
;
666 posix_acl_release(clone
);
671 posix_acl_release(p
);
678 posix_acl_release(clone
);
680 posix_acl_release(p
);
683 EXPORT_SYMBOL_GPL(posix_acl_create
);
686 * posix_acl_update_mode - update mode in set_acl
687 * @idmap: idmap of the mount @inode was found from
688 * @inode: target inode
689 * @mode_p: mode (pointer) for update
692 * Update the file mode when setting an ACL: compute the new file permission
693 * bits based on the ACL. In addition, if the ACL is equivalent to the new
694 * file mode, set *@acl to NULL to indicate that no ACL should be set.
696 * As with chmod, clear the setgid bit if the caller is not in the owning group
697 * or capable of CAP_FSETID (see inode_change_ok).
699 * If the inode has been found through an idmapped mount the idmap of
700 * the vfsmount must be passed through @idmap. This function will then
701 * take care to map the inode according to @idmap before checking
702 * permissions. On non-idmapped mounts or if permission checking is to be
703 * performed on the raw inode simply passs @nop_mnt_idmap.
705 * Called from set_acl inode operations.
707 int posix_acl_update_mode(struct mnt_idmap
*idmap
,
708 struct inode
*inode
, umode_t
*mode_p
,
709 struct posix_acl
**acl
)
711 umode_t mode
= inode
->i_mode
;
714 error
= posix_acl_equiv_mode(*acl
, &mode
);
719 if (!vfsgid_in_group_p(i_gid_into_vfsgid(idmap
, inode
)) &&
720 !capable_wrt_inode_uidgid(idmap
, inode
, CAP_FSETID
))
725 EXPORT_SYMBOL(posix_acl_update_mode
);
728 * Fix up the uids and gids in posix acl extended attributes in place.
730 static int posix_acl_fix_xattr_common(const void *value
, size_t size
)
732 const struct posix_acl_xattr_header
*header
= value
;
737 if (size
< sizeof(struct posix_acl_xattr_header
))
739 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
742 count
= posix_acl_xattr_count(size
);
752 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
753 * @userns: the filesystem's idmapping
754 * @value: the uapi representation of POSIX ACLs
755 * @size: the size of @void
757 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
758 * posix_acl_from_xattr() when reading them from the backing store and
759 * converting them into the struct posix_acl VFS format. The helper is
760 * specifically intended to be called from the acl inode operation.
762 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
763 * in ACL_{GROUP,USER} entries into idmapping in @userns.
765 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
766 * If it did it calling it from the get acl inode operation would return POSIX
767 * ACLs mapped according to an idmapped mount which would mean that the value
768 * couldn't be cached for the filesystem. Idmapped mounts are taken into
769 * account on the fly during permission checking or right at the VFS -
770 * userspace boundary before reporting them to the user.
772 * Return: Allocated struct posix_acl on success, NULL for a valid header but
773 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
775 struct posix_acl
*posix_acl_from_xattr(struct user_namespace
*userns
,
776 const void *value
, size_t size
)
778 const struct posix_acl_xattr_header
*header
= value
;
779 const struct posix_acl_xattr_entry
*entry
= (const void *)(header
+ 1), *end
;
781 struct posix_acl
*acl
;
782 struct posix_acl_entry
*acl_e
;
784 count
= posix_acl_fix_xattr_common(value
, size
);
786 return ERR_PTR(count
);
790 acl
= posix_acl_alloc(count
, GFP_NOFS
);
792 return ERR_PTR(-ENOMEM
);
793 acl_e
= acl
->a_entries
;
795 for (end
= entry
+ count
; entry
!= end
; acl_e
++, entry
++) {
796 acl_e
->e_tag
= le16_to_cpu(entry
->e_tag
);
797 acl_e
->e_perm
= le16_to_cpu(entry
->e_perm
);
799 switch(acl_e
->e_tag
) {
807 acl_e
->e_uid
= make_kuid(userns
,
808 le32_to_cpu(entry
->e_id
));
809 if (!uid_valid(acl_e
->e_uid
))
813 acl_e
->e_gid
= make_kgid(userns
,
814 le32_to_cpu(entry
->e_id
));
815 if (!gid_valid(acl_e
->e_gid
))
826 posix_acl_release(acl
);
827 return ERR_PTR(-EINVAL
);
829 EXPORT_SYMBOL (posix_acl_from_xattr
);
832 * Convert from in-memory to extended attribute representation.
835 posix_acl_to_xattr(struct user_namespace
*user_ns
, const struct posix_acl
*acl
,
836 void *buffer
, size_t size
)
838 struct posix_acl_xattr_header
*ext_acl
= buffer
;
839 struct posix_acl_xattr_entry
*ext_entry
;
842 real_size
= posix_acl_xattr_size(acl
->a_count
);
845 if (real_size
> size
)
848 ext_entry
= (void *)(ext_acl
+ 1);
849 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
851 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
852 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
853 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
854 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
855 switch(acl_e
->e_tag
) {
858 cpu_to_le32(from_kuid(user_ns
, acl_e
->e_uid
));
862 cpu_to_le32(from_kgid(user_ns
, acl_e
->e_gid
));
865 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
871 EXPORT_SYMBOL (posix_acl_to_xattr
);
874 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
875 * @idmap: idmap of the mount
876 * @inode: inode the posix acls are set on
877 * @acl: the posix acls as represented by the vfs
878 * @buffer: the buffer into which to convert @acl
879 * @size: size of @buffer
881 * This converts @acl from the VFS representation in the filesystem idmapping
882 * to the uapi form reportable to userspace. And mount and caller idmappings
883 * are handled appropriately.
885 * Return: On success, the size of the stored uapi posix acls, on error a
888 static ssize_t
vfs_posix_acl_to_xattr(struct mnt_idmap
*idmap
,
890 const struct posix_acl
*acl
, void *buffer
,
894 struct posix_acl_xattr_header
*ext_acl
= buffer
;
895 struct posix_acl_xattr_entry
*ext_entry
;
896 struct user_namespace
*fs_userns
, *caller_userns
;
897 ssize_t real_size
, n
;
901 real_size
= posix_acl_xattr_size(acl
->a_count
);
904 if (real_size
> size
)
907 ext_entry
= (void *)(ext_acl
+ 1);
908 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
910 fs_userns
= i_user_ns(inode
);
911 caller_userns
= current_user_ns();
912 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
913 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
914 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
915 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
916 switch(acl_e
->e_tag
) {
918 vfsuid
= make_vfsuid(idmap
, fs_userns
, acl_e
->e_uid
);
919 ext_entry
->e_id
= cpu_to_le32(from_kuid(
920 caller_userns
, vfsuid_into_kuid(vfsuid
)));
923 vfsgid
= make_vfsgid(idmap
, fs_userns
, acl_e
->e_gid
);
924 ext_entry
->e_id
= cpu_to_le32(from_kgid(
925 caller_userns
, vfsgid_into_kgid(vfsgid
)));
928 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
936 set_posix_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
937 int type
, struct posix_acl
*acl
)
939 struct inode
*inode
= d_inode(dentry
);
941 if (!IS_POSIXACL(inode
))
943 if (!inode
->i_op
->set_acl
)
946 if (type
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
947 return acl
? -EACCES
: 0;
948 if (!inode_owner_or_capable(idmap
, inode
))
952 int ret
= posix_acl_valid(inode
->i_sb
->s_user_ns
, acl
);
956 return inode
->i_op
->set_acl(idmap
, dentry
, acl
, type
);
958 EXPORT_SYMBOL(set_posix_acl
);
960 int posix_acl_listxattr(struct inode
*inode
, char **buffer
,
961 ssize_t
*remaining_size
)
965 if (!IS_POSIXACL(inode
))
969 err
= xattr_list_one(buffer
, remaining_size
,
970 XATTR_NAME_POSIX_ACL_ACCESS
);
975 if (inode
->i_default_acl
) {
976 err
= xattr_list_one(buffer
, remaining_size
,
977 XATTR_NAME_POSIX_ACL_DEFAULT
);
986 posix_acl_xattr_list(struct dentry
*dentry
)
988 return IS_POSIXACL(d_backing_inode(dentry
));
992 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
994 * This is the legacy POSIX ACL access xattr handler. It is used by some
995 * filesystems to implement their ->listxattr() inode operation. New code
996 * should never use them.
998 const struct xattr_handler nop_posix_acl_access
= {
999 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
1000 .list
= posix_acl_xattr_list
,
1002 EXPORT_SYMBOL_GPL(nop_posix_acl_access
);
1005 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
1007 * This is the legacy POSIX ACL default xattr handler. It is used by some
1008 * filesystems to implement their ->listxattr() inode operation. New code
1009 * should never use them.
1011 const struct xattr_handler nop_posix_acl_default
= {
1012 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1013 .list
= posix_acl_xattr_list
,
1015 EXPORT_SYMBOL_GPL(nop_posix_acl_default
);
1017 int simple_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1018 struct posix_acl
*acl
, int type
)
1021 struct inode
*inode
= d_inode(dentry
);
1023 if (type
== ACL_TYPE_ACCESS
) {
1024 error
= posix_acl_update_mode(idmap
, inode
,
1025 &inode
->i_mode
, &acl
);
1030 inode_set_ctime_current(inode
);
1031 if (IS_I_VERSION(inode
))
1032 inode_inc_iversion(inode
);
1033 set_cached_acl(inode
, type
, acl
);
1037 int simple_acl_create(struct inode
*dir
, struct inode
*inode
)
1039 struct posix_acl
*default_acl
, *acl
;
1042 error
= posix_acl_create(dir
, &inode
->i_mode
, &default_acl
, &acl
);
1046 set_cached_acl(inode
, ACL_TYPE_DEFAULT
, default_acl
);
1047 set_cached_acl(inode
, ACL_TYPE_ACCESS
, acl
);
1050 posix_acl_release(default_acl
);
1052 posix_acl_release(acl
);
1056 static int vfs_set_acl_idmapped_mnt(struct mnt_idmap
*idmap
,
1057 struct user_namespace
*fs_userns
,
1058 struct posix_acl
*acl
)
1060 for (int n
= 0; n
< acl
->a_count
; n
++) {
1061 struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
1063 switch (acl_e
->e_tag
) {
1065 acl_e
->e_uid
= from_vfsuid(idmap
, fs_userns
,
1066 VFSUIDT_INIT(acl_e
->e_uid
));
1069 acl_e
->e_gid
= from_vfsgid(idmap
, fs_userns
,
1070 VFSGIDT_INIT(acl_e
->e_gid
));
1079 * vfs_set_acl - set posix acls
1080 * @idmap: idmap of the mount
1081 * @dentry: the dentry based on which to set the posix acls
1082 * @acl_name: the name of the posix acl
1083 * @kacl: the posix acls in the appropriate VFS format
1085 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1088 * Return: On success 0, on error negative errno.
1090 int vfs_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1091 const char *acl_name
, struct posix_acl
*kacl
)
1095 struct inode
*inode
= d_inode(dentry
);
1096 struct inode
*delegated_inode
= NULL
;
1098 acl_type
= posix_acl_type(acl_name
);
1104 * If we're on an idmapped mount translate from mount specific
1105 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1106 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1107 * if this is a filesystem with a backing store - ultimately
1108 * translate them to backing store values.
1110 error
= vfs_set_acl_idmapped_mnt(idmap
, i_user_ns(inode
), kacl
);
1119 * We only care about restrictions the inode struct itself places upon
1120 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1122 error
= may_write_xattr(idmap
, inode
);
1124 goto out_inode_unlock
;
1126 error
= security_inode_set_acl(idmap
, dentry
, acl_name
, kacl
);
1128 goto out_inode_unlock
;
1130 error
= try_break_deleg(inode
, &delegated_inode
);
1132 goto out_inode_unlock
;
1134 if (likely(!is_bad_inode(inode
)))
1135 error
= set_posix_acl(idmap
, dentry
, acl_type
, kacl
);
1139 fsnotify_xattr(dentry
);
1140 evm_inode_post_set_acl(dentry
, acl_name
, kacl
);
1144 inode_unlock(inode
);
1146 if (delegated_inode
) {
1147 error
= break_deleg_wait(&delegated_inode
);
1154 EXPORT_SYMBOL_GPL(vfs_set_acl
);
1157 * vfs_get_acl - get posix acls
1158 * @idmap: idmap of the mount
1159 * @dentry: the dentry based on which to retrieve the posix acls
1160 * @acl_name: the name of the posix acl
1162 * This function retrieves @kacl from the filesystem. The caller must all
1163 * posix_acl_release() on @kacl.
1165 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1167 struct posix_acl
*vfs_get_acl(struct mnt_idmap
*idmap
,
1168 struct dentry
*dentry
, const char *acl_name
)
1170 struct inode
*inode
= d_inode(dentry
);
1171 struct posix_acl
*acl
;
1172 int acl_type
, error
;
1174 acl_type
= posix_acl_type(acl_name
);
1176 return ERR_PTR(-EINVAL
);
1179 * The VFS has no restrictions on reading POSIX ACLs so calling
1180 * something like xattr_permission() isn't needed. Only LSMs get a say.
1182 error
= security_inode_get_acl(idmap
, dentry
, acl_name
);
1184 return ERR_PTR(error
);
1186 if (!IS_POSIXACL(inode
))
1187 return ERR_PTR(-EOPNOTSUPP
);
1188 if (S_ISLNK(inode
->i_mode
))
1189 return ERR_PTR(-EOPNOTSUPP
);
1191 acl
= __get_acl(idmap
, dentry
, inode
, acl_type
);
1195 return ERR_PTR(-ENODATA
);
1199 EXPORT_SYMBOL_GPL(vfs_get_acl
);
1202 * vfs_remove_acl - remove posix acls
1203 * @idmap: idmap of the mount
1204 * @dentry: the dentry based on which to retrieve the posix acls
1205 * @acl_name: the name of the posix acl
1207 * This function removes posix acls.
1209 * Return: On success 0, on error negative errno.
1211 int vfs_remove_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1212 const char *acl_name
)
1216 struct inode
*inode
= d_inode(dentry
);
1217 struct inode
*delegated_inode
= NULL
;
1219 acl_type
= posix_acl_type(acl_name
);
1227 * We only care about restrictions the inode struct itself places upon
1228 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1230 error
= may_write_xattr(idmap
, inode
);
1232 goto out_inode_unlock
;
1234 error
= security_inode_remove_acl(idmap
, dentry
, acl_name
);
1236 goto out_inode_unlock
;
1238 error
= try_break_deleg(inode
, &delegated_inode
);
1240 goto out_inode_unlock
;
1242 if (likely(!is_bad_inode(inode
)))
1243 error
= set_posix_acl(idmap
, dentry
, acl_type
, NULL
);
1247 fsnotify_xattr(dentry
);
1248 evm_inode_post_remove_acl(idmap
, dentry
, acl_name
);
1252 inode_unlock(inode
);
1254 if (delegated_inode
) {
1255 error
= break_deleg_wait(&delegated_inode
);
1262 EXPORT_SYMBOL_GPL(vfs_remove_acl
);
1264 int do_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1265 const char *acl_name
, const void *kvalue
, size_t size
)
1268 struct posix_acl
*acl
= NULL
;
1272 * Note that posix_acl_from_xattr() uses GFP_NOFS when it
1273 * probably doesn't need to here.
1275 acl
= posix_acl_from_xattr(current_user_ns(), kvalue
, size
);
1277 return PTR_ERR(acl
);
1280 error
= vfs_set_acl(idmap
, dentry
, acl_name
, acl
);
1281 posix_acl_release(acl
);
1285 ssize_t
do_get_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1286 const char *acl_name
, void *kvalue
, size_t size
)
1289 struct posix_acl
*acl
;
1291 acl
= vfs_get_acl(idmap
, dentry
, acl_name
);
1293 return PTR_ERR(acl
);
1295 error
= vfs_posix_acl_to_xattr(idmap
, d_inode(dentry
),
1297 posix_acl_release(acl
);