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