ovl: don't allow writing ioctl on lower layer
[sfrench/cifs-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/file.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/namei.h>
16 #include <linux/security.h>
17 #include <linux/evm.h>
18 #include <linux/syscalls.h>
19 #include <linux/export.h>
20 #include <linux/fsnotify.h>
21 #include <linux/audit.h>
22 #include <linux/vmalloc.h>
23 #include <linux/posix_acl_xattr.h>
24
25 #include <linux/uaccess.h>
26 #include "internal.h"
27
28 static const char *
29 strcmp_prefix(const char *a, const char *a_prefix)
30 {
31         while (*a_prefix && *a == *a_prefix) {
32                 a++;
33                 a_prefix++;
34         }
35         return *a_prefix ? NULL : a;
36 }
37
38 /*
39  * In order to implement different sets of xattr operations for each xattr
40  * prefix, a filesystem should create a null-terminated array of struct
41  * xattr_handler (one for each prefix) and hang a pointer to it off of the
42  * s_xattr field of the superblock.
43  */
44 #define for_each_xattr_handler(handlers, handler)               \
45         if (handlers)                                           \
46                 for ((handler) = *(handlers)++;                 \
47                         (handler) != NULL;                      \
48                         (handler) = *(handlers)++)
49
50 /*
51  * Find the xattr_handler with the matching prefix.
52  */
53 static const struct xattr_handler *
54 xattr_resolve_name(struct inode *inode, const char **name)
55 {
56         const struct xattr_handler **handlers = inode->i_sb->s_xattr;
57         const struct xattr_handler *handler;
58
59         if (!(inode->i_opflags & IOP_XATTR)) {
60                 if (unlikely(is_bad_inode(inode)))
61                         return ERR_PTR(-EIO);
62                 return ERR_PTR(-EOPNOTSUPP);
63         }
64         for_each_xattr_handler(handlers, handler) {
65                 const char *n;
66
67                 n = strcmp_prefix(*name, xattr_prefix(handler));
68                 if (n) {
69                         if (!handler->prefix ^ !*n) {
70                                 if (*n)
71                                         continue;
72                                 return ERR_PTR(-EINVAL);
73                         }
74                         *name = n;
75                         return handler;
76                 }
77         }
78         return ERR_PTR(-EOPNOTSUPP);
79 }
80
81 /*
82  * Check permissions for extended attribute access.  This is a bit complicated
83  * because different namespaces have very different rules.
84  */
85 static int
86 xattr_permission(struct inode *inode, const char *name, int mask)
87 {
88         /*
89          * We can never set or remove an extended attribute on a read-only
90          * filesystem  or on an immutable / append-only inode.
91          */
92         if (mask & MAY_WRITE) {
93                 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
94                         return -EPERM;
95                 /*
96                  * Updating an xattr will likely cause i_uid and i_gid
97                  * to be writen back improperly if their true value is
98                  * unknown to the vfs.
99                  */
100                 if (HAS_UNMAPPED_ID(inode))
101                         return -EPERM;
102         }
103
104         /*
105          * No restriction for security.* and system.* from the VFS.  Decision
106          * on these is left to the underlying filesystem / security module.
107          */
108         if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
109             !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
110                 return 0;
111
112         /*
113          * The trusted.* namespace can only be accessed by privileged users.
114          */
115         if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
116                 if (!capable(CAP_SYS_ADMIN))
117                         return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
118                 return 0;
119         }
120
121         /*
122          * In the user.* namespace, only regular files and directories can have
123          * extended attributes. For sticky directories, only the owner and
124          * privileged users can write attributes.
125          */
126         if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
127                 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
128                         return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
129                 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
130                     (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
131                         return -EPERM;
132         }
133
134         return inode_permission(inode, mask);
135 }
136
137 int
138 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
139                const void *value, size_t size, int flags)
140 {
141         const struct xattr_handler *handler;
142
143         handler = xattr_resolve_name(inode, &name);
144         if (IS_ERR(handler))
145                 return PTR_ERR(handler);
146         if (!handler->set)
147                 return -EOPNOTSUPP;
148         if (size == 0)
149                 value = "";  /* empty EA, do not remove */
150         return handler->set(handler, dentry, inode, name, value, size, flags);
151 }
152 EXPORT_SYMBOL(__vfs_setxattr);
153
154 /**
155  *  __vfs_setxattr_noperm - perform setxattr operation without performing
156  *  permission checks.
157  *
158  *  @dentry - object to perform setxattr on
159  *  @name - xattr name to set
160  *  @value - value to set @name to
161  *  @size - size of @value
162  *  @flags - flags to pass into filesystem operations
163  *
164  *  returns the result of the internal setxattr or setsecurity operations.
165  *
166  *  This function requires the caller to lock the inode's i_mutex before it
167  *  is executed. It also assumes that the caller will make the appropriate
168  *  permission checks.
169  */
170 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
171                 const void *value, size_t size, int flags)
172 {
173         struct inode *inode = dentry->d_inode;
174         int error = -EAGAIN;
175         int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
176                                    XATTR_SECURITY_PREFIX_LEN);
177
178         if (issec)
179                 inode->i_flags &= ~S_NOSEC;
180         if (inode->i_opflags & IOP_XATTR) {
181                 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
182                 if (!error) {
183                         fsnotify_xattr(dentry);
184                         security_inode_post_setxattr(dentry, name, value,
185                                                      size, flags);
186                 }
187         } else {
188                 if (unlikely(is_bad_inode(inode)))
189                         return -EIO;
190         }
191         if (error == -EAGAIN) {
192                 error = -EOPNOTSUPP;
193
194                 if (issec) {
195                         const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
196
197                         error = security_inode_setsecurity(inode, suffix, value,
198                                                            size, flags);
199                         if (!error)
200                                 fsnotify_xattr(dentry);
201                 }
202         }
203
204         return error;
205 }
206
207
208 int
209 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
210                 size_t size, int flags)
211 {
212         struct inode *inode = dentry->d_inode;
213         int error;
214
215         error = xattr_permission(inode, name, MAY_WRITE);
216         if (error)
217                 return error;
218
219         inode_lock(inode);
220         error = security_inode_setxattr(dentry, name, value, size, flags);
221         if (error)
222                 goto out;
223
224         error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
225
226 out:
227         inode_unlock(inode);
228         return error;
229 }
230 EXPORT_SYMBOL_GPL(vfs_setxattr);
231
232 ssize_t
233 xattr_getsecurity(struct inode *inode, const char *name, void *value,
234                         size_t size)
235 {
236         void *buffer = NULL;
237         ssize_t len;
238
239         if (!value || !size) {
240                 len = security_inode_getsecurity(inode, name, &buffer, false);
241                 goto out_noalloc;
242         }
243
244         len = security_inode_getsecurity(inode, name, &buffer, true);
245         if (len < 0)
246                 return len;
247         if (size < len) {
248                 len = -ERANGE;
249                 goto out;
250         }
251         memcpy(value, buffer, len);
252 out:
253         security_release_secctx(buffer, len);
254 out_noalloc:
255         return len;
256 }
257 EXPORT_SYMBOL_GPL(xattr_getsecurity);
258
259 /*
260  * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
261  *
262  * Allocate memory, if not already allocated, or re-allocate correct size,
263  * before retrieving the extended attribute.
264  *
265  * Returns the result of alloc, if failed, or the getxattr operation.
266  */
267 ssize_t
268 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
269                    size_t xattr_size, gfp_t flags)
270 {
271         const struct xattr_handler *handler;
272         struct inode *inode = dentry->d_inode;
273         char *value = *xattr_value;
274         int error;
275
276         error = xattr_permission(inode, name, MAY_READ);
277         if (error)
278                 return error;
279
280         handler = xattr_resolve_name(inode, &name);
281         if (IS_ERR(handler))
282                 return PTR_ERR(handler);
283         if (!handler->get)
284                 return -EOPNOTSUPP;
285         error = handler->get(handler, dentry, inode, name, NULL, 0);
286         if (error < 0)
287                 return error;
288
289         if (!value || (error > xattr_size)) {
290                 value = krealloc(*xattr_value, error + 1, flags);
291                 if (!value)
292                         return -ENOMEM;
293                 memset(value, 0, error + 1);
294         }
295
296         error = handler->get(handler, dentry, inode, name, value, error);
297         *xattr_value = value;
298         return error;
299 }
300
301 ssize_t
302 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
303                void *value, size_t size)
304 {
305         const struct xattr_handler *handler;
306
307         handler = xattr_resolve_name(inode, &name);
308         if (IS_ERR(handler))
309                 return PTR_ERR(handler);
310         if (!handler->get)
311                 return -EOPNOTSUPP;
312         return handler->get(handler, dentry, inode, name, value, size);
313 }
314 EXPORT_SYMBOL(__vfs_getxattr);
315
316 ssize_t
317 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
318 {
319         struct inode *inode = dentry->d_inode;
320         int error;
321
322         error = xattr_permission(inode, name, MAY_READ);
323         if (error)
324                 return error;
325
326         error = security_inode_getxattr(dentry, name);
327         if (error)
328                 return error;
329
330         if (!strncmp(name, XATTR_SECURITY_PREFIX,
331                                 XATTR_SECURITY_PREFIX_LEN)) {
332                 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
333                 int ret = xattr_getsecurity(inode, suffix, value, size);
334                 /*
335                  * Only overwrite the return value if a security module
336                  * is actually active.
337                  */
338                 if (ret == -EOPNOTSUPP)
339                         goto nolsm;
340                 return ret;
341         }
342 nolsm:
343         return __vfs_getxattr(dentry, inode, name, value, size);
344 }
345 EXPORT_SYMBOL_GPL(vfs_getxattr);
346
347 ssize_t
348 vfs_listxattr(struct dentry *dentry, char *list, size_t size)
349 {
350         struct inode *inode = d_inode(dentry);
351         ssize_t error;
352
353         error = security_inode_listxattr(dentry);
354         if (error)
355                 return error;
356         if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
357                 error = -EOPNOTSUPP;
358                 error = inode->i_op->listxattr(dentry, list, size);
359         } else {
360                 error = security_inode_listsecurity(inode, list, size);
361                 if (size && error > size)
362                         error = -ERANGE;
363         }
364         return error;
365 }
366 EXPORT_SYMBOL_GPL(vfs_listxattr);
367
368 int
369 __vfs_removexattr(struct dentry *dentry, const char *name)
370 {
371         struct inode *inode = d_inode(dentry);
372         const struct xattr_handler *handler;
373
374         handler = xattr_resolve_name(inode, &name);
375         if (IS_ERR(handler))
376                 return PTR_ERR(handler);
377         if (!handler->set)
378                 return -EOPNOTSUPP;
379         return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
380 }
381 EXPORT_SYMBOL(__vfs_removexattr);
382
383 int
384 vfs_removexattr(struct dentry *dentry, const char *name)
385 {
386         struct inode *inode = dentry->d_inode;
387         int error;
388
389         error = xattr_permission(inode, name, MAY_WRITE);
390         if (error)
391                 return error;
392
393         inode_lock(inode);
394         error = security_inode_removexattr(dentry, name);
395         if (error)
396                 goto out;
397
398         error = __vfs_removexattr(dentry, name);
399
400         if (!error) {
401                 fsnotify_xattr(dentry);
402                 evm_inode_post_removexattr(dentry, name);
403         }
404
405 out:
406         inode_unlock(inode);
407         return error;
408 }
409 EXPORT_SYMBOL_GPL(vfs_removexattr);
410
411
412 /*
413  * Extended attribute SET operations
414  */
415 static long
416 setxattr(struct dentry *d, const char __user *name, const void __user *value,
417          size_t size, int flags)
418 {
419         int error;
420         void *kvalue = NULL;
421         char kname[XATTR_NAME_MAX + 1];
422
423         if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
424                 return -EINVAL;
425
426         error = strncpy_from_user(kname, name, sizeof(kname));
427         if (error == 0 || error == sizeof(kname))
428                 error = -ERANGE;
429         if (error < 0)
430                 return error;
431
432         if (size) {
433                 if (size > XATTR_SIZE_MAX)
434                         return -E2BIG;
435                 kvalue = kvmalloc(size, GFP_KERNEL);
436                 if (!kvalue)
437                         return -ENOMEM;
438                 if (copy_from_user(kvalue, value, size)) {
439                         error = -EFAULT;
440                         goto out;
441                 }
442                 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
443                     (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
444                         posix_acl_fix_xattr_from_user(kvalue, size);
445         }
446
447         error = vfs_setxattr(d, kname, kvalue, size, flags);
448 out:
449         kvfree(kvalue);
450
451         return error;
452 }
453
454 static int path_setxattr(const char __user *pathname,
455                          const char __user *name, const void __user *value,
456                          size_t size, int flags, unsigned int lookup_flags)
457 {
458         struct path path;
459         int error;
460 retry:
461         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
462         if (error)
463                 return error;
464         error = mnt_want_write(path.mnt);
465         if (!error) {
466                 error = setxattr(path.dentry, name, value, size, flags);
467                 mnt_drop_write(path.mnt);
468         }
469         path_put(&path);
470         if (retry_estale(error, lookup_flags)) {
471                 lookup_flags |= LOOKUP_REVAL;
472                 goto retry;
473         }
474         return error;
475 }
476
477 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
478                 const char __user *, name, const void __user *, value,
479                 size_t, size, int, flags)
480 {
481         return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
482 }
483
484 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
485                 const char __user *, name, const void __user *, value,
486                 size_t, size, int, flags)
487 {
488         return path_setxattr(pathname, name, value, size, flags, 0);
489 }
490
491 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
492                 const void __user *,value, size_t, size, int, flags)
493 {
494         struct fd f = fdget(fd);
495         int error = -EBADF;
496
497         if (!f.file)
498                 return error;
499         audit_file(f.file);
500         error = mnt_want_write_file_path(f.file);
501         if (!error) {
502                 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
503                 mnt_drop_write_file_path(f.file);
504         }
505         fdput(f);
506         return error;
507 }
508
509 /*
510  * Extended attribute GET operations
511  */
512 static ssize_t
513 getxattr(struct dentry *d, const char __user *name, void __user *value,
514          size_t size)
515 {
516         ssize_t error;
517         void *kvalue = NULL;
518         char kname[XATTR_NAME_MAX + 1];
519
520         error = strncpy_from_user(kname, name, sizeof(kname));
521         if (error == 0 || error == sizeof(kname))
522                 error = -ERANGE;
523         if (error < 0)
524                 return error;
525
526         if (size) {
527                 if (size > XATTR_SIZE_MAX)
528                         size = XATTR_SIZE_MAX;
529                 kvalue = kvzalloc(size, GFP_KERNEL);
530                 if (!kvalue)
531                         return -ENOMEM;
532         }
533
534         error = vfs_getxattr(d, kname, kvalue, size);
535         if (error > 0) {
536                 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
537                     (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
538                         posix_acl_fix_xattr_to_user(kvalue, size);
539                 if (size && copy_to_user(value, kvalue, error))
540                         error = -EFAULT;
541         } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
542                 /* The file system tried to returned a value bigger
543                    than XATTR_SIZE_MAX bytes. Not possible. */
544                 error = -E2BIG;
545         }
546
547         kvfree(kvalue);
548
549         return error;
550 }
551
552 static ssize_t path_getxattr(const char __user *pathname,
553                              const char __user *name, void __user *value,
554                              size_t size, unsigned int lookup_flags)
555 {
556         struct path path;
557         ssize_t error;
558 retry:
559         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
560         if (error)
561                 return error;
562         error = getxattr(path.dentry, name, value, size);
563         path_put(&path);
564         if (retry_estale(error, lookup_flags)) {
565                 lookup_flags |= LOOKUP_REVAL;
566                 goto retry;
567         }
568         return error;
569 }
570
571 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
572                 const char __user *, name, void __user *, value, size_t, size)
573 {
574         return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
575 }
576
577 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
578                 const char __user *, name, void __user *, value, size_t, size)
579 {
580         return path_getxattr(pathname, name, value, size, 0);
581 }
582
583 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
584                 void __user *, value, size_t, size)
585 {
586         struct fd f = fdget(fd);
587         ssize_t error = -EBADF;
588
589         if (!f.file)
590                 return error;
591         audit_file(f.file);
592         error = getxattr(f.file->f_path.dentry, name, value, size);
593         fdput(f);
594         return error;
595 }
596
597 /*
598  * Extended attribute LIST operations
599  */
600 static ssize_t
601 listxattr(struct dentry *d, char __user *list, size_t size)
602 {
603         ssize_t error;
604         char *klist = NULL;
605
606         if (size) {
607                 if (size > XATTR_LIST_MAX)
608                         size = XATTR_LIST_MAX;
609                 klist = kvmalloc(size, GFP_KERNEL);
610                 if (!klist)
611                         return -ENOMEM;
612         }
613
614         error = vfs_listxattr(d, klist, size);
615         if (error > 0) {
616                 if (size && copy_to_user(list, klist, error))
617                         error = -EFAULT;
618         } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
619                 /* The file system tried to returned a list bigger
620                    than XATTR_LIST_MAX bytes. Not possible. */
621                 error = -E2BIG;
622         }
623
624         kvfree(klist);
625
626         return error;
627 }
628
629 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
630                               size_t size, unsigned int lookup_flags)
631 {
632         struct path path;
633         ssize_t error;
634 retry:
635         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
636         if (error)
637                 return error;
638         error = listxattr(path.dentry, list, size);
639         path_put(&path);
640         if (retry_estale(error, lookup_flags)) {
641                 lookup_flags |= LOOKUP_REVAL;
642                 goto retry;
643         }
644         return error;
645 }
646
647 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
648                 size_t, size)
649 {
650         return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
651 }
652
653 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
654                 size_t, size)
655 {
656         return path_listxattr(pathname, list, size, 0);
657 }
658
659 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
660 {
661         struct fd f = fdget(fd);
662         ssize_t error = -EBADF;
663
664         if (!f.file)
665                 return error;
666         audit_file(f.file);
667         error = listxattr(f.file->f_path.dentry, list, size);
668         fdput(f);
669         return error;
670 }
671
672 /*
673  * Extended attribute REMOVE operations
674  */
675 static long
676 removexattr(struct dentry *d, const char __user *name)
677 {
678         int error;
679         char kname[XATTR_NAME_MAX + 1];
680
681         error = strncpy_from_user(kname, name, sizeof(kname));
682         if (error == 0 || error == sizeof(kname))
683                 error = -ERANGE;
684         if (error < 0)
685                 return error;
686
687         return vfs_removexattr(d, kname);
688 }
689
690 static int path_removexattr(const char __user *pathname,
691                             const char __user *name, unsigned int lookup_flags)
692 {
693         struct path path;
694         int error;
695 retry:
696         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
697         if (error)
698                 return error;
699         error = mnt_want_write(path.mnt);
700         if (!error) {
701                 error = removexattr(path.dentry, name);
702                 mnt_drop_write(path.mnt);
703         }
704         path_put(&path);
705         if (retry_estale(error, lookup_flags)) {
706                 lookup_flags |= LOOKUP_REVAL;
707                 goto retry;
708         }
709         return error;
710 }
711
712 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
713                 const char __user *, name)
714 {
715         return path_removexattr(pathname, name, LOOKUP_FOLLOW);
716 }
717
718 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
719                 const char __user *, name)
720 {
721         return path_removexattr(pathname, name, 0);
722 }
723
724 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
725 {
726         struct fd f = fdget(fd);
727         int error = -EBADF;
728
729         if (!f.file)
730                 return error;
731         audit_file(f.file);
732         error = mnt_want_write_file_path(f.file);
733         if (!error) {
734                 error = removexattr(f.file->f_path.dentry, name);
735                 mnt_drop_write_file_path(f.file);
736         }
737         fdput(f);
738         return error;
739 }
740
741 /*
742  * Combine the results of the list() operation from every xattr_handler in the
743  * list.
744  */
745 ssize_t
746 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
747 {
748         const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
749         unsigned int size = 0;
750
751         if (!buffer) {
752                 for_each_xattr_handler(handlers, handler) {
753                         if (!handler->name ||
754                             (handler->list && !handler->list(dentry)))
755                                 continue;
756                         size += strlen(handler->name) + 1;
757                 }
758         } else {
759                 char *buf = buffer;
760                 size_t len;
761
762                 for_each_xattr_handler(handlers, handler) {
763                         if (!handler->name ||
764                             (handler->list && !handler->list(dentry)))
765                                 continue;
766                         len = strlen(handler->name);
767                         if (len + 1 > buffer_size)
768                                 return -ERANGE;
769                         memcpy(buf, handler->name, len + 1);
770                         buf += len + 1;
771                         buffer_size -= len + 1;
772                 }
773                 size = buf - buffer;
774         }
775         return size;
776 }
777 EXPORT_SYMBOL(generic_listxattr);
778
779 /**
780  * xattr_full_name  -  Compute full attribute name from suffix
781  *
782  * @handler:    handler of the xattr_handler operation
783  * @name:       name passed to the xattr_handler operation
784  *
785  * The get and set xattr handler operations are called with the remainder of
786  * the attribute name after skipping the handler's prefix: for example, "foo"
787  * is passed to the get operation of a handler with prefix "user." to get
788  * attribute "user.foo".  The full name is still "there" in the name though.
789  *
790  * Note: the list xattr handler operation when called from the vfs is passed a
791  * NULL name; some file systems use this operation internally, with varying
792  * semantics.
793  */
794 const char *xattr_full_name(const struct xattr_handler *handler,
795                             const char *name)
796 {
797         size_t prefix_len = strlen(xattr_prefix(handler));
798
799         return name - prefix_len;
800 }
801 EXPORT_SYMBOL(xattr_full_name);
802
803 /*
804  * Allocate new xattr and copy in the value; but leave the name to callers.
805  */
806 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
807 {
808         struct simple_xattr *new_xattr;
809         size_t len;
810
811         /* wrap around? */
812         len = sizeof(*new_xattr) + size;
813         if (len < sizeof(*new_xattr))
814                 return NULL;
815
816         new_xattr = kmalloc(len, GFP_KERNEL);
817         if (!new_xattr)
818                 return NULL;
819
820         new_xattr->size = size;
821         memcpy(new_xattr->value, value, size);
822         return new_xattr;
823 }
824
825 /*
826  * xattr GET operation for in-memory/pseudo filesystems
827  */
828 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
829                      void *buffer, size_t size)
830 {
831         struct simple_xattr *xattr;
832         int ret = -ENODATA;
833
834         spin_lock(&xattrs->lock);
835         list_for_each_entry(xattr, &xattrs->head, list) {
836                 if (strcmp(name, xattr->name))
837                         continue;
838
839                 ret = xattr->size;
840                 if (buffer) {
841                         if (size < xattr->size)
842                                 ret = -ERANGE;
843                         else
844                                 memcpy(buffer, xattr->value, xattr->size);
845                 }
846                 break;
847         }
848         spin_unlock(&xattrs->lock);
849         return ret;
850 }
851
852 /**
853  * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
854  * @xattrs: target simple_xattr list
855  * @name: name of the extended attribute
856  * @value: value of the xattr. If %NULL, will remove the attribute.
857  * @size: size of the new xattr
858  * @flags: %XATTR_{CREATE|REPLACE}
859  *
860  * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
861  * with -EEXIST.  If %XATTR_REPLACE is set, the xattr should exist;
862  * otherwise, fails with -ENODATA.
863  *
864  * Returns 0 on success, -errno on failure.
865  */
866 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
867                      const void *value, size_t size, int flags)
868 {
869         struct simple_xattr *xattr;
870         struct simple_xattr *new_xattr = NULL;
871         int err = 0;
872
873         /* value == NULL means remove */
874         if (value) {
875                 new_xattr = simple_xattr_alloc(value, size);
876                 if (!new_xattr)
877                         return -ENOMEM;
878
879                 new_xattr->name = kstrdup(name, GFP_KERNEL);
880                 if (!new_xattr->name) {
881                         kfree(new_xattr);
882                         return -ENOMEM;
883                 }
884         }
885
886         spin_lock(&xattrs->lock);
887         list_for_each_entry(xattr, &xattrs->head, list) {
888                 if (!strcmp(name, xattr->name)) {
889                         if (flags & XATTR_CREATE) {
890                                 xattr = new_xattr;
891                                 err = -EEXIST;
892                         } else if (new_xattr) {
893                                 list_replace(&xattr->list, &new_xattr->list);
894                         } else {
895                                 list_del(&xattr->list);
896                         }
897                         goto out;
898                 }
899         }
900         if (flags & XATTR_REPLACE) {
901                 xattr = new_xattr;
902                 err = -ENODATA;
903         } else {
904                 list_add(&new_xattr->list, &xattrs->head);
905                 xattr = NULL;
906         }
907 out:
908         spin_unlock(&xattrs->lock);
909         if (xattr) {
910                 kfree(xattr->name);
911                 kfree(xattr);
912         }
913         return err;
914
915 }
916
917 static bool xattr_is_trusted(const char *name)
918 {
919         return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
920 }
921
922 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
923                           const char *name)
924 {
925         size_t len = strlen(name) + 1;
926         if (*buffer) {
927                 if (*remaining_size < len)
928                         return -ERANGE;
929                 memcpy(*buffer, name, len);
930                 *buffer += len;
931         }
932         *remaining_size -= len;
933         return 0;
934 }
935
936 /*
937  * xattr LIST operation for in-memory/pseudo filesystems
938  */
939 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
940                           char *buffer, size_t size)
941 {
942         bool trusted = capable(CAP_SYS_ADMIN);
943         struct simple_xattr *xattr;
944         ssize_t remaining_size = size;
945         int err = 0;
946
947 #ifdef CONFIG_FS_POSIX_ACL
948         if (inode->i_acl) {
949                 err = xattr_list_one(&buffer, &remaining_size,
950                                      XATTR_NAME_POSIX_ACL_ACCESS);
951                 if (err)
952                         return err;
953         }
954         if (inode->i_default_acl) {
955                 err = xattr_list_one(&buffer, &remaining_size,
956                                      XATTR_NAME_POSIX_ACL_DEFAULT);
957                 if (err)
958                         return err;
959         }
960 #endif
961
962         spin_lock(&xattrs->lock);
963         list_for_each_entry(xattr, &xattrs->head, list) {
964                 /* skip "trusted." attributes for unprivileged callers */
965                 if (!trusted && xattr_is_trusted(xattr->name))
966                         continue;
967
968                 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
969                 if (err)
970                         break;
971         }
972         spin_unlock(&xattrs->lock);
973
974         return err ? err : size - remaining_size;
975 }
976
977 /*
978  * Adds an extended attribute to the list
979  */
980 void simple_xattr_list_add(struct simple_xattrs *xattrs,
981                            struct simple_xattr *new_xattr)
982 {
983         spin_lock(&xattrs->lock);
984         list_add(&new_xattr->list, &xattrs->head);
985         spin_unlock(&xattrs->lock);
986 }