acl: handle idmapped mounts
[linux-2.6-block.git] / fs / xattr.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3   File: fs/xattr.c
4
5   Extended attribute handling.
6
7   Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8   Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
9   Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
10  */
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/evm.h>
19 #include <linux/syscalls.h>
20 #include <linux/export.h>
21 #include <linux/fsnotify.h>
22 #include <linux/audit.h>
23 #include <linux/vmalloc.h>
24 #include <linux/posix_acl_xattr.h>
25
26 #include <linux/uaccess.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) &&
131                     !inode_owner_or_capable(&init_user_ns, inode))
132                         return -EPERM;
133         }
134
135         return inode_permission(&init_user_ns, inode, mask);
136 }
137
138 /*
139  * Look for any handler that deals with the specified namespace.
140  */
141 int
142 xattr_supported_namespace(struct inode *inode, const char *prefix)
143 {
144         const struct xattr_handler **handlers = inode->i_sb->s_xattr;
145         const struct xattr_handler *handler;
146         size_t preflen;
147
148         if (!(inode->i_opflags & IOP_XATTR)) {
149                 if (unlikely(is_bad_inode(inode)))
150                         return -EIO;
151                 return -EOPNOTSUPP;
152         }
153
154         preflen = strlen(prefix);
155
156         for_each_xattr_handler(handlers, handler) {
157                 if (!strncmp(xattr_prefix(handler), prefix, preflen))
158                         return 0;
159         }
160
161         return -EOPNOTSUPP;
162 }
163 EXPORT_SYMBOL(xattr_supported_namespace);
164
165 int
166 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
167                const void *value, size_t size, int flags)
168 {
169         const struct xattr_handler *handler;
170
171         handler = xattr_resolve_name(inode, &name);
172         if (IS_ERR(handler))
173                 return PTR_ERR(handler);
174         if (!handler->set)
175                 return -EOPNOTSUPP;
176         if (size == 0)
177                 value = "";  /* empty EA, do not remove */
178         return handler->set(handler, &init_user_ns, dentry, inode, name, value,
179                             size, flags);
180 }
181 EXPORT_SYMBOL(__vfs_setxattr);
182
183 /**
184  *  __vfs_setxattr_noperm - perform setxattr operation without performing
185  *  permission checks.
186  *
187  *  @dentry - object to perform setxattr on
188  *  @name - xattr name to set
189  *  @value - value to set @name to
190  *  @size - size of @value
191  *  @flags - flags to pass into filesystem operations
192  *
193  *  returns the result of the internal setxattr or setsecurity operations.
194  *
195  *  This function requires the caller to lock the inode's i_mutex before it
196  *  is executed. It also assumes that the caller will make the appropriate
197  *  permission checks.
198  */
199 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
200                 const void *value, size_t size, int flags)
201 {
202         struct inode *inode = dentry->d_inode;
203         int error = -EAGAIN;
204         int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
205                                    XATTR_SECURITY_PREFIX_LEN);
206
207         if (issec)
208                 inode->i_flags &= ~S_NOSEC;
209         if (inode->i_opflags & IOP_XATTR) {
210                 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
211                 if (!error) {
212                         fsnotify_xattr(dentry);
213                         security_inode_post_setxattr(dentry, name, value,
214                                                      size, flags);
215                 }
216         } else {
217                 if (unlikely(is_bad_inode(inode)))
218                         return -EIO;
219         }
220         if (error == -EAGAIN) {
221                 error = -EOPNOTSUPP;
222
223                 if (issec) {
224                         const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
225
226                         error = security_inode_setsecurity(inode, suffix, value,
227                                                            size, flags);
228                         if (!error)
229                                 fsnotify_xattr(dentry);
230                 }
231         }
232
233         return error;
234 }
235
236 /**
237  * __vfs_setxattr_locked - set an extended attribute while holding the inode
238  * lock
239  *
240  *  @dentry: object to perform setxattr on
241  *  @name: xattr name to set
242  *  @value: value to set @name to
243  *  @size: size of @value
244  *  @flags: flags to pass into filesystem operations
245  *  @delegated_inode: on return, will contain an inode pointer that
246  *  a delegation was broken on, NULL if none.
247  */
248 int
249 __vfs_setxattr_locked(struct dentry *dentry, const char *name,
250                 const void *value, size_t size, int flags,
251                 struct inode **delegated_inode)
252 {
253         struct inode *inode = dentry->d_inode;
254         int error;
255
256         error = xattr_permission(inode, name, MAY_WRITE);
257         if (error)
258                 return error;
259
260         error = security_inode_setxattr(dentry, name, value, size, flags);
261         if (error)
262                 goto out;
263
264         error = try_break_deleg(inode, delegated_inode);
265         if (error)
266                 goto out;
267
268         error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
269
270 out:
271         return error;
272 }
273 EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
274
275 int
276 vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
277                 size_t size, int flags)
278 {
279         struct inode *inode = dentry->d_inode;
280         struct inode *delegated_inode = NULL;
281         const void  *orig_value = value;
282         int error;
283
284         if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
285                 error = cap_convert_nscap(&init_user_ns, dentry, &value, size);
286                 if (error < 0)
287                         return error;
288                 size = error;
289         }
290
291 retry_deleg:
292         inode_lock(inode);
293         error = __vfs_setxattr_locked(dentry, name, value, size, flags,
294             &delegated_inode);
295         inode_unlock(inode);
296
297         if (delegated_inode) {
298                 error = break_deleg_wait(&delegated_inode);
299                 if (!error)
300                         goto retry_deleg;
301         }
302         if (value != orig_value)
303                 kfree(value);
304
305         return error;
306 }
307 EXPORT_SYMBOL_GPL(vfs_setxattr);
308
309 static ssize_t
310 xattr_getsecurity(struct inode *inode, const char *name, void *value,
311                         size_t size)
312 {
313         void *buffer = NULL;
314         ssize_t len;
315
316         if (!value || !size) {
317                 len = security_inode_getsecurity(inode, name, &buffer, false);
318                 goto out_noalloc;
319         }
320
321         len = security_inode_getsecurity(inode, name, &buffer, true);
322         if (len < 0)
323                 return len;
324         if (size < len) {
325                 len = -ERANGE;
326                 goto out;
327         }
328         memcpy(value, buffer, len);
329 out:
330         kfree(buffer);
331 out_noalloc:
332         return len;
333 }
334
335 /*
336  * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
337  *
338  * Allocate memory, if not already allocated, or re-allocate correct size,
339  * before retrieving the extended attribute.
340  *
341  * Returns the result of alloc, if failed, or the getxattr operation.
342  */
343 ssize_t
344 vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
345                    size_t xattr_size, gfp_t flags)
346 {
347         const struct xattr_handler *handler;
348         struct inode *inode = dentry->d_inode;
349         char *value = *xattr_value;
350         int error;
351
352         error = xattr_permission(inode, name, MAY_READ);
353         if (error)
354                 return error;
355
356         handler = xattr_resolve_name(inode, &name);
357         if (IS_ERR(handler))
358                 return PTR_ERR(handler);
359         if (!handler->get)
360                 return -EOPNOTSUPP;
361         error = handler->get(handler, dentry, inode, name, NULL, 0);
362         if (error < 0)
363                 return error;
364
365         if (!value || (error > xattr_size)) {
366                 value = krealloc(*xattr_value, error + 1, flags);
367                 if (!value)
368                         return -ENOMEM;
369                 memset(value, 0, error + 1);
370         }
371
372         error = handler->get(handler, dentry, inode, name, value, error);
373         *xattr_value = value;
374         return error;
375 }
376
377 ssize_t
378 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
379                void *value, size_t size)
380 {
381         const struct xattr_handler *handler;
382
383         handler = xattr_resolve_name(inode, &name);
384         if (IS_ERR(handler))
385                 return PTR_ERR(handler);
386         if (!handler->get)
387                 return -EOPNOTSUPP;
388         return handler->get(handler, dentry, inode, name, value, size);
389 }
390 EXPORT_SYMBOL(__vfs_getxattr);
391
392 ssize_t
393 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
394 {
395         struct inode *inode = dentry->d_inode;
396         int error;
397
398         error = xattr_permission(inode, name, MAY_READ);
399         if (error)
400                 return error;
401
402         error = security_inode_getxattr(dentry, name);
403         if (error)
404                 return error;
405
406         if (!strncmp(name, XATTR_SECURITY_PREFIX,
407                                 XATTR_SECURITY_PREFIX_LEN)) {
408                 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
409                 int ret = xattr_getsecurity(inode, suffix, value, size);
410                 /*
411                  * Only overwrite the return value if a security module
412                  * is actually active.
413                  */
414                 if (ret == -EOPNOTSUPP)
415                         goto nolsm;
416                 return ret;
417         }
418 nolsm:
419         return __vfs_getxattr(dentry, inode, name, value, size);
420 }
421 EXPORT_SYMBOL_GPL(vfs_getxattr);
422
423 ssize_t
424 vfs_listxattr(struct dentry *dentry, char *list, size_t size)
425 {
426         struct inode *inode = d_inode(dentry);
427         ssize_t error;
428
429         error = security_inode_listxattr(dentry);
430         if (error)
431                 return error;
432         if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
433                 error = inode->i_op->listxattr(dentry, list, size);
434         } else {
435                 error = security_inode_listsecurity(inode, list, size);
436                 if (size && error > size)
437                         error = -ERANGE;
438         }
439         return error;
440 }
441 EXPORT_SYMBOL_GPL(vfs_listxattr);
442
443 int
444 __vfs_removexattr(struct dentry *dentry, const char *name)
445 {
446         struct inode *inode = d_inode(dentry);
447         const struct xattr_handler *handler;
448
449         handler = xattr_resolve_name(inode, &name);
450         if (IS_ERR(handler))
451                 return PTR_ERR(handler);
452         if (!handler->set)
453                 return -EOPNOTSUPP;
454         return handler->set(handler, &init_user_ns, dentry, inode, name, NULL,
455                             0, XATTR_REPLACE);
456 }
457 EXPORT_SYMBOL(__vfs_removexattr);
458
459 /**
460  * __vfs_removexattr_locked - set an extended attribute while holding the inode
461  * lock
462  *
463  *  @dentry: object to perform setxattr on
464  *  @name: name of xattr to remove
465  *  @delegated_inode: on return, will contain an inode pointer that
466  *  a delegation was broken on, NULL if none.
467  */
468 int
469 __vfs_removexattr_locked(struct dentry *dentry, const char *name,
470                 struct inode **delegated_inode)
471 {
472         struct inode *inode = dentry->d_inode;
473         int error;
474
475         error = xattr_permission(inode, name, MAY_WRITE);
476         if (error)
477                 return error;
478
479         error = security_inode_removexattr(dentry, name);
480         if (error)
481                 goto out;
482
483         error = try_break_deleg(inode, delegated_inode);
484         if (error)
485                 goto out;
486
487         error = __vfs_removexattr(dentry, name);
488
489         if (!error) {
490                 fsnotify_xattr(dentry);
491                 evm_inode_post_removexattr(dentry, name);
492         }
493
494 out:
495         return error;
496 }
497 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
498
499 int
500 vfs_removexattr(struct dentry *dentry, const char *name)
501 {
502         struct inode *inode = dentry->d_inode;
503         struct inode *delegated_inode = NULL;
504         int error;
505
506 retry_deleg:
507         inode_lock(inode);
508         error = __vfs_removexattr_locked(dentry, name, &delegated_inode);
509         inode_unlock(inode);
510
511         if (delegated_inode) {
512                 error = break_deleg_wait(&delegated_inode);
513                 if (!error)
514                         goto retry_deleg;
515         }
516
517         return error;
518 }
519 EXPORT_SYMBOL_GPL(vfs_removexattr);
520
521 /*
522  * Extended attribute SET operations
523  */
524 static long
525 setxattr(struct dentry *d, const char __user *name, const void __user *value,
526          size_t size, int flags)
527 {
528         int error;
529         void *kvalue = NULL;
530         char kname[XATTR_NAME_MAX + 1];
531
532         if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
533                 return -EINVAL;
534
535         error = strncpy_from_user(kname, name, sizeof(kname));
536         if (error == 0 || error == sizeof(kname))
537                 error = -ERANGE;
538         if (error < 0)
539                 return error;
540
541         if (size) {
542                 if (size > XATTR_SIZE_MAX)
543                         return -E2BIG;
544                 kvalue = kvmalloc(size, GFP_KERNEL);
545                 if (!kvalue)
546                         return -ENOMEM;
547                 if (copy_from_user(kvalue, value, size)) {
548                         error = -EFAULT;
549                         goto out;
550                 }
551                 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
552                     (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
553                         posix_acl_fix_xattr_from_user(&init_user_ns, kvalue,
554                                                       size);
555         }
556
557         error = vfs_setxattr(d, kname, kvalue, size, flags);
558 out:
559         kvfree(kvalue);
560
561         return error;
562 }
563
564 static int path_setxattr(const char __user *pathname,
565                          const char __user *name, const void __user *value,
566                          size_t size, int flags, unsigned int lookup_flags)
567 {
568         struct path path;
569         int error;
570 retry:
571         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
572         if (error)
573                 return error;
574         error = mnt_want_write(path.mnt);
575         if (!error) {
576                 error = setxattr(path.dentry, name, value, size, flags);
577                 mnt_drop_write(path.mnt);
578         }
579         path_put(&path);
580         if (retry_estale(error, lookup_flags)) {
581                 lookup_flags |= LOOKUP_REVAL;
582                 goto retry;
583         }
584         return error;
585 }
586
587 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
588                 const char __user *, name, const void __user *, value,
589                 size_t, size, int, flags)
590 {
591         return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
592 }
593
594 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
595                 const char __user *, name, const void __user *, value,
596                 size_t, size, int, flags)
597 {
598         return path_setxattr(pathname, name, value, size, flags, 0);
599 }
600
601 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
602                 const void __user *,value, size_t, size, int, flags)
603 {
604         struct fd f = fdget(fd);
605         int error = -EBADF;
606
607         if (!f.file)
608                 return error;
609         audit_file(f.file);
610         error = mnt_want_write_file(f.file);
611         if (!error) {
612                 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
613                 mnt_drop_write_file(f.file);
614         }
615         fdput(f);
616         return error;
617 }
618
619 /*
620  * Extended attribute GET operations
621  */
622 static ssize_t
623 getxattr(struct dentry *d, const char __user *name, void __user *value,
624          size_t size)
625 {
626         ssize_t error;
627         void *kvalue = NULL;
628         char kname[XATTR_NAME_MAX + 1];
629
630         error = strncpy_from_user(kname, name, sizeof(kname));
631         if (error == 0 || error == sizeof(kname))
632                 error = -ERANGE;
633         if (error < 0)
634                 return error;
635
636         if (size) {
637                 if (size > XATTR_SIZE_MAX)
638                         size = XATTR_SIZE_MAX;
639                 kvalue = kvzalloc(size, GFP_KERNEL);
640                 if (!kvalue)
641                         return -ENOMEM;
642         }
643
644         error = vfs_getxattr(d, kname, kvalue, size);
645         if (error > 0) {
646                 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
647                     (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
648                         posix_acl_fix_xattr_to_user(&init_user_ns, kvalue,
649                                                     error);
650                 if (size && copy_to_user(value, kvalue, error))
651                         error = -EFAULT;
652         } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
653                 /* The file system tried to returned a value bigger
654                    than XATTR_SIZE_MAX bytes. Not possible. */
655                 error = -E2BIG;
656         }
657
658         kvfree(kvalue);
659
660         return error;
661 }
662
663 static ssize_t path_getxattr(const char __user *pathname,
664                              const char __user *name, void __user *value,
665                              size_t size, unsigned int lookup_flags)
666 {
667         struct path path;
668         ssize_t error;
669 retry:
670         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
671         if (error)
672                 return error;
673         error = getxattr(path.dentry, name, value, size);
674         path_put(&path);
675         if (retry_estale(error, lookup_flags)) {
676                 lookup_flags |= LOOKUP_REVAL;
677                 goto retry;
678         }
679         return error;
680 }
681
682 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
683                 const char __user *, name, void __user *, value, size_t, size)
684 {
685         return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
686 }
687
688 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
689                 const char __user *, name, void __user *, value, size_t, size)
690 {
691         return path_getxattr(pathname, name, value, size, 0);
692 }
693
694 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
695                 void __user *, value, size_t, size)
696 {
697         struct fd f = fdget(fd);
698         ssize_t error = -EBADF;
699
700         if (!f.file)
701                 return error;
702         audit_file(f.file);
703         error = getxattr(f.file->f_path.dentry, name, value, size);
704         fdput(f);
705         return error;
706 }
707
708 /*
709  * Extended attribute LIST operations
710  */
711 static ssize_t
712 listxattr(struct dentry *d, char __user *list, size_t size)
713 {
714         ssize_t error;
715         char *klist = NULL;
716
717         if (size) {
718                 if (size > XATTR_LIST_MAX)
719                         size = XATTR_LIST_MAX;
720                 klist = kvmalloc(size, GFP_KERNEL);
721                 if (!klist)
722                         return -ENOMEM;
723         }
724
725         error = vfs_listxattr(d, klist, size);
726         if (error > 0) {
727                 if (size && copy_to_user(list, klist, error))
728                         error = -EFAULT;
729         } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
730                 /* The file system tried to returned a list bigger
731                    than XATTR_LIST_MAX bytes. Not possible. */
732                 error = -E2BIG;
733         }
734
735         kvfree(klist);
736
737         return error;
738 }
739
740 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
741                               size_t size, unsigned int lookup_flags)
742 {
743         struct path path;
744         ssize_t error;
745 retry:
746         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
747         if (error)
748                 return error;
749         error = listxattr(path.dentry, list, size);
750         path_put(&path);
751         if (retry_estale(error, lookup_flags)) {
752                 lookup_flags |= LOOKUP_REVAL;
753                 goto retry;
754         }
755         return error;
756 }
757
758 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
759                 size_t, size)
760 {
761         return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
762 }
763
764 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
765                 size_t, size)
766 {
767         return path_listxattr(pathname, list, size, 0);
768 }
769
770 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
771 {
772         struct fd f = fdget(fd);
773         ssize_t error = -EBADF;
774
775         if (!f.file)
776                 return error;
777         audit_file(f.file);
778         error = listxattr(f.file->f_path.dentry, list, size);
779         fdput(f);
780         return error;
781 }
782
783 /*
784  * Extended attribute REMOVE operations
785  */
786 static long
787 removexattr(struct dentry *d, const char __user *name)
788 {
789         int error;
790         char kname[XATTR_NAME_MAX + 1];
791
792         error = strncpy_from_user(kname, name, sizeof(kname));
793         if (error == 0 || error == sizeof(kname))
794                 error = -ERANGE;
795         if (error < 0)
796                 return error;
797
798         return vfs_removexattr(d, kname);
799 }
800
801 static int path_removexattr(const char __user *pathname,
802                             const char __user *name, unsigned int lookup_flags)
803 {
804         struct path path;
805         int error;
806 retry:
807         error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
808         if (error)
809                 return error;
810         error = mnt_want_write(path.mnt);
811         if (!error) {
812                 error = removexattr(path.dentry, name);
813                 mnt_drop_write(path.mnt);
814         }
815         path_put(&path);
816         if (retry_estale(error, lookup_flags)) {
817                 lookup_flags |= LOOKUP_REVAL;
818                 goto retry;
819         }
820         return error;
821 }
822
823 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
824                 const char __user *, name)
825 {
826         return path_removexattr(pathname, name, LOOKUP_FOLLOW);
827 }
828
829 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
830                 const char __user *, name)
831 {
832         return path_removexattr(pathname, name, 0);
833 }
834
835 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
836 {
837         struct fd f = fdget(fd);
838         int error = -EBADF;
839
840         if (!f.file)
841                 return error;
842         audit_file(f.file);
843         error = mnt_want_write_file(f.file);
844         if (!error) {
845                 error = removexattr(f.file->f_path.dentry, name);
846                 mnt_drop_write_file(f.file);
847         }
848         fdput(f);
849         return error;
850 }
851
852 /*
853  * Combine the results of the list() operation from every xattr_handler in the
854  * list.
855  */
856 ssize_t
857 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
858 {
859         const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
860         unsigned int size = 0;
861
862         if (!buffer) {
863                 for_each_xattr_handler(handlers, handler) {
864                         if (!handler->name ||
865                             (handler->list && !handler->list(dentry)))
866                                 continue;
867                         size += strlen(handler->name) + 1;
868                 }
869         } else {
870                 char *buf = buffer;
871                 size_t len;
872
873                 for_each_xattr_handler(handlers, handler) {
874                         if (!handler->name ||
875                             (handler->list && !handler->list(dentry)))
876                                 continue;
877                         len = strlen(handler->name);
878                         if (len + 1 > buffer_size)
879                                 return -ERANGE;
880                         memcpy(buf, handler->name, len + 1);
881                         buf += len + 1;
882                         buffer_size -= len + 1;
883                 }
884                 size = buf - buffer;
885         }
886         return size;
887 }
888 EXPORT_SYMBOL(generic_listxattr);
889
890 /**
891  * xattr_full_name  -  Compute full attribute name from suffix
892  *
893  * @handler:    handler of the xattr_handler operation
894  * @name:       name passed to the xattr_handler operation
895  *
896  * The get and set xattr handler operations are called with the remainder of
897  * the attribute name after skipping the handler's prefix: for example, "foo"
898  * is passed to the get operation of a handler with prefix "user." to get
899  * attribute "user.foo".  The full name is still "there" in the name though.
900  *
901  * Note: the list xattr handler operation when called from the vfs is passed a
902  * NULL name; some file systems use this operation internally, with varying
903  * semantics.
904  */
905 const char *xattr_full_name(const struct xattr_handler *handler,
906                             const char *name)
907 {
908         size_t prefix_len = strlen(xattr_prefix(handler));
909
910         return name - prefix_len;
911 }
912 EXPORT_SYMBOL(xattr_full_name);
913
914 /*
915  * Allocate new xattr and copy in the value; but leave the name to callers.
916  */
917 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
918 {
919         struct simple_xattr *new_xattr;
920         size_t len;
921
922         /* wrap around? */
923         len = sizeof(*new_xattr) + size;
924         if (len < sizeof(*new_xattr))
925                 return NULL;
926
927         new_xattr = kvmalloc(len, GFP_KERNEL);
928         if (!new_xattr)
929                 return NULL;
930
931         new_xattr->size = size;
932         memcpy(new_xattr->value, value, size);
933         return new_xattr;
934 }
935
936 /*
937  * xattr GET operation for in-memory/pseudo filesystems
938  */
939 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
940                      void *buffer, size_t size)
941 {
942         struct simple_xattr *xattr;
943         int ret = -ENODATA;
944
945         spin_lock(&xattrs->lock);
946         list_for_each_entry(xattr, &xattrs->head, list) {
947                 if (strcmp(name, xattr->name))
948                         continue;
949
950                 ret = xattr->size;
951                 if (buffer) {
952                         if (size < xattr->size)
953                                 ret = -ERANGE;
954                         else
955                                 memcpy(buffer, xattr->value, xattr->size);
956                 }
957                 break;
958         }
959         spin_unlock(&xattrs->lock);
960         return ret;
961 }
962
963 /**
964  * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
965  * @xattrs: target simple_xattr list
966  * @name: name of the extended attribute
967  * @value: value of the xattr. If %NULL, will remove the attribute.
968  * @size: size of the new xattr
969  * @flags: %XATTR_{CREATE|REPLACE}
970  * @removed_size: returns size of the removed xattr, -1 if none removed
971  *
972  * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
973  * with -EEXIST.  If %XATTR_REPLACE is set, the xattr should exist;
974  * otherwise, fails with -ENODATA.
975  *
976  * Returns 0 on success, -errno on failure.
977  */
978 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
979                      const void *value, size_t size, int flags,
980                      ssize_t *removed_size)
981 {
982         struct simple_xattr *xattr;
983         struct simple_xattr *new_xattr = NULL;
984         int err = 0;
985
986         if (removed_size)
987                 *removed_size = -1;
988
989         /* value == NULL means remove */
990         if (value) {
991                 new_xattr = simple_xattr_alloc(value, size);
992                 if (!new_xattr)
993                         return -ENOMEM;
994
995                 new_xattr->name = kstrdup(name, GFP_KERNEL);
996                 if (!new_xattr->name) {
997                         kvfree(new_xattr);
998                         return -ENOMEM;
999                 }
1000         }
1001
1002         spin_lock(&xattrs->lock);
1003         list_for_each_entry(xattr, &xattrs->head, list) {
1004                 if (!strcmp(name, xattr->name)) {
1005                         if (flags & XATTR_CREATE) {
1006                                 xattr = new_xattr;
1007                                 err = -EEXIST;
1008                         } else if (new_xattr) {
1009                                 list_replace(&xattr->list, &new_xattr->list);
1010                                 if (removed_size)
1011                                         *removed_size = xattr->size;
1012                         } else {
1013                                 list_del(&xattr->list);
1014                                 if (removed_size)
1015                                         *removed_size = xattr->size;
1016                         }
1017                         goto out;
1018                 }
1019         }
1020         if (flags & XATTR_REPLACE) {
1021                 xattr = new_xattr;
1022                 err = -ENODATA;
1023         } else {
1024                 list_add(&new_xattr->list, &xattrs->head);
1025                 xattr = NULL;
1026         }
1027 out:
1028         spin_unlock(&xattrs->lock);
1029         if (xattr) {
1030                 kfree(xattr->name);
1031                 kvfree(xattr);
1032         }
1033         return err;
1034
1035 }
1036
1037 static bool xattr_is_trusted(const char *name)
1038 {
1039         return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1040 }
1041
1042 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1043                           const char *name)
1044 {
1045         size_t len = strlen(name) + 1;
1046         if (*buffer) {
1047                 if (*remaining_size < len)
1048                         return -ERANGE;
1049                 memcpy(*buffer, name, len);
1050                 *buffer += len;
1051         }
1052         *remaining_size -= len;
1053         return 0;
1054 }
1055
1056 /*
1057  * xattr LIST operation for in-memory/pseudo filesystems
1058  */
1059 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1060                           char *buffer, size_t size)
1061 {
1062         bool trusted = capable(CAP_SYS_ADMIN);
1063         struct simple_xattr *xattr;
1064         ssize_t remaining_size = size;
1065         int err = 0;
1066
1067 #ifdef CONFIG_FS_POSIX_ACL
1068         if (IS_POSIXACL(inode)) {
1069                 if (inode->i_acl) {
1070                         err = xattr_list_one(&buffer, &remaining_size,
1071                                              XATTR_NAME_POSIX_ACL_ACCESS);
1072                         if (err)
1073                                 return err;
1074                 }
1075                 if (inode->i_default_acl) {
1076                         err = xattr_list_one(&buffer, &remaining_size,
1077                                              XATTR_NAME_POSIX_ACL_DEFAULT);
1078                         if (err)
1079                                 return err;
1080                 }
1081         }
1082 #endif
1083
1084         spin_lock(&xattrs->lock);
1085         list_for_each_entry(xattr, &xattrs->head, list) {
1086                 /* skip "trusted." attributes for unprivileged callers */
1087                 if (!trusted && xattr_is_trusted(xattr->name))
1088                         continue;
1089
1090                 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1091                 if (err)
1092                         break;
1093         }
1094         spin_unlock(&xattrs->lock);
1095
1096         return err ? err : size - remaining_size;
1097 }
1098
1099 /*
1100  * Adds an extended attribute to the list
1101  */
1102 void simple_xattr_list_add(struct simple_xattrs *xattrs,
1103                            struct simple_xattr *new_xattr)
1104 {
1105         spin_lock(&xattrs->lock);
1106         list_add(&new_xattr->list, &xattrs->head);
1107         spin_unlock(&xattrs->lock);
1108 }