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(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 inode *inode, const char *name, int mask)
91 * We can never set or remove an extended attribute on a read-only
92 * filesystem or on an immutable / append-only inode.
94 if (mask & MAY_WRITE) {
95 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
98 * Updating an xattr will likely cause i_uid and i_gid
99 * to be writen back improperly if their true value is
100 * unknown to the vfs.
102 if (HAS_UNMAPPED_ID(inode))
107 * No restriction for security.* and system.* from the VFS. Decision
108 * on these is left to the underlying filesystem / security module.
110 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
111 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
115 * The trusted.* namespace can only be accessed by privileged users.
117 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
118 if (!capable(CAP_SYS_ADMIN))
119 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
124 * In the user.* namespace, only regular files and directories can have
125 * extended attributes. For sticky directories, only the owner and
126 * privileged users can write attributes.
128 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
129 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
130 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
131 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
132 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
136 return inode_permission(inode, mask);
140 * __vfs_setxattr_noperm - perform setxattr operation without performing
143 * @dentry - object to perform setxattr on
144 * @name - xattr name to set
145 * @value - value to set @name to
146 * @size - size of @value
147 * @flags - flags to pass into filesystem operations
149 * returns the result of the internal setxattr or setsecurity operations.
151 * This function requires the caller to lock the inode's i_mutex before it
152 * is executed. It also assumes that the caller will make the appropriate
155 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
156 const void *value, size_t size, int flags)
158 struct inode *inode = dentry->d_inode;
159 int error = -EOPNOTSUPP;
160 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
161 XATTR_SECURITY_PREFIX_LEN);
164 inode->i_flags &= ~S_NOSEC;
165 if (inode->i_op->setxattr) {
166 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
168 fsnotify_xattr(dentry);
169 security_inode_post_setxattr(dentry, name, value,
173 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
175 if (unlikely(is_bad_inode(inode)))
177 error = security_inode_setsecurity(inode, suffix, value,
180 fsnotify_xattr(dentry);
188 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
189 size_t size, int flags)
191 struct inode *inode = dentry->d_inode;
194 error = xattr_permission(inode, name, MAY_WRITE);
199 error = security_inode_setxattr(dentry, name, value, size, flags);
203 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
209 EXPORT_SYMBOL_GPL(vfs_setxattr);
212 xattr_getsecurity(struct inode *inode, const char *name, void *value,
218 if (!value || !size) {
219 len = security_inode_getsecurity(inode, name, &buffer, false);
223 len = security_inode_getsecurity(inode, name, &buffer, true);
230 memcpy(value, buffer, len);
232 security_release_secctx(buffer, len);
236 EXPORT_SYMBOL_GPL(xattr_getsecurity);
239 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
241 * Allocate memory, if not already allocated, or re-allocate correct size,
242 * before retrieving the extended attribute.
244 * Returns the result of alloc, if failed, or the getxattr operation.
247 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
248 size_t xattr_size, gfp_t flags)
250 struct inode *inode = dentry->d_inode;
251 char *value = *xattr_value;
254 error = xattr_permission(inode, name, MAY_READ);
258 if (!inode->i_op->getxattr)
261 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
265 if (!value || (error > xattr_size)) {
266 value = krealloc(*xattr_value, error + 1, flags);
269 memset(value, 0, error + 1);
272 error = inode->i_op->getxattr(dentry, inode, name, value, error);
273 *xattr_value = value;
278 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
280 struct inode *inode = dentry->d_inode;
283 error = xattr_permission(inode, name, MAY_READ);
287 error = security_inode_getxattr(dentry, name);
291 if (!strncmp(name, XATTR_SECURITY_PREFIX,
292 XATTR_SECURITY_PREFIX_LEN)) {
293 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
294 int ret = xattr_getsecurity(inode, suffix, value, size);
296 * Only overwrite the return value if a security module
297 * is actually active.
299 if (ret == -EOPNOTSUPP)
304 if (inode->i_op->getxattr)
305 error = inode->i_op->getxattr(dentry, inode, name, value, size);
312 EXPORT_SYMBOL_GPL(vfs_getxattr);
315 vfs_listxattr(struct dentry *d, char *list, size_t size)
319 error = security_inode_listxattr(d);
323 if (d->d_inode->i_op->listxattr) {
324 error = d->d_inode->i_op->listxattr(d, list, size);
326 error = security_inode_listsecurity(d->d_inode, list, size);
327 if (size && error > size)
332 EXPORT_SYMBOL_GPL(vfs_listxattr);
335 vfs_removexattr(struct dentry *dentry, const char *name)
337 struct inode *inode = dentry->d_inode;
340 if (!inode->i_op->removexattr)
343 error = xattr_permission(inode, name, MAY_WRITE);
348 error = security_inode_removexattr(dentry, name);
352 error = inode->i_op->removexattr(dentry, name);
355 fsnotify_xattr(dentry);
356 evm_inode_post_removexattr(dentry, name);
363 EXPORT_SYMBOL_GPL(vfs_removexattr);
367 * Extended attribute SET operations
370 setxattr(struct dentry *d, const char __user *name, const void __user *value,
371 size_t size, int flags)
375 char kname[XATTR_NAME_MAX + 1];
377 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
380 error = strncpy_from_user(kname, name, sizeof(kname));
381 if (error == 0 || error == sizeof(kname))
387 if (size > XATTR_SIZE_MAX)
389 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
391 kvalue = vmalloc(size);
395 if (copy_from_user(kvalue, value, size)) {
399 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
400 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
401 posix_acl_fix_xattr_from_user(kvalue, size);
404 error = vfs_setxattr(d, kname, kvalue, size, flags);
411 static int path_setxattr(const char __user *pathname,
412 const char __user *name, const void __user *value,
413 size_t size, int flags, unsigned int lookup_flags)
418 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
421 error = mnt_want_write(path.mnt);
423 error = setxattr(path.dentry, name, value, size, flags);
424 mnt_drop_write(path.mnt);
427 if (retry_estale(error, lookup_flags)) {
428 lookup_flags |= LOOKUP_REVAL;
434 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
435 const char __user *, name, const void __user *, value,
436 size_t, size, int, flags)
438 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
441 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
442 const char __user *, name, const void __user *, value,
443 size_t, size, int, flags)
445 return path_setxattr(pathname, name, value, size, flags, 0);
448 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
449 const void __user *,value, size_t, size, int, flags)
451 struct fd f = fdget(fd);
457 error = mnt_want_write_file(f.file);
459 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
460 mnt_drop_write_file(f.file);
467 * Extended attribute GET operations
470 getxattr(struct dentry *d, const char __user *name, void __user *value,
475 char kname[XATTR_NAME_MAX + 1];
477 error = strncpy_from_user(kname, name, sizeof(kname));
478 if (error == 0 || error == sizeof(kname))
484 if (size > XATTR_SIZE_MAX)
485 size = XATTR_SIZE_MAX;
486 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
488 kvalue = vmalloc(size);
494 error = vfs_getxattr(d, kname, kvalue, size);
496 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
497 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
498 posix_acl_fix_xattr_to_user(kvalue, size);
499 if (size && copy_to_user(value, kvalue, error))
501 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
502 /* The file system tried to returned a value bigger
503 than XATTR_SIZE_MAX bytes. Not possible. */
512 static ssize_t path_getxattr(const char __user *pathname,
513 const char __user *name, void __user *value,
514 size_t size, unsigned int lookup_flags)
519 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
522 error = getxattr(path.dentry, name, value, size);
524 if (retry_estale(error, lookup_flags)) {
525 lookup_flags |= LOOKUP_REVAL;
531 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
532 const char __user *, name, void __user *, value, size_t, size)
534 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
537 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
538 const char __user *, name, void __user *, value, size_t, size)
540 return path_getxattr(pathname, name, value, size, 0);
543 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
544 void __user *, value, size_t, size)
546 struct fd f = fdget(fd);
547 ssize_t error = -EBADF;
552 error = getxattr(f.file->f_path.dentry, name, value, size);
558 * Extended attribute LIST operations
561 listxattr(struct dentry *d, char __user *list, size_t size)
567 if (size > XATTR_LIST_MAX)
568 size = XATTR_LIST_MAX;
569 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
571 klist = vmalloc(size);
577 error = vfs_listxattr(d, klist, size);
579 if (size && copy_to_user(list, klist, error))
581 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
582 /* The file system tried to returned a list bigger
583 than XATTR_LIST_MAX bytes. Not possible. */
592 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
593 size_t size, unsigned int lookup_flags)
598 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
601 error = listxattr(path.dentry, list, size);
603 if (retry_estale(error, lookup_flags)) {
604 lookup_flags |= LOOKUP_REVAL;
610 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
613 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
616 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
619 return path_listxattr(pathname, list, size, 0);
622 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
624 struct fd f = fdget(fd);
625 ssize_t error = -EBADF;
630 error = listxattr(f.file->f_path.dentry, list, size);
636 * Extended attribute REMOVE operations
639 removexattr(struct dentry *d, const char __user *name)
642 char kname[XATTR_NAME_MAX + 1];
644 error = strncpy_from_user(kname, name, sizeof(kname));
645 if (error == 0 || error == sizeof(kname))
650 return vfs_removexattr(d, kname);
653 static int path_removexattr(const char __user *pathname,
654 const char __user *name, unsigned int lookup_flags)
659 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
662 error = mnt_want_write(path.mnt);
664 error = removexattr(path.dentry, name);
665 mnt_drop_write(path.mnt);
668 if (retry_estale(error, lookup_flags)) {
669 lookup_flags |= LOOKUP_REVAL;
675 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
676 const char __user *, name)
678 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
681 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
682 const char __user *, name)
684 return path_removexattr(pathname, name, 0);
687 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
689 struct fd f = fdget(fd);
695 error = mnt_want_write_file(f.file);
697 error = removexattr(f.file->f_path.dentry, name);
698 mnt_drop_write_file(f.file);
705 * Find the handler for the prefix and dispatch its get() operation.
708 generic_getxattr(struct dentry *dentry, struct inode *inode,
709 const char *name, void *buffer, size_t size)
711 const struct xattr_handler *handler;
713 handler = xattr_resolve_name(inode, &name);
715 return PTR_ERR(handler);
716 return handler->get(handler, dentry, inode,
721 * Combine the results of the list() operation from every xattr_handler in the
725 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
727 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
728 unsigned int size = 0;
731 for_each_xattr_handler(handlers, handler) {
732 if (!handler->name ||
733 (handler->list && !handler->list(dentry)))
735 size += strlen(handler->name) + 1;
741 for_each_xattr_handler(handlers, handler) {
742 if (!handler->name ||
743 (handler->list && !handler->list(dentry)))
745 len = strlen(handler->name);
746 if (len + 1 > buffer_size)
748 memcpy(buf, handler->name, len + 1);
750 buffer_size -= len + 1;
758 * Find the handler for the prefix and dispatch its set() operation.
761 generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
762 const void *value, size_t size, int flags)
764 const struct xattr_handler *handler;
767 value = ""; /* empty EA, do not remove */
768 handler = xattr_resolve_name(inode, &name);
770 return PTR_ERR(handler);
771 return handler->set(handler, dentry, inode, name, value, size, flags);
775 * Find the handler for the prefix and dispatch its set() operation to remove
776 * any associated extended attribute.
779 generic_removexattr(struct dentry *dentry, const char *name)
781 const struct xattr_handler *handler;
783 handler = xattr_resolve_name(d_inode(dentry), &name);
785 return PTR_ERR(handler);
786 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
790 EXPORT_SYMBOL(generic_getxattr);
791 EXPORT_SYMBOL(generic_listxattr);
792 EXPORT_SYMBOL(generic_setxattr);
793 EXPORT_SYMBOL(generic_removexattr);
796 * xattr_full_name - Compute full attribute name from suffix
798 * @handler: handler of the xattr_handler operation
799 * @name: name passed to the xattr_handler operation
801 * The get and set xattr handler operations are called with the remainder of
802 * the attribute name after skipping the handler's prefix: for example, "foo"
803 * is passed to the get operation of a handler with prefix "user." to get
804 * attribute "user.foo". The full name is still "there" in the name though.
806 * Note: the list xattr handler operation when called from the vfs is passed a
807 * NULL name; some file systems use this operation internally, with varying
810 const char *xattr_full_name(const struct xattr_handler *handler,
813 size_t prefix_len = strlen(xattr_prefix(handler));
815 return name - prefix_len;
817 EXPORT_SYMBOL(xattr_full_name);
820 * Allocate new xattr and copy in the value; but leave the name to callers.
822 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
824 struct simple_xattr *new_xattr;
828 len = sizeof(*new_xattr) + size;
829 if (len < sizeof(*new_xattr))
832 new_xattr = kmalloc(len, GFP_KERNEL);
836 new_xattr->size = size;
837 memcpy(new_xattr->value, value, size);
842 * xattr GET operation for in-memory/pseudo filesystems
844 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
845 void *buffer, size_t size)
847 struct simple_xattr *xattr;
850 spin_lock(&xattrs->lock);
851 list_for_each_entry(xattr, &xattrs->head, list) {
852 if (strcmp(name, xattr->name))
857 if (size < xattr->size)
860 memcpy(buffer, xattr->value, xattr->size);
864 spin_unlock(&xattrs->lock);
869 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
870 * @xattrs: target simple_xattr list
871 * @name: name of the extended attribute
872 * @value: value of the xattr. If %NULL, will remove the attribute.
873 * @size: size of the new xattr
874 * @flags: %XATTR_{CREATE|REPLACE}
876 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
877 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
878 * otherwise, fails with -ENODATA.
880 * Returns 0 on success, -errno on failure.
882 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
883 const void *value, size_t size, int flags)
885 struct simple_xattr *xattr;
886 struct simple_xattr *new_xattr = NULL;
889 /* value == NULL means remove */
891 new_xattr = simple_xattr_alloc(value, size);
895 new_xattr->name = kstrdup(name, GFP_KERNEL);
896 if (!new_xattr->name) {
902 spin_lock(&xattrs->lock);
903 list_for_each_entry(xattr, &xattrs->head, list) {
904 if (!strcmp(name, xattr->name)) {
905 if (flags & XATTR_CREATE) {
908 } else if (new_xattr) {
909 list_replace(&xattr->list, &new_xattr->list);
911 list_del(&xattr->list);
916 if (flags & XATTR_REPLACE) {
920 list_add(&new_xattr->list, &xattrs->head);
924 spin_unlock(&xattrs->lock);
933 static bool xattr_is_trusted(const char *name)
935 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
938 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
941 size_t len = strlen(name) + 1;
943 if (*remaining_size < len)
945 memcpy(*buffer, name, len);
948 *remaining_size -= len;
953 * xattr LIST operation for in-memory/pseudo filesystems
955 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
956 char *buffer, size_t size)
958 bool trusted = capable(CAP_SYS_ADMIN);
959 struct simple_xattr *xattr;
960 ssize_t remaining_size = size;
963 #ifdef CONFIG_FS_POSIX_ACL
965 err = xattr_list_one(&buffer, &remaining_size,
966 XATTR_NAME_POSIX_ACL_ACCESS);
970 if (inode->i_default_acl) {
971 err = xattr_list_one(&buffer, &remaining_size,
972 XATTR_NAME_POSIX_ACL_DEFAULT);
978 spin_lock(&xattrs->lock);
979 list_for_each_entry(xattr, &xattrs->head, list) {
980 /* skip "trusted." attributes for unprivileged callers */
981 if (!trusted && xattr_is_trusted(xattr->name))
984 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
988 spin_unlock(&xattrs->lock);
990 return err ? err : size - remaining_size;
994 * Adds an extended attribute to the list
996 void simple_xattr_list_add(struct simple_xattrs *xattrs,
997 struct simple_xattr *new_xattr)
999 spin_lock(&xattrs->lock);
1000 list_add(&new_xattr->list, &xattrs->head);
1001 spin_unlock(&xattrs->lock);