]> git.ipfire.org Git - people/arne_f/kernel.git/blame - fs/xattr.c
Merge commit '2c563880ea' into work.xattr
[people/arne_f/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
e0ad7b07 27/*
28 * Check permissions for extended attribute access. This is a bit complicated
29 * because different namespaces have very different rules.
30 */
31static int
32xattr_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) {
e0ad7b07 39 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
40 return -EPERM;
0bd23d09
EB
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;
e0ad7b07 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 /*
55b23bde 59 * The trusted.* namespace can only be accessed by privileged users.
e0ad7b07 60 */
55b23bde
AG
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 }
e0ad7b07 66
55b23bde
AG
67 /*
68 * In the user.* namespace, only regular files and directories can have
f1f2d871 69 * extended attributes. For sticky directories, only the owner and
55b23bde 70 * privileged users can write attributes.
f1f2d871 71 */
e0ad7b07 72 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
f1f2d871 73 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
55b23bde 74 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
f1f2d871 75 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
2e149670 76 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
e0ad7b07 77 return -EPERM;
78 }
79
f419a2e3 80 return inode_permission(inode, mask);
e0ad7b07 81}
82
b1ab7e4b
DQ
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 */
99int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
100 const void *value, size_t size, int flags)
5be196e5
CH
101{
102 struct inode *inode = dentry->d_inode;
b1ab7e4b 103 int error = -EOPNOTSUPP;
69b45732
AK
104 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
105 XATTR_SECURITY_PREFIX_LEN);
e0ad7b07 106
69b45732
AK
107 if (issec)
108 inode->i_flags &= ~S_NOSEC;
5be196e5 109 if (inode->i_op->setxattr) {
3767e255 110 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
5be196e5
CH
111 if (!error) {
112 fsnotify_xattr(dentry);
113 security_inode_post_setxattr(dentry, name, value,
114 size, flags);
115 }
69b45732 116 } else if (issec) {
e0ad7b07 117 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
5be196e5
CH
118 error = security_inode_setsecurity(inode, suffix, value,
119 size, flags);
120 if (!error)
121 fsnotify_xattr(dentry);
122 }
b1ab7e4b
DQ
123
124 return error;
125}
126
127
128int
129vfs_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
5955102c 139 inode_lock(inode);
b1ab7e4b
DQ
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
5be196e5 146out:
5955102c 147 inode_unlock(inode);
5be196e5
CH
148 return error;
149}
150EXPORT_SYMBOL_GPL(vfs_setxattr);
151
42492594
DQ
152ssize_t
153xattr_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);
172out:
173 security_release_secctx(buffer, len);
174out_noalloc:
175 return len;
176}
177EXPORT_SYMBOL_GPL(xattr_getsecurity);
178
1601fbad
MZ
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 */
187ssize_t
188vfs_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
ce23e640 202 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
1601fbad
MZ
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
ce23e640 213 error = inode->i_op->getxattr(dentry, inode, name, value, error);
1601fbad
MZ
214 *xattr_value = value;
215 return error;
216}
217
5be196e5 218ssize_t
8f0cfa52 219vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
5be196e5
CH
220{
221 struct inode *inode = dentry->d_inode;
222 int error;
223
e0ad7b07 224 error = xattr_permission(inode, name, MAY_READ);
225 if (error)
226 return error;
227
5be196e5
CH
228 error = security_inode_getxattr(dentry, name);
229 if (error)
230 return error;
231
5be196e5 232 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 233 XATTR_SECURITY_PREFIX_LEN)) {
234 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
42492594 235 int ret = xattr_getsecurity(inode, suffix, value, size);
5be196e5
CH
236 /*
237 * Only overwrite the return value if a security module
238 * is actually active.
239 */
4bea5805
DQ
240 if (ret == -EOPNOTSUPP)
241 goto nolsm;
242 return ret;
5be196e5 243 }
4bea5805
DQ
244nolsm:
245 if (inode->i_op->getxattr)
ce23e640 246 error = inode->i_op->getxattr(dentry, inode, name, value, size);
4bea5805
DQ
247 else
248 error = -EOPNOTSUPP;
5be196e5
CH
249
250 return error;
251}
252EXPORT_SYMBOL_GPL(vfs_getxattr);
253
659564c8
BN
254ssize_t
255vfs_listxattr(struct dentry *d, char *list, size_t size)
256{
257 ssize_t error;
258
259 error = security_inode_listxattr(d);
260 if (error)
261 return error;
262 error = -EOPNOTSUPP;
acfa4380 263 if (d->d_inode->i_op->listxattr) {
659564c8
BN
264 error = d->d_inode->i_op->listxattr(d, list, size);
265 } else {
266 error = security_inode_listsecurity(d->d_inode, list, size);
267 if (size && error > size)
268 error = -ERANGE;
269 }
270 return error;
271}
272EXPORT_SYMBOL_GPL(vfs_listxattr);
273
5be196e5 274int
8f0cfa52 275vfs_removexattr(struct dentry *dentry, const char *name)
5be196e5
CH
276{
277 struct inode *inode = dentry->d_inode;
278 int error;
279
280 if (!inode->i_op->removexattr)
281 return -EOPNOTSUPP;
282
e0ad7b07 283 error = xattr_permission(inode, name, MAY_WRITE);
284 if (error)
285 return error;
286
5955102c 287 inode_lock(inode);
5be196e5 288 error = security_inode_removexattr(dentry, name);
7c51bb00
DK
289 if (error)
290 goto out;
5be196e5 291
5be196e5 292 error = inode->i_op->removexattr(dentry, name);
5be196e5 293
c7b87de2 294 if (!error) {
5be196e5 295 fsnotify_xattr(dentry);
c7b87de2
MZ
296 evm_inode_post_removexattr(dentry, name);
297 }
7c51bb00
DK
298
299out:
5955102c 300 inode_unlock(inode);
5be196e5
CH
301 return error;
302}
303EXPORT_SYMBOL_GPL(vfs_removexattr);
304
305
1da177e4
LT
306/*
307 * Extended attribute SET operations
308 */
309static long
8f0cfa52 310setxattr(struct dentry *d, const char __user *name, const void __user *value,
1da177e4
LT
311 size_t size, int flags)
312{
313 int error;
314 void *kvalue = NULL;
315 char kname[XATTR_NAME_MAX + 1];
316
317 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
318 return -EINVAL;
319
320 error = strncpy_from_user(kname, name, sizeof(kname));
321 if (error == 0 || error == sizeof(kname))
322 error = -ERANGE;
323 if (error < 0)
324 return error;
325
326 if (size) {
327 if (size > XATTR_SIZE_MAX)
328 return -E2BIG;
44c82498
AM
329 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
330 if (!kvalue) {
0b2a6f23
RW
331 kvalue = vmalloc(size);
332 if (!kvalue)
44c82498 333 return -ENOMEM;
44c82498
AM
334 }
335 if (copy_from_user(kvalue, value, size)) {
336 error = -EFAULT;
337 goto out;
338 }
2f6f0654
EB
339 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
340 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
341 posix_acl_fix_xattr_from_user(kvalue, size);
1da177e4
LT
342 }
343
5be196e5 344 error = vfs_setxattr(d, kname, kvalue, size, flags);
44c82498 345out:
0b2a6f23
RW
346 kvfree(kvalue);
347
1da177e4
LT
348 return error;
349}
350
8cc43116
EB
351static int path_setxattr(const char __user *pathname,
352 const char __user *name, const void __user *value,
353 size_t size, int flags, unsigned int lookup_flags)
1da177e4 354{
2d8f3038 355 struct path path;
1da177e4 356 int error;
68f1bb8b
JL
357retry:
358 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
359 if (error)
360 return error;
2d8f3038 361 error = mnt_want_write(path.mnt);
18f335af 362 if (!error) {
2d8f3038
AV
363 error = setxattr(path.dentry, name, value, size, flags);
364 mnt_drop_write(path.mnt);
18f335af 365 }
2d8f3038 366 path_put(&path);
68f1bb8b
JL
367 if (retry_estale(error, lookup_flags)) {
368 lookup_flags |= LOOKUP_REVAL;
369 goto retry;
370 }
1da177e4
LT
371 return error;
372}
373
8cc43116
EB
374SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
375 const char __user *, name, const void __user *, value,
376 size_t, size, int, flags)
377{
378 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
379}
380
64fd1de3
HC
381SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
382 const char __user *, name, const void __user *, value,
383 size_t, size, int, flags)
1da177e4 384{
8cc43116 385 return path_setxattr(pathname, name, value, size, flags, 0);
1da177e4
LT
386}
387
64fd1de3
HC
388SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
389 const void __user *,value, size_t, size, int, flags)
1da177e4 390{
2903ff01 391 struct fd f = fdget(fd);
1da177e4
LT
392 int error = -EBADF;
393
2903ff01 394 if (!f.file)
1da177e4 395 return error;
9f45f5bf 396 audit_file(f.file);
2903ff01 397 error = mnt_want_write_file(f.file);
18f335af 398 if (!error) {
9f45f5bf 399 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
2903ff01 400 mnt_drop_write_file(f.file);
18f335af 401 }
2903ff01 402 fdput(f);
1da177e4
LT
403 return error;
404}
405
406/*
407 * Extended attribute GET operations
408 */
409static ssize_t
8f0cfa52
DH
410getxattr(struct dentry *d, const char __user *name, void __user *value,
411 size_t size)
1da177e4
LT
412{
413 ssize_t error;
414 void *kvalue = NULL;
415 char kname[XATTR_NAME_MAX + 1];
416
417 error = strncpy_from_user(kname, name, sizeof(kname));
418 if (error == 0 || error == sizeof(kname))
419 error = -ERANGE;
420 if (error < 0)
421 return error;
422
423 if (size) {
424 if (size > XATTR_SIZE_MAX)
425 size = XATTR_SIZE_MAX;
779302e6
SL
426 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
427 if (!kvalue) {
0b2a6f23
RW
428 kvalue = vmalloc(size);
429 if (!kvalue)
779302e6 430 return -ENOMEM;
779302e6 431 }
1da177e4
LT
432 }
433
5be196e5 434 error = vfs_getxattr(d, kname, kvalue, size);
f549d6c1 435 if (error > 0) {
2f6f0654
EB
436 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
437 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
438 posix_acl_fix_xattr_to_user(kvalue, size);
f549d6c1
SS
439 if (size && copy_to_user(value, kvalue, error))
440 error = -EFAULT;
441 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
442 /* The file system tried to returned a value bigger
443 than XATTR_SIZE_MAX bytes. Not possible. */
444 error = -E2BIG;
1da177e4 445 }
0b2a6f23
RW
446
447 kvfree(kvalue);
448
1da177e4
LT
449 return error;
450}
451
8cc43116
EB
452static ssize_t path_getxattr(const char __user *pathname,
453 const char __user *name, void __user *value,
454 size_t size, unsigned int lookup_flags)
1da177e4 455{
2d8f3038 456 struct path path;
1da177e4 457 ssize_t error;
60e66b48
JL
458retry:
459 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
460 if (error)
461 return error;
2d8f3038
AV
462 error = getxattr(path.dentry, name, value, size);
463 path_put(&path);
60e66b48
JL
464 if (retry_estale(error, lookup_flags)) {
465 lookup_flags |= LOOKUP_REVAL;
466 goto retry;
467 }
1da177e4
LT
468 return error;
469}
470
8cc43116
EB
471SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
472 const char __user *, name, void __user *, value, size_t, size)
473{
474 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
475}
476
64fd1de3
HC
477SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
478 const char __user *, name, void __user *, value, size_t, size)
1da177e4 479{
8cc43116 480 return path_getxattr(pathname, name, value, size, 0);
1da177e4
LT
481}
482
64fd1de3
HC
483SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
484 void __user *, value, size_t, size)
1da177e4 485{
2903ff01 486 struct fd f = fdget(fd);
1da177e4
LT
487 ssize_t error = -EBADF;
488
2903ff01 489 if (!f.file)
1da177e4 490 return error;
9f45f5bf 491 audit_file(f.file);
2903ff01
AV
492 error = getxattr(f.file->f_path.dentry, name, value, size);
493 fdput(f);
1da177e4
LT
494 return error;
495}
496
497/*
498 * Extended attribute LIST operations
499 */
500static ssize_t
501listxattr(struct dentry *d, char __user *list, size_t size)
502{
503 ssize_t error;
504 char *klist = NULL;
505
506 if (size) {
507 if (size > XATTR_LIST_MAX)
508 size = XATTR_LIST_MAX;
703bf2d1 509 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
0d08d7b7 510 if (!klist) {
0b2a6f23
RW
511 klist = vmalloc(size);
512 if (!klist)
0d08d7b7 513 return -ENOMEM;
0d08d7b7 514 }
1da177e4
LT
515 }
516
659564c8 517 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
518 if (error > 0) {
519 if (size && copy_to_user(list, klist, error))
520 error = -EFAULT;
521 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
522 /* The file system tried to returned a list bigger
523 than XATTR_LIST_MAX bytes. Not possible. */
524 error = -E2BIG;
1da177e4 525 }
0b2a6f23
RW
526
527 kvfree(klist);
528
1da177e4
LT
529 return error;
530}
531
8cc43116
EB
532static ssize_t path_listxattr(const char __user *pathname, char __user *list,
533 size_t size, unsigned int lookup_flags)
1da177e4 534{
2d8f3038 535 struct path path;
1da177e4 536 ssize_t error;
10a90cf3
JL
537retry:
538 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
539 if (error)
540 return error;
2d8f3038
AV
541 error = listxattr(path.dentry, list, size);
542 path_put(&path);
10a90cf3
JL
543 if (retry_estale(error, lookup_flags)) {
544 lookup_flags |= LOOKUP_REVAL;
545 goto retry;
546 }
1da177e4
LT
547 return error;
548}
549
8cc43116
EB
550SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
551 size_t, size)
552{
553 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
554}
555
64fd1de3
HC
556SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
557 size_t, size)
1da177e4 558{
8cc43116 559 return path_listxattr(pathname, list, size, 0);
1da177e4
LT
560}
561
64fd1de3 562SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4 563{
2903ff01 564 struct fd f = fdget(fd);
1da177e4
LT
565 ssize_t error = -EBADF;
566
2903ff01 567 if (!f.file)
1da177e4 568 return error;
9f45f5bf 569 audit_file(f.file);
2903ff01
AV
570 error = listxattr(f.file->f_path.dentry, list, size);
571 fdput(f);
1da177e4
LT
572 return error;
573}
574
575/*
576 * Extended attribute REMOVE operations
577 */
578static long
8f0cfa52 579removexattr(struct dentry *d, const char __user *name)
1da177e4
LT
580{
581 int error;
582 char kname[XATTR_NAME_MAX + 1];
583
584 error = strncpy_from_user(kname, name, sizeof(kname));
585 if (error == 0 || error == sizeof(kname))
586 error = -ERANGE;
587 if (error < 0)
588 return error;
589
5be196e5 590 return vfs_removexattr(d, kname);
1da177e4
LT
591}
592
8cc43116
EB
593static int path_removexattr(const char __user *pathname,
594 const char __user *name, unsigned int lookup_flags)
1da177e4 595{
2d8f3038 596 struct path path;
1da177e4 597 int error;
12f06212
JL
598retry:
599 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
600 if (error)
601 return error;
2d8f3038 602 error = mnt_want_write(path.mnt);
18f335af 603 if (!error) {
2d8f3038
AV
604 error = removexattr(path.dentry, name);
605 mnt_drop_write(path.mnt);
18f335af 606 }
2d8f3038 607 path_put(&path);
12f06212
JL
608 if (retry_estale(error, lookup_flags)) {
609 lookup_flags |= LOOKUP_REVAL;
610 goto retry;
611 }
1da177e4
LT
612 return error;
613}
614
8cc43116
EB
615SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
616 const char __user *, name)
617{
618 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
619}
620
6a6160a7
HC
621SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
622 const char __user *, name)
1da177e4 623{
8cc43116 624 return path_removexattr(pathname, name, 0);
1da177e4
LT
625}
626
6a6160a7 627SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4 628{
2903ff01 629 struct fd f = fdget(fd);
1da177e4
LT
630 int error = -EBADF;
631
2903ff01 632 if (!f.file)
1da177e4 633 return error;
9f45f5bf 634 audit_file(f.file);
2903ff01 635 error = mnt_want_write_file(f.file);
18f335af 636 if (!error) {
9f45f5bf 637 error = removexattr(f.file->f_path.dentry, name);
2903ff01 638 mnt_drop_write_file(f.file);
18f335af 639 }
2903ff01 640 fdput(f);
1da177e4
LT
641 return error;
642}
643
644
645static const char *
646strcmp_prefix(const char *a, const char *a_prefix)
647{
648 while (*a_prefix && *a == *a_prefix) {
649 a++;
650 a_prefix++;
651 }
652 return *a_prefix ? NULL : a;
653}
654
655/*
656 * In order to implement different sets of xattr operations for each xattr
657 * prefix with the generic xattr API, a filesystem should create a
658 * null-terminated array of struct xattr_handler (one for each prefix) and
659 * hang a pointer to it off of the s_xattr field of the superblock.
660 *
661 * The generic_fooxattr() functions will use this list to dispatch xattr
662 * operations to the correct xattr_handler.
663 */
664#define for_each_xattr_handler(handlers, handler) \
0040773b 665 if (handlers) \
1da177e4
LT
666 for ((handler) = *(handlers)++; \
667 (handler) != NULL; \
668 (handler) = *(handlers)++)
669
670/*
671 * Find the xattr_handler with the matching prefix.
672 */
bb435453
SH
673static const struct xattr_handler *
674xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
1da177e4 675{
bb435453 676 const struct xattr_handler *handler;
1da177e4 677
1da177e4 678 for_each_xattr_handler(handlers, handler) {
98e9cb57
AG
679 const char *n;
680
681 n = strcmp_prefix(*name, xattr_prefix(handler));
1da177e4 682 if (n) {
98e9cb57
AG
683 if (!handler->prefix ^ !*n) {
684 if (*n)
685 continue;
686 return ERR_PTR(-EINVAL);
687 }
1da177e4 688 *name = n;
98e9cb57 689 return handler;
1da177e4
LT
690 }
691 }
98e9cb57 692 return ERR_PTR(-EOPNOTSUPP);
1da177e4
LT
693}
694
695/*
696 * Find the handler for the prefix and dispatch its get() operation.
697 */
698ssize_t
ce23e640
AV
699generic_getxattr(struct dentry *dentry, struct inode *inode,
700 const char *name, void *buffer, size_t size)
1da177e4 701{
bb435453 702 const struct xattr_handler *handler;
1da177e4 703
431547b3 704 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
98e9cb57
AG
705 if (IS_ERR(handler))
706 return PTR_ERR(handler);
ce23e640 707 return handler->get(handler, dentry, inode,
b296821a 708 name, buffer, size);
1da177e4
LT
709}
710
711/*
712 * Combine the results of the list() operation from every xattr_handler in the
713 * list.
714 */
715ssize_t
716generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
717{
bb435453 718 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
719 unsigned int size = 0;
720
721 if (!buffer) {
431547b3 722 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
723 if (!handler->name ||
724 (handler->list && !handler->list(dentry)))
c4803c49 725 continue;
764a5c6b 726 size += strlen(handler->name) + 1;
431547b3 727 }
1da177e4
LT
728 } else {
729 char *buf = buffer;
764a5c6b 730 size_t len;
1da177e4
LT
731
732 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
733 if (!handler->name ||
734 (handler->list && !handler->list(dentry)))
c4803c49 735 continue;
764a5c6b
AG
736 len = strlen(handler->name);
737 if (len + 1 > buffer_size)
1da177e4 738 return -ERANGE;
764a5c6b
AG
739 memcpy(buf, handler->name, len + 1);
740 buf += len + 1;
741 buffer_size -= len + 1;
1da177e4
LT
742 }
743 size = buf - buffer;
744 }
745 return size;
746}
747
748/*
749 * Find the handler for the prefix and dispatch its set() operation.
750 */
751int
3767e255
AV
752generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
753 const void *value, size_t size, int flags)
1da177e4 754{
bb435453 755 const struct xattr_handler *handler;
1da177e4
LT
756
757 if (size == 0)
758 value = ""; /* empty EA, do not remove */
431547b3 759 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
98e9cb57
AG
760 if (IS_ERR(handler))
761 return PTR_ERR(handler);
3767e255 762 return handler->set(handler, dentry, inode, name, value, size, flags);
1da177e4
LT
763}
764
765/*
766 * Find the handler for the prefix and dispatch its set() operation to remove
767 * any associated extended attribute.
768 */
769int
770generic_removexattr(struct dentry *dentry, const char *name)
771{
bb435453 772 const struct xattr_handler *handler;
1da177e4 773
431547b3 774 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
98e9cb57
AG
775 if (IS_ERR(handler))
776 return PTR_ERR(handler);
59301226
AV
777 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
778 0, XATTR_REPLACE);
1da177e4
LT
779}
780
781EXPORT_SYMBOL(generic_getxattr);
782EXPORT_SYMBOL(generic_listxattr);
783EXPORT_SYMBOL(generic_setxattr);
784EXPORT_SYMBOL(generic_removexattr);
38f38657 785
e409de99
AG
786/**
787 * xattr_full_name - Compute full attribute name from suffix
788 *
789 * @handler: handler of the xattr_handler operation
790 * @name: name passed to the xattr_handler operation
791 *
792 * The get and set xattr handler operations are called with the remainder of
793 * the attribute name after skipping the handler's prefix: for example, "foo"
794 * is passed to the get operation of a handler with prefix "user." to get
795 * attribute "user.foo". The full name is still "there" in the name though.
796 *
797 * Note: the list xattr handler operation when called from the vfs is passed a
798 * NULL name; some file systems use this operation internally, with varying
799 * semantics.
800 */
801const char *xattr_full_name(const struct xattr_handler *handler,
802 const char *name)
803{
98e9cb57 804 size_t prefix_len = strlen(xattr_prefix(handler));
e409de99
AG
805
806 return name - prefix_len;
807}
808EXPORT_SYMBOL(xattr_full_name);
809
38f38657
AR
810/*
811 * Allocate new xattr and copy in the value; but leave the name to callers.
812 */
813struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
814{
815 struct simple_xattr *new_xattr;
816 size_t len;
817
818 /* wrap around? */
819 len = sizeof(*new_xattr) + size;
4e66d445 820 if (len < sizeof(*new_xattr))
38f38657
AR
821 return NULL;
822
823 new_xattr = kmalloc(len, GFP_KERNEL);
824 if (!new_xattr)
825 return NULL;
826
827 new_xattr->size = size;
828 memcpy(new_xattr->value, value, size);
829 return new_xattr;
830}
831
832/*
833 * xattr GET operation for in-memory/pseudo filesystems
834 */
835int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
836 void *buffer, size_t size)
837{
838 struct simple_xattr *xattr;
839 int ret = -ENODATA;
840
841 spin_lock(&xattrs->lock);
842 list_for_each_entry(xattr, &xattrs->head, list) {
843 if (strcmp(name, xattr->name))
844 continue;
845
846 ret = xattr->size;
847 if (buffer) {
848 if (size < xattr->size)
849 ret = -ERANGE;
850 else
851 memcpy(buffer, xattr->value, xattr->size);
852 }
853 break;
854 }
855 spin_unlock(&xattrs->lock);
856 return ret;
857}
858
aa7c5241
AG
859/**
860 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
861 * @xattrs: target simple_xattr list
862 * @name: name of the extended attribute
863 * @value: value of the xattr. If %NULL, will remove the attribute.
864 * @size: size of the new xattr
865 * @flags: %XATTR_{CREATE|REPLACE}
866 *
867 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
868 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
869 * otherwise, fails with -ENODATA.
870 *
871 * Returns 0 on success, -errno on failure.
872 */
873int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
874 const void *value, size_t size, int flags)
38f38657
AR
875{
876 struct simple_xattr *xattr;
43385846 877 struct simple_xattr *new_xattr = NULL;
38f38657
AR
878 int err = 0;
879
880 /* value == NULL means remove */
881 if (value) {
882 new_xattr = simple_xattr_alloc(value, size);
883 if (!new_xattr)
884 return -ENOMEM;
885
886 new_xattr->name = kstrdup(name, GFP_KERNEL);
887 if (!new_xattr->name) {
888 kfree(new_xattr);
889 return -ENOMEM;
890 }
891 }
892
893 spin_lock(&xattrs->lock);
894 list_for_each_entry(xattr, &xattrs->head, list) {
895 if (!strcmp(name, xattr->name)) {
896 if (flags & XATTR_CREATE) {
897 xattr = new_xattr;
898 err = -EEXIST;
899 } else if (new_xattr) {
900 list_replace(&xattr->list, &new_xattr->list);
901 } else {
902 list_del(&xattr->list);
903 }
904 goto out;
905 }
906 }
907 if (flags & XATTR_REPLACE) {
908 xattr = new_xattr;
909 err = -ENODATA;
910 } else {
911 list_add(&new_xattr->list, &xattrs->head);
912 xattr = NULL;
913 }
914out:
915 spin_unlock(&xattrs->lock);
916 if (xattr) {
917 kfree(xattr->name);
918 kfree(xattr);
919 }
920 return err;
921
922}
923
38f38657
AR
924static bool xattr_is_trusted(const char *name)
925{
926 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
927}
928
786534b9
AG
929static int xattr_list_one(char **buffer, ssize_t *remaining_size,
930 const char *name)
931{
932 size_t len = strlen(name) + 1;
933 if (*buffer) {
934 if (*remaining_size < len)
935 return -ERANGE;
936 memcpy(*buffer, name, len);
937 *buffer += len;
938 }
939 *remaining_size -= len;
940 return 0;
941}
942
38f38657
AR
943/*
944 * xattr LIST operation for in-memory/pseudo filesystems
945 */
786534b9
AG
946ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
947 char *buffer, size_t size)
38f38657
AR
948{
949 bool trusted = capable(CAP_SYS_ADMIN);
950 struct simple_xattr *xattr;
786534b9 951 ssize_t remaining_size = size;
0e9a7da5 952 int err = 0;
786534b9
AG
953
954#ifdef CONFIG_FS_POSIX_ACL
955 if (inode->i_acl) {
956 err = xattr_list_one(&buffer, &remaining_size,
957 XATTR_NAME_POSIX_ACL_ACCESS);
958 if (err)
959 return err;
960 }
961 if (inode->i_default_acl) {
962 err = xattr_list_one(&buffer, &remaining_size,
963 XATTR_NAME_POSIX_ACL_DEFAULT);
964 if (err)
965 return err;
966 }
967#endif
38f38657
AR
968
969 spin_lock(&xattrs->lock);
970 list_for_each_entry(xattr, &xattrs->head, list) {
38f38657
AR
971 /* skip "trusted." attributes for unprivileged callers */
972 if (!trusted && xattr_is_trusted(xattr->name))
973 continue;
974
786534b9
AG
975 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
976 if (err)
0e9a7da5 977 break;
38f38657
AR
978 }
979 spin_unlock(&xattrs->lock);
980
0e9a7da5 981 return err ? err : size - remaining_size;
38f38657
AR
982}
983
4895768b
AR
984/*
985 * Adds an extended attribute to the list
986 */
38f38657
AR
987void simple_xattr_list_add(struct simple_xattrs *xattrs,
988 struct simple_xattr *new_xattr)
989{
990 spin_lock(&xattrs->lock);
991 list_add(&new_xattr->list, &xattrs->head);
992 spin_unlock(&xattrs->lock);
993}