]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xattr.c
Merge branch 'for-6.0/dax' into libnvdimm-fixes
[people/ms/linux.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,
0c5fd887 293 const char *name, 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) {
0c5fd887
CB
301 error = cap_convert_nscap(mnt_userns, dentry,
302 (const void **)&value, size);
7c03e2cd
MS
303 if (error < 0)
304 return error;
305 size = error;
306 }
307
0c5fd887
CB
308 if (size && is_posix_acl_xattr(name))
309 posix_acl_setxattr_idmapped_mnt(mnt_userns, inode, value, size);
310
08b5d501
FL
311retry_deleg:
312 inode_lock(inode);
c7c7a1a1
TA
313 error = __vfs_setxattr_locked(mnt_userns, dentry, name, value, size,
314 flags, &delegated_inode);
5955102c 315 inode_unlock(inode);
08b5d501
FL
316
317 if (delegated_inode) {
318 error = break_deleg_wait(&delegated_inode);
319 if (!error)
320 goto retry_deleg;
321 }
7c03e2cd
MS
322 if (value != orig_value)
323 kfree(value);
324
5be196e5
CH
325 return error;
326}
327EXPORT_SYMBOL_GPL(vfs_setxattr);
328
2220c5b0 329static ssize_t
71bc356f
CB
330xattr_getsecurity(struct user_namespace *mnt_userns, struct inode *inode,
331 const char *name, void *value, size_t size)
42492594
DQ
332{
333 void *buffer = NULL;
334 ssize_t len;
335
336 if (!value || !size) {
71bc356f
CB
337 len = security_inode_getsecurity(mnt_userns, inode, name,
338 &buffer, false);
42492594
DQ
339 goto out_noalloc;
340 }
341
71bc356f
CB
342 len = security_inode_getsecurity(mnt_userns, inode, name, &buffer,
343 true);
42492594
DQ
344 if (len < 0)
345 return len;
346 if (size < len) {
347 len = -ERANGE;
348 goto out;
349 }
350 memcpy(value, buffer, len);
351out:
57e7ba04 352 kfree(buffer);
42492594
DQ
353out_noalloc:
354 return len;
355}
42492594 356
1601fbad
MZ
357/*
358 * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr
359 *
360 * Allocate memory, if not already allocated, or re-allocate correct size,
361 * before retrieving the extended attribute.
362 *
363 * Returns the result of alloc, if failed, or the getxattr operation.
364 */
365ssize_t
c7c7a1a1
TA
366vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
367 const char *name, char **xattr_value, size_t xattr_size,
368 gfp_t flags)
1601fbad 369{
6c6ef9f2 370 const struct xattr_handler *handler;
1601fbad
MZ
371 struct inode *inode = dentry->d_inode;
372 char *value = *xattr_value;
373 int error;
374
c7c7a1a1 375 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
1601fbad
MZ
376 if (error)
377 return error;
378
6c6ef9f2
AG
379 handler = xattr_resolve_name(inode, &name);
380 if (IS_ERR(handler))
381 return PTR_ERR(handler);
382 if (!handler->get)
1601fbad 383 return -EOPNOTSUPP;
6c6ef9f2 384 error = handler->get(handler, dentry, inode, name, NULL, 0);
1601fbad
MZ
385 if (error < 0)
386 return error;
387
388 if (!value || (error > xattr_size)) {
389 value = krealloc(*xattr_value, error + 1, flags);
390 if (!value)
391 return -ENOMEM;
392 memset(value, 0, error + 1);
393 }
394
6c6ef9f2 395 error = handler->get(handler, dentry, inode, name, value, error);
1601fbad
MZ
396 *xattr_value = value;
397 return error;
398}
399
5d6c3191
AG
400ssize_t
401__vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
402 void *value, size_t size)
403{
6c6ef9f2
AG
404 const struct xattr_handler *handler;
405
406 handler = xattr_resolve_name(inode, &name);
407 if (IS_ERR(handler))
408 return PTR_ERR(handler);
409 if (!handler->get)
5d6c3191 410 return -EOPNOTSUPP;
6c6ef9f2 411 return handler->get(handler, dentry, inode, name, value, size);
5d6c3191
AG
412}
413EXPORT_SYMBOL(__vfs_getxattr);
414
5be196e5 415ssize_t
c7c7a1a1
TA
416vfs_getxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
417 const char *name, void *value, size_t size)
5be196e5
CH
418{
419 struct inode *inode = dentry->d_inode;
420 int error;
421
c7c7a1a1 422 error = xattr_permission(mnt_userns, inode, name, MAY_READ);
e0ad7b07 423 if (error)
424 return error;
425
5be196e5
CH
426 error = security_inode_getxattr(dentry, name);
427 if (error)
428 return error;
429
5be196e5 430 if (!strncmp(name, XATTR_SECURITY_PREFIX,
e0ad7b07 431 XATTR_SECURITY_PREFIX_LEN)) {
432 const char *suffix = name + XATTR_SECURITY_PREFIX_LEN;
71bc356f
CB
433 int ret = xattr_getsecurity(mnt_userns, inode, suffix, value,
434 size);
5be196e5
CH
435 /*
436 * Only overwrite the return value if a security module
437 * is actually active.
438 */
4bea5805
DQ
439 if (ret == -EOPNOTSUPP)
440 goto nolsm;
441 return ret;
5be196e5 442 }
4bea5805 443nolsm:
0c5fd887
CB
444 error = __vfs_getxattr(dentry, inode, name, value, size);
445 if (error > 0 && is_posix_acl_xattr(name))
446 posix_acl_getxattr_idmapped_mnt(mnt_userns, inode, value, size);
447 return error;
5be196e5
CH
448}
449EXPORT_SYMBOL_GPL(vfs_getxattr);
450
659564c8 451ssize_t
bf3ee713 452vfs_listxattr(struct dentry *dentry, char *list, size_t size)
659564c8 453{
bf3ee713 454 struct inode *inode = d_inode(dentry);
659564c8
BN
455 ssize_t error;
456
bf3ee713 457 error = security_inode_listxattr(dentry);
659564c8
BN
458 if (error)
459 return error;
bf3ee713 460 if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) {
bf3ee713 461 error = inode->i_op->listxattr(dentry, list, size);
659564c8 462 } else {
bf3ee713 463 error = security_inode_listsecurity(inode, list, size);
659564c8
BN
464 if (size && error > size)
465 error = -ERANGE;
466 }
467 return error;
468}
469EXPORT_SYMBOL_GPL(vfs_listxattr);
470
5be196e5 471int
c7c7a1a1
TA
472__vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
473 const char *name)
5be196e5 474{
6c6ef9f2
AG
475 struct inode *inode = d_inode(dentry);
476 const struct xattr_handler *handler;
5be196e5 477
6c6ef9f2
AG
478 handler = xattr_resolve_name(inode, &name);
479 if (IS_ERR(handler))
480 return PTR_ERR(handler);
481 if (!handler->set)
5be196e5 482 return -EOPNOTSUPP;
c7c7a1a1
TA
483 return handler->set(handler, mnt_userns, dentry, inode, name, NULL, 0,
484 XATTR_REPLACE);
5d6c3191
AG
485}
486EXPORT_SYMBOL(__vfs_removexattr);
487
08b5d501 488/**
da5c1c0b 489 * __vfs_removexattr_locked - set an extended attribute while holding the inode
08b5d501
FL
490 * lock
491 *
6961fed4 492 * @mnt_userns: user namespace of the mount of the target inode
da5c1c0b
RD
493 * @dentry: object to perform setxattr on
494 * @name: name of xattr to remove
495 * @delegated_inode: on return, will contain an inode pointer that
08b5d501
FL
496 * a delegation was broken on, NULL if none.
497 */
5d6c3191 498int
c7c7a1a1
TA
499__vfs_removexattr_locked(struct user_namespace *mnt_userns,
500 struct dentry *dentry, const char *name,
501 struct inode **delegated_inode)
5d6c3191
AG
502{
503 struct inode *inode = dentry->d_inode;
504 int error;
5be196e5 505
c7c7a1a1 506 error = xattr_permission(mnt_userns, inode, name, MAY_WRITE);
e0ad7b07 507 if (error)
508 return error;
509
71bc356f 510 error = security_inode_removexattr(mnt_userns, dentry, name);
7c51bb00
DK
511 if (error)
512 goto out;
5be196e5 513
08b5d501
FL
514 error = try_break_deleg(inode, delegated_inode);
515 if (error)
516 goto out;
517
c7c7a1a1 518 error = __vfs_removexattr(mnt_userns, dentry, name);
5be196e5 519
c7b87de2 520 if (!error) {
5be196e5 521 fsnotify_xattr(dentry);
c7b87de2
MZ
522 evm_inode_post_removexattr(dentry, name);
523 }
7c51bb00
DK
524
525out:
08b5d501
FL
526 return error;
527}
528EXPORT_SYMBOL_GPL(__vfs_removexattr_locked);
529
530int
c7c7a1a1
TA
531vfs_removexattr(struct user_namespace *mnt_userns, struct dentry *dentry,
532 const char *name)
08b5d501
FL
533{
534 struct inode *inode = dentry->d_inode;
535 struct inode *delegated_inode = NULL;
536 int error;
537
538retry_deleg:
539 inode_lock(inode);
c7c7a1a1
TA
540 error = __vfs_removexattr_locked(mnt_userns, dentry,
541 name, &delegated_inode);
5955102c 542 inode_unlock(inode);
08b5d501
FL
543
544 if (delegated_inode) {
545 error = break_deleg_wait(&delegated_inode);
546 if (!error)
547 goto retry_deleg;
548 }
549
5be196e5
CH
550 return error;
551}
552EXPORT_SYMBOL_GPL(vfs_removexattr);
553
1da177e4
LT
554/*
555 * Extended attribute SET operations
556 */
1a91794c
SR
557
558int setxattr_copy(const char __user *name, struct xattr_ctx *ctx)
1da177e4
LT
559{
560 int error;
1da177e4 561
1a91794c 562 if (ctx->flags & ~(XATTR_CREATE|XATTR_REPLACE))
1da177e4
LT
563 return -EINVAL;
564
1a91794c
SR
565 error = strncpy_from_user(ctx->kname->name, name,
566 sizeof(ctx->kname->name));
567 if (error == 0 || error == sizeof(ctx->kname->name))
568 return -ERANGE;
1da177e4
LT
569 if (error < 0)
570 return error;
571
1a91794c
SR
572 error = 0;
573 if (ctx->size) {
574 if (ctx->size > XATTR_SIZE_MAX)
1da177e4 575 return -E2BIG;
1a91794c
SR
576
577 ctx->kvalue = vmemdup_user(ctx->cvalue, ctx->size);
578 if (IS_ERR(ctx->kvalue)) {
579 error = PTR_ERR(ctx->kvalue);
580 ctx->kvalue = NULL;
44c82498 581 }
1da177e4
LT
582 }
583
1a91794c
SR
584 return error;
585}
586
587static void setxattr_convert(struct user_namespace *mnt_userns,
588 struct dentry *d, struct xattr_ctx *ctx)
589{
590 if (ctx->size &&
591 ((strcmp(ctx->kname->name, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
592 (strcmp(ctx->kname->name, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)))
0c5fd887 593 posix_acl_fix_xattr_from_user(ctx->kvalue, ctx->size);
1a91794c
SR
594}
595
596int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
597 struct xattr_ctx *ctx)
598{
599 setxattr_convert(mnt_userns, dentry, ctx);
600 return vfs_setxattr(mnt_userns, dentry, ctx->kname->name,
601 ctx->kvalue, ctx->size, ctx->flags);
602}
603
604static long
605setxattr(struct user_namespace *mnt_userns, struct dentry *d,
606 const char __user *name, const void __user *value, size_t size,
607 int flags)
608{
609 struct xattr_name kname;
610 struct xattr_ctx ctx = {
611 .cvalue = value,
612 .kvalue = NULL,
613 .size = size,
614 .kname = &kname,
615 .flags = flags,
616 };
617 int error;
618
619 error = setxattr_copy(name, &ctx);
620 if (error)
621 return error;
622
623 error = do_setxattr(mnt_userns, d, &ctx);
0b2a6f23 624
1a91794c 625 kvfree(ctx.kvalue);
1da177e4
LT
626 return error;
627}
628
8cc43116
EB
629static int path_setxattr(const char __user *pathname,
630 const char __user *name, const void __user *value,
631 size_t size, int flags, unsigned int lookup_flags)
1da177e4 632{
2d8f3038 633 struct path path;
1da177e4 634 int error;
c7c7a1a1 635
68f1bb8b
JL
636retry:
637 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
638 if (error)
639 return error;
2d8f3038 640 error = mnt_want_write(path.mnt);
18f335af 641 if (!error) {
c7c7a1a1
TA
642 error = setxattr(mnt_user_ns(path.mnt), path.dentry, name,
643 value, size, flags);
2d8f3038 644 mnt_drop_write(path.mnt);
18f335af 645 }
2d8f3038 646 path_put(&path);
68f1bb8b
JL
647 if (retry_estale(error, lookup_flags)) {
648 lookup_flags |= LOOKUP_REVAL;
649 goto retry;
650 }
1da177e4
LT
651 return error;
652}
653
8cc43116
EB
654SYSCALL_DEFINE5(setxattr, const char __user *, pathname,
655 const char __user *, name, const void __user *, value,
656 size_t, size, int, flags)
657{
658 return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW);
659}
660
64fd1de3
HC
661SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname,
662 const char __user *, name, const void __user *, value,
663 size_t, size, int, flags)
1da177e4 664{
8cc43116 665 return path_setxattr(pathname, name, value, size, flags, 0);
1da177e4
LT
666}
667
64fd1de3
HC
668SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name,
669 const void __user *,value, size_t, size, int, flags)
1da177e4 670{
2903ff01 671 struct fd f = fdget(fd);
1da177e4
LT
672 int error = -EBADF;
673
2903ff01 674 if (!f.file)
1da177e4 675 return error;
9f45f5bf 676 audit_file(f.file);
6742cee0 677 error = mnt_want_write_file(f.file);
18f335af 678 if (!error) {
c7c7a1a1
TA
679 error = setxattr(file_mnt_user_ns(f.file),
680 f.file->f_path.dentry, name,
681 value, size, flags);
6742cee0 682 mnt_drop_write_file(f.file);
18f335af 683 }
2903ff01 684 fdput(f);
1da177e4
LT
685 return error;
686}
687
688/*
689 * Extended attribute GET operations
690 */
c975cad9
SR
691ssize_t
692do_getxattr(struct user_namespace *mnt_userns, struct dentry *d,
693 struct xattr_ctx *ctx)
1da177e4
LT
694{
695 ssize_t error;
c975cad9 696 char *kname = ctx->kname->name;
1da177e4 697
c975cad9
SR
698 if (ctx->size) {
699 if (ctx->size > XATTR_SIZE_MAX)
700 ctx->size = XATTR_SIZE_MAX;
701 ctx->kvalue = kvzalloc(ctx->size, GFP_KERNEL);
702 if (!ctx->kvalue)
752ade68 703 return -ENOMEM;
1da177e4
LT
704 }
705
c975cad9 706 error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
f549d6c1 707 if (error > 0) {
2f6f0654
EB
708 if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) ||
709 (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0))
0c5fd887 710 posix_acl_fix_xattr_to_user(ctx->kvalue, error);
c975cad9 711 if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
f549d6c1 712 error = -EFAULT;
c975cad9 713 } else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
f549d6c1
SS
714 /* The file system tried to returned a value bigger
715 than XATTR_SIZE_MAX bytes. Not possible. */
716 error = -E2BIG;
1da177e4 717 }
0b2a6f23 718
c975cad9
SR
719 return error;
720}
721
722static ssize_t
723getxattr(struct user_namespace *mnt_userns, struct dentry *d,
724 const char __user *name, void __user *value, size_t size)
725{
726 ssize_t error;
727 struct xattr_name kname;
728 struct xattr_ctx ctx = {
729 .value = value,
730 .kvalue = NULL,
731 .size = size,
732 .kname = &kname,
733 .flags = 0,
734 };
735
736 error = strncpy_from_user(kname.name, name, sizeof(kname.name));
737 if (error == 0 || error == sizeof(kname.name))
738 error = -ERANGE;
739 if (error < 0)
740 return error;
741
742 error = do_getxattr(mnt_userns, d, &ctx);
0b2a6f23 743
c975cad9 744 kvfree(ctx.kvalue);
1da177e4
LT
745 return error;
746}
747
8cc43116
EB
748static ssize_t path_getxattr(const char __user *pathname,
749 const char __user *name, void __user *value,
750 size_t size, unsigned int lookup_flags)
1da177e4 751{
2d8f3038 752 struct path path;
1da177e4 753 ssize_t error;
60e66b48
JL
754retry:
755 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
756 if (error)
757 return error;
c7c7a1a1 758 error = getxattr(mnt_user_ns(path.mnt), path.dentry, name, value, size);
2d8f3038 759 path_put(&path);
60e66b48
JL
760 if (retry_estale(error, lookup_flags)) {
761 lookup_flags |= LOOKUP_REVAL;
762 goto retry;
763 }
1da177e4
LT
764 return error;
765}
766
8cc43116
EB
767SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
768 const char __user *, name, void __user *, value, size_t, size)
769{
770 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
771}
772
64fd1de3
HC
773SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
774 const char __user *, name, void __user *, value, size_t, size)
1da177e4 775{
8cc43116 776 return path_getxattr(pathname, name, value, size, 0);
1da177e4
LT
777}
778
64fd1de3
HC
779SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
780 void __user *, value, size_t, size)
1da177e4 781{
2903ff01 782 struct fd f = fdget(fd);
1da177e4
LT
783 ssize_t error = -EBADF;
784
2903ff01 785 if (!f.file)
1da177e4 786 return error;
9f45f5bf 787 audit_file(f.file);
c7c7a1a1
TA
788 error = getxattr(file_mnt_user_ns(f.file), f.file->f_path.dentry,
789 name, value, size);
2903ff01 790 fdput(f);
1da177e4
LT
791 return error;
792}
793
794/*
795 * Extended attribute LIST operations
796 */
797static ssize_t
798listxattr(struct dentry *d, char __user *list, size_t size)
799{
800 ssize_t error;
801 char *klist = NULL;
802
803 if (size) {
804 if (size > XATTR_LIST_MAX)
805 size = XATTR_LIST_MAX;
752ade68
MH
806 klist = kvmalloc(size, GFP_KERNEL);
807 if (!klist)
808 return -ENOMEM;
1da177e4
LT
809 }
810
659564c8 811 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
812 if (error > 0) {
813 if (size && copy_to_user(list, klist, error))
814 error = -EFAULT;
815 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
816 /* The file system tried to returned a list bigger
817 than XATTR_LIST_MAX bytes. Not possible. */
818 error = -E2BIG;
1da177e4 819 }
0b2a6f23
RW
820
821 kvfree(klist);
822
1da177e4
LT
823 return error;
824}
825
8cc43116
EB
826static ssize_t path_listxattr(const char __user *pathname, char __user *list,
827 size_t size, unsigned int lookup_flags)
1da177e4 828{
2d8f3038 829 struct path path;
1da177e4 830 ssize_t error;
10a90cf3
JL
831retry:
832 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
833 if (error)
834 return error;
2d8f3038
AV
835 error = listxattr(path.dentry, list, size);
836 path_put(&path);
10a90cf3
JL
837 if (retry_estale(error, lookup_flags)) {
838 lookup_flags |= LOOKUP_REVAL;
839 goto retry;
840 }
1da177e4
LT
841 return error;
842}
843
8cc43116
EB
844SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
845 size_t, size)
846{
847 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
848}
849
64fd1de3
HC
850SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
851 size_t, size)
1da177e4 852{
8cc43116 853 return path_listxattr(pathname, list, size, 0);
1da177e4
LT
854}
855
64fd1de3 856SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4 857{
2903ff01 858 struct fd f = fdget(fd);
1da177e4
LT
859 ssize_t error = -EBADF;
860
2903ff01 861 if (!f.file)
1da177e4 862 return error;
9f45f5bf 863 audit_file(f.file);
2903ff01
AV
864 error = listxattr(f.file->f_path.dentry, list, size);
865 fdput(f);
1da177e4
LT
866 return error;
867}
868
869/*
870 * Extended attribute REMOVE operations
871 */
872static long
c7c7a1a1
TA
873removexattr(struct user_namespace *mnt_userns, struct dentry *d,
874 const char __user *name)
1da177e4
LT
875{
876 int error;
877 char kname[XATTR_NAME_MAX + 1];
878
879 error = strncpy_from_user(kname, name, sizeof(kname));
880 if (error == 0 || error == sizeof(kname))
881 error = -ERANGE;
882 if (error < 0)
883 return error;
884
c7c7a1a1 885 return vfs_removexattr(mnt_userns, d, kname);
1da177e4
LT
886}
887
8cc43116
EB
888static int path_removexattr(const char __user *pathname,
889 const char __user *name, unsigned int lookup_flags)
1da177e4 890{
2d8f3038 891 struct path path;
1da177e4 892 int error;
12f06212
JL
893retry:
894 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
895 if (error)
896 return error;
2d8f3038 897 error = mnt_want_write(path.mnt);
18f335af 898 if (!error) {
c7c7a1a1 899 error = removexattr(mnt_user_ns(path.mnt), path.dentry, name);
2d8f3038 900 mnt_drop_write(path.mnt);
18f335af 901 }
2d8f3038 902 path_put(&path);
12f06212
JL
903 if (retry_estale(error, lookup_flags)) {
904 lookup_flags |= LOOKUP_REVAL;
905 goto retry;
906 }
1da177e4
LT
907 return error;
908}
909
8cc43116
EB
910SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
911 const char __user *, name)
912{
913 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
914}
915
6a6160a7
HC
916SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
917 const char __user *, name)
1da177e4 918{
8cc43116 919 return path_removexattr(pathname, name, 0);
1da177e4
LT
920}
921
6a6160a7 922SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4 923{
2903ff01 924 struct fd f = fdget(fd);
1da177e4
LT
925 int error = -EBADF;
926
2903ff01 927 if (!f.file)
1da177e4 928 return error;
9f45f5bf 929 audit_file(f.file);
6742cee0 930 error = mnt_want_write_file(f.file);
18f335af 931 if (!error) {
c7c7a1a1
TA
932 error = removexattr(file_mnt_user_ns(f.file),
933 f.file->f_path.dentry, name);
6742cee0 934 mnt_drop_write_file(f.file);
18f335af 935 }
2903ff01 936 fdput(f);
1da177e4
LT
937 return error;
938}
939
1da177e4
LT
940/*
941 * Combine the results of the list() operation from every xattr_handler in the
942 * list.
943 */
944ssize_t
945generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
946{
bb435453 947 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
948 unsigned int size = 0;
949
950 if (!buffer) {
431547b3 951 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
952 if (!handler->name ||
953 (handler->list && !handler->list(dentry)))
c4803c49 954 continue;
764a5c6b 955 size += strlen(handler->name) + 1;
431547b3 956 }
1da177e4
LT
957 } else {
958 char *buf = buffer;
764a5c6b 959 size_t len;
1da177e4
LT
960
961 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
962 if (!handler->name ||
963 (handler->list && !handler->list(dentry)))
c4803c49 964 continue;
764a5c6b
AG
965 len = strlen(handler->name);
966 if (len + 1 > buffer_size)
1da177e4 967 return -ERANGE;
764a5c6b
AG
968 memcpy(buf, handler->name, len + 1);
969 buf += len + 1;
970 buffer_size -= len + 1;
1da177e4
LT
971 }
972 size = buf - buffer;
973 }
974 return size;
975}
1da177e4 976EXPORT_SYMBOL(generic_listxattr);
38f38657 977
e409de99
AG
978/**
979 * xattr_full_name - Compute full attribute name from suffix
980 *
981 * @handler: handler of the xattr_handler operation
982 * @name: name passed to the xattr_handler operation
983 *
984 * The get and set xattr handler operations are called with the remainder of
985 * the attribute name after skipping the handler's prefix: for example, "foo"
986 * is passed to the get operation of a handler with prefix "user." to get
987 * attribute "user.foo". The full name is still "there" in the name though.
988 *
989 * Note: the list xattr handler operation when called from the vfs is passed a
990 * NULL name; some file systems use this operation internally, with varying
991 * semantics.
992 */
993const char *xattr_full_name(const struct xattr_handler *handler,
994 const char *name)
995{
98e9cb57 996 size_t prefix_len = strlen(xattr_prefix(handler));
e409de99
AG
997
998 return name - prefix_len;
999}
1000EXPORT_SYMBOL(xattr_full_name);
1001
38f38657
AR
1002/*
1003 * Allocate new xattr and copy in the value; but leave the name to callers.
1004 */
1005struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
1006{
1007 struct simple_xattr *new_xattr;
1008 size_t len;
1009
1010 /* wrap around? */
1011 len = sizeof(*new_xattr) + size;
4e66d445 1012 if (len < sizeof(*new_xattr))
38f38657
AR
1013 return NULL;
1014
fdc85222 1015 new_xattr = kvmalloc(len, GFP_KERNEL);
38f38657
AR
1016 if (!new_xattr)
1017 return NULL;
1018
1019 new_xattr->size = size;
1020 memcpy(new_xattr->value, value, size);
1021 return new_xattr;
1022}
1023
1024/*
1025 * xattr GET operation for in-memory/pseudo filesystems
1026 */
1027int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
1028 void *buffer, size_t size)
1029{
1030 struct simple_xattr *xattr;
1031 int ret = -ENODATA;
1032
1033 spin_lock(&xattrs->lock);
1034 list_for_each_entry(xattr, &xattrs->head, list) {
1035 if (strcmp(name, xattr->name))
1036 continue;
1037
1038 ret = xattr->size;
1039 if (buffer) {
1040 if (size < xattr->size)
1041 ret = -ERANGE;
1042 else
1043 memcpy(buffer, xattr->value, xattr->size);
1044 }
1045 break;
1046 }
1047 spin_unlock(&xattrs->lock);
1048 return ret;
1049}
1050
aa7c5241
AG
1051/**
1052 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
1053 * @xattrs: target simple_xattr list
1054 * @name: name of the extended attribute
1055 * @value: value of the xattr. If %NULL, will remove the attribute.
1056 * @size: size of the new xattr
1057 * @flags: %XATTR_{CREATE|REPLACE}
a46a2295 1058 * @removed_size: returns size of the removed xattr, -1 if none removed
aa7c5241
AG
1059 *
1060 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
1061 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
1062 * otherwise, fails with -ENODATA.
1063 *
1064 * Returns 0 on success, -errno on failure.
1065 */
1066int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
a46a2295
DX
1067 const void *value, size_t size, int flags,
1068 ssize_t *removed_size)
38f38657
AR
1069{
1070 struct simple_xattr *xattr;
43385846 1071 struct simple_xattr *new_xattr = NULL;
38f38657
AR
1072 int err = 0;
1073
772b3140
DX
1074 if (removed_size)
1075 *removed_size = -1;
1076
38f38657
AR
1077 /* value == NULL means remove */
1078 if (value) {
1079 new_xattr = simple_xattr_alloc(value, size);
1080 if (!new_xattr)
1081 return -ENOMEM;
1082
1083 new_xattr->name = kstrdup(name, GFP_KERNEL);
1084 if (!new_xattr->name) {
fdc85222 1085 kvfree(new_xattr);
38f38657
AR
1086 return -ENOMEM;
1087 }
1088 }
1089
1090 spin_lock(&xattrs->lock);
1091 list_for_each_entry(xattr, &xattrs->head, list) {
1092 if (!strcmp(name, xattr->name)) {
1093 if (flags & XATTR_CREATE) {
1094 xattr = new_xattr;
1095 err = -EEXIST;
1096 } else if (new_xattr) {
1097 list_replace(&xattr->list, &new_xattr->list);
a46a2295
DX
1098 if (removed_size)
1099 *removed_size = xattr->size;
38f38657
AR
1100 } else {
1101 list_del(&xattr->list);
a46a2295
DX
1102 if (removed_size)
1103 *removed_size = xattr->size;
38f38657
AR
1104 }
1105 goto out;
1106 }
1107 }
1108 if (flags & XATTR_REPLACE) {
1109 xattr = new_xattr;
1110 err = -ENODATA;
1111 } else {
1112 list_add(&new_xattr->list, &xattrs->head);
1113 xattr = NULL;
1114 }
1115out:
1116 spin_unlock(&xattrs->lock);
1117 if (xattr) {
1118 kfree(xattr->name);
fdc85222 1119 kvfree(xattr);
38f38657
AR
1120 }
1121 return err;
1122
1123}
1124
38f38657
AR
1125static bool xattr_is_trusted(const char *name)
1126{
1127 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1128}
1129
786534b9
AG
1130static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1131 const char *name)
1132{
1133 size_t len = strlen(name) + 1;
1134 if (*buffer) {
1135 if (*remaining_size < len)
1136 return -ERANGE;
1137 memcpy(*buffer, name, len);
1138 *buffer += len;
1139 }
1140 *remaining_size -= len;
1141 return 0;
1142}
1143
38f38657
AR
1144/*
1145 * xattr LIST operation for in-memory/pseudo filesystems
1146 */
786534b9
AG
1147ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1148 char *buffer, size_t size)
38f38657
AR
1149{
1150 bool trusted = capable(CAP_SYS_ADMIN);
1151 struct simple_xattr *xattr;
786534b9 1152 ssize_t remaining_size = size;
0e9a7da5 1153 int err = 0;
786534b9
AG
1154
1155#ifdef CONFIG_FS_POSIX_ACL
ffc4c922
AG
1156 if (IS_POSIXACL(inode)) {
1157 if (inode->i_acl) {
1158 err = xattr_list_one(&buffer, &remaining_size,
1159 XATTR_NAME_POSIX_ACL_ACCESS);
1160 if (err)
1161 return err;
1162 }
1163 if (inode->i_default_acl) {
1164 err = xattr_list_one(&buffer, &remaining_size,
1165 XATTR_NAME_POSIX_ACL_DEFAULT);
1166 if (err)
1167 return err;
1168 }
786534b9
AG
1169 }
1170#endif
38f38657
AR
1171
1172 spin_lock(&xattrs->lock);
1173 list_for_each_entry(xattr, &xattrs->head, list) {
38f38657
AR
1174 /* skip "trusted." attributes for unprivileged callers */
1175 if (!trusted && xattr_is_trusted(xattr->name))
1176 continue;
1177
786534b9
AG
1178 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1179 if (err)
0e9a7da5 1180 break;
38f38657
AR
1181 }
1182 spin_unlock(&xattrs->lock);
1183
0e9a7da5 1184 return err ? err : size - remaining_size;
38f38657
AR
1185}
1186
4895768b
AR
1187/*
1188 * Adds an extended attribute to the list
1189 */
38f38657
AR
1190void simple_xattr_list_add(struct simple_xattrs *xattrs,
1191 struct simple_xattr *new_xattr)
1192{
1193 spin_lock(&xattrs->lock);
1194 list_add(&new_xattr->list, &xattrs->head);
1195 spin_unlock(&xattrs->lock);
1196}