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/module.h>
20 #include <linux/fsnotify.h>
21 #include <linux/audit.h>
22 #include <asm/uaccess.h>
26 * Check permissions for extended attribute access. This is a bit complicated
27 * because different namespaces have very different rules.
30 xattr_permission(struct inode *inode, const char *name, int mask)
33 * We can never set or remove an extended attribute on a read-only
34 * filesystem or on an immutable / append-only inode.
36 if (mask & MAY_WRITE) {
37 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
42 * No restriction for security.* and system.* from the VFS. Decision
43 * on these is left to the underlying filesystem / security module.
45 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
46 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
50 * The trusted.* namespace can only be accessed by privileged users.
52 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
53 if (!capable(CAP_SYS_ADMIN))
54 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
59 * In the user.* namespace, only regular files and directories can have
60 * extended attributes. For sticky directories, only the owner and
61 * privileged users can write attributes.
63 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
64 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
65 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
66 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
67 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
71 return inode_permission(inode, mask);
75 * __vfs_setxattr_noperm - perform setxattr operation without performing
78 * @dentry - object to perform setxattr on
79 * @name - xattr name to set
80 * @value - value to set @name to
81 * @size - size of @value
82 * @flags - flags to pass into filesystem operations
84 * returns the result of the internal setxattr or setsecurity operations.
86 * This function requires the caller to lock the inode's i_mutex before it
87 * is executed. It also assumes that the caller will make the appropriate
90 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
91 const void *value, size_t size, int flags)
93 struct inode *inode = dentry->d_inode;
94 int error = -EOPNOTSUPP;
95 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
96 XATTR_SECURITY_PREFIX_LEN);
99 inode->i_flags &= ~S_NOSEC;
100 if (inode->i_op->setxattr) {
101 error = inode->i_op->setxattr(dentry, name, value, size, flags);
103 fsnotify_xattr(dentry);
104 security_inode_post_setxattr(dentry, name, value,
108 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
109 error = security_inode_setsecurity(inode, suffix, value,
112 fsnotify_xattr(dentry);
120 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
121 size_t size, int flags)
123 struct inode *inode = dentry->d_inode;
126 error = xattr_permission(inode, name, MAY_WRITE);
130 mutex_lock(&inode->i_mutex);
131 error = security_inode_setxattr(dentry, name, value, size, flags);
135 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
138 mutex_unlock(&inode->i_mutex);
141 EXPORT_SYMBOL_GPL(vfs_setxattr);
144 xattr_getsecurity(struct inode *inode, const char *name, void *value,
150 if (!value || !size) {
151 len = security_inode_getsecurity(inode, name, &buffer, false);
155 len = security_inode_getsecurity(inode, name, &buffer, true);
162 memcpy(value, buffer, len);
164 security_release_secctx(buffer, len);
168 EXPORT_SYMBOL_GPL(xattr_getsecurity);
171 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
173 * Allocate memory, if not already allocated, or re-allocate correct size,
174 * before retrieving the extended attribute.
176 * Returns the result of alloc, if failed, or the getxattr operation.
179 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
180 size_t xattr_size, gfp_t flags)
182 struct inode *inode = dentry->d_inode;
183 char *value = *xattr_value;
186 error = xattr_permission(inode, name, MAY_READ);
190 if (!inode->i_op->getxattr)
193 error = inode->i_op->getxattr(dentry, name, NULL, 0);
197 if (!value || (error > xattr_size)) {
198 value = krealloc(*xattr_value, error + 1, flags);
201 memset(value, 0, error + 1);
204 error = inode->i_op->getxattr(dentry, name, value, error);
205 *xattr_value = value;
209 /* Compare an extended attribute value with the given value */
210 int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
211 const char *value, size_t size, gfp_t flags)
213 char *xattr_value = NULL;
216 rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_value, 0, flags);
220 if ((rc != size) || (memcmp(xattr_value, value, rc) != 0))
229 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
231 struct inode *inode = dentry->d_inode;
234 error = xattr_permission(inode, name, MAY_READ);
238 error = security_inode_getxattr(dentry, name);
242 if (!strncmp(name, XATTR_SECURITY_PREFIX,
243 XATTR_SECURITY_PREFIX_LEN)) {
244 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
245 int ret = xattr_getsecurity(inode, suffix, value, size);
247 * Only overwrite the return value if a security module
248 * is actually active.
250 if (ret == -EOPNOTSUPP)
255 if (inode->i_op->getxattr)
256 error = inode->i_op->getxattr(dentry, name, value, size);
262 EXPORT_SYMBOL_GPL(vfs_getxattr);
265 vfs_listxattr(struct dentry *d, char *list, size_t size)
269 error = security_inode_listxattr(d);
273 if (d->d_inode->i_op->listxattr) {
274 error = d->d_inode->i_op->listxattr(d, list, size);
276 error = security_inode_listsecurity(d->d_inode, list, size);
277 if (size && error > size)
282 EXPORT_SYMBOL_GPL(vfs_listxattr);
285 vfs_removexattr(struct dentry *dentry, const char *name)
287 struct inode *inode = dentry->d_inode;
290 if (!inode->i_op->removexattr)
293 error = xattr_permission(inode, name, MAY_WRITE);
297 error = security_inode_removexattr(dentry, name);
301 mutex_lock(&inode->i_mutex);
302 error = inode->i_op->removexattr(dentry, name);
303 mutex_unlock(&inode->i_mutex);
306 fsnotify_xattr(dentry);
307 evm_inode_post_removexattr(dentry, name);
311 EXPORT_SYMBOL_GPL(vfs_removexattr);
315 * Extended attribute SET operations
318 setxattr(struct dentry *d, const char __user *name, const void __user *value,
319 size_t size, int flags)
323 char kname[XATTR_NAME_MAX + 1];
325 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
328 error = strncpy_from_user(kname, name, sizeof(kname));
329 if (error == 0 || error == sizeof(kname))
335 if (size > XATTR_SIZE_MAX)
337 kvalue = memdup_user(value, size);
339 return PTR_ERR(kvalue);
342 error = vfs_setxattr(d, kname, kvalue, size, flags);
347 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
348 const char __user *, name, const void __user *, value,
349 size_t, size, int, flags)
354 error = user_path(pathname, &path);
357 error = mnt_want_write(path.mnt);
359 error = setxattr(path.dentry, name, value, size, flags);
360 mnt_drop_write(path.mnt);
366 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
367 const char __user *, name, const void __user *, value,
368 size_t, size, int, flags)
373 error = user_lpath(pathname, &path);
376 error = mnt_want_write(path.mnt);
378 error = setxattr(path.dentry, name, value, size, flags);
379 mnt_drop_write(path.mnt);
385 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
386 const void __user *,value, size_t, size, int, flags)
389 struct dentry *dentry;
395 dentry = f->f_path.dentry;
396 audit_inode(NULL, dentry);
397 error = mnt_want_write_file(f);
399 error = setxattr(dentry, name, value, size, flags);
400 mnt_drop_write_file(f);
407 * Extended attribute GET operations
410 getxattr(struct dentry *d, const char __user *name, void __user *value,
415 char kname[XATTR_NAME_MAX + 1];
417 error = strncpy_from_user(kname, name, sizeof(kname));
418 if (error == 0 || error == sizeof(kname))
424 if (size > XATTR_SIZE_MAX)
425 size = XATTR_SIZE_MAX;
426 kvalue = kzalloc(size, GFP_KERNEL);
431 error = vfs_getxattr(d, kname, kvalue, size);
433 if (size && copy_to_user(value, kvalue, error))
435 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
436 /* The file system tried to returned a value bigger
437 than XATTR_SIZE_MAX bytes. Not possible. */
444 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
445 const char __user *, name, void __user *, value, size_t, size)
450 error = user_path(pathname, &path);
453 error = getxattr(path.dentry, name, value, size);
458 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
459 const char __user *, name, void __user *, value, size_t, size)
464 error = user_lpath(pathname, &path);
467 error = getxattr(path.dentry, name, value, size);
472 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
473 void __user *, value, size_t, size)
476 ssize_t error = -EBADF;
481 audit_inode(NULL, f->f_path.dentry);
482 error = getxattr(f->f_path.dentry, name, value, size);
488 * Extended attribute LIST operations
491 listxattr(struct dentry *d, char __user *list, size_t size)
497 if (size > XATTR_LIST_MAX)
498 size = XATTR_LIST_MAX;
499 klist = kmalloc(size, GFP_KERNEL);
504 error = vfs_listxattr(d, klist, size);
506 if (size && copy_to_user(list, klist, error))
508 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
509 /* The file system tried to returned a list bigger
510 than XATTR_LIST_MAX bytes. Not possible. */
517 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
523 error = user_path(pathname, &path);
526 error = listxattr(path.dentry, list, size);
531 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
537 error = user_lpath(pathname, &path);
540 error = listxattr(path.dentry, list, size);
545 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
548 ssize_t error = -EBADF;
553 audit_inode(NULL, f->f_path.dentry);
554 error = listxattr(f->f_path.dentry, list, size);
560 * Extended attribute REMOVE operations
563 removexattr(struct dentry *d, const char __user *name)
566 char kname[XATTR_NAME_MAX + 1];
568 error = strncpy_from_user(kname, name, sizeof(kname));
569 if (error == 0 || error == sizeof(kname))
574 return vfs_removexattr(d, kname);
577 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
578 const char __user *, name)
583 error = user_path(pathname, &path);
586 error = mnt_want_write(path.mnt);
588 error = removexattr(path.dentry, name);
589 mnt_drop_write(path.mnt);
595 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
596 const char __user *, name)
601 error = user_lpath(pathname, &path);
604 error = mnt_want_write(path.mnt);
606 error = removexattr(path.dentry, name);
607 mnt_drop_write(path.mnt);
613 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
616 struct dentry *dentry;
622 dentry = f->f_path.dentry;
623 audit_inode(NULL, dentry);
624 error = mnt_want_write_file(f);
626 error = removexattr(dentry, name);
627 mnt_drop_write_file(f);
635 strcmp_prefix(const char *a, const char *a_prefix)
637 while (*a_prefix && *a == *a_prefix) {
641 return *a_prefix ? NULL : a;
645 * In order to implement different sets of xattr operations for each xattr
646 * prefix with the generic xattr API, a filesystem should create a
647 * null-terminated array of struct xattr_handler (one for each prefix) and
648 * hang a pointer to it off of the s_xattr field of the superblock.
650 * The generic_fooxattr() functions will use this list to dispatch xattr
651 * operations to the correct xattr_handler.
653 #define for_each_xattr_handler(handlers, handler) \
654 for ((handler) = *(handlers)++; \
656 (handler) = *(handlers)++)
659 * Find the xattr_handler with the matching prefix.
661 static const struct xattr_handler *
662 xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
664 const struct xattr_handler *handler;
669 for_each_xattr_handler(handlers, handler) {
670 const char *n = strcmp_prefix(*name, handler->prefix);
680 * Find the handler for the prefix and dispatch its get() operation.
683 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
685 const struct xattr_handler *handler;
687 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
690 return handler->get(dentry, name, buffer, size, handler->flags);
694 * Combine the results of the list() operation from every xattr_handler in the
698 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
700 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
701 unsigned int size = 0;
704 for_each_xattr_handler(handlers, handler) {
705 size += handler->list(dentry, NULL, 0, NULL, 0,
711 for_each_xattr_handler(handlers, handler) {
712 size = handler->list(dentry, buf, buffer_size,
713 NULL, 0, handler->flags);
714 if (size > buffer_size)
725 * Find the handler for the prefix and dispatch its set() operation.
728 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
730 const struct xattr_handler *handler;
733 value = ""; /* empty EA, do not remove */
734 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
737 return handler->set(dentry, name, value, size, flags, handler->flags);
741 * Find the handler for the prefix and dispatch its set() operation to remove
742 * any associated extended attribute.
745 generic_removexattr(struct dentry *dentry, const char *name)
747 const struct xattr_handler *handler;
749 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
752 return handler->set(dentry, name, NULL, 0,
753 XATTR_REPLACE, handler->flags);
756 EXPORT_SYMBOL(generic_getxattr);
757 EXPORT_SYMBOL(generic_listxattr);
758 EXPORT_SYMBOL(generic_setxattr);
759 EXPORT_SYMBOL(generic_removexattr);