r8169: fix forced-mode link settings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / xattr.c
blob80eca7d3d69f68272a70b4a79bdc7c8f6d273ebf
1 /*
2 File: fs/xattr.c
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>
9 */
10 #include <linux/fs.h>
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.
27 static int
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) {
35 if (IS_RDONLY(inode))
36 return -EROFS;
37 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
38 return -EPERM;
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))
47 return 0;
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))
58 return -EPERM;
61 return permission(inode, mask, NULL);
64 int
65 vfs_setxattr(struct dentry *dentry, char *name, void *value,
66 size_t size, int flags)
68 struct inode *inode = dentry->d_inode;
69 int error;
71 error = xattr_permission(inode, name, MAY_WRITE);
72 if (error)
73 return error;
75 mutex_lock(&inode->i_mutex);
76 error = security_inode_setxattr(dentry, name, value, size, flags);
77 if (error)
78 goto out;
79 error = -EOPNOTSUPP;
80 if (inode->i_op->setxattr) {
81 error = inode->i_op->setxattr(dentry, name, value, size, flags);
82 if (!error) {
83 fsnotify_xattr(dentry);
84 security_inode_post_setxattr(dentry, name, value,
85 size, flags);
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,
91 size, flags);
92 if (!error)
93 fsnotify_xattr(dentry);
95 out:
96 mutex_unlock(&inode->i_mutex);
97 return error;
99 EXPORT_SYMBOL_GPL(vfs_setxattr);
101 ssize_t
102 vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
104 struct inode *inode = dentry->d_inode;
105 int error;
107 error = xattr_permission(inode, name, MAY_READ);
108 if (error)
109 return error;
111 error = security_inode_getxattr(dentry, name);
112 if (error)
113 return error;
115 if (inode->i_op->getxattr)
116 error = inode->i_op->getxattr(dentry, name, value, size);
117 else
118 error = -EOPNOTSUPP;
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,
124 size, error);
126 * Only overwrite the return value if a security module
127 * is actually active.
129 if (ret != -EOPNOTSUPP)
130 error = ret;
133 return error;
135 EXPORT_SYMBOL_GPL(vfs_getxattr);
138 vfs_removexattr(struct dentry *dentry, char *name)
140 struct inode *inode = dentry->d_inode;
141 int error;
143 if (!inode->i_op->removexattr)
144 return -EOPNOTSUPP;
146 error = xattr_permission(inode, name, MAY_WRITE);
147 if (error)
148 return error;
150 error = security_inode_removexattr(dentry, name);
151 if (error)
152 return error;
154 mutex_lock(&inode->i_mutex);
155 error = inode->i_op->removexattr(dentry, name);
156 mutex_unlock(&inode->i_mutex);
158 if (!error)
159 fsnotify_xattr(dentry);
160 return error;
162 EXPORT_SYMBOL_GPL(vfs_removexattr);
166 * Extended attribute SET operations
168 static long
169 setxattr(struct dentry *d, char __user *name, void __user *value,
170 size_t size, int flags)
172 int error;
173 void *kvalue = NULL;
174 char kname[XATTR_NAME_MAX + 1];
176 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
177 return -EINVAL;
179 error = strncpy_from_user(kname, name, sizeof(kname));
180 if (error == 0 || error == sizeof(kname))
181 error = -ERANGE;
182 if (error < 0)
183 return error;
185 if (size) {
186 if (size > XATTR_SIZE_MAX)
187 return -E2BIG;
188 kvalue = kmalloc(size, GFP_KERNEL);
189 if (!kvalue)
190 return -ENOMEM;
191 if (copy_from_user(kvalue, value, size)) {
192 kfree(kvalue);
193 return -EFAULT;
197 error = vfs_setxattr(d, kname, kvalue, size, flags);
198 kfree(kvalue);
199 return error;
202 asmlinkage long
203 sys_setxattr(char __user *path, char __user *name, void __user *value,
204 size_t size, int flags)
206 struct nameidata nd;
207 int error;
209 error = user_path_walk(path, &nd);
210 if (error)
211 return error;
212 error = setxattr(nd.dentry, name, value, size, flags);
213 path_release(&nd);
214 return error;
217 asmlinkage long
218 sys_lsetxattr(char __user *path, char __user *name, void __user *value,
219 size_t size, int flags)
221 struct nameidata nd;
222 int error;
224 error = user_path_walk_link(path, &nd);
225 if (error)
226 return error;
227 error = setxattr(nd.dentry, name, value, size, flags);
228 path_release(&nd);
229 return error;
232 asmlinkage long
233 sys_fsetxattr(int fd, char __user *name, void __user *value,
234 size_t size, int flags)
236 struct file *f;
237 int error = -EBADF;
239 f = fget(fd);
240 if (!f)
241 return error;
242 error = setxattr(f->f_dentry, name, value, size, flags);
243 fput(f);
244 return error;
248 * Extended attribute GET operations
250 static ssize_t
251 getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
253 ssize_t error;
254 void *kvalue = NULL;
255 char kname[XATTR_NAME_MAX + 1];
257 error = strncpy_from_user(kname, name, sizeof(kname));
258 if (error == 0 || error == sizeof(kname))
259 error = -ERANGE;
260 if (error < 0)
261 return error;
263 if (size) {
264 if (size > XATTR_SIZE_MAX)
265 size = XATTR_SIZE_MAX;
266 kvalue = kzalloc(size, GFP_KERNEL);
267 if (!kvalue)
268 return -ENOMEM;
271 error = vfs_getxattr(d, kname, kvalue, size);
272 if (error > 0) {
273 if (size && copy_to_user(value, kvalue, error))
274 error = -EFAULT;
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. */
278 error = -E2BIG;
280 kfree(kvalue);
281 return error;
284 asmlinkage ssize_t
285 sys_getxattr(char __user *path, char __user *name, void __user *value,
286 size_t size)
288 struct nameidata nd;
289 ssize_t error;
291 error = user_path_walk(path, &nd);
292 if (error)
293 return error;
294 error = getxattr(nd.dentry, name, value, size);
295 path_release(&nd);
296 return error;
299 asmlinkage ssize_t
300 sys_lgetxattr(char __user *path, char __user *name, void __user *value,
301 size_t size)
303 struct nameidata nd;
304 ssize_t error;
306 error = user_path_walk_link(path, &nd);
307 if (error)
308 return error;
309 error = getxattr(nd.dentry, name, value, size);
310 path_release(&nd);
311 return error;
314 asmlinkage ssize_t
315 sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
317 struct file *f;
318 ssize_t error = -EBADF;
320 f = fget(fd);
321 if (!f)
322 return error;
323 error = getxattr(f->f_dentry, name, value, size);
324 fput(f);
325 return error;
329 * Extended attribute LIST operations
331 static ssize_t
332 listxattr(struct dentry *d, char __user *list, size_t size)
334 ssize_t error;
335 char *klist = NULL;
337 if (size) {
338 if (size > XATTR_LIST_MAX)
339 size = XATTR_LIST_MAX;
340 klist = kmalloc(size, GFP_KERNEL);
341 if (!klist)
342 return -ENOMEM;
345 error = security_inode_listxattr(d);
346 if (error)
347 goto out;
348 error = -EOPNOTSUPP;
349 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
350 error = d->d_inode->i_op->listxattr(d, klist, size);
351 } else {
352 error = security_inode_listsecurity(d->d_inode, klist, size);
353 if (size && error > size)
354 error = -ERANGE;
356 if (error > 0) {
357 if (size && copy_to_user(list, klist, error))
358 error = -EFAULT;
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. */
362 error = -E2BIG;
364 out:
365 kfree(klist);
366 return error;
369 asmlinkage ssize_t
370 sys_listxattr(char __user *path, char __user *list, size_t size)
372 struct nameidata nd;
373 ssize_t error;
375 error = user_path_walk(path, &nd);
376 if (error)
377 return error;
378 error = listxattr(nd.dentry, list, size);
379 path_release(&nd);
380 return error;
383 asmlinkage ssize_t
384 sys_llistxattr(char __user *path, char __user *list, size_t size)
386 struct nameidata nd;
387 ssize_t error;
389 error = user_path_walk_link(path, &nd);
390 if (error)
391 return error;
392 error = listxattr(nd.dentry, list, size);
393 path_release(&nd);
394 return error;
397 asmlinkage ssize_t
398 sys_flistxattr(int fd, char __user *list, size_t size)
400 struct file *f;
401 ssize_t error = -EBADF;
403 f = fget(fd);
404 if (!f)
405 return error;
406 error = listxattr(f->f_dentry, list, size);
407 fput(f);
408 return error;
412 * Extended attribute REMOVE operations
414 static long
415 removexattr(struct dentry *d, char __user *name)
417 int error;
418 char kname[XATTR_NAME_MAX + 1];
420 error = strncpy_from_user(kname, name, sizeof(kname));
421 if (error == 0 || error == sizeof(kname))
422 error = -ERANGE;
423 if (error < 0)
424 return error;
426 return vfs_removexattr(d, kname);
429 asmlinkage long
430 sys_removexattr(char __user *path, char __user *name)
432 struct nameidata nd;
433 int error;
435 error = user_path_walk(path, &nd);
436 if (error)
437 return error;
438 error = removexattr(nd.dentry, name);
439 path_release(&nd);
440 return error;
443 asmlinkage long
444 sys_lremovexattr(char __user *path, char __user *name)
446 struct nameidata nd;
447 int error;
449 error = user_path_walk_link(path, &nd);
450 if (error)
451 return error;
452 error = removexattr(nd.dentry, name);
453 path_release(&nd);
454 return error;
457 asmlinkage long
458 sys_fremovexattr(int fd, char __user *name)
460 struct file *f;
461 int error = -EBADF;
463 f = fget(fd);
464 if (!f)
465 return error;
466 error = removexattr(f->f_dentry, name);
467 fput(f);
468 return error;
472 static const char *
473 strcmp_prefix(const char *a, const char *a_prefix)
475 while (*a_prefix && *a == *a_prefix) {
476 a++;
477 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)++; \
493 (handler) != NULL; \
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;
504 if (!*name)
505 return NULL;
507 for_each_xattr_handler(handlers, handler) {
508 const char *n = strcmp_prefix(*name, handler->prefix);
509 if (n) {
510 *name = n;
511 break;
514 return handler;
518 * Find the handler for the prefix and dispatch its get() operation.
520 ssize_t
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);
527 if (!handler)
528 return -EOPNOTSUPP;
529 return handler->get(inode, name, buffer, size);
533 * Combine the results of the list() operation from every xattr_handler in the
534 * list.
536 ssize_t
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;
543 if (!buffer) {
544 for_each_xattr_handler(handlers, handler)
545 size += handler->list(inode, NULL, 0, NULL, 0);
546 } else {
547 char *buf = buffer;
549 for_each_xattr_handler(handlers, handler) {
550 size = handler->list(inode, buf, buffer_size, NULL, 0);
551 if (size > buffer_size)
552 return -ERANGE;
553 buf += size;
554 buffer_size -= size;
556 size = buf - buffer;
558 return 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;
570 if (size == 0)
571 value = ""; /* empty EA, do not remove */
572 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
573 if (!handler)
574 return -EOPNOTSUPP;
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);
589 if (!handler)
590 return -EOPNOTSUPP;
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);