blob: 54a41151912713d84dd3a5d970303216b9d04ec6 [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
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200139int
140__vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
141 const void *value, size_t size, int flags)
142{
143 if (!inode->i_op->setxattr)
144 return -EOPNOTSUPP;
145 return inode->i_op->setxattr(dentry, inode, name, value, size, flags);
146}
147EXPORT_SYMBOL(__vfs_setxattr);
148
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400149/**
150 * __vfs_setxattr_noperm - perform setxattr operation without performing
151 * permission checks.
152 *
153 * @dentry - object to perform setxattr on
154 * @name - xattr name to set
155 * @value - value to set @name to
156 * @size - size of @value
157 * @flags - flags to pass into filesystem operations
158 *
159 * returns the result of the internal setxattr or setsecurity operations.
160 *
161 * This function requires the caller to lock the inode's i_mutex before it
162 * is executed. It also assumes that the caller will make the appropriate
163 * permission checks.
164 */
165int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
166 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800167{
168 struct inode *inode = dentry->d_inode;
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400169 int error = -EOPNOTSUPP;
Andi Kleen69b45732011-05-28 08:25:51 -0700170 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
171 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800172
Andi Kleen69b45732011-05-28 08:25:51 -0700173 if (issec)
174 inode->i_flags &= ~S_NOSEC;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800175 if (inode->i_op->setxattr) {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200176 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800177 if (!error) {
178 fsnotify_xattr(dentry);
179 security_inode_post_setxattr(dentry, name, value,
180 size, flags);
181 }
Andi Kleen69b45732011-05-28 08:25:51 -0700182 } else if (issec) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800183 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +0200184
185 if (unlikely(is_bad_inode(inode)))
186 return -EIO;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800187 error = security_inode_setsecurity(inode, suffix, value,
188 size, flags);
189 if (!error)
190 fsnotify_xattr(dentry);
191 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400192
193 return error;
194}
195
196
197int
198vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
199 size_t size, int flags)
200{
201 struct inode *inode = dentry->d_inode;
202 int error;
203
204 error = xattr_permission(inode, name, MAY_WRITE);
205 if (error)
206 return error;
207
Al Viro59551022016-01-22 15:40:57 -0500208 inode_lock(inode);
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400209 error = security_inode_setxattr(dentry, name, value, size, flags);
210 if (error)
211 goto out;
212
213 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
214
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800215out:
Al Viro59551022016-01-22 15:40:57 -0500216 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800217 return error;
218}
219EXPORT_SYMBOL_GPL(vfs_setxattr);
220
221ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800222xattr_getsecurity(struct inode *inode, const char *name, void *value,
223 size_t size)
224{
225 void *buffer = NULL;
226 ssize_t len;
227
228 if (!value || !size) {
229 len = security_inode_getsecurity(inode, name, &buffer, false);
230 goto out_noalloc;
231 }
232
233 len = security_inode_getsecurity(inode, name, &buffer, true);
234 if (len < 0)
235 return len;
236 if (size < len) {
237 len = -ERANGE;
238 goto out;
239 }
240 memcpy(value, buffer, len);
241out:
242 security_release_secctx(buffer, len);
243out_noalloc:
244 return len;
245}
246EXPORT_SYMBOL_GPL(xattr_getsecurity);
247
Mimi Zohar1601fba2011-03-09 14:23:34 -0500248/*
249 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
250 *
251 * Allocate memory, if not already allocated, or re-allocate correct size,
252 * before retrieving the extended attribute.
253 *
254 * Returns the result of alloc, if failed, or the getxattr operation.
255 */
256ssize_t
257vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
258 size_t xattr_size, gfp_t flags)
259{
260 struct inode *inode = dentry->d_inode;
261 char *value = *xattr_value;
262 int error;
263
264 error = xattr_permission(inode, name, MAY_READ);
265 if (error)
266 return error;
267
268 if (!inode->i_op->getxattr)
269 return -EOPNOTSUPP;
270
Al Viroce23e642016-04-11 00:48:00 -0400271 error = inode->i_op->getxattr(dentry, inode, name, NULL, 0);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500272 if (error < 0)
273 return error;
274
275 if (!value || (error > xattr_size)) {
276 value = krealloc(*xattr_value, error + 1, flags);
277 if (!value)
278 return -ENOMEM;
279 memset(value, 0, error + 1);
280 }
281
Al Viroce23e642016-04-11 00:48:00 -0400282 error = inode->i_op->getxattr(dentry, inode, name, value, error);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500283 *xattr_value = value;
284 return error;
285}
286
David P. Quigley42492592008-02-04 22:29:39 -0800287ssize_t
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200288__vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
289 void *value, size_t size)
290{
291 if (!inode->i_op->getxattr)
292 return -EOPNOTSUPP;
293 return inode->i_op->getxattr(dentry, inode, name, value, size);
294}
295EXPORT_SYMBOL(__vfs_getxattr);
296
297ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700298vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800299{
300 struct inode *inode = dentry->d_inode;
301 int error;
302
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800303 error = xattr_permission(inode, name, MAY_READ);
304 if (error)
305 return error;
306
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800307 error = security_inode_getxattr(dentry, name);
308 if (error)
309 return error;
310
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800311 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800312 XATTR_SECURITY_PREFIX_LEN)) {
313 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800314 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800315 /*
316 * Only overwrite the return value if a security module
317 * is actually active.
318 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800319 if (ret == -EOPNOTSUPP)
320 goto nolsm;
321 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800322 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800323nolsm:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200324 return __vfs_getxattr(dentry, inode, name, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800325}
326EXPORT_SYMBOL_GPL(vfs_getxattr);
327
Bill Nottingham659564c2006-10-09 16:10:48 -0400328ssize_t
329vfs_listxattr(struct dentry *d, char *list, size_t size)
330{
331 ssize_t error;
332
333 error = security_inode_listxattr(d);
334 if (error)
335 return error;
336 error = -EOPNOTSUPP;
Al Viroacfa4382008-12-04 10:06:33 -0500337 if (d->d_inode->i_op->listxattr) {
Bill Nottingham659564c2006-10-09 16:10:48 -0400338 error = d->d_inode->i_op->listxattr(d, list, size);
339 } else {
340 error = security_inode_listsecurity(d->d_inode, list, size);
341 if (size && error > size)
342 error = -ERANGE;
343 }
344 return error;
345}
346EXPORT_SYMBOL_GPL(vfs_listxattr);
347
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800348int
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200349__vfs_removexattr(struct dentry *dentry, const char *name)
350{
351 struct inode *inode = dentry->d_inode;
352
353 if (!inode->i_op->removexattr)
354 return -EOPNOTSUPP;
355 return inode->i_op->removexattr(dentry, name);
356}
357EXPORT_SYMBOL(__vfs_removexattr);
358
359int
David Howells8f0cfa52008-04-29 00:59:41 -0700360vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800361{
362 struct inode *inode = dentry->d_inode;
363 int error;
364
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800365 error = xattr_permission(inode, name, MAY_WRITE);
366 if (error)
367 return error;
368
Al Viro59551022016-01-22 15:40:57 -0500369 inode_lock(inode);
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400370 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200371 if (error)
372 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400373
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200374 error = __vfs_removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800375
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500376 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800377 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500378 evm_inode_post_removexattr(dentry, name);
379 }
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200380
381out:
Al Viro59551022016-01-22 15:40:57 -0500382 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800383 return error;
384}
385EXPORT_SYMBOL_GPL(vfs_removexattr);
386
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/*
389 * Extended attribute SET operations
390 */
391static long
David Howells8f0cfa52008-04-29 00:59:41 -0700392setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 size_t size, int flags)
394{
395 int error;
396 void *kvalue = NULL;
397 char kname[XATTR_NAME_MAX + 1];
398
399 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
400 return -EINVAL;
401
402 error = strncpy_from_user(kname, name, sizeof(kname));
403 if (error == 0 || error == sizeof(kname))
404 error = -ERANGE;
405 if (error < 0)
406 return error;
407
408 if (size) {
409 if (size > XATTR_SIZE_MAX)
410 return -E2BIG;
Andrew Morton44c82492012-04-05 14:25:07 -0700411 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
412 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100413 kvalue = vmalloc(size);
414 if (!kvalue)
Andrew Morton44c82492012-04-05 14:25:07 -0700415 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700416 }
417 if (copy_from_user(kvalue, value, size)) {
418 error = -EFAULT;
419 goto out;
420 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800421 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
422 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
423 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800426 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700427out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100428 kvfree(kvalue);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return error;
431}
432
Eric Biggers8cc43112014-10-12 11:59:58 -0500433static int path_setxattr(const char __user *pathname,
434 const char __user *name, const void __user *value,
435 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Al Viro2d8f3032008-07-22 09:59:21 -0400437 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500439retry:
440 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (error)
442 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400443 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800444 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400445 error = setxattr(path.dentry, name, value, size, flags);
446 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800447 }
Al Viro2d8f3032008-07-22 09:59:21 -0400448 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500449 if (retry_estale(error, lookup_flags)) {
450 lookup_flags |= LOOKUP_REVAL;
451 goto retry;
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return error;
454}
455
Eric Biggers8cc43112014-10-12 11:59:58 -0500456SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
457 const char __user *, name, const void __user *, value,
458 size_t, size, int, flags)
459{
460 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
461}
462
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100463SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
464 const char __user *, name, const void __user *, value,
465 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Eric Biggers8cc43112014-10-12 11:59:58 -0500467 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100470SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
471 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Al Viro2903ff02012-08-28 12:52:22 -0400473 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int error = -EBADF;
475
Al Viro2903ff02012-08-28 12:52:22 -0400476 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400478 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400479 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800480 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400481 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400482 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800483 }
Al Viro2903ff02012-08-28 12:52:22 -0400484 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return error;
486}
487
488/*
489 * Extended attribute GET operations
490 */
491static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700492getxattr(struct dentry *d, const char __user *name, void __user *value,
493 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 ssize_t error;
496 void *kvalue = NULL;
497 char kname[XATTR_NAME_MAX + 1];
498
499 error = strncpy_from_user(kname, name, sizeof(kname));
500 if (error == 0 || error == sizeof(kname))
501 error = -ERANGE;
502 if (error < 0)
503 return error;
504
505 if (size) {
506 if (size > XATTR_SIZE_MAX)
507 size = XATTR_SIZE_MAX;
Sasha Levin779302e2012-07-30 14:39:13 -0700508 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
509 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100510 kvalue = vmalloc(size);
511 if (!kvalue)
Sasha Levin779302e2012-07-30 14:39:13 -0700512 return -ENOMEM;
Sasha Levin779302e2012-07-30 14:39:13 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800516 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700517 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800518 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
519 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
520 posix_acl_fix_xattr_to_user(kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700521 if (size && copy_to_user(value, kvalue, error))
522 error = -EFAULT;
523 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
524 /* The file system tried to returned a value bigger
525 than XATTR_SIZE_MAX bytes. Not possible. */
526 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100528
529 kvfree(kvalue);
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return error;
532}
533
Eric Biggers8cc43112014-10-12 11:59:58 -0500534static ssize_t path_getxattr(const char __user *pathname,
535 const char __user *name, void __user *value,
536 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Al Viro2d8f3032008-07-22 09:59:21 -0400538 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500540retry:
541 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (error)
543 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400544 error = getxattr(path.dentry, name, value, size);
545 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500546 if (retry_estale(error, lookup_flags)) {
547 lookup_flags |= LOOKUP_REVAL;
548 goto retry;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return error;
551}
552
Eric Biggers8cc43112014-10-12 11:59:58 -0500553SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
554 const char __user *, name, void __user *, value, size_t, size)
555{
556 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
557}
558
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100559SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
560 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
Eric Biggers8cc43112014-10-12 11:59:58 -0500562 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100565SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
566 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Al Viro2903ff02012-08-28 12:52:22 -0400568 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ssize_t error = -EBADF;
570
Al Viro2903ff02012-08-28 12:52:22 -0400571 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400573 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400574 error = getxattr(f.file->f_path.dentry, name, value, size);
575 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return error;
577}
578
579/*
580 * Extended attribute LIST operations
581 */
582static ssize_t
583listxattr(struct dentry *d, char __user *list, size_t size)
584{
585 ssize_t error;
586 char *klist = NULL;
587
588 if (size) {
589 if (size > XATTR_LIST_MAX)
590 size = XATTR_LIST_MAX;
Dave Jones703bf2d2012-04-05 14:25:06 -0700591 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700592 if (!klist) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100593 klist = vmalloc(size);
594 if (!klist)
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700595 return -ENOMEM;
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Bill Nottingham659564c2006-10-09 16:10:48 -0400599 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700600 if (error > 0) {
601 if (size && copy_to_user(list, klist, error))
602 error = -EFAULT;
603 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
604 /* The file system tried to returned a list bigger
605 than XATTR_LIST_MAX bytes. Not possible. */
606 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100608
609 kvfree(klist);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return error;
612}
613
Eric Biggers8cc43112014-10-12 11:59:58 -0500614static ssize_t path_listxattr(const char __user *pathname, char __user *list,
615 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Al Viro2d8f3032008-07-22 09:59:21 -0400617 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500619retry:
620 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (error)
622 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400623 error = listxattr(path.dentry, list, size);
624 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500625 if (retry_estale(error, lookup_flags)) {
626 lookup_flags |= LOOKUP_REVAL;
627 goto retry;
628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return error;
630}
631
Eric Biggers8cc43112014-10-12 11:59:58 -0500632SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
633 size_t, size)
634{
635 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
636}
637
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100638SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
639 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Eric Biggers8cc43112014-10-12 11:59:58 -0500641 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100644SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Al Viro2903ff02012-08-28 12:52:22 -0400646 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ssize_t error = -EBADF;
648
Al Viro2903ff02012-08-28 12:52:22 -0400649 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400651 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400652 error = listxattr(f.file->f_path.dentry, list, size);
653 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return error;
655}
656
657/*
658 * Extended attribute REMOVE operations
659 */
660static long
David Howells8f0cfa52008-04-29 00:59:41 -0700661removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
663 int error;
664 char kname[XATTR_NAME_MAX + 1];
665
666 error = strncpy_from_user(kname, name, sizeof(kname));
667 if (error == 0 || error == sizeof(kname))
668 error = -ERANGE;
669 if (error < 0)
670 return error;
671
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800672 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
Eric Biggers8cc43112014-10-12 11:59:58 -0500675static int path_removexattr(const char __user *pathname,
676 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Al Viro2d8f3032008-07-22 09:59:21 -0400678 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500680retry:
681 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (error)
683 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400684 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800685 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400686 error = removexattr(path.dentry, name);
687 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800688 }
Al Viro2d8f3032008-07-22 09:59:21 -0400689 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500690 if (retry_estale(error, lookup_flags)) {
691 lookup_flags |= LOOKUP_REVAL;
692 goto retry;
693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return error;
695}
696
Eric Biggers8cc43112014-10-12 11:59:58 -0500697SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
698 const char __user *, name)
699{
700 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
701}
702
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100703SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
704 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Eric Biggers8cc43112014-10-12 11:59:58 -0500706 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100709SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Al Viro2903ff02012-08-28 12:52:22 -0400711 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 int error = -EBADF;
713
Al Viro2903ff02012-08-28 12:52:22 -0400714 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400716 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400717 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800718 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400719 error = removexattr(f.file->f_path.dentry, name);
Al Viro2903ff02012-08-28 12:52:22 -0400720 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800721 }
Al Viro2903ff02012-08-28 12:52:22 -0400722 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return error;
724}
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726/*
727 * Find the handler for the prefix and dispatch its get() operation.
728 */
729ssize_t
Al Viroce23e642016-04-11 00:48:00 -0400730generic_getxattr(struct dentry *dentry, struct inode *inode,
731 const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700733 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200735 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100736 if (IS_ERR(handler))
737 return PTR_ERR(handler);
Al Viroce23e642016-04-11 00:48:00 -0400738 return handler->get(handler, dentry, inode,
Al Virob2968212016-04-10 20:48:24 -0400739 name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742/*
743 * Combine the results of the list() operation from every xattr_handler in the
744 * list.
745 */
746ssize_t
747generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
748{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700749 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 unsigned int size = 0;
751
752 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000753 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100754 if (!handler->name ||
755 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100756 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100757 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else {
760 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100761 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100764 if (!handler->name ||
765 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100766 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100767 len = strlen(handler->name);
768 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100770 memcpy(buf, handler->name, len + 1);
771 buf += len + 1;
772 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774 size = buf - buffer;
775 }
776 return size;
777}
778
779/*
780 * Find the handler for the prefix and dispatch its set() operation.
781 */
782int
Al Viro3767e252016-05-27 11:06:05 -0400783generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
784 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700786 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (size == 0)
789 value = ""; /* empty EA, do not remove */
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200790 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100791 if (IS_ERR(handler))
792 return PTR_ERR(handler);
Al Viro3767e252016-05-27 11:06:05 -0400793 return handler->set(handler, dentry, inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/*
797 * Find the handler for the prefix and dispatch its set() operation to remove
798 * any associated extended attribute.
799 */
800int
801generic_removexattr(struct dentry *dentry, const char *name)
802{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700803 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200805 handler = xattr_resolve_name(d_inode(dentry), &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100806 if (IS_ERR(handler))
807 return PTR_ERR(handler);
Al Viro59301222016-05-27 10:19:30 -0400808 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
809 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812EXPORT_SYMBOL(generic_getxattr);
813EXPORT_SYMBOL(generic_listxattr);
814EXPORT_SYMBOL(generic_setxattr);
815EXPORT_SYMBOL(generic_removexattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400816
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200817/**
818 * xattr_full_name - Compute full attribute name from suffix
819 *
820 * @handler: handler of the xattr_handler operation
821 * @name: name passed to the xattr_handler operation
822 *
823 * The get and set xattr handler operations are called with the remainder of
824 * the attribute name after skipping the handler's prefix: for example, "foo"
825 * is passed to the get operation of a handler with prefix "user." to get
826 * attribute "user.foo". The full name is still "there" in the name though.
827 *
828 * Note: the list xattr handler operation when called from the vfs is passed a
829 * NULL name; some file systems use this operation internally, with varying
830 * semantics.
831 */
832const char *xattr_full_name(const struct xattr_handler *handler,
833 const char *name)
834{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100835 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200836
837 return name - prefix_len;
838}
839EXPORT_SYMBOL(xattr_full_name);
840
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400841/*
842 * Allocate new xattr and copy in the value; but leave the name to callers.
843 */
844struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
845{
846 struct simple_xattr *new_xattr;
847 size_t len;
848
849 /* wrap around? */
850 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700851 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400852 return NULL;
853
854 new_xattr = kmalloc(len, GFP_KERNEL);
855 if (!new_xattr)
856 return NULL;
857
858 new_xattr->size = size;
859 memcpy(new_xattr->value, value, size);
860 return new_xattr;
861}
862
863/*
864 * xattr GET operation for in-memory/pseudo filesystems
865 */
866int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
867 void *buffer, size_t size)
868{
869 struct simple_xattr *xattr;
870 int ret = -ENODATA;
871
872 spin_lock(&xattrs->lock);
873 list_for_each_entry(xattr, &xattrs->head, list) {
874 if (strcmp(name, xattr->name))
875 continue;
876
877 ret = xattr->size;
878 if (buffer) {
879 if (size < xattr->size)
880 ret = -ERANGE;
881 else
882 memcpy(buffer, xattr->value, xattr->size);
883 }
884 break;
885 }
886 spin_unlock(&xattrs->lock);
887 return ret;
888}
889
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100890/**
891 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
892 * @xattrs: target simple_xattr list
893 * @name: name of the extended attribute
894 * @value: value of the xattr. If %NULL, will remove the attribute.
895 * @size: size of the new xattr
896 * @flags: %XATTR_{CREATE|REPLACE}
897 *
898 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
899 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
900 * otherwise, fails with -ENODATA.
901 *
902 * Returns 0 on success, -errno on failure.
903 */
904int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
905 const void *value, size_t size, int flags)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400906{
907 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700908 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400909 int err = 0;
910
911 /* value == NULL means remove */
912 if (value) {
913 new_xattr = simple_xattr_alloc(value, size);
914 if (!new_xattr)
915 return -ENOMEM;
916
917 new_xattr->name = kstrdup(name, GFP_KERNEL);
918 if (!new_xattr->name) {
919 kfree(new_xattr);
920 return -ENOMEM;
921 }
922 }
923
924 spin_lock(&xattrs->lock);
925 list_for_each_entry(xattr, &xattrs->head, list) {
926 if (!strcmp(name, xattr->name)) {
927 if (flags & XATTR_CREATE) {
928 xattr = new_xattr;
929 err = -EEXIST;
930 } else if (new_xattr) {
931 list_replace(&xattr->list, &new_xattr->list);
932 } else {
933 list_del(&xattr->list);
934 }
935 goto out;
936 }
937 }
938 if (flags & XATTR_REPLACE) {
939 xattr = new_xattr;
940 err = -ENODATA;
941 } else {
942 list_add(&new_xattr->list, &xattrs->head);
943 xattr = NULL;
944 }
945out:
946 spin_unlock(&xattrs->lock);
947 if (xattr) {
948 kfree(xattr->name);
949 kfree(xattr);
950 }
951 return err;
952
953}
954
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400955static bool xattr_is_trusted(const char *name)
956{
957 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
958}
959
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100960static int xattr_list_one(char **buffer, ssize_t *remaining_size,
961 const char *name)
962{
963 size_t len = strlen(name) + 1;
964 if (*buffer) {
965 if (*remaining_size < len)
966 return -ERANGE;
967 memcpy(*buffer, name, len);
968 *buffer += len;
969 }
970 *remaining_size -= len;
971 return 0;
972}
973
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400974/*
975 * xattr LIST operation for in-memory/pseudo filesystems
976 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100977ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
978 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400979{
980 bool trusted = capable(CAP_SYS_ADMIN);
981 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100982 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100983 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100984
985#ifdef CONFIG_FS_POSIX_ACL
986 if (inode->i_acl) {
987 err = xattr_list_one(&buffer, &remaining_size,
988 XATTR_NAME_POSIX_ACL_ACCESS);
989 if (err)
990 return err;
991 }
992 if (inode->i_default_acl) {
993 err = xattr_list_one(&buffer, &remaining_size,
994 XATTR_NAME_POSIX_ACL_DEFAULT);
995 if (err)
996 return err;
997 }
998#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400999
1000 spin_lock(&xattrs->lock);
1001 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001002 /* skip "trusted." attributes for unprivileged callers */
1003 if (!trusted && xattr_is_trusted(xattr->name))
1004 continue;
1005
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001006 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1007 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001008 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001009 }
1010 spin_unlock(&xattrs->lock);
1011
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001012 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001013}
1014
Aristeu Rozanski48957682012-09-11 16:28:11 -04001015/*
1016 * Adds an extended attribute to the list
1017 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001018void simple_xattr_list_add(struct simple_xattrs *xattrs,
1019 struct simple_xattr *new_xattr)
1020{
1021 spin_lock(&xattrs->lock);
1022 list_add(&new_xattr->list, &xattrs->head);
1023 spin_unlock(&xattrs->lock);
1024}