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