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