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