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(struct dentry *dentry, struct inode *inode, const char *name,
141 const void *value, size_t size, int flags)
143 if (!inode->i_op->setxattr)
145 return inode->i_op->setxattr(dentry, inode, name, value, size, flags);
147 EXPORT_SYMBOL(__vfs_setxattr);
150 * __vfs_setxattr_noperm - perform setxattr operation without performing
153 * @dentry - object to perform setxattr on
154 * @name - xattr name to set
155 * @value - value to set @name to
156 * @size - size of @value
157 * @flags - flags to pass into filesystem operations
159 * returns the result of the internal setxattr or setsecurity operations.
161 * This function requires the caller to lock the inode's i_mutex before it
162 * is executed. It also assumes that the caller will make the appropriate
165 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
166 const void *value, size_t size, int flags)
168 struct inode *inode = dentry->d_inode;
169 int error = -EOPNOTSUPP;
170 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
171 XATTR_SECURITY_PREFIX_LEN);
174 inode->i_flags &= ~S_NOSEC;
175 if (inode->i_op->setxattr) {
176 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
178 fsnotify_xattr(dentry);
179 security_inode_post_setxattr(dentry, name, value,
183 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
185 if (unlikely(is_bad_inode(inode)))
187 error = security_inode_setsecurity(inode, suffix, value,
190 fsnotify_xattr(dentry);
198 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
199 size_t size, int flags)
201 struct inode *inode = dentry->d_inode;
204 error = xattr_permission(inode, name, MAY_WRITE);
209 error = security_inode_setxattr(dentry, name, value, size, flags);
213 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
219 EXPORT_SYMBOL_GPL(vfs_setxattr);
222 xattr_getsecurity(struct inode *inode, const char *name, void *value,
228 if (!value || !size) {
229 len = security_inode_getsecurity(inode, name, &buffer, false);
233 len = security_inode_getsecurity(inode, name, &buffer, true);
240 memcpy(value, buffer, len);
242 security_release_secctx(buffer, len);
246 EXPORT_SYMBOL_GPL(xattr_getsecurity);
249 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
251 * Allocate memory, if not already allocated, or re-allocate correct size,
252 * before retrieving the extended attribute.
254 * Returns the result of alloc, if failed, or the getxattr operation.
257 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
258 size_t xattr_size, gfp_t flags)
260 struct inode *inode = dentry->d_inode;
261 char *value = *xattr_value;
264 error = xattr_permission(inode, name, MAY_READ);
268 if (!inode->i_op->getxattr)
271 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
275 if (!value || (error > xattr_size)) {
276 value = krealloc(*xattr_value, error + 1, flags);
279 memset(value, 0, error + 1);
282 error = inode->i_op->getxattr(dentry, inode, name, value, error);
283 *xattr_value = value;
288 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
289 void *value, size_t size)
291 if (!inode->i_op->getxattr)
293 return inode->i_op->getxattr(dentry, inode, name, value, size);
295 EXPORT_SYMBOL(__vfs_getxattr);
298 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
300 struct inode *inode = dentry->d_inode;
303 error = xattr_permission(inode, name, MAY_READ);
307 error = security_inode_getxattr(dentry, name);
311 if (!strncmp(name, XATTR_SECURITY_PREFIX,
312 XATTR_SECURITY_PREFIX_LEN)) {
313 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
314 int ret = xattr_getsecurity(inode, suffix, value, size);
316 * Only overwrite the return value if a security module
317 * is actually active.
319 if (ret == -EOPNOTSUPP)
324 return __vfs_getxattr(dentry, inode, name, value, size);
326 EXPORT_SYMBOL_GPL(vfs_getxattr);
329 vfs_listxattr(struct dentry *dentry, char *list, size_t size)
331 struct inode *inode = d_inode(dentry);
334 error = security_inode_listxattr(dentry);
337 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
339 error = inode->i_op->listxattr(dentry, list, size);
341 error = security_inode_listsecurity(inode, list, size);
342 if (size && error > size)
347 EXPORT_SYMBOL_GPL(vfs_listxattr);
350 __vfs_removexattr(struct dentry *dentry, const char *name)
352 struct inode *inode = dentry->d_inode;
354 if (!inode->i_op->removexattr)
356 return inode->i_op->removexattr(dentry, name);
358 EXPORT_SYMBOL(__vfs_removexattr);
361 vfs_removexattr(struct dentry *dentry, const char *name)
363 struct inode *inode = dentry->d_inode;
366 error = xattr_permission(inode, name, MAY_WRITE);
371 error = security_inode_removexattr(dentry, name);
375 error = __vfs_removexattr(dentry, name);
378 fsnotify_xattr(dentry);
379 evm_inode_post_removexattr(dentry, name);
386 EXPORT_SYMBOL_GPL(vfs_removexattr);
390 * Extended attribute SET operations
393 setxattr(struct dentry *d, const char __user *name, const void __user *value,
394 size_t size, int flags)
398 char kname[XATTR_NAME_MAX + 1];
400 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
403 error = strncpy_from_user(kname, name, sizeof(kname));
404 if (error == 0 || error == sizeof(kname))
410 if (size > XATTR_SIZE_MAX)
412 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
414 kvalue = vmalloc(size);
418 if (copy_from_user(kvalue, value, size)) {
422 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
423 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
424 posix_acl_fix_xattr_from_user(kvalue, size);
427 error = vfs_setxattr(d, kname, kvalue, size, flags);
434 static int path_setxattr(const char __user *pathname,
435 const char __user *name, const void __user *value,
436 size_t size, int flags, unsigned int lookup_flags)
441 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
444 error = mnt_want_write(path.mnt);
446 error = setxattr(path.dentry, name, value, size, flags);
447 mnt_drop_write(path.mnt);
450 if (retry_estale(error, lookup_flags)) {
451 lookup_flags |= LOOKUP_REVAL;
457 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
458 const char __user *, name, const void __user *, value,
459 size_t, size, int, flags)
461 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
464 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
465 const char __user *, name, const void __user *, value,
466 size_t, size, int, flags)
468 return path_setxattr(pathname, name, value, size, flags, 0);
471 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
472 const void __user *,value, size_t, size, int, flags)
474 struct fd f = fdget(fd);
480 error = mnt_want_write_file(f.file);
482 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
483 mnt_drop_write_file(f.file);
490 * Extended attribute GET operations
493 getxattr(struct dentry *d, const char __user *name, void __user *value,
498 char kname[XATTR_NAME_MAX + 1];
500 error = strncpy_from_user(kname, name, sizeof(kname));
501 if (error == 0 || error == sizeof(kname))
507 if (size > XATTR_SIZE_MAX)
508 size = XATTR_SIZE_MAX;
509 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
511 kvalue = vmalloc(size);
517 error = vfs_getxattr(d, kname, kvalue, size);
519 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
520 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
521 posix_acl_fix_xattr_to_user(kvalue, size);
522 if (size && copy_to_user(value, kvalue, error))
524 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
525 /* The file system tried to returned a value bigger
526 than XATTR_SIZE_MAX bytes. Not possible. */
535 static ssize_t path_getxattr(const char __user *pathname,
536 const char __user *name, void __user *value,
537 size_t size, unsigned int lookup_flags)
542 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
545 error = getxattr(path.dentry, name, value, size);
547 if (retry_estale(error, lookup_flags)) {
548 lookup_flags |= LOOKUP_REVAL;
554 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
555 const char __user *, name, void __user *, value, size_t, size)
557 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
560 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
561 const char __user *, name, void __user *, value, size_t, size)
563 return path_getxattr(pathname, name, value, size, 0);
566 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
567 void __user *, value, size_t, size)
569 struct fd f = fdget(fd);
570 ssize_t error = -EBADF;
575 error = getxattr(f.file->f_path.dentry, name, value, size);
581 * Extended attribute LIST operations
584 listxattr(struct dentry *d, char __user *list, size_t size)
590 if (size > XATTR_LIST_MAX)
591 size = XATTR_LIST_MAX;
592 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
594 klist = vmalloc(size);
600 error = vfs_listxattr(d, klist, size);
602 if (size && copy_to_user(list, klist, error))
604 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
605 /* The file system tried to returned a list bigger
606 than XATTR_LIST_MAX bytes. Not possible. */
615 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
616 size_t size, unsigned int lookup_flags)
621 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
624 error = listxattr(path.dentry, list, size);
626 if (retry_estale(error, lookup_flags)) {
627 lookup_flags |= LOOKUP_REVAL;
633 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
636 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
639 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
642 return path_listxattr(pathname, list, size, 0);
645 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
647 struct fd f = fdget(fd);
648 ssize_t error = -EBADF;
653 error = listxattr(f.file->f_path.dentry, list, size);
659 * Extended attribute REMOVE operations
662 removexattr(struct dentry *d, const char __user *name)
665 char kname[XATTR_NAME_MAX + 1];
667 error = strncpy_from_user(kname, name, sizeof(kname));
668 if (error == 0 || error == sizeof(kname))
673 return vfs_removexattr(d, kname);
676 static int path_removexattr(const char __user *pathname,
677 const char __user *name, unsigned int lookup_flags)
682 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
685 error = mnt_want_write(path.mnt);
687 error = removexattr(path.dentry, name);
688 mnt_drop_write(path.mnt);
691 if (retry_estale(error, lookup_flags)) {
692 lookup_flags |= LOOKUP_REVAL;
698 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
699 const char __user *, name)
701 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
704 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
705 const char __user *, name)
707 return path_removexattr(pathname, name, 0);
710 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
712 struct fd f = fdget(fd);
718 error = mnt_want_write_file(f.file);
720 error = removexattr(f.file->f_path.dentry, name);
721 mnt_drop_write_file(f.file);
728 * Find the handler for the prefix and dispatch its get() operation.
731 generic_getxattr(struct dentry *dentry, struct inode *inode,
732 const char *name, void *buffer, size_t size)
734 const struct xattr_handler *handler;
736 handler = xattr_resolve_name(inode, &name);
738 return PTR_ERR(handler);
739 return handler->get(handler, dentry, inode,
744 * Combine the results of the list() operation from every xattr_handler in the
748 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
750 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
751 unsigned int size = 0;
754 for_each_xattr_handler(handlers, handler) {
755 if (!handler->name ||
756 (handler->list && !handler->list(dentry)))
758 size += strlen(handler->name) + 1;
764 for_each_xattr_handler(handlers, handler) {
765 if (!handler->name ||
766 (handler->list && !handler->list(dentry)))
768 len = strlen(handler->name);
769 if (len + 1 > buffer_size)
771 memcpy(buf, handler->name, len + 1);
773 buffer_size -= len + 1;
781 * Find the handler for the prefix and dispatch its set() operation.
784 generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
785 const void *value, size_t size, int flags)
787 const struct xattr_handler *handler;
790 value = ""; /* empty EA, do not remove */
791 handler = xattr_resolve_name(inode, &name);
793 return PTR_ERR(handler);
794 return handler->set(handler, dentry, inode, name, value, size, flags);
798 * Find the handler for the prefix and dispatch its set() operation to remove
799 * any associated extended attribute.
802 generic_removexattr(struct dentry *dentry, const char *name)
804 const struct xattr_handler *handler;
806 handler = xattr_resolve_name(d_inode(dentry), &name);
808 return PTR_ERR(handler);
809 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
813 EXPORT_SYMBOL(generic_getxattr);
814 EXPORT_SYMBOL(generic_listxattr);
815 EXPORT_SYMBOL(generic_setxattr);
816 EXPORT_SYMBOL(generic_removexattr);
819 * xattr_full_name - Compute full attribute name from suffix
821 * @handler: handler of the xattr_handler operation
822 * @name: name passed to the xattr_handler operation
824 * The get and set xattr handler operations are called with the remainder of
825 * the attribute name after skipping the handler's prefix: for example, "foo"
826 * is passed to the get operation of a handler with prefix "user." to get
827 * attribute "user.foo". The full name is still "there" in the name though.
829 * Note: the list xattr handler operation when called from the vfs is passed a
830 * NULL name; some file systems use this operation internally, with varying
833 const char *xattr_full_name(const struct xattr_handler *handler,
836 size_t prefix_len = strlen(xattr_prefix(handler));
838 return name - prefix_len;
840 EXPORT_SYMBOL(xattr_full_name);
843 * Allocate new xattr and copy in the value; but leave the name to callers.
845 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
847 struct simple_xattr *new_xattr;
851 len = sizeof(*new_xattr) + size;
852 if (len < sizeof(*new_xattr))
855 new_xattr = kmalloc(len, GFP_KERNEL);
859 new_xattr->size = size;
860 memcpy(new_xattr->value, value, size);
865 * xattr GET operation for in-memory/pseudo filesystems
867 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
868 void *buffer, size_t size)
870 struct simple_xattr *xattr;
873 spin_lock(&xattrs->lock);
874 list_for_each_entry(xattr, &xattrs->head, list) {
875 if (strcmp(name, xattr->name))
880 if (size < xattr->size)
883 memcpy(buffer, xattr->value, xattr->size);
887 spin_unlock(&xattrs->lock);
892 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
893 * @xattrs: target simple_xattr list
894 * @name: name of the extended attribute
895 * @value: value of the xattr. If %NULL, will remove the attribute.
896 * @size: size of the new xattr
897 * @flags: %XATTR_{CREATE|REPLACE}
899 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
900 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
901 * otherwise, fails with -ENODATA.
903 * Returns 0 on success, -errno on failure.
905 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
906 const void *value, size_t size, int flags)
908 struct simple_xattr *xattr;
909 struct simple_xattr *new_xattr = NULL;
912 /* value == NULL means remove */
914 new_xattr = simple_xattr_alloc(value, size);
918 new_xattr->name = kstrdup(name, GFP_KERNEL);
919 if (!new_xattr->name) {
925 spin_lock(&xattrs->lock);
926 list_for_each_entry(xattr, &xattrs->head, list) {
927 if (!strcmp(name, xattr->name)) {
928 if (flags & XATTR_CREATE) {
931 } else if (new_xattr) {
932 list_replace(&xattr->list, &new_xattr->list);
934 list_del(&xattr->list);
939 if (flags & XATTR_REPLACE) {
943 list_add(&new_xattr->list, &xattrs->head);
947 spin_unlock(&xattrs->lock);
956 static bool xattr_is_trusted(const char *name)
958 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
961 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
964 size_t len = strlen(name) + 1;
966 if (*remaining_size < len)
968 memcpy(*buffer, name, len);
971 *remaining_size -= len;
976 * xattr LIST operation for in-memory/pseudo filesystems
978 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
979 char *buffer, size_t size)
981 bool trusted = capable(CAP_SYS_ADMIN);
982 struct simple_xattr *xattr;
983 ssize_t remaining_size = size;
986 #ifdef CONFIG_FS_POSIX_ACL
988 err = xattr_list_one(&buffer, &remaining_size,
989 XATTR_NAME_POSIX_ACL_ACCESS);
993 if (inode->i_default_acl) {
994 err = xattr_list_one(&buffer, &remaining_size,
995 XATTR_NAME_POSIX_ACL_DEFAULT);
1001 spin_lock(&xattrs->lock);
1002 list_for_each_entry(xattr, &xattrs->head, list) {
1003 /* skip "trusted." attributes for unprivileged callers */
1004 if (!trusted && xattr_is_trusted(xattr->name))
1007 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1011 spin_unlock(&xattrs->lock);
1013 return err ? err : size - remaining_size;
1017 * Adds an extended attribute to the list
1019 void simple_xattr_list_add(struct simple_xattrs *xattrs,
1020 struct simple_xattr *new_xattr)
1022 spin_lock(&xattrs->lock);
1023 list_add(&new_xattr->list, &xattrs->head);
1024 spin_unlock(&xattrs->lock);