]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xattr.c
security: imbed evm calls in security hooks
[mirror_ubuntu-bionic-kernel.git] / fs / xattr.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
12#include <linux/file.h>
13#include <linux/xattr.h>
18f335af 14#include <linux/mount.h>
1da177e4
LT
15#include <linux/namei.h>
16#include <linux/security.h>
17#include <linux/syscalls.h>
18#include <linux/module.h>
0eeca283 19#include <linux/fsnotify.h>
73241ccc 20#include <linux/audit.h>
1da177e4
LT
21#include <asm/uaccess.h>
22
5be196e5 23
e0ad7b07 24/*
25 * Check permissions for extended attribute access. This is a bit complicated
26 * because different namespaces have very different rules.
27 */
28static int
29xattr_permission(struct inode *inode, const char *name, int mask)
30{
31 /*
32 * We can never set or remove an extended attribute on a read-only
33 * filesystem or on an immutable / append-only inode.
34 */
35 if (mask & MAY_WRITE) {
e0ad7b07 36 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
37 return -EPERM;
38 }
39
40 /*
41 * No restriction for security.* and system.* from the VFS. Decision
42 * on these is left to the underlying filesystem / security module.
43 */
44 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
45 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
46 return 0;
47
48 /*
55b23bde 49 * The trusted.* namespace can only be accessed by privileged users.
e0ad7b07 50 */
55b23bde
AG
51 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
52 if (!capable(CAP_SYS_ADMIN))
53 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
54 return 0;
55 }
e0ad7b07 56
55b23bde
AG
57 /*
58 * In the user.* namespace, only regular files and directories can have
f1f2d871 59 * extended attributes. For sticky directories, only the owner and
55b23bde 60 * privileged users can write attributes.
f1f2d871 61 */
e0ad7b07 62 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
f1f2d871 63 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
55b23bde 64 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
f1f2d871 65 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2e149670 66 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
e0ad7b07 67 return -EPERM;
68 }
69
f419a2e3 70 return inode_permission(inode, mask);
e0ad7b07 71}
72
b1ab7e4b
DQ
73/**
74 * __vfs_setxattr_noperm - perform setxattr operation without performing
75 * permission checks.
76 *
77 * @dentry - object to perform setxattr on
78 * @name - xattr name to set
79 * @value - value to set @name to
80 * @size - size of @value
81 * @flags - flags to pass into filesystem operations
82 *
83 * returns the result of the internal setxattr or setsecurity operations.
84 *
85 * This function requires the caller to lock the inode's i_mutex before it
86 * is executed. It also assumes that the caller will make the appropriate
87 * permission checks.
88 */
89int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
90 const void *value, size_t size, int flags)
5be196e5
CH
91{
92 struct inode *inode = dentry->d_inode;
b1ab7e4b 93 int error = -EOPNOTSUPP;
69b45732
AK
94 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
95 XATTR_SECURITY_PREFIX_LEN);
e0ad7b07 96
69b45732
AK
97 if (issec)
98 inode->i_flags &= ~S_NOSEC;
5be196e5
CH
99 if (inode->i_op->setxattr) {
100 error = inode->i_op->setxattr(dentry, name, value, size, flags);
101 if (!error) {
102 fsnotify_xattr(dentry);
103 security_inode_post_setxattr(dentry, name, value,
104 size, flags);
105 }
69b45732 106 } else if (issec) {
e0ad7b07 107 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
5be196e5
CH
108 error = security_inode_setsecurity(inode, suffix, value,
109 size, flags);
110 if (!error)
111 fsnotify_xattr(dentry);
112 }
b1ab7e4b
DQ
113
114 return error;
115}
116
117
118int
119vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
120 size_t size, int flags)
121{
122 struct inode *inode = dentry->d_inode;
123 int error;
124
125 error = xattr_permission(inode, name, MAY_WRITE);
126 if (error)
127 return error;
128
129 mutex_lock(&inode->i_mutex);
130 error = security_inode_setxattr(dentry, name, value, size, flags);
131 if (error)
132 goto out;
133
134 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
135
5be196e5
CH
136out:
137 mutex_unlock(&inode->i_mutex);
138 return error;
139}
140EXPORT_SYMBOL_GPL(vfs_setxattr);
141
42492594
DQ
142ssize_t
143xattr_getsecurity(struct inode *inode, const char *name, void *value,
144 size_t size)
145{
146 void *buffer = NULL;
147 ssize_t len;
148
149 if (!value || !size) {
150 len = security_inode_getsecurity(inode, name, &buffer, false);
151 goto out_noalloc;
152 }
153
154 len = security_inode_getsecurity(inode, name, &buffer, true);
155 if (len < 0)
156 return len;
157 if (size < len) {
158 len = -ERANGE;
159 goto out;
160 }
161 memcpy(value, buffer, len);
162out:
163 security_release_secctx(buffer, len);
164out_noalloc:
165 return len;
166}
167EXPORT_SYMBOL_GPL(xattr_getsecurity);
168
1601fbad
MZ
169/*
170 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
171 *
172 * Allocate memory, if not already allocated, or re-allocate correct size,
173 * before retrieving the extended attribute.
174 *
175 * Returns the result of alloc, if failed, or the getxattr operation.
176 */
177ssize_t
178vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
179 size_t xattr_size, gfp_t flags)
180{
181 struct inode *inode = dentry->d_inode;
182 char *value = *xattr_value;
183 int error;
184
185 error = xattr_permission(inode, name, MAY_READ);
186 if (error)
187 return error;
188
189 if (!inode->i_op->getxattr)
190 return -EOPNOTSUPP;
191
192 error = inode->i_op->getxattr(dentry, name, NULL, 0);
193 if (error < 0)
194 return error;
195
196 if (!value || (error > xattr_size)) {
197 value = krealloc(*xattr_value, error + 1, flags);
198 if (!value)
199 return -ENOMEM;
200 memset(value, 0, error + 1);
201 }
202
203 error = inode->i_op->getxattr(dentry, name, value, error);
204 *xattr_value = value;
205 return error;
206}
207
208/* Compare an extended attribute value with the given value */
209int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
210 const char *value, size_t size, gfp_t flags)
211{
212 char *xattr_value = NULL;
213 int rc;
214
215 rc = vfs_getxattr_alloc(dentry, xattr_name, &xattr_value, 0, flags);
216 if (rc < 0)
217 return rc;
218
219 if ((rc != size) || (memcmp(xattr_value, value, rc) != 0))
220 rc = -EINVAL;
221 else
222 rc = 0;
223 kfree(xattr_value);
224 return rc;
225}
226
5be196e5 227ssize_t
8f0cfa52 228vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
5be196e5
CH
229{
230 struct inode *inode = dentry->d_inode;
231 int error;
232
e0ad7b07 233 error = xattr_permission(inode, name, MAY_READ);
234 if (error)
235 return error;
236
5be196e5
CH
237 error = security_inode_getxattr(dentry, name);
238 if (error)
239 return error;
240
5be196e5 241 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 242 XATTR_SECURITY_PREFIX_LEN)) {
243 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
42492594 244 int ret = xattr_getsecurity(inode, suffix, value, size);
5be196e5
CH
245 /*
246 * Only overwrite the return value if a security module
247 * is actually active.
248 */
4bea5805
DQ
249 if (ret == -EOPNOTSUPP)
250 goto nolsm;
251 return ret;
5be196e5 252 }
4bea5805
DQ
253nolsm:
254 if (inode->i_op->getxattr)
255 error = inode->i_op->getxattr(dentry, name, value, size);
256 else
257 error = -EOPNOTSUPP;
5be196e5
CH
258
259 return error;
260}
261EXPORT_SYMBOL_GPL(vfs_getxattr);
262
659564c8
BN
263ssize_t
264vfs_listxattr(struct dentry *d, char *list, size_t size)
265{
266 ssize_t error;
267
268 error = security_inode_listxattr(d);
269 if (error)
270 return error;
271 error = -EOPNOTSUPP;
acfa4380 272 if (d->d_inode->i_op->listxattr) {
659564c8
BN
273 error = d->d_inode->i_op->listxattr(d, list, size);
274 } else {
275 error = security_inode_listsecurity(d->d_inode, list, size);
276 if (size && error > size)
277 error = -ERANGE;
278 }
279 return error;
280}
281EXPORT_SYMBOL_GPL(vfs_listxattr);
282
5be196e5 283int
8f0cfa52 284vfs_removexattr(struct dentry *dentry, const char *name)
5be196e5
CH
285{
286 struct inode *inode = dentry->d_inode;
287 int error;
288
289 if (!inode->i_op->removexattr)
290 return -EOPNOTSUPP;
291
e0ad7b07 292 error = xattr_permission(inode, name, MAY_WRITE);
293 if (error)
294 return error;
295
5be196e5
CH
296 error = security_inode_removexattr(dentry, name);
297 if (error)
298 return error;
299
300 mutex_lock(&inode->i_mutex);
301 error = inode->i_op->removexattr(dentry, name);
302 mutex_unlock(&inode->i_mutex);
303
304 if (!error)
305 fsnotify_xattr(dentry);
306 return error;
307}
308EXPORT_SYMBOL_GPL(vfs_removexattr);
309
310
1da177e4
LT
311/*
312 * Extended attribute SET operations
313 */
314static long
8f0cfa52 315setxattr(struct dentry *d, const char __user *name, const void __user *value,
1da177e4
LT
316 size_t size, int flags)
317{
318 int error;
319 void *kvalue = NULL;
320 char kname[XATTR_NAME_MAX + 1];
321
322 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
323 return -EINVAL;
324
325 error = strncpy_from_user(kname, name, sizeof(kname));
326 if (error == 0 || error == sizeof(kname))
327 error = -ERANGE;
328 if (error < 0)
329 return error;
330
331 if (size) {
332 if (size > XATTR_SIZE_MAX)
333 return -E2BIG;
3939fcde
LZ
334 kvalue = memdup_user(value, size);
335 if (IS_ERR(kvalue))
336 return PTR_ERR(kvalue);
1da177e4
LT
337 }
338
5be196e5 339 error = vfs_setxattr(d, kname, kvalue, size, flags);
f99d49ad 340 kfree(kvalue);
1da177e4
LT
341 return error;
342}
343
64fd1de3
HC
344SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
345 const char __user *, name, const void __user *, value,
346 size_t, size, int, flags)
1da177e4 347{
2d8f3038 348 struct path path;
1da177e4
LT
349 int error;
350
2d8f3038 351 error = user_path(pathname, &path);
1da177e4
LT
352 if (error)
353 return error;
2d8f3038 354 error = mnt_want_write(path.mnt);
18f335af 355 if (!error) {
2d8f3038
AV
356 error = setxattr(path.dentry, name, value, size, flags);
357 mnt_drop_write(path.mnt);
18f335af 358 }
2d8f3038 359 path_put(&path);
1da177e4
LT
360 return error;
361}
362
64fd1de3
HC
363SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
364 const char __user *, name, const void __user *, value,
365 size_t, size, int, flags)
1da177e4 366{
2d8f3038 367 struct path path;
1da177e4
LT
368 int error;
369
2d8f3038 370 error = user_lpath(pathname, &path);
1da177e4
LT
371 if (error)
372 return error;
2d8f3038 373 error = mnt_want_write(path.mnt);
18f335af 374 if (!error) {
2d8f3038
AV
375 error = setxattr(path.dentry, name, value, size, flags);
376 mnt_drop_write(path.mnt);
18f335af 377 }
2d8f3038 378 path_put(&path);
1da177e4
LT
379 return error;
380}
381
64fd1de3
HC
382SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
383 const void __user *,value, size_t, size, int, flags)
1da177e4
LT
384{
385 struct file *f;
73241ccc 386 struct dentry *dentry;
1da177e4
LT
387 int error = -EBADF;
388
389 f = fget(fd);
390 if (!f)
391 return error;
0f7fc9e4 392 dentry = f->f_path.dentry;
5a190ae6 393 audit_inode(NULL, dentry);
96029c4e 394 error = mnt_want_write_file(f);
18f335af
DH
395 if (!error) {
396 error = setxattr(dentry, name, value, size, flags);
397 mnt_drop_write(f->f_path.mnt);
398 }
1da177e4
LT
399 fput(f);
400 return error;
401}
402
403/*
404 * Extended attribute GET operations
405 */
406static ssize_t
8f0cfa52
DH
407getxattr(struct dentry *d, const char __user *name, void __user *value,
408 size_t size)
1da177e4
LT
409{
410 ssize_t error;
411 void *kvalue = NULL;
412 char kname[XATTR_NAME_MAX + 1];
413
414 error = strncpy_from_user(kname, name, sizeof(kname));
415 if (error == 0 || error == sizeof(kname))
416 error = -ERANGE;
417 if (error < 0)
418 return error;
419
420 if (size) {
421 if (size > XATTR_SIZE_MAX)
422 size = XATTR_SIZE_MAX;
d381d8a9 423 kvalue = kzalloc(size, GFP_KERNEL);
1da177e4
LT
424 if (!kvalue)
425 return -ENOMEM;
426 }
427
5be196e5 428 error = vfs_getxattr(d, kname, kvalue, size);
f549d6c1
SS
429 if (error > 0) {
430 if (size && copy_to_user(value, kvalue, error))
431 error = -EFAULT;
432 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
433 /* The file system tried to returned a value bigger
434 than XATTR_SIZE_MAX bytes. Not possible. */
435 error = -E2BIG;
1da177e4 436 }
f99d49ad 437 kfree(kvalue);
1da177e4
LT
438 return error;
439}
440
64fd1de3
HC
441SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
442 const char __user *, name, void __user *, value, size_t, size)
1da177e4 443{
2d8f3038 444 struct path path;
1da177e4
LT
445 ssize_t error;
446
2d8f3038 447 error = user_path(pathname, &path);
1da177e4
LT
448 if (error)
449 return error;
2d8f3038
AV
450 error = getxattr(path.dentry, name, value, size);
451 path_put(&path);
1da177e4
LT
452 return error;
453}
454
64fd1de3
HC
455SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
456 const char __user *, name, void __user *, value, size_t, size)
1da177e4 457{
2d8f3038 458 struct path path;
1da177e4
LT
459 ssize_t error;
460
2d8f3038 461 error = user_lpath(pathname, &path);
1da177e4
LT
462 if (error)
463 return error;
2d8f3038
AV
464 error = getxattr(path.dentry, name, value, size);
465 path_put(&path);
1da177e4
LT
466 return error;
467}
468
64fd1de3
HC
469SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
470 void __user *, value, size_t, size)
1da177e4
LT
471{
472 struct file *f;
473 ssize_t error = -EBADF;
474
475 f = fget(fd);
476 if (!f)
477 return error;
5a190ae6 478 audit_inode(NULL, f->f_path.dentry);
0f7fc9e4 479 error = getxattr(f->f_path.dentry, name, value, size);
1da177e4
LT
480 fput(f);
481 return error;
482}
483
484/*
485 * Extended attribute LIST operations
486 */
487static ssize_t
488listxattr(struct dentry *d, char __user *list, size_t size)
489{
490 ssize_t error;
491 char *klist = NULL;
492
493 if (size) {
494 if (size > XATTR_LIST_MAX)
495 size = XATTR_LIST_MAX;
496 klist = kmalloc(size, GFP_KERNEL);
497 if (!klist)
498 return -ENOMEM;
499 }
500
659564c8 501 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
502 if (error > 0) {
503 if (size && copy_to_user(list, klist, error))
504 error = -EFAULT;
505 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
506 /* The file system tried to returned a list bigger
507 than XATTR_LIST_MAX bytes. Not possible. */
508 error = -E2BIG;
1da177e4 509 }
f99d49ad 510 kfree(klist);
1da177e4
LT
511 return error;
512}
513
64fd1de3
HC
514SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
515 size_t, size)
1da177e4 516{
2d8f3038 517 struct path path;
1da177e4
LT
518 ssize_t error;
519
2d8f3038 520 error = user_path(pathname, &path);
1da177e4
LT
521 if (error)
522 return error;
2d8f3038
AV
523 error = listxattr(path.dentry, list, size);
524 path_put(&path);
1da177e4
LT
525 return error;
526}
527
64fd1de3
HC
528SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
529 size_t, size)
1da177e4 530{
2d8f3038 531 struct path path;
1da177e4
LT
532 ssize_t error;
533
2d8f3038 534 error = user_lpath(pathname, &path);
1da177e4
LT
535 if (error)
536 return error;
2d8f3038
AV
537 error = listxattr(path.dentry, list, size);
538 path_put(&path);
1da177e4
LT
539 return error;
540}
541
64fd1de3 542SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4
LT
543{
544 struct file *f;
545 ssize_t error = -EBADF;
546
547 f = fget(fd);
548 if (!f)
549 return error;
5a190ae6 550 audit_inode(NULL, f->f_path.dentry);
0f7fc9e4 551 error = listxattr(f->f_path.dentry, list, size);
1da177e4
LT
552 fput(f);
553 return error;
554}
555
556/*
557 * Extended attribute REMOVE operations
558 */
559static long
8f0cfa52 560removexattr(struct dentry *d, const char __user *name)
1da177e4
LT
561{
562 int error;
563 char kname[XATTR_NAME_MAX + 1];
564
565 error = strncpy_from_user(kname, name, sizeof(kname));
566 if (error == 0 || error == sizeof(kname))
567 error = -ERANGE;
568 if (error < 0)
569 return error;
570
5be196e5 571 return vfs_removexattr(d, kname);
1da177e4
LT
572}
573
64fd1de3
HC
574SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
575 const char __user *, name)
1da177e4 576{
2d8f3038 577 struct path path;
1da177e4
LT
578 int error;
579
2d8f3038 580 error = user_path(pathname, &path);
1da177e4
LT
581 if (error)
582 return error;
2d8f3038 583 error = mnt_want_write(path.mnt);
18f335af 584 if (!error) {
2d8f3038
AV
585 error = removexattr(path.dentry, name);
586 mnt_drop_write(path.mnt);
18f335af 587 }
2d8f3038 588 path_put(&path);
1da177e4
LT
589 return error;
590}
591
6a6160a7
HC
592SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
593 const char __user *, name)
1da177e4 594{
2d8f3038 595 struct path path;
1da177e4
LT
596 int error;
597
2d8f3038 598 error = user_lpath(pathname, &path);
1da177e4
LT
599 if (error)
600 return error;
2d8f3038 601 error = mnt_want_write(path.mnt);
18f335af 602 if (!error) {
2d8f3038
AV
603 error = removexattr(path.dentry, name);
604 mnt_drop_write(path.mnt);
18f335af 605 }
2d8f3038 606 path_put(&path);
1da177e4
LT
607 return error;
608}
609
6a6160a7 610SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4
LT
611{
612 struct file *f;
73241ccc 613 struct dentry *dentry;
1da177e4
LT
614 int error = -EBADF;
615
616 f = fget(fd);
617 if (!f)
618 return error;
0f7fc9e4 619 dentry = f->f_path.dentry;
5a190ae6 620 audit_inode(NULL, dentry);
96029c4e 621 error = mnt_want_write_file(f);
18f335af
DH
622 if (!error) {
623 error = removexattr(dentry, name);
624 mnt_drop_write(f->f_path.mnt);
625 }
1da177e4
LT
626 fput(f);
627 return error;
628}
629
630
631static const char *
632strcmp_prefix(const char *a, const char *a_prefix)
633{
634 while (*a_prefix && *a == *a_prefix) {
635 a++;
636 a_prefix++;
637 }
638 return *a_prefix ? NULL : a;
639}
640
641/*
642 * In order to implement different sets of xattr operations for each xattr
643 * prefix with the generic xattr API, a filesystem should create a
644 * null-terminated array of struct xattr_handler (one for each prefix) and
645 * hang a pointer to it off of the s_xattr field of the superblock.
646 *
647 * The generic_fooxattr() functions will use this list to dispatch xattr
648 * operations to the correct xattr_handler.
649 */
650#define for_each_xattr_handler(handlers, handler) \
651 for ((handler) = *(handlers)++; \
652 (handler) != NULL; \
653 (handler) = *(handlers)++)
654
655/*
656 * Find the xattr_handler with the matching prefix.
657 */
bb435453
SH
658static const struct xattr_handler *
659xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
1da177e4 660{
bb435453 661 const struct xattr_handler *handler;
1da177e4
LT
662
663 if (!*name)
664 return NULL;
665
666 for_each_xattr_handler(handlers, handler) {
667 const char *n = strcmp_prefix(*name, handler->prefix);
668 if (n) {
669 *name = n;
670 break;
671 }
672 }
673 return handler;
674}
675
676/*
677 * Find the handler for the prefix and dispatch its get() operation.
678 */
679ssize_t
680generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
681{
bb435453 682 const struct xattr_handler *handler;
1da177e4 683
431547b3 684 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
1da177e4
LT
685 if (!handler)
686 return -EOPNOTSUPP;
431547b3 687 return handler->get(dentry, name, buffer, size, handler->flags);
1da177e4
LT
688}
689
690/*
691 * Combine the results of the list() operation from every xattr_handler in the
692 * list.
693 */
694ssize_t
695generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
696{
bb435453 697 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
698 unsigned int size = 0;
699
700 if (!buffer) {
431547b3
CH
701 for_each_xattr_handler(handlers, handler) {
702 size += handler->list(dentry, NULL, 0, NULL, 0,
703 handler->flags);
704 }
1da177e4
LT
705 } else {
706 char *buf = buffer;
707
708 for_each_xattr_handler(handlers, handler) {
431547b3
CH
709 size = handler->list(dentry, buf, buffer_size,
710 NULL, 0, handler->flags);
1da177e4
LT
711 if (size > buffer_size)
712 return -ERANGE;
713 buf += size;
714 buffer_size -= size;
715 }
716 size = buf - buffer;
717 }
718 return size;
719}
720
721/*
722 * Find the handler for the prefix and dispatch its set() operation.
723 */
724int
725generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
726{
bb435453 727 const struct xattr_handler *handler;
1da177e4
LT
728
729 if (size == 0)
730 value = ""; /* empty EA, do not remove */
431547b3 731 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
1da177e4
LT
732 if (!handler)
733 return -EOPNOTSUPP;
df7e1303 734 return handler->set(dentry, name, value, size, flags, handler->flags);
1da177e4
LT
735}
736
737/*
738 * Find the handler for the prefix and dispatch its set() operation to remove
739 * any associated extended attribute.
740 */
741int
742generic_removexattr(struct dentry *dentry, const char *name)
743{
bb435453 744 const struct xattr_handler *handler;
1da177e4 745
431547b3 746 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
1da177e4
LT
747 if (!handler)
748 return -EOPNOTSUPP;
431547b3
CH
749 return handler->set(dentry, name, NULL, 0,
750 XATTR_REPLACE, handler->flags);
1da177e4
LT
751}
752
753EXPORT_SYMBOL(generic_getxattr);
754EXPORT_SYMBOL(generic_listxattr);
755EXPORT_SYMBOL(generic_setxattr);
756EXPORT_SYMBOL(generic_removexattr);