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