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/smp_lock.h>
13 #include <linux/file.h>
14 #include <linux/xattr.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 <linux/mount.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) {
39 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
44 * No restriction for security.* and system.* from the VFS. Decision
45 * on these is left to the underlying filesystem / security module.
47 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
48 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
52 * The trusted.* namespace can only be accessed by a privileged user.
54 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
55 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
57 /* In user.* namespace, only regular files and directories can have
58 * extended attributes. For sticky directories, only the owner and
59 * privileged user can write attributes.
61 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
62 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
64 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
65 (mask & MAY_WRITE) && (current->fsuid != inode->i_uid) &&
70 return permission(inode, mask, NULL);
74 vfs_setxattr(struct dentry *dentry, char *name, void *value,
75 size_t size, int flags)
77 struct inode *inode = dentry->d_inode;
80 error = xattr_permission(inode, name, MAY_WRITE);
84 mutex_lock(&inode->i_mutex);
85 error = security_inode_setxattr(dentry, name, value, size, flags);
89 if (inode->i_op->setxattr) {
90 error = inode->i_op->setxattr(dentry, name, value, size, flags);
92 fsnotify_xattr(dentry);
93 security_inode_post_setxattr(dentry, name, value,
96 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
97 XATTR_SECURITY_PREFIX_LEN)) {
98 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
99 error = security_inode_setsecurity(inode, suffix, value,
102 fsnotify_xattr(dentry);
105 mutex_unlock(&inode->i_mutex);
108 EXPORT_SYMBOL_GPL(vfs_setxattr);
111 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
113 struct inode *inode = dentry->d_inode;
116 error = xattr_permission(inode, name, MAY_READ);
120 error = security_inode_getxattr(dentry, name);
124 if (inode->i_op->getxattr)
125 error = inode->i_op->getxattr(dentry, name, value, size);
129 if (!strncmp(name, XATTR_SECURITY_PREFIX,
130 XATTR_SECURITY_PREFIX_LEN)) {
131 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
132 int ret = security_inode_getsecurity(inode, suffix, value,
135 * Only overwrite the return value if a security module
136 * is actually active.
138 if (ret != -EOPNOTSUPP)
144 EXPORT_SYMBOL_GPL(vfs_getxattr);
147 vfs_listxattr(struct dentry *d, char *list, size_t size)
151 error = security_inode_listxattr(d);
155 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
156 error = d->d_inode->i_op->listxattr(d, list, size);
158 error = security_inode_listsecurity(d->d_inode, list, size);
159 if (size && error > size)
164 EXPORT_SYMBOL_GPL(vfs_listxattr);
167 vfs_removexattr(struct dentry *dentry, char *name)
169 struct inode *inode = dentry->d_inode;
172 if (!inode->i_op->removexattr)
175 error = xattr_permission(inode, name, MAY_WRITE);
179 error = security_inode_removexattr(dentry, name);
183 mutex_lock(&inode->i_mutex);
184 error = inode->i_op->removexattr(dentry, name);
185 mutex_unlock(&inode->i_mutex);
188 fsnotify_xattr(dentry);
191 EXPORT_SYMBOL_GPL(vfs_removexattr);
195 * Extended attribute SET operations
198 setxattr(struct dentry *d, char __user *name, void __user *value,
199 size_t size, int flags, struct vfsmount *mnt)
203 char kname[XATTR_NAME_MAX + 1];
205 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
208 error = strncpy_from_user(kname, name, sizeof(kname));
209 if (error == 0 || error == sizeof(kname))
215 if (size > XATTR_SIZE_MAX)
217 kvalue = kmalloc(size, GFP_KERNEL);
220 if (copy_from_user(kvalue, value, size)) {
226 if (MNT_IS_RDONLY(mnt))
229 error = vfs_setxattr(d, kname, kvalue, size, flags);
235 sys_setxattr(char __user *path, char __user *name, void __user *value,
236 size_t size, int flags)
241 error = user_path_walk(path, &nd);
244 error = setxattr(nd.dentry, name, value, size, flags, nd.mnt);
250 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
251 size_t size, int flags)
256 error = user_path_walk_link(path, &nd);
259 error = setxattr(nd.dentry, name, value, size, flags, nd.mnt);
265 sys_fsetxattr(int fd, char __user *name, void __user *value,
266 size_t size, int flags)
269 struct dentry *dentry;
275 dentry = f->f_path.dentry;
276 audit_inode(NULL, dentry->d_inode);
277 error = setxattr(dentry, name, value, size, flags, f->f_vfsmnt);
283 * Extended attribute GET operations
286 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
290 char kname[XATTR_NAME_MAX + 1];
292 error = strncpy_from_user(kname, name, sizeof(kname));
293 if (error == 0 || error == sizeof(kname))
299 if (size > XATTR_SIZE_MAX)
300 size = XATTR_SIZE_MAX;
301 kvalue = kzalloc(size, GFP_KERNEL);
306 error = vfs_getxattr(d, kname, kvalue, size);
308 if (size && copy_to_user(value, kvalue, error))
310 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
311 /* The file system tried to returned a value bigger
312 than XATTR_SIZE_MAX bytes. Not possible. */
320 sys_getxattr(char __user *path, char __user *name, void __user *value,
326 error = user_path_walk(path, &nd);
329 error = getxattr(nd.dentry, name, value, size);
335 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
341 error = user_path_walk_link(path, &nd);
344 error = getxattr(nd.dentry, name, value, size);
350 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
353 ssize_t error = -EBADF;
358 error = getxattr(f->f_path.dentry, name, value, size);
364 * Extended attribute LIST operations
367 listxattr(struct dentry *d, char __user *list, size_t size)
373 if (size > XATTR_LIST_MAX)
374 size = XATTR_LIST_MAX;
375 klist = kmalloc(size, GFP_KERNEL);
380 error = vfs_listxattr(d, klist, size);
382 if (size && copy_to_user(list, klist, error))
384 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
385 /* The file system tried to returned a list bigger
386 than XATTR_LIST_MAX bytes. Not possible. */
394 sys_listxattr(char __user *path, char __user *list, size_t size)
399 error = user_path_walk(path, &nd);
402 error = listxattr(nd.dentry, list, size);
408 sys_llistxattr(char __user *path, char __user *list, size_t size)
413 error = user_path_walk_link(path, &nd);
416 error = listxattr(nd.dentry, list, size);
422 sys_flistxattr(int fd, char __user *list, size_t size)
425 ssize_t error = -EBADF;
430 error = listxattr(f->f_path.dentry, list, size);
436 * Extended attribute REMOVE operations
439 removexattr(struct dentry *d, char __user *name, struct vfsmount *mnt)
442 char kname[XATTR_NAME_MAX + 1];
444 error = strncpy_from_user(kname, name, sizeof(kname));
445 if (error == 0 || error == sizeof(kname))
450 if (MNT_IS_RDONLY(mnt))
453 return vfs_removexattr(d, kname);
457 sys_removexattr(char __user *path, char __user *name)
462 error = user_path_walk(path, &nd);
465 error = removexattr(nd.dentry, name, nd.mnt);
471 sys_lremovexattr(char __user *path, char __user *name)
476 error = user_path_walk_link(path, &nd);
479 error = removexattr(nd.dentry, name, nd.mnt);
485 sys_fremovexattr(int fd, char __user *name)
488 struct dentry *dentry;
494 dentry = f->f_path.dentry;
495 audit_inode(NULL, dentry->d_inode);
496 error = removexattr(dentry, name, f->f_vfsmnt);
503 strcmp_prefix(const char *a, const char *a_prefix)
505 while (*a_prefix && *a == *a_prefix) {
509 return *a_prefix ? NULL : a;
513 * In order to implement different sets of xattr operations for each xattr
514 * prefix with the generic xattr API, a filesystem should create a
515 * null-terminated array of struct xattr_handler (one for each prefix) and
516 * hang a pointer to it off of the s_xattr field of the superblock.
518 * The generic_fooxattr() functions will use this list to dispatch xattr
519 * operations to the correct xattr_handler.
521 #define for_each_xattr_handler(handlers, handler) \
522 for ((handler) = *(handlers)++; \
524 (handler) = *(handlers)++)
527 * Find the xattr_handler with the matching prefix.
529 static struct xattr_handler *
530 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
532 struct xattr_handler *handler;
537 for_each_xattr_handler(handlers, handler) {
538 const char *n = strcmp_prefix(*name, handler->prefix);
548 * Find the handler for the prefix and dispatch its get() operation.
551 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
553 struct xattr_handler *handler;
554 struct inode *inode = dentry->d_inode;
556 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
559 return handler->get(inode, name, buffer, size);
563 * Combine the results of the list() operation from every xattr_handler in the
567 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
569 struct inode *inode = dentry->d_inode;
570 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
571 unsigned int size = 0;
574 for_each_xattr_handler(handlers, handler)
575 size += handler->list(inode, NULL, 0, NULL, 0);
579 for_each_xattr_handler(handlers, handler) {
580 size = handler->list(inode, buf, buffer_size, NULL, 0);
581 if (size > buffer_size)
592 * Find the handler for the prefix and dispatch its set() operation.
595 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
597 struct xattr_handler *handler;
598 struct inode *inode = dentry->d_inode;
601 value = ""; /* empty EA, do not remove */
602 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
605 return handler->set(inode, name, value, size, flags);
609 * Find the handler for the prefix and dispatch its set() operation to remove
610 * any associated extended attribute.
613 generic_removexattr(struct dentry *dentry, const char *name)
615 struct xattr_handler *handler;
616 struct inode *inode = dentry->d_inode;
618 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
621 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
624 EXPORT_SYMBOL(generic_getxattr);
625 EXPORT_SYMBOL(generic_listxattr);
626 EXPORT_SYMBOL(generic_setxattr);
627 EXPORT_SYMBOL(generic_removexattr);