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