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