blob: 38b52a356d1b3aec0d3ee9639a5a363e9b92b1bb [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 Gruenbacherd0a5b992016-09-29 17:48:39 +020061 if (!(inode->i_opflags & IOP_XATTR))
62 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020063 for_each_xattr_handler(handlers, handler) {
64 const char *n;
65
66 n = strcmp_prefix(*name, xattr_prefix(handler));
67 if (n) {
68 if (!handler->prefix ^ !*n) {
69 if (*n)
70 continue;
71 return ERR_PTR(-EINVAL);
72 }
73 *name = n;
74 return handler;
75 }
76 }
77 return ERR_PTR(-EOPNOTSUPP);
78}
79
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080080/*
81 * Check permissions for extended attribute access. This is a bit complicated
82 * because different namespaces have very different rules.
83 */
84static int
85xattr_permission(struct inode *inode, const char *name, int mask)
86{
87 /*
88 * We can never set or remove an extended attribute on a read-only
89 * filesystem or on an immutable / append-only inode.
90 */
91 if (mask & MAY_WRITE) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080092 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
93 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -050094 /*
95 * Updating an xattr will likely cause i_uid and i_gid
96 * to be writen back improperly if their true value is
97 * unknown to the vfs.
98 */
99 if (HAS_UNMAPPED_ID(inode))
100 return -EPERM;
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800101 }
102
103 /*
104 * No restriction for security.* and system.* from the VFS. Decision
105 * on these is left to the underlying filesystem / security module.
106 */
107 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
108 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
109 return 0;
110
111 /*
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200112 * The trusted.* namespace can only be accessed by privileged users.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800113 */
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200114 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
115 if (!capable(CAP_SYS_ADMIN))
116 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
117 return 0;
118 }
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800119
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200120 /*
121 * In the user.* namespace, only regular files and directories can have
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800122 * extended attributes. For sticky directories, only the owner and
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200123 * privileged users can write attributes.
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800124 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800125 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800126 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200127 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800128 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700129 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800130 return -EPERM;
131 }
132
Al Virof419a2e2008-07-22 00:07:17 -0400133 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800134}
135
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400136/**
137 * __vfs_setxattr_noperm - perform setxattr operation without performing
138 * permission checks.
139 *
140 * @dentry - object to perform setxattr on
141 * @name - xattr name to set
142 * @value - value to set @name to
143 * @size - size of @value
144 * @flags - flags to pass into filesystem operations
145 *
146 * returns the result of the internal setxattr or setsecurity operations.
147 *
148 * This function requires the caller to lock the inode's i_mutex before it
149 * is executed. It also assumes that the caller will make the appropriate
150 * permission checks.
151 */
152int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
153 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800154{
155 struct inode *inode = dentry->d_inode;
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400156 int error = -EOPNOTSUPP;
Andi Kleen69b45732011-05-28 08:25:51 -0700157 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
158 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800159
Andi Kleen69b45732011-05-28 08:25:51 -0700160 if (issec)
161 inode->i_flags &= ~S_NOSEC;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800162 if (inode->i_op->setxattr) {
Al Viro3767e252016-05-27 11:06:05 -0400163 error = inode->i_op->setxattr(dentry, inode, name, value, size, flags);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800164 if (!error) {
165 fsnotify_xattr(dentry);
166 security_inode_post_setxattr(dentry, name, value,
167 size, flags);
168 }
Andi Kleen69b45732011-05-28 08:25:51 -0700169 } else if (issec) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800170 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800171 error = security_inode_setsecurity(inode, suffix, value,
172 size, flags);
173 if (!error)
174 fsnotify_xattr(dentry);
175 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400176
177 return error;
178}
179
180
181int
182vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
183 size_t size, int flags)
184{
185 struct inode *inode = dentry->d_inode;
186 int error;
187
188 error = xattr_permission(inode, name, MAY_WRITE);
189 if (error)
190 return error;
191
Al Viro59551022016-01-22 15:40:57 -0500192 inode_lock(inode);
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400193 error = security_inode_setxattr(dentry, name, value, size, flags);
194 if (error)
195 goto out;
196
197 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
198
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800199out:
Al Viro59551022016-01-22 15:40:57 -0500200 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800201 return error;
202}
203EXPORT_SYMBOL_GPL(vfs_setxattr);
204
205ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800206xattr_getsecurity(struct inode *inode, const char *name, void *value,
207 size_t size)
208{
209 void *buffer = NULL;
210 ssize_t len;
211
212 if (!value || !size) {
213 len = security_inode_getsecurity(inode, name, &buffer, false);
214 goto out_noalloc;
215 }
216
217 len = security_inode_getsecurity(inode, name, &buffer, true);
218 if (len < 0)
219 return len;
220 if (size < len) {
221 len = -ERANGE;
222 goto out;
223 }
224 memcpy(value, buffer, len);
225out:
226 security_release_secctx(buffer, len);
227out_noalloc:
228 return len;
229}
230EXPORT_SYMBOL_GPL(xattr_getsecurity);
231
Mimi Zohar1601fba2011-03-09 14:23:34 -0500232/*
233 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
234 *
235 * Allocate memory, if not already allocated, or re-allocate correct size,
236 * before retrieving the extended attribute.
237 *
238 * Returns the result of alloc, if failed, or the getxattr operation.
239 */
240ssize_t
241vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
242 size_t xattr_size, gfp_t flags)
243{
244 struct inode *inode = dentry->d_inode;
245 char *value = *xattr_value;
246 int error;
247
248 error = xattr_permission(inode, name, MAY_READ);
249 if (error)
250 return error;
251
252 if (!inode->i_op->getxattr)
253 return -EOPNOTSUPP;
254
Al Viroce23e642016-04-11 00:48:00 -0400255 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500256 if (error < 0)
257 return error;
258
259 if (!value || (error > xattr_size)) {
260 value = krealloc(*xattr_value, error + 1, flags);
261 if (!value)
262 return -ENOMEM;
263 memset(value, 0, error + 1);
264 }
265
Al Viroce23e642016-04-11 00:48:00 -0400266 error = inode->i_op->getxattr(dentry, inode, name, value, error);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500267 *xattr_value = value;
268 return error;
269}
270
David P. Quigley42492592008-02-04 22:29:39 -0800271ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700272vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800273{
274 struct inode *inode = dentry->d_inode;
275 int error;
276
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800277 error = xattr_permission(inode, name, MAY_READ);
278 if (error)
279 return error;
280
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800281 error = security_inode_getxattr(dentry, name);
282 if (error)
283 return error;
284
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800285 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800286 XATTR_SECURITY_PREFIX_LEN)) {
287 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800288 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800289 /*
290 * Only overwrite the return value if a security module
291 * is actually active.
292 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800293 if (ret == -EOPNOTSUPP)
294 goto nolsm;
295 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800296 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800297nolsm:
298 if (inode->i_op->getxattr)
Al Viroce23e642016-04-11 00:48:00 -0400299 error = inode->i_op->getxattr(dentry, inode, name, value, size);
David P. Quigley4bea5802008-02-04 22:29:40 -0800300 else
301 error = -EOPNOTSUPP;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800302
303 return error;
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200304
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800305}
306EXPORT_SYMBOL_GPL(vfs_getxattr);
307
Bill Nottingham659564c2006-10-09 16:10:48 -0400308ssize_t
309vfs_listxattr(struct dentry *d, char *list, size_t size)
310{
311 ssize_t error;
312
313 error = security_inode_listxattr(d);
314 if (error)
315 return error;
316 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500317 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400318 error = d->d_inode->i_op->listxattr(d, list, size);
319 } else {
320 error = security_inode_listsecurity(d->d_inode, list, size);
321 if (size && error > size)
322 error = -ERANGE;
323 }
324 return error;
325}
326EXPORT_SYMBOL_GPL(vfs_listxattr);
327
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800328int
David Howells8f0cfa52008-04-29 00:59:41 -0700329vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800330{
331 struct inode *inode = dentry->d_inode;
332 int error;
333
334 if (!inode->i_op->removexattr)
335 return -EOPNOTSUPP;
336
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800337 error = xattr_permission(inode, name, MAY_WRITE);
338 if (error)
339 return error;
340
Al Viro59551022016-01-22 15:40:57 -0500341 inode_lock(inode);
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400342 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200343 if (error)
344 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400345
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800346 error = inode->i_op->removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800347
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500348 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800349 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500350 evm_inode_post_removexattr(dentry, name);
351 }
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200352
353out:
Al Viro59551022016-01-22 15:40:57 -0500354 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800355 return error;
356}
357EXPORT_SYMBOL_GPL(vfs_removexattr);
358
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Extended attribute SET operations
362 */
363static long
David Howells8f0cfa52008-04-29 00:59:41 -0700364setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 size_t size, int flags)
366{
367 int error;
368 void *kvalue = NULL;
369 char kname[XATTR_NAME_MAX + 1];
370
371 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
372 return -EINVAL;
373
374 error = strncpy_from_user(kname, name, sizeof(kname));
375 if (error == 0 || error == sizeof(kname))
376 error = -ERANGE;
377 if (error < 0)
378 return error;
379
380 if (size) {
381 if (size > XATTR_SIZE_MAX)
382 return -E2BIG;
Andrew Morton44c82492012-04-05 14:25:07 -0700383 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
384 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100385 kvalue = vmalloc(size);
386 if (!kvalue)
Andrew Morton44c82492012-04-05 14:25:07 -0700387 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700388 }
389 if (copy_from_user(kvalue, value, size)) {
390 error = -EFAULT;
391 goto out;
392 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800393 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
394 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
395 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800398 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700399out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100400 kvfree(kvalue);
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return error;
403}
404
Eric Biggers8cc43112014-10-12 11:59:58 -0500405static int path_setxattr(const char __user *pathname,
406 const char __user *name, const void __user *value,
407 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Al Viro2d8f3032008-07-22 09:59:21 -0400409 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500411retry:
412 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (error)
414 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400415 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800416 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400417 error = setxattr(path.dentry, name, value, size, flags);
418 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800419 }
Al Viro2d8f3032008-07-22 09:59:21 -0400420 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500421 if (retry_estale(error, lookup_flags)) {
422 lookup_flags |= LOOKUP_REVAL;
423 goto retry;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return error;
426}
427
Eric Biggers8cc43112014-10-12 11:59:58 -0500428SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
429 const char __user *, name, const void __user *, value,
430 size_t, size, int, flags)
431{
432 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
433}
434
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100435SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
436 const char __user *, name, const void __user *, value,
437 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Eric Biggers8cc43112014-10-12 11:59:58 -0500439 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100442SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
443 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Al Viro2903ff02012-08-28 12:52:22 -0400445 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 int error = -EBADF;
447
Al Viro2903ff02012-08-28 12:52:22 -0400448 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400450 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400451 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800452 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400453 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400454 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800455 }
Al Viro2903ff02012-08-28 12:52:22 -0400456 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return error;
458}
459
460/*
461 * Extended attribute GET operations
462 */
463static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700464getxattr(struct dentry *d, const char __user *name, void __user *value,
465 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 ssize_t error;
468 void *kvalue = NULL;
469 char kname[XATTR_NAME_MAX + 1];
470
471 error = strncpy_from_user(kname, name, sizeof(kname));
472 if (error == 0 || error == sizeof(kname))
473 error = -ERANGE;
474 if (error < 0)
475 return error;
476
477 if (size) {
478 if (size > XATTR_SIZE_MAX)
479 size = XATTR_SIZE_MAX;
Sasha Levin779302e2012-07-30 14:39:13 -0700480 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
481 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100482 kvalue = vmalloc(size);
483 if (!kvalue)
Sasha Levin779302e2012-07-30 14:39:13 -0700484 return -ENOMEM;
Sasha Levin779302e2012-07-30 14:39:13 -0700485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800488 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700489 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800490 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
491 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
492 posix_acl_fix_xattr_to_user(kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700493 if (size && copy_to_user(value, kvalue, error))
494 error = -EFAULT;
495 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
496 /* The file system tried to returned a value bigger
497 than XATTR_SIZE_MAX bytes. Not possible. */
498 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100500
501 kvfree(kvalue);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return error;
504}
505
Eric Biggers8cc43112014-10-12 11:59:58 -0500506static ssize_t path_getxattr(const char __user *pathname,
507 const char __user *name, void __user *value,
508 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Al Viro2d8f3032008-07-22 09:59:21 -0400510 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500512retry:
513 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (error)
515 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400516 error = getxattr(path.dentry, name, value, size);
517 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500518 if (retry_estale(error, lookup_flags)) {
519 lookup_flags |= LOOKUP_REVAL;
520 goto retry;
521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return error;
523}
524
Eric Biggers8cc43112014-10-12 11:59:58 -0500525SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
526 const char __user *, name, void __user *, value, size_t, size)
527{
528 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
529}
530
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100531SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
532 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Eric Biggers8cc43112014-10-12 11:59:58 -0500534 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100537SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
538 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Al Viro2903ff02012-08-28 12:52:22 -0400540 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 ssize_t error = -EBADF;
542
Al Viro2903ff02012-08-28 12:52:22 -0400543 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400545 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400546 error = getxattr(f.file->f_path.dentry, name, value, size);
547 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return error;
549}
550
551/*
552 * Extended attribute LIST operations
553 */
554static ssize_t
555listxattr(struct dentry *d, char __user *list, size_t size)
556{
557 ssize_t error;
558 char *klist = NULL;
559
560 if (size) {
561 if (size > XATTR_LIST_MAX)
562 size = XATTR_LIST_MAX;
Dave Jones703bf2d2012-04-05 14:25:06 -0700563 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700564 if (!klist) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100565 klist = vmalloc(size);
566 if (!klist)
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700567 return -ENOMEM;
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Bill Nottingham659564c2006-10-09 16:10:48 -0400571 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700572 if (error > 0) {
573 if (size && copy_to_user(list, klist, error))
574 error = -EFAULT;
575 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
576 /* The file system tried to returned a list bigger
577 than XATTR_LIST_MAX bytes. Not possible. */
578 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100580
581 kvfree(klist);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return error;
584}
585
Eric Biggers8cc43112014-10-12 11:59:58 -0500586static ssize_t path_listxattr(const char __user *pathname, char __user *list,
587 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Al Viro2d8f3032008-07-22 09:59:21 -0400589 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500591retry:
592 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (error)
594 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400595 error = listxattr(path.dentry, list, size);
596 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500597 if (retry_estale(error, lookup_flags)) {
598 lookup_flags |= LOOKUP_REVAL;
599 goto retry;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return error;
602}
603
Eric Biggers8cc43112014-10-12 11:59:58 -0500604SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
605 size_t, size)
606{
607 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
608}
609
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100610SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
611 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Eric Biggers8cc43112014-10-12 11:59:58 -0500613 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100616SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Al Viro2903ff02012-08-28 12:52:22 -0400618 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ssize_t error = -EBADF;
620
Al Viro2903ff02012-08-28 12:52:22 -0400621 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400623 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400624 error = listxattr(f.file->f_path.dentry, list, size);
625 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return error;
627}
628
629/*
630 * Extended attribute REMOVE operations
631 */
632static long
David Howells8f0cfa52008-04-29 00:59:41 -0700633removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 int error;
636 char kname[XATTR_NAME_MAX + 1];
637
638 error = strncpy_from_user(kname, name, sizeof(kname));
639 if (error == 0 || error == sizeof(kname))
640 error = -ERANGE;
641 if (error < 0)
642 return error;
643
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800644 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Eric Biggers8cc43112014-10-12 11:59:58 -0500647static int path_removexattr(const char __user *pathname,
648 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Al Viro2d8f3032008-07-22 09:59:21 -0400650 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500652retry:
653 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (error)
655 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400656 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800657 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400658 error = removexattr(path.dentry, name);
659 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800660 }
Al Viro2d8f3032008-07-22 09:59:21 -0400661 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500662 if (retry_estale(error, lookup_flags)) {
663 lookup_flags |= LOOKUP_REVAL;
664 goto retry;
665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return error;
667}
668
Eric Biggers8cc43112014-10-12 11:59:58 -0500669SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
670 const char __user *, name)
671{
672 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
673}
674
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100675SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
676 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Eric Biggers8cc43112014-10-12 11:59:58 -0500678 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100681SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Al Viro2903ff02012-08-28 12:52:22 -0400683 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 int error = -EBADF;
685
Al Viro2903ff02012-08-28 12:52:22 -0400686 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400688 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400689 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800690 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400691 error = removexattr(f.file->f_path.dentry, name);
Al Viro2903ff02012-08-28 12:52:22 -0400692 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800693 }
Al Viro2903ff02012-08-28 12:52:22 -0400694 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return error;
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698/*
699 * Find the handler for the prefix and dispatch its get() operation.
700 */
701ssize_t
Al Viroce23e642016-04-11 00:48:00 -0400702generic_getxattr(struct dentry *dentry, struct inode *inode,
703 const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700705 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200707 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100708 if (IS_ERR(handler))
709 return PTR_ERR(handler);
Al Viroce23e642016-04-11 00:48:00 -0400710 return handler->get(handler, dentry, inode,
Al Virob2968212016-04-10 20:48:24 -0400711 name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
714/*
715 * Combine the results of the list() operation from every xattr_handler in the
716 * list.
717 */
718ssize_t
719generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
720{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700721 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 unsigned int size = 0;
723
724 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000725 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100726 if (!handler->name ||
727 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100728 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100729 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
732 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100733 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100736 if (!handler->name ||
737 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100738 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100739 len = strlen(handler->name);
740 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100742 memcpy(buf, handler->name, len + 1);
743 buf += len + 1;
744 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 size = buf - buffer;
747 }
748 return size;
749}
750
751/*
752 * Find the handler for the prefix and dispatch its set() operation.
753 */
754int
Al Viro3767e252016-05-27 11:06:05 -0400755generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
756 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700758 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 if (size == 0)
761 value = ""; /* empty EA, do not remove */
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200762 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100763 if (IS_ERR(handler))
764 return PTR_ERR(handler);
Al Viro3767e252016-05-27 11:06:05 -0400765 return handler->set(handler, dentry, inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/*
769 * Find the handler for the prefix and dispatch its set() operation to remove
770 * any associated extended attribute.
771 */
772int
773generic_removexattr(struct dentry *dentry, const char *name)
774{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700775 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200777 handler = xattr_resolve_name(d_inode(dentry), &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100778 if (IS_ERR(handler))
779 return PTR_ERR(handler);
Al Viro59301222016-05-27 10:19:30 -0400780 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
781 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784EXPORT_SYMBOL(generic_getxattr);
785EXPORT_SYMBOL(generic_listxattr);
786EXPORT_SYMBOL(generic_setxattr);
787EXPORT_SYMBOL(generic_removexattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400788
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200789/**
790 * xattr_full_name - Compute full attribute name from suffix
791 *
792 * @handler: handler of the xattr_handler operation
793 * @name: name passed to the xattr_handler operation
794 *
795 * The get and set xattr handler operations are called with the remainder of
796 * the attribute name after skipping the handler's prefix: for example, "foo"
797 * is passed to the get operation of a handler with prefix "user." to get
798 * attribute "user.foo". The full name is still "there" in the name though.
799 *
800 * Note: the list xattr handler operation when called from the vfs is passed a
801 * NULL name; some file systems use this operation internally, with varying
802 * semantics.
803 */
804const char *xattr_full_name(const struct xattr_handler *handler,
805 const char *name)
806{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100807 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200808
809 return name - prefix_len;
810}
811EXPORT_SYMBOL(xattr_full_name);
812
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400813/*
814 * Allocate new xattr and copy in the value; but leave the name to callers.
815 */
816struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
817{
818 struct simple_xattr *new_xattr;
819 size_t len;
820
821 /* wrap around? */
822 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700823 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400824 return NULL;
825
826 new_xattr = kmalloc(len, GFP_KERNEL);
827 if (!new_xattr)
828 return NULL;
829
830 new_xattr->size = size;
831 memcpy(new_xattr->value, value, size);
832 return new_xattr;
833}
834
835/*
836 * xattr GET operation for in-memory/pseudo filesystems
837 */
838int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
839 void *buffer, size_t size)
840{
841 struct simple_xattr *xattr;
842 int ret = -ENODATA;
843
844 spin_lock(&xattrs->lock);
845 list_for_each_entry(xattr, &xattrs->head, list) {
846 if (strcmp(name, xattr->name))
847 continue;
848
849 ret = xattr->size;
850 if (buffer) {
851 if (size < xattr->size)
852 ret = -ERANGE;
853 else
854 memcpy(buffer, xattr->value, xattr->size);
855 }
856 break;
857 }
858 spin_unlock(&xattrs->lock);
859 return ret;
860}
861
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100862/**
863 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
864 * @xattrs: target simple_xattr list
865 * @name: name of the extended attribute
866 * @value: value of the xattr. If %NULL, will remove the attribute.
867 * @size: size of the new xattr
868 * @flags: %XATTR_{CREATE|REPLACE}
869 *
870 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
871 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
872 * otherwise, fails with -ENODATA.
873 *
874 * Returns 0 on success, -errno on failure.
875 */
876int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
877 const void *value, size_t size, int flags)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400878{
879 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700880 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400881 int err = 0;
882
883 /* value == NULL means remove */
884 if (value) {
885 new_xattr = simple_xattr_alloc(value, size);
886 if (!new_xattr)
887 return -ENOMEM;
888
889 new_xattr->name = kstrdup(name, GFP_KERNEL);
890 if (!new_xattr->name) {
891 kfree(new_xattr);
892 return -ENOMEM;
893 }
894 }
895
896 spin_lock(&xattrs->lock);
897 list_for_each_entry(xattr, &xattrs->head, list) {
898 if (!strcmp(name, xattr->name)) {
899 if (flags & XATTR_CREATE) {
900 xattr = new_xattr;
901 err = -EEXIST;
902 } else if (new_xattr) {
903 list_replace(&xattr->list, &new_xattr->list);
904 } else {
905 list_del(&xattr->list);
906 }
907 goto out;
908 }
909 }
910 if (flags & XATTR_REPLACE) {
911 xattr = new_xattr;
912 err = -ENODATA;
913 } else {
914 list_add(&new_xattr->list, &xattrs->head);
915 xattr = NULL;
916 }
917out:
918 spin_unlock(&xattrs->lock);
919 if (xattr) {
920 kfree(xattr->name);
921 kfree(xattr);
922 }
923 return err;
924
925}
926
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400927static bool xattr_is_trusted(const char *name)
928{
929 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
930}
931
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100932static int xattr_list_one(char **buffer, ssize_t *remaining_size,
933 const char *name)
934{
935 size_t len = strlen(name) + 1;
936 if (*buffer) {
937 if (*remaining_size < len)
938 return -ERANGE;
939 memcpy(*buffer, name, len);
940 *buffer += len;
941 }
942 *remaining_size -= len;
943 return 0;
944}
945
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400946/*
947 * xattr LIST operation for in-memory/pseudo filesystems
948 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100949ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
950 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400951{
952 bool trusted = capable(CAP_SYS_ADMIN);
953 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100954 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100955 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100956
957#ifdef CONFIG_FS_POSIX_ACL
958 if (inode->i_acl) {
959 err = xattr_list_one(&buffer, &remaining_size,
960 XATTR_NAME_POSIX_ACL_ACCESS);
961 if (err)
962 return err;
963 }
964 if (inode->i_default_acl) {
965 err = xattr_list_one(&buffer, &remaining_size,
966 XATTR_NAME_POSIX_ACL_DEFAULT);
967 if (err)
968 return err;
969 }
970#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400971
972 spin_lock(&xattrs->lock);
973 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400974 /* skip "trusted." attributes for unprivileged callers */
975 if (!trusted && xattr_is_trusted(xattr->name))
976 continue;
977
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100978 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
979 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100980 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400981 }
982 spin_unlock(&xattrs->lock);
983
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100984 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400985}
986
Aristeu Rozanski48957682012-09-11 16:28:11 -0400987/*
988 * Adds an extended attribute to the list
989 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400990void simple_xattr_list_add(struct simple_xattrs *xattrs,
991 struct simple_xattr *new_xattr)
992{
993 spin_lock(&xattrs->lock);
994 list_add(&new_xattr->list, &xattrs->head);
995 spin_unlock(&xattrs->lock);
996}