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 <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 accessed by a privilegued user.
52 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
53 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
55 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
56 if (!S_ISREG(inode->i_mode) &&
57 (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX))
61 return permission(inode, mask, NULL);
65 vfs_setxattr(struct dentry *dentry, char *name, void *value,
66 size_t size, int flags)
68 struct inode *inode = dentry->d_inode;
71 error = xattr_permission(inode, name, MAY_WRITE);
75 mutex_lock(&inode->i_mutex);
76 error = security_inode_setxattr(dentry, name, value, size, flags);
80 if (inode->i_op->setxattr) {
81 error = inode->i_op->setxattr(dentry, name, value, size, flags);
83 fsnotify_xattr(dentry);
84 security_inode_post_setxattr(dentry, name, value,
87 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
88 XATTR_SECURITY_PREFIX_LEN)) {
89 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
90 error = security_inode_setsecurity(inode, suffix, value,
93 fsnotify_xattr(dentry);
96 mutex_unlock(&inode->i_mutex);
99 EXPORT_SYMBOL_GPL(vfs_setxattr);
102 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
104 struct inode *inode = dentry->d_inode;
107 error = xattr_permission(inode, name, MAY_READ);
111 error = security_inode_getxattr(dentry, name);
115 if (inode->i_op->getxattr)
116 error = inode->i_op->getxattr(dentry, name, value, size);
120 if (!strncmp(name, XATTR_SECURITY_PREFIX,
121 XATTR_SECURITY_PREFIX_LEN)) {
122 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
123 int ret = security_inode_getsecurity(inode, suffix, value,
126 * Only overwrite the return value if a security module
127 * is actually active.
129 if (ret != -EOPNOTSUPP)
135 EXPORT_SYMBOL_GPL(vfs_getxattr);
138 vfs_removexattr(struct dentry *dentry, char *name)
140 struct inode *inode = dentry->d_inode;
143 if (!inode->i_op->removexattr)
146 error = xattr_permission(inode, name, MAY_WRITE);
150 error = security_inode_removexattr(dentry, name);
154 mutex_lock(&inode->i_mutex);
155 error = inode->i_op->removexattr(dentry, name);
156 mutex_unlock(&inode->i_mutex);
159 fsnotify_xattr(dentry);
162 EXPORT_SYMBOL_GPL(vfs_removexattr);
166 * Extended attribute SET operations
169 setxattr(struct dentry *d, char __user *name, void __user *value,
170 size_t size, int flags)
174 char kname[XATTR_NAME_MAX + 1];
176 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
179 error = strncpy_from_user(kname, name, sizeof(kname));
180 if (error == 0 || error == sizeof(kname))
186 if (size > XATTR_SIZE_MAX)
188 kvalue = kmalloc(size, GFP_KERNEL);
191 if (copy_from_user(kvalue, value, size)) {
197 error = vfs_setxattr(d, kname, kvalue, size, flags);
203 sys_setxattr(char __user *path, char __user *name, void __user *value,
204 size_t size, int flags)
209 error = user_path_walk(path, &nd);
212 error = setxattr(nd.dentry, name, value, size, flags);
218 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
219 size_t size, int flags)
224 error = user_path_walk_link(path, &nd);
227 error = setxattr(nd.dentry, name, value, size, flags);
233 sys_fsetxattr(int fd, char __user *name, void __user *value,
234 size_t size, int flags)
242 error = setxattr(f->f_dentry, name, value, size, flags);
248 * Extended attribute GET operations
251 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
255 char kname[XATTR_NAME_MAX + 1];
257 error = strncpy_from_user(kname, name, sizeof(kname));
258 if (error == 0 || error == sizeof(kname))
264 if (size > XATTR_SIZE_MAX)
265 size = XATTR_SIZE_MAX;
266 kvalue = kzalloc(size, GFP_KERNEL);
271 error = vfs_getxattr(d, kname, kvalue, size);
273 if (size && copy_to_user(value, kvalue, error))
275 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
276 /* The file system tried to returned a value bigger
277 than XATTR_SIZE_MAX bytes. Not possible. */
285 sys_getxattr(char __user *path, char __user *name, void __user *value,
291 error = user_path_walk(path, &nd);
294 error = getxattr(nd.dentry, name, value, size);
300 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
306 error = user_path_walk_link(path, &nd);
309 error = getxattr(nd.dentry, name, value, size);
315 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
318 ssize_t error = -EBADF;
323 error = getxattr(f->f_dentry, name, value, size);
329 * Extended attribute LIST operations
332 listxattr(struct dentry *d, char __user *list, size_t size)
338 if (size > XATTR_LIST_MAX)
339 size = XATTR_LIST_MAX;
340 klist = kmalloc(size, GFP_KERNEL);
345 error = security_inode_listxattr(d);
349 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
350 error = d->d_inode->i_op->listxattr(d, klist, size);
352 error = security_inode_listsecurity(d->d_inode, klist, size);
353 if (size && error > size)
357 if (size && copy_to_user(list, klist, error))
359 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
360 /* The file system tried to returned a list bigger
361 than XATTR_LIST_MAX bytes. Not possible. */
370 sys_listxattr(char __user *path, char __user *list, size_t size)
375 error = user_path_walk(path, &nd);
378 error = listxattr(nd.dentry, list, size);
384 sys_llistxattr(char __user *path, char __user *list, size_t size)
389 error = user_path_walk_link(path, &nd);
392 error = listxattr(nd.dentry, list, size);
398 sys_flistxattr(int fd, char __user *list, size_t size)
401 ssize_t error = -EBADF;
406 error = listxattr(f->f_dentry, list, size);
412 * Extended attribute REMOVE operations
415 removexattr(struct dentry *d, char __user *name)
418 char kname[XATTR_NAME_MAX + 1];
420 error = strncpy_from_user(kname, name, sizeof(kname));
421 if (error == 0 || error == sizeof(kname))
426 return vfs_removexattr(d, kname);
430 sys_removexattr(char __user *path, char __user *name)
435 error = user_path_walk(path, &nd);
438 error = removexattr(nd.dentry, name);
444 sys_lremovexattr(char __user *path, char __user *name)
449 error = user_path_walk_link(path, &nd);
452 error = removexattr(nd.dentry, name);
458 sys_fremovexattr(int fd, char __user *name)
466 error = removexattr(f->f_dentry, name);
473 strcmp_prefix(const char *a, const char *a_prefix)
475 while (*a_prefix && *a == *a_prefix) {
479 return *a_prefix ? NULL : a;
483 * In order to implement different sets of xattr operations for each xattr
484 * prefix with the generic xattr API, a filesystem should create a
485 * null-terminated array of struct xattr_handler (one for each prefix) and
486 * hang a pointer to it off of the s_xattr field of the superblock.
488 * The generic_fooxattr() functions will use this list to dispatch xattr
489 * operations to the correct xattr_handler.
491 #define for_each_xattr_handler(handlers, handler) \
492 for ((handler) = *(handlers)++; \
494 (handler) = *(handlers)++)
497 * Find the xattr_handler with the matching prefix.
499 static struct xattr_handler *
500 xattr_resolve_name(struct xattr_handler **handlers, const char **name)
502 struct xattr_handler *handler;
507 for_each_xattr_handler(handlers, handler) {
508 const char *n = strcmp_prefix(*name, handler->prefix);
518 * Find the handler for the prefix and dispatch its get() operation.
521 generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
523 struct xattr_handler *handler;
524 struct inode *inode = dentry->d_inode;
526 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
529 return handler->get(inode, name, buffer, size);
533 * Combine the results of the list() operation from every xattr_handler in the
537 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
539 struct inode *inode = dentry->d_inode;
540 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
541 unsigned int size = 0;
544 for_each_xattr_handler(handlers, handler)
545 size += handler->list(inode, NULL, 0, NULL, 0);
549 for_each_xattr_handler(handlers, handler) {
550 size = handler->list(inode, buf, buffer_size, NULL, 0);
551 if (size > buffer_size)
562 * Find the handler for the prefix and dispatch its set() operation.
565 generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
567 struct xattr_handler *handler;
568 struct inode *inode = dentry->d_inode;
571 value = ""; /* empty EA, do not remove */
572 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
575 return handler->set(inode, name, value, size, flags);
579 * Find the handler for the prefix and dispatch its set() operation to remove
580 * any associated extended attribute.
583 generic_removexattr(struct dentry *dentry, const char *name)
585 struct xattr_handler *handler;
586 struct inode *inode = dentry->d_inode;
588 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
591 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
594 EXPORT_SYMBOL(generic_getxattr);
595 EXPORT_SYMBOL(generic_listxattr);
596 EXPORT_SYMBOL(generic_setxattr);
597 EXPORT_SYMBOL(generic_removexattr);