blob: 38646132ab0e29ec4c8ab8a4d3c7bbb491eaeaeb [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>
Amy Griffis73241cc2005-11-03 16:00:25 +000020#include <linux/audit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22
Christoph Hellwig5be196e2006-01-09 20:51:55 -080023
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080024/*
25 * Check permissions for extended attribute access. This is a bit complicated
26 * because different namespaces have very different rules.
27 */
28static int
29xattr_permission(struct inode *inode, const char *name, int mask)
30{
31 /*
32 * We can never set or remove an extended attribute on a read-only
33 * filesystem or on an immutable / append-only inode.
34 */
35 if (mask & MAY_WRITE) {
36 if (IS_RDONLY(inode))
37 return -EROFS;
38 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
39 return -EPERM;
40 }
41
42 /*
43 * No restriction for security.* and system.* from the VFS. Decision
44 * on these is left to the underlying filesystem / security module.
45 */
46 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
47 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
48 return 0;
49
50 /*
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080051 * The trusted.* namespace can only be accessed by a privileged user.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080052 */
53 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
54 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
55
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080056 /* In user.* namespace, only regular files and directories can have
57 * extended attributes. For sticky directories, only the owner and
58 * privileged user can write attributes.
59 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080060 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -080061 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
62 return -EPERM;
63 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
64 (mask & MAY_WRITE) && (current->fsuid != inode->i_uid) &&
65 !capable(CAP_FOWNER))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080066 return -EPERM;
67 }
68
69 return permission(inode, mask, NULL);
70}
71
Christoph Hellwig5be196e2006-01-09 20:51:55 -080072int
73vfs_setxattr(struct dentry *dentry, char *name, void *value,
74 size_t size, int flags)
75{
76 struct inode *inode = dentry->d_inode;
77 int error;
78
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080079 error = xattr_permission(inode, name, MAY_WRITE);
80 if (error)
81 return error;
82
Christoph Hellwig5be196e2006-01-09 20:51:55 -080083 mutex_lock(&inode->i_mutex);
84 error = security_inode_setxattr(dentry, name, value, size, flags);
85 if (error)
86 goto out;
87 error = -EOPNOTSUPP;
88 if (inode->i_op->setxattr) {
89 error = inode->i_op->setxattr(dentry, name, value, size, flags);
90 if (!error) {
91 fsnotify_xattr(dentry);
92 security_inode_post_setxattr(dentry, name, value,
93 size, flags);
94 }
95 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080096 XATTR_SECURITY_PREFIX_LEN)) {
97 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -080098 error = security_inode_setsecurity(inode, suffix, value,
99 size, flags);
100 if (!error)
101 fsnotify_xattr(dentry);
102 }
103out:
104 mutex_unlock(&inode->i_mutex);
105 return error;
106}
107EXPORT_SYMBOL_GPL(vfs_setxattr);
108
109ssize_t
110vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
111{
112 struct inode *inode = dentry->d_inode;
113 int error;
114
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800115 error = xattr_permission(inode, name, MAY_READ);
116 if (error)
117 return error;
118
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800119 error = security_inode_getxattr(dentry, name);
120 if (error)
121 return error;
122
123 if (inode->i_op->getxattr)
124 error = inode->i_op->getxattr(dentry, name, value, size);
125 else
126 error = -EOPNOTSUPP;
127
128 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800129 XATTR_SECURITY_PREFIX_LEN)) {
130 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800131 int ret = security_inode_getsecurity(inode, suffix, value,
132 size, error);
133 /*
134 * Only overwrite the return value if a security module
135 * is actually active.
136 */
137 if (ret != -EOPNOTSUPP)
138 error = ret;
139 }
140
141 return error;
142}
143EXPORT_SYMBOL_GPL(vfs_getxattr);
144
Bill Nottingham659564c2006-10-09 16:10:48 -0400145ssize_t
146vfs_listxattr(struct dentry *d, char *list, size_t size)
147{
148 ssize_t error;
149
150 error = security_inode_listxattr(d);
151 if (error)
152 return error;
153 error = -EOPNOTSUPP;
154 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
155 error = d->d_inode->i_op->listxattr(d, list, size);
156 } else {
157 error = security_inode_listsecurity(d->d_inode, list, size);
158 if (size && error > size)
159 error = -ERANGE;
160 }
161 return error;
162}
163EXPORT_SYMBOL_GPL(vfs_listxattr);
164
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800165int
166vfs_removexattr(struct dentry *dentry, char *name)
167{
168 struct inode *inode = dentry->d_inode;
169 int error;
170
171 if (!inode->i_op->removexattr)
172 return -EOPNOTSUPP;
173
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800174 error = xattr_permission(inode, name, MAY_WRITE);
175 if (error)
176 return error;
177
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800178 error = security_inode_removexattr(dentry, name);
179 if (error)
180 return error;
181
182 mutex_lock(&inode->i_mutex);
183 error = inode->i_op->removexattr(dentry, name);
184 mutex_unlock(&inode->i_mutex);
185
186 if (!error)
187 fsnotify_xattr(dentry);
188 return error;
189}
190EXPORT_SYMBOL_GPL(vfs_removexattr);
191
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * Extended attribute SET operations
195 */
196static long
197setxattr(struct dentry *d, char __user *name, void __user *value,
198 size_t size, int flags)
199{
200 int error;
201 void *kvalue = NULL;
202 char kname[XATTR_NAME_MAX + 1];
203
204 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
205 return -EINVAL;
206
207 error = strncpy_from_user(kname, name, sizeof(kname));
208 if (error == 0 || error == sizeof(kname))
209 error = -ERANGE;
210 if (error < 0)
211 return error;
212
213 if (size) {
214 if (size > XATTR_SIZE_MAX)
215 return -E2BIG;
216 kvalue = kmalloc(size, GFP_KERNEL);
217 if (!kvalue)
218 return -ENOMEM;
219 if (copy_from_user(kvalue, value, size)) {
220 kfree(kvalue);
221 return -EFAULT;
222 }
223 }
224
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800225 error = vfs_setxattr(d, kname, kvalue, size, flags);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800226 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return error;
228}
229
230asmlinkage long
231sys_setxattr(char __user *path, char __user *name, void __user *value,
232 size_t size, int flags)
233{
234 struct nameidata nd;
235 int error;
236
237 error = user_path_walk(path, &nd);
238 if (error)
239 return error;
240 error = setxattr(nd.dentry, name, value, size, flags);
241 path_release(&nd);
242 return error;
243}
244
245asmlinkage long
246sys_lsetxattr(char __user *path, char __user *name, void __user *value,
247 size_t size, int flags)
248{
249 struct nameidata nd;
250 int error;
251
252 error = user_path_walk_link(path, &nd);
253 if (error)
254 return error;
255 error = setxattr(nd.dentry, name, value, size, flags);
256 path_release(&nd);
257 return error;
258}
259
260asmlinkage long
261sys_fsetxattr(int fd, char __user *name, void __user *value,
262 size_t size, int flags)
263{
264 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000265 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 int error = -EBADF;
267
268 f = fget(fd);
269 if (!f)
270 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800271 dentry = f->f_path.dentry;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400272 audit_inode(NULL, dentry->d_inode);
Amy Griffis73241cc2005-11-03 16:00:25 +0000273 error = setxattr(dentry, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 fput(f);
275 return error;
276}
277
278/*
279 * Extended attribute GET operations
280 */
281static ssize_t
282getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
283{
284 ssize_t error;
285 void *kvalue = NULL;
286 char kname[XATTR_NAME_MAX + 1];
287
288 error = strncpy_from_user(kname, name, sizeof(kname));
289 if (error == 0 || error == sizeof(kname))
290 error = -ERANGE;
291 if (error < 0)
292 return error;
293
294 if (size) {
295 if (size > XATTR_SIZE_MAX)
296 size = XATTR_SIZE_MAX;
James Morrisd381d8a2005-10-30 14:59:22 -0800297 kvalue = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!kvalue)
299 return -ENOMEM;
300 }
301
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800302 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700303 if (error > 0) {
304 if (size && copy_to_user(value, kvalue, error))
305 error = -EFAULT;
306 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
307 /* The file system tried to returned a value bigger
308 than XATTR_SIZE_MAX bytes. Not possible. */
309 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800311 kfree(kvalue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return error;
313}
314
315asmlinkage ssize_t
316sys_getxattr(char __user *path, char __user *name, void __user *value,
317 size_t size)
318{
319 struct nameidata nd;
320 ssize_t error;
321
322 error = user_path_walk(path, &nd);
323 if (error)
324 return error;
325 error = getxattr(nd.dentry, name, value, size);
326 path_release(&nd);
327 return error;
328}
329
330asmlinkage ssize_t
331sys_lgetxattr(char __user *path, char __user *name, void __user *value,
332 size_t size)
333{
334 struct nameidata nd;
335 ssize_t error;
336
337 error = user_path_walk_link(path, &nd);
338 if (error)
339 return error;
340 error = getxattr(nd.dentry, name, value, size);
341 path_release(&nd);
342 return error;
343}
344
345asmlinkage ssize_t
346sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
347{
348 struct file *f;
349 ssize_t error = -EBADF;
350
351 f = fget(fd);
352 if (!f)
353 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800354 error = getxattr(f->f_path.dentry, name, value, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 fput(f);
356 return error;
357}
358
359/*
360 * Extended attribute LIST operations
361 */
362static ssize_t
363listxattr(struct dentry *d, char __user *list, size_t size)
364{
365 ssize_t error;
366 char *klist = NULL;
367
368 if (size) {
369 if (size > XATTR_LIST_MAX)
370 size = XATTR_LIST_MAX;
371 klist = kmalloc(size, GFP_KERNEL);
372 if (!klist)
373 return -ENOMEM;
374 }
375
Bill Nottingham659564c2006-10-09 16:10:48 -0400376 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700377 if (error > 0) {
378 if (size && copy_to_user(list, klist, error))
379 error = -EFAULT;
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. */
383 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800385 kfree(klist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return error;
387}
388
389asmlinkage ssize_t
390sys_listxattr(char __user *path, char __user *list, size_t size)
391{
392 struct nameidata nd;
393 ssize_t error;
394
395 error = user_path_walk(path, &nd);
396 if (error)
397 return error;
398 error = listxattr(nd.dentry, list, size);
399 path_release(&nd);
400 return error;
401}
402
403asmlinkage ssize_t
404sys_llistxattr(char __user *path, char __user *list, size_t size)
405{
406 struct nameidata nd;
407 ssize_t error;
408
409 error = user_path_walk_link(path, &nd);
410 if (error)
411 return error;
412 error = listxattr(nd.dentry, list, size);
413 path_release(&nd);
414 return error;
415}
416
417asmlinkage ssize_t
418sys_flistxattr(int fd, char __user *list, size_t size)
419{
420 struct file *f;
421 ssize_t error = -EBADF;
422
423 f = fget(fd);
424 if (!f)
425 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800426 error = listxattr(f->f_path.dentry, list, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 fput(f);
428 return error;
429}
430
431/*
432 * Extended attribute REMOVE operations
433 */
434static long
435removexattr(struct dentry *d, char __user *name)
436{
437 int error;
438 char kname[XATTR_NAME_MAX + 1];
439
440 error = strncpy_from_user(kname, name, sizeof(kname));
441 if (error == 0 || error == sizeof(kname))
442 error = -ERANGE;
443 if (error < 0)
444 return error;
445
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800446 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449asmlinkage long
450sys_removexattr(char __user *path, char __user *name)
451{
452 struct nameidata nd;
453 int error;
454
455 error = user_path_walk(path, &nd);
456 if (error)
457 return error;
458 error = removexattr(nd.dentry, name);
459 path_release(&nd);
460 return error;
461}
462
463asmlinkage long
464sys_lremovexattr(char __user *path, char __user *name)
465{
466 struct nameidata nd;
467 int error;
468
469 error = user_path_walk_link(path, &nd);
470 if (error)
471 return error;
472 error = removexattr(nd.dentry, name);
473 path_release(&nd);
474 return error;
475}
476
477asmlinkage long
478sys_fremovexattr(int fd, char __user *name)
479{
480 struct file *f;
Amy Griffis73241cc2005-11-03 16:00:25 +0000481 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int error = -EBADF;
483
484 f = fget(fd);
485 if (!f)
486 return error;
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800487 dentry = f->f_path.dentry;
Amy Griffis9c937dc2006-06-08 23:19:31 -0400488 audit_inode(NULL, dentry->d_inode);
Amy Griffis73241cc2005-11-03 16:00:25 +0000489 error = removexattr(dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 fput(f);
491 return error;
492}
493
494
495static const char *
496strcmp_prefix(const char *a, const char *a_prefix)
497{
498 while (*a_prefix && *a == *a_prefix) {
499 a++;
500 a_prefix++;
501 }
502 return *a_prefix ? NULL : a;
503}
504
505/*
506 * In order to implement different sets of xattr operations for each xattr
507 * prefix with the generic xattr API, a filesystem should create a
508 * null-terminated array of struct xattr_handler (one for each prefix) and
509 * hang a pointer to it off of the s_xattr field of the superblock.
510 *
511 * The generic_fooxattr() functions will use this list to dispatch xattr
512 * operations to the correct xattr_handler.
513 */
514#define for_each_xattr_handler(handlers, handler) \
515 for ((handler) = *(handlers)++; \
516 (handler) != NULL; \
517 (handler) = *(handlers)++)
518
519/*
520 * Find the xattr_handler with the matching prefix.
521 */
522static struct xattr_handler *
523xattr_resolve_name(struct xattr_handler **handlers, const char **name)
524{
525 struct xattr_handler *handler;
526
527 if (!*name)
528 return NULL;
529
530 for_each_xattr_handler(handlers, handler) {
531 const char *n = strcmp_prefix(*name, handler->prefix);
532 if (n) {
533 *name = n;
534 break;
535 }
536 }
537 return handler;
538}
539
540/*
541 * Find the handler for the prefix and dispatch its get() operation.
542 */
543ssize_t
544generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
545{
546 struct xattr_handler *handler;
547 struct inode *inode = dentry->d_inode;
548
549 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
550 if (!handler)
551 return -EOPNOTSUPP;
552 return handler->get(inode, name, buffer, size);
553}
554
555/*
556 * Combine the results of the list() operation from every xattr_handler in the
557 * list.
558 */
559ssize_t
560generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
561{
562 struct inode *inode = dentry->d_inode;
563 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
564 unsigned int size = 0;
565
566 if (!buffer) {
567 for_each_xattr_handler(handlers, handler)
568 size += handler->list(inode, NULL, 0, NULL, 0);
569 } else {
570 char *buf = buffer;
571
572 for_each_xattr_handler(handlers, handler) {
573 size = handler->list(inode, buf, buffer_size, NULL, 0);
574 if (size > buffer_size)
575 return -ERANGE;
576 buf += size;
577 buffer_size -= size;
578 }
579 size = buf - buffer;
580 }
581 return size;
582}
583
584/*
585 * Find the handler for the prefix and dispatch its set() operation.
586 */
587int
588generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
589{
590 struct xattr_handler *handler;
591 struct inode *inode = dentry->d_inode;
592
593 if (size == 0)
594 value = ""; /* empty EA, do not remove */
595 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
596 if (!handler)
597 return -EOPNOTSUPP;
598 return handler->set(inode, name, value, size, flags);
599}
600
601/*
602 * Find the handler for the prefix and dispatch its set() operation to remove
603 * any associated extended attribute.
604 */
605int
606generic_removexattr(struct dentry *dentry, const char *name)
607{
608 struct xattr_handler *handler;
609 struct inode *inode = dentry->d_inode;
610
611 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
612 if (!handler)
613 return -EOPNOTSUPP;
614 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
615}
616
617EXPORT_SYMBOL(generic_getxattr);
618EXPORT_SYMBOL(generic_listxattr);
619EXPORT_SYMBOL(generic_setxattr);
620EXPORT_SYMBOL(generic_removexattr);