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