blob: 1f5d0b48422a7e450a2ccca92e020dc94dd8ebcb [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 *
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020056xattr_resolve_name(struct inode *inode, const char **name)
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020057{
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020058 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020059 const struct xattr_handler *handler;
60
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +020061 if (!(inode->i_opflags & IOP_XATTR)) {
62 if (unlikely(is_bad_inode(inode)))
63 return ERR_PTR(-EIO);
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020064 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +020065 }
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020066 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
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080083/*
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
86 */
87static int
88xattr_permission(struct inode *inode, const char *name, int mask)
89{
90 /*
91 * We can never set or remove an extended attribute on a read-only
92 * filesystem or on an immutable / append-only inode.
93 */
94 if (mask & MAY_WRITE) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080095 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
96 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -050097 /*
98 * Updating an xattr will likely cause i_uid and i_gid
99 * to be writen back improperly if their true value is
100 * unknown to the vfs.
101 */
102 if (HAS_UNMAPPED_ID(inode))
103 return -EPERM;
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800104 }
105
106 /*
107 * No restriction for security.* and system.* from the VFS. Decision
108 * on these is left to the underlying filesystem / security module.
109 */
110 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
111 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
112 return 0;
113
114 /*
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200115 * The trusted.* namespace can only be accessed by privileged users.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800116 */
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200117 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
118 if (!capable(CAP_SYS_ADMIN))
119 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
120 return 0;
121 }
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800122
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200123 /*
124 * In the user.* namespace, only regular files and directories can have
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800125 * extended attributes. For sticky directories, only the owner and
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200126 * privileged users can write attributes.
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800127 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800128 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800129 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200130 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800131 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700132 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800133 return -EPERM;
134 }
135
Al Virof419a2e2008-07-22 00:07:17 -0400136 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800137}
138
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400139/**
140 * __vfs_setxattr_noperm - perform setxattr operation without performing
141 * permission checks.
142 *
143 * @dentry - object to perform setxattr on
144 * @name - xattr name to set
145 * @value - value to set @name to
146 * @size - size of @value
147 * @flags - flags to pass into filesystem operations
148 *
149 * returns the result of the internal setxattr or setsecurity operations.
150 *
151 * This function requires the caller to lock the inode's i_mutex before it
152 * is executed. It also assumes that the caller will make the appropriate
153 * permission checks.
154 */
155int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
156 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800157{
158 struct inode *inode = dentry->d_inode;
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400159 int error = -EOPNOTSUPP;
Andi Kleen69b45732011-05-28 08:25:51 -0700160 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
161 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800162
Andi Kleen69b45732011-05-28 08:25:51 -0700163 if (issec)
164 inode->i_flags &= ~S_NOSEC;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800165 if (inode->i_op->setxattr) {
Al Viro3767e252016-05-27 11:06:05 -0400166 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800167 if (!error) {
168 fsnotify_xattr(dentry);
169 security_inode_post_setxattr(dentry, name, value,
170 size, flags);
171 }
Andi Kleen69b45732011-05-28 08:25:51 -0700172 } else if (issec) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800173 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +0200174
175 if (unlikely(is_bad_inode(inode)))
176 return -EIO;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800177 error = security_inode_setsecurity(inode, suffix, value,
178 size, flags);
179 if (!error)
180 fsnotify_xattr(dentry);
181 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400182
183 return error;
184}
185
186
187int
188vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
189 size_t size, int flags)
190{
191 struct inode *inode = dentry->d_inode;
192 int error;
193
194 error = xattr_permission(inode, name, MAY_WRITE);
195 if (error)
196 return error;
197
Al Viro59551022016-01-22 15:40:57 -0500198 inode_lock(inode);
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400199 error = security_inode_setxattr(dentry, name, value, size, flags);
200 if (error)
201 goto out;
202
203 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
204
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800205out:
Al Viro59551022016-01-22 15:40:57 -0500206 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800207 return error;
208}
209EXPORT_SYMBOL_GPL(vfs_setxattr);
210
211ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800212xattr_getsecurity(struct inode *inode, const char *name, void *value,
213 size_t size)
214{
215 void *buffer = NULL;
216 ssize_t len;
217
218 if (!value || !size) {
219 len = security_inode_getsecurity(inode, name, &buffer, false);
220 goto out_noalloc;
221 }
222
223 len = security_inode_getsecurity(inode, name, &buffer, true);
224 if (len < 0)
225 return len;
226 if (size < len) {
227 len = -ERANGE;
228 goto out;
229 }
230 memcpy(value, buffer, len);
231out:
232 security_release_secctx(buffer, len);
233out_noalloc:
234 return len;
235}
236EXPORT_SYMBOL_GPL(xattr_getsecurity);
237
Mimi Zohar1601fba2011-03-09 14:23:34 -0500238/*
239 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
240 *
241 * Allocate memory, if not already allocated, or re-allocate correct size,
242 * before retrieving the extended attribute.
243 *
244 * Returns the result of alloc, if failed, or the getxattr operation.
245 */
246ssize_t
247vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
248 size_t xattr_size, gfp_t flags)
249{
250 struct inode *inode = dentry->d_inode;
251 char *value = *xattr_value;
252 int error;
253
254 error = xattr_permission(inode, name, MAY_READ);
255 if (error)
256 return error;
257
258 if (!inode->i_op->getxattr)
259 return -EOPNOTSUPP;
260
Al Viroce23e642016-04-11 00:48:00 -0400261 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500262 if (error < 0)
263 return error;
264
265 if (!value || (error > xattr_size)) {
266 value = krealloc(*xattr_value, error + 1, flags);
267 if (!value)
268 return -ENOMEM;
269 memset(value, 0, error + 1);
270 }
271
Al Viroce23e642016-04-11 00:48:00 -0400272 error = inode->i_op->getxattr(dentry, inode, name, value, error);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500273 *xattr_value = value;
274 return error;
275}
276
David P. Quigley42492592008-02-04 22:29:39 -0800277ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700278vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800279{
280 struct inode *inode = dentry->d_inode;
281 int error;
282
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800283 error = xattr_permission(inode, name, MAY_READ);
284 if (error)
285 return error;
286
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800287 error = security_inode_getxattr(dentry, name);
288 if (error)
289 return error;
290
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800291 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800292 XATTR_SECURITY_PREFIX_LEN)) {
293 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800294 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800295 /*
296 * Only overwrite the return value if a security module
297 * is actually active.
298 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800299 if (ret == -EOPNOTSUPP)
300 goto nolsm;
301 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800302 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800303nolsm:
304 if (inode->i_op->getxattr)
Al Viroce23e642016-04-11 00:48:00 -0400305 error = inode->i_op->getxattr(dentry, inode, name, value, size);
David P. Quigley4bea5802008-02-04 22:29:40 -0800306 else
307 error = -EOPNOTSUPP;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800308
309 return error;
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200310
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800311}
312EXPORT_SYMBOL_GPL(vfs_getxattr);
313
Bill Nottingham659564c2006-10-09 16:10:48 -0400314ssize_t
315vfs_listxattr(struct dentry *d, char *list, size_t size)
316{
317 ssize_t error;
318
319 error = security_inode_listxattr(d);
320 if (error)
321 return error;
322 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500323 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400324 error = d->d_inode->i_op->listxattr(d, list, size);
325 } else {
326 error = security_inode_listsecurity(d->d_inode, list, size);
327 if (size && error > size)
328 error = -ERANGE;
329 }
330 return error;
331}
332EXPORT_SYMBOL_GPL(vfs_listxattr);
333
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800334int
David Howells8f0cfa52008-04-29 00:59:41 -0700335vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800336{
337 struct inode *inode = dentry->d_inode;
338 int error;
339
340 if (!inode->i_op->removexattr)
341 return -EOPNOTSUPP;
342
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800343 error = xattr_permission(inode, name, MAY_WRITE);
344 if (error)
345 return error;
346
Al Viro59551022016-01-22 15:40:57 -0500347 inode_lock(inode);
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400348 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200349 if (error)
350 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400351
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800352 error = inode->i_op->removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800353
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500354 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800355 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500356 evm_inode_post_removexattr(dentry, name);
357 }
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200358
359out:
Al Viro59551022016-01-22 15:40:57 -0500360 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800361 return error;
362}
363EXPORT_SYMBOL_GPL(vfs_removexattr);
364
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Extended attribute SET operations
368 */
369static long
David Howells8f0cfa52008-04-29 00:59:41 -0700370setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 size_t size, int flags)
372{
373 int error;
374 void *kvalue = NULL;
375 char kname[XATTR_NAME_MAX + 1];
376
377 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
378 return -EINVAL;
379
380 error = strncpy_from_user(kname, name, sizeof(kname));
381 if (error == 0 || error == sizeof(kname))
382 error = -ERANGE;
383 if (error < 0)
384 return error;
385
386 if (size) {
387 if (size > XATTR_SIZE_MAX)
388 return -E2BIG;
Andrew Morton44c82492012-04-05 14:25:07 -0700389 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
390 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100391 kvalue = vmalloc(size);
392 if (!kvalue)
Andrew Morton44c82492012-04-05 14:25:07 -0700393 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700394 }
395 if (copy_from_user(kvalue, value, size)) {
396 error = -EFAULT;
397 goto out;
398 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800399 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
400 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
401 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800404 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700405out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100406 kvfree(kvalue);
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return error;
409}
410
Eric Biggers8cc43112014-10-12 11:59:58 -0500411static int path_setxattr(const char __user *pathname,
412 const char __user *name, const void __user *value,
413 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Al Viro2d8f3032008-07-22 09:59:21 -0400415 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500417retry:
418 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (error)
420 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400421 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800422 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400423 error = setxattr(path.dentry, name, value, size, flags);
424 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800425 }
Al Viro2d8f3032008-07-22 09:59:21 -0400426 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500427 if (retry_estale(error, lookup_flags)) {
428 lookup_flags |= LOOKUP_REVAL;
429 goto retry;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return error;
432}
433
Eric Biggers8cc43112014-10-12 11:59:58 -0500434SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
435 const char __user *, name, const void __user *, value,
436 size_t, size, int, flags)
437{
438 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
439}
440
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100441SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
442 const char __user *, name, const void __user *, value,
443 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Eric Biggers8cc43112014-10-12 11:59:58 -0500445 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100448SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
449 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Al Viro2903ff02012-08-28 12:52:22 -0400451 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 int error = -EBADF;
453
Al Viro2903ff02012-08-28 12:52:22 -0400454 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400456 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400457 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800458 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400459 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400460 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800461 }
Al Viro2903ff02012-08-28 12:52:22 -0400462 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return error;
464}
465
466/*
467 * Extended attribute GET operations
468 */
469static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700470getxattr(struct dentry *d, const char __user *name, void __user *value,
471 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 ssize_t error;
474 void *kvalue = NULL;
475 char kname[XATTR_NAME_MAX + 1];
476
477 error = strncpy_from_user(kname, name, sizeof(kname));
478 if (error == 0 || error == sizeof(kname))
479 error = -ERANGE;
480 if (error < 0)
481 return error;
482
483 if (size) {
484 if (size > XATTR_SIZE_MAX)
485 size = XATTR_SIZE_MAX;
Sasha Levin779302e2012-07-30 14:39:13 -0700486 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
487 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100488 kvalue = vmalloc(size);
489 if (!kvalue)
Sasha Levin779302e2012-07-30 14:39:13 -0700490 return -ENOMEM;
Sasha Levin779302e2012-07-30 14:39:13 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800494 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700495 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800496 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
497 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
498 posix_acl_fix_xattr_to_user(kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700499 if (size && copy_to_user(value, kvalue, error))
500 error = -EFAULT;
501 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
502 /* The file system tried to returned a value bigger
503 than XATTR_SIZE_MAX bytes. Not possible. */
504 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100506
507 kvfree(kvalue);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return error;
510}
511
Eric Biggers8cc43112014-10-12 11:59:58 -0500512static ssize_t path_getxattr(const char __user *pathname,
513 const char __user *name, void __user *value,
514 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Al Viro2d8f3032008-07-22 09:59:21 -0400516 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500518retry:
519 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (error)
521 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400522 error = getxattr(path.dentry, name, value, size);
523 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500524 if (retry_estale(error, lookup_flags)) {
525 lookup_flags |= LOOKUP_REVAL;
526 goto retry;
527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return error;
529}
530
Eric Biggers8cc43112014-10-12 11:59:58 -0500531SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
532 const char __user *, name, void __user *, value, size_t, size)
533{
534 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
535}
536
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100537SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
538 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Eric Biggers8cc43112014-10-12 11:59:58 -0500540 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100543SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
544 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Al Viro2903ff02012-08-28 12:52:22 -0400546 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ssize_t error = -EBADF;
548
Al Viro2903ff02012-08-28 12:52:22 -0400549 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400551 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400552 error = getxattr(f.file->f_path.dentry, name, value, size);
553 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return error;
555}
556
557/*
558 * Extended attribute LIST operations
559 */
560static ssize_t
561listxattr(struct dentry *d, char __user *list, size_t size)
562{
563 ssize_t error;
564 char *klist = NULL;
565
566 if (size) {
567 if (size > XATTR_LIST_MAX)
568 size = XATTR_LIST_MAX;
Dave Jones703bf2d2012-04-05 14:25:06 -0700569 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700570 if (!klist) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100571 klist = vmalloc(size);
572 if (!klist)
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700573 return -ENOMEM;
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Bill Nottingham659564c2006-10-09 16:10:48 -0400577 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700578 if (error > 0) {
579 if (size && copy_to_user(list, klist, error))
580 error = -EFAULT;
581 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
582 /* The file system tried to returned a list bigger
583 than XATTR_LIST_MAX bytes. Not possible. */
584 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100586
587 kvfree(klist);
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return error;
590}
591
Eric Biggers8cc43112014-10-12 11:59:58 -0500592static ssize_t path_listxattr(const char __user *pathname, char __user *list,
593 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Al Viro2d8f3032008-07-22 09:59:21 -0400595 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500597retry:
598 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (error)
600 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400601 error = listxattr(path.dentry, list, size);
602 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500603 if (retry_estale(error, lookup_flags)) {
604 lookup_flags |= LOOKUP_REVAL;
605 goto retry;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return error;
608}
609
Eric Biggers8cc43112014-10-12 11:59:58 -0500610SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
611 size_t, size)
612{
613 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
614}
615
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100616SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
617 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Eric Biggers8cc43112014-10-12 11:59:58 -0500619 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100622SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Al Viro2903ff02012-08-28 12:52:22 -0400624 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 ssize_t error = -EBADF;
626
Al Viro2903ff02012-08-28 12:52:22 -0400627 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400629 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400630 error = listxattr(f.file->f_path.dentry, list, size);
631 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return error;
633}
634
635/*
636 * Extended attribute REMOVE operations
637 */
638static long
David Howells8f0cfa52008-04-29 00:59:41 -0700639removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
641 int error;
642 char kname[XATTR_NAME_MAX + 1];
643
644 error = strncpy_from_user(kname, name, sizeof(kname));
645 if (error == 0 || error == sizeof(kname))
646 error = -ERANGE;
647 if (error < 0)
648 return error;
649
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800650 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
Eric Biggers8cc43112014-10-12 11:59:58 -0500653static int path_removexattr(const char __user *pathname,
654 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Al Viro2d8f3032008-07-22 09:59:21 -0400656 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500658retry:
659 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (error)
661 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400662 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800663 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400664 error = removexattr(path.dentry, name);
665 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800666 }
Al Viro2d8f3032008-07-22 09:59:21 -0400667 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500668 if (retry_estale(error, lookup_flags)) {
669 lookup_flags |= LOOKUP_REVAL;
670 goto retry;
671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return error;
673}
674
Eric Biggers8cc43112014-10-12 11:59:58 -0500675SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
676 const char __user *, name)
677{
678 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
679}
680
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100681SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
682 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Eric Biggers8cc43112014-10-12 11:59:58 -0500684 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100687SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Al Viro2903ff02012-08-28 12:52:22 -0400689 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 int error = -EBADF;
691
Al Viro2903ff02012-08-28 12:52:22 -0400692 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400694 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400695 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800696 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400697 error = removexattr(f.file->f_path.dentry, name);
Al Viro2903ff02012-08-28 12:52:22 -0400698 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800699 }
Al Viro2903ff02012-08-28 12:52:22 -0400700 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return error;
702}
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704/*
705 * Find the handler for the prefix and dispatch its get() operation.
706 */
707ssize_t
Al Viroce23e642016-04-11 00:48:00 -0400708generic_getxattr(struct dentry *dentry, struct inode *inode,
709 const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700711 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200713 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100714 if (IS_ERR(handler))
715 return PTR_ERR(handler);
Al Viroce23e642016-04-11 00:48:00 -0400716 return handler->get(handler, dentry, inode,
Al Virob2968212016-04-10 20:48:24 -0400717 name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/*
721 * Combine the results of the list() operation from every xattr_handler in the
722 * list.
723 */
724ssize_t
725generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
726{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700727 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned int size = 0;
729
730 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000731 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 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 } else {
738 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100739 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100742 if (!handler->name ||
743 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100744 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100745 len = strlen(handler->name);
746 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100748 memcpy(buf, handler->name, len + 1);
749 buf += len + 1;
750 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 size = buf - buffer;
753 }
754 return size;
755}
756
757/*
758 * Find the handler for the prefix and dispatch its set() operation.
759 */
760int
Al Viro3767e252016-05-27 11:06:05 -0400761generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
762 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700764 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (size == 0)
767 value = ""; /* empty EA, do not remove */
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200768 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100769 if (IS_ERR(handler))
770 return PTR_ERR(handler);
Al Viro3767e252016-05-27 11:06:05 -0400771 return handler->set(handler, dentry, inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774/*
775 * Find the handler for the prefix and dispatch its set() operation to remove
776 * any associated extended attribute.
777 */
778int
779generic_removexattr(struct dentry *dentry, const char *name)
780{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700781 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200783 handler = xattr_resolve_name(d_inode(dentry), &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100784 if (IS_ERR(handler))
785 return PTR_ERR(handler);
Al Viro59301222016-05-27 10:19:30 -0400786 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
787 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
790EXPORT_SYMBOL(generic_getxattr);
791EXPORT_SYMBOL(generic_listxattr);
792EXPORT_SYMBOL(generic_setxattr);
793EXPORT_SYMBOL(generic_removexattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400794
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200795/**
796 * xattr_full_name - Compute full attribute name from suffix
797 *
798 * @handler: handler of the xattr_handler operation
799 * @name: name passed to the xattr_handler operation
800 *
801 * The get and set xattr handler operations are called with the remainder of
802 * the attribute name after skipping the handler's prefix: for example, "foo"
803 * is passed to the get operation of a handler with prefix "user." to get
804 * attribute "user.foo". The full name is still "there" in the name though.
805 *
806 * Note: the list xattr handler operation when called from the vfs is passed a
807 * NULL name; some file systems use this operation internally, with varying
808 * semantics.
809 */
810const char *xattr_full_name(const struct xattr_handler *handler,
811 const char *name)
812{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100813 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200814
815 return name - prefix_len;
816}
817EXPORT_SYMBOL(xattr_full_name);
818
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400819/*
820 * Allocate new xattr and copy in the value; but leave the name to callers.
821 */
822struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
823{
824 struct simple_xattr *new_xattr;
825 size_t len;
826
827 /* wrap around? */
828 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700829 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400830 return NULL;
831
832 new_xattr = kmalloc(len, GFP_KERNEL);
833 if (!new_xattr)
834 return NULL;
835
836 new_xattr->size = size;
837 memcpy(new_xattr->value, value, size);
838 return new_xattr;
839}
840
841/*
842 * xattr GET operation for in-memory/pseudo filesystems
843 */
844int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
845 void *buffer, size_t size)
846{
847 struct simple_xattr *xattr;
848 int ret = -ENODATA;
849
850 spin_lock(&xattrs->lock);
851 list_for_each_entry(xattr, &xattrs->head, list) {
852 if (strcmp(name, xattr->name))
853 continue;
854
855 ret = xattr->size;
856 if (buffer) {
857 if (size < xattr->size)
858 ret = -ERANGE;
859 else
860 memcpy(buffer, xattr->value, xattr->size);
861 }
862 break;
863 }
864 spin_unlock(&xattrs->lock);
865 return ret;
866}
867
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100868/**
869 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
870 * @xattrs: target simple_xattr list
871 * @name: name of the extended attribute
872 * @value: value of the xattr. If %NULL, will remove the attribute.
873 * @size: size of the new xattr
874 * @flags: %XATTR_{CREATE|REPLACE}
875 *
876 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
877 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
878 * otherwise, fails with -ENODATA.
879 *
880 * Returns 0 on success, -errno on failure.
881 */
882int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
883 const void *value, size_t size, int flags)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400884{
885 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700886 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400887 int err = 0;
888
889 /* value == NULL means remove */
890 if (value) {
891 new_xattr = simple_xattr_alloc(value, size);
892 if (!new_xattr)
893 return -ENOMEM;
894
895 new_xattr->name = kstrdup(name, GFP_KERNEL);
896 if (!new_xattr->name) {
897 kfree(new_xattr);
898 return -ENOMEM;
899 }
900 }
901
902 spin_lock(&xattrs->lock);
903 list_for_each_entry(xattr, &xattrs->head, list) {
904 if (!strcmp(name, xattr->name)) {
905 if (flags & XATTR_CREATE) {
906 xattr = new_xattr;
907 err = -EEXIST;
908 } else if (new_xattr) {
909 list_replace(&xattr->list, &new_xattr->list);
910 } else {
911 list_del(&xattr->list);
912 }
913 goto out;
914 }
915 }
916 if (flags & XATTR_REPLACE) {
917 xattr = new_xattr;
918 err = -ENODATA;
919 } else {
920 list_add(&new_xattr->list, &xattrs->head);
921 xattr = NULL;
922 }
923out:
924 spin_unlock(&xattrs->lock);
925 if (xattr) {
926 kfree(xattr->name);
927 kfree(xattr);
928 }
929 return err;
930
931}
932
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400933static bool xattr_is_trusted(const char *name)
934{
935 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
936}
937
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100938static int xattr_list_one(char **buffer, ssize_t *remaining_size,
939 const char *name)
940{
941 size_t len = strlen(name) + 1;
942 if (*buffer) {
943 if (*remaining_size < len)
944 return -ERANGE;
945 memcpy(*buffer, name, len);
946 *buffer += len;
947 }
948 *remaining_size -= len;
949 return 0;
950}
951
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400952/*
953 * xattr LIST operation for in-memory/pseudo filesystems
954 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100955ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
956 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400957{
958 bool trusted = capable(CAP_SYS_ADMIN);
959 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100960 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100961 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100962
963#ifdef CONFIG_FS_POSIX_ACL
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#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400977
978 spin_lock(&xattrs->lock);
979 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400980 /* skip "trusted." attributes for unprivileged callers */
981 if (!trusted && xattr_is_trusted(xattr->name))
982 continue;
983
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100984 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
985 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100986 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400987 }
988 spin_unlock(&xattrs->lock);
989
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100990 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400991}
992
Aristeu Rozanski48957682012-09-11 16:28:11 -0400993/*
994 * Adds an extended attribute to the list
995 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400996void simple_xattr_list_add(struct simple_xattrs *xattrs,
997 struct simple_xattr *new_xattr)
998{
999 spin_lock(&xattrs->lock);
1000 list_add(&new_xattr->list, &xattrs->head);
1001 spin_unlock(&xattrs->lock);
1002}