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) && (current->fsuid != inode->i_uid) &&
68 return permission(inode, mask, NULL);
72 vfs_setxattr(struct dentry *dentry, char *name, void *value,
73 size_t size, int flags)
75 struct inode *inode = dentry->d_inode;
78 error = xattr_permission(inode, name, MAY_WRITE);
82 mutex_lock(&inode->i_mutex);
83 error = security_inode_setxattr(dentry, name, value, size, flags);
87 if (inode->i_op->setxattr) {
88 error = inode->i_op->setxattr(dentry, name, value, size, flags);
90 fsnotify_xattr(dentry);
91 security_inode_post_setxattr(dentry, name, value,
94 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
95 XATTR_SECURITY_PREFIX_LEN)) {
96 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
97 error = security_inode_setsecurity(inode, suffix, value,
100 fsnotify_xattr(dentry);
103 mutex_unlock(&inode->i_mutex);
106 EXPORT_SYMBOL_GPL(vfs_setxattr);
109 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
111 struct inode *inode = dentry->d_inode;
114 error = xattr_permission(inode, name, MAY_READ);
118 error = security_inode_getxattr(dentry, name);
122 if (inode->i_op->getxattr)
123 error = inode->i_op->getxattr(dentry, name, value, size);
127 if (!strncmp(name, XATTR_SECURITY_PREFIX,
128 XATTR_SECURITY_PREFIX_LEN)) {
129 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
130 int ret = security_inode_getsecurity(inode, suffix, value,
133 * Only overwrite the return value if a security module
134 * is actually active.
136 if (ret != -EOPNOTSUPP)
142 EXPORT_SYMBOL_GPL(vfs_getxattr);
145 vfs_listxattr(struct dentry *d, char *list, size_t size)
149 error = security_inode_listxattr(d);
153 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
154 error = d->d_inode->i_op->listxattr(d, list, size);
156 error = security_inode_listsecurity(d->d_inode, list, size);
157 if (size && error > size)
162 EXPORT_SYMBOL_GPL(vfs_listxattr);
165 vfs_removexattr(struct dentry *dentry, char *name)
167 struct inode *inode = dentry->d_inode;
170 if (!inode->i_op->removexattr)
173 error = xattr_permission(inode, name, MAY_WRITE);
177 error = security_inode_removexattr(dentry, name);
181 mutex_lock(&inode->i_mutex);
182 error = inode->i_op->removexattr(dentry, name);
183 mutex_unlock(&inode->i_mutex);
186 fsnotify_xattr(dentry);
189 EXPORT_SYMBOL_GPL(vfs_removexattr);
193 * Extended attribute SET operations
196 setxattr(struct dentry *d, char __user *name, void __user *value,
197 size_t size, int flags)
201 char kname[XATTR_NAME_MAX + 1];
203 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
206 error = strncpy_from_user(kname, name, sizeof(kname));
207 if (error == 0 || error == sizeof(kname))
213 if (size > XATTR_SIZE_MAX)
215 kvalue = kmalloc(size, GFP_KERNEL);
218 if (copy_from_user(kvalue, value, size)) {
224 error = vfs_setxattr(d, kname, kvalue, size, flags);
230 sys_setxattr(char __user *path, char __user *name, void __user *value,
231 size_t size, int flags)
236 error = user_path_walk(path, &nd);
239 error = setxattr(nd.dentry, name, value, size, flags);
245 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
246 size_t size, int flags)
251 error = user_path_walk_link(path, &nd);
254 error = setxattr(nd.dentry, name, value, size, flags);
260 sys_fsetxattr(int fd, char __user *name, void __user *value,
261 size_t size, int flags)
264 struct dentry *dentry;
270 dentry = f->f_path.dentry;
271 audit_inode(NULL, dentry->d_inode);
272 error = setxattr(dentry, name, value, size, flags);
278 * Extended attribute GET operations
281 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
285 char kname[XATTR_NAME_MAX + 1];
287 error = strncpy_from_user(kname, name, sizeof(kname));
288 if (error == 0 || error == sizeof(kname))
294 if (size > XATTR_SIZE_MAX)
295 size = XATTR_SIZE_MAX;
296 kvalue = kzalloc(size, GFP_KERNEL);
301 error = vfs_getxattr(d, kname, kvalue, size);
303 if (size && copy_to_user(value, kvalue, error))
305 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
306 /* The file system tried to returned a value bigger
307 than XATTR_SIZE_MAX bytes. Not possible. */
315 sys_getxattr(char __user *path, char __user *name, void __user *value,
321 error = user_path_walk(path, &nd);
324 error = getxattr(nd.dentry, name, value, size);
330 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
336 error = user_path_walk_link(path, &nd);
339 error = getxattr(nd.dentry, name, value, size);
345 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
348 ssize_t error = -EBADF;
353 audit_inode(NULL, f->f_path.dentry->d_inode);
354 error = getxattr(f->f_path.dentry, name, value, size);
360 * Extended attribute LIST operations
363 listxattr(struct dentry *d, char __user *list, size_t size)
369 if (size > XATTR_LIST_MAX)
370 size = XATTR_LIST_MAX;
371 klist = kmalloc(size, GFP_KERNEL);
376 error = vfs_listxattr(d, klist, size);
378 if (size && copy_to_user(list, klist, error))
380 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
381 /* The file system tried to returned a list bigger
382 than XATTR_LIST_MAX bytes. Not possible. */
390 sys_listxattr(char __user *path, char __user *list, size_t size)
395 error = user_path_walk(path, &nd);
398 error = listxattr(nd.dentry, list, size);
404 sys_llistxattr(char __user *path, char __user *list, size_t size)
409 error = user_path_walk_link(path, &nd);
412 error = listxattr(nd.dentry, list, size);
418 sys_flistxattr(int fd, char __user *list, size_t size)
421 ssize_t error = -EBADF;
426 audit_inode(NULL, f->f_path.dentry->d_inode);
427 error = listxattr(f->f_path.dentry, list, size);
433 * Extended attribute REMOVE operations
436 removexattr(struct dentry *d, char __user *name)
439 char kname[XATTR_NAME_MAX + 1];
441 error = strncpy_from_user(kname, name, sizeof(kname));
442 if (error == 0 || error == sizeof(kname))
447 return vfs_removexattr(d, kname);
451 sys_removexattr(char __user *path, char __user *name)
456 error = user_path_walk(path, &nd);
459 error = removexattr(nd.dentry, name);
465 sys_lremovexattr(char __user *path, char __user *name)
470 error = user_path_walk_link(path, &nd);
473 error = removexattr(nd.dentry, name);
479 sys_fremovexattr(int fd, char __user *name)
482 struct dentry *dentry;
488 dentry = f->f_path.dentry;
489 audit_inode(NULL, dentry->d_inode);
490 error = removexattr(dentry, name);
497 strcmp_prefix(const char *a, const char *a_prefix)
499 while (*a_prefix && *a == *a_prefix) {
503 return *a_prefix ? NULL : a;
507 * In order to implement different sets of xattr operations for each xattr
508 * prefix with the generic xattr API, a filesystem should create a
509 * null-terminated array of struct xattr_handler (one for each prefix) and
510 * hang a pointer to it off of the s_xattr field of the superblock.
512 * The generic_fooxattr() functions will use this list to dispatch xattr
513 * operations to the correct xattr_handler.
515 #define for_each_xattr_handler(handlers, handler) \
516 for ((handler) = *(handlers)++; \
518 (handler) = *(handlers)++)
521 * Find the xattr_handler with the matching prefix.
523 static struct xattr_handler *
524 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
526 struct xattr_handler *handler;
531 for_each_xattr_handler(handlers, handler) {
532 const char *n = strcmp_prefix(*name, handler->prefix);
542 * Find the handler for the prefix and dispatch its get() operation.
545 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
547 struct xattr_handler *handler;
548 struct inode *inode = dentry->d_inode;
550 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
553 return handler->get(inode, name, buffer, size);
557 * Combine the results of the list() operation from every xattr_handler in the
561 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
563 struct inode *inode = dentry->d_inode;
564 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
565 unsigned int size = 0;
568 for_each_xattr_handler(handlers, handler)
569 size += handler->list(inode, NULL, 0, NULL, 0);
573 for_each_xattr_handler(handlers, handler) {
574 size = handler->list(inode, buf, buffer_size, NULL, 0);
575 if (size > buffer_size)
586 * Find the handler for the prefix and dispatch its set() operation.
589 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
591 struct xattr_handler *handler;
592 struct inode *inode = dentry->d_inode;
595 value = ""; /* empty EA, do not remove */
596 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
599 return handler->set(inode, name, value, size, flags);
603 * Find the handler for the prefix and dispatch its set() operation to remove
604 * any associated extended attribute.
607 generic_removexattr(struct dentry *dentry, const char *name)
609 struct xattr_handler *handler;
610 struct inode *inode = dentry->d_inode;
612 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
615 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
618 EXPORT_SYMBOL(generic_getxattr);
619 EXPORT_SYMBOL(generic_listxattr);
620 EXPORT_SYMBOL(generic_setxattr);
621 EXPORT_SYMBOL(generic_removexattr);