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