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>
31 strcmp_prefix(const char *a
, const char *a_prefix
)
33 while (*a_prefix
&& *a
== *a_prefix
) {
37 return *a_prefix
? NULL
: a
;
41 * In order to implement different sets of xattr operations for each xattr
42 * prefix, a filesystem should create a null-terminated array of struct
43 * xattr_handler (one for each prefix) and hang a pointer to it off of the
44 * s_xattr field of the superblock.
46 #define for_each_xattr_handler(handlers, handler) \
48 for ((handler) = *(handlers)++; \
50 (handler) = *(handlers)++)
53 * Find the xattr_handler with the matching prefix.
55 static const struct xattr_handler
*
56 xattr_resolve_name(struct inode
*inode
, const char **name
)
58 const struct xattr_handler
**handlers
= inode
->i_sb
->s_xattr
;
59 const struct xattr_handler
*handler
;
61 if (!(inode
->i_opflags
& IOP_XATTR
)) {
62 if (unlikely(is_bad_inode(inode
)))
64 return ERR_PTR(-EOPNOTSUPP
);
66 for_each_xattr_handler(handlers
, handler
) {
69 n
= strcmp_prefix(*name
, xattr_prefix(handler
));
71 if (!handler
->prefix
^ !*n
) {
74 return ERR_PTR(-EINVAL
);
80 return ERR_PTR(-EOPNOTSUPP
);
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
88 xattr_permission(struct user_namespace
*mnt_userns
, struct inode
*inode
,
89 const char *name
, int mask
)
92 * We can never set or remove an extended attribute on a read-only
93 * filesystem or on an immutable / append-only inode.
95 if (mask
& MAY_WRITE
) {
96 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
99 * Updating an xattr will likely cause i_uid and i_gid
100 * to be writen back improperly if their true value is
101 * unknown to the vfs.
103 if (HAS_UNMAPPED_ID(mnt_userns
, inode
))
108 * No restriction for security.* and system.* from the VFS. Decision
109 * on these is left to the underlying filesystem / security module.
111 if (!strncmp(name
, XATTR_SECURITY_PREFIX
, XATTR_SECURITY_PREFIX_LEN
) ||
112 !strncmp(name
, XATTR_SYSTEM_PREFIX
, XATTR_SYSTEM_PREFIX_LEN
))
116 * The trusted.* namespace can only be accessed by privileged users.
118 if (!strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
)) {
119 if (!capable(CAP_SYS_ADMIN
))
120 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
125 * In the user.* namespace, only regular files and directories can have
126 * extended attributes. For sticky directories, only the owner and
127 * privileged users can write attributes.
129 if (!strncmp(name
, XATTR_USER_PREFIX
, XATTR_USER_PREFIX_LEN
)) {
130 if (!S_ISREG(inode
->i_mode
) && !S_ISDIR(inode
->i_mode
))
131 return (mask
& MAY_WRITE
) ? -EPERM
: -ENODATA
;
132 if (S_ISDIR(inode
->i_mode
) && (inode
->i_mode
& S_ISVTX
) &&
133 (mask
& MAY_WRITE
) &&
134 !inode_owner_or_capable(mnt_userns
, inode
))
138 return inode_permission(mnt_userns
, inode
, mask
);
142 * Look for any handler that deals with the specified namespace.
145 xattr_supported_namespace(struct inode
*inode
, const char *prefix
)
147 const struct xattr_handler
**handlers
= inode
->i_sb
->s_xattr
;
148 const struct xattr_handler
*handler
;
151 if (!(inode
->i_opflags
& IOP_XATTR
)) {
152 if (unlikely(is_bad_inode(inode
)))
157 preflen
= strlen(prefix
);
159 for_each_xattr_handler(handlers
, handler
) {
160 if (!strncmp(xattr_prefix(handler
), prefix
, preflen
))
166 EXPORT_SYMBOL(xattr_supported_namespace
);
169 __vfs_setxattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
170 struct inode
*inode
, const char *name
, const void *value
,
171 size_t size
, int flags
)
173 const struct xattr_handler
*handler
;
175 handler
= xattr_resolve_name(inode
, &name
);
177 return PTR_ERR(handler
);
181 value
= ""; /* empty EA, do not remove */
182 return handler
->set(handler
, mnt_userns
, dentry
, inode
, name
, value
,
185 EXPORT_SYMBOL(__vfs_setxattr
);
188 * __vfs_setxattr_noperm - perform setxattr operation without performing
191 * @mnt_userns: user namespace of the mount the inode was found from
192 * @dentry: object to perform setxattr on
193 * @name: xattr name to set
194 * @value: value to set @name to
195 * @size: size of @value
196 * @flags: flags to pass into filesystem operations
198 * returns the result of the internal setxattr or setsecurity operations.
200 * This function requires the caller to lock the inode's i_mutex before it
201 * is executed. It also assumes that the caller will make the appropriate
204 int __vfs_setxattr_noperm(struct user_namespace
*mnt_userns
,
205 struct dentry
*dentry
, const char *name
,
206 const void *value
, size_t size
, int flags
)
208 struct inode
*inode
= dentry
->d_inode
;
210 int issec
= !strncmp(name
, XATTR_SECURITY_PREFIX
,
211 XATTR_SECURITY_PREFIX_LEN
);
214 inode
->i_flags
&= ~S_NOSEC
;
215 if (inode
->i_opflags
& IOP_XATTR
) {
216 error
= __vfs_setxattr(mnt_userns
, dentry
, inode
, name
, value
,
219 fsnotify_xattr(dentry
);
220 security_inode_post_setxattr(dentry
, name
, value
,
224 if (unlikely(is_bad_inode(inode
)))
227 if (error
== -EAGAIN
) {
231 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
233 error
= security_inode_setsecurity(inode
, suffix
, value
,
236 fsnotify_xattr(dentry
);
244 * __vfs_setxattr_locked - set an extended attribute while holding the inode
247 * @mnt_userns: user namespace of the mount of the target inode
248 * @dentry: object to perform setxattr on
249 * @name: xattr name to set
250 * @value: value to set @name to
251 * @size: size of @value
252 * @flags: flags to pass into filesystem operations
253 * @delegated_inode: on return, will contain an inode pointer that
254 * a delegation was broken on, NULL if none.
257 __vfs_setxattr_locked(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
258 const char *name
, const void *value
, size_t size
,
259 int flags
, struct inode
**delegated_inode
)
261 struct inode
*inode
= dentry
->d_inode
;
264 error
= xattr_permission(mnt_userns
, inode
, name
, MAY_WRITE
);
268 error
= security_inode_setxattr(mnt_userns
, dentry
, name
, value
, size
,
273 error
= try_break_deleg(inode
, delegated_inode
);
277 error
= __vfs_setxattr_noperm(mnt_userns
, dentry
, name
, value
,
283 EXPORT_SYMBOL_GPL(__vfs_setxattr_locked
);
285 static inline bool is_posix_acl_xattr(const char *name
)
287 return (strcmp(name
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
288 (strcmp(name
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0);
292 vfs_setxattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
293 const char *name
, void *value
, size_t size
, int flags
)
295 struct inode
*inode
= dentry
->d_inode
;
296 struct inode
*delegated_inode
= NULL
;
297 const void *orig_value
= value
;
300 if (size
&& strcmp(name
, XATTR_NAME_CAPS
) == 0) {
301 error
= cap_convert_nscap(mnt_userns
, dentry
,
302 (const void **)&value
, size
);
308 if (size
&& is_posix_acl_xattr(name
))
309 posix_acl_setxattr_idmapped_mnt(mnt_userns
, inode
, value
, size
);
313 error
= __vfs_setxattr_locked(mnt_userns
, dentry
, name
, value
, size
,
314 flags
, &delegated_inode
);
317 if (delegated_inode
) {
318 error
= break_deleg_wait(&delegated_inode
);
322 if (value
!= orig_value
)
327 EXPORT_SYMBOL_GPL(vfs_setxattr
);
330 xattr_getsecurity(struct user_namespace
*mnt_userns
, struct inode
*inode
,
331 const char *name
, void *value
, size_t size
)
336 if (!value
|| !size
) {
337 len
= security_inode_getsecurity(mnt_userns
, inode
, name
,
342 len
= security_inode_getsecurity(mnt_userns
, inode
, name
, &buffer
,
350 memcpy(value
, buffer
, len
);
358 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
360 * Allocate memory, if not already allocated, or re-allocate correct size,
361 * before retrieving the extended attribute.
363 * Returns the result of alloc, if failed, or the getxattr operation.
366 vfs_getxattr_alloc(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
367 const char *name
, char **xattr_value
, size_t xattr_size
,
370 const struct xattr_handler
*handler
;
371 struct inode
*inode
= dentry
->d_inode
;
372 char *value
= *xattr_value
;
375 error
= xattr_permission(mnt_userns
, inode
, name
, MAY_READ
);
379 handler
= xattr_resolve_name(inode
, &name
);
381 return PTR_ERR(handler
);
384 error
= handler
->get(handler
, dentry
, inode
, name
, NULL
, 0);
388 if (!value
|| (error
> xattr_size
)) {
389 value
= krealloc(*xattr_value
, error
+ 1, flags
);
392 memset(value
, 0, error
+ 1);
395 error
= handler
->get(handler
, dentry
, inode
, name
, value
, error
);
396 *xattr_value
= value
;
401 __vfs_getxattr(struct dentry
*dentry
, struct inode
*inode
, const char *name
,
402 void *value
, size_t size
)
404 const struct xattr_handler
*handler
;
406 handler
= xattr_resolve_name(inode
, &name
);
408 return PTR_ERR(handler
);
411 return handler
->get(handler
, dentry
, inode
, name
, value
, size
);
413 EXPORT_SYMBOL(__vfs_getxattr
);
416 vfs_getxattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
417 const char *name
, void *value
, size_t size
)
419 struct inode
*inode
= dentry
->d_inode
;
422 error
= xattr_permission(mnt_userns
, inode
, name
, MAY_READ
);
426 error
= security_inode_getxattr(dentry
, name
);
430 if (!strncmp(name
, XATTR_SECURITY_PREFIX
,
431 XATTR_SECURITY_PREFIX_LEN
)) {
432 const char *suffix
= name
+ XATTR_SECURITY_PREFIX_LEN
;
433 int ret
= xattr_getsecurity(mnt_userns
, inode
, suffix
, value
,
436 * Only overwrite the return value if a security module
437 * is actually active.
439 if (ret
== -EOPNOTSUPP
)
444 error
= __vfs_getxattr(dentry
, inode
, name
, value
, size
);
445 if (error
> 0 && is_posix_acl_xattr(name
))
446 posix_acl_getxattr_idmapped_mnt(mnt_userns
, inode
, value
, size
);
449 EXPORT_SYMBOL_GPL(vfs_getxattr
);
452 vfs_listxattr(struct dentry
*dentry
, char *list
, size_t size
)
454 struct inode
*inode
= d_inode(dentry
);
457 error
= security_inode_listxattr(dentry
);
460 if (inode
->i_op
->listxattr
&& (inode
->i_opflags
& IOP_XATTR
)) {
461 error
= inode
->i_op
->listxattr(dentry
, list
, size
);
463 error
= security_inode_listsecurity(inode
, list
, size
);
464 if (size
&& error
> size
)
469 EXPORT_SYMBOL_GPL(vfs_listxattr
);
472 __vfs_removexattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
475 struct inode
*inode
= d_inode(dentry
);
476 const struct xattr_handler
*handler
;
478 handler
= xattr_resolve_name(inode
, &name
);
480 return PTR_ERR(handler
);
483 return handler
->set(handler
, mnt_userns
, dentry
, inode
, name
, NULL
, 0,
486 EXPORT_SYMBOL(__vfs_removexattr
);
489 * __vfs_removexattr_locked - set an extended attribute while holding the inode
492 * @mnt_userns: user namespace of the mount of the target inode
493 * @dentry: object to perform setxattr on
494 * @name: name of xattr to remove
495 * @delegated_inode: on return, will contain an inode pointer that
496 * a delegation was broken on, NULL if none.
499 __vfs_removexattr_locked(struct user_namespace
*mnt_userns
,
500 struct dentry
*dentry
, const char *name
,
501 struct inode
**delegated_inode
)
503 struct inode
*inode
= dentry
->d_inode
;
506 error
= xattr_permission(mnt_userns
, inode
, name
, MAY_WRITE
);
510 error
= security_inode_removexattr(mnt_userns
, dentry
, name
);
514 error
= try_break_deleg(inode
, delegated_inode
);
518 error
= __vfs_removexattr(mnt_userns
, dentry
, name
);
521 fsnotify_xattr(dentry
);
522 evm_inode_post_removexattr(dentry
, name
);
528 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked
);
531 vfs_removexattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
534 struct inode
*inode
= dentry
->d_inode
;
535 struct inode
*delegated_inode
= NULL
;
540 error
= __vfs_removexattr_locked(mnt_userns
, dentry
,
541 name
, &delegated_inode
);
544 if (delegated_inode
) {
545 error
= break_deleg_wait(&delegated_inode
);
552 EXPORT_SYMBOL_GPL(vfs_removexattr
);
555 * Extended attribute SET operations
558 int setxattr_copy(const char __user
*name
, struct xattr_ctx
*ctx
)
562 if (ctx
->flags
& ~(XATTR_CREATE
|XATTR_REPLACE
))
565 error
= strncpy_from_user(ctx
->kname
->name
, name
,
566 sizeof(ctx
->kname
->name
));
567 if (error
== 0 || error
== sizeof(ctx
->kname
->name
))
574 if (ctx
->size
> XATTR_SIZE_MAX
)
577 ctx
->kvalue
= vmemdup_user(ctx
->cvalue
, ctx
->size
);
578 if (IS_ERR(ctx
->kvalue
)) {
579 error
= PTR_ERR(ctx
->kvalue
);
587 static void setxattr_convert(struct user_namespace
*mnt_userns
,
588 struct dentry
*d
, struct xattr_ctx
*ctx
)
591 ((strcmp(ctx
->kname
->name
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
592 (strcmp(ctx
->kname
->name
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0)))
593 posix_acl_fix_xattr_from_user(ctx
->kvalue
, ctx
->size
);
596 int do_setxattr(struct user_namespace
*mnt_userns
, struct dentry
*dentry
,
597 struct xattr_ctx
*ctx
)
599 setxattr_convert(mnt_userns
, dentry
, ctx
);
600 return vfs_setxattr(mnt_userns
, dentry
, ctx
->kname
->name
,
601 ctx
->kvalue
, ctx
->size
, ctx
->flags
);
605 setxattr(struct user_namespace
*mnt_userns
, struct dentry
*d
,
606 const char __user
*name
, const void __user
*value
, size_t size
,
609 struct xattr_name kname
;
610 struct xattr_ctx ctx
= {
619 error
= setxattr_copy(name
, &ctx
);
623 error
= do_setxattr(mnt_userns
, d
, &ctx
);
629 static int path_setxattr(const char __user
*pathname
,
630 const char __user
*name
, const void __user
*value
,
631 size_t size
, int flags
, unsigned int lookup_flags
)
637 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
640 error
= mnt_want_write(path
.mnt
);
642 error
= setxattr(mnt_user_ns(path
.mnt
), path
.dentry
, name
,
644 mnt_drop_write(path
.mnt
);
647 if (retry_estale(error
, lookup_flags
)) {
648 lookup_flags
|= LOOKUP_REVAL
;
654 SYSCALL_DEFINE5(setxattr
, const char __user
*, pathname
,
655 const char __user
*, name
, const void __user
*, value
,
656 size_t, size
, int, flags
)
658 return path_setxattr(pathname
, name
, value
, size
, flags
, LOOKUP_FOLLOW
);
661 SYSCALL_DEFINE5(lsetxattr
, const char __user
*, pathname
,
662 const char __user
*, name
, const void __user
*, value
,
663 size_t, size
, int, flags
)
665 return path_setxattr(pathname
, name
, value
, size
, flags
, 0);
668 SYSCALL_DEFINE5(fsetxattr
, int, fd
, const char __user
*, name
,
669 const void __user
*,value
, size_t, size
, int, flags
)
671 struct fd f
= fdget(fd
);
677 error
= mnt_want_write_file(f
.file
);
679 error
= setxattr(file_mnt_user_ns(f
.file
),
680 f
.file
->f_path
.dentry
, name
,
682 mnt_drop_write_file(f
.file
);
689 * Extended attribute GET operations
692 do_getxattr(struct user_namespace
*mnt_userns
, struct dentry
*d
,
693 struct xattr_ctx
*ctx
)
696 char *kname
= ctx
->kname
->name
;
699 if (ctx
->size
> XATTR_SIZE_MAX
)
700 ctx
->size
= XATTR_SIZE_MAX
;
701 ctx
->kvalue
= kvzalloc(ctx
->size
, GFP_KERNEL
);
706 error
= vfs_getxattr(mnt_userns
, d
, kname
, ctx
->kvalue
, ctx
->size
);
708 if ((strcmp(kname
, XATTR_NAME_POSIX_ACL_ACCESS
) == 0) ||
709 (strcmp(kname
, XATTR_NAME_POSIX_ACL_DEFAULT
) == 0))
710 posix_acl_fix_xattr_to_user(ctx
->kvalue
, error
);
711 if (ctx
->size
&& copy_to_user(ctx
->value
, ctx
->kvalue
, error
))
713 } else if (error
== -ERANGE
&& ctx
->size
>= XATTR_SIZE_MAX
) {
714 /* The file system tried to returned a value bigger
715 than XATTR_SIZE_MAX bytes. Not possible. */
723 getxattr(struct user_namespace
*mnt_userns
, struct dentry
*d
,
724 const char __user
*name
, void __user
*value
, size_t size
)
727 struct xattr_name kname
;
728 struct xattr_ctx ctx
= {
736 error
= strncpy_from_user(kname
.name
, name
, sizeof(kname
.name
));
737 if (error
== 0 || error
== sizeof(kname
.name
))
742 error
= do_getxattr(mnt_userns
, d
, &ctx
);
748 static ssize_t
path_getxattr(const char __user
*pathname
,
749 const char __user
*name
, void __user
*value
,
750 size_t size
, unsigned int lookup_flags
)
755 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
758 error
= getxattr(mnt_user_ns(path
.mnt
), path
.dentry
, name
, value
, size
);
760 if (retry_estale(error
, lookup_flags
)) {
761 lookup_flags
|= LOOKUP_REVAL
;
767 SYSCALL_DEFINE4(getxattr
, const char __user
*, pathname
,
768 const char __user
*, name
, void __user
*, value
, size_t, size
)
770 return path_getxattr(pathname
, name
, value
, size
, LOOKUP_FOLLOW
);
773 SYSCALL_DEFINE4(lgetxattr
, const char __user
*, pathname
,
774 const char __user
*, name
, void __user
*, value
, size_t, size
)
776 return path_getxattr(pathname
, name
, value
, size
, 0);
779 SYSCALL_DEFINE4(fgetxattr
, int, fd
, const char __user
*, name
,
780 void __user
*, value
, size_t, size
)
782 struct fd f
= fdget(fd
);
783 ssize_t error
= -EBADF
;
788 error
= getxattr(file_mnt_user_ns(f
.file
), f
.file
->f_path
.dentry
,
795 * Extended attribute LIST operations
798 listxattr(struct dentry
*d
, char __user
*list
, size_t size
)
804 if (size
> XATTR_LIST_MAX
)
805 size
= XATTR_LIST_MAX
;
806 klist
= kvmalloc(size
, GFP_KERNEL
);
811 error
= vfs_listxattr(d
, klist
, size
);
813 if (size
&& copy_to_user(list
, klist
, error
))
815 } else if (error
== -ERANGE
&& size
>= XATTR_LIST_MAX
) {
816 /* The file system tried to returned a list bigger
817 than XATTR_LIST_MAX bytes. Not possible. */
826 static ssize_t
path_listxattr(const char __user
*pathname
, char __user
*list
,
827 size_t size
, unsigned int lookup_flags
)
832 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
835 error
= listxattr(path
.dentry
, list
, size
);
837 if (retry_estale(error
, lookup_flags
)) {
838 lookup_flags
|= LOOKUP_REVAL
;
844 SYSCALL_DEFINE3(listxattr
, const char __user
*, pathname
, char __user
*, list
,
847 return path_listxattr(pathname
, list
, size
, LOOKUP_FOLLOW
);
850 SYSCALL_DEFINE3(llistxattr
, const char __user
*, pathname
, char __user
*, list
,
853 return path_listxattr(pathname
, list
, size
, 0);
856 SYSCALL_DEFINE3(flistxattr
, int, fd
, char __user
*, list
, size_t, size
)
858 struct fd f
= fdget(fd
);
859 ssize_t error
= -EBADF
;
864 error
= listxattr(f
.file
->f_path
.dentry
, list
, size
);
870 * Extended attribute REMOVE operations
873 removexattr(struct user_namespace
*mnt_userns
, struct dentry
*d
,
874 const char __user
*name
)
877 char kname
[XATTR_NAME_MAX
+ 1];
879 error
= strncpy_from_user(kname
, name
, sizeof(kname
));
880 if (error
== 0 || error
== sizeof(kname
))
885 return vfs_removexattr(mnt_userns
, d
, kname
);
888 static int path_removexattr(const char __user
*pathname
,
889 const char __user
*name
, unsigned int lookup_flags
)
894 error
= user_path_at(AT_FDCWD
, pathname
, lookup_flags
, &path
);
897 error
= mnt_want_write(path
.mnt
);
899 error
= removexattr(mnt_user_ns(path
.mnt
), path
.dentry
, name
);
900 mnt_drop_write(path
.mnt
);
903 if (retry_estale(error
, lookup_flags
)) {
904 lookup_flags
|= LOOKUP_REVAL
;
910 SYSCALL_DEFINE2(removexattr
, const char __user
*, pathname
,
911 const char __user
*, name
)
913 return path_removexattr(pathname
, name
, LOOKUP_FOLLOW
);
916 SYSCALL_DEFINE2(lremovexattr
, const char __user
*, pathname
,
917 const char __user
*, name
)
919 return path_removexattr(pathname
, name
, 0);
922 SYSCALL_DEFINE2(fremovexattr
, int, fd
, const char __user
*, name
)
924 struct fd f
= fdget(fd
);
930 error
= mnt_want_write_file(f
.file
);
932 error
= removexattr(file_mnt_user_ns(f
.file
),
933 f
.file
->f_path
.dentry
, name
);
934 mnt_drop_write_file(f
.file
);
941 * Combine the results of the list() operation from every xattr_handler in the
945 generic_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
947 const struct xattr_handler
*handler
, **handlers
= dentry
->d_sb
->s_xattr
;
948 unsigned int size
= 0;
951 for_each_xattr_handler(handlers
, handler
) {
952 if (!handler
->name
||
953 (handler
->list
&& !handler
->list(dentry
)))
955 size
+= strlen(handler
->name
) + 1;
961 for_each_xattr_handler(handlers
, handler
) {
962 if (!handler
->name
||
963 (handler
->list
&& !handler
->list(dentry
)))
965 len
= strlen(handler
->name
);
966 if (len
+ 1 > buffer_size
)
968 memcpy(buf
, handler
->name
, len
+ 1);
970 buffer_size
-= len
+ 1;
976 EXPORT_SYMBOL(generic_listxattr
);
979 * xattr_full_name - Compute full attribute name from suffix
981 * @handler: handler of the xattr_handler operation
982 * @name: name passed to the xattr_handler operation
984 * The get and set xattr handler operations are called with the remainder of
985 * the attribute name after skipping the handler's prefix: for example, "foo"
986 * is passed to the get operation of a handler with prefix "user." to get
987 * attribute "user.foo". The full name is still "there" in the name though.
989 * Note: the list xattr handler operation when called from the vfs is passed a
990 * NULL name; some file systems use this operation internally, with varying
993 const char *xattr_full_name(const struct xattr_handler
*handler
,
996 size_t prefix_len
= strlen(xattr_prefix(handler
));
998 return name
- prefix_len
;
1000 EXPORT_SYMBOL(xattr_full_name
);
1003 * Allocate new xattr and copy in the value; but leave the name to callers.
1005 struct simple_xattr
*simple_xattr_alloc(const void *value
, size_t size
)
1007 struct simple_xattr
*new_xattr
;
1011 len
= sizeof(*new_xattr
) + size
;
1012 if (len
< sizeof(*new_xattr
))
1015 new_xattr
= kvmalloc(len
, GFP_KERNEL
);
1019 new_xattr
->size
= size
;
1020 memcpy(new_xattr
->value
, value
, size
);
1025 * xattr GET operation for in-memory/pseudo filesystems
1027 int simple_xattr_get(struct simple_xattrs
*xattrs
, const char *name
,
1028 void *buffer
, size_t size
)
1030 struct simple_xattr
*xattr
;
1033 spin_lock(&xattrs
->lock
);
1034 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1035 if (strcmp(name
, xattr
->name
))
1040 if (size
< xattr
->size
)
1043 memcpy(buffer
, xattr
->value
, xattr
->size
);
1047 spin_unlock(&xattrs
->lock
);
1052 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
1053 * @xattrs: target simple_xattr list
1054 * @name: name of the extended attribute
1055 * @value: value of the xattr. If %NULL, will remove the attribute.
1056 * @size: size of the new xattr
1057 * @flags: %XATTR_{CREATE|REPLACE}
1058 * @removed_size: returns size of the removed xattr, -1 if none removed
1060 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
1061 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
1062 * otherwise, fails with -ENODATA.
1064 * Returns 0 on success, -errno on failure.
1066 int simple_xattr_set(struct simple_xattrs
*xattrs
, const char *name
,
1067 const void *value
, size_t size
, int flags
,
1068 ssize_t
*removed_size
)
1070 struct simple_xattr
*xattr
;
1071 struct simple_xattr
*new_xattr
= NULL
;
1077 /* value == NULL means remove */
1079 new_xattr
= simple_xattr_alloc(value
, size
);
1083 new_xattr
->name
= kstrdup(name
, GFP_KERNEL
);
1084 if (!new_xattr
->name
) {
1090 spin_lock(&xattrs
->lock
);
1091 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1092 if (!strcmp(name
, xattr
->name
)) {
1093 if (flags
& XATTR_CREATE
) {
1096 } else if (new_xattr
) {
1097 list_replace(&xattr
->list
, &new_xattr
->list
);
1099 *removed_size
= xattr
->size
;
1101 list_del(&xattr
->list
);
1103 *removed_size
= xattr
->size
;
1108 if (flags
& XATTR_REPLACE
) {
1112 list_add(&new_xattr
->list
, &xattrs
->head
);
1116 spin_unlock(&xattrs
->lock
);
1125 static bool xattr_is_trusted(const char *name
)
1127 return !strncmp(name
, XATTR_TRUSTED_PREFIX
, XATTR_TRUSTED_PREFIX_LEN
);
1130 static int xattr_list_one(char **buffer
, ssize_t
*remaining_size
,
1133 size_t len
= strlen(name
) + 1;
1135 if (*remaining_size
< len
)
1137 memcpy(*buffer
, name
, len
);
1140 *remaining_size
-= len
;
1145 * xattr LIST operation for in-memory/pseudo filesystems
1147 ssize_t
simple_xattr_list(struct inode
*inode
, struct simple_xattrs
*xattrs
,
1148 char *buffer
, size_t size
)
1150 bool trusted
= capable(CAP_SYS_ADMIN
);
1151 struct simple_xattr
*xattr
;
1152 ssize_t remaining_size
= size
;
1155 #ifdef CONFIG_FS_POSIX_ACL
1156 if (IS_POSIXACL(inode
)) {
1158 err
= xattr_list_one(&buffer
, &remaining_size
,
1159 XATTR_NAME_POSIX_ACL_ACCESS
);
1163 if (inode
->i_default_acl
) {
1164 err
= xattr_list_one(&buffer
, &remaining_size
,
1165 XATTR_NAME_POSIX_ACL_DEFAULT
);
1172 spin_lock(&xattrs
->lock
);
1173 list_for_each_entry(xattr
, &xattrs
->head
, list
) {
1174 /* skip "trusted." attributes for unprivileged callers */
1175 if (!trusted
&& xattr_is_trusted(xattr
->name
))
1178 err
= xattr_list_one(&buffer
, &remaining_size
, xattr
->name
);
1182 spin_unlock(&xattrs
->lock
);
1184 return err
? err
: size
- remaining_size
;
1188 * Adds an extended attribute to the list
1190 void simple_xattr_list_add(struct simple_xattrs
*xattrs
,
1191 struct simple_xattr
*new_xattr
)
1193 spin_lock(&xattrs
->lock
);
1194 list_add(&new_xattr
->list
, &xattrs
->head
);
1195 spin_unlock(&xattrs
->lock
);