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/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(unsigned int count
, gfp_t flags
)
205 struct posix_acl
*acl
;
207 acl
= kmalloc(struct_size(acl
, a_entries
, count
), 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 clone
= kmemdup(acl
, struct_size(acl
, a_entries
, acl
->a_count
),
226 refcount_set(&clone
->a_refcount
, 1);
230 EXPORT_SYMBOL_GPL(posix_acl_clone
);
233 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise.
236 posix_acl_valid(struct user_namespace
*user_ns
, const struct posix_acl
*acl
)
238 const struct posix_acl_entry
*pa
, *pe
;
239 int state
= ACL_USER_OBJ
;
242 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
243 if (pa
->e_perm
& ~(ACL_READ
|ACL_WRITE
|ACL_EXECUTE
))
247 if (state
== ACL_USER_OBJ
) {
254 if (state
!= ACL_USER
)
256 if (!kuid_has_mapping(user_ns
, pa
->e_uid
))
262 if (state
== ACL_USER
) {
269 if (state
!= ACL_GROUP
)
271 if (!kgid_has_mapping(user_ns
, pa
->e_gid
))
277 if (state
!= ACL_GROUP
)
283 if (state
== ACL_OTHER
||
284 (state
== ACL_GROUP
&& !needs_mask
)) {
298 EXPORT_SYMBOL(posix_acl_valid
);
301 * Returns 0 if the acl can be exactly represented in the traditional
302 * file mode permission bits, or else 1. Returns -E... on error.
305 posix_acl_equiv_mode(const struct posix_acl
*acl
, umode_t
*mode_p
)
307 const struct posix_acl_entry
*pa
, *pe
;
312 * A null ACL can always be presented as mode bits.
317 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
320 mode
|= (pa
->e_perm
& S_IRWXO
) << 6;
323 mode
|= (pa
->e_perm
& S_IRWXO
) << 3;
326 mode
|= pa
->e_perm
& S_IRWXO
;
329 mode
= (mode
& ~S_IRWXG
) |
330 ((pa
->e_perm
& S_IRWXO
) << 3);
342 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
345 EXPORT_SYMBOL(posix_acl_equiv_mode
);
348 * Create an ACL representing the file mode permission bits of an inode.
351 posix_acl_from_mode(umode_t mode
, gfp_t flags
)
353 struct posix_acl
*acl
= posix_acl_alloc(3, flags
);
355 return ERR_PTR(-ENOMEM
);
357 acl
->a_entries
[0].e_tag
= ACL_USER_OBJ
;
358 acl
->a_entries
[0].e_perm
= (mode
& S_IRWXU
) >> 6;
360 acl
->a_entries
[1].e_tag
= ACL_GROUP_OBJ
;
361 acl
->a_entries
[1].e_perm
= (mode
& S_IRWXG
) >> 3;
363 acl
->a_entries
[2].e_tag
= ACL_OTHER
;
364 acl
->a_entries
[2].e_perm
= (mode
& S_IRWXO
);
367 EXPORT_SYMBOL(posix_acl_from_mode
);
370 * Return 0 if current is granted want access to the inode
371 * by the acl. Returns -E... otherwise.
374 posix_acl_permission(struct mnt_idmap
*idmap
, struct inode
*inode
,
375 const struct posix_acl
*acl
, int want
)
377 const struct posix_acl_entry
*pa
, *pe
, *mask_obj
;
378 struct user_namespace
*fs_userns
= i_user_ns(inode
);
383 want
&= MAY_READ
| MAY_WRITE
| MAY_EXEC
;
385 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
388 /* (May have been checked already) */
389 vfsuid
= i_uid_into_vfsuid(idmap
, inode
);
390 if (vfsuid_eq_kuid(vfsuid
, current_fsuid()))
394 vfsuid
= make_vfsuid(idmap
, fs_userns
,
396 if (vfsuid_eq_kuid(vfsuid
, current_fsuid()))
400 vfsgid
= i_gid_into_vfsgid(idmap
, inode
);
401 if (vfsgid_in_group_p(vfsgid
)) {
403 if ((pa
->e_perm
& want
) == want
)
408 vfsgid
= make_vfsgid(idmap
, fs_userns
,
410 if (vfsgid_in_group_p(vfsgid
)) {
412 if ((pa
->e_perm
& want
) == want
)
430 for (mask_obj
= pa
+1; mask_obj
!= pe
; mask_obj
++) {
431 if (mask_obj
->e_tag
== ACL_MASK
) {
432 if ((pa
->e_perm
& mask_obj
->e_perm
& want
) == want
)
439 if ((pa
->e_perm
& want
) == want
)
445 * Modify acl when creating a new inode. The caller must ensure the acl is
446 * only referenced once.
448 * mode_p initially must contain the mode parameter to the open() / creat()
449 * system calls. All permissions that are not granted by the acl are removed.
450 * The permissions in the acl are changed to reflect the mode_p parameter.
452 static int posix_acl_create_masq(struct posix_acl
*acl
, umode_t
*mode_p
)
454 struct posix_acl_entry
*pa
, *pe
;
455 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
456 umode_t mode
= *mode_p
;
459 /* assert(atomic_read(acl->a_refcount) == 1); */
461 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
464 pa
->e_perm
&= (mode
>> 6) | ~S_IRWXO
;
465 mode
&= (pa
->e_perm
<< 6) | ~S_IRWXU
;
478 pa
->e_perm
&= mode
| ~S_IRWXO
;
479 mode
&= pa
->e_perm
| ~S_IRWXO
;
493 mask_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
494 mode
&= (mask_obj
->e_perm
<< 3) | ~S_IRWXG
;
498 group_obj
->e_perm
&= (mode
>> 3) | ~S_IRWXO
;
499 mode
&= (group_obj
->e_perm
<< 3) | ~S_IRWXG
;
502 *mode_p
= (*mode_p
& ~S_IRWXUGO
) | mode
;
507 * Modify the ACL for the chmod syscall.
509 static int __posix_acl_chmod_masq(struct posix_acl
*acl
, umode_t mode
)
511 struct posix_acl_entry
*group_obj
= NULL
, *mask_obj
= NULL
;
512 struct posix_acl_entry
*pa
, *pe
;
514 /* assert(atomic_read(acl->a_refcount) == 1); */
516 FOREACH_ACL_ENTRY(pa
, acl
, pe
) {
519 pa
->e_perm
= (mode
& S_IRWXU
) >> 6;
535 pa
->e_perm
= (mode
& S_IRWXO
);
544 mask_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
548 group_obj
->e_perm
= (mode
& S_IRWXG
) >> 3;
555 __posix_acl_create(struct posix_acl
**acl
, gfp_t gfp
, umode_t
*mode_p
)
557 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
560 err
= posix_acl_create_masq(clone
, mode_p
);
562 posix_acl_release(clone
);
566 posix_acl_release(*acl
);
570 EXPORT_SYMBOL(__posix_acl_create
);
573 __posix_acl_chmod(struct posix_acl
**acl
, gfp_t gfp
, umode_t mode
)
575 struct posix_acl
*clone
= posix_acl_clone(*acl
, gfp
);
578 err
= __posix_acl_chmod_masq(clone
, mode
);
580 posix_acl_release(clone
);
584 posix_acl_release(*acl
);
588 EXPORT_SYMBOL(__posix_acl_chmod
);
591 * posix_acl_chmod - chmod a posix acl
593 * @idmap: idmap of the mount @inode was found from
594 * @dentry: dentry to check permissions on
595 * @mode: the new mode of @inode
597 * If the dentry has been found through an idmapped mount the idmap of
598 * the vfsmount must be passed through @idmap. This function will then
599 * take care to map the inode according to @idmap before checking
600 * permissions. On non-idmapped mounts or if permission checking is to be
601 * performed on the raw inode simply pass @nop_mnt_idmap.
604 posix_acl_chmod(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
607 struct inode
*inode
= d_inode(dentry
);
608 struct posix_acl
*acl
;
611 if (!IS_POSIXACL(inode
))
613 if (!inode
->i_op
->set_acl
)
616 acl
= get_inode_acl(inode
, ACL_TYPE_ACCESS
);
617 if (IS_ERR_OR_NULL(acl
)) {
618 if (acl
== ERR_PTR(-EOPNOTSUPP
))
623 ret
= __posix_acl_chmod(&acl
, GFP_KERNEL
, mode
);
626 ret
= inode
->i_op
->set_acl(idmap
, dentry
, acl
, ACL_TYPE_ACCESS
);
627 posix_acl_release(acl
);
630 EXPORT_SYMBOL(posix_acl_chmod
);
633 posix_acl_create(struct inode
*dir
, umode_t
*mode
,
634 struct posix_acl
**default_acl
, struct posix_acl
**acl
)
637 struct posix_acl
*clone
;
643 if (S_ISLNK(*mode
) || !IS_POSIXACL(dir
))
646 p
= get_inode_acl(dir
, ACL_TYPE_DEFAULT
);
647 if (!p
|| p
== ERR_PTR(-EOPNOTSUPP
)) {
648 *mode
&= ~current_umask();
655 clone
= posix_acl_clone(p
, GFP_NOFS
);
659 ret
= posix_acl_create_masq(clone
, mode
);
661 goto err_release_clone
;
664 posix_acl_release(clone
);
669 posix_acl_release(p
);
676 posix_acl_release(clone
);
678 posix_acl_release(p
);
681 EXPORT_SYMBOL_GPL(posix_acl_create
);
684 * posix_acl_update_mode - update mode in set_acl
685 * @idmap: idmap of the mount @inode was found from
686 * @inode: target inode
687 * @mode_p: mode (pointer) for update
690 * Update the file mode when setting an ACL: compute the new file permission
691 * bits based on the ACL. In addition, if the ACL is equivalent to the new
692 * file mode, set *@acl to NULL to indicate that no ACL should be set.
694 * As with chmod, clear the setgid bit if the caller is not in the owning group
695 * or capable of CAP_FSETID (see inode_change_ok).
697 * If the inode has been found through an idmapped mount the idmap of
698 * the vfsmount must be passed through @idmap. This function will then
699 * take care to map the inode according to @idmap before checking
700 * permissions. On non-idmapped mounts or if permission checking is to be
701 * performed on the raw inode simply pass @nop_mnt_idmap.
703 * Called from set_acl inode operations.
705 int posix_acl_update_mode(struct mnt_idmap
*idmap
,
706 struct inode
*inode
, umode_t
*mode_p
,
707 struct posix_acl
**acl
)
709 umode_t mode
= inode
->i_mode
;
712 error
= posix_acl_equiv_mode(*acl
, &mode
);
717 if (!in_group_or_capable(idmap
, inode
,
718 i_gid_into_vfsgid(idmap
, inode
)))
723 EXPORT_SYMBOL(posix_acl_update_mode
);
726 * Fix up the uids and gids in posix acl extended attributes in place.
728 static int posix_acl_fix_xattr_common(const void *value
, size_t size
)
730 const struct posix_acl_xattr_header
*header
= value
;
735 if (size
< sizeof(struct posix_acl_xattr_header
))
737 if (header
->a_version
!= cpu_to_le32(POSIX_ACL_XATTR_VERSION
))
740 count
= posix_acl_xattr_count(size
);
750 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format
751 * @userns: the filesystem's idmapping
752 * @value: the uapi representation of POSIX ACLs
753 * @size: the size of @void
755 * Filesystems that store POSIX ACLs in the unaltered uapi format should use
756 * posix_acl_from_xattr() when reading them from the backing store and
757 * converting them into the struct posix_acl VFS format. The helper is
758 * specifically intended to be called from the acl inode operation.
760 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored
761 * in ACL_{GROUP,USER} entries into idmapping in @userns.
763 * Note that posix_acl_from_xattr() does not take idmapped mounts into account.
764 * If it did it calling it from the get acl inode operation would return POSIX
765 * ACLs mapped according to an idmapped mount which would mean that the value
766 * couldn't be cached for the filesystem. Idmapped mounts are taken into
767 * account on the fly during permission checking or right at the VFS -
768 * userspace boundary before reporting them to the user.
770 * Return: Allocated struct posix_acl on success, NULL for a valid header but
771 * without actual POSIX ACL entries, or ERR_PTR() encoded error code.
773 struct posix_acl
*posix_acl_from_xattr(struct user_namespace
*userns
,
774 const void *value
, size_t size
)
776 const struct posix_acl_xattr_header
*header
= value
;
777 const struct posix_acl_xattr_entry
*entry
= (const void *)(header
+ 1), *end
;
779 struct posix_acl
*acl
;
780 struct posix_acl_entry
*acl_e
;
782 count
= posix_acl_fix_xattr_common(value
, size
);
784 return ERR_PTR(count
);
788 acl
= posix_acl_alloc(count
, GFP_NOFS
);
790 return ERR_PTR(-ENOMEM
);
791 acl_e
= acl
->a_entries
;
793 for (end
= entry
+ count
; entry
!= end
; acl_e
++, entry
++) {
794 acl_e
->e_tag
= le16_to_cpu(entry
->e_tag
);
795 acl_e
->e_perm
= le16_to_cpu(entry
->e_perm
);
797 switch(acl_e
->e_tag
) {
805 acl_e
->e_uid
= make_kuid(userns
,
806 le32_to_cpu(entry
->e_id
));
807 if (!uid_valid(acl_e
->e_uid
))
811 acl_e
->e_gid
= make_kgid(userns
,
812 le32_to_cpu(entry
->e_id
));
813 if (!gid_valid(acl_e
->e_gid
))
824 posix_acl_release(acl
);
825 return ERR_PTR(-EINVAL
);
827 EXPORT_SYMBOL (posix_acl_from_xattr
);
830 * Convert from in-memory to extended attribute representation.
833 posix_acl_to_xattr(struct user_namespace
*user_ns
, const struct posix_acl
*acl
,
834 void *buffer
, size_t size
)
836 struct posix_acl_xattr_header
*ext_acl
= buffer
;
837 struct posix_acl_xattr_entry
*ext_entry
;
840 real_size
= posix_acl_xattr_size(acl
->a_count
);
843 if (real_size
> size
)
846 ext_entry
= (void *)(ext_acl
+ 1);
847 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
849 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
850 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
851 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
852 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
853 switch(acl_e
->e_tag
) {
856 cpu_to_le32(from_kuid(user_ns
, acl_e
->e_uid
));
860 cpu_to_le32(from_kgid(user_ns
, acl_e
->e_gid
));
863 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
869 EXPORT_SYMBOL (posix_acl_to_xattr
);
872 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation
873 * @idmap: idmap of the mount
874 * @inode: inode the posix acls are set on
875 * @acl: the posix acls as represented by the vfs
876 * @buffer: the buffer into which to convert @acl
877 * @size: size of @buffer
879 * This converts @acl from the VFS representation in the filesystem idmapping
880 * to the uapi form reportable to userspace. And mount and caller idmappings
881 * are handled appropriately.
883 * Return: On success, the size of the stored uapi posix acls, on error a
886 static ssize_t
vfs_posix_acl_to_xattr(struct mnt_idmap
*idmap
,
888 const struct posix_acl
*acl
, void *buffer
,
892 struct posix_acl_xattr_header
*ext_acl
= buffer
;
893 struct posix_acl_xattr_entry
*ext_entry
;
894 struct user_namespace
*fs_userns
, *caller_userns
;
895 ssize_t real_size
, n
;
899 real_size
= posix_acl_xattr_size(acl
->a_count
);
902 if (real_size
> size
)
905 ext_entry
= (void *)(ext_acl
+ 1);
906 ext_acl
->a_version
= cpu_to_le32(POSIX_ACL_XATTR_VERSION
);
908 fs_userns
= i_user_ns(inode
);
909 caller_userns
= current_user_ns();
910 for (n
=0; n
< acl
->a_count
; n
++, ext_entry
++) {
911 const struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
912 ext_entry
->e_tag
= cpu_to_le16(acl_e
->e_tag
);
913 ext_entry
->e_perm
= cpu_to_le16(acl_e
->e_perm
);
914 switch(acl_e
->e_tag
) {
916 vfsuid
= make_vfsuid(idmap
, fs_userns
, acl_e
->e_uid
);
917 ext_entry
->e_id
= cpu_to_le32(from_kuid(
918 caller_userns
, vfsuid_into_kuid(vfsuid
)));
921 vfsgid
= make_vfsgid(idmap
, fs_userns
, acl_e
->e_gid
);
922 ext_entry
->e_id
= cpu_to_le32(from_kgid(
923 caller_userns
, vfsgid_into_kgid(vfsgid
)));
926 ext_entry
->e_id
= cpu_to_le32(ACL_UNDEFINED_ID
);
934 set_posix_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
935 int type
, struct posix_acl
*acl
)
937 struct inode
*inode
= d_inode(dentry
);
939 if (!IS_POSIXACL(inode
))
941 if (!inode
->i_op
->set_acl
)
944 if (type
== ACL_TYPE_DEFAULT
&& !S_ISDIR(inode
->i_mode
))
945 return acl
? -EACCES
: 0;
946 if (!inode_owner_or_capable(idmap
, inode
))
950 int ret
= posix_acl_valid(inode
->i_sb
->s_user_ns
, acl
);
954 return inode
->i_op
->set_acl(idmap
, dentry
, acl
, type
);
956 EXPORT_SYMBOL(set_posix_acl
);
958 int posix_acl_listxattr(struct inode
*inode
, char **buffer
,
959 ssize_t
*remaining_size
)
963 if (!IS_POSIXACL(inode
))
967 err
= xattr_list_one(buffer
, remaining_size
,
968 XATTR_NAME_POSIX_ACL_ACCESS
);
973 if (inode
->i_default_acl
) {
974 err
= xattr_list_one(buffer
, remaining_size
,
975 XATTR_NAME_POSIX_ACL_DEFAULT
);
984 posix_acl_xattr_list(struct dentry
*dentry
)
986 return IS_POSIXACL(d_backing_inode(dentry
));
990 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs
992 * This is the legacy POSIX ACL access xattr handler. It is used by some
993 * filesystems to implement their ->listxattr() inode operation. New code
994 * should never use them.
996 const struct xattr_handler nop_posix_acl_access
= {
997 .name
= XATTR_NAME_POSIX_ACL_ACCESS
,
998 .list
= posix_acl_xattr_list
,
1000 EXPORT_SYMBOL_GPL(nop_posix_acl_access
);
1003 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs
1005 * This is the legacy POSIX ACL default xattr handler. It is used by some
1006 * filesystems to implement their ->listxattr() inode operation. New code
1007 * should never use them.
1009 const struct xattr_handler nop_posix_acl_default
= {
1010 .name
= XATTR_NAME_POSIX_ACL_DEFAULT
,
1011 .list
= posix_acl_xattr_list
,
1013 EXPORT_SYMBOL_GPL(nop_posix_acl_default
);
1015 int simple_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1016 struct posix_acl
*acl
, int type
)
1019 struct inode
*inode
= d_inode(dentry
);
1021 if (type
== ACL_TYPE_ACCESS
) {
1022 error
= posix_acl_update_mode(idmap
, inode
,
1023 &inode
->i_mode
, &acl
);
1028 inode_set_ctime_current(inode
);
1029 if (IS_I_VERSION(inode
))
1030 inode_inc_iversion(inode
);
1031 set_cached_acl(inode
, type
, acl
);
1035 int simple_acl_create(struct inode
*dir
, struct inode
*inode
)
1037 struct posix_acl
*default_acl
, *acl
;
1040 error
= posix_acl_create(dir
, &inode
->i_mode
, &default_acl
, &acl
);
1044 set_cached_acl(inode
, ACL_TYPE_DEFAULT
, default_acl
);
1045 set_cached_acl(inode
, ACL_TYPE_ACCESS
, acl
);
1048 posix_acl_release(default_acl
);
1050 posix_acl_release(acl
);
1054 static int vfs_set_acl_idmapped_mnt(struct mnt_idmap
*idmap
,
1055 struct user_namespace
*fs_userns
,
1056 struct posix_acl
*acl
)
1058 for (int n
= 0; n
< acl
->a_count
; n
++) {
1059 struct posix_acl_entry
*acl_e
= &acl
->a_entries
[n
];
1061 switch (acl_e
->e_tag
) {
1063 acl_e
->e_uid
= from_vfsuid(idmap
, fs_userns
,
1064 VFSUIDT_INIT(acl_e
->e_uid
));
1067 acl_e
->e_gid
= from_vfsgid(idmap
, fs_userns
,
1068 VFSGIDT_INIT(acl_e
->e_gid
));
1077 * vfs_set_acl - set posix acls
1078 * @idmap: idmap of the mount
1079 * @dentry: the dentry based on which to set the posix acls
1080 * @acl_name: the name of the posix acl
1081 * @kacl: the posix acls in the appropriate VFS format
1083 * This function sets @kacl. The caller must all posix_acl_release() on @kacl
1086 * Return: On success 0, on error negative errno.
1088 int vfs_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1089 const char *acl_name
, struct posix_acl
*kacl
)
1093 struct inode
*inode
= d_inode(dentry
);
1094 struct inode
*delegated_inode
= NULL
;
1096 acl_type
= posix_acl_type(acl_name
);
1102 * If we're on an idmapped mount translate from mount specific
1103 * vfs{g,u}id_t into global filesystem k{g,u}id_t.
1104 * Afterwards we can cache the POSIX ACLs filesystem wide and -
1105 * if this is a filesystem with a backing store - ultimately
1106 * translate them to backing store values.
1108 error
= vfs_set_acl_idmapped_mnt(idmap
, i_user_ns(inode
), kacl
);
1117 * We only care about restrictions the inode struct itself places upon
1118 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1120 error
= may_write_xattr(idmap
, inode
);
1122 goto out_inode_unlock
;
1124 error
= security_inode_set_acl(idmap
, dentry
, acl_name
, kacl
);
1126 goto out_inode_unlock
;
1128 error
= try_break_deleg(inode
, &delegated_inode
);
1130 goto out_inode_unlock
;
1132 if (likely(!is_bad_inode(inode
)))
1133 error
= set_posix_acl(idmap
, dentry
, acl_type
, kacl
);
1137 fsnotify_xattr(dentry
);
1138 security_inode_post_set_acl(dentry
, acl_name
, kacl
);
1142 inode_unlock(inode
);
1144 if (delegated_inode
) {
1145 error
= break_deleg_wait(&delegated_inode
);
1152 EXPORT_SYMBOL_GPL(vfs_set_acl
);
1155 * vfs_get_acl - get posix acls
1156 * @idmap: idmap of the mount
1157 * @dentry: the dentry based on which to retrieve the posix acls
1158 * @acl_name: the name of the posix acl
1160 * This function retrieves @kacl from the filesystem. The caller must all
1161 * posix_acl_release() on @kacl.
1163 * Return: On success POSIX ACLs in VFS format, on error negative errno.
1165 struct posix_acl
*vfs_get_acl(struct mnt_idmap
*idmap
,
1166 struct dentry
*dentry
, const char *acl_name
)
1168 struct inode
*inode
= d_inode(dentry
);
1169 struct posix_acl
*acl
;
1170 int acl_type
, error
;
1172 acl_type
= posix_acl_type(acl_name
);
1174 return ERR_PTR(-EINVAL
);
1177 * The VFS has no restrictions on reading POSIX ACLs so calling
1178 * something like xattr_permission() isn't needed. Only LSMs get a say.
1180 error
= security_inode_get_acl(idmap
, dentry
, acl_name
);
1182 return ERR_PTR(error
);
1184 if (!IS_POSIXACL(inode
))
1185 return ERR_PTR(-EOPNOTSUPP
);
1186 if (S_ISLNK(inode
->i_mode
))
1187 return ERR_PTR(-EOPNOTSUPP
);
1189 acl
= __get_acl(idmap
, dentry
, inode
, acl_type
);
1193 return ERR_PTR(-ENODATA
);
1197 EXPORT_SYMBOL_GPL(vfs_get_acl
);
1200 * vfs_remove_acl - remove posix acls
1201 * @idmap: idmap of the mount
1202 * @dentry: the dentry based on which to retrieve the posix acls
1203 * @acl_name: the name of the posix acl
1205 * This function removes posix acls.
1207 * Return: On success 0, on error negative errno.
1209 int vfs_remove_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1210 const char *acl_name
)
1214 struct inode
*inode
= d_inode(dentry
);
1215 struct inode
*delegated_inode
= NULL
;
1217 acl_type
= posix_acl_type(acl_name
);
1225 * We only care about restrictions the inode struct itself places upon
1226 * us otherwise POSIX ACLs aren't subject to any VFS restrictions.
1228 error
= may_write_xattr(idmap
, inode
);
1230 goto out_inode_unlock
;
1232 error
= security_inode_remove_acl(idmap
, dentry
, acl_name
);
1234 goto out_inode_unlock
;
1236 error
= try_break_deleg(inode
, &delegated_inode
);
1238 goto out_inode_unlock
;
1240 if (likely(!is_bad_inode(inode
)))
1241 error
= set_posix_acl(idmap
, dentry
, acl_type
, NULL
);
1245 fsnotify_xattr(dentry
);
1246 security_inode_post_remove_acl(idmap
, dentry
, acl_name
);
1250 inode_unlock(inode
);
1252 if (delegated_inode
) {
1253 error
= break_deleg_wait(&delegated_inode
);
1260 EXPORT_SYMBOL_GPL(vfs_remove_acl
);
1262 int do_set_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1263 const char *acl_name
, const void *kvalue
, size_t size
)
1266 struct posix_acl
*acl
= NULL
;
1270 * Note that posix_acl_from_xattr() uses GFP_NOFS when it
1271 * probably doesn't need to here.
1273 acl
= posix_acl_from_xattr(current_user_ns(), kvalue
, size
);
1275 return PTR_ERR(acl
);
1278 error
= vfs_set_acl(idmap
, dentry
, acl_name
, acl
);
1279 posix_acl_release(acl
);
1283 ssize_t
do_get_acl(struct mnt_idmap
*idmap
, struct dentry
*dentry
,
1284 const char *acl_name
, void *kvalue
, size_t size
)
1287 struct posix_acl
*acl
;
1289 acl
= vfs_get_acl(idmap
, dentry
, acl_name
);
1291 return PTR_ERR(acl
);
1293 error
= vfs_posix_acl_to_xattr(idmap
, d_inode(dentry
),
1295 posix_acl_release(acl
);