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