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 <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) {
38 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
43 * No restriction for security.* and system.* from the VFS. Decision
44 * on these is left to the underlying filesystem / security module.
46 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
47 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
51 * The trusted.* namespace can only accessed by a privilegued user.
53 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
54 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
56 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
57 if (!S_ISREG(inode->i_mode) &&
58 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
62 return permission(inode, mask, NULL);
66 vfs_setxattr(struct dentry *dentry, char *name, void *value,
67 size_t size, int flags)
69 struct inode *inode = dentry->d_inode;
72 error = xattr_permission(inode, name, MAY_WRITE);
76 mutex_lock(&inode->i_mutex);
77 error = security_inode_setxattr(dentry, name, value, size, flags);
81 if (inode->i_op->setxattr) {
82 error = inode->i_op->setxattr(dentry, name, value, size, flags);
84 fsnotify_xattr(dentry);
85 security_inode_post_setxattr(dentry, name, value,
88 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
89 XATTR_SECURITY_PREFIX_LEN)) {
90 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
91 error = security_inode_setsecurity(inode, suffix, value,
94 fsnotify_xattr(dentry);
97 mutex_unlock(&inode->i_mutex);
100 EXPORT_SYMBOL_GPL(vfs_setxattr);
103 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
105 struct inode *inode = dentry->d_inode;
108 error = xattr_permission(inode, name, MAY_READ);
112 error = security_inode_getxattr(dentry, name);
116 if (inode->i_op->getxattr)
117 error = inode->i_op->getxattr(dentry, name, value, size);
121 if (!strncmp(name, XATTR_SECURITY_PREFIX,
122 XATTR_SECURITY_PREFIX_LEN)) {
123 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
124 int ret = security_inode_getsecurity(inode, suffix, value,
127 * Only overwrite the return value if a security module
128 * is actually active.
130 if (ret != -EOPNOTSUPP)
136 EXPORT_SYMBOL_GPL(vfs_getxattr);
139 vfs_listxattr(struct dentry *d, char *list, size_t size)
143 error = security_inode_listxattr(d);
147 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
148 error = d->d_inode->i_op->listxattr(d, list, size);
150 error = security_inode_listsecurity(d->d_inode, list, size);
151 if (size && error > size)
156 EXPORT_SYMBOL_GPL(vfs_listxattr);
159 vfs_removexattr(struct dentry *dentry, char *name)
161 struct inode *inode = dentry->d_inode;
164 if (!inode->i_op->removexattr)
167 error = xattr_permission(inode, name, MAY_WRITE);
171 error = security_inode_removexattr(dentry, name);
175 mutex_lock(&inode->i_mutex);
176 error = inode->i_op->removexattr(dentry, name);
177 mutex_unlock(&inode->i_mutex);
180 fsnotify_xattr(dentry);
183 EXPORT_SYMBOL_GPL(vfs_removexattr);
187 * Extended attribute SET operations
190 setxattr(struct dentry *d, char __user *name, void __user *value,
191 size_t size, int flags)
195 char kname[XATTR_NAME_MAX + 1];
197 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
200 error = strncpy_from_user(kname, name, sizeof(kname));
201 if (error == 0 || error == sizeof(kname))
207 if (size > XATTR_SIZE_MAX)
209 kvalue = kmalloc(size, GFP_KERNEL);
212 if (copy_from_user(kvalue, value, size)) {
218 error = vfs_setxattr(d, kname, kvalue, size, flags);
224 sys_setxattr(char __user *path, char __user *name, void __user *value,
225 size_t size, int flags)
230 error = user_path_walk(path, &nd);
233 error = setxattr(nd.dentry, name, value, size, flags);
239 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
240 size_t size, int flags)
245 error = user_path_walk_link(path, &nd);
248 error = setxattr(nd.dentry, name, value, size, flags);
254 sys_fsetxattr(int fd, char __user *name, void __user *value,
255 size_t size, int flags)
258 struct dentry *dentry;
264 dentry = f->f_dentry;
265 audit_inode(NULL, dentry->d_inode);
266 error = setxattr(dentry, name, value, size, flags);
272 * Extended attribute GET operations
275 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
279 char kname[XATTR_NAME_MAX + 1];
281 error = strncpy_from_user(kname, name, sizeof(kname));
282 if (error == 0 || error == sizeof(kname))
288 if (size > XATTR_SIZE_MAX)
289 size = XATTR_SIZE_MAX;
290 kvalue = kzalloc(size, GFP_KERNEL);
295 error = vfs_getxattr(d, kname, kvalue, size);
297 if (size && copy_to_user(value, kvalue, error))
299 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
300 /* The file system tried to returned a value bigger
301 than XATTR_SIZE_MAX bytes. Not possible. */
309 sys_getxattr(char __user *path, char __user *name, void __user *value,
315 error = user_path_walk(path, &nd);
318 error = getxattr(nd.dentry, name, value, size);
324 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
330 error = user_path_walk_link(path, &nd);
333 error = getxattr(nd.dentry, name, value, size);
339 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
342 ssize_t error = -EBADF;
347 error = getxattr(f->f_dentry, name, value, size);
353 * Extended attribute LIST operations
356 listxattr(struct dentry *d, char __user *list, size_t size)
362 if (size > XATTR_LIST_MAX)
363 size = XATTR_LIST_MAX;
364 klist = kmalloc(size, GFP_KERNEL);
369 error = vfs_listxattr(d, klist, size);
371 if (size && copy_to_user(list, klist, error))
373 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
374 /* The file system tried to returned a list bigger
375 than XATTR_LIST_MAX bytes. Not possible. */
383 sys_listxattr(char __user *path, char __user *list, size_t size)
388 error = user_path_walk(path, &nd);
391 error = listxattr(nd.dentry, list, size);
397 sys_llistxattr(char __user *path, char __user *list, size_t size)
402 error = user_path_walk_link(path, &nd);
405 error = listxattr(nd.dentry, list, size);
411 sys_flistxattr(int fd, char __user *list, size_t size)
414 ssize_t error = -EBADF;
419 error = listxattr(f->f_dentry, list, size);
425 * Extended attribute REMOVE operations
428 removexattr(struct dentry *d, char __user *name)
431 char kname[XATTR_NAME_MAX + 1];
433 error = strncpy_from_user(kname, name, sizeof(kname));
434 if (error == 0 || error == sizeof(kname))
439 return vfs_removexattr(d, kname);
443 sys_removexattr(char __user *path, char __user *name)
448 error = user_path_walk(path, &nd);
451 error = removexattr(nd.dentry, name);
457 sys_lremovexattr(char __user *path, char __user *name)
462 error = user_path_walk_link(path, &nd);
465 error = removexattr(nd.dentry, name);
471 sys_fremovexattr(int fd, char __user *name)
474 struct dentry *dentry;
480 dentry = f->f_dentry;
481 audit_inode(NULL, dentry->d_inode);
482 error = removexattr(dentry, name);
489 strcmp_prefix(const char *a, const char *a_prefix)
491 while (*a_prefix && *a == *a_prefix) {
495 return *a_prefix ? NULL : a;
499 * In order to implement different sets of xattr operations for each xattr
500 * prefix with the generic xattr API, a filesystem should create a
501 * null-terminated array of struct xattr_handler (one for each prefix) and
502 * hang a pointer to it off of the s_xattr field of the superblock.
504 * The generic_fooxattr() functions will use this list to dispatch xattr
505 * operations to the correct xattr_handler.
507 #define for_each_xattr_handler(handlers, handler) \
508 for ((handler) = *(handlers)++; \
510 (handler) = *(handlers)++)
513 * Find the xattr_handler with the matching prefix.
515 static struct xattr_handler *
516 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
518 struct xattr_handler *handler;
523 for_each_xattr_handler(handlers, handler) {
524 const char *n = strcmp_prefix(*name, handler->prefix);
534 * Find the handler for the prefix and dispatch its get() operation.
537 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
539 struct xattr_handler *handler;
540 struct inode *inode = dentry->d_inode;
542 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
545 return handler->get(inode, name, buffer, size);
549 * Combine the results of the list() operation from every xattr_handler in the
553 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
555 struct inode *inode = dentry->d_inode;
556 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
557 unsigned int size = 0;
560 for_each_xattr_handler(handlers, handler)
561 size += handler->list(inode, NULL, 0, NULL, 0);
565 for_each_xattr_handler(handlers, handler) {
566 size = handler->list(inode, buf, buffer_size, NULL, 0);
567 if (size > buffer_size)
578 * Find the handler for the prefix and dispatch its set() operation.
581 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
583 struct xattr_handler *handler;
584 struct inode *inode = dentry->d_inode;
587 value = ""; /* empty EA, do not remove */
588 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
591 return handler->set(inode, name, value, size, flags);
595 * Find the handler for the prefix and dispatch its set() operation to remove
596 * any associated extended attribute.
599 generic_removexattr(struct dentry *dentry, const char *name)
601 struct xattr_handler *handler;
602 struct inode *inode = dentry->d_inode;
604 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
607 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
610 EXPORT_SYMBOL(generic_getxattr);
611 EXPORT_SYMBOL(generic_listxattr);
612 EXPORT_SYMBOL(generic_setxattr);
613 EXPORT_SYMBOL(generic_removexattr);