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