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
);
233 EXPORT_SYMBOL_GPL(__vfs_setxattr_noperm
);
236 * __vfs_setxattr_locked - set an extended attribute while holding the inode
239 * @dentry: object to perform setxattr on
240 * @name: xattr name to set
241 * @value: value to set @name to
242 * @size: size of @value
243 * @flags: flags to pass into filesystem operations
244 * @delegated_inode: on return, will contain an inode pointer that
245 * a delegation was broken on, NULL if none.
248 __vfs_setxattr_locked(struct dentry
*dentry
, const char *name
,
249 const void *value
, size_t size
, int flags
,
250 struct inode
**delegated_inode
)
252 struct inode
*inode
= dentry
->d_inode
;
255 error
= xattr_permission(inode
, name
, MAY_WRITE
);
259 error
= security_inode_setxattr(dentry
, name
, value
, size
, flags
);
263 error
= try_break_deleg(inode
, delegated_inode
);
267 error
= __vfs_setxattr_noperm(dentry
, name
, value
, size
, flags
);
272 EXPORT_SYMBOL_GPL(__vfs_setxattr_locked
);
275 vfs_setxattr(struct dentry
*dentry
, const char *name
, const void *value
,
276 size_t size
, int flags
)
278 struct inode
*inode
= dentry
->d_inode
;
279 struct inode
*delegated_inode
= NULL
;
280 const void *orig_value
= value
;
283 if (size
&& strcmp(name
, XATTR_NAME_CAPS
) == 0) {
284 error
= cap_convert_nscap(dentry
, &value
, size
);
292 error
= __vfs_setxattr_locked(dentry
, name
, value
, size
, flags
,
296 if (delegated_inode
) {
297 error
= break_deleg_wait(&delegated_inode
);
301 if (value
!= orig_value
)
306 EXPORT_SYMBOL_GPL(vfs_setxattr
);
309 xattr_getsecurity(struct inode
*inode
, const char *name
, void *value
,
315 if (!value
|| !size
) {
316 len
= security_inode_getsecurity(inode
, name
, &buffer
, false);
320 len
= security_inode_getsecurity(inode
, name
, &buffer
, true);
327 memcpy(value
, buffer
, len
);
335 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
337 * Allocate memory, if not already allocated, or re-allocate correct size,
338 * before retrieving the extended attribute.
340 * Returns the result of alloc, if failed, or the getxattr operation.
343 vfs_getxattr_alloc(struct dentry
*dentry
, const char *name
, char **xattr_value
,
344 size_t xattr_size
, gfp_t flags
)
346 const struct xattr_handler
*handler
;
347 struct inode
*inode
= dentry
->d_inode
;
348 char *value
= *xattr_value
;
351 error
= xattr_permission(inode
, name
, MAY_READ
);
355 handler
= xattr_resolve_name(inode
, &name
);
357 return PTR_ERR(handler
);
360 error
= handler
->get(handler
, dentry
, inode
, name
, NULL
, 0);
364 if (!value
|| (error
> xattr_size
)) {
365 value
= krealloc(*xattr_value
, error
+ 1, flags
);
368 memset(value
, 0, error
+ 1);
371 error
= handler
->get(handler
, dentry
, inode
, name
, value
, error
);
372 *xattr_value
= value
;
375 EXPORT_SYMBOL_GPL(vfs_getxattr_alloc
);
378 __vfs_getxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
379 void *value
, size_t size
)
381 const struct xattr_handler
*handler
;
383 handler
= xattr_resolve_name(inode
, &name
);
385 return PTR_ERR(handler
);
388 return handler
->get(handler
, dentry
, inode
, name
, value
, size
);
390 EXPORT_SYMBOL(__vfs_getxattr
);
393 vfs_getxattr(struct dentry
*dentry
, const char *name
, void *value
, size_t size
)
395 struct inode
*inode
= dentry
->d_inode
;
398 error
= xattr_permission(inode
, name
, MAY_READ
);
402 error
= security_inode_getxattr(dentry
, name
);
406 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
407 XATTR_SECURITY_PREFIX_LEN
)) {
408 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
409 int ret
= xattr_getsecurity(inode
, suffix
, value
, size
);
411 * Only overwrite the return value if a security module
412 * is actually active.
414 if (ret
== -EOPNOTSUPP
)
419 return __vfs_getxattr(dentry
, inode
, name
, value
, size
);
421 EXPORT_SYMBOL_GPL(vfs_getxattr
);
424 vfs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
426 struct inode
*inode
= d_inode(dentry
);
429 error
= security_inode_listxattr(dentry
);
432 if (inode
->i_op
->listxattr
&& (inode
->i_opflags
& IOP_XATTR
)) {
433 error
= inode
->i_op
->listxattr(dentry
, list
, size
);
435 error
= security_inode_listsecurity(inode
, list
, size
);
436 if (size
&& error
> size
)
441 EXPORT_SYMBOL_GPL(vfs_listxattr
);
444 __vfs_removexattr(struct dentry
*dentry
, const char *name
)
446 struct inode
*inode
= d_inode(dentry
);
447 const struct xattr_handler
*handler
;
449 handler
= xattr_resolve_name(inode
, &name
);
451 return PTR_ERR(handler
);
454 return handler
->set(handler
, dentry
, inode
, name
, NULL
, 0, XATTR_REPLACE
);
456 EXPORT_SYMBOL(__vfs_removexattr
);
459 * __vfs_removexattr_noperm - perform removexattr operation without
460 * performing permission checks.
462 * @dentry - object to perform setxattr on
463 * @name - xattr name to set
465 * returns the result of the internal setxattr or setsecurity operations.
467 * This function requires the caller to lock the inode's i_mutex before it
468 * is executed. It also assumes that the caller will make the appropriate
472 __vfs_removexattr_noperm(struct dentry
*dentry
, const char *name
)
476 error
=__vfs_removexattr(dentry
, name
);
478 fsnotify_xattr(dentry
);
479 evm_inode_post_removexattr(dentry
, name
);
483 EXPORT_SYMBOL_GPL(__vfs_removexattr_noperm
);
486 * __vfs_removexattr_locked - set an extended attribute while holding the inode
489 * @dentry: object to perform setxattr on
490 * @name: name of xattr to remove
491 * @delegated_inode: on return, will contain an inode pointer that
492 * a delegation was broken on, NULL if none.
495 __vfs_removexattr_locked(struct dentry
*dentry
, const char *name
,
496 struct inode
**delegated_inode
)
498 struct inode
*inode
= dentry
->d_inode
;
501 error
= xattr_permission(inode
, name
, MAY_WRITE
);
505 error
= security_inode_removexattr(dentry
, name
);
509 error
= try_break_deleg(inode
, delegated_inode
);
513 error
= __vfs_removexattr_noperm(dentry
, name
);
518 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked
);
521 vfs_removexattr(struct dentry
*dentry
, const char *name
)
523 struct inode
*inode
= dentry
->d_inode
;
524 struct inode
*delegated_inode
= NULL
;
529 error
= __vfs_removexattr_locked(dentry
, name
, &delegated_inode
);
532 if (delegated_inode
) {
533 error
= break_deleg_wait(&delegated_inode
);
540 EXPORT_SYMBOL_GPL(vfs_removexattr
);
543 * Extended attribute SET operations
546 setxattr(struct dentry
*d
, const char __user
*name
, const void __user
*value
,
547 size_t size
, int flags
)
551 char kname
[XATTR_NAME_MAX
+ 1];
553 if (flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
556 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
557 if (error
== 0 || error
== sizeof(kname
))
563 if (size
> XATTR_SIZE_MAX
)
565 kvalue
= kvmalloc(size
, GFP_KERNEL
);
568 if (copy_from_user(kvalue
, value
, size
)) {
572 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
573 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
574 posix_acl_fix_xattr_from_user(kvalue
, size
);
577 error
= vfs_setxattr(d
, kname
, kvalue
, size
, flags
);
584 static int path_setxattr(const char __user
*pathname
,
585 const char __user
*name
, const void __user
*value
,
586 size_t size
, int flags
, unsigned int lookup_flags
)
591 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
594 error
= mnt_want_write(path
.mnt
);
596 error
= setxattr(path
.dentry
, name
, value
, size
, flags
);
597 mnt_drop_write(path
.mnt
);
600 if (retry_estale(error
, lookup_flags
)) {
601 lookup_flags
|= LOOKUP_REVAL
;
607 SYSCALL_DEFINE5(setxattr
, const char __user
*, pathname
,
608 const char __user
*, name
, const void __user
*, value
,
609 size_t, size
, int, flags
)
611 return path_setxattr(pathname
, name
, value
, size
, flags
, LOOKUP_FOLLOW
);
614 SYSCALL_DEFINE5(lsetxattr
, const char __user
*, pathname
,
615 const char __user
*, name
, const void __user
*, value
,
616 size_t, size
, int, flags
)
618 return path_setxattr(pathname
, name
, value
, size
, flags
, 0);
621 SYSCALL_DEFINE5(fsetxattr
, int, fd
, const char __user
*, name
,
622 const void __user
*,value
, size_t, size
, int, flags
)
624 struct fd f
= fdget(fd
);
630 error
= mnt_want_write_file(f
.file
);
632 error
= setxattr(f
.file
->f_path
.dentry
, name
, value
, size
, flags
);
633 mnt_drop_write_file(f
.file
);
640 * Extended attribute GET operations
643 getxattr(struct dentry
*d
, const char __user
*name
, void __user
*value
,
648 char kname
[XATTR_NAME_MAX
+ 1];
650 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
651 if (error
== 0 || error
== sizeof(kname
))
657 if (size
> XATTR_SIZE_MAX
)
658 size
= XATTR_SIZE_MAX
;
659 kvalue
= kvzalloc(size
, GFP_KERNEL
);
664 error
= vfs_getxattr(d
, kname
, kvalue
, size
);
666 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
667 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
668 posix_acl_fix_xattr_to_user(kvalue
, error
);
669 if (size
&& copy_to_user(value
, kvalue
, error
))
671 } else if (error
== -ERANGE
&& size
>= XATTR_SIZE_MAX
) {
672 /* The file system tried to returned a value bigger
673 than XATTR_SIZE_MAX bytes. Not possible. */
682 static ssize_t
path_getxattr(const char __user
*pathname
,
683 const char __user
*name
, void __user
*value
,
684 size_t size
, unsigned int lookup_flags
)
689 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
692 error
= getxattr(path
.dentry
, name
, value
, size
);
694 if (retry_estale(error
, lookup_flags
)) {
695 lookup_flags
|= LOOKUP_REVAL
;
701 SYSCALL_DEFINE4(getxattr
, const char __user
*, pathname
,
702 const char __user
*, name
, void __user
*, value
, size_t, size
)
704 return path_getxattr(pathname
, name
, value
, size
, LOOKUP_FOLLOW
);
707 SYSCALL_DEFINE4(lgetxattr
, const char __user
*, pathname
,
708 const char __user
*, name
, void __user
*, value
, size_t, size
)
710 return path_getxattr(pathname
, name
, value
, size
, 0);
713 SYSCALL_DEFINE4(fgetxattr
, int, fd
, const char __user
*, name
,
714 void __user
*, value
, size_t, size
)
716 struct fd f
= fdget(fd
);
717 ssize_t error
= -EBADF
;
722 error
= getxattr(f
.file
->f_path
.dentry
, name
, value
, size
);
728 * Extended attribute LIST operations
731 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
737 if (size
> XATTR_LIST_MAX
)
738 size
= XATTR_LIST_MAX
;
739 klist
= kvmalloc(size
, GFP_KERNEL
);
744 error
= vfs_listxattr(d
, klist
, size
);
746 if (size
&& copy_to_user(list
, klist
, error
))
748 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
749 /* The file system tried to returned a list bigger
750 than XATTR_LIST_MAX bytes. Not possible. */
759 static ssize_t
path_listxattr(const char __user
*pathname
, char __user
*list
,
760 size_t size
, unsigned int lookup_flags
)
765 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
768 error
= listxattr(path
.dentry
, list
, size
);
770 if (retry_estale(error
, lookup_flags
)) {
771 lookup_flags
|= LOOKUP_REVAL
;
777 SYSCALL_DEFINE3(listxattr
, const char __user
*, pathname
, char __user
*, list
,
780 return path_listxattr(pathname
, list
, size
, LOOKUP_FOLLOW
);
783 SYSCALL_DEFINE3(llistxattr
, const char __user
*, pathname
, char __user
*, list
,
786 return path_listxattr(pathname
, list
, size
, 0);
789 SYSCALL_DEFINE3(flistxattr
, int, fd
, char __user
*, list
, size_t, size
)
791 struct fd f
= fdget(fd
);
792 ssize_t error
= -EBADF
;
797 error
= listxattr(f
.file
->f_path
.dentry
, list
, size
);
803 * Extended attribute REMOVE operations
806 removexattr(struct dentry
*d
, const char __user
*name
)
809 char kname
[XATTR_NAME_MAX
+ 1];
811 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
812 if (error
== 0 || error
== sizeof(kname
))
817 return vfs_removexattr(d
, kname
);
820 static int path_removexattr(const char __user
*pathname
,
821 const char __user
*name
, unsigned int lookup_flags
)
826 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
829 error
= mnt_want_write(path
.mnt
);
831 error
= removexattr(path
.dentry
, name
);
832 mnt_drop_write(path
.mnt
);
835 if (retry_estale(error
, lookup_flags
)) {
836 lookup_flags
|= LOOKUP_REVAL
;
842 SYSCALL_DEFINE2(removexattr
, const char __user
*, pathname
,
843 const char __user
*, name
)
845 return path_removexattr(pathname
, name
, LOOKUP_FOLLOW
);
848 SYSCALL_DEFINE2(lremovexattr
, const char __user
*, pathname
,
849 const char __user
*, name
)
851 return path_removexattr(pathname
, name
, 0);
854 SYSCALL_DEFINE2(fremovexattr
, int, fd
, const char __user
*, name
)
856 struct fd f
= fdget(fd
);
862 error
= mnt_want_write_file(f
.file
);
864 error
= removexattr(f
.file
->f_path
.dentry
, name
);
865 mnt_drop_write_file(f
.file
);
872 * Combine the results of the list() operation from every xattr_handler in the
876 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
878 const struct xattr_handler
*handler
, **handlers
= dentry
->d_sb
->s_xattr
;
879 unsigned int size
= 0;
882 for_each_xattr_handler(handlers
, handler
) {
883 if (!handler
->name
||
884 (handler
->list
&& !handler
->list(dentry
)))
886 size
+= strlen(handler
->name
) + 1;
892 for_each_xattr_handler(handlers
, handler
) {
893 if (!handler
->name
||
894 (handler
->list
&& !handler
->list(dentry
)))
896 len
= strlen(handler
->name
);
897 if (len
+ 1 > buffer_size
)
899 memcpy(buf
, handler
->name
, len
+ 1);
901 buffer_size
-= len
+ 1;
907 EXPORT_SYMBOL(generic_listxattr
);
910 * xattr_full_name - Compute full attribute name from suffix
912 * @handler: handler of the xattr_handler operation
913 * @name: name passed to the xattr_handler operation
915 * The get and set xattr handler operations are called with the remainder of
916 * the attribute name after skipping the handler's prefix: for example, "foo"
917 * is passed to the get operation of a handler with prefix "user." to get
918 * attribute "user.foo". The full name is still "there" in the name though.
920 * Note: the list xattr handler operation when called from the vfs is passed a
921 * NULL name; some file systems use this operation internally, with varying
924 const char *xattr_full_name(const struct xattr_handler
*handler
,
927 size_t prefix_len
= strlen(xattr_prefix(handler
));
929 return name
- prefix_len
;
931 EXPORT_SYMBOL(xattr_full_name
);
934 * Allocate new xattr and copy in the value; but leave the name to callers.
936 struct simple_xattr
*simple_xattr_alloc(const void *value
, size_t size
)
938 struct simple_xattr
*new_xattr
;
942 len
= sizeof(*new_xattr
) + size
;
943 if (len
< sizeof(*new_xattr
))
946 new_xattr
= kvmalloc(len
, GFP_KERNEL
);
950 new_xattr
->size
= size
;
951 memcpy(new_xattr
->value
, value
, size
);
956 * xattr GET operation for in-memory/pseudo filesystems
958 int simple_xattr_get(struct simple_xattrs
*xattrs
, const char *name
,
959 void *buffer
, size_t size
)
961 struct simple_xattr
*xattr
;
964 spin_lock(&xattrs
->lock
);
965 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
966 if (strcmp(name
, xattr
->name
))
971 if (size
< xattr
->size
)
974 memcpy(buffer
, xattr
->value
, xattr
->size
);
978 spin_unlock(&xattrs
->lock
);
983 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
984 * @xattrs: target simple_xattr list
985 * @name: name of the extended attribute
986 * @value: value of the xattr. If %NULL, will remove the attribute.
987 * @size: size of the new xattr
988 * @flags: %XATTR_{CREATE|REPLACE}
989 * @removed_size: returns size of the removed xattr, -1 if none removed
991 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
992 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
993 * otherwise, fails with -ENODATA.
995 * Returns 0 on success, -errno on failure.
997 int simple_xattr_set(struct simple_xattrs
*xattrs
, const char *name
,
998 const void *value
, size_t size
, int flags
,
999 ssize_t
*removed_size
)
1001 struct simple_xattr
*xattr
;
1002 struct simple_xattr
*new_xattr
= NULL
;
1008 /* value == NULL means remove */
1010 new_xattr
= simple_xattr_alloc(value
, size
);
1014 new_xattr
->name
= kstrdup(name
, GFP_KERNEL
);
1015 if (!new_xattr
->name
) {
1021 spin_lock(&xattrs
->lock
);
1022 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1023 if (!strcmp(name
, xattr
->name
)) {
1024 if (flags
& XATTR_CREATE
) {
1027 } else if (new_xattr
) {
1028 list_replace(&xattr
->list
, &new_xattr
->list
);
1030 *removed_size
= xattr
->size
;
1032 list_del(&xattr
->list
);
1034 *removed_size
= xattr
->size
;
1039 if (flags
& XATTR_REPLACE
) {
1043 list_add(&new_xattr
->list
, &xattrs
->head
);
1047 spin_unlock(&xattrs
->lock
);
1056 static bool xattr_is_trusted(const char *name
)
1058 return !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
);
1061 static int xattr_list_one(char **buffer
, ssize_t
*remaining_size
,
1064 size_t len
= strlen(name
) + 1;
1066 if (*remaining_size
< len
)
1068 memcpy(*buffer
, name
, len
);
1071 *remaining_size
-= len
;
1076 * xattr LIST operation for in-memory/pseudo filesystems
1078 ssize_t
simple_xattr_list(struct inode
*inode
, struct simple_xattrs
*xattrs
,
1079 char *buffer
, size_t size
)
1081 bool trusted
= capable(CAP_SYS_ADMIN
);
1082 struct simple_xattr
*xattr
;
1083 ssize_t remaining_size
= size
;
1086 #ifdef CONFIG_FS_POSIX_ACL
1087 if (IS_POSIXACL(inode
)) {
1089 err
= xattr_list_one(&buffer
, &remaining_size
,
1090 XATTR_NAME_POSIX_ACL_ACCESS
);
1094 if (inode
->i_default_acl
) {
1095 err
= xattr_list_one(&buffer
, &remaining_size
,
1096 XATTR_NAME_POSIX_ACL_DEFAULT
);
1103 spin_lock(&xattrs
->lock
);
1104 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1105 /* skip "trusted." attributes for unprivileged callers */
1106 if (!trusted
&& xattr_is_trusted(xattr
->name
))
1109 err
= xattr_list_one(&buffer
, &remaining_size
, xattr
->name
);
1113 spin_unlock(&xattrs
->lock
);
1115 return err
? err
: size
- remaining_size
;
1119 * Adds an extended attribute to the list
1121 void simple_xattr_list_add(struct simple_xattrs
*xattrs
,
1122 struct simple_xattr
*new_xattr
)
1124 spin_lock(&xattrs
->lock
);
1125 list_add(&new_xattr
->list
, &xattrs
->head
);
1126 spin_unlock(&xattrs
->lock
);