blob: e1ccf2be88ac599f82cc222af031f09582e704db [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
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200329vfs_listxattr(struct dentry *dentry, char *list, size_t size)
Bill Nottingham659564c2006-10-09 16:10:48 -0400330{
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200331 struct inode *inode = d_inode(dentry);
Bill Nottingham659564c2006-10-09 16:10:48 -0400332 ssize_t error;
333
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200334 error = security_inode_listxattr(dentry);
Bill Nottingham659564c2006-10-09 16:10:48 -0400335 if (error)
336 return error;
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200337 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
338 error = -EOPNOTSUPP;
339 error = inode->i_op->listxattr(dentry, list, size);
Bill Nottingham659564c2006-10-09 16:10:48 -0400340 } else {
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200341 error = security_inode_listsecurity(inode, list, size);
Bill Nottingham659564c2006-10-09 16:10:48 -0400342 if (size && error > size)
343 error = -ERANGE;
344 }
345 return error;
346}
347EXPORT_SYMBOL_GPL(vfs_listxattr);
348
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800349int
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200350__vfs_removexattr(struct dentry *dentry, const char *name)
351{
352 struct inode *inode = dentry->d_inode;
353
354 if (!inode->i_op->removexattr)
355 return -EOPNOTSUPP;
356 return inode->i_op->removexattr(dentry, name);
357}
358EXPORT_SYMBOL(__vfs_removexattr);
359
360int
David Howells8f0cfa52008-04-29 00:59:41 -0700361vfs_removexattr(struct dentry *dentry, const char *name)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800362{
363 struct inode *inode = dentry->d_inode;
364 int error;
365
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800366 error = xattr_permission(inode, name, MAY_WRITE);
367 if (error)
368 return error;
369
Al Viro59551022016-01-22 15:40:57 -0500370 inode_lock(inode);
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400371 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200372 if (error)
373 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400374
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200375 error = __vfs_removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800376
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500377 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800378 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500379 evm_inode_post_removexattr(dentry, name);
380 }
Dmitry Kasatkin7c51bb02014-11-20 16:31:01 +0200381
382out:
Al Viro59551022016-01-22 15:40:57 -0500383 inode_unlock(inode);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800384 return error;
385}
386EXPORT_SYMBOL_GPL(vfs_removexattr);
387
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/*
390 * Extended attribute SET operations
391 */
392static long
David Howells8f0cfa52008-04-29 00:59:41 -0700393setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 size_t size, int flags)
395{
396 int error;
397 void *kvalue = NULL;
398 char kname[XATTR_NAME_MAX + 1];
399
400 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
401 return -EINVAL;
402
403 error = strncpy_from_user(kname, name, sizeof(kname));
404 if (error == 0 || error == sizeof(kname))
405 error = -ERANGE;
406 if (error < 0)
407 return error;
408
409 if (size) {
410 if (size > XATTR_SIZE_MAX)
411 return -E2BIG;
Andrew Morton44c82492012-04-05 14:25:07 -0700412 kvalue = kmalloc(size, GFP_KERNEL | __GFP_NOWARN);
413 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100414 kvalue = vmalloc(size);
415 if (!kvalue)
Andrew Morton44c82492012-04-05 14:25:07 -0700416 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700417 }
418 if (copy_from_user(kvalue, value, size)) {
419 error = -EFAULT;
420 goto out;
421 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800422 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
423 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
424 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800427 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700428out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100429 kvfree(kvalue);
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return error;
432}
433
Eric Biggers8cc43112014-10-12 11:59:58 -0500434static int path_setxattr(const char __user *pathname,
435 const char __user *name, const void __user *value,
436 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
Al Viro2d8f3032008-07-22 09:59:21 -0400438 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500440retry:
441 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (error)
443 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400444 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800445 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400446 error = setxattr(path.dentry, name, value, size, flags);
447 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800448 }
Al Viro2d8f3032008-07-22 09:59:21 -0400449 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500450 if (retry_estale(error, lookup_flags)) {
451 lookup_flags |= LOOKUP_REVAL;
452 goto retry;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return error;
455}
456
Eric Biggers8cc43112014-10-12 11:59:58 -0500457SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
458 const char __user *, name, const void __user *, value,
459 size_t, size, int, flags)
460{
461 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
462}
463
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100464SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
465 const char __user *, name, const void __user *, value,
466 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Eric Biggers8cc43112014-10-12 11:59:58 -0500468 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100471SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
472 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Al Viro2903ff02012-08-28 12:52:22 -0400474 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int error = -EBADF;
476
Al Viro2903ff02012-08-28 12:52:22 -0400477 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400479 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400480 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800481 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400482 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Al Viro2903ff02012-08-28 12:52:22 -0400483 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800484 }
Al Viro2903ff02012-08-28 12:52:22 -0400485 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return error;
487}
488
489/*
490 * Extended attribute GET operations
491 */
492static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700493getxattr(struct dentry *d, const char __user *name, void __user *value,
494 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 ssize_t error;
497 void *kvalue = NULL;
498 char kname[XATTR_NAME_MAX + 1];
499
500 error = strncpy_from_user(kname, name, sizeof(kname));
501 if (error == 0 || error == sizeof(kname))
502 error = -ERANGE;
503 if (error < 0)
504 return error;
505
506 if (size) {
507 if (size > XATTR_SIZE_MAX)
508 size = XATTR_SIZE_MAX;
Sasha Levin779302e2012-07-30 14:39:13 -0700509 kvalue = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
510 if (!kvalue) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100511 kvalue = vmalloc(size);
512 if (!kvalue)
Sasha Levin779302e2012-07-30 14:39:13 -0700513 return -ENOMEM;
Sasha Levin779302e2012-07-30 14:39:13 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800517 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700518 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800519 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
520 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
521 posix_acl_fix_xattr_to_user(kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700522 if (size && copy_to_user(value, kvalue, error))
523 error = -EFAULT;
524 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
525 /* The file system tried to returned a value bigger
526 than XATTR_SIZE_MAX bytes. Not possible. */
527 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100529
530 kvfree(kvalue);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return error;
533}
534
Eric Biggers8cc43112014-10-12 11:59:58 -0500535static ssize_t path_getxattr(const char __user *pathname,
536 const char __user *name, void __user *value,
537 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Al Viro2d8f3032008-07-22 09:59:21 -0400539 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500541retry:
542 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (error)
544 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400545 error = getxattr(path.dentry, name, value, size);
546 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500547 if (retry_estale(error, lookup_flags)) {
548 lookup_flags |= LOOKUP_REVAL;
549 goto retry;
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return error;
552}
553
Eric Biggers8cc43112014-10-12 11:59:58 -0500554SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
555 const char __user *, name, void __user *, value, size_t, size)
556{
557 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
558}
559
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100560SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
561 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Eric Biggers8cc43112014-10-12 11:59:58 -0500563 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100566SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
567 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Al Viro2903ff02012-08-28 12:52:22 -0400569 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ssize_t error = -EBADF;
571
Al Viro2903ff02012-08-28 12:52:22 -0400572 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400574 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400575 error = getxattr(f.file->f_path.dentry, name, value, size);
576 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
578}
579
580/*
581 * Extended attribute LIST operations
582 */
583static ssize_t
584listxattr(struct dentry *d, char __user *list, size_t size)
585{
586 ssize_t error;
587 char *klist = NULL;
588
589 if (size) {
590 if (size > XATTR_LIST_MAX)
591 size = XATTR_LIST_MAX;
Dave Jones703bf2d2012-04-05 14:25:06 -0700592 klist = kmalloc(size, __GFP_NOWARN | GFP_KERNEL);
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700593 if (!klist) {
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100594 klist = vmalloc(size);
595 if (!klist)
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700596 return -ENOMEM;
Andrew Morton0d08d7b2012-04-05 14:25:07 -0700597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
Bill Nottingham659564c2006-10-09 16:10:48 -0400600 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700601 if (error > 0) {
602 if (size && copy_to_user(list, klist, error))
603 error = -EFAULT;
604 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
605 /* The file system tried to returned a list bigger
606 than XATTR_LIST_MAX bytes. Not possible. */
607 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100609
610 kvfree(klist);
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return error;
613}
614
Eric Biggers8cc43112014-10-12 11:59:58 -0500615static ssize_t path_listxattr(const char __user *pathname, char __user *list,
616 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Al Viro2d8f3032008-07-22 09:59:21 -0400618 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500620retry:
621 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (error)
623 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400624 error = listxattr(path.dentry, list, size);
625 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500626 if (retry_estale(error, lookup_flags)) {
627 lookup_flags |= LOOKUP_REVAL;
628 goto retry;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return error;
631}
632
Eric Biggers8cc43112014-10-12 11:59:58 -0500633SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
634 size_t, size)
635{
636 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
637}
638
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100639SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
640 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Eric Biggers8cc43112014-10-12 11:59:58 -0500642 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100645SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Al Viro2903ff02012-08-28 12:52:22 -0400647 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ssize_t error = -EBADF;
649
Al Viro2903ff02012-08-28 12:52:22 -0400650 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400652 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400653 error = listxattr(f.file->f_path.dentry, list, size);
654 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return error;
656}
657
658/*
659 * Extended attribute REMOVE operations
660 */
661static long
David Howells8f0cfa52008-04-29 00:59:41 -0700662removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 int error;
665 char kname[XATTR_NAME_MAX + 1];
666
667 error = strncpy_from_user(kname, name, sizeof(kname));
668 if (error == 0 || error == sizeof(kname))
669 error = -ERANGE;
670 if (error < 0)
671 return error;
672
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800673 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Eric Biggers8cc43112014-10-12 11:59:58 -0500676static int path_removexattr(const char __user *pathname,
677 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Al Viro2d8f3032008-07-22 09:59:21 -0400679 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500681retry:
682 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (error)
684 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400685 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800686 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400687 error = removexattr(path.dentry, name);
688 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800689 }
Al Viro2d8f3032008-07-22 09:59:21 -0400690 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500691 if (retry_estale(error, lookup_flags)) {
692 lookup_flags |= LOOKUP_REVAL;
693 goto retry;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return error;
696}
697
Eric Biggers8cc43112014-10-12 11:59:58 -0500698SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
699 const char __user *, name)
700{
701 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
702}
703
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100704SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
705 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Eric Biggers8cc43112014-10-12 11:59:58 -0500707 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100710SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Al Viro2903ff02012-08-28 12:52:22 -0400712 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 int error = -EBADF;
714
Al Viro2903ff02012-08-28 12:52:22 -0400715 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400717 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400718 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800719 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400720 error = removexattr(f.file->f_path.dentry, name);
Al Viro2903ff02012-08-28 12:52:22 -0400721 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800722 }
Al Viro2903ff02012-08-28 12:52:22 -0400723 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return error;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * Find the handler for the prefix and dispatch its get() operation.
729 */
730ssize_t
Al Viroce23e642016-04-11 00:48:00 -0400731generic_getxattr(struct dentry *dentry, struct inode *inode,
732 const char *name, void *buffer, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700734 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200736 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100737 if (IS_ERR(handler))
738 return PTR_ERR(handler);
Al Viroce23e642016-04-11 00:48:00 -0400739 return handler->get(handler, dentry, inode,
Al Virob2968212016-04-10 20:48:24 -0400740 name, buffer, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
744 * Combine the results of the list() operation from every xattr_handler in the
745 * list.
746 */
747ssize_t
748generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
749{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700750 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 unsigned int size = 0;
752
753 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000754 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100755 if (!handler->name ||
756 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100757 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100758 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } else {
761 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100762 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100765 if (!handler->name ||
766 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100767 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100768 len = strlen(handler->name);
769 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100771 memcpy(buf, handler->name, len + 1);
772 buf += len + 1;
773 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 }
775 size = buf - buffer;
776 }
777 return size;
778}
779
780/*
781 * Find the handler for the prefix and dispatch its set() operation.
782 */
783int
Al Viro3767e252016-05-27 11:06:05 -0400784generic_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
785 const void *value, size_t size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700787 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 if (size == 0)
790 value = ""; /* empty EA, do not remove */
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200791 handler = xattr_resolve_name(inode, &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100792 if (IS_ERR(handler))
793 return PTR_ERR(handler);
Al Viro3767e252016-05-27 11:06:05 -0400794 return handler->set(handler, dentry, inode, name, value, size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797/*
798 * Find the handler for the prefix and dispatch its set() operation to remove
799 * any associated extended attribute.
800 */
801int
802generic_removexattr(struct dentry *dentry, const char *name)
803{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700804 const struct xattr_handler *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +0200806 handler = xattr_resolve_name(d_inode(dentry), &name);
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100807 if (IS_ERR(handler))
808 return PTR_ERR(handler);
Al Viro59301222016-05-27 10:19:30 -0400809 return handler->set(handler, dentry, d_inode(dentry), name, NULL,
810 0, XATTR_REPLACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813EXPORT_SYMBOL(generic_getxattr);
814EXPORT_SYMBOL(generic_listxattr);
815EXPORT_SYMBOL(generic_setxattr);
816EXPORT_SYMBOL(generic_removexattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400817
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200818/**
819 * xattr_full_name - Compute full attribute name from suffix
820 *
821 * @handler: handler of the xattr_handler operation
822 * @name: name passed to the xattr_handler operation
823 *
824 * The get and set xattr handler operations are called with the remainder of
825 * the attribute name after skipping the handler's prefix: for example, "foo"
826 * is passed to the get operation of a handler with prefix "user." to get
827 * attribute "user.foo". The full name is still "there" in the name though.
828 *
829 * Note: the list xattr handler operation when called from the vfs is passed a
830 * NULL name; some file systems use this operation internally, with varying
831 * semantics.
832 */
833const char *xattr_full_name(const struct xattr_handler *handler,
834 const char *name)
835{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100836 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200837
838 return name - prefix_len;
839}
840EXPORT_SYMBOL(xattr_full_name);
841
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400842/*
843 * Allocate new xattr and copy in the value; but leave the name to callers.
844 */
845struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
846{
847 struct simple_xattr *new_xattr;
848 size_t len;
849
850 /* wrap around? */
851 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700852 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400853 return NULL;
854
855 new_xattr = kmalloc(len, GFP_KERNEL);
856 if (!new_xattr)
857 return NULL;
858
859 new_xattr->size = size;
860 memcpy(new_xattr->value, value, size);
861 return new_xattr;
862}
863
864/*
865 * xattr GET operation for in-memory/pseudo filesystems
866 */
867int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
868 void *buffer, size_t size)
869{
870 struct simple_xattr *xattr;
871 int ret = -ENODATA;
872
873 spin_lock(&xattrs->lock);
874 list_for_each_entry(xattr, &xattrs->head, list) {
875 if (strcmp(name, xattr->name))
876 continue;
877
878 ret = xattr->size;
879 if (buffer) {
880 if (size < xattr->size)
881 ret = -ERANGE;
882 else
883 memcpy(buffer, xattr->value, xattr->size);
884 }
885 break;
886 }
887 spin_unlock(&xattrs->lock);
888 return ret;
889}
890
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100891/**
892 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
893 * @xattrs: target simple_xattr list
894 * @name: name of the extended attribute
895 * @value: value of the xattr. If %NULL, will remove the attribute.
896 * @size: size of the new xattr
897 * @flags: %XATTR_{CREATE|REPLACE}
898 *
899 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
900 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
901 * otherwise, fails with -ENODATA.
902 *
903 * Returns 0 on success, -errno on failure.
904 */
905int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
906 const void *value, size_t size, int flags)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400907{
908 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700909 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400910 int err = 0;
911
912 /* value == NULL means remove */
913 if (value) {
914 new_xattr = simple_xattr_alloc(value, size);
915 if (!new_xattr)
916 return -ENOMEM;
917
918 new_xattr->name = kstrdup(name, GFP_KERNEL);
919 if (!new_xattr->name) {
920 kfree(new_xattr);
921 return -ENOMEM;
922 }
923 }
924
925 spin_lock(&xattrs->lock);
926 list_for_each_entry(xattr, &xattrs->head, list) {
927 if (!strcmp(name, xattr->name)) {
928 if (flags & XATTR_CREATE) {
929 xattr = new_xattr;
930 err = -EEXIST;
931 } else if (new_xattr) {
932 list_replace(&xattr->list, &new_xattr->list);
933 } else {
934 list_del(&xattr->list);
935 }
936 goto out;
937 }
938 }
939 if (flags & XATTR_REPLACE) {
940 xattr = new_xattr;
941 err = -ENODATA;
942 } else {
943 list_add(&new_xattr->list, &xattrs->head);
944 xattr = NULL;
945 }
946out:
947 spin_unlock(&xattrs->lock);
948 if (xattr) {
949 kfree(xattr->name);
950 kfree(xattr);
951 }
952 return err;
953
954}
955
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400956static bool xattr_is_trusted(const char *name)
957{
958 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
959}
960
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100961static int xattr_list_one(char **buffer, ssize_t *remaining_size,
962 const char *name)
963{
964 size_t len = strlen(name) + 1;
965 if (*buffer) {
966 if (*remaining_size < len)
967 return -ERANGE;
968 memcpy(*buffer, name, len);
969 *buffer += len;
970 }
971 *remaining_size -= len;
972 return 0;
973}
974
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400975/*
976 * xattr LIST operation for in-memory/pseudo filesystems
977 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100978ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
979 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400980{
981 bool trusted = capable(CAP_SYS_ADMIN);
982 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100983 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +0100984 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +0100985
986#ifdef CONFIG_FS_POSIX_ACL
987 if (inode->i_acl) {
988 err = xattr_list_one(&buffer, &remaining_size,
989 XATTR_NAME_POSIX_ACL_ACCESS);
990 if (err)
991 return err;
992 }
993 if (inode->i_default_acl) {
994 err = xattr_list_one(&buffer, &remaining_size,
995 XATTR_NAME_POSIX_ACL_DEFAULT);
996 if (err)
997 return err;
998 }
999#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001000
1001 spin_lock(&xattrs->lock);
1002 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001003 /* skip "trusted." attributes for unprivileged callers */
1004 if (!trusted && xattr_is_trusted(xattr->name))
1005 continue;
1006
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001007 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1008 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001009 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001010 }
1011 spin_unlock(&xattrs->lock);
1012
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001013 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001014}
1015
Aristeu Rozanski48957682012-09-11 16:28:11 -04001016/*
1017 * Adds an extended attribute to the list
1018 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001019void simple_xattr_list_add(struct simple_xattrs *xattrs,
1020 struct simple_xattr *new_xattr)
1021{
1022 spin_lock(&xattrs->lock);
1023 list_add(&new_xattr->list, &xattrs->head);
1024 spin_unlock(&xattrs->lock);
1025}