blob: fd57153b1f6170f2780736c4d0221afd40be21fa [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 File: fs/xattr.c
4
5 Extended attribute handling.
6
7 Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org>
8 Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com>
9 Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
10 */
11#include <linux/fs.h>
12#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/file.h>
14#include <linux/xattr.h>
Dave Hansen18f335a2008-02-15 14:37:38 -080015#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/namei.h>
17#include <linux/security.h>
Mimi Zoharc7b87de2011-03-09 14:39:18 -050018#include <linux/evm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050020#include <linux/export.h>
Robert Love0eeca282005-07-12 17:06:03 -040021#include <linux/fsnotify.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000022#include <linux/audit.h>
Andrew Morton0d08d7b2012-04-05 14:25:07 -070023#include <linux/vmalloc.h>
Eric W. Biederman2f6f0652012-02-07 18:52:57 -080024#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Christoph Hellwig5be196e2006-01-09 20:51:55 -080027
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020028static const char *
29strcmp_prefix(const char *a, const char *a_prefix)
30{
31 while (*a_prefix && *a == *a_prefix) {
32 a++;
33 a_prefix++;
34 }
35 return *a_prefix ? NULL : a;
36}
37
38/*
39 * In order to implement different sets of xattr operations for each xattr
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +020040 * prefix, a filesystem should create a null-terminated array of struct
41 * xattr_handler (one for each prefix) and hang a pointer to it off of the
42 * s_xattr field of the superblock.
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020043 */
44#define for_each_xattr_handler(handlers, handler) \
45 if (handlers) \
46 for ((handler) = *(handlers)++; \
47 (handler) != NULL; \
48 (handler) = *(handlers)++)
49
50/*
51 * Find the xattr_handler with the matching prefix.
52 */
53static const struct xattr_handler *
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020054xattr_resolve_name(struct inode *inode, const char **name)
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020055{
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020056 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020057 const struct xattr_handler *handler;
58
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +020059 if (!(inode->i_opflags & IOP_XATTR)) {
60 if (unlikely(is_bad_inode(inode)))
61 return ERR_PTR(-EIO);
Andreas Gruenbacherd0a5b992016-09-29 17:48:39 +020062 return ERR_PTR(-EOPNOTSUPP);
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +020063 }
Andreas Gruenbacherb6ba1172016-09-29 17:48:38 +020064 for_each_xattr_handler(handlers, handler) {
65 const char *n;
66
67 n = strcmp_prefix(*name, xattr_prefix(handler));
68 if (n) {
69 if (!handler->prefix ^ !*n) {
70 if (*n)
71 continue;
72 return ERR_PTR(-EINVAL);
73 }
74 *name = n;
75 return handler;
76 }
77 }
78 return ERR_PTR(-EOPNOTSUPP);
79}
80
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080081/*
82 * Check permissions for extended attribute access. This is a bit complicated
83 * because different namespaces have very different rules.
84 */
85static int
86xattr_permission(struct inode *inode, const char *name, int mask)
87{
88 /*
89 * We can never set or remove an extended attribute on a read-only
90 * filesystem or on an immutable / append-only inode.
91 */
92 if (mask & MAY_WRITE) {
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -080093 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
94 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -050095 /*
96 * Updating an xattr will likely cause i_uid and i_gid
97 * to be writen back improperly if their true value is
98 * unknown to the vfs.
99 */
100 if (HAS_UNMAPPED_ID(inode))
101 return -EPERM;
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800102 }
103
104 /*
105 * No restriction for security.* and system.* from the VFS. Decision
106 * on these is left to the underlying filesystem / security module.
107 */
108 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
109 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
110 return 0;
111
112 /*
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200113 * The trusted.* namespace can only be accessed by privileged users.
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800114 */
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200115 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
116 if (!capable(CAP_SYS_ADMIN))
117 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
118 return 0;
119 }
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800120
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200121 /*
122 * In the user.* namespace, only regular files and directories can have
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800123 * extended attributes. For sticky directories, only the owner and
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200124 * privileged users can write attributes.
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800125 */
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800126 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800127 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
Andreas Gruenbacher55b23bd2011-05-27 14:50:36 +0200128 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
Andreas Gruenbacherf1f2d872006-11-02 22:07:29 -0800129 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
Serge E. Hallyn2e149672011-03-23 16:43:26 -0700130 (mask & MAY_WRITE) && !inode_owner_or_capable(inode))
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800131 return -EPERM;
132 }
133
Al Virof419a2e2008-07-22 00:07:17 -0400134 return inode_permission(inode, mask);
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800135}
136
Frank van der Lindencab8d282020-06-23 22:39:19 +0000137/*
138 * Look for any handler that deals with the specified namespace.
139 */
140int
141xattr_supported_namespace(struct inode *inode, const char *prefix)
142{
143 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
144 const struct xattr_handler *handler;
145 size_t preflen;
146
147 if (!(inode->i_opflags & IOP_XATTR)) {
148 if (unlikely(is_bad_inode(inode)))
149 return -EIO;
150 return -EOPNOTSUPP;
151 }
152
153 preflen = strlen(prefix);
154
155 for_each_xattr_handler(handlers, handler) {
156 if (!strncmp(xattr_prefix(handler), prefix, preflen))
157 return 0;
158 }
159
160 return -EOPNOTSUPP;
161}
162EXPORT_SYMBOL(xattr_supported_namespace);
163
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200164int
165__vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
166 const void *value, size_t size, int flags)
167{
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200168 const struct xattr_handler *handler;
169
170 handler = xattr_resolve_name(inode, &name);
171 if (IS_ERR(handler))
172 return PTR_ERR(handler);
173 if (!handler->set)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200174 return -EOPNOTSUPP;
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200175 if (size == 0)
176 value = ""; /* empty EA, do not remove */
177 return handler->set(handler, dentry, inode, name, value, size, flags);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200178}
179EXPORT_SYMBOL(__vfs_setxattr);
180
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400181/**
182 * __vfs_setxattr_noperm - perform setxattr operation without performing
183 * permission checks.
184 *
185 * @dentry - object to perform setxattr on
186 * @name - xattr name to set
187 * @value - value to set @name to
188 * @size - size of @value
189 * @flags - flags to pass into filesystem operations
190 *
191 * returns the result of the internal setxattr or setsecurity operations.
192 *
193 * This function requires the caller to lock the inode's i_mutex before it
194 * is executed. It also assumes that the caller will make the appropriate
195 * permission checks.
196 */
197int __vfs_setxattr_noperm(struct dentry *dentry, const char *name,
198 const void *value, size_t size, int flags)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800199{
200 struct inode *inode = dentry->d_inode;
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100201 int error = -EAGAIN;
Andi Kleen69b45732011-05-28 08:25:51 -0700202 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
203 XATTR_SECURITY_PREFIX_LEN);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800204
Andi Kleen69b45732011-05-28 08:25:51 -0700205 if (issec)
206 inode->i_flags &= ~S_NOSEC;
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200207 if (inode->i_opflags & IOP_XATTR) {
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200208 error = __vfs_setxattr(dentry, inode, name, value, size, flags);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800209 if (!error) {
210 fsnotify_xattr(dentry);
211 security_inode_post_setxattr(dentry, name, value,
212 size, flags);
213 }
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100214 } else {
Andreas Gruenbacher5f6e59a2016-09-29 17:48:40 +0200215 if (unlikely(is_bad_inode(inode)))
216 return -EIO;
Andreas Gruenbacher4a590152016-11-13 21:23:34 +0100217 }
218 if (error == -EAGAIN) {
219 error = -EOPNOTSUPP;
220
221 if (issec) {
222 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
223
224 error = security_inode_setsecurity(inode, suffix, value,
225 size, flags);
226 if (!error)
227 fsnotify_xattr(dentry);
228 }
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800229 }
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400230
231 return error;
232}
233
Frank van der Linden08b5d502020-06-23 22:39:18 +0000234/**
Randy Dunlapda5c1c02020-10-13 16:48:27 -0700235 * __vfs_setxattr_locked - set an extended attribute while holding the inode
Frank van der Linden08b5d502020-06-23 22:39:18 +0000236 * lock
237 *
Randy Dunlapda5c1c02020-10-13 16:48:27 -0700238 * @dentry: object to perform setxattr on
239 * @name: xattr name to set
240 * @value: value to set @name to
241 * @size: size of @value
242 * @flags: flags to pass into filesystem operations
243 * @delegated_inode: on return, will contain an inode pointer that
Frank van der Linden08b5d502020-06-23 22:39:18 +0000244 * a delegation was broken on, NULL if none.
245 */
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400246int
Frank van der Linden08b5d502020-06-23 22:39:18 +0000247__vfs_setxattr_locked(struct dentry *dentry, const char *name,
248 const void *value, size_t size, int flags,
249 struct inode **delegated_inode)
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400250{
251 struct inode *inode = dentry->d_inode;
252 int error;
253
254 error = xattr_permission(inode, name, MAY_WRITE);
255 if (error)
256 return error;
257
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400258 error = security_inode_setxattr(dentry, name, value, size, flags);
259 if (error)
260 goto out;
261
Frank van der Linden08b5d502020-06-23 22:39:18 +0000262 error = try_break_deleg(inode, delegated_inode);
263 if (error)
264 goto out;
265
David P. Quigleyb1ab7e42009-09-03 14:25:56 -0400266 error = __vfs_setxattr_noperm(dentry, name, value, size, flags);
267
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800268out:
Frank van der Linden08b5d502020-06-23 22:39:18 +0000269 return error;
270}
271EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
272
273int
274vfs_setxattr(struct dentry *dentry, const char *name, const void *value,
275 size_t size, int flags)
276{
277 struct inode *inode = dentry->d_inode;
278 struct inode *delegated_inode = NULL;
Miklos Szeredi7c03e2c2020-12-14 15:26:13 +0100279 const void *orig_value = value;
Frank van der Linden08b5d502020-06-23 22:39:18 +0000280 int error;
281
Miklos Szeredi7c03e2c2020-12-14 15:26:13 +0100282 if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
283 error = cap_convert_nscap(dentry, &value, size);
284 if (error < 0)
285 return error;
286 size = error;
287 }
288
Frank van der Linden08b5d502020-06-23 22:39:18 +0000289retry_deleg:
290 inode_lock(inode);
291 error = __vfs_setxattr_locked(dentry, name, value, size, flags,
292 &delegated_inode);
Al Viro59551022016-01-22 15:40:57 -0500293 inode_unlock(inode);
Frank van der Linden08b5d502020-06-23 22:39:18 +0000294
295 if (delegated_inode) {
296 error = break_deleg_wait(&delegated_inode);
297 if (!error)
298 goto retry_deleg;
299 }
Miklos Szeredi7c03e2c2020-12-14 15:26:13 +0100300 if (value != orig_value)
301 kfree(value);
302
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800303 return error;
304}
305EXPORT_SYMBOL_GPL(vfs_setxattr);
306
Al Viro2220c5b2018-04-24 21:22:04 -0400307static ssize_t
David P. Quigley42492592008-02-04 22:29:39 -0800308xattr_getsecurity(struct inode *inode, const char *name, void *value,
309 size_t size)
310{
311 void *buffer = NULL;
312 ssize_t len;
313
314 if (!value || !size) {
315 len = security_inode_getsecurity(inode, name, &buffer, false);
316 goto out_noalloc;
317 }
318
319 len = security_inode_getsecurity(inode, name, &buffer, true);
320 if (len < 0)
321 return len;
322 if (size < len) {
323 len = -ERANGE;
324 goto out;
325 }
326 memcpy(value, buffer, len);
327out:
Casey Schaufler57e7ba02017-09-19 09:39:08 -0700328 kfree(buffer);
David P. Quigley42492592008-02-04 22:29:39 -0800329out_noalloc:
330 return len;
331}
David P. Quigley42492592008-02-04 22:29:39 -0800332
Mimi Zohar1601fba2011-03-09 14:23:34 -0500333/*
334 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
335 *
336 * Allocate memory, if not already allocated, or re-allocate correct size,
337 * before retrieving the extended attribute.
338 *
339 * Returns the result of alloc, if failed, or the getxattr operation.
340 */
341ssize_t
342vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
343 size_t xattr_size, gfp_t flags)
344{
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200345 const struct xattr_handler *handler;
Mimi Zohar1601fba2011-03-09 14:23:34 -0500346 struct inode *inode = dentry->d_inode;
347 char *value = *xattr_value;
348 int error;
349
350 error = xattr_permission(inode, name, MAY_READ);
351 if (error)
352 return error;
353
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200354 handler = xattr_resolve_name(inode, &name);
355 if (IS_ERR(handler))
356 return PTR_ERR(handler);
357 if (!handler->get)
Mimi Zohar1601fba2011-03-09 14:23:34 -0500358 return -EOPNOTSUPP;
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200359 error = handler->get(handler, dentry, inode, name, NULL, 0);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500360 if (error < 0)
361 return error;
362
363 if (!value || (error > xattr_size)) {
364 value = krealloc(*xattr_value, error + 1, flags);
365 if (!value)
366 return -ENOMEM;
367 memset(value, 0, error + 1);
368 }
369
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200370 error = handler->get(handler, dentry, inode, name, value, error);
Mimi Zohar1601fba2011-03-09 14:23:34 -0500371 *xattr_value = value;
372 return error;
373}
374
David P. Quigley42492592008-02-04 22:29:39 -0800375ssize_t
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200376__vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
377 void *value, size_t size)
378{
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200379 const struct xattr_handler *handler;
380
381 handler = xattr_resolve_name(inode, &name);
382 if (IS_ERR(handler))
383 return PTR_ERR(handler);
384 if (!handler->get)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200385 return -EOPNOTSUPP;
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200386 return handler->get(handler, dentry, inode, name, value, size);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200387}
388EXPORT_SYMBOL(__vfs_getxattr);
389
390ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700391vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800392{
393 struct inode *inode = dentry->d_inode;
394 int error;
395
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800396 error = xattr_permission(inode, name, MAY_READ);
397 if (error)
398 return error;
399
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800400 error = security_inode_getxattr(dentry, name);
401 if (error)
402 return error;
403
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800404 if (!strncmp(name, XATTR_SECURITY_PREFIX,
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800405 XATTR_SECURITY_PREFIX_LEN)) {
406 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
David P. Quigley42492592008-02-04 22:29:39 -0800407 int ret = xattr_getsecurity(inode, suffix, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800408 /*
409 * Only overwrite the return value if a security module
410 * is actually active.
411 */
David P. Quigley4bea5802008-02-04 22:29:40 -0800412 if (ret == -EOPNOTSUPP)
413 goto nolsm;
414 return ret;
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800415 }
David P. Quigley4bea5802008-02-04 22:29:40 -0800416nolsm:
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200417 return __vfs_getxattr(dentry, inode, name, value, size);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800418}
419EXPORT_SYMBOL_GPL(vfs_getxattr);
420
Bill Nottingham659564c2006-10-09 16:10:48 -0400421ssize_t
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200422vfs_listxattr(struct dentry *dentry, char *list, size_t size)
Bill Nottingham659564c2006-10-09 16:10:48 -0400423{
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200424 struct inode *inode = d_inode(dentry);
Bill Nottingham659564c2006-10-09 16:10:48 -0400425 ssize_t error;
426
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200427 error = security_inode_listxattr(dentry);
Bill Nottingham659564c2006-10-09 16:10:48 -0400428 if (error)
429 return error;
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200430 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200431 error = inode->i_op->listxattr(dentry, list, size);
Bill Nottingham659564c2006-10-09 16:10:48 -0400432 } else {
Andreas Gruenbacherbf3ee712016-09-29 17:48:43 +0200433 error = security_inode_listsecurity(inode, list, size);
Bill Nottingham659564c2006-10-09 16:10:48 -0400434 if (size && error > size)
435 error = -ERANGE;
436 }
437 return error;
438}
439EXPORT_SYMBOL_GPL(vfs_listxattr);
440
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800441int
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200442__vfs_removexattr(struct dentry *dentry, const char *name)
443{
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200444 struct inode *inode = d_inode(dentry);
445 const struct xattr_handler *handler;
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200446
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200447 handler = xattr_resolve_name(inode, &name);
448 if (IS_ERR(handler))
449 return PTR_ERR(handler);
450 if (!handler->set)
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200451 return -EOPNOTSUPP;
Andreas Gruenbacher6c6ef9f2016-09-29 17:48:44 +0200452 return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE);
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200453}
454EXPORT_SYMBOL(__vfs_removexattr);
455
Frank van der Linden08b5d502020-06-23 22:39:18 +0000456/**
Randy Dunlapda5c1c02020-10-13 16:48:27 -0700457 * __vfs_removexattr_locked - set an extended attribute while holding the inode
Frank van der Linden08b5d502020-06-23 22:39:18 +0000458 * lock
459 *
Randy Dunlapda5c1c02020-10-13 16:48:27 -0700460 * @dentry: object to perform setxattr on
461 * @name: name of xattr to remove
462 * @delegated_inode: on return, will contain an inode pointer that
Frank van der Linden08b5d502020-06-23 22:39:18 +0000463 * a delegation was broken on, NULL if none.
464 */
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200465int
Frank van der Linden08b5d502020-06-23 22:39:18 +0000466__vfs_removexattr_locked(struct dentry *dentry, const char *name,
467 struct inode **delegated_inode)
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800468{
469 struct inode *inode = dentry->d_inode;
470 int error;
471
akpm@osdl.orge0ad7b02006-01-09 20:51:56 -0800472 error = xattr_permission(inode, name, MAY_WRITE);
473 if (error)
474 return error;
475
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400476 error = security_inode_removexattr(dentry, name);
Dmitry Kasatkin7c51bb002014-11-20 16:31:01 +0200477 if (error)
478 goto out;
Mimi Zohar2ab51f32011-03-23 17:23:06 -0400479
Frank van der Linden08b5d502020-06-23 22:39:18 +0000480 error = try_break_deleg(inode, delegated_inode);
481 if (error)
482 goto out;
483
Andreas Gruenbacher5d6c3192016-09-29 17:48:42 +0200484 error = __vfs_removexattr(dentry, name);
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800485
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500486 if (!error) {
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800487 fsnotify_xattr(dentry);
Mimi Zoharc7b87de2011-03-09 14:39:18 -0500488 evm_inode_post_removexattr(dentry, name);
489 }
Dmitry Kasatkin7c51bb002014-11-20 16:31:01 +0200490
491out:
Frank van der Linden08b5d502020-06-23 22:39:18 +0000492 return error;
493}
494EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
495
496int
497vfs_removexattr(struct dentry *dentry, const char *name)
498{
499 struct inode *inode = dentry->d_inode;
500 struct inode *delegated_inode = NULL;
501 int error;
502
503retry_deleg:
504 inode_lock(inode);
505 error = __vfs_removexattr_locked(dentry, name, &delegated_inode);
Al Viro59551022016-01-22 15:40:57 -0500506 inode_unlock(inode);
Frank van der Linden08b5d502020-06-23 22:39:18 +0000507
508 if (delegated_inode) {
509 error = break_deleg_wait(&delegated_inode);
510 if (!error)
511 goto retry_deleg;
512 }
513
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800514 return error;
515}
516EXPORT_SYMBOL_GPL(vfs_removexattr);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/*
519 * Extended attribute SET operations
520 */
521static long
David Howells8f0cfa52008-04-29 00:59:41 -0700522setxattr(struct dentry *d, const char __user *name, const void __user *value,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 size_t size, int flags)
524{
525 int error;
526 void *kvalue = NULL;
527 char kname[XATTR_NAME_MAX + 1];
528
529 if (flags & ~(XATTR_CREATE|XATTR_REPLACE))
530 return -EINVAL;
531
532 error = strncpy_from_user(kname, name, sizeof(kname));
533 if (error == 0 || error == sizeof(kname))
534 error = -ERANGE;
535 if (error < 0)
536 return error;
537
538 if (size) {
539 if (size > XATTR_SIZE_MAX)
540 return -E2BIG;
Michal Hocko752ade62017-05-08 15:57:27 -0700541 kvalue = kvmalloc(size, GFP_KERNEL);
542 if (!kvalue)
543 return -ENOMEM;
Andrew Morton44c82492012-04-05 14:25:07 -0700544 if (copy_from_user(kvalue, value, size)) {
545 error = -EFAULT;
546 goto out;
547 }
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800548 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
549 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
550 posix_acl_fix_xattr_from_user(kvalue, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800553 error = vfs_setxattr(d, kname, kvalue, size, flags);
Andrew Morton44c82492012-04-05 14:25:07 -0700554out:
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100555 kvfree(kvalue);
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return error;
558}
559
Eric Biggers8cc43112014-10-12 11:59:58 -0500560static int path_setxattr(const char __user *pathname,
561 const char __user *name, const void __user *value,
562 size_t size, int flags, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Al Viro2d8f3032008-07-22 09:59:21 -0400564 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 int error;
Jeff Layton68f1bb82012-12-11 12:10:15 -0500566retry:
567 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (error)
569 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400570 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800571 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400572 error = setxattr(path.dentry, name, value, size, flags);
573 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800574 }
Al Viro2d8f3032008-07-22 09:59:21 -0400575 path_put(&path);
Jeff Layton68f1bb82012-12-11 12:10:15 -0500576 if (retry_estale(error, lookup_flags)) {
577 lookup_flags |= LOOKUP_REVAL;
578 goto retry;
579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return error;
581}
582
Eric Biggers8cc43112014-10-12 11:59:58 -0500583SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
584 const char __user *, name, const void __user *, value,
585 size_t, size, int, flags)
586{
587 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
588}
589
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100590SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
591 const char __user *, name, const void __user *, value,
592 size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Eric Biggers8cc43112014-10-12 11:59:58 -0500594 return path_setxattr(pathname, name, value, size, flags, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100597SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
598 const void __user *,value, size_t, size, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Al Viro2903ff02012-08-28 12:52:22 -0400600 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int error = -EBADF;
602
Al Viro2903ff02012-08-28 12:52:22 -0400603 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400605 audit_file(f.file);
Miklos Szeredi6742cee2018-07-18 15:44:43 +0200606 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800607 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400608 error = setxattr(f.file->f_path.dentry, name, value, size, flags);
Miklos Szeredi6742cee2018-07-18 15:44:43 +0200609 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800610 }
Al Viro2903ff02012-08-28 12:52:22 -0400611 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return error;
613}
614
615/*
616 * Extended attribute GET operations
617 */
618static ssize_t
David Howells8f0cfa52008-04-29 00:59:41 -0700619getxattr(struct dentry *d, const char __user *name, void __user *value,
620 size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 ssize_t error;
623 void *kvalue = NULL;
624 char kname[XATTR_NAME_MAX + 1];
625
626 error = strncpy_from_user(kname, name, sizeof(kname));
627 if (error == 0 || error == sizeof(kname))
628 error = -ERANGE;
629 if (error < 0)
630 return error;
631
632 if (size) {
633 if (size > XATTR_SIZE_MAX)
634 size = XATTR_SIZE_MAX;
Michal Hocko752ade62017-05-08 15:57:27 -0700635 kvalue = kvzalloc(size, GFP_KERNEL);
636 if (!kvalue)
637 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800640 error = vfs_getxattr(d, kname, kvalue, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700641 if (error > 0) {
Eric W. Biederman2f6f0652012-02-07 18:52:57 -0800642 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
643 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
Christian Brauner82c9a922018-06-07 13:43:48 +0200644 posix_acl_fix_xattr_to_user(kvalue, error);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700645 if (size && copy_to_user(value, kvalue, error))
646 error = -EFAULT;
647 } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) {
648 /* The file system tried to returned a value bigger
649 than XATTR_SIZE_MAX bytes. Not possible. */
650 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100652
653 kvfree(kvalue);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return error;
656}
657
Eric Biggers8cc43112014-10-12 11:59:58 -0500658static ssize_t path_getxattr(const char __user *pathname,
659 const char __user *name, void __user *value,
660 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Al Viro2d8f3032008-07-22 09:59:21 -0400662 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ssize_t error;
Jeff Layton60e66b42012-12-11 12:10:16 -0500664retry:
665 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (error)
667 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400668 error = getxattr(path.dentry, name, value, size);
669 path_put(&path);
Jeff Layton60e66b42012-12-11 12:10:16 -0500670 if (retry_estale(error, lookup_flags)) {
671 lookup_flags |= LOOKUP_REVAL;
672 goto retry;
673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return error;
675}
676
Eric Biggers8cc43112014-10-12 11:59:58 -0500677SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
678 const char __user *, name, void __user *, value, size_t, size)
679{
680 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
681}
682
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100683SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
684 const char __user *, name, void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Eric Biggers8cc43112014-10-12 11:59:58 -0500686 return path_getxattr(pathname, name, value, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100689SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
690 void __user *, value, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Al Viro2903ff02012-08-28 12:52:22 -0400692 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ssize_t error = -EBADF;
694
Al Viro2903ff02012-08-28 12:52:22 -0400695 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400697 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400698 error = getxattr(f.file->f_path.dentry, name, value, size);
699 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return error;
701}
702
703/*
704 * Extended attribute LIST operations
705 */
706static ssize_t
707listxattr(struct dentry *d, char __user *list, size_t size)
708{
709 ssize_t error;
710 char *klist = NULL;
711
712 if (size) {
713 if (size > XATTR_LIST_MAX)
714 size = XATTR_LIST_MAX;
Michal Hocko752ade62017-05-08 15:57:27 -0700715 klist = kvmalloc(size, GFP_KERNEL);
716 if (!klist)
717 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Bill Nottingham659564c2006-10-09 16:10:48 -0400720 error = vfs_listxattr(d, klist, size);
Stephen Smalleyf549d6c2005-09-03 15:55:18 -0700721 if (error > 0) {
722 if (size && copy_to_user(list, klist, error))
723 error = -EFAULT;
724 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
725 /* The file system tried to returned a list bigger
726 than XATTR_LIST_MAX bytes. Not possible. */
727 error = -E2BIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Richard Weinberger0b2a6f22016-01-02 23:09:47 +0100729
730 kvfree(klist);
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return error;
733}
734
Eric Biggers8cc43112014-10-12 11:59:58 -0500735static ssize_t path_listxattr(const char __user *pathname, char __user *list,
736 size_t size, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Al Viro2d8f3032008-07-22 09:59:21 -0400738 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ssize_t error;
Jeff Layton10a90cf2012-12-11 12:10:16 -0500740retry:
741 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (error)
743 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400744 error = listxattr(path.dentry, list, size);
745 path_put(&path);
Jeff Layton10a90cf2012-12-11 12:10:16 -0500746 if (retry_estale(error, lookup_flags)) {
747 lookup_flags |= LOOKUP_REVAL;
748 goto retry;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return error;
751}
752
Eric Biggers8cc43112014-10-12 11:59:58 -0500753SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
754 size_t, size)
755{
756 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
757}
758
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100759SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
760 size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Eric Biggers8cc43112014-10-12 11:59:58 -0500762 return path_listxattr(pathname, list, size, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Heiko Carstens64fd1de2009-01-14 14:14:14 +0100765SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Al Viro2903ff02012-08-28 12:52:22 -0400767 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 ssize_t error = -EBADF;
769
Al Viro2903ff02012-08-28 12:52:22 -0400770 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400772 audit_file(f.file);
Al Viro2903ff02012-08-28 12:52:22 -0400773 error = listxattr(f.file->f_path.dentry, list, size);
774 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return error;
776}
777
778/*
779 * Extended attribute REMOVE operations
780 */
781static long
David Howells8f0cfa52008-04-29 00:59:41 -0700782removexattr(struct dentry *d, const char __user *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int error;
785 char kname[XATTR_NAME_MAX + 1];
786
787 error = strncpy_from_user(kname, name, sizeof(kname));
788 if (error == 0 || error == sizeof(kname))
789 error = -ERANGE;
790 if (error < 0)
791 return error;
792
Christoph Hellwig5be196e2006-01-09 20:51:55 -0800793 return vfs_removexattr(d, kname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Eric Biggers8cc43112014-10-12 11:59:58 -0500796static int path_removexattr(const char __user *pathname,
797 const char __user *name, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Al Viro2d8f3032008-07-22 09:59:21 -0400799 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 int error;
Jeff Layton12f06212012-12-11 12:10:17 -0500801retry:
802 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (error)
804 return error;
Al Viro2d8f3032008-07-22 09:59:21 -0400805 error = mnt_want_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800806 if (!error) {
Al Viro2d8f3032008-07-22 09:59:21 -0400807 error = removexattr(path.dentry, name);
808 mnt_drop_write(path.mnt);
Dave Hansen18f335a2008-02-15 14:37:38 -0800809 }
Al Viro2d8f3032008-07-22 09:59:21 -0400810 path_put(&path);
Jeff Layton12f06212012-12-11 12:10:17 -0500811 if (retry_estale(error, lookup_flags)) {
812 lookup_flags |= LOOKUP_REVAL;
813 goto retry;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return error;
816}
817
Eric Biggers8cc43112014-10-12 11:59:58 -0500818SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
819 const char __user *, name)
820{
821 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
822}
823
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100824SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
825 const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Eric Biggers8cc43112014-10-12 11:59:58 -0500827 return path_removexattr(pathname, name, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Heiko Carstens6a6160a2009-01-14 14:14:15 +0100830SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Al Viro2903ff02012-08-28 12:52:22 -0400832 struct fd f = fdget(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 int error = -EBADF;
834
Al Viro2903ff02012-08-28 12:52:22 -0400835 if (!f.file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return error;
Al Viro9f45f5b2014-10-31 17:44:57 -0400837 audit_file(f.file);
Miklos Szeredi6742cee2018-07-18 15:44:43 +0200838 error = mnt_want_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800839 if (!error) {
Al Viro9f45f5b2014-10-31 17:44:57 -0400840 error = removexattr(f.file->f_path.dentry, name);
Miklos Szeredi6742cee2018-07-18 15:44:43 +0200841 mnt_drop_write_file(f.file);
Dave Hansen18f335a2008-02-15 14:37:38 -0800842 }
Al Viro2903ff02012-08-28 12:52:22 -0400843 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return error;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * Combine the results of the list() operation from every xattr_handler in the
849 * list.
850 */
851ssize_t
852generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
853{
Stephen Hemmingerbb435452010-05-13 17:53:14 -0700854 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 unsigned int size = 0;
856
857 if (!buffer) {
Christoph Hellwig431547b2009-11-13 09:52:56 +0000858 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100859 if (!handler->name ||
860 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100861 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100862 size += strlen(handler->name) + 1;
Christoph Hellwig431547b2009-11-13 09:52:56 +0000863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 } else {
865 char *buf = buffer;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100866 size_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 for_each_xattr_handler(handlers, handler) {
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100869 if (!handler->name ||
870 (handler->list && !handler->list(dentry)))
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +0100871 continue;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100872 len = strlen(handler->name);
873 if (len + 1 > buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return -ERANGE;
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +0100875 memcpy(buf, handler->name, len + 1);
876 buf += len + 1;
877 buffer_size -= len + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
879 size = buf - buffer;
880 }
881 return size;
882}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883EXPORT_SYMBOL(generic_listxattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400884
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200885/**
886 * xattr_full_name - Compute full attribute name from suffix
887 *
888 * @handler: handler of the xattr_handler operation
889 * @name: name passed to the xattr_handler operation
890 *
891 * The get and set xattr handler operations are called with the remainder of
892 * the attribute name after skipping the handler's prefix: for example, "foo"
893 * is passed to the get operation of a handler with prefix "user." to get
894 * attribute "user.foo". The full name is still "there" in the name though.
895 *
896 * Note: the list xattr handler operation when called from the vfs is passed a
897 * NULL name; some file systems use this operation internally, with varying
898 * semantics.
899 */
900const char *xattr_full_name(const struct xattr_handler *handler,
901 const char *name)
902{
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +0100903 size_t prefix_len = strlen(xattr_prefix(handler));
Andreas Gruenbachere409de92015-10-04 19:18:52 +0200904
905 return name - prefix_len;
906}
907EXPORT_SYMBOL(xattr_full_name);
908
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400909/*
910 * Allocate new xattr and copy in the value; but leave the name to callers.
911 */
912struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
913{
914 struct simple_xattr *new_xattr;
915 size_t len;
916
917 /* wrap around? */
918 len = sizeof(*new_xattr) + size;
Hugh Dickins4e66d442014-07-23 14:00:17 -0700919 if (len < sizeof(*new_xattr))
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400920 return NULL;
921
Daniel Xufdc85222020-03-12 13:03:14 -0700922 new_xattr = kvmalloc(len, GFP_KERNEL);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400923 if (!new_xattr)
924 return NULL;
925
926 new_xattr->size = size;
927 memcpy(new_xattr->value, value, size);
928 return new_xattr;
929}
930
931/*
932 * xattr GET operation for in-memory/pseudo filesystems
933 */
934int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
935 void *buffer, size_t size)
936{
937 struct simple_xattr *xattr;
938 int ret = -ENODATA;
939
940 spin_lock(&xattrs->lock);
941 list_for_each_entry(xattr, &xattrs->head, list) {
942 if (strcmp(name, xattr->name))
943 continue;
944
945 ret = xattr->size;
946 if (buffer) {
947 if (size < xattr->size)
948 ret = -ERANGE;
949 else
950 memcpy(buffer, xattr->value, xattr->size);
951 }
952 break;
953 }
954 spin_unlock(&xattrs->lock);
955 return ret;
956}
957
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100958/**
959 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
960 * @xattrs: target simple_xattr list
961 * @name: name of the extended attribute
962 * @value: value of the xattr. If %NULL, will remove the attribute.
963 * @size: size of the new xattr
964 * @flags: %XATTR_{CREATE|REPLACE}
Daniel Xua46a2292020-03-12 13:03:15 -0700965 * @removed_size: returns size of the removed xattr, -1 if none removed
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +0100966 *
967 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
968 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
969 * otherwise, fails with -ENODATA.
970 *
971 * Returns 0 on success, -errno on failure.
972 */
973int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
Daniel Xua46a2292020-03-12 13:03:15 -0700974 const void *value, size_t size, int flags,
975 ssize_t *removed_size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400976{
977 struct simple_xattr *xattr;
David Rientjes43385842012-10-17 20:41:15 -0700978 struct simple_xattr *new_xattr = NULL;
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400979 int err = 0;
980
Daniel Xu772b3142020-04-08 23:27:29 -0700981 if (removed_size)
982 *removed_size = -1;
983
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400984 /* value == NULL means remove */
985 if (value) {
986 new_xattr = simple_xattr_alloc(value, size);
987 if (!new_xattr)
988 return -ENOMEM;
989
990 new_xattr->name = kstrdup(name, GFP_KERNEL);
991 if (!new_xattr->name) {
Daniel Xufdc85222020-03-12 13:03:14 -0700992 kvfree(new_xattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -0400993 return -ENOMEM;
994 }
995 }
996
997 spin_lock(&xattrs->lock);
998 list_for_each_entry(xattr, &xattrs->head, list) {
999 if (!strcmp(name, xattr->name)) {
1000 if (flags & XATTR_CREATE) {
1001 xattr = new_xattr;
1002 err = -EEXIST;
1003 } else if (new_xattr) {
1004 list_replace(&xattr->list, &new_xattr->list);
Daniel Xua46a2292020-03-12 13:03:15 -07001005 if (removed_size)
1006 *removed_size = xattr->size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001007 } else {
1008 list_del(&xattr->list);
Daniel Xua46a2292020-03-12 13:03:15 -07001009 if (removed_size)
1010 *removed_size = xattr->size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001011 }
1012 goto out;
1013 }
1014 }
1015 if (flags & XATTR_REPLACE) {
1016 xattr = new_xattr;
1017 err = -ENODATA;
1018 } else {
1019 list_add(&new_xattr->list, &xattrs->head);
1020 xattr = NULL;
1021 }
1022out:
1023 spin_unlock(&xattrs->lock);
1024 if (xattr) {
1025 kfree(xattr->name);
Daniel Xufdc85222020-03-12 13:03:14 -07001026 kvfree(xattr);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001027 }
1028 return err;
1029
1030}
1031
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001032static bool xattr_is_trusted(const char *name)
1033{
1034 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1035}
1036
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001037static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1038 const char *name)
1039{
1040 size_t len = strlen(name) + 1;
1041 if (*buffer) {
1042 if (*remaining_size < len)
1043 return -ERANGE;
1044 memcpy(*buffer, name, len);
1045 *buffer += len;
1046 }
1047 *remaining_size -= len;
1048 return 0;
1049}
1050
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001051/*
1052 * xattr LIST operation for in-memory/pseudo filesystems
1053 */
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001054ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1055 char *buffer, size_t size)
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001056{
1057 bool trusted = capable(CAP_SYS_ADMIN);
1058 struct simple_xattr *xattr;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001059 ssize_t remaining_size = size;
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001060 int err = 0;
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001061
1062#ifdef CONFIG_FS_POSIX_ACL
Andreas Gruenbacherffc4c922018-09-18 00:36:36 -04001063 if (IS_POSIXACL(inode)) {
1064 if (inode->i_acl) {
1065 err = xattr_list_one(&buffer, &remaining_size,
1066 XATTR_NAME_POSIX_ACL_ACCESS);
1067 if (err)
1068 return err;
1069 }
1070 if (inode->i_default_acl) {
1071 err = xattr_list_one(&buffer, &remaining_size,
1072 XATTR_NAME_POSIX_ACL_DEFAULT);
1073 if (err)
1074 return err;
1075 }
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001076 }
1077#endif
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001078
1079 spin_lock(&xattrs->lock);
1080 list_for_each_entry(xattr, &xattrs->head, list) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001081 /* skip "trusted." attributes for unprivileged callers */
1082 if (!trusted && xattr_is_trusted(xattr->name))
1083 continue;
1084
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01001085 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1086 if (err)
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001087 break;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001088 }
1089 spin_unlock(&xattrs->lock);
1090
Mateusz Guzik0e9a7da2016-02-04 02:56:30 +01001091 return err ? err : size - remaining_size;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001092}
1093
Aristeu Rozanski48957682012-09-11 16:28:11 -04001094/*
1095 * Adds an extended attribute to the list
1096 */
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001097void simple_xattr_list_add(struct simple_xattrs *xattrs,
1098 struct simple_xattr *new_xattr)
1099{
1100 spin_lock(&xattrs->lock);
1101 list_add(&new_xattr->list, &xattrs->head);
1102 spin_unlock(&xattrs->lock);
1103}