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/syscalls.h>
18 #include <linux/module.h>
19 #include <linux/fsnotify.h>
20 #include <linux/audit.h>
21 #include <asm/uaccess.h>
25 * Check permissions for extended attribute access. This is a bit complicated
26 * because different namespaces have very different rules.
29 xattr_permission(struct inode *inode, const char *name, int mask)
32 * We can never set or remove an extended attribute on a read-only
33 * filesystem or on an immutable / append-only inode.
35 if (mask & MAY_WRITE) {
36 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
41 * No restriction for security.* and system.* from the VFS. Decision
42 * on these is left to the underlying filesystem / security module.
44 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
45 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
49 * The trusted.* namespace can only be accessed by privileged users.
51 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
52 if (!capable(CAP_SYS_ADMIN))
53 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
58 * In the user.* namespace, only regular files and directories can have
59 * extended attributes. For sticky directories, only the owner and
60 * privileged users can write attributes.
62 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
63 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
64 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
65 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
66 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
70 return inode_permission(inode, mask);
74 * __vfs_setxattr_noperm - perform setxattr operation without performing
77 * @dentry - object to perform setxattr on
78 * @name - xattr name to set
79 * @value - value to set @name to
80 * @size - size of @value
81 * @flags - flags to pass into filesystem operations
83 * returns the result of the internal setxattr or setsecurity operations.
85 * This function requires the caller to lock the inode's i_mutex before it
86 * is executed. It also assumes that the caller will make the appropriate
89 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
90 const void *value, size_t size, int flags)
92 struct inode *inode = dentry->d_inode;
93 int error = -EOPNOTSUPP;
95 if (inode->i_op->setxattr) {
96 error = inode->i_op->setxattr(dentry, name, value, size, flags);
98 fsnotify_xattr(dentry);
99 security_inode_post_setxattr(dentry, name, value,
102 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
103 XATTR_SECURITY_PREFIX_LEN)) {
104 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
105 error = security_inode_setsecurity(inode, suffix, value,
108 fsnotify_xattr(dentry);
116 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
117 size_t size, int flags)
119 struct inode *inode = dentry->d_inode;
122 error = xattr_permission(inode, name, MAY_WRITE);
126 mutex_lock(&inode->i_mutex);
127 error = security_inode_setxattr(dentry, name, value, size, flags);
131 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
134 mutex_unlock(&inode->i_mutex);
137 EXPORT_SYMBOL_GPL(vfs_setxattr);
140 xattr_getsecurity(struct inode *inode, const char *name, void *value,
146 if (!value || !size) {
147 len = security_inode_getsecurity(inode, name, &buffer, false);
151 len = security_inode_getsecurity(inode, name, &buffer, true);
158 memcpy(value, buffer, len);
160 security_release_secctx(buffer, len);
164 EXPORT_SYMBOL_GPL(xattr_getsecurity);
167 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
169 struct inode *inode = dentry->d_inode;
172 error = xattr_permission(inode, name, MAY_READ);
176 error = security_inode_getxattr(dentry, name);
180 if (!strncmp(name, XATTR_SECURITY_PREFIX,
181 XATTR_SECURITY_PREFIX_LEN)) {
182 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
183 int ret = xattr_getsecurity(inode, suffix, value, size);
185 * Only overwrite the return value if a security module
186 * is actually active.
188 if (ret == -EOPNOTSUPP)
193 if (inode->i_op->getxattr)
194 error = inode->i_op->getxattr(dentry, name, value, size);
200 EXPORT_SYMBOL_GPL(vfs_getxattr);
203 vfs_listxattr(struct dentry *d, char *list, size_t size)
207 error = security_inode_listxattr(d);
211 if (d->d_inode->i_op->listxattr) {
212 error = d->d_inode->i_op->listxattr(d, list, size);
214 error = security_inode_listsecurity(d->d_inode, list, size);
215 if (size && error > size)
220 EXPORT_SYMBOL_GPL(vfs_listxattr);
223 vfs_removexattr(struct dentry *dentry, const char *name)
225 struct inode *inode = dentry->d_inode;
228 if (!inode->i_op->removexattr)
231 error = xattr_permission(inode, name, MAY_WRITE);
235 error = security_inode_removexattr(dentry, name);
239 mutex_lock(&inode->i_mutex);
240 error = inode->i_op->removexattr(dentry, name);
241 mutex_unlock(&inode->i_mutex);
244 fsnotify_xattr(dentry);
247 EXPORT_SYMBOL_GPL(vfs_removexattr);
251 * Extended attribute SET operations
254 setxattr(struct dentry *d, const char __user *name, const void __user *value,
255 size_t size, int flags)
259 char kname[XATTR_NAME_MAX + 1];
261 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
264 error = strncpy_from_user(kname, name, sizeof(kname));
265 if (error == 0 || error == sizeof(kname))
271 if (size > XATTR_SIZE_MAX)
273 kvalue = memdup_user(value, size);
275 return PTR_ERR(kvalue);
278 error = vfs_setxattr(d, kname, kvalue, size, flags);
283 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
284 const char __user *, name, const void __user *, value,
285 size_t, size, int, flags)
290 error = user_path(pathname, &path);
293 error = mnt_want_write(path.mnt);
295 error = setxattr(path.dentry, name, value, size, flags);
296 mnt_drop_write(path.mnt);
302 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
303 const char __user *, name, const void __user *, value,
304 size_t, size, int, flags)
309 error = user_lpath(pathname, &path);
312 error = mnt_want_write(path.mnt);
314 error = setxattr(path.dentry, name, value, size, flags);
315 mnt_drop_write(path.mnt);
321 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
322 const void __user *,value, size_t, size, int, flags)
325 struct dentry *dentry;
331 dentry = f->f_path.dentry;
332 audit_inode(NULL, dentry);
333 error = mnt_want_write_file(f);
335 error = setxattr(dentry, name, value, size, flags);
336 mnt_drop_write(f->f_path.mnt);
343 * Extended attribute GET operations
346 getxattr(struct dentry *d, const char __user *name, void __user *value,
351 char kname[XATTR_NAME_MAX + 1];
353 error = strncpy_from_user(kname, name, sizeof(kname));
354 if (error == 0 || error == sizeof(kname))
360 if (size > XATTR_SIZE_MAX)
361 size = XATTR_SIZE_MAX;
362 kvalue = kzalloc(size, GFP_KERNEL);
367 error = vfs_getxattr(d, kname, kvalue, size);
369 if (size && copy_to_user(value, kvalue, error))
371 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
372 /* The file system tried to returned a value bigger
373 than XATTR_SIZE_MAX bytes. Not possible. */
380 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
381 const char __user *, name, void __user *, value, size_t, size)
386 error = user_path(pathname, &path);
389 error = getxattr(path.dentry, name, value, size);
394 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
395 const char __user *, name, void __user *, value, size_t, size)
400 error = user_lpath(pathname, &path);
403 error = getxattr(path.dentry, name, value, size);
408 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
409 void __user *, value, size_t, size)
412 ssize_t error = -EBADF;
417 audit_inode(NULL, f->f_path.dentry);
418 error = getxattr(f->f_path.dentry, name, value, size);
424 * Extended attribute LIST operations
427 listxattr(struct dentry *d, char __user *list, size_t size)
433 if (size > XATTR_LIST_MAX)
434 size = XATTR_LIST_MAX;
435 klist = kmalloc(size, GFP_KERNEL);
440 error = vfs_listxattr(d, klist, size);
442 if (size && copy_to_user(list, klist, error))
444 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
445 /* The file system tried to returned a list bigger
446 than XATTR_LIST_MAX bytes. Not possible. */
453 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
459 error = user_path(pathname, &path);
462 error = listxattr(path.dentry, list, size);
467 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
473 error = user_lpath(pathname, &path);
476 error = listxattr(path.dentry, list, size);
481 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
484 ssize_t error = -EBADF;
489 audit_inode(NULL, f->f_path.dentry);
490 error = listxattr(f->f_path.dentry, list, size);
496 * Extended attribute REMOVE operations
499 removexattr(struct dentry *d, const char __user *name)
502 char kname[XATTR_NAME_MAX + 1];
504 error = strncpy_from_user(kname, name, sizeof(kname));
505 if (error == 0 || error == sizeof(kname))
510 return vfs_removexattr(d, kname);
513 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
514 const char __user *, name)
519 error = user_path(pathname, &path);
522 error = mnt_want_write(path.mnt);
524 error = removexattr(path.dentry, name);
525 mnt_drop_write(path.mnt);
531 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
532 const char __user *, name)
537 error = user_lpath(pathname, &path);
540 error = mnt_want_write(path.mnt);
542 error = removexattr(path.dentry, name);
543 mnt_drop_write(path.mnt);
549 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
552 struct dentry *dentry;
558 dentry = f->f_path.dentry;
559 audit_inode(NULL, dentry);
560 error = mnt_want_write_file(f);
562 error = removexattr(dentry, name);
563 mnt_drop_write(f->f_path.mnt);
571 strcmp_prefix(const char *a, const char *a_prefix)
573 while (*a_prefix && *a == *a_prefix) {
577 return *a_prefix ? NULL : a;
581 * In order to implement different sets of xattr operations for each xattr
582 * prefix with the generic xattr API, a filesystem should create a
583 * null-terminated array of struct xattr_handler (one for each prefix) and
584 * hang a pointer to it off of the s_xattr field of the superblock.
586 * The generic_fooxattr() functions will use this list to dispatch xattr
587 * operations to the correct xattr_handler.
589 #define for_each_xattr_handler(handlers, handler) \
590 for ((handler) = *(handlers)++; \
592 (handler) = *(handlers)++)
595 * Find the xattr_handler with the matching prefix.
597 static const struct xattr_handler *
598 xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
600 const struct xattr_handler *handler;
605 for_each_xattr_handler(handlers, handler) {
606 const char *n = strcmp_prefix(*name, handler->prefix);
616 * Find the handler for the prefix and dispatch its get() operation.
619 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
621 const struct xattr_handler *handler;
623 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
626 return handler->get(dentry, name, buffer, size, handler->flags);
630 * Combine the results of the list() operation from every xattr_handler in the
634 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
636 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
637 unsigned int size = 0;
640 for_each_xattr_handler(handlers, handler) {
641 size += handler->list(dentry, NULL, 0, NULL, 0,
647 for_each_xattr_handler(handlers, handler) {
648 size = handler->list(dentry, buf, buffer_size,
649 NULL, 0, handler->flags);
650 if (size > buffer_size)
661 * Find the handler for the prefix and dispatch its set() operation.
664 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
666 const struct xattr_handler *handler;
669 value = ""; /* empty EA, do not remove */
670 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
673 return handler->set(dentry, name, value, size, flags, handler->flags);
677 * Find the handler for the prefix and dispatch its set() operation to remove
678 * any associated extended attribute.
681 generic_removexattr(struct dentry *dentry, const char *name)
683 const struct xattr_handler *handler;
685 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
688 return handler->set(dentry, name, NULL, 0,
689 XATTR_REPLACE, handler->flags);
692 EXPORT_SYMBOL(generic_getxattr);
693 EXPORT_SYMBOL(generic_listxattr);
694 EXPORT_SYMBOL(generic_setxattr);
695 EXPORT_SYMBOL(generic_removexattr);