blob: 80eca7d3d69f68272a70b4a79bdc7c8f6d273ebf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 File: fs/xattr.c
3
4 Extended attribute handling.
5
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>
Robert Love0eeca282005-07-12 17:06:03 -040019#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21
Christoph Hellwig5be196e2006-01-09 20:51:55 -080022
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080023/*
24 * Check permissions for extended attribute access. This is a bit complicated
25 * because different namespaces have very different rules.
26 */
27static int
28xattr_permission(struct inode *inode, const char *name, int mask)
29{
30 /*
31 * We can never set or remove an extended attribute on a read-only
32 * filesystem or on an immutable / append-only inode.
33 */
34 if (mask & MAY_WRITE) {
35 if (IS_RDONLY(inode))
36 return -EROFS;
37 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
38 return -EPERM;
39 }
40
41 /*
42 * No restriction for security.* and system.* from the VFS. Decision
43 * on these is left to the underlying filesystem / security module.
44 */
45 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
46 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
47 return 0;
48
49 /*
50 * The trusted.* namespace can only accessed by a privilegued user.
51 */
52 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
53 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
54
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;
59 }
60
61 return permission(inode, mask, NULL);
62}
63
Christoph Hellwig5be196e2006-01-09 20:51:55 -080064int
65vfs_setxattr(struct dentry *dentry, char *name, void *value,
66 size_t size, int flags)
67{
68 struct inode *inode = dentry->d_inode;
69 int error;
70
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080071 error = xattr_permission(inode, name, MAY_WRITE);
72 if (error)
73 return error;
74
Christoph Hellwig5be196e2006-01-09 20:51:55 -080075 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);
86 }
87 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080088 XATTR_SECURITY_PREFIX_LEN)) {
89 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -080090 error = security_inode_setsecurity(inode, suffix, value,
91 size, flags);
92 if (!error)
93 fsnotify_xattr(dentry);
94 }
95out:
96 mutex_unlock(&inode->i_mutex);
97 return error;
98}
99EXPORT_SYMBOL_GPL(vfs_setxattr);
100
101ssize_t
102vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
103{
104 struct inode *inode = dentry->d_inode;
105 int error;
106
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800107 error = xattr_permission(inode, name, MAY_READ);
108 if (error)
109 return error;
110
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800111 error = security_inode_getxattr(dentry, name);
112 if (error)
113 return error;
114
115 if (inode->i_op->getxattr)
116 error = inode->i_op->getxattr(dentry, name, value, size);
117 else
118 error = -EOPNOTSUPP;
119
120 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800121 XATTR_SECURITY_PREFIX_LEN)) {
122 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800123 int ret = security_inode_getsecurity(inode, suffix, value,
124 size, error);
125 /*
126 * Only overwrite the return value if a security module
127 * is actually active.
128 */
129 if (ret != -EOPNOTSUPP)
130 error = ret;
131 }
132
133 return error;
134}
135EXPORT_SYMBOL_GPL(vfs_getxattr);
136
137int
138vfs_removexattr(struct dentry *dentry, char *name)
139{
140 struct inode *inode = dentry->d_inode;
141 int error;
142
143 if (!inode->i_op->removexattr)
144 return -EOPNOTSUPP;
145
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800146 error = xattr_permission(inode, name, MAY_WRITE);
147 if (error)
148 return error;
149
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800150 error = security_inode_removexattr(dentry, name);
151 if (error)
152 return error;
153
154 mutex_lock(&inode->i_mutex);
155 error = inode->i_op->removexattr(dentry, name);
156 mutex_unlock(&inode->i_mutex);
157
158 if (!error)
159 fsnotify_xattr(dentry);
160 return error;
161}
162EXPORT_SYMBOL_GPL(vfs_removexattr);
163
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * Extended attribute SET operations
167 */
168static long
169setxattr(struct dentry *d, char __user *name, void __user *value,
170 size_t size, int flags)
171{
172 int error;
173 void *kvalue = NULL;
174 char kname[XATTR_NAME_MAX + 1];
175
176 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
177 return -EINVAL;
178
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;
184
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;
194 }
195 }
196
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800197 error = vfs_setxattr(d, kname, kvalue, size, flags);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800198 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return error;
200}
201
202asmlinkage long
203sys_setxattr(char __user *path, char __user *name, void __user *value,
204 size_t size, int flags)
205{
206 struct nameidata nd;
207 int error;
208
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;
215}
216
217asmlinkage long
218sys_lsetxattr(char __user *path, char __user *name, void __user *value,
219 size_t size, int flags)
220{
221 struct nameidata nd;
222 int error;
223
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;
230}
231
232asmlinkage long
233sys_fsetxattr(int fd, char __user *name, void __user *value,
234 size_t size, int flags)
235{
236 struct file *f;
237 int error = -EBADF;
238
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;
245}
246
247/*
248 * Extended attribute GET operations
249 */
250static ssize_t
251getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
252{
253 ssize_t error;
254 void *kvalue = NULL;
255 char kname[XATTR_NAME_MAX + 1];
256
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;
262
263 if (size) {
264 if (size > XATTR_SIZE_MAX)
265 size = XATTR_SIZE_MAX;
James Morrisd381d8a2005-10-30 14:59:22 -0800266 kvalue = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 if (!kvalue)
268 return -ENOMEM;
269 }
270
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800271 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700272 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800280 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return error;
282}
283
284asmlinkage ssize_t
285sys_getxattr(char __user *path, char __user *name, void __user *value,
286 size_t size)
287{
288 struct nameidata nd;
289 ssize_t error;
290
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;
297}
298
299asmlinkage ssize_t
300sys_lgetxattr(char __user *path, char __user *name, void __user *value,
301 size_t size)
302{
303 struct nameidata nd;
304 ssize_t error;
305
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;
312}
313
314asmlinkage ssize_t
315sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
316{
317 struct file *f;
318 ssize_t error = -EBADF;
319
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;
326}
327
328/*
329 * Extended attribute LIST operations
330 */
331static ssize_t
332listxattr(struct dentry *d, char __user *list, size_t size)
333{
334 ssize_t error;
335 char *klist = NULL;
336
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;
343 }
344
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700345 error = security_inode_listxattr(d);
346 if (error)
347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 error = -EOPNOTSUPP;
349 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 error = d->d_inode->i_op->listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700351 } else {
352 error = security_inode_listsecurity(d->d_inode, klist, size);
Daniel Drake894ec872005-12-12 00:37:08 -0800353 if (size && error > size)
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700354 error = -ERANGE;
355 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364out:
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800365 kfree(klist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return error;
367}
368
369asmlinkage ssize_t
370sys_listxattr(char __user *path, char __user *list, size_t size)
371{
372 struct nameidata nd;
373 ssize_t error;
374
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;
381}
382
383asmlinkage ssize_t
384sys_llistxattr(char __user *path, char __user *list, size_t size)
385{
386 struct nameidata nd;
387 ssize_t error;
388
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;
395}
396
397asmlinkage ssize_t
398sys_flistxattr(int fd, char __user *list, size_t size)
399{
400 struct file *f;
401 ssize_t error = -EBADF;
402
403 f = fget(fd);
404 if (!f)
405 return error;
406 error = listxattr(f->f_dentry, list, size);
407 fput(f);
408 return error;
409}
410
411/*
412 * Extended attribute REMOVE operations
413 */
414static long
415removexattr(struct dentry *d, char __user *name)
416{
417 int error;
418 char kname[XATTR_NAME_MAX + 1];
419
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;
425
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800426 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429asmlinkage long
430sys_removexattr(char __user *path, char __user *name)
431{
432 struct nameidata nd;
433 int error;
434
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;
441}
442
443asmlinkage long
444sys_lremovexattr(char __user *path, char __user *name)
445{
446 struct nameidata nd;
447 int error;
448
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;
455}
456
457asmlinkage long
458sys_fremovexattr(int fd, char __user *name)
459{
460 struct file *f;
461 int error = -EBADF;
462
463 f = fget(fd);
464 if (!f)
465 return error;
466 error = removexattr(f->f_dentry, name);
467 fput(f);
468 return error;
469}
470
471
472static const char *
473strcmp_prefix(const char *a, const char *a_prefix)
474{
475 while (*a_prefix && *a == *a_prefix) {
476 a++;
477 a_prefix++;
478 }
479 return *a_prefix ? NULL : a;
480}
481
482/*
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.
487 *
488 * The generic_fooxattr() functions will use this list to dispatch xattr
489 * operations to the correct xattr_handler.
490 */
491#define for_each_xattr_handler(handlers, handler) \
492 for ((handler) = *(handlers)++; \
493 (handler) != NULL; \
494 (handler) = *(handlers)++)
495
496/*
497 * Find the xattr_handler with the matching prefix.
498 */
499static struct xattr_handler *
500xattr_resolve_name(struct xattr_handler **handlers, const char **name)
501{
502 struct xattr_handler *handler;
503
504 if (!*name)
505 return NULL;
506
507 for_each_xattr_handler(handlers, handler) {
508 const char *n = strcmp_prefix(*name, handler->prefix);
509 if (n) {
510 *name = n;
511 break;
512 }
513 }
514 return handler;
515}
516
517/*
518 * Find the handler for the prefix and dispatch its get() operation.
519 */
520ssize_t
521generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
522{
523 struct xattr_handler *handler;
524 struct inode *inode = dentry->d_inode;
525
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);
530}
531
532/*
533 * Combine the results of the list() operation from every xattr_handler in the
534 * list.
535 */
536ssize_t
537generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
538{
539 struct inode *inode = dentry->d_inode;
540 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
541 unsigned int size = 0;
542
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;
548
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;
555 }
556 size = buf - buffer;
557 }
558 return size;
559}
560
561/*
562 * Find the handler for the prefix and dispatch its set() operation.
563 */
564int
565generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
566{
567 struct xattr_handler *handler;
568 struct inode *inode = dentry->d_inode;
569
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);
576}
577
578/*
579 * Find the handler for the prefix and dispatch its set() operation to remove
580 * any associated extended attribute.
581 */
582int
583generic_removexattr(struct dentry *dentry, const char *name)
584{
585 struct xattr_handler *handler;
586 struct inode *inode = dentry->d_inode;
587
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);
592}
593
594EXPORT_SYMBOL(generic_getxattr);
595EXPORT_SYMBOL(generic_listxattr);
596EXPORT_SYMBOL(generic_setxattr);
597EXPORT_SYMBOL(generic_removexattr);