1 // SPDX-License-Identifier: GPL-2.0-only
5 Extended attribute handling.
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
9 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/evm.h>
19 #include <linux/syscalls.h>
20 #include <linux/export.h>
21 #include <linux/fsnotify.h>
22 #include <linux/audit.h>
23 #include <linux/vmalloc.h>
24 #include <linux/posix_acl_xattr.h>
26 #include <linux/uaccess.h>
29 strcmp_prefix(const char *a
, const char *a_prefix
)
31 while (*a_prefix
&& *a
== *a_prefix
) {
35 return *a_prefix
? NULL
: a
;
39 * In order to implement different sets of xattr operations for each xattr
40 * prefix, a filesystem should create a null-terminated array of struct
41 * xattr_handler (one for each prefix) and hang a pointer to it off of the
42 * s_xattr field of the superblock.
44 #define for_each_xattr_handler(handlers, handler) \
46 for ((handler) = *(handlers)++; \
48 (handler) = *(handlers)++)
51 * Find the xattr_handler with the matching prefix.
53 static const struct xattr_handler
*
54 xattr_resolve_name(struct inode
*inode
, const char **name
)
56 const struct xattr_handler
**handlers
= inode
->i_sb
->s_xattr
;
57 const struct xattr_handler
*handler
;
59 if (!(inode
->i_opflags
& IOP_XATTR
)) {
60 if (unlikely(is_bad_inode(inode
)))
62 return ERR_PTR(-EOPNOTSUPP
);
64 for_each_xattr_handler(handlers
, handler
) {
67 n
= strcmp_prefix(*name
, xattr_prefix(handler
));
69 if (!handler
->prefix
^ !*n
) {
72 return ERR_PTR(-EINVAL
);
78 return ERR_PTR(-EOPNOTSUPP
);
82 * Check permissions for extended attribute access. This is a bit complicated
83 * because different namespaces have very different rules.
86 xattr_permission(struct inode
*inode
, const char *name
, int mask
)
89 * We can never set or remove an extended attribute on a read-only
90 * filesystem or on an immutable / append-only inode.
92 if (mask
& MAY_WRITE
) {
93 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
96 * Updating an xattr will likely cause i_uid and i_gid
97 * to be writen back improperly if their true value is
100 if (HAS_UNMAPPED_ID(inode
))
105 * No restriction for security.* and system.* from the VFS. Decision
106 * on these is left to the underlying filesystem / security module.
108 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
109 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
113 * The trusted.* namespace can only be accessed by privileged users.
115 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
)) {
116 if (!capable(CAP_SYS_ADMIN
))
117 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
122 * In the user.* namespace, only regular files and directories can have
123 * extended attributes. For sticky directories, only the owner and
124 * privileged users can write attributes.
126 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)) {
127 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
128 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
129 if (S_ISDIR(inode
->i_mode
) && (inode
->i_mode
& S_ISVTX
) &&
130 (mask
& MAY_WRITE
) && !inode_owner_or_capable(inode
))
134 return inode_permission(inode
, mask
);
138 * Look for any handler that deals with the specified namespace.
141 xattr_supported_namespace(struct inode
*inode
, const char *prefix
)
143 const struct xattr_handler
**handlers
= inode
->i_sb
->s_xattr
;
144 const struct xattr_handler
*handler
;
147 if (!(inode
->i_opflags
& IOP_XATTR
)) {
148 if (unlikely(is_bad_inode(inode
)))
153 preflen
= strlen(prefix
);
155 for_each_xattr_handler(handlers
, handler
) {
156 if (!strncmp(xattr_prefix(handler
), prefix
, preflen
))
162 EXPORT_SYMBOL(xattr_supported_namespace
);
165 __vfs_setxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
166 const void *value
, size_t size
, int flags
)
168 const struct xattr_handler
*handler
;
170 handler
= xattr_resolve_name(inode
, &name
);
172 return PTR_ERR(handler
);
176 value
= ""; /* empty EA, do not remove */
177 return handler
->set(handler
, dentry
, inode
, name
, value
, size
, flags
);
179 EXPORT_SYMBOL(__vfs_setxattr
);
182 * __vfs_setxattr_noperm - perform setxattr operation without performing
185 * @dentry - object to perform setxattr on
186 * @name - xattr name to set
187 * @value - value to set @name to
188 * @size - size of @value
189 * @flags - flags to pass into filesystem operations
191 * returns the result of the internal setxattr or setsecurity operations.
193 * This function requires the caller to lock the inode's i_mutex before it
194 * is executed. It also assumes that the caller will make the appropriate
197 int __vfs_setxattr_noperm(struct dentry
*dentry
, const char *name
,
198 const void *value
, size_t size
, int flags
)
200 struct inode
*inode
= dentry
->d_inode
;
202 int issec
= !strncmp(name
, XATTR_SECURITY_PREFIX
,
203 XATTR_SECURITY_PREFIX_LEN
);
206 inode
->i_flags
&= ~S_NOSEC
;
207 if (inode
->i_opflags
& IOP_XATTR
) {
208 error
= __vfs_setxattr(dentry
, inode
, name
, value
, size
, flags
);
210 fsnotify_xattr(dentry
);
211 security_inode_post_setxattr(dentry
, name
, value
,
215 if (unlikely(is_bad_inode(inode
)))
218 if (error
== -EAGAIN
) {
222 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
224 error
= security_inode_setsecurity(inode
, suffix
, value
,
227 fsnotify_xattr(dentry
);
235 * __vfs_setxattr_locked - set an extended attribute while holding the inode
238 * @dentry: object to perform setxattr on
239 * @name: xattr name to set
240 * @value: value to set @name to
241 * @size: size of @value
242 * @flags: flags to pass into filesystem operations
243 * @delegated_inode: on return, will contain an inode pointer that
244 * a delegation was broken on, NULL if none.
247 __vfs_setxattr_locked(struct dentry
*dentry
, const char *name
,
248 const void *value
, size_t size
, int flags
,
249 struct inode
**delegated_inode
)
251 struct inode
*inode
= dentry
->d_inode
;
254 error
= xattr_permission(inode
, name
, MAY_WRITE
);
258 error
= security_inode_setxattr(dentry
, name
, value
, size
, flags
);
262 error
= try_break_deleg(inode
, delegated_inode
);
266 error
= __vfs_setxattr_noperm(dentry
, name
, value
, size
, flags
);
271 EXPORT_SYMBOL_GPL(__vfs_setxattr_locked
);
274 vfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
275 size_t size
, int flags
)
277 struct inode
*inode
= dentry
->d_inode
;
278 struct inode
*delegated_inode
= NULL
;
279 const void *orig_value
= value
;
282 if (size
&& strcmp(name
, XATTR_NAME_CAPS
) == 0) {
283 error
= cap_convert_nscap(dentry
, &value
, size
);
291 error
= __vfs_setxattr_locked(dentry
, name
, value
, size
, flags
,
295 if (delegated_inode
) {
296 error
= break_deleg_wait(&delegated_inode
);
300 if (value
!= orig_value
)
305 EXPORT_SYMBOL_GPL(vfs_setxattr
);
308 xattr_getsecurity(struct inode
*inode
, const char *name
, void *value
,
314 if (!value
|| !size
) {
315 len
= security_inode_getsecurity(inode
, name
, &buffer
, false);
319 len
= security_inode_getsecurity(inode
, name
, &buffer
, true);
326 memcpy(value
, buffer
, len
);
334 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
336 * Allocate memory, if not already allocated, or re-allocate correct size,
337 * before retrieving the extended attribute.
339 * Returns the result of alloc, if failed, or the getxattr operation.
342 vfs_getxattr_alloc(struct dentry
*dentry
, const char *name
, char **xattr_value
,
343 size_t xattr_size
, gfp_t flags
)
345 const struct xattr_handler
*handler
;
346 struct inode
*inode
= dentry
->d_inode
;
347 char *value
= *xattr_value
;
350 error
= xattr_permission(inode
, name
, MAY_READ
);
354 handler
= xattr_resolve_name(inode
, &name
);
356 return PTR_ERR(handler
);
359 error
= handler
->get(handler
, dentry
, inode
, name
, NULL
, 0);
363 if (!value
|| (error
> xattr_size
)) {
364 value
= krealloc(*xattr_value
, error
+ 1, flags
);
367 memset(value
, 0, error
+ 1);
370 error
= handler
->get(handler
, dentry
, inode
, name
, value
, error
);
371 *xattr_value
= value
;
376 __vfs_getxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
377 void *value
, size_t size
)
379 const struct xattr_handler
*handler
;
381 handler
= xattr_resolve_name(inode
, &name
);
383 return PTR_ERR(handler
);
386 return handler
->get(handler
, dentry
, inode
, name
, value
, size
);
388 EXPORT_SYMBOL(__vfs_getxattr
);
391 vfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
, size_t size
)
393 struct inode
*inode
= dentry
->d_inode
;
396 error
= xattr_permission(inode
, name
, MAY_READ
);
400 error
= security_inode_getxattr(dentry
, name
);
404 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
405 XATTR_SECURITY_PREFIX_LEN
)) {
406 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
407 int ret
= xattr_getsecurity(inode
, suffix
, value
, size
);
409 * Only overwrite the return value if a security module
410 * is actually active.
412 if (ret
== -EOPNOTSUPP
)
417 return __vfs_getxattr(dentry
, inode
, name
, value
, size
);
419 EXPORT_SYMBOL_GPL(vfs_getxattr
);
422 vfs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
424 struct inode
*inode
= d_inode(dentry
);
427 error
= security_inode_listxattr(dentry
);
430 if (inode
->i_op
->listxattr
&& (inode
->i_opflags
& IOP_XATTR
)) {
431 error
= inode
->i_op
->listxattr(dentry
, list
, size
);
433 error
= security_inode_listsecurity(inode
, list
, size
);
434 if (size
&& error
> size
)
439 EXPORT_SYMBOL_GPL(vfs_listxattr
);
442 __vfs_removexattr(struct dentry
*dentry
, const char *name
)
444 struct inode
*inode
= d_inode(dentry
);
445 const struct xattr_handler
*handler
;
447 handler
= xattr_resolve_name(inode
, &name
);
449 return PTR_ERR(handler
);
452 return handler
->set(handler
, dentry
, inode
, name
, NULL
, 0, XATTR_REPLACE
);
454 EXPORT_SYMBOL(__vfs_removexattr
);
457 * __vfs_removexattr_locked - set an extended attribute while holding the inode
460 * @dentry: object to perform setxattr on
461 * @name: name of xattr to remove
462 * @delegated_inode: on return, will contain an inode pointer that
463 * a delegation was broken on, NULL if none.
466 __vfs_removexattr_locked(struct dentry
*dentry
, const char *name
,
467 struct inode
**delegated_inode
)
469 struct inode
*inode
= dentry
->d_inode
;
472 error
= xattr_permission(inode
, name
, MAY_WRITE
);
476 error
= security_inode_removexattr(dentry
, name
);
480 error
= try_break_deleg(inode
, delegated_inode
);
484 error
= __vfs_removexattr(dentry
, name
);
487 fsnotify_xattr(dentry
);
488 evm_inode_post_removexattr(dentry
, name
);
494 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked
);
497 vfs_removexattr(struct dentry
*dentry
, const char *name
)
499 struct inode
*inode
= dentry
->d_inode
;
500 struct inode
*delegated_inode
= NULL
;
505 error
= __vfs_removexattr_locked(dentry
, name
, &delegated_inode
);
508 if (delegated_inode
) {
509 error
= break_deleg_wait(&delegated_inode
);
516 EXPORT_SYMBOL_GPL(vfs_removexattr
);
519 * Extended attribute SET operations
522 setxattr(struct dentry
*d
, const char __user
*name
, const void __user
*value
,
523 size_t size
, int flags
)
527 char kname
[XATTR_NAME_MAX
+ 1];
529 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
532 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
533 if (error
== 0 || error
== sizeof(kname
))
539 if (size
> XATTR_SIZE_MAX
)
541 kvalue
= kvmalloc(size
, GFP_KERNEL
);
544 if (copy_from_user(kvalue
, value
, size
)) {
548 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
549 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
550 posix_acl_fix_xattr_from_user(kvalue
, size
);
553 error
= vfs_setxattr(d
, kname
, kvalue
, size
, flags
);
560 static int path_setxattr(const char __user
*pathname
,
561 const char __user
*name
, const void __user
*value
,
562 size_t size
, int flags
, unsigned int lookup_flags
)
567 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
570 error
= mnt_want_write(path
.mnt
);
572 error
= setxattr(path
.dentry
, name
, value
, size
, flags
);
573 mnt_drop_write(path
.mnt
);
576 if (retry_estale(error
, lookup_flags
)) {
577 lookup_flags
|= LOOKUP_REVAL
;
583 SYSCALL_DEFINE5(setxattr
, const char __user
*, pathname
,
584 const char __user
*, name
, const void __user
*, value
,
585 size_t, size
, int, flags
)
587 return path_setxattr(pathname
, name
, value
, size
, flags
, LOOKUP_FOLLOW
);
590 SYSCALL_DEFINE5(lsetxattr
, const char __user
*, pathname
,
591 const char __user
*, name
, const void __user
*, value
,
592 size_t, size
, int, flags
)
594 return path_setxattr(pathname
, name
, value
, size
, flags
, 0);
597 SYSCALL_DEFINE5(fsetxattr
, int, fd
, const char __user
*, name
,
598 const void __user
*,value
, size_t, size
, int, flags
)
600 struct fd f
= fdget(fd
);
606 error
= mnt_want_write_file(f
.file
);
608 error
= setxattr(f
.file
->f_path
.dentry
, name
, value
, size
, flags
);
609 mnt_drop_write_file(f
.file
);
616 * Extended attribute GET operations
619 getxattr(struct dentry
*d
, const char __user
*name
, void __user
*value
,
624 char kname
[XATTR_NAME_MAX
+ 1];
626 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
627 if (error
== 0 || error
== sizeof(kname
))
633 if (size
> XATTR_SIZE_MAX
)
634 size
= XATTR_SIZE_MAX
;
635 kvalue
= kvzalloc(size
, GFP_KERNEL
);
640 error
= vfs_getxattr(d
, kname
, kvalue
, size
);
642 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
643 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
644 posix_acl_fix_xattr_to_user(kvalue
, error
);
645 if (size
&& copy_to_user(value
, kvalue
, error
))
647 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
648 /* The file system tried to returned a value bigger
649 than XATTR_SIZE_MAX bytes. Not possible. */
658 static ssize_t
path_getxattr(const char __user
*pathname
,
659 const char __user
*name
, void __user
*value
,
660 size_t size
, unsigned int lookup_flags
)
665 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
668 error
= getxattr(path
.dentry
, name
, value
, size
);
670 if (retry_estale(error
, lookup_flags
)) {
671 lookup_flags
|= LOOKUP_REVAL
;
677 SYSCALL_DEFINE4(getxattr
, const char __user
*, pathname
,
678 const char __user
*, name
, void __user
*, value
, size_t, size
)
680 return path_getxattr(pathname
, name
, value
, size
, LOOKUP_FOLLOW
);
683 SYSCALL_DEFINE4(lgetxattr
, const char __user
*, pathname
,
684 const char __user
*, name
, void __user
*, value
, size_t, size
)
686 return path_getxattr(pathname
, name
, value
, size
, 0);
689 SYSCALL_DEFINE4(fgetxattr
, int, fd
, const char __user
*, name
,
690 void __user
*, value
, size_t, size
)
692 struct fd f
= fdget(fd
);
693 ssize_t error
= -EBADF
;
698 error
= getxattr(f
.file
->f_path
.dentry
, name
, value
, size
);
704 * Extended attribute LIST operations
707 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
713 if (size
> XATTR_LIST_MAX
)
714 size
= XATTR_LIST_MAX
;
715 klist
= kvmalloc(size
, GFP_KERNEL
);
720 error
= vfs_listxattr(d
, klist
, size
);
722 if (size
&& copy_to_user(list
, klist
, error
))
724 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
725 /* The file system tried to returned a list bigger
726 than XATTR_LIST_MAX bytes. Not possible. */
735 static ssize_t
path_listxattr(const char __user
*pathname
, char __user
*list
,
736 size_t size
, unsigned int lookup_flags
)
741 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
744 error
= listxattr(path
.dentry
, list
, size
);
746 if (retry_estale(error
, lookup_flags
)) {
747 lookup_flags
|= LOOKUP_REVAL
;
753 SYSCALL_DEFINE3(listxattr
, const char __user
*, pathname
, char __user
*, list
,
756 return path_listxattr(pathname
, list
, size
, LOOKUP_FOLLOW
);
759 SYSCALL_DEFINE3(llistxattr
, const char __user
*, pathname
, char __user
*, list
,
762 return path_listxattr(pathname
, list
, size
, 0);
765 SYSCALL_DEFINE3(flistxattr
, int, fd
, char __user
*, list
, size_t, size
)
767 struct fd f
= fdget(fd
);
768 ssize_t error
= -EBADF
;
773 error
= listxattr(f
.file
->f_path
.dentry
, list
, size
);
779 * Extended attribute REMOVE operations
782 removexattr(struct dentry
*d
, const char __user
*name
)
785 char kname
[XATTR_NAME_MAX
+ 1];
787 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
788 if (error
== 0 || error
== sizeof(kname
))
793 return vfs_removexattr(d
, kname
);
796 static int path_removexattr(const char __user
*pathname
,
797 const char __user
*name
, unsigned int lookup_flags
)
802 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
805 error
= mnt_want_write(path
.mnt
);
807 error
= removexattr(path
.dentry
, name
);
808 mnt_drop_write(path
.mnt
);
811 if (retry_estale(error
, lookup_flags
)) {
812 lookup_flags
|= LOOKUP_REVAL
;
818 SYSCALL_DEFINE2(removexattr
, const char __user
*, pathname
,
819 const char __user
*, name
)
821 return path_removexattr(pathname
, name
, LOOKUP_FOLLOW
);
824 SYSCALL_DEFINE2(lremovexattr
, const char __user
*, pathname
,
825 const char __user
*, name
)
827 return path_removexattr(pathname
, name
, 0);
830 SYSCALL_DEFINE2(fremovexattr
, int, fd
, const char __user
*, name
)
832 struct fd f
= fdget(fd
);
838 error
= mnt_want_write_file(f
.file
);
840 error
= removexattr(f
.file
->f_path
.dentry
, name
);
841 mnt_drop_write_file(f
.file
);
848 * Combine the results of the list() operation from every xattr_handler in the
852 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
854 const struct xattr_handler
*handler
, **handlers
= dentry
->d_sb
->s_xattr
;
855 unsigned int size
= 0;
858 for_each_xattr_handler(handlers
, handler
) {
859 if (!handler
->name
||
860 (handler
->list
&& !handler
->list(dentry
)))
862 size
+= strlen(handler
->name
) + 1;
868 for_each_xattr_handler(handlers
, handler
) {
869 if (!handler
->name
||
870 (handler
->list
&& !handler
->list(dentry
)))
872 len
= strlen(handler
->name
);
873 if (len
+ 1 > buffer_size
)
875 memcpy(buf
, handler
->name
, len
+ 1);
877 buffer_size
-= len
+ 1;
883 EXPORT_SYMBOL(generic_listxattr
);
886 * xattr_full_name - Compute full attribute name from suffix
888 * @handler: handler of the xattr_handler operation
889 * @name: name passed to the xattr_handler operation
891 * The get and set xattr handler operations are called with the remainder of
892 * the attribute name after skipping the handler's prefix: for example, "foo"
893 * is passed to the get operation of a handler with prefix "user." to get
894 * attribute "user.foo". The full name is still "there" in the name though.
896 * Note: the list xattr handler operation when called from the vfs is passed a
897 * NULL name; some file systems use this operation internally, with varying
900 const char *xattr_full_name(const struct xattr_handler
*handler
,
903 size_t prefix_len
= strlen(xattr_prefix(handler
));
905 return name
- prefix_len
;
907 EXPORT_SYMBOL(xattr_full_name
);
910 * Allocate new xattr and copy in the value; but leave the name to callers.
912 struct simple_xattr
*simple_xattr_alloc(const void *value
, size_t size
)
914 struct simple_xattr
*new_xattr
;
918 len
= sizeof(*new_xattr
) + size
;
919 if (len
< sizeof(*new_xattr
))
922 new_xattr
= kvmalloc(len
, GFP_KERNEL
);
926 new_xattr
->size
= size
;
927 memcpy(new_xattr
->value
, value
, size
);
932 * xattr GET operation for in-memory/pseudo filesystems
934 int simple_xattr_get(struct simple_xattrs
*xattrs
, const char *name
,
935 void *buffer
, size_t size
)
937 struct simple_xattr
*xattr
;
940 spin_lock(&xattrs
->lock
);
941 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
942 if (strcmp(name
, xattr
->name
))
947 if (size
< xattr
->size
)
950 memcpy(buffer
, xattr
->value
, xattr
->size
);
954 spin_unlock(&xattrs
->lock
);
959 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
960 * @xattrs: target simple_xattr list
961 * @name: name of the extended attribute
962 * @value: value of the xattr. If %NULL, will remove the attribute.
963 * @size: size of the new xattr
964 * @flags: %XATTR_{CREATE|REPLACE}
965 * @removed_size: returns size of the removed xattr, -1 if none removed
967 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
968 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
969 * otherwise, fails with -ENODATA.
971 * Returns 0 on success, -errno on failure.
973 int simple_xattr_set(struct simple_xattrs
*xattrs
, const char *name
,
974 const void *value
, size_t size
, int flags
,
975 ssize_t
*removed_size
)
977 struct simple_xattr
*xattr
;
978 struct simple_xattr
*new_xattr
= NULL
;
984 /* value == NULL means remove */
986 new_xattr
= simple_xattr_alloc(value
, size
);
990 new_xattr
->name
= kstrdup(name
, GFP_KERNEL
);
991 if (!new_xattr
->name
) {
997 spin_lock(&xattrs
->lock
);
998 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
999 if (!strcmp(name
, xattr
->name
)) {
1000 if (flags
& XATTR_CREATE
) {
1003 } else if (new_xattr
) {
1004 list_replace(&xattr
->list
, &new_xattr
->list
);
1006 *removed_size
= xattr
->size
;
1008 list_del(&xattr
->list
);
1010 *removed_size
= xattr
->size
;
1015 if (flags
& XATTR_REPLACE
) {
1019 list_add(&new_xattr
->list
, &xattrs
->head
);
1023 spin_unlock(&xattrs
->lock
);
1032 static bool xattr_is_trusted(const char *name
)
1034 return !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
);
1037 static int xattr_list_one(char **buffer
, ssize_t
*remaining_size
,
1040 size_t len
= strlen(name
) + 1;
1042 if (*remaining_size
< len
)
1044 memcpy(*buffer
, name
, len
);
1047 *remaining_size
-= len
;
1052 * xattr LIST operation for in-memory/pseudo filesystems
1054 ssize_t
simple_xattr_list(struct inode
*inode
, struct simple_xattrs
*xattrs
,
1055 char *buffer
, size_t size
)
1057 bool trusted
= capable(CAP_SYS_ADMIN
);
1058 struct simple_xattr
*xattr
;
1059 ssize_t remaining_size
= size
;
1062 #ifdef CONFIG_FS_POSIX_ACL
1063 if (IS_POSIXACL(inode
)) {
1065 err
= xattr_list_one(&buffer
, &remaining_size
,
1066 XATTR_NAME_POSIX_ACL_ACCESS
);
1070 if (inode
->i_default_acl
) {
1071 err
= xattr_list_one(&buffer
, &remaining_size
,
1072 XATTR_NAME_POSIX_ACL_DEFAULT
);
1079 spin_lock(&xattrs
->lock
);
1080 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1081 /* skip "trusted." attributes for unprivileged callers */
1082 if (!trusted
&& xattr_is_trusted(xattr
->name
))
1085 err
= xattr_list_one(&buffer
, &remaining_size
, xattr
->name
);
1089 spin_unlock(&xattrs
->lock
);
1091 return err
? err
: size
- remaining_size
;
1095 * Adds an extended attribute to the list
1097 void simple_xattr_list_add(struct simple_xattrs
*xattrs
,
1098 struct simple_xattr
*new_xattr
)
1100 spin_lock(&xattrs
->lock
);
1101 list_add(&new_xattr
->list
, &xattrs
->head
);
1102 spin_unlock(&xattrs
->lock
);