]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/xattr.c
csky: fix syscache.c fallthrough warning
[mirror_ubuntu-hirsute-kernel.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) && !inode_owner_or_capable(inode))
131 return -EPERM;
132 }
133
134 return inode_permission(inode, mask);
135 }
136
137 /*
138 * Look for any handler that deals with the specified namespace.
139 */
140 int
141 xattr_supported_namespace(struct inode *inode, const char *prefix)
142 {
143 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
144 const struct xattr_handler *handler;
145 size_t preflen;
146
147 if (!(inode->i_opflags & IOP_XATTR)) {
148 if (unlikely(is_bad_inode(inode)))
149 return -EIO;
150 return -EOPNOTSUPP;
151 }
152
153 preflen = strlen(prefix);
154
155 for_each_xattr_handler(handlers, handler) {
156 if (!strncmp(xattr_prefix(handler), prefix, preflen))
157 return 0;
158 }
159
160 return -EOPNOTSUPP;
161 }
162 EXPORT_SYMBOL(xattr_supported_namespace);
163
164 int
165 __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
166 const void *value, size_t size, int flags)
167 {
168 const struct xattr_handler *handler;
169
170 handler = xattr_resolve_name(inode, &name);
171 if (IS_ERR(handler))
172 return PTR_ERR(handler);
173 if (!handler->set)
174 return -EOPNOTSUPP;
175 if (size == 0)
176 value = ""; /* empty EA, do not remove */
177 return handler->set(handler, dentry, inode, name, value, size, flags);
178 }
179 EXPORT_SYMBOL(__vfs_setxattr);
180
181 /**
182 * __vfs_setxattr_noperm - perform setxattr operation without performing
183 * permission checks.
184 *
185 * @dentry - object to perform setxattr on
186 * @name - xattr name to set
187 * @value - value to set @name to
188 * @size - size of @value
189 * @flags - flags to pass into filesystem operations
190 *
191 * returns the result of the internal setxattr or setsecurity operations.
192 *
193 * This function requires the caller to lock the inode's i_mutex before it
194 * is executed. It also assumes that the caller will make the appropriate
195 * permission checks.
196 */
197 int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
198 const void *value, size_t size, int flags)
199 {
200 struct inode *inode = dentry->d_inode;
201 int error = -EAGAIN;
202 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
203 XATTR_SECURITY_PREFIX_LEN);
204
205 if (issec)
206 inode->i_flags &= ~S_NOSEC;
207 if (inode->i_opflags & IOP_XATTR) {
208 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
209 if (!error) {
210 fsnotify_xattr(dentry);
211 security_inode_post_setxattr(dentry, name, value,
212 size, flags);
213 }
214 } else {
215 if (unlikely(is_bad_inode(inode)))
216 return -EIO;
217 }
218 if (error == -EAGAIN) {
219 error = -EOPNOTSUPP;
220
221 if (issec) {
222 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
223
224 error = security_inode_setsecurity(inode, suffix, value,
225 size, flags);
226 if (!error)
227 fsnotify_xattr(dentry);
228 }
229 }
230
231 return error;
232 }
233 EXPORT_SYMBOL_GPL(__vfs_setxattr_noperm);
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 EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
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, dentry, inode, name, NULL, 0, XATTR_REPLACE);
455 }
456 EXPORT_SYMBOL(__vfs_removexattr);
457
458 /**
459 * __vfs_removexattr_noperm - perform removexattr operation without
460 * performing permission checks.
461 *
462 * @dentry - object to perform setxattr on
463 * @name - xattr name to set
464 *
465 * returns the result of the internal setxattr or setsecurity operations.
466 *
467 * This function requires the caller to lock the inode's i_mutex before it
468 * is executed. It also assumes that the caller will make the appropriate
469 * permission checks.
470 */
471 int
472 __vfs_removexattr_noperm(struct dentry *dentry, const char *name)
473 {
474 int error;
475
476 error =__vfs_removexattr(dentry, name);
477 if (!error) {
478 fsnotify_xattr(dentry);
479 evm_inode_post_removexattr(dentry, name);
480 }
481 return error;
482 }
483 EXPORT_SYMBOL_GPL(__vfs_removexattr_noperm);
484
485 /**
486 * __vfs_removexattr_locked - set an extended attribute while holding the inode
487 * lock
488 *
489 * @dentry: object to perform setxattr on
490 * @name: name of xattr to remove
491 * @delegated_inode: on return, will contain an inode pointer that
492 * a delegation was broken on, NULL if none.
493 */
494 int
495 __vfs_removexattr_locked(struct dentry *dentry, const char *name,
496 struct inode **delegated_inode)
497 {
498 struct inode *inode = dentry->d_inode;
499 int error;
500
501 error = xattr_permission(inode, name, MAY_WRITE);
502 if (error)
503 return error;
504
505 error = security_inode_removexattr(dentry, name);
506 if (error)
507 goto out;
508
509 error = try_break_deleg(inode, delegated_inode);
510 if (error)
511 goto out;
512
513 error = __vfs_removexattr_noperm(dentry, name);
514
515 out:
516 return error;
517 }
518 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
519
520 int
521 vfs_removexattr(struct dentry *dentry, const char *name)
522 {
523 struct inode *inode = dentry->d_inode;
524 struct inode *delegated_inode = NULL;
525 int error;
526
527 retry_deleg:
528 inode_lock(inode);
529 error = __vfs_removexattr_locked(dentry, name, &delegated_inode);
530 inode_unlock(inode);
531
532 if (delegated_inode) {
533 error = break_deleg_wait(&delegated_inode);
534 if (!error)
535 goto retry_deleg;
536 }
537
538 return error;
539 }
540 EXPORT_SYMBOL_GPL(vfs_removexattr);
541
542 /*
543 * Extended attribute SET operations
544 */
545 static long
546 setxattr(struct dentry *d, const char __user *name, const void __user *value,
547 size_t size, int flags)
548 {
549 int error;
550 void *kvalue = NULL;
551 char kname[XATTR_NAME_MAX + 1];
552
553 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
554 return -EINVAL;
555
556 error = strncpy_from_user(kname, name, sizeof(kname));
557 if (error == 0 || error == sizeof(kname))
558 error = -ERANGE;
559 if (error < 0)
560 return error;
561
562 if (size) {
563 if (size > XATTR_SIZE_MAX)
564 return -E2BIG;
565 kvalue = kvmalloc(size, GFP_KERNEL);
566 if (!kvalue)
567 return -ENOMEM;
568 if (copy_from_user(kvalue, value, size)) {
569 error = -EFAULT;
570 goto out;
571 }
572 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
573 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
574 posix_acl_fix_xattr_from_user(kvalue, size);
575 }
576
577 error = vfs_setxattr(d, kname, kvalue, size, flags);
578 out:
579 kvfree(kvalue);
580
581 return error;
582 }
583
584 static int path_setxattr(const char __user *pathname,
585 const char __user *name, const void __user *value,
586 size_t size, int flags, unsigned int lookup_flags)
587 {
588 struct path path;
589 int error;
590 retry:
591 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
592 if (error)
593 return error;
594 error = mnt_want_write(path.mnt);
595 if (!error) {
596 error = setxattr(path.dentry, name, value, size, flags);
597 mnt_drop_write(path.mnt);
598 }
599 path_put(&path);
600 if (retry_estale(error, lookup_flags)) {
601 lookup_flags |= LOOKUP_REVAL;
602 goto retry;
603 }
604 return error;
605 }
606
607 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
608 const char __user *, name, const void __user *, value,
609 size_t, size, int, flags)
610 {
611 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
612 }
613
614 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
615 const char __user *, name, const void __user *, value,
616 size_t, size, int, flags)
617 {
618 return path_setxattr(pathname, name, value, size, flags, 0);
619 }
620
621 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
622 const void __user *,value, size_t, size, int, flags)
623 {
624 struct fd f = fdget(fd);
625 int error = -EBADF;
626
627 if (!f.file)
628 return error;
629 audit_file(f.file);
630 error = mnt_want_write_file(f.file);
631 if (!error) {
632 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
633 mnt_drop_write_file(f.file);
634 }
635 fdput(f);
636 return error;
637 }
638
639 /*
640 * Extended attribute GET operations
641 */
642 static ssize_t
643 getxattr(struct dentry *d, const char __user *name, void __user *value,
644 size_t size)
645 {
646 ssize_t error;
647 void *kvalue = NULL;
648 char kname[XATTR_NAME_MAX + 1];
649
650 error = strncpy_from_user(kname, name, sizeof(kname));
651 if (error == 0 || error == sizeof(kname))
652 error = -ERANGE;
653 if (error < 0)
654 return error;
655
656 if (size) {
657 if (size > XATTR_SIZE_MAX)
658 size = XATTR_SIZE_MAX;
659 kvalue = kvzalloc(size, GFP_KERNEL);
660 if (!kvalue)
661 return -ENOMEM;
662 }
663
664 error = vfs_getxattr(d, kname, kvalue, size);
665 if (error > 0) {
666 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
667 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
668 posix_acl_fix_xattr_to_user(kvalue, error);
669 if (size && copy_to_user(value, kvalue, error))
670 error = -EFAULT;
671 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
672 /* The file system tried to returned a value bigger
673 than XATTR_SIZE_MAX bytes. Not possible. */
674 error = -E2BIG;
675 }
676
677 kvfree(kvalue);
678
679 return error;
680 }
681
682 static ssize_t path_getxattr(const char __user *pathname,
683 const char __user *name, void __user *value,
684 size_t size, unsigned int lookup_flags)
685 {
686 struct path path;
687 ssize_t error;
688 retry:
689 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
690 if (error)
691 return error;
692 error = getxattr(path.dentry, name, value, size);
693 path_put(&path);
694 if (retry_estale(error, lookup_flags)) {
695 lookup_flags |= LOOKUP_REVAL;
696 goto retry;
697 }
698 return error;
699 }
700
701 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
702 const char __user *, name, void __user *, value, size_t, size)
703 {
704 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
705 }
706
707 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
708 const char __user *, name, void __user *, value, size_t, size)
709 {
710 return path_getxattr(pathname, name, value, size, 0);
711 }
712
713 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
714 void __user *, value, size_t, size)
715 {
716 struct fd f = fdget(fd);
717 ssize_t error = -EBADF;
718
719 if (!f.file)
720 return error;
721 audit_file(f.file);
722 error = getxattr(f.file->f_path.dentry, name, value, size);
723 fdput(f);
724 return error;
725 }
726
727 /*
728 * Extended attribute LIST operations
729 */
730 static ssize_t
731 listxattr(struct dentry *d, char __user *list, size_t size)
732 {
733 ssize_t error;
734 char *klist = NULL;
735
736 if (size) {
737 if (size > XATTR_LIST_MAX)
738 size = XATTR_LIST_MAX;
739 klist = kvmalloc(size, GFP_KERNEL);
740 if (!klist)
741 return -ENOMEM;
742 }
743
744 error = vfs_listxattr(d, klist, size);
745 if (error > 0) {
746 if (size && copy_to_user(list, klist, error))
747 error = -EFAULT;
748 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
749 /* The file system tried to returned a list bigger
750 than XATTR_LIST_MAX bytes. Not possible. */
751 error = -E2BIG;
752 }
753
754 kvfree(klist);
755
756 return error;
757 }
758
759 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
760 size_t size, unsigned int lookup_flags)
761 {
762 struct path path;
763 ssize_t error;
764 retry:
765 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
766 if (error)
767 return error;
768 error = listxattr(path.dentry, list, size);
769 path_put(&path);
770 if (retry_estale(error, lookup_flags)) {
771 lookup_flags |= LOOKUP_REVAL;
772 goto retry;
773 }
774 return error;
775 }
776
777 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
778 size_t, size)
779 {
780 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
781 }
782
783 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
784 size_t, size)
785 {
786 return path_listxattr(pathname, list, size, 0);
787 }
788
789 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
790 {
791 struct fd f = fdget(fd);
792 ssize_t error = -EBADF;
793
794 if (!f.file)
795 return error;
796 audit_file(f.file);
797 error = listxattr(f.file->f_path.dentry, list, size);
798 fdput(f);
799 return error;
800 }
801
802 /*
803 * Extended attribute REMOVE operations
804 */
805 static long
806 removexattr(struct dentry *d, const char __user *name)
807 {
808 int error;
809 char kname[XATTR_NAME_MAX + 1];
810
811 error = strncpy_from_user(kname, name, sizeof(kname));
812 if (error == 0 || error == sizeof(kname))
813 error = -ERANGE;
814 if (error < 0)
815 return error;
816
817 return vfs_removexattr(d, kname);
818 }
819
820 static int path_removexattr(const char __user *pathname,
821 const char __user *name, unsigned int lookup_flags)
822 {
823 struct path path;
824 int error;
825 retry:
826 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
827 if (error)
828 return error;
829 error = mnt_want_write(path.mnt);
830 if (!error) {
831 error = removexattr(path.dentry, name);
832 mnt_drop_write(path.mnt);
833 }
834 path_put(&path);
835 if (retry_estale(error, lookup_flags)) {
836 lookup_flags |= LOOKUP_REVAL;
837 goto retry;
838 }
839 return error;
840 }
841
842 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
843 const char __user *, name)
844 {
845 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
846 }
847
848 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
849 const char __user *, name)
850 {
851 return path_removexattr(pathname, name, 0);
852 }
853
854 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
855 {
856 struct fd f = fdget(fd);
857 int error = -EBADF;
858
859 if (!f.file)
860 return error;
861 audit_file(f.file);
862 error = mnt_want_write_file(f.file);
863 if (!error) {
864 error = removexattr(f.file->f_path.dentry, name);
865 mnt_drop_write_file(f.file);
866 }
867 fdput(f);
868 return error;
869 }
870
871 /*
872 * Combine the results of the list() operation from every xattr_handler in the
873 * list.
874 */
875 ssize_t
876 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
877 {
878 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
879 unsigned int size = 0;
880
881 if (!buffer) {
882 for_each_xattr_handler(handlers, handler) {
883 if (!handler->name ||
884 (handler->list && !handler->list(dentry)))
885 continue;
886 size += strlen(handler->name) + 1;
887 }
888 } else {
889 char *buf = buffer;
890 size_t len;
891
892 for_each_xattr_handler(handlers, handler) {
893 if (!handler->name ||
894 (handler->list && !handler->list(dentry)))
895 continue;
896 len = strlen(handler->name);
897 if (len + 1 > buffer_size)
898 return -ERANGE;
899 memcpy(buf, handler->name, len + 1);
900 buf += len + 1;
901 buffer_size -= len + 1;
902 }
903 size = buf - buffer;
904 }
905 return size;
906 }
907 EXPORT_SYMBOL(generic_listxattr);
908
909 /**
910 * xattr_full_name - Compute full attribute name from suffix
911 *
912 * @handler: handler of the xattr_handler operation
913 * @name: name passed to the xattr_handler operation
914 *
915 * The get and set xattr handler operations are called with the remainder of
916 * the attribute name after skipping the handler's prefix: for example, "foo"
917 * is passed to the get operation of a handler with prefix "user." to get
918 * attribute "user.foo". The full name is still "there" in the name though.
919 *
920 * Note: the list xattr handler operation when called from the vfs is passed a
921 * NULL name; some file systems use this operation internally, with varying
922 * semantics.
923 */
924 const char *xattr_full_name(const struct xattr_handler *handler,
925 const char *name)
926 {
927 size_t prefix_len = strlen(xattr_prefix(handler));
928
929 return name - prefix_len;
930 }
931 EXPORT_SYMBOL(xattr_full_name);
932
933 /*
934 * Allocate new xattr and copy in the value; but leave the name to callers.
935 */
936 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
937 {
938 struct simple_xattr *new_xattr;
939 size_t len;
940
941 /* wrap around? */
942 len = sizeof(*new_xattr) + size;
943 if (len < sizeof(*new_xattr))
944 return NULL;
945
946 new_xattr = kvmalloc(len, GFP_KERNEL);
947 if (!new_xattr)
948 return NULL;
949
950 new_xattr->size = size;
951 memcpy(new_xattr->value, value, size);
952 return new_xattr;
953 }
954
955 /*
956 * xattr GET operation for in-memory/pseudo filesystems
957 */
958 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
959 void *buffer, size_t size)
960 {
961 struct simple_xattr *xattr;
962 int ret = -ENODATA;
963
964 spin_lock(&xattrs->lock);
965 list_for_each_entry(xattr, &xattrs->head, list) {
966 if (strcmp(name, xattr->name))
967 continue;
968
969 ret = xattr->size;
970 if (buffer) {
971 if (size < xattr->size)
972 ret = -ERANGE;
973 else
974 memcpy(buffer, xattr->value, xattr->size);
975 }
976 break;
977 }
978 spin_unlock(&xattrs->lock);
979 return ret;
980 }
981
982 /**
983 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
984 * @xattrs: target simple_xattr list
985 * @name: name of the extended attribute
986 * @value: value of the xattr. If %NULL, will remove the attribute.
987 * @size: size of the new xattr
988 * @flags: %XATTR_{CREATE|REPLACE}
989 * @removed_size: returns size of the removed xattr, -1 if none removed
990 *
991 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
992 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
993 * otherwise, fails with -ENODATA.
994 *
995 * Returns 0 on success, -errno on failure.
996 */
997 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
998 const void *value, size_t size, int flags,
999 ssize_t *removed_size)
1000 {
1001 struct simple_xattr *xattr;
1002 struct simple_xattr *new_xattr = NULL;
1003 int err = 0;
1004
1005 if (removed_size)
1006 *removed_size = -1;
1007
1008 /* value == NULL means remove */
1009 if (value) {
1010 new_xattr = simple_xattr_alloc(value, size);
1011 if (!new_xattr)
1012 return -ENOMEM;
1013
1014 new_xattr->name = kstrdup(name, GFP_KERNEL);
1015 if (!new_xattr->name) {
1016 kvfree(new_xattr);
1017 return -ENOMEM;
1018 }
1019 }
1020
1021 spin_lock(&xattrs->lock);
1022 list_for_each_entry(xattr, &xattrs->head, list) {
1023 if (!strcmp(name, xattr->name)) {
1024 if (flags & XATTR_CREATE) {
1025 xattr = new_xattr;
1026 err = -EEXIST;
1027 } else if (new_xattr) {
1028 list_replace(&xattr->list, &new_xattr->list);
1029 if (removed_size)
1030 *removed_size = xattr->size;
1031 } else {
1032 list_del(&xattr->list);
1033 if (removed_size)
1034 *removed_size = xattr->size;
1035 }
1036 goto out;
1037 }
1038 }
1039 if (flags & XATTR_REPLACE) {
1040 xattr = new_xattr;
1041 err = -ENODATA;
1042 } else {
1043 list_add(&new_xattr->list, &xattrs->head);
1044 xattr = NULL;
1045 }
1046 out:
1047 spin_unlock(&xattrs->lock);
1048 if (xattr) {
1049 kfree(xattr->name);
1050 kvfree(xattr);
1051 }
1052 return err;
1053
1054 }
1055
1056 static bool xattr_is_trusted(const char *name)
1057 {
1058 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1059 }
1060
1061 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1062 const char *name)
1063 {
1064 size_t len = strlen(name) + 1;
1065 if (*buffer) {
1066 if (*remaining_size < len)
1067 return -ERANGE;
1068 memcpy(*buffer, name, len);
1069 *buffer += len;
1070 }
1071 *remaining_size -= len;
1072 return 0;
1073 }
1074
1075 /*
1076 * xattr LIST operation for in-memory/pseudo filesystems
1077 */
1078 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1079 char *buffer, size_t size)
1080 {
1081 bool trusted = capable(CAP_SYS_ADMIN);
1082 struct simple_xattr *xattr;
1083 ssize_t remaining_size = size;
1084 int err = 0;
1085
1086 #ifdef CONFIG_FS_POSIX_ACL
1087 if (IS_POSIXACL(inode)) {
1088 if (inode->i_acl) {
1089 err = xattr_list_one(&buffer, &remaining_size,
1090 XATTR_NAME_POSIX_ACL_ACCESS);
1091 if (err)
1092 return err;
1093 }
1094 if (inode->i_default_acl) {
1095 err = xattr_list_one(&buffer, &remaining_size,
1096 XATTR_NAME_POSIX_ACL_DEFAULT);
1097 if (err)
1098 return err;
1099 }
1100 }
1101 #endif
1102
1103 spin_lock(&xattrs->lock);
1104 list_for_each_entry(xattr, &xattrs->head, list) {
1105 /* skip "trusted." attributes for unprivileged callers */
1106 if (!trusted && xattr_is_trusted(xattr->name))
1107 continue;
1108
1109 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1110 if (err)
1111 break;
1112 }
1113 spin_unlock(&xattrs->lock);
1114
1115 return err ? err : size - remaining_size;
1116 }
1117
1118 /*
1119 * Adds an extended attribute to the list
1120 */
1121 void simple_xattr_list_add(struct simple_xattrs *xattrs,
1122 struct simple_xattr *new_xattr)
1123 {
1124 spin_lock(&xattrs->lock);
1125 list_add(&new_xattr->list, &xattrs->head);
1126 spin_unlock(&xattrs->lock);
1127 }