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/namei.h>
15 #include <linux/security.h>
16 #include <linux/syscalls.h>
17 #include <linux/module.h>
18 #include <linux/fsnotify.h>
19 #include <linux/audit.h>
20 #include <asm/uaccess.h>
24 * Check permissions for extended attribute access. This is a bit complicated
25 * because different namespaces have very different rules.
28 xattr_permission(struct inode *inode, const char *name, int mask)
31 * We can never set or remove an extended attribute on a read-only
32 * filesystem or on an immutable / append-only inode.
34 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 a privileged user.
52 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
53 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
55 /* In user.* namespace, only regular files and directories can have
56 * extended attributes. For sticky directories, only the owner and
57 * privileged user can write attributes.
59 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
60 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
62 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
63 (mask & MAY_WRITE) && !is_owner_or_cap(inode))
67 return permission(inode, mask, NULL);
71 vfs_setxattr(struct dentry *dentry, char *name, void *value,
72 size_t size, int flags)
74 struct inode *inode = dentry->d_inode;
77 error = xattr_permission(inode, name, MAY_WRITE);
81 mutex_lock(&inode->i_mutex);
82 error = security_inode_setxattr(dentry, name, value, size, flags);
86 if (inode->i_op->setxattr) {
87 error = inode->i_op->setxattr(dentry, name, value, size, flags);
89 fsnotify_xattr(dentry);
90 security_inode_post_setxattr(dentry, name, value,
93 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
94 XATTR_SECURITY_PREFIX_LEN)) {
95 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
96 error = security_inode_setsecurity(inode, suffix, value,
99 fsnotify_xattr(dentry);
102 mutex_unlock(&inode->i_mutex);
105 EXPORT_SYMBOL_GPL(vfs_setxattr);
108 xattr_getsecurity(struct inode *inode, const char *name, void *value,
114 if (!value || !size) {
115 len = security_inode_getsecurity(inode, name, &buffer, false);
119 len = security_inode_getsecurity(inode, name, &buffer, true);
126 memcpy(value, buffer, len);
128 security_release_secctx(buffer, len);
132 EXPORT_SYMBOL_GPL(xattr_getsecurity);
135 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
137 struct inode *inode = dentry->d_inode;
140 error = xattr_permission(inode, name, MAY_READ);
144 error = security_inode_getxattr(dentry, name);
148 if (!strncmp(name, XATTR_SECURITY_PREFIX,
149 XATTR_SECURITY_PREFIX_LEN)) {
150 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
151 int ret = xattr_getsecurity(inode, suffix, value, size);
153 * Only overwrite the return value if a security module
154 * is actually active.
156 if (ret == -EOPNOTSUPP)
161 if (inode->i_op->getxattr)
162 error = inode->i_op->getxattr(dentry, name, value, size);
168 EXPORT_SYMBOL_GPL(vfs_getxattr);
171 vfs_listxattr(struct dentry *d, char *list, size_t size)
175 error = security_inode_listxattr(d);
179 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
180 error = d->d_inode->i_op->listxattr(d, list, size);
182 error = security_inode_listsecurity(d->d_inode, list, size);
183 if (size && error > size)
188 EXPORT_SYMBOL_GPL(vfs_listxattr);
191 vfs_removexattr(struct dentry *dentry, char *name)
193 struct inode *inode = dentry->d_inode;
196 if (!inode->i_op->removexattr)
199 error = xattr_permission(inode, name, MAY_WRITE);
203 error = security_inode_removexattr(dentry, name);
207 mutex_lock(&inode->i_mutex);
208 error = inode->i_op->removexattr(dentry, name);
209 mutex_unlock(&inode->i_mutex);
212 fsnotify_xattr(dentry);
215 EXPORT_SYMBOL_GPL(vfs_removexattr);
219 * Extended attribute SET operations
222 setxattr(struct dentry *d, char __user *name, void __user *value,
223 size_t size, int flags)
227 char kname[XATTR_NAME_MAX + 1];
229 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
232 error = strncpy_from_user(kname, name, sizeof(kname));
233 if (error == 0 || error == sizeof(kname))
239 if (size > XATTR_SIZE_MAX)
241 kvalue = kmalloc(size, GFP_KERNEL);
244 if (copy_from_user(kvalue, value, size)) {
250 error = vfs_setxattr(d, kname, kvalue, size, flags);
256 sys_setxattr(char __user *path, char __user *name, void __user *value,
257 size_t size, int flags)
262 error = user_path_walk(path, &nd);
265 error = setxattr(nd.dentry, name, value, size, flags);
271 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
272 size_t size, int flags)
277 error = user_path_walk_link(path, &nd);
280 error = setxattr(nd.dentry, name, value, size, flags);
286 sys_fsetxattr(int fd, char __user *name, void __user *value,
287 size_t size, int flags)
290 struct dentry *dentry;
296 dentry = f->f_path.dentry;
297 audit_inode(NULL, dentry);
298 error = setxattr(dentry, name, value, size, flags);
304 * Extended attribute GET operations
307 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
311 char kname[XATTR_NAME_MAX + 1];
313 error = strncpy_from_user(kname, name, sizeof(kname));
314 if (error == 0 || error == sizeof(kname))
320 if (size > XATTR_SIZE_MAX)
321 size = XATTR_SIZE_MAX;
322 kvalue = kzalloc(size, GFP_KERNEL);
327 error = vfs_getxattr(d, kname, kvalue, size);
329 if (size && copy_to_user(value, kvalue, error))
331 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
332 /* The file system tried to returned a value bigger
333 than XATTR_SIZE_MAX bytes. Not possible. */
341 sys_getxattr(char __user *path, char __user *name, void __user *value,
347 error = user_path_walk(path, &nd);
350 error = getxattr(nd.dentry, name, value, size);
356 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
362 error = user_path_walk_link(path, &nd);
365 error = getxattr(nd.dentry, name, value, size);
371 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
374 ssize_t error = -EBADF;
379 audit_inode(NULL, f->f_path.dentry);
380 error = getxattr(f->f_path.dentry, name, value, size);
386 * Extended attribute LIST operations
389 listxattr(struct dentry *d, char __user *list, size_t size)
395 if (size > XATTR_LIST_MAX)
396 size = XATTR_LIST_MAX;
397 klist = kmalloc(size, GFP_KERNEL);
402 error = vfs_listxattr(d, klist, size);
404 if (size && copy_to_user(list, klist, error))
406 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
407 /* The file system tried to returned a list bigger
408 than XATTR_LIST_MAX bytes. Not possible. */
416 sys_listxattr(char __user *path, char __user *list, size_t size)
421 error = user_path_walk(path, &nd);
424 error = listxattr(nd.dentry, list, size);
430 sys_llistxattr(char __user *path, char __user *list, size_t size)
435 error = user_path_walk_link(path, &nd);
438 error = listxattr(nd.dentry, list, size);
444 sys_flistxattr(int fd, char __user *list, size_t size)
447 ssize_t error = -EBADF;
452 audit_inode(NULL, f->f_path.dentry);
453 error = listxattr(f->f_path.dentry, list, size);
459 * Extended attribute REMOVE operations
462 removexattr(struct dentry *d, char __user *name)
465 char kname[XATTR_NAME_MAX + 1];
467 error = strncpy_from_user(kname, name, sizeof(kname));
468 if (error == 0 || error == sizeof(kname))
473 return vfs_removexattr(d, kname);
477 sys_removexattr(char __user *path, char __user *name)
482 error = user_path_walk(path, &nd);
485 error = removexattr(nd.dentry, name);
491 sys_lremovexattr(char __user *path, char __user *name)
496 error = user_path_walk_link(path, &nd);
499 error = removexattr(nd.dentry, name);
505 sys_fremovexattr(int fd, char __user *name)
508 struct dentry *dentry;
514 dentry = f->f_path.dentry;
515 audit_inode(NULL, dentry);
516 error = removexattr(dentry, name);
523 strcmp_prefix(const char *a, const char *a_prefix)
525 while (*a_prefix && *a == *a_prefix) {
529 return *a_prefix ? NULL : a;
533 * In order to implement different sets of xattr operations for each xattr
534 * prefix with the generic xattr API, a filesystem should create a
535 * null-terminated array of struct xattr_handler (one for each prefix) and
536 * hang a pointer to it off of the s_xattr field of the superblock.
538 * The generic_fooxattr() functions will use this list to dispatch xattr
539 * operations to the correct xattr_handler.
541 #define for_each_xattr_handler(handlers, handler) \
542 for ((handler) = *(handlers)++; \
544 (handler) = *(handlers)++)
547 * Find the xattr_handler with the matching prefix.
549 static struct xattr_handler *
550 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
552 struct xattr_handler *handler;
557 for_each_xattr_handler(handlers, handler) {
558 const char *n = strcmp_prefix(*name, handler->prefix);
568 * Find the handler for the prefix and dispatch its get() operation.
571 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
573 struct xattr_handler *handler;
574 struct inode *inode = dentry->d_inode;
576 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
579 return handler->get(inode, name, buffer, size);
583 * Combine the results of the list() operation from every xattr_handler in the
587 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
589 struct inode *inode = dentry->d_inode;
590 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
591 unsigned int size = 0;
594 for_each_xattr_handler(handlers, handler)
595 size += handler->list(inode, NULL, 0, NULL, 0);
599 for_each_xattr_handler(handlers, handler) {
600 size = handler->list(inode, buf, buffer_size, NULL, 0);
601 if (size > buffer_size)
612 * Find the handler for the prefix and dispatch its set() operation.
615 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
617 struct xattr_handler *handler;
618 struct inode *inode = dentry->d_inode;
621 value = ""; /* empty EA, do not remove */
622 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
625 return handler->set(inode, name, value, size, flags);
629 * Find the handler for the prefix and dispatch its set() operation to remove
630 * any associated extended attribute.
633 generic_removexattr(struct dentry *dentry, const char *name)
635 struct xattr_handler *handler;
636 struct inode *inode = dentry->d_inode;
638 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
641 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
644 EXPORT_SYMBOL(generic_getxattr);
645 EXPORT_SYMBOL(generic_listxattr);
646 EXPORT_SYMBOL(generic_setxattr);
647 EXPORT_SYMBOL(generic_removexattr);