blob: bb3cbbfd982df9c6ae530fec1622e628f80ddbfa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/file.h>
13#include <linux/xattr.h>
Dave Hansen18f335a2008-02-15 14:37:38 -080014#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/namei.h>
16#include <linux/security.h>
Mimi Zoharc7b87de2011-03-09 14:39:18 -050017#include <linux/evm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050019#include <linux/export.h>
Robert Love0eeca282005-07-12 17:06:03 -040020#include <linux/fsnotify.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000021#include <linux/audit.h>
Andrew Morton0d08d7b2012-04-05 14:25:07 -070022#include <linux/vmalloc.h>
Eric W. Biederman2f6f0652012-02-07 18:52:57 -080023#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Andrew Morton0d08d7b2012-04-05 14:25:07 -070025#include <asm/uaccess.h>
Christoph Hellwig5be196e2006-01-09 20:51:55 -080026
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020027static const char *
28strcmp_prefix(const char *a, const char *a_prefix)
29{
30 while (*a_prefix && *a == *a_prefix) {
31 a++;
32 a_prefix++;
33 }
34 return *a_prefix ? NULL : a;
35}
36
37/*
38 * In order to implement different sets of xattr operations for each xattr
39 * prefix with the generic xattr API, a filesystem should create a
40 * null-terminated array of struct xattr_handler (one for each prefix) and
41 * hang a pointer to it off of the s_xattr field of the superblock.
42 *
43 * The generic_fooxattr() functions will use this list to dispatch xattr
44 * operations to the correct xattr_handler.
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 */
55static const struct xattr_handler *
56xattr_resolve_name(const struct xattr_handler **handlers, const char **name)
57{
58 const struct xattr_handler *handler;
59
60 for_each_xattr_handler(handlers, handler) {
61 const char *n;
62
63 n = strcmp_prefix(*name, xattr_prefix(handler));
64 if (n) {
65 if (!handler->prefix ^ !*n) {
66 if (*n)
67 continue;
68 return ERR_PTR(-EINVAL);
69 }
70 *name = n;
71 return handler;
72 }
73 }
74 return ERR_PTR(-EOPNOTSUPP);
75}
76
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080077/*
78 * Check permissions for extended attribute access. This is a bit complicated
79 * because different namespaces have very different rules.
80 */
81static int
82xattr_permission(struct inode *inode, const char *name, int mask)
83{
84 /*
85 * We can never set or remove an extended attribute on a read-only
86 * filesystem or on an immutable / append-only inode.
87 */
88 if (mask & MAY_WRITE) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080089 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
90 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -050091 /*
92 * Updating an xattr will likely cause i_uid and i_gid
93 * to be writen back improperly if their true value is
94 * unknown to the vfs.
95 */
96 if (HAS_UNMAPPED_ID(inode))
97 return -EPERM;
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080098 }
99
100 /*
101 * No restriction for security.* and system.* from the VFS. Decision
102 * on these is left to the underlying filesystem / security module.
103 */
104 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
105 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
106 return 0;
107
108 /*
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200109 * The trusted.* namespace can only be accessed by privileged users.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800110 */
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200111 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
112 if (!capable(CAP_SYS_ADMIN))
113 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
114 return 0;
115 }
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800116
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200117 /*
118 * In the user.* namespace, only regular files and directories can have
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800119 * extended attributes. For sticky directories, only the owner and
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200120 * privileged users can write attributes.
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800121 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800122 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800123 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200124 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800125 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700126 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800127 return -EPERM;
128 }
129
Al Virof419a2e2008-07-22 00:07:17 -0400130 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800131}
132
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400133/**
134 * __vfs_setxattr_noperm - perform setxattr operation without performing
135 * permission checks.
136 *
137 * @dentry - object to perform setxattr on
138 * @name - xattr name to set
139 * @value - value to set @name to
140 * @size - size of @value
141 * @flags - flags to pass into filesystem operations
142 *
143 * returns the result of the internal setxattr or setsecurity operations.
144 *
145 * This function requires the caller to lock the inode's i_mutex before it
146 * is executed. It also assumes that the caller will make the appropriate
147 * permission checks.
148 */
149int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
150 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800151{
152 struct inode *inode = dentry->d_inode;
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400153 int error = -EOPNOTSUPP;
Andi Kleen69b45732011-05-28 08:25:51 -0700154 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
155 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800156
Andi Kleen69b45732011-05-28 08:25:51 -0700157 if (issec)
158 inode->i_flags &= ~S_NOSEC;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800159 if (inode->i_op->setxattr) {
Al Viro3767e252016-05-27 11:06:05 -0400160 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800161 if (!error) {
162 fsnotify_xattr(dentry);
163 security_inode_post_setxattr(dentry, name, value,
164 size, flags);
165 }
Andi Kleen69b45732011-05-28 08:25:51 -0700166 } else if (issec) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800167 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800168 error = security_inode_setsecurity(inode, suffix, value,
169 size, flags);
170 if (!error)
171 fsnotify_xattr(dentry);
172 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400173
174 return error;
175}
176
177
178int
179vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
180 size_t size, int flags)
181{
182 struct inode *inode = dentry->d_inode;
183 int error;
184
185 error = xattr_permission(inode, name, MAY_WRITE);
186 if (error)
187 return error;
188
Al Viro59551022016-01-22 15:40:57 -0500189 inode_lock(inode);
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400190 error = security_inode_setxattr(dentry, name, value, size, flags);
191 if (error)
192 goto out;
193
194 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
195
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800196out:
Al Viro59551022016-01-22 15:40:57 -0500197 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800198 return error;
199}
200EXPORT_SYMBOL_GPL(vfs_setxattr);
201
202ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800203xattr_getsecurity(struct inode *inode, const char *name, void *value,
204 size_t size)
205{
206 void *buffer = NULL;
207 ssize_t len;
208
209 if (!value || !size) {
210 len = security_inode_getsecurity(inode, name, &buffer, false);
211 goto out_noalloc;
212 }
213
214 len = security_inode_getsecurity(inode, name, &buffer, true);
215 if (len < 0)
216 return len;
217 if (size < len) {
218 len = -ERANGE;
219 goto out;
220 }
221 memcpy(value, buffer, len);
222out:
223 security_release_secctx(buffer, len);
224out_noalloc:
225 return len;
226}
227EXPORT_SYMBOL_GPL(xattr_getsecurity);
228
Mimi Zohar1601fba2011-03-09 14:23:34 -0500229/*
230 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
231 *
232 * Allocate memory, if not already allocated, or re-allocate correct size,
233 * before retrieving the extended attribute.
234 *
235 * Returns the result of alloc, if failed, or the getxattr operation.
236 */
237ssize_t
238vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
239 size_t xattr_size, gfp_t flags)
240{
241 struct inode *inode = dentry->d_inode;
242 char *value = *xattr_value;
243 int error;
244
245 error = xattr_permission(inode, name, MAY_READ);
246 if (error)
247 return error;
248
249 if (!inode->i_op->getxattr)
250 return -EOPNOTSUPP;
251
Al Viroce23e642016-04-11 00:48:00 -0400252 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500253 if (error < 0)
254 return error;
255
256 if (!value || (error > xattr_size)) {
257 value = krealloc(*xattr_value, error + 1, flags);
258 if (!value)
259 return -ENOMEM;
260 memset(value, 0, error + 1);
261 }
262
Al Viroce23e642016-04-11 00:48:00 -0400263 error = inode->i_op->getxattr(dentry, inode, name, value, error);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500264 *xattr_value = value;
265 return error;
266}
267
David P. Quigley42492592008-02-04 22:29:39 -0800268ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700269vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800270{
271 struct inode *inode = dentry->d_inode;
272 int error;
273
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800274 error = xattr_permission(inode, name, MAY_READ);
275 if (error)
276 return error;
277
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800278 error = security_inode_getxattr(dentry, name);
279 if (error)
280 return error;
281
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800282 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800283 XATTR_SECURITY_PREFIX_LEN)) {
284 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800285 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800286 /*
287 * Only overwrite the return value if a security module
288 * is actually active.
289 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800290 if (ret == -EOPNOTSUPP)
291 goto nolsm;
292 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800293 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800294nolsm:
295 if (inode->i_op->getxattr)
Al Viroce23e642016-04-11 00:48:00 -0400296 error = inode->i_op->getxattr(dentry, inode, name, value, size);
David P. Quigley4bea5802008-02-04 22:29:40 -0800297 else
298 error = -EOPNOTSUPP;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800299
300 return error;
301}
302EXPORT_SYMBOL_GPL(vfs_getxattr);
303
Bill Nottingham659564c2006-10-09 16:10:48 -0400304ssize_t
305vfs_listxattr(struct dentry *d, char *list, size_t size)
306{
307 ssize_t error;
308
309 error = security_inode_listxattr(d);
310 if (error)
311 return error;
312 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500313 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400314 error = d->d_inode->i_op->listxattr(d, list, size);
315 } else {
316 error = security_inode_listsecurity(d->d_inode, list, size);
317 if (size && error > size)
318 error = -ERANGE;
319 }
320 return error;
321}
322EXPORT_SYMBOL_GPL(vfs_listxattr);
323
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800324int
David Howells8f0cfa52008-04-29 00:59:41 -0700325vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800326{
327 struct inode *inode = dentry->d_inode;
328 int error;
329
330 if (!inode->i_op->removexattr)
331 return -EOPNOTSUPP;
332
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800333 error = xattr_permission(inode, name, MAY_WRITE);
334 if (error)
335 return error;
336
Al Viro59551022016-01-22 15:40:57 -0500337 inode_lock(inode);
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400338 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200339 if (error)
340 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400341
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800342 error = inode->i_op->removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800343
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500344 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800345 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500346 evm_inode_post_removexattr(dentry, name);
347 }
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200348
349out:
Al Viro59551022016-01-22 15:40:57 -0500350 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800351 return error;
352}
353EXPORT_SYMBOL_GPL(vfs_removexattr);
354
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
357 * Extended attribute SET operations
358 */
359static long
David Howells8f0cfa52008-04-29 00:59:41 -0700360setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 size_t size, int flags)
362{
363 int error;
364 void *kvalue = NULL;
365 char kname[XATTR_NAME_MAX + 1];
366
367 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
368 return -EINVAL;
369
370 error = strncpy_from_user(kname, name, sizeof(kname));
371 if (error == 0 || error == sizeof(kname))
372 error = -ERANGE;
373 if (error < 0)
374 return error;
375
376 if (size) {
377 if (size > XATTR_SIZE_MAX)
378 return -E2BIG;
Andrew Morton44c82492012-04-05 14:25:07 -0700379 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
380 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100381 kvalue = vmalloc(size);
382 if (!kvalue)
Andrew Morton44c82492012-04-05 14:25:07 -0700383 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700384 }
385 if (copy_from_user(kvalue, value, size)) {
386 error = -EFAULT;
387 goto out;
388 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800389 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
390 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
391 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800394 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700395out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100396 kvfree(kvalue);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return error;
399}
400
Eric Biggers8cc43112014-10-12 11:59:58 -0500401static int path_setxattr(const char __user *pathname,
402 const char __user *name, const void __user *value,
403 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Al Viro2d8f3032008-07-22 09:59:21 -0400405 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500407retry:
408 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (error)
410 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400411 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800412 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400413 error = setxattr(path.dentry, name, value, size, flags);
414 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800415 }
Al Viro2d8f3032008-07-22 09:59:21 -0400416 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500417 if (retry_estale(error, lookup_flags)) {
418 lookup_flags |= LOOKUP_REVAL;
419 goto retry;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return error;
422}
423
Eric Biggers8cc43112014-10-12 11:59:58 -0500424SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
425 const char __user *, name, const void __user *, value,
426 size_t, size, int, flags)
427{
428 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
429}
430
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100431SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
432 const char __user *, name, const void __user *, value,
433 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Eric Biggers8cc43112014-10-12 11:59:58 -0500435 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100438SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
439 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Al Viro2903ff02012-08-28 12:52:22 -0400441 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int error = -EBADF;
443
Al Viro2903ff02012-08-28 12:52:22 -0400444 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400446 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400447 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800448 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400449 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400450 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800451 }
Al Viro2903ff02012-08-28 12:52:22 -0400452 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return error;
454}
455
456/*
457 * Extended attribute GET operations
458 */
459static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700460getxattr(struct dentry *d, const char __user *name, void __user *value,
461 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 ssize_t error;
464 void *kvalue = NULL;
465 char kname[XATTR_NAME_MAX + 1];
466
467 error = strncpy_from_user(kname, name, sizeof(kname));
468 if (error == 0 || error == sizeof(kname))
469 error = -ERANGE;
470 if (error < 0)
471 return error;
472
473 if (size) {
474 if (size > XATTR_SIZE_MAX)
475 size = XATTR_SIZE_MAX;
Sasha Levin779302e2012-07-30 14:39:13 -0700476 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
477 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100478 kvalue = vmalloc(size);
479 if (!kvalue)
Sasha Levin779302e2012-07-30 14:39:13 -0700480 return -ENOMEM;
Sasha Levin779302e2012-07-30 14:39:13 -0700481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800484 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700485 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800486 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
487 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
488 posix_acl_fix_xattr_to_user(kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700489 if (size && copy_to_user(value, kvalue, error))
490 error = -EFAULT;
491 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
492 /* The file system tried to returned a value bigger
493 than XATTR_SIZE_MAX bytes. Not possible. */
494 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100496
497 kvfree(kvalue);
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return error;
500}
501
Eric Biggers8cc43112014-10-12 11:59:58 -0500502static ssize_t path_getxattr(const char __user *pathname,
503 const char __user *name, void __user *value,
504 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Al Viro2d8f3032008-07-22 09:59:21 -0400506 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500508retry:
509 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (error)
511 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400512 error = getxattr(path.dentry, name, value, size);
513 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500514 if (retry_estale(error, lookup_flags)) {
515 lookup_flags |= LOOKUP_REVAL;
516 goto retry;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return error;
519}
520
Eric Biggers8cc43112014-10-12 11:59:58 -0500521SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
522 const char __user *, name, void __user *, value, size_t, size)
523{
524 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
525}
526
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100527SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
528 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Eric Biggers8cc43112014-10-12 11:59:58 -0500530 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100533SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
534 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Al Viro2903ff02012-08-28 12:52:22 -0400536 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ssize_t error = -EBADF;
538
Al Viro2903ff02012-08-28 12:52:22 -0400539 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400541 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400542 error = getxattr(f.file->f_path.dentry, name, value, size);
543 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return error;
545}
546
547/*
548 * Extended attribute LIST operations
549 */
550static ssize_t
551listxattr(struct dentry *d, char __user *list, size_t size)
552{
553 ssize_t error;
554 char *klist = NULL;
555
556 if (size) {
557 if (size > XATTR_LIST_MAX)
558 size = XATTR_LIST_MAX;
Dave Jones703bf2d2012-04-05 14:25:06 -0700559 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700560 if (!klist) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100561 klist = vmalloc(size);
562 if (!klist)
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700563 return -ENOMEM;
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Bill Nottingham659564c2006-10-09 16:10:48 -0400567 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700568 if (error > 0) {
569 if (size && copy_to_user(list, klist, error))
570 error = -EFAULT;
571 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
572 /* The file system tried to returned a list bigger
573 than XATTR_LIST_MAX bytes. Not possible. */
574 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100576
577 kvfree(klist);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return error;
580}
581
Eric Biggers8cc43112014-10-12 11:59:58 -0500582static ssize_t path_listxattr(const char __user *pathname, char __user *list,
583 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Al Viro2d8f3032008-07-22 09:59:21 -0400585 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500587retry:
588 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (error)
590 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400591 error = listxattr(path.dentry, list, size);
592 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500593 if (retry_estale(error, lookup_flags)) {
594 lookup_flags |= LOOKUP_REVAL;
595 goto retry;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return error;
598}
599
Eric Biggers8cc43112014-10-12 11:59:58 -0500600SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
601 size_t, size)
602{
603 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
604}
605
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100606SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
607 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Eric Biggers8cc43112014-10-12 11:59:58 -0500609 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100612SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Al Viro2903ff02012-08-28 12:52:22 -0400614 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 ssize_t error = -EBADF;
616
Al Viro2903ff02012-08-28 12:52:22 -0400617 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400619 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400620 error = listxattr(f.file->f_path.dentry, list, size);
621 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return error;
623}
624
625/*
626 * Extended attribute REMOVE operations
627 */
628static long
David Howells8f0cfa52008-04-29 00:59:41 -0700629removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 int error;
632 char kname[XATTR_NAME_MAX + 1];
633
634 error = strncpy_from_user(kname, name, sizeof(kname));
635 if (error == 0 || error == sizeof(kname))
636 error = -ERANGE;
637 if (error < 0)
638 return error;
639
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800640 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Eric Biggers8cc43112014-10-12 11:59:58 -0500643static int path_removexattr(const char __user *pathname,
644 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Al Viro2d8f3032008-07-22 09:59:21 -0400646 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500648retry:
649 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (error)
651 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400652 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800653 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400654 error = removexattr(path.dentry, name);
655 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800656 }
Al Viro2d8f3032008-07-22 09:59:21 -0400657 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500658 if (retry_estale(error, lookup_flags)) {
659 lookup_flags |= LOOKUP_REVAL;
660 goto retry;
661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return error;
663}
664
Eric Biggers8cc43112014-10-12 11:59:58 -0500665SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
666 const char __user *, name)
667{
668 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
669}
670
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100671SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
672 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Eric Biggers8cc43112014-10-12 11:59:58 -0500674 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100677SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Viro2903ff02012-08-28 12:52:22 -0400679 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int error = -EBADF;
681
Al Viro2903ff02012-08-28 12:52:22 -0400682 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400684 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400685 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800686 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400687 error = removexattr(f.file->f_path.dentry, name);
Al Viro2903ff02012-08-28 12:52:22 -0400688 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800689 }
Al Viro2903ff02012-08-28 12:52:22 -0400690 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 return error;
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
695 * Find the handler for the prefix and dispatch its get() operation.
696 */
697ssize_t
Al Viroce23e642016-04-11 00:48:00 -0400698generic_getxattr(struct dentry *dentry, struct inode *inode,
699 const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700701 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Christoph Hellwig431547b2009-11-13 09:52:56 +0000703 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100704 if (IS_ERR(handler))
705 return PTR_ERR(handler);
Al Viroce23e642016-04-11 00:48:00 -0400706 return handler->get(handler, dentry, inode,
Al Virob2968212016-04-10 20:48:24 -0400707 name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/*
711 * Combine the results of the list() operation from every xattr_handler in the
712 * list.
713 */
714ssize_t
715generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
716{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700717 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned int size = 0;
719
720 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000721 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100722 if (!handler->name ||
723 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100724 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100725 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 } else {
728 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100729 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100732 if (!handler->name ||
733 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100734 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100735 len = strlen(handler->name);
736 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100738 memcpy(buf, handler->name, len + 1);
739 buf += len + 1;
740 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 size = buf - buffer;
743 }
744 return size;
745}
746
747/*
748 * Find the handler for the prefix and dispatch its set() operation.
749 */
750int
Al Viro3767e252016-05-27 11:06:05 -0400751generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
752 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700754 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 if (size == 0)
757 value = ""; /* empty EA, do not remove */
Christoph Hellwig431547b2009-11-13 09:52:56 +0000758 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100759 if (IS_ERR(handler))
760 return PTR_ERR(handler);
Al Viro3767e252016-05-27 11:06:05 -0400761 return handler->set(handler, dentry, inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/*
765 * Find the handler for the prefix and dispatch its set() operation to remove
766 * any associated extended attribute.
767 */
768int
769generic_removexattr(struct dentry *dentry, const char *name)
770{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700771 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Christoph Hellwig431547b2009-11-13 09:52:56 +0000773 handler = xattr_resolve_name(dentry->d_sb->s_xattr, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100774 if (IS_ERR(handler))
775 return PTR_ERR(handler);
Al Viro59301222016-05-27 10:19:30 -0400776 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
777 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
780EXPORT_SYMBOL(generic_getxattr);
781EXPORT_SYMBOL(generic_listxattr);
782EXPORT_SYMBOL(generic_setxattr);
783EXPORT_SYMBOL(generic_removexattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400784
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200785/**
786 * xattr_full_name - Compute full attribute name from suffix
787 *
788 * @handler: handler of the xattr_handler operation
789 * @name: name passed to the xattr_handler operation
790 *
791 * The get and set xattr handler operations are called with the remainder of
792 * the attribute name after skipping the handler's prefix: for example, "foo"
793 * is passed to the get operation of a handler with prefix "user." to get
794 * attribute "user.foo". The full name is still "there" in the name though.
795 *
796 * Note: the list xattr handler operation when called from the vfs is passed a
797 * NULL name; some file systems use this operation internally, with varying
798 * semantics.
799 */
800const char *xattr_full_name(const struct xattr_handler *handler,
801 const char *name)
802{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100803 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200804
805 return name - prefix_len;
806}
807EXPORT_SYMBOL(xattr_full_name);
808
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400809/*
810 * Allocate new xattr and copy in the value; but leave the name to callers.
811 */
812struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
813{
814 struct simple_xattr *new_xattr;
815 size_t len;
816
817 /* wrap around? */
818 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700819 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400820 return NULL;
821
822 new_xattr = kmalloc(len, GFP_KERNEL);
823 if (!new_xattr)
824 return NULL;
825
826 new_xattr->size = size;
827 memcpy(new_xattr->value, value, size);
828 return new_xattr;
829}
830
831/*
832 * xattr GET operation for in-memory/pseudo filesystems
833 */
834int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
835 void *buffer, size_t size)
836{
837 struct simple_xattr *xattr;
838 int ret = -ENODATA;
839
840 spin_lock(&xattrs->lock);
841 list_for_each_entry(xattr, &xattrs->head, list) {
842 if (strcmp(name, xattr->name))
843 continue;
844
845 ret = xattr->size;
846 if (buffer) {
847 if (size < xattr->size)
848 ret = -ERANGE;
849 else
850 memcpy(buffer, xattr->value, xattr->size);
851 }
852 break;
853 }
854 spin_unlock(&xattrs->lock);
855 return ret;
856}
857
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100858/**
859 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
860 * @xattrs: target simple_xattr list
861 * @name: name of the extended attribute
862 * @value: value of the xattr. If %NULL, will remove the attribute.
863 * @size: size of the new xattr
864 * @flags: %XATTR_{CREATE|REPLACE}
865 *
866 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
867 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
868 * otherwise, fails with -ENODATA.
869 *
870 * Returns 0 on success, -errno on failure.
871 */
872int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
873 const void *value, size_t size, int flags)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400874{
875 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700876 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400877 int err = 0;
878
879 /* value == NULL means remove */
880 if (value) {
881 new_xattr = simple_xattr_alloc(value, size);
882 if (!new_xattr)
883 return -ENOMEM;
884
885 new_xattr->name = kstrdup(name, GFP_KERNEL);
886 if (!new_xattr->name) {
887 kfree(new_xattr);
888 return -ENOMEM;
889 }
890 }
891
892 spin_lock(&xattrs->lock);
893 list_for_each_entry(xattr, &xattrs->head, list) {
894 if (!strcmp(name, xattr->name)) {
895 if (flags & XATTR_CREATE) {
896 xattr = new_xattr;
897 err = -EEXIST;
898 } else if (new_xattr) {
899 list_replace(&xattr->list, &new_xattr->list);
900 } else {
901 list_del(&xattr->list);
902 }
903 goto out;
904 }
905 }
906 if (flags & XATTR_REPLACE) {
907 xattr = new_xattr;
908 err = -ENODATA;
909 } else {
910 list_add(&new_xattr->list, &xattrs->head);
911 xattr = NULL;
912 }
913out:
914 spin_unlock(&xattrs->lock);
915 if (xattr) {
916 kfree(xattr->name);
917 kfree(xattr);
918 }
919 return err;
920
921}
922
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400923static bool xattr_is_trusted(const char *name)
924{
925 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
926}
927
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100928static int xattr_list_one(char **buffer, ssize_t *remaining_size,
929 const char *name)
930{
931 size_t len = strlen(name) + 1;
932 if (*buffer) {
933 if (*remaining_size < len)
934 return -ERANGE;
935 memcpy(*buffer, name, len);
936 *buffer += len;
937 }
938 *remaining_size -= len;
939 return 0;
940}
941
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400942/*
943 * xattr LIST operation for in-memory/pseudo filesystems
944 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100945ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
946 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400947{
948 bool trusted = capable(CAP_SYS_ADMIN);
949 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100950 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100951 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100952
953#ifdef CONFIG_FS_POSIX_ACL
954 if (inode->i_acl) {
955 err = xattr_list_one(&buffer, &remaining_size,
956 XATTR_NAME_POSIX_ACL_ACCESS);
957 if (err)
958 return err;
959 }
960 if (inode->i_default_acl) {
961 err = xattr_list_one(&buffer, &remaining_size,
962 XATTR_NAME_POSIX_ACL_DEFAULT);
963 if (err)
964 return err;
965 }
966#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400967
968 spin_lock(&xattrs->lock);
969 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400970 /* skip "trusted." attributes for unprivileged callers */
971 if (!trusted && xattr_is_trusted(xattr->name))
972 continue;
973
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100974 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
975 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100976 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400977 }
978 spin_unlock(&xattrs->lock);
979
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100980 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400981}
982
Aristeu Rozanski48957682012-09-11 16:28:11 -0400983/*
984 * Adds an extended attribute to the list
985 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400986void simple_xattr_list_add(struct simple_xattrs *xattrs,
987 struct simple_xattr *new_xattr)
988{
989 spin_lock(&xattrs->lock);
990 list_add(&new_xattr->list, &xattrs->head);
991 spin_unlock(&xattrs->lock);
992}