]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/xattr.c
vfs: remove lives_below_in_same_fs()
[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>
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 /*
f1f2d871 49 * The trusted.* namespace can only be accessed by a privileged user.
e0ad7b07 50 */
51 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
52 return (capable(CAP_SYS_ADMIN) ? 0 : -EPERM);
53
f1f2d871
AG
54 /* In user.* namespace, only regular files and directories can have
55 * extended attributes. For sticky directories, only the owner and
56 * privileged user can write attributes.
57 */
e0ad7b07 58 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
f1f2d871
AG
59 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
60 return -EPERM;
61 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
3bd858ab 62 (mask & MAY_WRITE) && !is_owner_or_cap(inode))
e0ad7b07 63 return -EPERM;
64 }
65
66 return permission(inode, mask, NULL);
67}
68
5be196e5
CH
69int
70vfs_setxattr(struct dentry *dentry, char *name, void *value,
71 size_t size, int flags)
72{
73 struct inode *inode = dentry->d_inode;
74 int error;
75
e0ad7b07 76 error = xattr_permission(inode, name, MAY_WRITE);
77 if (error)
78 return error;
79
5be196e5
CH
80 mutex_lock(&inode->i_mutex);
81 error = security_inode_setxattr(dentry, name, value, size, flags);
82 if (error)
83 goto out;
84 error = -EOPNOTSUPP;
85 if (inode->i_op->setxattr) {
86 error = inode->i_op->setxattr(dentry, name, value, size, flags);
87 if (!error) {
88 fsnotify_xattr(dentry);
89 security_inode_post_setxattr(dentry, name, value,
90 size, flags);
91 }
92 } else if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 93 XATTR_SECURITY_PREFIX_LEN)) {
94 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
5be196e5
CH
95 error = security_inode_setsecurity(inode, suffix, value,
96 size, flags);
97 if (!error)
98 fsnotify_xattr(dentry);
99 }
100out:
101 mutex_unlock(&inode->i_mutex);
102 return error;
103}
104EXPORT_SYMBOL_GPL(vfs_setxattr);
105
42492594
DQ
106ssize_t
107xattr_getsecurity(struct inode *inode, const char *name, void *value,
108 size_t size)
109{
110 void *buffer = NULL;
111 ssize_t len;
112
113 if (!value || !size) {
114 len = security_inode_getsecurity(inode, name, &buffer, false);
115 goto out_noalloc;
116 }
117
118 len = security_inode_getsecurity(inode, name, &buffer, true);
119 if (len < 0)
120 return len;
121 if (size < len) {
122 len = -ERANGE;
123 goto out;
124 }
125 memcpy(value, buffer, len);
126out:
127 security_release_secctx(buffer, len);
128out_noalloc:
129 return len;
130}
131EXPORT_SYMBOL_GPL(xattr_getsecurity);
132
5be196e5
CH
133ssize_t
134vfs_getxattr(struct dentry *dentry, char *name, void *value, size_t size)
135{
136 struct inode *inode = dentry->d_inode;
137 int error;
138
e0ad7b07 139 error = xattr_permission(inode, name, MAY_READ);
140 if (error)
141 return error;
142
5be196e5
CH
143 error = security_inode_getxattr(dentry, name);
144 if (error)
145 return error;
146
5be196e5 147 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 148 XATTR_SECURITY_PREFIX_LEN)) {
149 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
42492594 150 int ret = xattr_getsecurity(inode, suffix, value, size);
5be196e5
CH
151 /*
152 * Only overwrite the return value if a security module
153 * is actually active.
154 */
4bea5805
DQ
155 if (ret == -EOPNOTSUPP)
156 goto nolsm;
157 return ret;
5be196e5 158 }
4bea5805
DQ
159nolsm:
160 if (inode->i_op->getxattr)
161 error = inode->i_op->getxattr(dentry, name, value, size);
162 else
163 error = -EOPNOTSUPP;
5be196e5
CH
164
165 return error;
166}
167EXPORT_SYMBOL_GPL(vfs_getxattr);
168
659564c8
BN
169ssize_t
170vfs_listxattr(struct dentry *d, char *list, size_t size)
171{
172 ssize_t error;
173
174 error = security_inode_listxattr(d);
175 if (error)
176 return error;
177 error = -EOPNOTSUPP;
178 if (d->d_inode->i_op && d->d_inode->i_op->listxattr) {
179 error = d->d_inode->i_op->listxattr(d, list, size);
180 } else {
181 error = security_inode_listsecurity(d->d_inode, list, size);
182 if (size && error > size)
183 error = -ERANGE;
184 }
185 return error;
186}
187EXPORT_SYMBOL_GPL(vfs_listxattr);
188
5be196e5
CH
189int
190vfs_removexattr(struct dentry *dentry, char *name)
191{
192 struct inode *inode = dentry->d_inode;
193 int error;
194
195 if (!inode->i_op->removexattr)
196 return -EOPNOTSUPP;
197
e0ad7b07 198 error = xattr_permission(inode, name, MAY_WRITE);
199 if (error)
200 return error;
201
5be196e5
CH
202 error = security_inode_removexattr(dentry, name);
203 if (error)
204 return error;
205
206 mutex_lock(&inode->i_mutex);
207 error = inode->i_op->removexattr(dentry, name);
208 mutex_unlock(&inode->i_mutex);
209
210 if (!error)
211 fsnotify_xattr(dentry);
212 return error;
213}
214EXPORT_SYMBOL_GPL(vfs_removexattr);
215
216
1da177e4
LT
217/*
218 * Extended attribute SET operations
219 */
220static long
221setxattr(struct dentry *d, char __user *name, void __user *value,
222 size_t size, int flags)
223{
224 int error;
225 void *kvalue = NULL;
226 char kname[XATTR_NAME_MAX + 1];
227
228 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
229 return -EINVAL;
230
231 error = strncpy_from_user(kname, name, sizeof(kname));
232 if (error == 0 || error == sizeof(kname))
233 error = -ERANGE;
234 if (error < 0)
235 return error;
236
237 if (size) {
238 if (size > XATTR_SIZE_MAX)
239 return -E2BIG;
240 kvalue = kmalloc(size, GFP_KERNEL);
241 if (!kvalue)
242 return -ENOMEM;
243 if (copy_from_user(kvalue, value, size)) {
244 kfree(kvalue);
245 return -EFAULT;
246 }
247 }
248
5be196e5 249 error = vfs_setxattr(d, kname, kvalue, size, flags);
f99d49ad 250 kfree(kvalue);
1da177e4
LT
251 return error;
252}
253
254asmlinkage long
255sys_setxattr(char __user *path, char __user *name, void __user *value,
256 size_t size, int flags)
257{
258 struct nameidata nd;
259 int error;
260
261 error = user_path_walk(path, &nd);
262 if (error)
263 return error;
18f335af
DH
264 error = mnt_want_write(nd.path.mnt);
265 if (!error) {
266 error = setxattr(nd.path.dentry, name, value, size, flags);
267 mnt_drop_write(nd.path.mnt);
268 }
1d957f9b 269 path_put(&nd.path);
1da177e4
LT
270 return error;
271}
272
273asmlinkage long
274sys_lsetxattr(char __user *path, char __user *name, void __user *value,
275 size_t size, int flags)
276{
277 struct nameidata nd;
278 int error;
279
280 error = user_path_walk_link(path, &nd);
281 if (error)
282 return error;
18f335af
DH
283 error = mnt_want_write(nd.path.mnt);
284 if (!error) {
285 error = setxattr(nd.path.dentry, name, value, size, flags);
286 mnt_drop_write(nd.path.mnt);
287 }
1d957f9b 288 path_put(&nd.path);
1da177e4
LT
289 return error;
290}
291
292asmlinkage long
293sys_fsetxattr(int fd, char __user *name, void __user *value,
294 size_t size, int flags)
295{
296 struct file *f;
73241ccc 297 struct dentry *dentry;
1da177e4
LT
298 int error = -EBADF;
299
300 f = fget(fd);
301 if (!f)
302 return error;
0f7fc9e4 303 dentry = f->f_path.dentry;
5a190ae6 304 audit_inode(NULL, dentry);
18f335af
DH
305 error = mnt_want_write(f->f_path.mnt);
306 if (!error) {
307 error = setxattr(dentry, name, value, size, flags);
308 mnt_drop_write(f->f_path.mnt);
309 }
1da177e4
LT
310 fput(f);
311 return error;
312}
313
314/*
315 * Extended attribute GET operations
316 */
317static ssize_t
318getxattr(struct dentry *d, char __user *name, void __user *value, size_t size)
319{
320 ssize_t error;
321 void *kvalue = NULL;
322 char kname[XATTR_NAME_MAX + 1];
323
324 error = strncpy_from_user(kname, name, sizeof(kname));
325 if (error == 0 || error == sizeof(kname))
326 error = -ERANGE;
327 if (error < 0)
328 return error;
329
330 if (size) {
331 if (size > XATTR_SIZE_MAX)
332 size = XATTR_SIZE_MAX;
d381d8a9 333 kvalue = kzalloc(size, GFP_KERNEL);
1da177e4
LT
334 if (!kvalue)
335 return -ENOMEM;
336 }
337
5be196e5 338 error = vfs_getxattr(d, kname, kvalue, size);
f549d6c1
SS
339 if (error > 0) {
340 if (size && copy_to_user(value, kvalue, error))
341 error = -EFAULT;
342 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
343 /* The file system tried to returned a value bigger
344 than XATTR_SIZE_MAX bytes. Not possible. */
345 error = -E2BIG;
1da177e4 346 }
f99d49ad 347 kfree(kvalue);
1da177e4
LT
348 return error;
349}
350
351asmlinkage ssize_t
352sys_getxattr(char __user *path, char __user *name, void __user *value,
353 size_t size)
354{
355 struct nameidata nd;
356 ssize_t error;
357
358 error = user_path_walk(path, &nd);
359 if (error)
360 return error;
4ac91378 361 error = getxattr(nd.path.dentry, name, value, size);
1d957f9b 362 path_put(&nd.path);
1da177e4
LT
363 return error;
364}
365
366asmlinkage ssize_t
367sys_lgetxattr(char __user *path, char __user *name, void __user *value,
368 size_t size)
369{
370 struct nameidata nd;
371 ssize_t error;
372
373 error = user_path_walk_link(path, &nd);
374 if (error)
375 return error;
4ac91378 376 error = getxattr(nd.path.dentry, name, value, size);
1d957f9b 377 path_put(&nd.path);
1da177e4
LT
378 return error;
379}
380
381asmlinkage ssize_t
382sys_fgetxattr(int fd, char __user *name, void __user *value, size_t size)
383{
384 struct file *f;
385 ssize_t error = -EBADF;
386
387 f = fget(fd);
388 if (!f)
389 return error;
5a190ae6 390 audit_inode(NULL, f->f_path.dentry);
0f7fc9e4 391 error = getxattr(f->f_path.dentry, name, value, size);
1da177e4
LT
392 fput(f);
393 return error;
394}
395
396/*
397 * Extended attribute LIST operations
398 */
399static ssize_t
400listxattr(struct dentry *d, char __user *list, size_t size)
401{
402 ssize_t error;
403 char *klist = NULL;
404
405 if (size) {
406 if (size > XATTR_LIST_MAX)
407 size = XATTR_LIST_MAX;
408 klist = kmalloc(size, GFP_KERNEL);
409 if (!klist)
410 return -ENOMEM;
411 }
412
659564c8 413 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
414 if (error > 0) {
415 if (size && copy_to_user(list, klist, error))
416 error = -EFAULT;
417 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
418 /* The file system tried to returned a list bigger
419 than XATTR_LIST_MAX bytes. Not possible. */
420 error = -E2BIG;
1da177e4 421 }
f99d49ad 422 kfree(klist);
1da177e4
LT
423 return error;
424}
425
426asmlinkage ssize_t
427sys_listxattr(char __user *path, char __user *list, size_t size)
428{
429 struct nameidata nd;
430 ssize_t error;
431
432 error = user_path_walk(path, &nd);
433 if (error)
434 return error;
4ac91378 435 error = listxattr(nd.path.dentry, list, size);
1d957f9b 436 path_put(&nd.path);
1da177e4
LT
437 return error;
438}
439
440asmlinkage ssize_t
441sys_llistxattr(char __user *path, char __user *list, size_t size)
442{
443 struct nameidata nd;
444 ssize_t error;
445
446 error = user_path_walk_link(path, &nd);
447 if (error)
448 return error;
4ac91378 449 error = listxattr(nd.path.dentry, list, size);
1d957f9b 450 path_put(&nd.path);
1da177e4
LT
451 return error;
452}
453
454asmlinkage ssize_t
455sys_flistxattr(int fd, char __user *list, size_t size)
456{
457 struct file *f;
458 ssize_t error = -EBADF;
459
460 f = fget(fd);
461 if (!f)
462 return error;
5a190ae6 463 audit_inode(NULL, f->f_path.dentry);
0f7fc9e4 464 error = listxattr(f->f_path.dentry, list, size);
1da177e4
LT
465 fput(f);
466 return error;
467}
468
469/*
470 * Extended attribute REMOVE operations
471 */
472static long
473removexattr(struct dentry *d, char __user *name)
474{
475 int error;
476 char kname[XATTR_NAME_MAX + 1];
477
478 error = strncpy_from_user(kname, name, sizeof(kname));
479 if (error == 0 || error == sizeof(kname))
480 error = -ERANGE;
481 if (error < 0)
482 return error;
483
5be196e5 484 return vfs_removexattr(d, kname);
1da177e4
LT
485}
486
487asmlinkage long
488sys_removexattr(char __user *path, char __user *name)
489{
490 struct nameidata nd;
491 int error;
492
493 error = user_path_walk(path, &nd);
494 if (error)
495 return error;
18f335af
DH
496 error = mnt_want_write(nd.path.mnt);
497 if (!error) {
498 error = removexattr(nd.path.dentry, name);
499 mnt_drop_write(nd.path.mnt);
500 }
1d957f9b 501 path_put(&nd.path);
1da177e4
LT
502 return error;
503}
504
505asmlinkage long
506sys_lremovexattr(char __user *path, char __user *name)
507{
508 struct nameidata nd;
509 int error;
510
511 error = user_path_walk_link(path, &nd);
512 if (error)
513 return error;
18f335af
DH
514 error = mnt_want_write(nd.path.mnt);
515 if (!error) {
516 error = removexattr(nd.path.dentry, name);
517 mnt_drop_write(nd.path.mnt);
518 }
1d957f9b 519 path_put(&nd.path);
1da177e4
LT
520 return error;
521}
522
523asmlinkage long
524sys_fremovexattr(int fd, char __user *name)
525{
526 struct file *f;
73241ccc 527 struct dentry *dentry;
1da177e4
LT
528 int error = -EBADF;
529
530 f = fget(fd);
531 if (!f)
532 return error;
0f7fc9e4 533 dentry = f->f_path.dentry;
5a190ae6 534 audit_inode(NULL, dentry);
18f335af
DH
535 error = mnt_want_write(f->f_path.mnt);
536 if (!error) {
537 error = removexattr(dentry, name);
538 mnt_drop_write(f->f_path.mnt);
539 }
1da177e4
LT
540 fput(f);
541 return error;
542}
543
544
545static const char *
546strcmp_prefix(const char *a, const char *a_prefix)
547{
548 while (*a_prefix && *a == *a_prefix) {
549 a++;
550 a_prefix++;
551 }
552 return *a_prefix ? NULL : a;
553}
554
555/*
556 * In order to implement different sets of xattr operations for each xattr
557 * prefix with the generic xattr API, a filesystem should create a
558 * null-terminated array of struct xattr_handler (one for each prefix) and
559 * hang a pointer to it off of the s_xattr field of the superblock.
560 *
561 * The generic_fooxattr() functions will use this list to dispatch xattr
562 * operations to the correct xattr_handler.
563 */
564#define for_each_xattr_handler(handlers, handler) \
565 for ((handler) = *(handlers)++; \
566 (handler) != NULL; \
567 (handler) = *(handlers)++)
568
569/*
570 * Find the xattr_handler with the matching prefix.
571 */
572static struct xattr_handler *
573xattr_resolve_name(struct xattr_handler **handlers, const char **name)
574{
575 struct xattr_handler *handler;
576
577 if (!*name)
578 return NULL;
579
580 for_each_xattr_handler(handlers, handler) {
581 const char *n = strcmp_prefix(*name, handler->prefix);
582 if (n) {
583 *name = n;
584 break;
585 }
586 }
587 return handler;
588}
589
590/*
591 * Find the handler for the prefix and dispatch its get() operation.
592 */
593ssize_t
594generic_getxattr(struct dentry *dentry, const char *name, void *buffer, size_t size)
595{
596 struct xattr_handler *handler;
597 struct inode *inode = dentry->d_inode;
598
599 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
600 if (!handler)
601 return -EOPNOTSUPP;
602 return handler->get(inode, name, buffer, size);
603}
604
605/*
606 * Combine the results of the list() operation from every xattr_handler in the
607 * list.
608 */
609ssize_t
610generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
611{
612 struct inode *inode = dentry->d_inode;
613 struct xattr_handler *handler, **handlers = inode->i_sb->s_xattr;
614 unsigned int size = 0;
615
616 if (!buffer) {
617 for_each_xattr_handler(handlers, handler)
618 size += handler->list(inode, NULL, 0, NULL, 0);
619 } else {
620 char *buf = buffer;
621
622 for_each_xattr_handler(handlers, handler) {
623 size = handler->list(inode, buf, buffer_size, NULL, 0);
624 if (size > buffer_size)
625 return -ERANGE;
626 buf += size;
627 buffer_size -= size;
628 }
629 size = buf - buffer;
630 }
631 return size;
632}
633
634/*
635 * Find the handler for the prefix and dispatch its set() operation.
636 */
637int
638generic_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags)
639{
640 struct xattr_handler *handler;
641 struct inode *inode = dentry->d_inode;
642
643 if (size == 0)
644 value = ""; /* empty EA, do not remove */
645 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
646 if (!handler)
647 return -EOPNOTSUPP;
648 return handler->set(inode, name, value, size, flags);
649}
650
651/*
652 * Find the handler for the prefix and dispatch its set() operation to remove
653 * any associated extended attribute.
654 */
655int
656generic_removexattr(struct dentry *dentry, const char *name)
657{
658 struct xattr_handler *handler;
659 struct inode *inode = dentry->d_inode;
660
661 handler = xattr_resolve_name(inode->i_sb->s_xattr, &name);
662 if (!handler)
663 return -EOPNOTSUPP;
664 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
665}
666
667EXPORT_SYMBOL(generic_getxattr);
668EXPORT_SYMBOL(generic_listxattr);
669EXPORT_SYMBOL(generic_setxattr);
670EXPORT_SYMBOL(generic_removexattr);