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