1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 File: fs/xattr.c
4
5 Extended attribute handling.
6
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
9 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
10 */
11 #include <linux/fs.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/xattr.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/security.h>
18 #include <linux/evm.h>
19 #include <linux/syscalls.h>
20 #include <linux/export.h>
21 #include <linux/fsnotify.h>
22 #include <linux/audit.h>
23 #include <linux/vmalloc.h>
24 #include <linux/posix_acl_xattr.h>
25
26 #include <linux/uaccess.h>
27
28 #include "internal.h"
29
30 static const char *
strcmp_prefix(const char * a,const char * a_prefix)31 strcmp_prefix(const char *a, const char *a_prefix)
32 {
33 while (*a_prefix && *a == *a_prefix) {
34 a++;
35 a_prefix++;
36 }
37 return *a_prefix ? NULL : a;
38 }
39
40 /*
41 * In order to implement different sets of xattr operations for each xattr
42 * prefix, a filesystem should create a null-terminated array of struct
43 * xattr_handler (one for each prefix) and hang a pointer to it off of the
44 * s_xattr field of the superblock.
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 */
55 static const struct xattr_handler *
xattr_resolve_name(struct inode * inode,const char ** name)56 xattr_resolve_name(struct inode *inode, const char **name)
57 {
58 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
59 const struct xattr_handler *handler;
60
61 if (!(inode->i_opflags & IOP_XATTR)) {
62 if (unlikely(is_bad_inode(inode)))
63 return ERR_PTR(-EIO);
64 return ERR_PTR(-EOPNOTSUPP);
65 }
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
83 /*
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
86 */
87 static int
xattr_permission(struct user_namespace * mnt_userns,struct inode * inode,const char * name,int mask)88 xattr_permission(struct user_namespace *mnt_userns, struct inode *inode,
89 const char *name, int mask)
90 {
91 /*
92 * We can never set or remove an extended attribute on a read-only
93 * filesystem or on an immutable / append-only inode.
94 */
95 if (mask & MAY_WRITE) {
96 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
97 return -EPERM;
98 /*
99 * Updating an xattr will likely cause i_uid and i_gid
100 * to be writen back improperly if their true value is
101 * unknown to the vfs.
102 */
103 if (HAS_UNMAPPED_ID(mnt_userns, inode))
104 return -EPERM;
105 }
106
107 /*
108 * No restriction for security.* and system.* from the VFS. Decision
109 * on these is left to the underlying filesystem / security module.
110 */
111 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
112 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
113 return 0;
114
115 /*
116 * The trusted.* namespace can only be accessed by privileged users.
117 */
118 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
119 if (!capable(CAP_SYS_ADMIN))
120 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
121 return 0;
122 }
123
124 /*
125 * In the user.* namespace, only regular files and directories can have
126 * extended attributes. For sticky directories, only the owner and
127 * privileged users can write attributes.
128 */
129 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
130 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
131 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
132 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
133 (mask & MAY_WRITE) &&
134 !inode_owner_or_capable(mnt_userns, inode))
135 return -EPERM;
136 }
137
138 return inode_permission(mnt_userns, inode, mask);
139 }
140
141 /*
142 * Look for any handler that deals with the specified namespace.
143 */
144 int
xattr_supported_namespace(struct inode * inode,const char * prefix)145 xattr_supported_namespace(struct inode *inode, const char *prefix)
146 {
147 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
148 const struct xattr_handler *handler;
149 size_t preflen;
150
151 if (!(inode->i_opflags & IOP_XATTR)) {
152 if (unlikely(is_bad_inode(inode)))
153 return -EIO;
154 return -EOPNOTSUPP;
155 }
156
157 preflen = strlen(prefix);
158
159 for_each_xattr_handler(handlers, handler) {
160 if (!strncmp(xattr_prefix(handler), prefix, preflen))
161 return 0;
162 }
163
164 return -EOPNOTSUPP;
165 }
166 EXPORT_SYMBOL(xattr_supported_namespace);
167
168 int
__vfs_setxattr(struct user_namespace * mnt_userns,struct dentry * dentry,struct inode * inode,const char * name,const void * value,size_t size,int flags)169 __vfs_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
170 struct inode *inode, const char *name, const void *value,
171 size_t size, int flags)
172 {
173 const struct xattr_handler *handler;
174
175 handler = xattr_resolve_name(inode, &name);
176 if (IS_ERR(handler))
177 return PTR_ERR(handler);
178 if (!handler->set)
179 return -EOPNOTSUPP;
180 if (size == 0)
181 value = ""; /* empty EA, do not remove */
182 return handler->set(handler, mnt_userns, dentry, inode, name, value,
183 size, flags);
184 }
185 EXPORT_SYMBOL(__vfs_setxattr);
186
187 /**
188 * __vfs_setxattr_noperm - perform setxattr operation without performing
189 * permission checks.
190 *
191 * @mnt_userns: user namespace of the mount the inode was found from
192 * @dentry: object to perform setxattr on
193 * @name: xattr name to set
194 * @value: value to set @name to
195 * @size: size of @value
196 * @flags: flags to pass into filesystem operations
197 *
198 * returns the result of the internal setxattr or setsecurity operations.
199 *
200 * This function requires the caller to lock the inode's i_mutex before it
201 * is executed. It also assumes that the caller will make the appropriate
202 * permission checks.
203 */
__vfs_setxattr_noperm(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,const void * value,size_t size,int flags)204 int __vfs_setxattr_noperm(struct user_namespace *mnt_userns,
205 struct dentry *dentry, const char *name,
206 const void *value, size_t size, int flags)
207 {
208 struct inode *inode = dentry->d_inode;
209 int error = -EAGAIN;
210 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
211 XATTR_SECURITY_PREFIX_LEN);
212
213 if (issec)
214 inode->i_flags &= ~S_NOSEC;
215 if (inode->i_opflags & IOP_XATTR) {
216 error = __vfs_setxattr(mnt_userns, dentry, inode, name, value,
217 size, flags);
218 if (!error) {
219 fsnotify_xattr(dentry);
220 security_inode_post_setxattr(dentry, name, value,
221 size, flags);
222 }
223 } else {
224 if (unlikely(is_bad_inode(inode)))
225 return -EIO;
226 }
227 if (error == -EAGAIN) {
228 error = -EOPNOTSUPP;
229
230 if (issec) {
231 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
232
233 error = security_inode_setsecurity(inode, suffix, value,
234 size, flags);
235 if (!error)
236 fsnotify_xattr(dentry);
237 }
238 }
239
240 return error;
241 }
242
243 /**
244 * __vfs_setxattr_locked - set an extended attribute while holding the inode
245 * lock
246 *
247 * @mnt_userns: user namespace of the mount of the target inode
248 * @dentry: object to perform setxattr on
249 * @name: xattr name to set
250 * @value: value to set @name to
251 * @size: size of @value
252 * @flags: flags to pass into filesystem operations
253 * @delegated_inode: on return, will contain an inode pointer that
254 * a delegation was broken on, NULL if none.
255 */
256 int
__vfs_setxattr_locked(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,const void * value,size_t size,int flags,struct inode ** delegated_inode)257 __vfs_setxattr_locked(struct user_namespace *mnt_userns, struct dentry *dentry,
258 const char *name, const void *value, size_t size,
259 int flags, struct inode **delegated_inode)
260 {
261 struct inode *inode = dentry->d_inode;
262 int error;
263
264 error = xattr_permission(mnt_userns, inode, name, MAY_WRITE);
265 if (error)
266 return error;
267
268 error = security_inode_setxattr(mnt_userns, dentry, name, value, size,
269 flags);
270 if (error)
271 goto out;
272
273 error = try_break_deleg(inode, delegated_inode);
274 if (error)
275 goto out;
276
277 error = __vfs_setxattr_noperm(mnt_userns, dentry, name, value,
278 size, flags);
279
280 out:
281 return error;
282 }
283 EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
284
285 int
vfs_setxattr(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,const void * value,size_t size,int flags)286 vfs_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
287 const char *name, const void *value, size_t size, int flags)
288 {
289 struct inode *inode = dentry->d_inode;
290 struct inode *delegated_inode = NULL;
291 const void *orig_value = value;
292 int error;
293
294 if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
295 error = cap_convert_nscap(mnt_userns, dentry, &value, size);
296 if (error < 0)
297 return error;
298 size = error;
299 }
300
301 retry_deleg:
302 inode_lock(inode);
303 error = __vfs_setxattr_locked(mnt_userns, dentry, name, value, size,
304 flags, &delegated_inode);
305 inode_unlock(inode);
306
307 if (delegated_inode) {
308 error = break_deleg_wait(&delegated_inode);
309 if (!error)
310 goto retry_deleg;
311 }
312 if (value != orig_value)
313 kfree(value);
314
315 return error;
316 }
317 EXPORT_SYMBOL_NS_GPL(vfs_setxattr, ANDROID_GKI_VFS_EXPORT_ONLY);
318
319 static ssize_t
xattr_getsecurity(struct user_namespace * mnt_userns,struct inode * inode,const char * name,void * value,size_t size)320 xattr_getsecurity(struct user_namespace *mnt_userns, struct inode *inode,
321 const char *name, void *value, size_t size)
322 {
323 void *buffer = NULL;
324 ssize_t len;
325
326 if (!value || !size) {
327 len = security_inode_getsecurity(mnt_userns, inode, name,
328 &buffer, false);
329 goto out_noalloc;
330 }
331
332 len = security_inode_getsecurity(mnt_userns, inode, name, &buffer,
333 true);
334 if (len < 0)
335 return len;
336 if (size < len) {
337 len = -ERANGE;
338 goto out;
339 }
340 memcpy(value, buffer, len);
341 out:
342 kfree(buffer);
343 out_noalloc:
344 return len;
345 }
346
347 /*
348 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
349 *
350 * Allocate memory, if not already allocated, or re-allocate correct size,
351 * before retrieving the extended attribute.
352 *
353 * Returns the result of alloc, if failed, or the getxattr operation.
354 */
355 ssize_t
vfs_getxattr_alloc(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,char ** xattr_value,size_t xattr_size,gfp_t flags)356 vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
357 const char *name, char **xattr_value, size_t xattr_size,
358 gfp_t flags)
359 {
360 const struct xattr_handler *handler;
361 struct inode *inode = dentry->d_inode;
362 char *value = *xattr_value;
363 int error;
364
365 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
366 if (error)
367 return error;
368
369 handler = xattr_resolve_name(inode, &name);
370 if (IS_ERR(handler))
371 return PTR_ERR(handler);
372 if (!handler->get)
373 return -EOPNOTSUPP;
374 error = handler->get(handler, dentry, inode, name, NULL, 0);
375 if (error < 0)
376 return error;
377
378 if (!value || (error > xattr_size)) {
379 value = krealloc(*xattr_value, error + 1, flags);
380 if (!value)
381 return -ENOMEM;
382 memset(value, 0, error + 1);
383 }
384
385 error = handler->get(handler, dentry, inode, name, value, error);
386 *xattr_value = value;
387 return error;
388 }
389
390 ssize_t
__vfs_getxattr(struct dentry * dentry,struct inode * inode,const char * name,void * value,size_t size)391 __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
392 void *value, size_t size)
393 {
394 const struct xattr_handler *handler;
395
396 handler = xattr_resolve_name(inode, &name);
397 if (IS_ERR(handler))
398 return PTR_ERR(handler);
399 if (!handler->get)
400 return -EOPNOTSUPP;
401 return handler->get(handler, dentry, inode, name, value, size);
402 }
403 EXPORT_SYMBOL(__vfs_getxattr);
404
405 ssize_t
vfs_getxattr(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,void * value,size_t size)406 vfs_getxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
407 const char *name, void *value, size_t size)
408 {
409 struct inode *inode = dentry->d_inode;
410 int error;
411
412 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
413 if (error)
414 return error;
415
416 error = security_inode_getxattr(dentry, name);
417 if (error)
418 return error;
419
420 if (!strncmp(name, XATTR_SECURITY_PREFIX,
421 XATTR_SECURITY_PREFIX_LEN)) {
422 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
423 int ret = xattr_getsecurity(mnt_userns, inode, suffix, value,
424 size);
425 /*
426 * Only overwrite the return value if a security module
427 * is actually active.
428 */
429 if (ret == -EOPNOTSUPP)
430 goto nolsm;
431 return ret;
432 }
433 nolsm:
434 return __vfs_getxattr(dentry, inode, name, value, size);
435 }
436 EXPORT_SYMBOL_NS_GPL(vfs_getxattr, ANDROID_GKI_VFS_EXPORT_ONLY);
437
438 ssize_t
vfs_listxattr(struct dentry * dentry,char * list,size_t size)439 vfs_listxattr(struct dentry *dentry, char *list, size_t size)
440 {
441 struct inode *inode = d_inode(dentry);
442 ssize_t error;
443
444 error = security_inode_listxattr(dentry);
445 if (error)
446 return error;
447 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
448 error = inode->i_op->listxattr(dentry, list, size);
449 } else {
450 error = security_inode_listsecurity(inode, list, size);
451 if (size && error > size)
452 error = -ERANGE;
453 }
454 return error;
455 }
456 EXPORT_SYMBOL_NS_GPL(vfs_listxattr, ANDROID_GKI_VFS_EXPORT_ONLY);
457
458 int
__vfs_removexattr(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name)459 __vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
460 const char *name)
461 {
462 struct inode *inode = d_inode(dentry);
463 const struct xattr_handler *handler;
464
465 handler = xattr_resolve_name(inode, &name);
466 if (IS_ERR(handler))
467 return PTR_ERR(handler);
468 if (!handler->set)
469 return -EOPNOTSUPP;
470 return handler->set(handler, mnt_userns, dentry, inode, name, NULL, 0,
471 XATTR_REPLACE);
472 }
473 EXPORT_SYMBOL(__vfs_removexattr);
474
475 /**
476 * __vfs_removexattr_locked - set an extended attribute while holding the inode
477 * lock
478 *
479 * @mnt_userns: user namespace of the mount of the target inode
480 * @dentry: object to perform setxattr on
481 * @name: name of xattr to remove
482 * @delegated_inode: on return, will contain an inode pointer that
483 * a delegation was broken on, NULL if none.
484 */
485 int
__vfs_removexattr_locked(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name,struct inode ** delegated_inode)486 __vfs_removexattr_locked(struct user_namespace *mnt_userns,
487 struct dentry *dentry, const char *name,
488 struct inode **delegated_inode)
489 {
490 struct inode *inode = dentry->d_inode;
491 int error;
492
493 error = xattr_permission(mnt_userns, inode, name, MAY_WRITE);
494 if (error)
495 return error;
496
497 error = security_inode_removexattr(mnt_userns, dentry, name);
498 if (error)
499 goto out;
500
501 error = try_break_deleg(inode, delegated_inode);
502 if (error)
503 goto out;
504
505 error = __vfs_removexattr(mnt_userns, dentry, name);
506
507 if (!error) {
508 fsnotify_xattr(dentry);
509 evm_inode_post_removexattr(dentry, name);
510 }
511
512 out:
513 return error;
514 }
515 EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
516
517 int
vfs_removexattr(struct user_namespace * mnt_userns,struct dentry * dentry,const char * name)518 vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
519 const char *name)
520 {
521 struct inode *inode = dentry->d_inode;
522 struct inode *delegated_inode = NULL;
523 int error;
524
525 retry_deleg:
526 inode_lock(inode);
527 error = __vfs_removexattr_locked(mnt_userns, dentry,
528 name, &delegated_inode);
529 inode_unlock(inode);
530
531 if (delegated_inode) {
532 error = break_deleg_wait(&delegated_inode);
533 if (!error)
534 goto retry_deleg;
535 }
536
537 return error;
538 }
539 EXPORT_SYMBOL_GPL(vfs_removexattr);
540
541 /*
542 * Extended attribute SET operations
543 */
544
setxattr_copy(const char __user * name,struct xattr_ctx * ctx)545 int setxattr_copy(const char __user *name, struct xattr_ctx *ctx)
546 {
547 int error;
548
549 if (ctx->flags & ~(XATTR_CREATE|XATTR_REPLACE))
550 return -EINVAL;
551
552 error = strncpy_from_user(ctx->kname->name, name,
553 sizeof(ctx->kname->name));
554 if (error == 0 || error == sizeof(ctx->kname->name))
555 return -ERANGE;
556 if (error < 0)
557 return error;
558
559 error = 0;
560 if (ctx->size) {
561 if (ctx->size > XATTR_SIZE_MAX)
562 return -E2BIG;
563
564 ctx->kvalue = vmemdup_user(ctx->cvalue, ctx->size);
565 if (IS_ERR(ctx->kvalue)) {
566 error = PTR_ERR(ctx->kvalue);
567 ctx->kvalue = NULL;
568 }
569 }
570
571 return error;
572 }
573
setxattr_convert(struct user_namespace * mnt_userns,struct dentry * d,struct xattr_ctx * ctx)574 static void setxattr_convert(struct user_namespace *mnt_userns,
575 struct dentry *d, struct xattr_ctx *ctx)
576 {
577 if (ctx->size &&
578 ((strcmp(ctx->kname->name, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
579 (strcmp(ctx->kname->name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)))
580 posix_acl_fix_xattr_from_user(mnt_userns, d_inode(d),
581 ctx->kvalue, ctx->size);
582 }
583
do_setxattr(struct user_namespace * mnt_userns,struct dentry * dentry,struct xattr_ctx * ctx)584 int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
585 struct xattr_ctx *ctx)
586 {
587 setxattr_convert(mnt_userns, dentry, ctx);
588 return vfs_setxattr(mnt_userns, dentry, ctx->kname->name,
589 ctx->kvalue, ctx->size, ctx->flags);
590 }
591
592 static long
setxattr(struct user_namespace * mnt_userns,struct dentry * d,const char __user * name,const void __user * value,size_t size,int flags)593 setxattr(struct user_namespace *mnt_userns, struct dentry *d,
594 const char __user *name, const void __user *value, size_t size,
595 int flags)
596 {
597 struct xattr_name kname;
598 struct xattr_ctx ctx = {
599 .cvalue = value,
600 .kvalue = NULL,
601 .size = size,
602 .kname = &kname,
603 .flags = flags,
604 };
605 int error;
606
607 error = setxattr_copy(name, &ctx);
608 if (error)
609 return error;
610
611 error = do_setxattr(mnt_userns, d, &ctx);
612
613 kvfree(ctx.kvalue);
614 return error;
615 }
616
path_setxattr(const char __user * pathname,const char __user * name,const void __user * value,size_t size,int flags,unsigned int lookup_flags)617 static int path_setxattr(const char __user *pathname,
618 const char __user *name, const void __user *value,
619 size_t size, int flags, unsigned int lookup_flags)
620 {
621 struct path path;
622 int error;
623
624 retry:
625 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
626 if (error)
627 return error;
628 error = mnt_want_write(path.mnt);
629 if (!error) {
630 error = setxattr(mnt_user_ns(path.mnt), path.dentry, name,
631 value, size, flags);
632 mnt_drop_write(path.mnt);
633 }
634 path_put(&path);
635 if (retry_estale(error, lookup_flags)) {
636 lookup_flags |= LOOKUP_REVAL;
637 goto retry;
638 }
639 return error;
640 }
641
SYSCALL_DEFINE5(setxattr,const char __user *,pathname,const char __user *,name,const void __user *,value,size_t,size,int,flags)642 SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
643 const char __user *, name, const void __user *, value,
644 size_t, size, int, flags)
645 {
646 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
647 }
648
SYSCALL_DEFINE5(lsetxattr,const char __user *,pathname,const char __user *,name,const void __user *,value,size_t,size,int,flags)649 SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
650 const char __user *, name, const void __user *, value,
651 size_t, size, int, flags)
652 {
653 return path_setxattr(pathname, name, value, size, flags, 0);
654 }
655
SYSCALL_DEFINE5(fsetxattr,int,fd,const char __user *,name,const void __user *,value,size_t,size,int,flags)656 SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
657 const void __user *,value, size_t, size, int, flags)
658 {
659 struct fd f = fdget(fd);
660 int error = -EBADF;
661
662 if (!f.file)
663 return error;
664 audit_file(f.file);
665 error = mnt_want_write_file(f.file);
666 if (!error) {
667 error = setxattr(file_mnt_user_ns(f.file),
668 f.file->f_path.dentry, name,
669 value, size, flags);
670 mnt_drop_write_file(f.file);
671 }
672 fdput(f);
673 return error;
674 }
675
676 /*
677 * Extended attribute GET operations
678 */
679 static ssize_t
getxattr(struct user_namespace * mnt_userns,struct dentry * d,const char __user * name,void __user * value,size_t size)680 getxattr(struct user_namespace *mnt_userns, struct dentry *d,
681 const char __user *name, void __user *value, size_t size)
682 {
683 ssize_t error;
684 void *kvalue = NULL;
685 char kname[XATTR_NAME_MAX + 1];
686
687 error = strncpy_from_user(kname, name, sizeof(kname));
688 if (error == 0 || error == sizeof(kname))
689 error = -ERANGE;
690 if (error < 0)
691 return error;
692
693 if (size) {
694 if (size > XATTR_SIZE_MAX)
695 size = XATTR_SIZE_MAX;
696 kvalue = kvzalloc(size, GFP_KERNEL);
697 if (!kvalue)
698 return -ENOMEM;
699 }
700
701 error = vfs_getxattr(mnt_userns, d, kname, kvalue, size);
702 if (error > 0) {
703 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
704 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
705 posix_acl_fix_xattr_to_user(mnt_userns, d_inode(d),
706 kvalue, error);
707 if (size && copy_to_user(value, kvalue, error))
708 error = -EFAULT;
709 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
710 /* The file system tried to returned a value bigger
711 than XATTR_SIZE_MAX bytes. Not possible. */
712 error = -E2BIG;
713 }
714
715 kvfree(kvalue);
716
717 return error;
718 }
719
path_getxattr(const char __user * pathname,const char __user * name,void __user * value,size_t size,unsigned int lookup_flags)720 static ssize_t path_getxattr(const char __user *pathname,
721 const char __user *name, void __user *value,
722 size_t size, unsigned int lookup_flags)
723 {
724 struct path path;
725 ssize_t error;
726 retry:
727 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
728 if (error)
729 return error;
730 error = getxattr(mnt_user_ns(path.mnt), path.dentry, name, value, size);
731 path_put(&path);
732 if (retry_estale(error, lookup_flags)) {
733 lookup_flags |= LOOKUP_REVAL;
734 goto retry;
735 }
736 return error;
737 }
738
SYSCALL_DEFINE4(getxattr,const char __user *,pathname,const char __user *,name,void __user *,value,size_t,size)739 SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
740 const char __user *, name, void __user *, value, size_t, size)
741 {
742 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
743 }
744
SYSCALL_DEFINE4(lgetxattr,const char __user *,pathname,const char __user *,name,void __user *,value,size_t,size)745 SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
746 const char __user *, name, void __user *, value, size_t, size)
747 {
748 return path_getxattr(pathname, name, value, size, 0);
749 }
750
SYSCALL_DEFINE4(fgetxattr,int,fd,const char __user *,name,void __user *,value,size_t,size)751 SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
752 void __user *, value, size_t, size)
753 {
754 struct fd f = fdget(fd);
755 ssize_t error = -EBADF;
756
757 if (!f.file)
758 return error;
759 audit_file(f.file);
760 error = getxattr(file_mnt_user_ns(f.file), f.file->f_path.dentry,
761 name, value, size);
762 fdput(f);
763 return error;
764 }
765
766 /*
767 * Extended attribute LIST operations
768 */
769 static ssize_t
listxattr(struct dentry * d,char __user * list,size_t size)770 listxattr(struct dentry *d, char __user *list, size_t size)
771 {
772 ssize_t error;
773 char *klist = NULL;
774
775 if (size) {
776 if (size > XATTR_LIST_MAX)
777 size = XATTR_LIST_MAX;
778 klist = kvmalloc(size, GFP_KERNEL);
779 if (!klist)
780 return -ENOMEM;
781 }
782
783 error = vfs_listxattr(d, klist, size);
784 if (error > 0) {
785 if (size && copy_to_user(list, klist, error))
786 error = -EFAULT;
787 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
788 /* The file system tried to returned a list bigger
789 than XATTR_LIST_MAX bytes. Not possible. */
790 error = -E2BIG;
791 }
792
793 kvfree(klist);
794
795 return error;
796 }
797
path_listxattr(const char __user * pathname,char __user * list,size_t size,unsigned int lookup_flags)798 static ssize_t path_listxattr(const char __user *pathname, char __user *list,
799 size_t size, unsigned int lookup_flags)
800 {
801 struct path path;
802 ssize_t error;
803 retry:
804 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
805 if (error)
806 return error;
807 error = listxattr(path.dentry, list, size);
808 path_put(&path);
809 if (retry_estale(error, lookup_flags)) {
810 lookup_flags |= LOOKUP_REVAL;
811 goto retry;
812 }
813 return error;
814 }
815
SYSCALL_DEFINE3(listxattr,const char __user *,pathname,char __user *,list,size_t,size)816 SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
817 size_t, size)
818 {
819 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
820 }
821
SYSCALL_DEFINE3(llistxattr,const char __user *,pathname,char __user *,list,size_t,size)822 SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
823 size_t, size)
824 {
825 return path_listxattr(pathname, list, size, 0);
826 }
827
SYSCALL_DEFINE3(flistxattr,int,fd,char __user *,list,size_t,size)828 SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
829 {
830 struct fd f = fdget(fd);
831 ssize_t error = -EBADF;
832
833 if (!f.file)
834 return error;
835 audit_file(f.file);
836 error = listxattr(f.file->f_path.dentry, list, size);
837 fdput(f);
838 return error;
839 }
840
841 /*
842 * Extended attribute REMOVE operations
843 */
844 static long
removexattr(struct user_namespace * mnt_userns,struct dentry * d,const char __user * name)845 removexattr(struct user_namespace *mnt_userns, struct dentry *d,
846 const char __user *name)
847 {
848 int error;
849 char kname[XATTR_NAME_MAX + 1];
850
851 error = strncpy_from_user(kname, name, sizeof(kname));
852 if (error == 0 || error == sizeof(kname))
853 error = -ERANGE;
854 if (error < 0)
855 return error;
856
857 return vfs_removexattr(mnt_userns, d, kname);
858 }
859
path_removexattr(const char __user * pathname,const char __user * name,unsigned int lookup_flags)860 static int path_removexattr(const char __user *pathname,
861 const char __user *name, unsigned int lookup_flags)
862 {
863 struct path path;
864 int error;
865 retry:
866 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
867 if (error)
868 return error;
869 error = mnt_want_write(path.mnt);
870 if (!error) {
871 error = removexattr(mnt_user_ns(path.mnt), path.dentry, name);
872 mnt_drop_write(path.mnt);
873 }
874 path_put(&path);
875 if (retry_estale(error, lookup_flags)) {
876 lookup_flags |= LOOKUP_REVAL;
877 goto retry;
878 }
879 return error;
880 }
881
SYSCALL_DEFINE2(removexattr,const char __user *,pathname,const char __user *,name)882 SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
883 const char __user *, name)
884 {
885 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
886 }
887
SYSCALL_DEFINE2(lremovexattr,const char __user *,pathname,const char __user *,name)888 SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
889 const char __user *, name)
890 {
891 return path_removexattr(pathname, name, 0);
892 }
893
SYSCALL_DEFINE2(fremovexattr,int,fd,const char __user *,name)894 SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
895 {
896 struct fd f = fdget(fd);
897 int error = -EBADF;
898
899 if (!f.file)
900 return error;
901 audit_file(f.file);
902 error = mnt_want_write_file(f.file);
903 if (!error) {
904 error = removexattr(file_mnt_user_ns(f.file),
905 f.file->f_path.dentry, name);
906 mnt_drop_write_file(f.file);
907 }
908 fdput(f);
909 return error;
910 }
911
912 /*
913 * Combine the results of the list() operation from every xattr_handler in the
914 * list.
915 */
916 ssize_t
generic_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)917 generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
918 {
919 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
920 unsigned int size = 0;
921
922 if (!buffer) {
923 for_each_xattr_handler(handlers, handler) {
924 if (!handler->name ||
925 (handler->list && !handler->list(dentry)))
926 continue;
927 size += strlen(handler->name) + 1;
928 }
929 } else {
930 char *buf = buffer;
931 size_t len;
932
933 for_each_xattr_handler(handlers, handler) {
934 if (!handler->name ||
935 (handler->list && !handler->list(dentry)))
936 continue;
937 len = strlen(handler->name);
938 if (len + 1 > buffer_size)
939 return -ERANGE;
940 memcpy(buf, handler->name, len + 1);
941 buf += len + 1;
942 buffer_size -= len + 1;
943 }
944 size = buf - buffer;
945 }
946 return size;
947 }
948 EXPORT_SYMBOL(generic_listxattr);
949
950 /**
951 * xattr_full_name - Compute full attribute name from suffix
952 *
953 * @handler: handler of the xattr_handler operation
954 * @name: name passed to the xattr_handler operation
955 *
956 * The get and set xattr handler operations are called with the remainder of
957 * the attribute name after skipping the handler's prefix: for example, "foo"
958 * is passed to the get operation of a handler with prefix "user." to get
959 * attribute "user.foo". The full name is still "there" in the name though.
960 *
961 * Note: the list xattr handler operation when called from the vfs is passed a
962 * NULL name; some file systems use this operation internally, with varying
963 * semantics.
964 */
xattr_full_name(const struct xattr_handler * handler,const char * name)965 const char *xattr_full_name(const struct xattr_handler *handler,
966 const char *name)
967 {
968 size_t prefix_len = strlen(xattr_prefix(handler));
969
970 return name - prefix_len;
971 }
972 EXPORT_SYMBOL(xattr_full_name);
973
974 /*
975 * Allocate new xattr and copy in the value; but leave the name to callers.
976 */
simple_xattr_alloc(const void * value,size_t size)977 struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
978 {
979 struct simple_xattr *new_xattr;
980 size_t len;
981
982 /* wrap around? */
983 len = sizeof(*new_xattr) + size;
984 if (len < sizeof(*new_xattr))
985 return NULL;
986
987 new_xattr = kvmalloc(len, GFP_KERNEL);
988 if (!new_xattr)
989 return NULL;
990
991 new_xattr->size = size;
992 memcpy(new_xattr->value, value, size);
993 return new_xattr;
994 }
995
996 /*
997 * xattr GET operation for in-memory/pseudo filesystems
998 */
simple_xattr_get(struct simple_xattrs * xattrs,const char * name,void * buffer,size_t size)999 int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
1000 void *buffer, size_t size)
1001 {
1002 struct simple_xattr *xattr;
1003 int ret = -ENODATA;
1004
1005 spin_lock(&xattrs->lock);
1006 list_for_each_entry(xattr, &xattrs->head, list) {
1007 if (strcmp(name, xattr->name))
1008 continue;
1009
1010 ret = xattr->size;
1011 if (buffer) {
1012 if (size < xattr->size)
1013 ret = -ERANGE;
1014 else
1015 memcpy(buffer, xattr->value, xattr->size);
1016 }
1017 break;
1018 }
1019 spin_unlock(&xattrs->lock);
1020 return ret;
1021 }
1022
1023 /**
1024 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
1025 * @xattrs: target simple_xattr list
1026 * @name: name of the extended attribute
1027 * @value: value of the xattr. If %NULL, will remove the attribute.
1028 * @size: size of the new xattr
1029 * @flags: %XATTR_{CREATE|REPLACE}
1030 * @removed_size: returns size of the removed xattr, -1 if none removed
1031 *
1032 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
1033 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
1034 * otherwise, fails with -ENODATA.
1035 *
1036 * Returns 0 on success, -errno on failure.
1037 */
simple_xattr_set(struct simple_xattrs * xattrs,const char * name,const void * value,size_t size,int flags,ssize_t * removed_size)1038 int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
1039 const void *value, size_t size, int flags,
1040 ssize_t *removed_size)
1041 {
1042 struct simple_xattr *xattr;
1043 struct simple_xattr *new_xattr = NULL;
1044 int err = 0;
1045
1046 if (removed_size)
1047 *removed_size = -1;
1048
1049 /* value == NULL means remove */
1050 if (value) {
1051 new_xattr = simple_xattr_alloc(value, size);
1052 if (!new_xattr)
1053 return -ENOMEM;
1054
1055 new_xattr->name = kstrdup(name, GFP_KERNEL);
1056 if (!new_xattr->name) {
1057 kvfree(new_xattr);
1058 return -ENOMEM;
1059 }
1060 }
1061
1062 spin_lock(&xattrs->lock);
1063 list_for_each_entry(xattr, &xattrs->head, list) {
1064 if (!strcmp(name, xattr->name)) {
1065 if (flags & XATTR_CREATE) {
1066 xattr = new_xattr;
1067 err = -EEXIST;
1068 } else if (new_xattr) {
1069 list_replace(&xattr->list, &new_xattr->list);
1070 if (removed_size)
1071 *removed_size = xattr->size;
1072 } else {
1073 list_del(&xattr->list);
1074 if (removed_size)
1075 *removed_size = xattr->size;
1076 }
1077 goto out;
1078 }
1079 }
1080 if (flags & XATTR_REPLACE) {
1081 xattr = new_xattr;
1082 err = -ENODATA;
1083 } else {
1084 list_add(&new_xattr->list, &xattrs->head);
1085 xattr = NULL;
1086 }
1087 out:
1088 spin_unlock(&xattrs->lock);
1089 if (xattr) {
1090 kfree(xattr->name);
1091 kvfree(xattr);
1092 }
1093 return err;
1094
1095 }
1096
xattr_is_trusted(const char * name)1097 static bool xattr_is_trusted(const char *name)
1098 {
1099 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1100 }
1101
xattr_list_one(char ** buffer,ssize_t * remaining_size,const char * name)1102 static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1103 const char *name)
1104 {
1105 size_t len = strlen(name) + 1;
1106 if (*buffer) {
1107 if (*remaining_size < len)
1108 return -ERANGE;
1109 memcpy(*buffer, name, len);
1110 *buffer += len;
1111 }
1112 *remaining_size -= len;
1113 return 0;
1114 }
1115
1116 /*
1117 * xattr LIST operation for in-memory/pseudo filesystems
1118 */
simple_xattr_list(struct inode * inode,struct simple_xattrs * xattrs,char * buffer,size_t size)1119 ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1120 char *buffer, size_t size)
1121 {
1122 bool trusted = ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
1123 struct simple_xattr *xattr;
1124 ssize_t remaining_size = size;
1125 int err = 0;
1126
1127 #ifdef CONFIG_FS_POSIX_ACL
1128 if (IS_POSIXACL(inode)) {
1129 if (inode->i_acl) {
1130 err = xattr_list_one(&buffer, &remaining_size,
1131 XATTR_NAME_POSIX_ACL_ACCESS);
1132 if (err)
1133 return err;
1134 }
1135 if (inode->i_default_acl) {
1136 err = xattr_list_one(&buffer, &remaining_size,
1137 XATTR_NAME_POSIX_ACL_DEFAULT);
1138 if (err)
1139 return err;
1140 }
1141 }
1142 #endif
1143
1144 spin_lock(&xattrs->lock);
1145 list_for_each_entry(xattr, &xattrs->head, list) {
1146 /* skip "trusted." attributes for unprivileged callers */
1147 if (!trusted && xattr_is_trusted(xattr->name))
1148 continue;
1149
1150 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1151 if (err)
1152 break;
1153 }
1154 spin_unlock(&xattrs->lock);
1155
1156 return err ? err : size - remaining_size;
1157 }
1158
1159 /*
1160 * Adds an extended attribute to the list
1161 */
simple_xattr_list_add(struct simple_xattrs * xattrs,struct simple_xattr * new_xattr)1162 void simple_xattr_list_add(struct simple_xattrs *xattrs,
1163 struct simple_xattr *new_xattr)
1164 {
1165 spin_lock(&xattrs->lock);
1166 list_add(&new_xattr->list, &xattrs->head);
1167 spin_unlock(&xattrs->lock);
1168 }
1169