4 Extended attribute handling.
6 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
7 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
8 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
11 #include <linux/slab.h>
12 #include <linux/file.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/evm.h>
18 #include <linux/syscalls.h>
19 #include <linux/export.h>
20 #include <linux/fsnotify.h>
21 #include <linux/audit.h>
22 #include <linux/vmalloc.h>
23 #include <linux/posix_acl_xattr.h>
25 #include <asm/uaccess.h>
28 strcmp_prefix(const char *a, const char *a_prefix)
30 while (*a_prefix && *a == *a_prefix) {
34 return *a_prefix ? NULL : a;
38 * In order to implement different sets of xattr operations for each xattr
39 * prefix with the generic xattr API, a filesystem should create a
40 * null-terminated array of struct xattr_handler (one for each prefix) and
41 * hang a pointer to it off of the s_xattr field of the superblock.
43 * The generic_fooxattr() functions will use this list to dispatch xattr
44 * operations to the correct xattr_handler.
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(const struct xattr_handler **handlers, const char **name)
58 const struct xattr_handler *handler;
60 for_each_xattr_handler(handlers, handler) {
63 n = strcmp_prefix(*name, xattr_prefix(handler));
65 if (!handler->prefix ^ !*n) {
68 return ERR_PTR(-EINVAL);
74 return ERR_PTR(-EOPNOTSUPP);
78 * Check permissions for extended attribute access. This is a bit complicated
79 * because different namespaces have very different rules.
82 xattr_permission(struct inode *inode, const char *name, int mask)
85 * We can never set or remove an extended attribute on a read-only
86 * filesystem or on an immutable / append-only inode.
88 if (mask & MAY_WRITE) {
89 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
92 * Updating an xattr will likely cause i_uid and i_gid
93 * to be writen back improperly if their true value is
96 if (HAS_UNMAPPED_ID(inode))
101 * No restriction for security.* and system.* from the VFS. Decision
102 * on these is left to the underlying filesystem / security module.
104 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
105 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
109 * The trusted.* namespace can only be accessed by privileged users.
111 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
112 if (!capable(CAP_SYS_ADMIN))
113 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
118 * In the user.* namespace, only regular files and directories can have
119 * extended attributes. For sticky directories, only the owner and
120 * privileged users can write attributes.
122 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
123 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
124 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
125 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
126 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
130 return inode_permission(inode, mask);
134 * __vfs_setxattr_noperm - perform setxattr operation without performing
137 * @dentry - object to perform setxattr on
138 * @name - xattr name to set
139 * @value - value to set @name to
140 * @size - size of @value
141 * @flags - flags to pass into filesystem operations
143 * returns the result of the internal setxattr or setsecurity operations.
145 * This function requires the caller to lock the inode's i_mutex before it
146 * is executed. It also assumes that the caller will make the appropriate
149 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
150 const void *value, size_t size, int flags)
152 struct inode *inode = dentry->d_inode;
153 int error = -EOPNOTSUPP;
154 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
155 XATTR_SECURITY_PREFIX_LEN);
158 inode->i_flags &= ~S_NOSEC;
159 if (inode->i_op->setxattr) {
160 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
162 fsnotify_xattr(dentry);
163 security_inode_post_setxattr(dentry, name, value,
167 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
168 error = security_inode_setsecurity(inode, suffix, value,
171 fsnotify_xattr(dentry);
179 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
180 size_t size, int flags)
182 struct inode *inode = dentry->d_inode;
185 error = xattr_permission(inode, name, MAY_WRITE);
190 error = security_inode_setxattr(dentry, name, value, size, flags);
194 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
200 EXPORT_SYMBOL_GPL(vfs_setxattr);
203 xattr_getsecurity(struct inode *inode, const char *name, void *value,
209 if (!value || !size) {
210 len = security_inode_getsecurity(inode, name, &buffer, false);
214 len = security_inode_getsecurity(inode, name, &buffer, true);
221 memcpy(value, buffer, len);
223 security_release_secctx(buffer, len);
227 EXPORT_SYMBOL_GPL(xattr_getsecurity);
230 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
232 * Allocate memory, if not already allocated, or re-allocate correct size,
233 * before retrieving the extended attribute.
235 * Returns the result of alloc, if failed, or the getxattr operation.
238 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
239 size_t xattr_size, gfp_t flags)
241 struct inode *inode = dentry->d_inode;
242 char *value = *xattr_value;
245 error = xattr_permission(inode, name, MAY_READ);
249 if (!inode->i_op->getxattr)
252 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
256 if (!value || (error > xattr_size)) {
257 value = krealloc(*xattr_value, error + 1, flags);
260 memset(value, 0, error + 1);
263 error = inode->i_op->getxattr(dentry, inode, name, value, error);
264 *xattr_value = value;
269 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
271 struct inode *inode = dentry->d_inode;
274 error = xattr_permission(inode, name, MAY_READ);
278 error = security_inode_getxattr(dentry, name);
282 if (!strncmp(name, XATTR_SECURITY_PREFIX,
283 XATTR_SECURITY_PREFIX_LEN)) {
284 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
285 int ret = xattr_getsecurity(inode, suffix, value, size);
287 * Only overwrite the return value if a security module
288 * is actually active.
290 if (ret == -EOPNOTSUPP)
295 if (inode->i_op->getxattr)
296 error = inode->i_op->getxattr(dentry, inode, name, value, size);
302 EXPORT_SYMBOL_GPL(vfs_getxattr);
305 vfs_listxattr(struct dentry *d, char *list, size_t size)
309 error = security_inode_listxattr(d);
313 if (d->d_inode->i_op->listxattr) {
314 error = d->d_inode->i_op->listxattr(d, list, size);
316 error = security_inode_listsecurity(d->d_inode, list, size);
317 if (size && error > size)
322 EXPORT_SYMBOL_GPL(vfs_listxattr);
325 vfs_removexattr(struct dentry *dentry, const char *name)
327 struct inode *inode = dentry->d_inode;
330 if (!inode->i_op->removexattr)
333 error = xattr_permission(inode, name, MAY_WRITE);
338 error = security_inode_removexattr(dentry, name);
342 error = inode->i_op->removexattr(dentry, name);
345 fsnotify_xattr(dentry);
346 evm_inode_post_removexattr(dentry, name);
353 EXPORT_SYMBOL_GPL(vfs_removexattr);
357 * Extended attribute SET operations
360 setxattr(struct dentry *d, const char __user *name, const void __user *value,
361 size_t size, int flags)
365 char kname[XATTR_NAME_MAX + 1];
367 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
370 error = strncpy_from_user(kname, name, sizeof(kname));
371 if (error == 0 || error == sizeof(kname))
377 if (size > XATTR_SIZE_MAX)
379 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
381 kvalue = vmalloc(size);
385 if (copy_from_user(kvalue, value, size)) {
389 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
390 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
391 posix_acl_fix_xattr_from_user(kvalue, size);
394 error = vfs_setxattr(d, kname, kvalue, size, flags);
401 static int path_setxattr(const char __user *pathname,
402 const char __user *name, const void __user *value,
403 size_t size, int flags, unsigned int lookup_flags)
408 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
411 error = mnt_want_write(path.mnt);
413 error = setxattr(path.dentry, name, value, size, flags);
414 mnt_drop_write(path.mnt);
417 if (retry_estale(error, lookup_flags)) {
418 lookup_flags |= LOOKUP_REVAL;
424 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
425 const char __user *, name, const void __user *, value,
426 size_t, size, int, flags)
428 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
431 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
432 const char __user *, name, const void __user *, value,
433 size_t, size, int, flags)
435 return path_setxattr(pathname, name, value, size, flags, 0);
438 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
439 const void __user *,value, size_t, size, int, flags)
441 struct fd f = fdget(fd);
447 error = mnt_want_write_file(f.file);
449 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
450 mnt_drop_write_file(f.file);
457 * Extended attribute GET operations
460 getxattr(struct dentry *d, const char __user *name, void __user *value,
465 char kname[XATTR_NAME_MAX + 1];
467 error = strncpy_from_user(kname, name, sizeof(kname));
468 if (error == 0 || error == sizeof(kname))
474 if (size > XATTR_SIZE_MAX)
475 size = XATTR_SIZE_MAX;
476 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
478 kvalue = vmalloc(size);
484 error = vfs_getxattr(d, kname, kvalue, size);
486 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
487 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
488 posix_acl_fix_xattr_to_user(kvalue, size);
489 if (size && copy_to_user(value, kvalue, error))
491 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
492 /* The file system tried to returned a value bigger
493 than XATTR_SIZE_MAX bytes. Not possible. */
502 static ssize_t path_getxattr(const char __user *pathname,
503 const char __user *name, void __user *value,
504 size_t size, unsigned int lookup_flags)
509 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
512 error = getxattr(path.dentry, name, value, size);
514 if (retry_estale(error, lookup_flags)) {
515 lookup_flags |= LOOKUP_REVAL;
521 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
522 const char __user *, name, void __user *, value, size_t, size)
524 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
527 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
528 const char __user *, name, void __user *, value, size_t, size)
530 return path_getxattr(pathname, name, value, size, 0);
533 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
534 void __user *, value, size_t, size)
536 struct fd f = fdget(fd);
537 ssize_t error = -EBADF;
542 error = getxattr(f.file->f_path.dentry, name, value, size);
548 * Extended attribute LIST operations
551 listxattr(struct dentry *d, char __user *list, size_t size)
557 if (size > XATTR_LIST_MAX)
558 size = XATTR_LIST_MAX;
559 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
561 klist = vmalloc(size);
567 error = vfs_listxattr(d, klist, size);
569 if (size && copy_to_user(list, klist, error))
571 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
572 /* The file system tried to returned a list bigger
573 than XATTR_LIST_MAX bytes. Not possible. */
582 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
583 size_t size, unsigned int lookup_flags)
588 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
591 error = listxattr(path.dentry, list, size);
593 if (retry_estale(error, lookup_flags)) {
594 lookup_flags |= LOOKUP_REVAL;
600 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
603 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
606 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
609 return path_listxattr(pathname, list, size, 0);
612 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
614 struct fd f = fdget(fd);
615 ssize_t error = -EBADF;
620 error = listxattr(f.file->f_path.dentry, list, size);
626 * Extended attribute REMOVE operations
629 removexattr(struct dentry *d, const char __user *name)
632 char kname[XATTR_NAME_MAX + 1];
634 error = strncpy_from_user(kname, name, sizeof(kname));
635 if (error == 0 || error == sizeof(kname))
640 return vfs_removexattr(d, kname);
643 static int path_removexattr(const char __user *pathname,
644 const char __user *name, unsigned int lookup_flags)
649 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
652 error = mnt_want_write(path.mnt);
654 error = removexattr(path.dentry, name);
655 mnt_drop_write(path.mnt);
658 if (retry_estale(error, lookup_flags)) {
659 lookup_flags |= LOOKUP_REVAL;
665 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
666 const char __user *, name)
668 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
671 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
672 const char __user *, name)
674 return path_removexattr(pathname, name, 0);
677 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
679 struct fd f = fdget(fd);
685 error = mnt_want_write_file(f.file);
687 error = removexattr(f.file->f_path.dentry, name);
688 mnt_drop_write_file(f.file);
695 * Find the handler for the prefix and dispatch its get() operation.
698 generic_getxattr(struct dentry *dentry, struct inode *inode,
699 const char *name, void *buffer, size_t size)
701 const struct xattr_handler *handler;
703 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
705 return PTR_ERR(handler);
706 return handler->get(handler, dentry, inode,
711 * Combine the results of the list() operation from every xattr_handler in the
715 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
717 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
718 unsigned int size = 0;
721 for_each_xattr_handler(handlers, handler) {
722 if (!handler->name ||
723 (handler->list && !handler->list(dentry)))
725 size += strlen(handler->name) + 1;
731 for_each_xattr_handler(handlers, handler) {
732 if (!handler->name ||
733 (handler->list && !handler->list(dentry)))
735 len = strlen(handler->name);
736 if (len + 1 > buffer_size)
738 memcpy(buf, handler->name, len + 1);
740 buffer_size -= len + 1;
748 * Find the handler for the prefix and dispatch its set() operation.
751 generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
752 const void *value, size_t size, int flags)
754 const struct xattr_handler *handler;
757 value = ""; /* empty EA, do not remove */
758 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
760 return PTR_ERR(handler);
761 return handler->set(handler, dentry, inode, name, value, size, flags);
765 * Find the handler for the prefix and dispatch its set() operation to remove
766 * any associated extended attribute.
769 generic_removexattr(struct dentry *dentry, const char *name)
771 const struct xattr_handler *handler;
773 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
775 return PTR_ERR(handler);
776 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
780 EXPORT_SYMBOL(generic_getxattr);
781 EXPORT_SYMBOL(generic_listxattr);
782 EXPORT_SYMBOL(generic_setxattr);
783 EXPORT_SYMBOL(generic_removexattr);
786 * xattr_full_name - Compute full attribute name from suffix
788 * @handler: handler of the xattr_handler operation
789 * @name: name passed to the xattr_handler operation
791 * The get and set xattr handler operations are called with the remainder of
792 * the attribute name after skipping the handler's prefix: for example, "foo"
793 * is passed to the get operation of a handler with prefix "user." to get
794 * attribute "user.foo". The full name is still "there" in the name though.
796 * Note: the list xattr handler operation when called from the vfs is passed a
797 * NULL name; some file systems use this operation internally, with varying
800 const char *xattr_full_name(const struct xattr_handler *handler,
803 size_t prefix_len = strlen(xattr_prefix(handler));
805 return name - prefix_len;
807 EXPORT_SYMBOL(xattr_full_name);
810 * Allocate new xattr and copy in the value; but leave the name to callers.
812 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
814 struct simple_xattr *new_xattr;
818 len = sizeof(*new_xattr) + size;
819 if (len < sizeof(*new_xattr))
822 new_xattr = kmalloc(len, GFP_KERNEL);
826 new_xattr->size = size;
827 memcpy(new_xattr->value, value, size);
832 * xattr GET operation for in-memory/pseudo filesystems
834 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
835 void *buffer, size_t size)
837 struct simple_xattr *xattr;
840 spin_lock(&xattrs->lock);
841 list_for_each_entry(xattr, &xattrs->head, list) {
842 if (strcmp(name, xattr->name))
847 if (size < xattr->size)
850 memcpy(buffer, xattr->value, xattr->size);
854 spin_unlock(&xattrs->lock);
859 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
860 * @xattrs: target simple_xattr list
861 * @name: name of the extended attribute
862 * @value: value of the xattr. If %NULL, will remove the attribute.
863 * @size: size of the new xattr
864 * @flags: %XATTR_{CREATE|REPLACE}
866 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
867 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
868 * otherwise, fails with -ENODATA.
870 * Returns 0 on success, -errno on failure.
872 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
873 const void *value, size_t size, int flags)
875 struct simple_xattr *xattr;
876 struct simple_xattr *new_xattr = NULL;
879 /* value == NULL means remove */
881 new_xattr = simple_xattr_alloc(value, size);
885 new_xattr->name = kstrdup(name, GFP_KERNEL);
886 if (!new_xattr->name) {
892 spin_lock(&xattrs->lock);
893 list_for_each_entry(xattr, &xattrs->head, list) {
894 if (!strcmp(name, xattr->name)) {
895 if (flags & XATTR_CREATE) {
898 } else if (new_xattr) {
899 list_replace(&xattr->list, &new_xattr->list);
901 list_del(&xattr->list);
906 if (flags & XATTR_REPLACE) {
910 list_add(&new_xattr->list, &xattrs->head);
914 spin_unlock(&xattrs->lock);
923 static bool xattr_is_trusted(const char *name)
925 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
928 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
931 size_t len = strlen(name) + 1;
933 if (*remaining_size < len)
935 memcpy(*buffer, name, len);
938 *remaining_size -= len;
943 * xattr LIST operation for in-memory/pseudo filesystems
945 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
946 char *buffer, size_t size)
948 bool trusted = capable(CAP_SYS_ADMIN);
949 struct simple_xattr *xattr;
950 ssize_t remaining_size = size;
953 #ifdef CONFIG_FS_POSIX_ACL
955 err = xattr_list_one(&buffer, &remaining_size,
956 XATTR_NAME_POSIX_ACL_ACCESS);
960 if (inode->i_default_acl) {
961 err = xattr_list_one(&buffer, &remaining_size,
962 XATTR_NAME_POSIX_ACL_DEFAULT);
968 spin_lock(&xattrs->lock);
969 list_for_each_entry(xattr, &xattrs->head, list) {
970 /* skip "trusted." attributes for unprivileged callers */
971 if (!trusted && xattr_is_trusted(xattr->name))
974 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
978 spin_unlock(&xattrs->lock);
980 return err ? err : size - remaining_size;
984 * Adds an extended attribute to the list
986 void simple_xattr_list_add(struct simple_xattrs *xattrs,
987 struct simple_xattr *new_xattr)
989 spin_lock(&xattrs->lock);
990 list_add(&new_xattr->list, &xattrs->head);
991 spin_unlock(&xattrs->lock);