Merge tag 'fbdev-for-6.1-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/deller...
[linux-block.git] / fs / xattr.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
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>
1da177e4
LT
13#include <linux/file.h>
14#include <linux/xattr.h>
18f335af 15#include <linux/mount.h>
1da177e4
LT
16#include <linux/namei.h>
17#include <linux/security.h>
c7b87de2 18#include <linux/evm.h>
1da177e4 19#include <linux/syscalls.h>
630d9c47 20#include <linux/export.h>
0eeca283 21#include <linux/fsnotify.h>
73241ccc 22#include <linux/audit.h>
0d08d7b7 23#include <linux/vmalloc.h>
2f6f0654 24#include <linux/posix_acl_xattr.h>
1da177e4 25
7c0f6ba6 26#include <linux/uaccess.h>
5be196e5 27
1a91794c
SR
28#include "internal.h"
29
b6ba1177
AG
30static const char *
31strcmp_prefix(const char *a, const char *a_prefix)
32{
33 while (*a_prefix && *a == *a_prefix) {
34 a++;
35 a_prefix++;
36 }
37 return *a_prefix ? NULL : a;
38}
39
40/*
41 * In order to implement different sets of xattr operations for each xattr
6c6ef9f2
AG
42 * prefix, a filesystem should create a null-terminated array of struct
43 * xattr_handler (one for each prefix) and hang a pointer to it off of the
44 * s_xattr field of the superblock.
b6ba1177
AG
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 *
d0a5b995 56xattr_resolve_name(struct inode *inode, const char **name)
b6ba1177 57{
d0a5b995 58 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
b6ba1177
AG
59 const struct xattr_handler *handler;
60
5f6e59ae
AG
61 if (!(inode->i_opflags & IOP_XATTR)) {
62 if (unlikely(is_bad_inode(inode)))
63 return ERR_PTR(-EIO);
d0a5b995 64 return ERR_PTR(-EOPNOTSUPP);
5f6e59ae 65 }
b6ba1177
AG
66 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
e0ad7b07 83/*
84 * Check permissions for extended attribute access. This is a bit complicated
85 * because different namespaces have very different rules.
86 */
87static int
c7c7a1a1
TA
88xattr_permission(struct user_namespace *mnt_userns, struct inode *inode,
89 const char *name, int mask)
e0ad7b07 90{
91 /*
92 * We can never set or remove an extended attribute on a read-only
93 * filesystem or on an immutable / append-only inode.
94 */
95 if (mask & MAY_WRITE) {
e0ad7b07 96 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
97 return -EPERM;
0bd23d09
EB
98 /*
99 * Updating an xattr will likely cause i_uid and i_gid
100 * to be writen back improperly if their true value is
101 * unknown to the vfs.
102 */
ba73d987 103 if (HAS_UNMAPPED_ID(mnt_userns, inode))
0bd23d09 104 return -EPERM;
e0ad7b07 105 }
106
107 /*
108 * No restriction for security.* and system.* from the VFS. Decision
109 * on these is left to the underlying filesystem / security module.
110 */
111 if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) ||
112 !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
113 return 0;
114
115 /*
55b23bde 116 * The trusted.* namespace can only be accessed by privileged users.
e0ad7b07 117 */
55b23bde
AG
118 if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) {
119 if (!capable(CAP_SYS_ADMIN))
120 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
121 return 0;
122 }
e0ad7b07 123
55b23bde
AG
124 /*
125 * In the user.* namespace, only regular files and directories can have
f1f2d871 126 * extended attributes. For sticky directories, only the owner and
55b23bde 127 * privileged users can write attributes.
f1f2d871 128 */
e0ad7b07 129 if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) {
f1f2d871 130 if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode))
55b23bde 131 return (mask & MAY_WRITE) ? -EPERM : -ENODATA;
f1f2d871 132 if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
21cb47be 133 (mask & MAY_WRITE) &&
c7c7a1a1 134 !inode_owner_or_capable(mnt_userns, inode))
e0ad7b07 135 return -EPERM;
136 }
137
c7c7a1a1 138 return inode_permission(mnt_userns, inode, mask);
e0ad7b07 139}
140
cab8d289
FL
141/*
142 * Look for any handler that deals with the specified namespace.
143 */
144int
145xattr_supported_namespace(struct inode *inode, const char *prefix)
146{
147 const struct xattr_handler **handlers = inode->i_sb->s_xattr;
148 const struct xattr_handler *handler;
149 size_t preflen;
150
151 if (!(inode->i_opflags & IOP_XATTR)) {
152 if (unlikely(is_bad_inode(inode)))
153 return -EIO;
154 return -EOPNOTSUPP;
155 }
156
157 preflen = strlen(prefix);
158
159 for_each_xattr_handler(handlers, handler) {
160 if (!strncmp(xattr_prefix(handler), prefix, preflen))
161 return 0;
162 }
163
164 return -EOPNOTSUPP;
165}
166EXPORT_SYMBOL(xattr_supported_namespace);
167
5d6c3191 168int
c7c7a1a1
TA
169__vfs_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
170 struct inode *inode, const char *name, const void *value,
171 size_t size, int flags)
5d6c3191 172{
6c6ef9f2
AG
173 const struct xattr_handler *handler;
174
175 handler = xattr_resolve_name(inode, &name);
176 if (IS_ERR(handler))
177 return PTR_ERR(handler);
178 if (!handler->set)
5d6c3191 179 return -EOPNOTSUPP;
6c6ef9f2
AG
180 if (size == 0)
181 value = ""; /* empty EA, do not remove */
c7c7a1a1 182 return handler->set(handler, mnt_userns, dentry, inode, name, value,
e65ce2a5 183 size, flags);
5d6c3191
AG
184}
185EXPORT_SYMBOL(__vfs_setxattr);
186
b1ab7e4b
DQ
187/**
188 * __vfs_setxattr_noperm - perform setxattr operation without performing
189 * permission checks.
190 *
6961fed4
RD
191 * @mnt_userns: user namespace of the mount the inode was found from
192 * @dentry: object to perform setxattr on
193 * @name: xattr name to set
194 * @value: value to set @name to
195 * @size: size of @value
196 * @flags: flags to pass into filesystem operations
b1ab7e4b
DQ
197 *
198 * returns the result of the internal setxattr or setsecurity operations.
199 *
200 * This function requires the caller to lock the inode's i_mutex before it
201 * is executed. It also assumes that the caller will make the appropriate
202 * permission checks.
203 */
c7c7a1a1
TA
204int __vfs_setxattr_noperm(struct user_namespace *mnt_userns,
205 struct dentry *dentry, const char *name,
206 const void *value, size_t size, int flags)
5be196e5
CH
207{
208 struct inode *inode = dentry->d_inode;
4a590153 209 int error = -EAGAIN;
69b45732
AK
210 int issec = !strncmp(name, XATTR_SECURITY_PREFIX,
211 XATTR_SECURITY_PREFIX_LEN);
e0ad7b07 212
69b45732
AK
213 if (issec)
214 inode->i_flags &= ~S_NOSEC;
6c6ef9f2 215 if (inode->i_opflags & IOP_XATTR) {
c7c7a1a1
TA
216 error = __vfs_setxattr(mnt_userns, dentry, inode, name, value,
217 size, flags);
5be196e5
CH
218 if (!error) {
219 fsnotify_xattr(dentry);
220 security_inode_post_setxattr(dentry, name, value,
221 size, flags);
222 }
4a590153 223 } else {
5f6e59ae
AG
224 if (unlikely(is_bad_inode(inode)))
225 return -EIO;
4a590153
AG
226 }
227 if (error == -EAGAIN) {
228 error = -EOPNOTSUPP;
229
230 if (issec) {
231 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
232
233 error = security_inode_setsecurity(inode, suffix, value,
234 size, flags);
235 if (!error)
236 fsnotify_xattr(dentry);
237 }
5be196e5 238 }
b1ab7e4b
DQ
239
240 return error;
241}
242
08b5d501 243/**
da5c1c0b 244 * __vfs_setxattr_locked - set an extended attribute while holding the inode
08b5d501
FL
245 * lock
246 *
6961fed4 247 * @mnt_userns: user namespace of the mount of the target inode
da5c1c0b
RD
248 * @dentry: object to perform setxattr on
249 * @name: xattr name to set
250 * @value: value to set @name to
251 * @size: size of @value
252 * @flags: flags to pass into filesystem operations
253 * @delegated_inode: on return, will contain an inode pointer that
08b5d501
FL
254 * a delegation was broken on, NULL if none.
255 */
b1ab7e4b 256int
c7c7a1a1
TA
257__vfs_setxattr_locked(struct user_namespace *mnt_userns, struct dentry *dentry,
258 const char *name, const void *value, size_t size,
259 int flags, struct inode **delegated_inode)
b1ab7e4b
DQ
260{
261 struct inode *inode = dentry->d_inode;
262 int error;
263
c7c7a1a1 264 error = xattr_permission(mnt_userns, inode, name, MAY_WRITE);
b1ab7e4b
DQ
265 if (error)
266 return error;
267
71bc356f
CB
268 error = security_inode_setxattr(mnt_userns, dentry, name, value, size,
269 flags);
b1ab7e4b
DQ
270 if (error)
271 goto out;
272
08b5d501
FL
273 error = try_break_deleg(inode, delegated_inode);
274 if (error)
275 goto out;
276
c7c7a1a1
TA
277 error = __vfs_setxattr_noperm(mnt_userns, dentry, name, value,
278 size, flags);
b1ab7e4b 279
5be196e5 280out:
08b5d501
FL
281 return error;
282}
283EXPORT_SYMBOL_GPL(__vfs_setxattr_locked);
284
0c5fd887
CB
285static inline bool is_posix_acl_xattr(const char *name)
286{
287 return (strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
288 (strcmp(name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0);
289}
290
08b5d501 291int
c7c7a1a1 292vfs_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
6344e669 293 const char *name, const void *value, size_t size, int flags)
08b5d501
FL
294{
295 struct inode *inode = dentry->d_inode;
296 struct inode *delegated_inode = NULL;
7c03e2cd 297 const void *orig_value = value;
08b5d501
FL
298 int error;
299
7c03e2cd 300 if (size && strcmp(name, XATTR_NAME_CAPS) == 0) {
6344e669 301 error = cap_convert_nscap(mnt_userns, dentry, &value, size);
7c03e2cd
MS
302 if (error < 0)
303 return error;
304 size = error;
305 }
306
08b5d501
FL
307retry_deleg:
308 inode_lock(inode);
c7c7a1a1
TA
309 error = __vfs_setxattr_locked(mnt_userns, dentry, name, value, size,
310 flags, &delegated_inode);
5955102c 311 inode_unlock(inode);
08b5d501
FL
312
313 if (delegated_inode) {
314 error = break_deleg_wait(&delegated_inode);
315 if (!error)
316 goto retry_deleg;
317 }
7c03e2cd
MS
318 if (value != orig_value)
319 kfree(value);
320
5be196e5
CH
321 return error;
322}
323EXPORT_SYMBOL_GPL(vfs_setxattr);
324
2220c5b0 325static ssize_t
71bc356f
CB
326xattr_getsecurity(struct user_namespace *mnt_userns, struct inode *inode,
327 const char *name, void *value, size_t size)
42492594
DQ
328{
329 void *buffer = NULL;
330 ssize_t len;
331
332 if (!value || !size) {
71bc356f
CB
333 len = security_inode_getsecurity(mnt_userns, inode, name,
334 &buffer, false);
42492594
DQ
335 goto out_noalloc;
336 }
337
71bc356f
CB
338 len = security_inode_getsecurity(mnt_userns, inode, name, &buffer,
339 true);
42492594
DQ
340 if (len < 0)
341 return len;
342 if (size < len) {
343 len = -ERANGE;
344 goto out;
345 }
346 memcpy(value, buffer, len);
347out:
57e7ba04 348 kfree(buffer);
42492594
DQ
349out_noalloc:
350 return len;
351}
42492594 352
1601fbad
MZ
353/*
354 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
355 *
356 * Allocate memory, if not already allocated, or re-allocate correct size,
357 * before retrieving the extended attribute.
358 *
359 * Returns the result of alloc, if failed, or the getxattr operation.
360 */
361ssize_t
c7c7a1a1
TA
362vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
363 const char *name, char **xattr_value, size_t xattr_size,
364 gfp_t flags)
1601fbad 365{
6c6ef9f2 366 const struct xattr_handler *handler;
1601fbad
MZ
367 struct inode *inode = dentry->d_inode;
368 char *value = *xattr_value;
369 int error;
370
c7c7a1a1 371 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
1601fbad
MZ
372 if (error)
373 return error;
374
6c6ef9f2
AG
375 handler = xattr_resolve_name(inode, &name);
376 if (IS_ERR(handler))
377 return PTR_ERR(handler);
378 if (!handler->get)
1601fbad 379 return -EOPNOTSUPP;
6c6ef9f2 380 error = handler->get(handler, dentry, inode, name, NULL, 0);
1601fbad
MZ
381 if (error < 0)
382 return error;
383
384 if (!value || (error > xattr_size)) {
385 value = krealloc(*xattr_value, error + 1, flags);
386 if (!value)
387 return -ENOMEM;
388 memset(value, 0, error + 1);
389 }
390
6c6ef9f2 391 error = handler->get(handler, dentry, inode, name, value, error);
1601fbad
MZ
392 *xattr_value = value;
393 return error;
394}
395
5d6c3191
AG
396ssize_t
397__vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
398 void *value, size_t size)
399{
6c6ef9f2
AG
400 const struct xattr_handler *handler;
401
402 handler = xattr_resolve_name(inode, &name);
403 if (IS_ERR(handler))
404 return PTR_ERR(handler);
405 if (!handler->get)
5d6c3191 406 return -EOPNOTSUPP;
6c6ef9f2 407 return handler->get(handler, dentry, inode, name, value, size);
5d6c3191
AG
408}
409EXPORT_SYMBOL(__vfs_getxattr);
410
5be196e5 411ssize_t
c7c7a1a1
TA
412vfs_getxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
413 const char *name, void *value, size_t size)
5be196e5
CH
414{
415 struct inode *inode = dentry->d_inode;
416 int error;
417
c7c7a1a1 418 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
e0ad7b07 419 if (error)
420 return error;
421
5be196e5
CH
422 error = security_inode_getxattr(dentry, name);
423 if (error)
424 return error;
425
5be196e5 426 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 427 XATTR_SECURITY_PREFIX_LEN)) {
428 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
71bc356f
CB
429 int ret = xattr_getsecurity(mnt_userns, inode, suffix, value,
430 size);
5be196e5
CH
431 /*
432 * Only overwrite the return value if a security module
433 * is actually active.
434 */
4bea5805
DQ
435 if (ret == -EOPNOTSUPP)
436 goto nolsm;
437 return ret;
5be196e5 438 }
4bea5805 439nolsm:
0c5fd887
CB
440 error = __vfs_getxattr(dentry, inode, name, value, size);
441 if (error > 0 && is_posix_acl_xattr(name))
442 posix_acl_getxattr_idmapped_mnt(mnt_userns, inode, value, size);
443 return error;
5be196e5
CH
444}
445EXPORT_SYMBOL_GPL(vfs_getxattr);
446
659564c8 447ssize_t
bf3ee713 448vfs_listxattr(struct dentry *dentry, char *list, size_t size)
659564c8 449{
bf3ee713 450 struct inode *inode = d_inode(dentry);
659564c8
BN
451 ssize_t error;
452
bf3ee713 453 error = security_inode_listxattr(dentry);
659564c8
BN
454 if (error)
455 return error;
bf3ee713 456 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
bf3ee713 457 error = inode->i_op->listxattr(dentry, list, size);
659564c8 458 } else {
bf3ee713 459 error = security_inode_listsecurity(inode, list, size);
659564c8
BN
460 if (size && error > size)
461 error = -ERANGE;
462 }
463 return error;
464}
465EXPORT_SYMBOL_GPL(vfs_listxattr);
466
5be196e5 467int
c7c7a1a1
TA
468__vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
469 const char *name)
5be196e5 470{
6c6ef9f2
AG
471 struct inode *inode = d_inode(dentry);
472 const struct xattr_handler *handler;
5be196e5 473
6c6ef9f2
AG
474 handler = xattr_resolve_name(inode, &name);
475 if (IS_ERR(handler))
476 return PTR_ERR(handler);
477 if (!handler->set)
5be196e5 478 return -EOPNOTSUPP;
c7c7a1a1
TA
479 return handler->set(handler, mnt_userns, dentry, inode, name, NULL, 0,
480 XATTR_REPLACE);
5d6c3191
AG
481}
482EXPORT_SYMBOL(__vfs_removexattr);
483
08b5d501 484/**
da5c1c0b 485 * __vfs_removexattr_locked - set an extended attribute while holding the inode
08b5d501
FL
486 * lock
487 *
6961fed4 488 * @mnt_userns: user namespace of the mount of the target inode
da5c1c0b
RD
489 * @dentry: object to perform setxattr on
490 * @name: name of xattr to remove
491 * @delegated_inode: on return, will contain an inode pointer that
08b5d501
FL
492 * a delegation was broken on, NULL if none.
493 */
5d6c3191 494int
c7c7a1a1
TA
495__vfs_removexattr_locked(struct user_namespace *mnt_userns,
496 struct dentry *dentry, const char *name,
497 struct inode **delegated_inode)
5d6c3191
AG
498{
499 struct inode *inode = dentry->d_inode;
500 int error;
5be196e5 501
c7c7a1a1 502 error = xattr_permission(mnt_userns, inode, name, MAY_WRITE);
e0ad7b07 503 if (error)
504 return error;
505
71bc356f 506 error = security_inode_removexattr(mnt_userns, dentry, name);
7c51bb00
DK
507 if (error)
508 goto out;
5be196e5 509
08b5d501
FL
510 error = try_break_deleg(inode, delegated_inode);
511 if (error)
512 goto out;
513
c7c7a1a1 514 error = __vfs_removexattr(mnt_userns, dentry, name);
5be196e5 515
c7b87de2 516 if (!error) {
5be196e5 517 fsnotify_xattr(dentry);
c7b87de2
MZ
518 evm_inode_post_removexattr(dentry, name);
519 }
7c51bb00
DK
520
521out:
08b5d501
FL
522 return error;
523}
524EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
525
526int
c7c7a1a1
TA
527vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
528 const char *name)
08b5d501
FL
529{
530 struct inode *inode = dentry->d_inode;
531 struct inode *delegated_inode = NULL;
532 int error;
533
534retry_deleg:
535 inode_lock(inode);
c7c7a1a1
TA
536 error = __vfs_removexattr_locked(mnt_userns, dentry,
537 name, &delegated_inode);
5955102c 538 inode_unlock(inode);
08b5d501
FL
539
540 if (delegated_inode) {
541 error = break_deleg_wait(&delegated_inode);
542 if (!error)
543 goto retry_deleg;
544 }
545
5be196e5
CH
546 return error;
547}
548EXPORT_SYMBOL_GPL(vfs_removexattr);
549
1da177e4
LT
550/*
551 * Extended attribute SET operations
552 */
1a91794c
SR
553
554int setxattr_copy(const char __user *name, struct xattr_ctx *ctx)
1da177e4
LT
555{
556 int error;
1da177e4 557
1a91794c 558 if (ctx->flags & ~(XATTR_CREATE|XATTR_REPLACE))
1da177e4
LT
559 return -EINVAL;
560
1a91794c
SR
561 error = strncpy_from_user(ctx->kname->name, name,
562 sizeof(ctx->kname->name));
563 if (error == 0 || error == sizeof(ctx->kname->name))
564 return -ERANGE;
1da177e4
LT
565 if (error < 0)
566 return error;
567
1a91794c
SR
568 error = 0;
569 if (ctx->size) {
570 if (ctx->size > XATTR_SIZE_MAX)
1da177e4 571 return -E2BIG;
1a91794c
SR
572
573 ctx->kvalue = vmemdup_user(ctx->cvalue, ctx->size);
574 if (IS_ERR(ctx->kvalue)) {
575 error = PTR_ERR(ctx->kvalue);
576 ctx->kvalue = NULL;
44c82498 577 }
1da177e4
LT
578 }
579
1a91794c
SR
580 return error;
581}
582
583static void setxattr_convert(struct user_namespace *mnt_userns,
584 struct dentry *d, struct xattr_ctx *ctx)
585{
38e31639 586 if (ctx->size && is_posix_acl_xattr(ctx->kname->name))
0c5fd887 587 posix_acl_fix_xattr_from_user(ctx->kvalue, ctx->size);
1a91794c
SR
588}
589
590int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
591 struct xattr_ctx *ctx)
592{
593 setxattr_convert(mnt_userns, dentry, ctx);
594 return vfs_setxattr(mnt_userns, dentry, ctx->kname->name,
595 ctx->kvalue, ctx->size, ctx->flags);
596}
597
598static long
599setxattr(struct user_namespace *mnt_userns, struct dentry *d,
600 const char __user *name, const void __user *value, size_t size,
601 int flags)
602{
603 struct xattr_name kname;
604 struct xattr_ctx ctx = {
605 .cvalue = value,
606 .kvalue = NULL,
607 .size = size,
608 .kname = &kname,
609 .flags = flags,
610 };
611 int error;
612
613 error = setxattr_copy(name, &ctx);
614 if (error)
615 return error;
616
617 error = do_setxattr(mnt_userns, d, &ctx);
0b2a6f23 618
1a91794c 619 kvfree(ctx.kvalue);
1da177e4
LT
620 return error;
621}
622
8cc43116
EB
623static int path_setxattr(const char __user *pathname,
624 const char __user *name, const void __user *value,
625 size_t size, int flags, unsigned int lookup_flags)
1da177e4 626{
2d8f3038 627 struct path path;
1da177e4 628 int error;
c7c7a1a1 629
68f1bb8b
JL
630retry:
631 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
632 if (error)
633 return error;
2d8f3038 634 error = mnt_want_write(path.mnt);
18f335af 635 if (!error) {
c7c7a1a1
TA
636 error = setxattr(mnt_user_ns(path.mnt), path.dentry, name,
637 value, size, flags);
2d8f3038 638 mnt_drop_write(path.mnt);
18f335af 639 }
2d8f3038 640 path_put(&path);
68f1bb8b
JL
641 if (retry_estale(error, lookup_flags)) {
642 lookup_flags |= LOOKUP_REVAL;
643 goto retry;
644 }
1da177e4
LT
645 return error;
646}
647
8cc43116
EB
648SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
649 const char __user *, name, const void __user *, value,
650 size_t, size, int, flags)
651{
652 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
653}
654
64fd1de3
HC
655SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
656 const char __user *, name, const void __user *, value,
657 size_t, size, int, flags)
1da177e4 658{
8cc43116 659 return path_setxattr(pathname, name, value, size, flags, 0);
1da177e4
LT
660}
661
64fd1de3
HC
662SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
663 const void __user *,value, size_t, size, int, flags)
1da177e4 664{
2903ff01 665 struct fd f = fdget(fd);
1da177e4
LT
666 int error = -EBADF;
667
2903ff01 668 if (!f.file)
1da177e4 669 return error;
9f45f5bf 670 audit_file(f.file);
6742cee0 671 error = mnt_want_write_file(f.file);
18f335af 672 if (!error) {
c7c7a1a1
TA
673 error = setxattr(file_mnt_user_ns(f.file),
674 f.file->f_path.dentry, name,
675 value, size, flags);
6742cee0 676 mnt_drop_write_file(f.file);
18f335af 677 }
2903ff01 678 fdput(f);
1da177e4
LT
679 return error;
680}
681
682/*
683 * Extended attribute GET operations
684 */
c975cad9
SR
685ssize_t
686do_getxattr(struct user_namespace *mnt_userns, struct dentry *d,
687 struct xattr_ctx *ctx)
1da177e4
LT
688{
689 ssize_t error;
c975cad9 690 char *kname = ctx->kname->name;
1da177e4 691
c975cad9
SR
692 if (ctx->size) {
693 if (ctx->size > XATTR_SIZE_MAX)
694 ctx->size = XATTR_SIZE_MAX;
695 ctx->kvalue = kvzalloc(ctx->size, GFP_KERNEL);
696 if (!ctx->kvalue)
752ade68 697 return -ENOMEM;
1da177e4
LT
698 }
699
c975cad9 700 error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
f549d6c1 701 if (error > 0) {
38e31639 702 if (is_posix_acl_xattr(kname))
0c5fd887 703 posix_acl_fix_xattr_to_user(ctx->kvalue, error);
c975cad9 704 if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
f549d6c1 705 error = -EFAULT;
c975cad9 706 } else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
f549d6c1
SS
707 /* The file system tried to returned a value bigger
708 than XATTR_SIZE_MAX bytes. Not possible. */
709 error = -E2BIG;
1da177e4 710 }
0b2a6f23 711
c975cad9
SR
712 return error;
713}
714
715static ssize_t
716getxattr(struct user_namespace *mnt_userns, struct dentry *d,
717 const char __user *name, void __user *value, size_t size)
718{
719 ssize_t error;
720 struct xattr_name kname;
721 struct xattr_ctx ctx = {
722 .value = value,
723 .kvalue = NULL,
724 .size = size,
725 .kname = &kname,
726 .flags = 0,
727 };
728
729 error = strncpy_from_user(kname.name, name, sizeof(kname.name));
730 if (error == 0 || error == sizeof(kname.name))
731 error = -ERANGE;
732 if (error < 0)
733 return error;
734
735 error = do_getxattr(mnt_userns, d, &ctx);
0b2a6f23 736
c975cad9 737 kvfree(ctx.kvalue);
1da177e4
LT
738 return error;
739}
740
8cc43116
EB
741static ssize_t path_getxattr(const char __user *pathname,
742 const char __user *name, void __user *value,
743 size_t size, unsigned int lookup_flags)
1da177e4 744{
2d8f3038 745 struct path path;
1da177e4 746 ssize_t error;
60e66b48
JL
747retry:
748 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
749 if (error)
750 return error;
c7c7a1a1 751 error = getxattr(mnt_user_ns(path.mnt), path.dentry, name, value, size);
2d8f3038 752 path_put(&path);
60e66b48
JL
753 if (retry_estale(error, lookup_flags)) {
754 lookup_flags |= LOOKUP_REVAL;
755 goto retry;
756 }
1da177e4
LT
757 return error;
758}
759
8cc43116
EB
760SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
761 const char __user *, name, void __user *, value, size_t, size)
762{
763 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
764}
765
64fd1de3
HC
766SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
767 const char __user *, name, void __user *, value, size_t, size)
1da177e4 768{
8cc43116 769 return path_getxattr(pathname, name, value, size, 0);
1da177e4
LT
770}
771
64fd1de3
HC
772SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
773 void __user *, value, size_t, size)
1da177e4 774{
2903ff01 775 struct fd f = fdget(fd);
1da177e4
LT
776 ssize_t error = -EBADF;
777
2903ff01 778 if (!f.file)
1da177e4 779 return error;
9f45f5bf 780 audit_file(f.file);
c7c7a1a1
TA
781 error = getxattr(file_mnt_user_ns(f.file), f.file->f_path.dentry,
782 name, value, size);
2903ff01 783 fdput(f);
1da177e4
LT
784 return error;
785}
786
787/*
788 * Extended attribute LIST operations
789 */
790static ssize_t
791listxattr(struct dentry *d, char __user *list, size_t size)
792{
793 ssize_t error;
794 char *klist = NULL;
795
796 if (size) {
797 if (size > XATTR_LIST_MAX)
798 size = XATTR_LIST_MAX;
752ade68
MH
799 klist = kvmalloc(size, GFP_KERNEL);
800 if (!klist)
801 return -ENOMEM;
1da177e4
LT
802 }
803
659564c8 804 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
805 if (error > 0) {
806 if (size && copy_to_user(list, klist, error))
807 error = -EFAULT;
808 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
809 /* The file system tried to returned a list bigger
810 than XATTR_LIST_MAX bytes. Not possible. */
811 error = -E2BIG;
1da177e4 812 }
0b2a6f23
RW
813
814 kvfree(klist);
815
1da177e4
LT
816 return error;
817}
818
8cc43116
EB
819static ssize_t path_listxattr(const char __user *pathname, char __user *list,
820 size_t size, unsigned int lookup_flags)
1da177e4 821{
2d8f3038 822 struct path path;
1da177e4 823 ssize_t error;
10a90cf3
JL
824retry:
825 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
826 if (error)
827 return error;
2d8f3038
AV
828 error = listxattr(path.dentry, list, size);
829 path_put(&path);
10a90cf3
JL
830 if (retry_estale(error, lookup_flags)) {
831 lookup_flags |= LOOKUP_REVAL;
832 goto retry;
833 }
1da177e4
LT
834 return error;
835}
836
8cc43116
EB
837SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
838 size_t, size)
839{
840 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
841}
842
64fd1de3
HC
843SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
844 size_t, size)
1da177e4 845{
8cc43116 846 return path_listxattr(pathname, list, size, 0);
1da177e4
LT
847}
848
64fd1de3 849SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4 850{
2903ff01 851 struct fd f = fdget(fd);
1da177e4
LT
852 ssize_t error = -EBADF;
853
2903ff01 854 if (!f.file)
1da177e4 855 return error;
9f45f5bf 856 audit_file(f.file);
2903ff01
AV
857 error = listxattr(f.file->f_path.dentry, list, size);
858 fdput(f);
1da177e4
LT
859 return error;
860}
861
862/*
863 * Extended attribute REMOVE operations
864 */
865static long
c7c7a1a1
TA
866removexattr(struct user_namespace *mnt_userns, struct dentry *d,
867 const char __user *name)
1da177e4
LT
868{
869 int error;
870 char kname[XATTR_NAME_MAX + 1];
871
872 error = strncpy_from_user(kname, name, sizeof(kname));
873 if (error == 0 || error == sizeof(kname))
874 error = -ERANGE;
875 if (error < 0)
876 return error;
877
c7c7a1a1 878 return vfs_removexattr(mnt_userns, d, kname);
1da177e4
LT
879}
880
8cc43116
EB
881static int path_removexattr(const char __user *pathname,
882 const char __user *name, unsigned int lookup_flags)
1da177e4 883{
2d8f3038 884 struct path path;
1da177e4 885 int error;
12f06212
JL
886retry:
887 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
888 if (error)
889 return error;
2d8f3038 890 error = mnt_want_write(path.mnt);
18f335af 891 if (!error) {
c7c7a1a1 892 error = removexattr(mnt_user_ns(path.mnt), path.dentry, name);
2d8f3038 893 mnt_drop_write(path.mnt);
18f335af 894 }
2d8f3038 895 path_put(&path);
12f06212
JL
896 if (retry_estale(error, lookup_flags)) {
897 lookup_flags |= LOOKUP_REVAL;
898 goto retry;
899 }
1da177e4
LT
900 return error;
901}
902
8cc43116
EB
903SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
904 const char __user *, name)
905{
906 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
907}
908
6a6160a7
HC
909SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
910 const char __user *, name)
1da177e4 911{
8cc43116 912 return path_removexattr(pathname, name, 0);
1da177e4
LT
913}
914
6a6160a7 915SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4 916{
2903ff01 917 struct fd f = fdget(fd);
1da177e4
LT
918 int error = -EBADF;
919
2903ff01 920 if (!f.file)
1da177e4 921 return error;
9f45f5bf 922 audit_file(f.file);
6742cee0 923 error = mnt_want_write_file(f.file);
18f335af 924 if (!error) {
c7c7a1a1
TA
925 error = removexattr(file_mnt_user_ns(f.file),
926 f.file->f_path.dentry, name);
6742cee0 927 mnt_drop_write_file(f.file);
18f335af 928 }
2903ff01 929 fdput(f);
1da177e4
LT
930 return error;
931}
932
1da177e4
LT
933/*
934 * Combine the results of the list() operation from every xattr_handler in the
935 * list.
936 */
937ssize_t
938generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
939{
bb435453 940 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
941 unsigned int size = 0;
942
943 if (!buffer) {
431547b3 944 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
945 if (!handler->name ||
946 (handler->list && !handler->list(dentry)))
c4803c49 947 continue;
764a5c6b 948 size += strlen(handler->name) + 1;
431547b3 949 }
1da177e4
LT
950 } else {
951 char *buf = buffer;
764a5c6b 952 size_t len;
1da177e4
LT
953
954 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
955 if (!handler->name ||
956 (handler->list && !handler->list(dentry)))
c4803c49 957 continue;
764a5c6b
AG
958 len = strlen(handler->name);
959 if (len + 1 > buffer_size)
1da177e4 960 return -ERANGE;
764a5c6b
AG
961 memcpy(buf, handler->name, len + 1);
962 buf += len + 1;
963 buffer_size -= len + 1;
1da177e4
LT
964 }
965 size = buf - buffer;
966 }
967 return size;
968}
1da177e4 969EXPORT_SYMBOL(generic_listxattr);
38f38657 970
e409de99
AG
971/**
972 * xattr_full_name - Compute full attribute name from suffix
973 *
974 * @handler: handler of the xattr_handler operation
975 * @name: name passed to the xattr_handler operation
976 *
977 * The get and set xattr handler operations are called with the remainder of
978 * the attribute name after skipping the handler's prefix: for example, "foo"
979 * is passed to the get operation of a handler with prefix "user." to get
980 * attribute "user.foo". The full name is still "there" in the name though.
981 *
982 * Note: the list xattr handler operation when called from the vfs is passed a
983 * NULL name; some file systems use this operation internally, with varying
984 * semantics.
985 */
986const char *xattr_full_name(const struct xattr_handler *handler,
987 const char *name)
988{
98e9cb57 989 size_t prefix_len = strlen(xattr_prefix(handler));
e409de99
AG
990
991 return name - prefix_len;
992}
993EXPORT_SYMBOL(xattr_full_name);
994
38f38657
AR
995/*
996 * Allocate new xattr and copy in the value; but leave the name to callers.
997 */
998struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
999{
1000 struct simple_xattr *new_xattr;
1001 size_t len;
1002
1003 /* wrap around? */
1004 len = sizeof(*new_xattr) + size;
4e66d445 1005 if (len < sizeof(*new_xattr))
38f38657
AR
1006 return NULL;
1007
fdc85222 1008 new_xattr = kvmalloc(len, GFP_KERNEL);
38f38657
AR
1009 if (!new_xattr)
1010 return NULL;
1011
1012 new_xattr->size = size;
1013 memcpy(new_xattr->value, value, size);
1014 return new_xattr;
1015}
1016
1017/*
1018 * xattr GET operation for in-memory/pseudo filesystems
1019 */
1020int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
1021 void *buffer, size_t size)
1022{
1023 struct simple_xattr *xattr;
1024 int ret = -ENODATA;
1025
1026 spin_lock(&xattrs->lock);
1027 list_for_each_entry(xattr, &xattrs->head, list) {
1028 if (strcmp(name, xattr->name))
1029 continue;
1030
1031 ret = xattr->size;
1032 if (buffer) {
1033 if (size < xattr->size)
1034 ret = -ERANGE;
1035 else
1036 memcpy(buffer, xattr->value, xattr->size);
1037 }
1038 break;
1039 }
1040 spin_unlock(&xattrs->lock);
1041 return ret;
1042}
1043
aa7c5241
AG
1044/**
1045 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
1046 * @xattrs: target simple_xattr list
1047 * @name: name of the extended attribute
1048 * @value: value of the xattr. If %NULL, will remove the attribute.
1049 * @size: size of the new xattr
1050 * @flags: %XATTR_{CREATE|REPLACE}
a46a2295 1051 * @removed_size: returns size of the removed xattr, -1 if none removed
aa7c5241
AG
1052 *
1053 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
1054 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
1055 * otherwise, fails with -ENODATA.
1056 *
1057 * Returns 0 on success, -errno on failure.
1058 */
1059int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
a46a2295
DX
1060 const void *value, size_t size, int flags,
1061 ssize_t *removed_size)
38f38657
AR
1062{
1063 struct simple_xattr *xattr;
43385846 1064 struct simple_xattr *new_xattr = NULL;
38f38657
AR
1065 int err = 0;
1066
772b3140
DX
1067 if (removed_size)
1068 *removed_size = -1;
1069
38f38657
AR
1070 /* value == NULL means remove */
1071 if (value) {
1072 new_xattr = simple_xattr_alloc(value, size);
1073 if (!new_xattr)
1074 return -ENOMEM;
1075
1076 new_xattr->name = kstrdup(name, GFP_KERNEL);
1077 if (!new_xattr->name) {
fdc85222 1078 kvfree(new_xattr);
38f38657
AR
1079 return -ENOMEM;
1080 }
1081 }
1082
1083 spin_lock(&xattrs->lock);
1084 list_for_each_entry(xattr, &xattrs->head, list) {
1085 if (!strcmp(name, xattr->name)) {
1086 if (flags & XATTR_CREATE) {
1087 xattr = new_xattr;
1088 err = -EEXIST;
1089 } else if (new_xattr) {
1090 list_replace(&xattr->list, &new_xattr->list);
a46a2295
DX
1091 if (removed_size)
1092 *removed_size = xattr->size;
38f38657
AR
1093 } else {
1094 list_del(&xattr->list);
a46a2295
DX
1095 if (removed_size)
1096 *removed_size = xattr->size;
38f38657
AR
1097 }
1098 goto out;
1099 }
1100 }
1101 if (flags & XATTR_REPLACE) {
1102 xattr = new_xattr;
1103 err = -ENODATA;
1104 } else {
1105 list_add(&new_xattr->list, &xattrs->head);
1106 xattr = NULL;
1107 }
1108out:
1109 spin_unlock(&xattrs->lock);
1110 if (xattr) {
1111 kfree(xattr->name);
fdc85222 1112 kvfree(xattr);
38f38657
AR
1113 }
1114 return err;
1115
1116}
1117
38f38657
AR
1118static bool xattr_is_trusted(const char *name)
1119{
1120 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1121}
1122
786534b9
AG
1123static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1124 const char *name)
1125{
1126 size_t len = strlen(name) + 1;
1127 if (*buffer) {
1128 if (*remaining_size < len)
1129 return -ERANGE;
1130 memcpy(*buffer, name, len);
1131 *buffer += len;
1132 }
1133 *remaining_size -= len;
1134 return 0;
1135}
1136
38f38657
AR
1137/*
1138 * xattr LIST operation for in-memory/pseudo filesystems
1139 */
786534b9
AG
1140ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1141 char *buffer, size_t size)
38f38657
AR
1142{
1143 bool trusted = capable(CAP_SYS_ADMIN);
1144 struct simple_xattr *xattr;
786534b9 1145 ssize_t remaining_size = size;
0e9a7da5 1146 int err = 0;
786534b9
AG
1147
1148#ifdef CONFIG_FS_POSIX_ACL
ffc4c922
AG
1149 if (IS_POSIXACL(inode)) {
1150 if (inode->i_acl) {
1151 err = xattr_list_one(&buffer, &remaining_size,
1152 XATTR_NAME_POSIX_ACL_ACCESS);
1153 if (err)
1154 return err;
1155 }
1156 if (inode->i_default_acl) {
1157 err = xattr_list_one(&buffer, &remaining_size,
1158 XATTR_NAME_POSIX_ACL_DEFAULT);
1159 if (err)
1160 return err;
1161 }
786534b9
AG
1162 }
1163#endif
38f38657
AR
1164
1165 spin_lock(&xattrs->lock);
1166 list_for_each_entry(xattr, &xattrs->head, list) {
38f38657
AR
1167 /* skip "trusted." attributes for unprivileged callers */
1168 if (!trusted && xattr_is_trusted(xattr->name))
1169 continue;
1170
786534b9
AG
1171 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1172 if (err)
0e9a7da5 1173 break;
38f38657
AR
1174 }
1175 spin_unlock(&xattrs->lock);
1176
0e9a7da5 1177 return err ? err : size - remaining_size;
38f38657
AR
1178}
1179
4895768b
AR
1180/*
1181 * Adds an extended attribute to the list
1182 */
38f38657
AR
1183void simple_xattr_list_add(struct simple_xattrs *xattrs,
1184 struct simple_xattr *new_xattr)
1185{
1186 spin_lock(&xattrs->lock);
1187 list_add(&new_xattr->list, &xattrs->head);
1188 spin_unlock(&xattrs->lock);
1189}