]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xattr.c
xattr: Add __vfs_{get,set,remove}xattr helpers
[mirror_ubuntu-artful-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>
c7b87de2 17#include <linux/evm.h>
1da177e4 18#include <linux/syscalls.h>
630d9c47 19#include <linux/export.h>
0eeca283 20#include <linux/fsnotify.h>
73241ccc 21#include <linux/audit.h>
0d08d7b7 22#include <linux/vmalloc.h>
2f6f0654 23#include <linux/posix_acl_xattr.h>
1da177e4 24
0d08d7b7 25#include <asm/uaccess.h>
5be196e5 26
b6ba1177
AG
27static const char *
28strcmp_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 */
55static const struct xattr_handler *
d0a5b995 56xattr_resolve_name(struct inode *inode, const char **name)
b6ba1177 57{
d0a5b995 58 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
b6ba1177
AG
59 const struct xattr_handler *handler;
60
5f6e59ae
AG
61 if (!(inode->i_opflags & IOP_XATTR)) {
62 if (unlikely(is_bad_inode(inode)))
63 return ERR_PTR(-EIO);
d0a5b995 64 return ERR_PTR(-EOPNOTSUPP);
5f6e59ae 65 }
b6ba1177
AG
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
e0ad7b07 83/*
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
86 */
87static int
88xattr_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) {
e0ad7b07 95 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
96 return -EPERM;
0bd23d09
EB
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;
e0ad7b07 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 /*
55b23bde 115 * The trusted.* namespace can only be accessed by privileged users.
e0ad7b07 116 */
55b23bde
AG
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 }
e0ad7b07 122
55b23bde
AG
123 /*
124 * In the user.* namespace, only regular files and directories can have
f1f2d871 125 * extended attributes. For sticky directories, only the owner and
55b23bde 126 * privileged users can write attributes.
f1f2d871 127 */
e0ad7b07 128 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
f1f2d871 129 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
55b23bde 130 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
f1f2d871 131 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2e149670 132 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
e0ad7b07 133 return -EPERM;
134 }
135
f419a2e3 136 return inode_permission(inode, mask);
e0ad7b07 137}
138
5d6c3191
AG
139int
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}
147EXPORT_SYMBOL(__vfs_setxattr);
148
b1ab7e4b
DQ
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 */
165int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
166 const void *value, size_t size, int flags)
5be196e5
CH
167{
168 struct inode *inode = dentry->d_inode;
b1ab7e4b 169 int error = -EOPNOTSUPP;
69b45732
AK
170 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
171 XATTR_SECURITY_PREFIX_LEN);
e0ad7b07 172
69b45732
AK
173 if (issec)
174 inode->i_flags &= ~S_NOSEC;
5be196e5 175 if (inode->i_op->setxattr) {
5d6c3191 176 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
5be196e5
CH
177 if (!error) {
178 fsnotify_xattr(dentry);
179 security_inode_post_setxattr(dentry, name, value,
180 size, flags);
181 }
69b45732 182 } else if (issec) {
e0ad7b07 183 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
5f6e59ae
AG
184
185 if (unlikely(is_bad_inode(inode)))
186 return -EIO;
5be196e5
CH
187 error = security_inode_setsecurity(inode, suffix, value,
188 size, flags);
189 if (!error)
190 fsnotify_xattr(dentry);
191 }
b1ab7e4b
DQ
192
193 return error;
194}
195
196
197int
198vfs_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
5955102c 208 inode_lock(inode);
b1ab7e4b
DQ
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
5be196e5 215out:
5955102c 216 inode_unlock(inode);
5be196e5
CH
217 return error;
218}
219EXPORT_SYMBOL_GPL(vfs_setxattr);
220
42492594
DQ
221ssize_t
222xattr_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);
241out:
242 security_release_secctx(buffer, len);
243out_noalloc:
244 return len;
245}
246EXPORT_SYMBOL_GPL(xattr_getsecurity);
247
1601fbad
MZ
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 */
256ssize_t
257vfs_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
ce23e640 271 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
1601fbad
MZ
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
ce23e640 282 error = inode->i_op->getxattr(dentry, inode, name, value, error);
1601fbad
MZ
283 *xattr_value = value;
284 return error;
285}
286
5d6c3191
AG
287ssize_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}
295EXPORT_SYMBOL(__vfs_getxattr);
296
5be196e5 297ssize_t
8f0cfa52 298vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
5be196e5
CH
299{
300 struct inode *inode = dentry->d_inode;
301 int error;
302
e0ad7b07 303 error = xattr_permission(inode, name, MAY_READ);
304 if (error)
305 return error;
306
5be196e5
CH
307 error = security_inode_getxattr(dentry, name);
308 if (error)
309 return error;
310
5be196e5 311 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 312 XATTR_SECURITY_PREFIX_LEN)) {
313 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
42492594 314 int ret = xattr_getsecurity(inode, suffix, value, size);
5be196e5
CH
315 /*
316 * Only overwrite the return value if a security module
317 * is actually active.
318 */
4bea5805
DQ
319 if (ret == -EOPNOTSUPP)
320 goto nolsm;
321 return ret;
5be196e5 322 }
4bea5805 323nolsm:
5d6c3191 324 return __vfs_getxattr(dentry, inode, name, value, size);
5be196e5
CH
325}
326EXPORT_SYMBOL_GPL(vfs_getxattr);
327
659564c8
BN
328ssize_t
329vfs_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;
acfa4380 337 if (d->d_inode->i_op->listxattr) {
659564c8
BN
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}
346EXPORT_SYMBOL_GPL(vfs_listxattr);
347
5be196e5 348int
5d6c3191 349__vfs_removexattr(struct dentry *dentry, const char *name)
5be196e5
CH
350{
351 struct inode *inode = dentry->d_inode;
5be196e5
CH
352
353 if (!inode->i_op->removexattr)
354 return -EOPNOTSUPP;
5d6c3191
AG
355 return inode->i_op->removexattr(dentry, name);
356}
357EXPORT_SYMBOL(__vfs_removexattr);
358
359int
360vfs_removexattr(struct dentry *dentry, const char *name)
361{
362 struct inode *inode = dentry->d_inode;
363 int error;
5be196e5 364
e0ad7b07 365 error = xattr_permission(inode, name, MAY_WRITE);
366 if (error)
367 return error;
368
5955102c 369 inode_lock(inode);
5be196e5 370 error = security_inode_removexattr(dentry, name);
7c51bb00
DK
371 if (error)
372 goto out;
5be196e5 373
5d6c3191 374 error = __vfs_removexattr(dentry, name);
5be196e5 375
c7b87de2 376 if (!error) {
5be196e5 377 fsnotify_xattr(dentry);
c7b87de2
MZ
378 evm_inode_post_removexattr(dentry, name);
379 }
7c51bb00
DK
380
381out:
5955102c 382 inode_unlock(inode);
5be196e5
CH
383 return error;
384}
385EXPORT_SYMBOL_GPL(vfs_removexattr);
386
387
1da177e4
LT
388/*
389 * Extended attribute SET operations
390 */
391static long
8f0cfa52 392setxattr(struct dentry *d, const char __user *name, const void __user *value,
1da177e4
LT
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;
44c82498
AM
411 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
412 if (!kvalue) {
0b2a6f23
RW
413 kvalue = vmalloc(size);
414 if (!kvalue)
44c82498 415 return -ENOMEM;
44c82498
AM
416 }
417 if (copy_from_user(kvalue, value, size)) {
418 error = -EFAULT;
419 goto out;
420 }
2f6f0654
EB
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);
1da177e4
LT
424 }
425
5be196e5 426 error = vfs_setxattr(d, kname, kvalue, size, flags);
44c82498 427out:
0b2a6f23
RW
428 kvfree(kvalue);
429
1da177e4
LT
430 return error;
431}
432
8cc43116
EB
433static 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)
1da177e4 436{
2d8f3038 437 struct path path;
1da177e4 438 int error;
68f1bb8b
JL
439retry:
440 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
441 if (error)
442 return error;
2d8f3038 443 error = mnt_want_write(path.mnt);
18f335af 444 if (!error) {
2d8f3038
AV
445 error = setxattr(path.dentry, name, value, size, flags);
446 mnt_drop_write(path.mnt);
18f335af 447 }
2d8f3038 448 path_put(&path);
68f1bb8b
JL
449 if (retry_estale(error, lookup_flags)) {
450 lookup_flags |= LOOKUP_REVAL;
451 goto retry;
452 }
1da177e4
LT
453 return error;
454}
455
8cc43116
EB
456SYSCALL_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
64fd1de3
HC
463SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
464 const char __user *, name, const void __user *, value,
465 size_t, size, int, flags)
1da177e4 466{
8cc43116 467 return path_setxattr(pathname, name, value, size, flags, 0);
1da177e4
LT
468}
469
64fd1de3
HC
470SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
471 const void __user *,value, size_t, size, int, flags)
1da177e4 472{
2903ff01 473 struct fd f = fdget(fd);
1da177e4
LT
474 int error = -EBADF;
475
2903ff01 476 if (!f.file)
1da177e4 477 return error;
9f45f5bf 478 audit_file(f.file);
2903ff01 479 error = mnt_want_write_file(f.file);
18f335af 480 if (!error) {
9f45f5bf 481 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
2903ff01 482 mnt_drop_write_file(f.file);
18f335af 483 }
2903ff01 484 fdput(f);
1da177e4
LT
485 return error;
486}
487
488/*
489 * Extended attribute GET operations
490 */
491static ssize_t
8f0cfa52
DH
492getxattr(struct dentry *d, const char __user *name, void __user *value,
493 size_t size)
1da177e4
LT
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;
779302e6
SL
508 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
509 if (!kvalue) {
0b2a6f23
RW
510 kvalue = vmalloc(size);
511 if (!kvalue)
779302e6 512 return -ENOMEM;
779302e6 513 }
1da177e4
LT
514 }
515
5be196e5 516 error = vfs_getxattr(d, kname, kvalue, size);
f549d6c1 517 if (error > 0) {
2f6f0654
EB
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);
f549d6c1
SS
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;
1da177e4 527 }
0b2a6f23
RW
528
529 kvfree(kvalue);
530
1da177e4
LT
531 return error;
532}
533
8cc43116
EB
534static ssize_t path_getxattr(const char __user *pathname,
535 const char __user *name, void __user *value,
536 size_t size, unsigned int lookup_flags)
1da177e4 537{
2d8f3038 538 struct path path;
1da177e4 539 ssize_t error;
60e66b48
JL
540retry:
541 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
542 if (error)
543 return error;
2d8f3038
AV
544 error = getxattr(path.dentry, name, value, size);
545 path_put(&path);
60e66b48
JL
546 if (retry_estale(error, lookup_flags)) {
547 lookup_flags |= LOOKUP_REVAL;
548 goto retry;
549 }
1da177e4
LT
550 return error;
551}
552
8cc43116
EB
553SYSCALL_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
64fd1de3
HC
559SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
560 const char __user *, name, void __user *, value, size_t, size)
1da177e4 561{
8cc43116 562 return path_getxattr(pathname, name, value, size, 0);
1da177e4
LT
563}
564
64fd1de3
HC
565SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
566 void __user *, value, size_t, size)
1da177e4 567{
2903ff01 568 struct fd f = fdget(fd);
1da177e4
LT
569 ssize_t error = -EBADF;
570
2903ff01 571 if (!f.file)
1da177e4 572 return error;
9f45f5bf 573 audit_file(f.file);
2903ff01
AV
574 error = getxattr(f.file->f_path.dentry, name, value, size);
575 fdput(f);
1da177e4
LT
576 return error;
577}
578
579/*
580 * Extended attribute LIST operations
581 */
582static ssize_t
583listxattr(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;
703bf2d1 591 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
0d08d7b7 592 if (!klist) {
0b2a6f23
RW
593 klist = vmalloc(size);
594 if (!klist)
0d08d7b7 595 return -ENOMEM;
0d08d7b7 596 }
1da177e4
LT
597 }
598
659564c8 599 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
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;
1da177e4 607 }
0b2a6f23
RW
608
609 kvfree(klist);
610
1da177e4
LT
611 return error;
612}
613
8cc43116
EB
614static ssize_t path_listxattr(const char __user *pathname, char __user *list,
615 size_t size, unsigned int lookup_flags)
1da177e4 616{
2d8f3038 617 struct path path;
1da177e4 618 ssize_t error;
10a90cf3
JL
619retry:
620 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
621 if (error)
622 return error;
2d8f3038
AV
623 error = listxattr(path.dentry, list, size);
624 path_put(&path);
10a90cf3
JL
625 if (retry_estale(error, lookup_flags)) {
626 lookup_flags |= LOOKUP_REVAL;
627 goto retry;
628 }
1da177e4
LT
629 return error;
630}
631
8cc43116
EB
632SYSCALL_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
64fd1de3
HC
638SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
639 size_t, size)
1da177e4 640{
8cc43116 641 return path_listxattr(pathname, list, size, 0);
1da177e4
LT
642}
643
64fd1de3 644SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4 645{
2903ff01 646 struct fd f = fdget(fd);
1da177e4
LT
647 ssize_t error = -EBADF;
648
2903ff01 649 if (!f.file)
1da177e4 650 return error;
9f45f5bf 651 audit_file(f.file);
2903ff01
AV
652 error = listxattr(f.file->f_path.dentry, list, size);
653 fdput(f);
1da177e4
LT
654 return error;
655}
656
657/*
658 * Extended attribute REMOVE operations
659 */
660static long
8f0cfa52 661removexattr(struct dentry *d, const char __user *name)
1da177e4
LT
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
5be196e5 672 return vfs_removexattr(d, kname);
1da177e4
LT
673}
674
8cc43116
EB
675static int path_removexattr(const char __user *pathname,
676 const char __user *name, unsigned int lookup_flags)
1da177e4 677{
2d8f3038 678 struct path path;
1da177e4 679 int error;
12f06212
JL
680retry:
681 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
682 if (error)
683 return error;
2d8f3038 684 error = mnt_want_write(path.mnt);
18f335af 685 if (!error) {
2d8f3038
AV
686 error = removexattr(path.dentry, name);
687 mnt_drop_write(path.mnt);
18f335af 688 }
2d8f3038 689 path_put(&path);
12f06212
JL
690 if (retry_estale(error, lookup_flags)) {
691 lookup_flags |= LOOKUP_REVAL;
692 goto retry;
693 }
1da177e4
LT
694 return error;
695}
696
8cc43116
EB
697SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
698 const char __user *, name)
699{
700 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
701}
702
6a6160a7
HC
703SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
704 const char __user *, name)
1da177e4 705{
8cc43116 706 return path_removexattr(pathname, name, 0);
1da177e4
LT
707}
708
6a6160a7 709SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4 710{
2903ff01 711 struct fd f = fdget(fd);
1da177e4
LT
712 int error = -EBADF;
713
2903ff01 714 if (!f.file)
1da177e4 715 return error;
9f45f5bf 716 audit_file(f.file);
2903ff01 717 error = mnt_want_write_file(f.file);
18f335af 718 if (!error) {
9f45f5bf 719 error = removexattr(f.file->f_path.dentry, name);
2903ff01 720 mnt_drop_write_file(f.file);
18f335af 721 }
2903ff01 722 fdput(f);
1da177e4
LT
723 return error;
724}
725
1da177e4
LT
726/*
727 * Find the handler for the prefix and dispatch its get() operation.
728 */
729ssize_t
ce23e640
AV
730generic_getxattr(struct dentry *dentry, struct inode *inode,
731 const char *name, void *buffer, size_t size)
1da177e4 732{
bb435453 733 const struct xattr_handler *handler;
1da177e4 734
d0a5b995 735 handler = xattr_resolve_name(inode, &name);
98e9cb57
AG
736 if (IS_ERR(handler))
737 return PTR_ERR(handler);
ce23e640 738 return handler->get(handler, dentry, inode,
b296821a 739 name, buffer, size);
1da177e4
LT
740}
741
742/*
743 * Combine the results of the list() operation from every xattr_handler in the
744 * list.
745 */
746ssize_t
747generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
748{
bb435453 749 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
750 unsigned int size = 0;
751
752 if (!buffer) {
431547b3 753 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
754 if (!handler->name ||
755 (handler->list && !handler->list(dentry)))
c4803c49 756 continue;
764a5c6b 757 size += strlen(handler->name) + 1;
431547b3 758 }
1da177e4
LT
759 } else {
760 char *buf = buffer;
764a5c6b 761 size_t len;
1da177e4
LT
762
763 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
764 if (!handler->name ||
765 (handler->list && !handler->list(dentry)))
c4803c49 766 continue;
764a5c6b
AG
767 len = strlen(handler->name);
768 if (len + 1 > buffer_size)
1da177e4 769 return -ERANGE;
764a5c6b
AG
770 memcpy(buf, handler->name, len + 1);
771 buf += len + 1;
772 buffer_size -= len + 1;
1da177e4
LT
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 */
782int
3767e255
AV
783generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
784 const void *value, size_t size, int flags)
1da177e4 785{
bb435453 786 const struct xattr_handler *handler;
1da177e4
LT
787
788 if (size == 0)
789 value = ""; /* empty EA, do not remove */
d0a5b995 790 handler = xattr_resolve_name(inode, &name);
98e9cb57
AG
791 if (IS_ERR(handler))
792 return PTR_ERR(handler);
3767e255 793 return handler->set(handler, dentry, inode, name, value, size, flags);
1da177e4
LT
794}
795
796/*
797 * Find the handler for the prefix and dispatch its set() operation to remove
798 * any associated extended attribute.
799 */
800int
801generic_removexattr(struct dentry *dentry, const char *name)
802{
bb435453 803 const struct xattr_handler *handler;
1da177e4 804
d0a5b995 805 handler = xattr_resolve_name(d_inode(dentry), &name);
98e9cb57
AG
806 if (IS_ERR(handler))
807 return PTR_ERR(handler);
59301226
AV
808 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
809 0, XATTR_REPLACE);
1da177e4
LT
810}
811
812EXPORT_SYMBOL(generic_getxattr);
813EXPORT_SYMBOL(generic_listxattr);
814EXPORT_SYMBOL(generic_setxattr);
815EXPORT_SYMBOL(generic_removexattr);
38f38657 816
e409de99
AG
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 */
832const char *xattr_full_name(const struct xattr_handler *handler,
833 const char *name)
834{
98e9cb57 835 size_t prefix_len = strlen(xattr_prefix(handler));
e409de99
AG
836
837 return name - prefix_len;
838}
839EXPORT_SYMBOL(xattr_full_name);
840
38f38657
AR
841/*
842 * Allocate new xattr and copy in the value; but leave the name to callers.
843 */
844struct 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;
4e66d445 851 if (len < sizeof(*new_xattr))
38f38657
AR
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 */
866int 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
aa7c5241
AG
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 */
904int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
905 const void *value, size_t size, int flags)
38f38657
AR
906{
907 struct simple_xattr *xattr;
43385846 908 struct simple_xattr *new_xattr = NULL;
38f38657
AR
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 }
945out:
946 spin_unlock(&xattrs->lock);
947 if (xattr) {
948 kfree(xattr->name);
949 kfree(xattr);
950 }
951 return err;
952
953}
954
38f38657
AR
955static bool xattr_is_trusted(const char *name)
956{
957 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
958}
959
786534b9
AG
960static 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
38f38657
AR
974/*
975 * xattr LIST operation for in-memory/pseudo filesystems
976 */
786534b9
AG
977ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
978 char *buffer, size_t size)
38f38657
AR
979{
980 bool trusted = capable(CAP_SYS_ADMIN);
981 struct simple_xattr *xattr;
786534b9 982 ssize_t remaining_size = size;
0e9a7da5 983 int err = 0;
786534b9
AG
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
38f38657
AR
999
1000 spin_lock(&xattrs->lock);
1001 list_for_each_entry(xattr, &xattrs->head, list) {
38f38657
AR
1002 /* skip "trusted." attributes for unprivileged callers */
1003 if (!trusted && xattr_is_trusted(xattr->name))
1004 continue;
1005
786534b9
AG
1006 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1007 if (err)
0e9a7da5 1008 break;
38f38657
AR
1009 }
1010 spin_unlock(&xattrs->lock);
1011
0e9a7da5 1012 return err ? err : size - remaining_size;
38f38657
AR
1013}
1014
4895768b
AR
1015/*
1016 * Adds an extended attribute to the list
1017 */
38f38657
AR
1018void 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}