acl: remove a slew of now unused helpers
[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
1a91794c
SR
600int do_setxattr(struct user_namespace *mnt_userns, struct dentry *dentry,
601 struct xattr_ctx *ctx)
602{
318e6685
CB
603 if (is_posix_acl_xattr(ctx->kname->name))
604 return do_set_acl(mnt_userns, dentry, ctx->kname->name,
605 ctx->kvalue, ctx->size);
606
1a91794c
SR
607 return vfs_setxattr(mnt_userns, dentry, ctx->kname->name,
608 ctx->kvalue, ctx->size, ctx->flags);
609}
610
611static long
612setxattr(struct user_namespace *mnt_userns, struct dentry *d,
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
630 error = do_setxattr(mnt_userns, 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) {
c7c7a1a1
TA
649 error = setxattr(mnt_user_ns(path.mnt), path.dentry, name,
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) {
c7c7a1a1
TA
686 error = setxattr(file_mnt_user_ns(f.file),
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
SR
698ssize_t
699do_getxattr(struct user_namespace *mnt_userns, struct dentry *d,
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
CB
713 if (is_posix_acl_xattr(ctx->kname->name))
714 error = do_get_acl(mnt_userns, d, kname, ctx->kvalue, ctx->size);
715 else
716 error = vfs_getxattr(mnt_userns, d, kname, ctx->kvalue, ctx->size);
f549d6c1 717 if (error > 0) {
c975cad9 718 if (ctx->size && copy_to_user(ctx->value, ctx->kvalue, error))
f549d6c1 719 error = -EFAULT;
c975cad9 720 } else if (error == -ERANGE && ctx->size >= XATTR_SIZE_MAX) {
f549d6c1
SS
721 /* The file system tried to returned a value bigger
722 than XATTR_SIZE_MAX bytes. Not possible. */
723 error = -E2BIG;
1da177e4 724 }
0b2a6f23 725
c975cad9
SR
726 return error;
727}
728
729static ssize_t
730getxattr(struct user_namespace *mnt_userns, struct dentry *d,
731 const char __user *name, void __user *value, size_t size)
732{
733 ssize_t error;
734 struct xattr_name kname;
735 struct xattr_ctx ctx = {
736 .value = value,
737 .kvalue = NULL,
738 .size = size,
739 .kname = &kname,
740 .flags = 0,
741 };
742
743 error = strncpy_from_user(kname.name, name, sizeof(kname.name));
744 if (error == 0 || error == sizeof(kname.name))
745 error = -ERANGE;
746 if (error < 0)
747 return error;
748
749 error = do_getxattr(mnt_userns, d, &ctx);
0b2a6f23 750
c975cad9 751 kvfree(ctx.kvalue);
1da177e4
LT
752 return error;
753}
754
8cc43116
EB
755static ssize_t path_getxattr(const char __user *pathname,
756 const char __user *name, void __user *value,
757 size_t size, unsigned int lookup_flags)
1da177e4 758{
2d8f3038 759 struct path path;
1da177e4 760 ssize_t error;
60e66b48
JL
761retry:
762 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
763 if (error)
764 return error;
c7c7a1a1 765 error = getxattr(mnt_user_ns(path.mnt), path.dentry, name, value, size);
2d8f3038 766 path_put(&path);
60e66b48
JL
767 if (retry_estale(error, lookup_flags)) {
768 lookup_flags |= LOOKUP_REVAL;
769 goto retry;
770 }
1da177e4
LT
771 return error;
772}
773
8cc43116
EB
774SYSCALL_DEFINE4(getxattr, const char __user *, pathname,
775 const char __user *, name, void __user *, value, size_t, size)
776{
777 return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW);
778}
779
64fd1de3
HC
780SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname,
781 const char __user *, name, void __user *, value, size_t, size)
1da177e4 782{
8cc43116 783 return path_getxattr(pathname, name, value, size, 0);
1da177e4
LT
784}
785
64fd1de3
HC
786SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name,
787 void __user *, value, size_t, size)
1da177e4 788{
2903ff01 789 struct fd f = fdget(fd);
1da177e4
LT
790 ssize_t error = -EBADF;
791
2903ff01 792 if (!f.file)
1da177e4 793 return error;
9f45f5bf 794 audit_file(f.file);
c7c7a1a1
TA
795 error = getxattr(file_mnt_user_ns(f.file), f.file->f_path.dentry,
796 name, value, size);
2903ff01 797 fdput(f);
1da177e4
LT
798 return error;
799}
800
801/*
802 * Extended attribute LIST operations
803 */
804static ssize_t
805listxattr(struct dentry *d, char __user *list, size_t size)
806{
807 ssize_t error;
808 char *klist = NULL;
809
810 if (size) {
811 if (size > XATTR_LIST_MAX)
812 size = XATTR_LIST_MAX;
752ade68
MH
813 klist = kvmalloc(size, GFP_KERNEL);
814 if (!klist)
815 return -ENOMEM;
1da177e4
LT
816 }
817
659564c8 818 error = vfs_listxattr(d, klist, size);
f549d6c1
SS
819 if (error > 0) {
820 if (size && copy_to_user(list, klist, error))
821 error = -EFAULT;
822 } else if (error == -ERANGE && size >= XATTR_LIST_MAX) {
823 /* The file system tried to returned a list bigger
824 than XATTR_LIST_MAX bytes. Not possible. */
825 error = -E2BIG;
1da177e4 826 }
0b2a6f23
RW
827
828 kvfree(klist);
829
1da177e4
LT
830 return error;
831}
832
8cc43116
EB
833static ssize_t path_listxattr(const char __user *pathname, char __user *list,
834 size_t size, unsigned int lookup_flags)
1da177e4 835{
2d8f3038 836 struct path path;
1da177e4 837 ssize_t error;
10a90cf3
JL
838retry:
839 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
840 if (error)
841 return error;
2d8f3038
AV
842 error = listxattr(path.dentry, list, size);
843 path_put(&path);
10a90cf3
JL
844 if (retry_estale(error, lookup_flags)) {
845 lookup_flags |= LOOKUP_REVAL;
846 goto retry;
847 }
1da177e4
LT
848 return error;
849}
850
8cc43116
EB
851SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list,
852 size_t, size)
853{
854 return path_listxattr(pathname, list, size, LOOKUP_FOLLOW);
855}
856
64fd1de3
HC
857SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list,
858 size_t, size)
1da177e4 859{
8cc43116 860 return path_listxattr(pathname, list, size, 0);
1da177e4
LT
861}
862
64fd1de3 863SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size)
1da177e4 864{
2903ff01 865 struct fd f = fdget(fd);
1da177e4
LT
866 ssize_t error = -EBADF;
867
2903ff01 868 if (!f.file)
1da177e4 869 return error;
9f45f5bf 870 audit_file(f.file);
2903ff01
AV
871 error = listxattr(f.file->f_path.dentry, list, size);
872 fdput(f);
1da177e4
LT
873 return error;
874}
875
876/*
877 * Extended attribute REMOVE operations
878 */
879static long
c7c7a1a1
TA
880removexattr(struct user_namespace *mnt_userns, struct dentry *d,
881 const char __user *name)
1da177e4
LT
882{
883 int error;
884 char kname[XATTR_NAME_MAX + 1];
885
886 error = strncpy_from_user(kname, name, sizeof(kname));
887 if (error == 0 || error == sizeof(kname))
888 error = -ERANGE;
889 if (error < 0)
890 return error;
891
318e6685
CB
892 if (is_posix_acl_xattr(kname))
893 return vfs_remove_acl(mnt_userns, d, kname);
894
c7c7a1a1 895 return vfs_removexattr(mnt_userns, d, kname);
1da177e4
LT
896}
897
8cc43116
EB
898static int path_removexattr(const char __user *pathname,
899 const char __user *name, unsigned int lookup_flags)
1da177e4 900{
2d8f3038 901 struct path path;
1da177e4 902 int error;
12f06212
JL
903retry:
904 error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path);
1da177e4
LT
905 if (error)
906 return error;
2d8f3038 907 error = mnt_want_write(path.mnt);
18f335af 908 if (!error) {
c7c7a1a1 909 error = removexattr(mnt_user_ns(path.mnt), path.dentry, name);
2d8f3038 910 mnt_drop_write(path.mnt);
18f335af 911 }
2d8f3038 912 path_put(&path);
12f06212
JL
913 if (retry_estale(error, lookup_flags)) {
914 lookup_flags |= LOOKUP_REVAL;
915 goto retry;
916 }
1da177e4
LT
917 return error;
918}
919
8cc43116
EB
920SYSCALL_DEFINE2(removexattr, const char __user *, pathname,
921 const char __user *, name)
922{
923 return path_removexattr(pathname, name, LOOKUP_FOLLOW);
924}
925
6a6160a7
HC
926SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname,
927 const char __user *, name)
1da177e4 928{
8cc43116 929 return path_removexattr(pathname, name, 0);
1da177e4
LT
930}
931
6a6160a7 932SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name)
1da177e4 933{
2903ff01 934 struct fd f = fdget(fd);
1da177e4
LT
935 int error = -EBADF;
936
2903ff01 937 if (!f.file)
1da177e4 938 return error;
9f45f5bf 939 audit_file(f.file);
6742cee0 940 error = mnt_want_write_file(f.file);
18f335af 941 if (!error) {
c7c7a1a1
TA
942 error = removexattr(file_mnt_user_ns(f.file),
943 f.file->f_path.dentry, name);
6742cee0 944 mnt_drop_write_file(f.file);
18f335af 945 }
2903ff01 946 fdput(f);
1da177e4
LT
947 return error;
948}
949
1da177e4
LT
950/*
951 * Combine the results of the list() operation from every xattr_handler in the
952 * list.
953 */
954ssize_t
955generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
956{
bb435453 957 const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr;
1da177e4
LT
958 unsigned int size = 0;
959
960 if (!buffer) {
431547b3 961 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
962 if (!handler->name ||
963 (handler->list && !handler->list(dentry)))
c4803c49 964 continue;
764a5c6b 965 size += strlen(handler->name) + 1;
431547b3 966 }
1da177e4
LT
967 } else {
968 char *buf = buffer;
764a5c6b 969 size_t len;
1da177e4
LT
970
971 for_each_xattr_handler(handlers, handler) {
764a5c6b
AG
972 if (!handler->name ||
973 (handler->list && !handler->list(dentry)))
c4803c49 974 continue;
764a5c6b
AG
975 len = strlen(handler->name);
976 if (len + 1 > buffer_size)
1da177e4 977 return -ERANGE;
764a5c6b
AG
978 memcpy(buf, handler->name, len + 1);
979 buf += len + 1;
980 buffer_size -= len + 1;
1da177e4
LT
981 }
982 size = buf - buffer;
983 }
984 return size;
985}
1da177e4 986EXPORT_SYMBOL(generic_listxattr);
38f38657 987
e409de99
AG
988/**
989 * xattr_full_name - Compute full attribute name from suffix
990 *
991 * @handler: handler of the xattr_handler operation
992 * @name: name passed to the xattr_handler operation
993 *
994 * The get and set xattr handler operations are called with the remainder of
995 * the attribute name after skipping the handler's prefix: for example, "foo"
996 * is passed to the get operation of a handler with prefix "user." to get
997 * attribute "user.foo". The full name is still "there" in the name though.
998 *
999 * Note: the list xattr handler operation when called from the vfs is passed a
1000 * NULL name; some file systems use this operation internally, with varying
1001 * semantics.
1002 */
1003const char *xattr_full_name(const struct xattr_handler *handler,
1004 const char *name)
1005{
98e9cb57 1006 size_t prefix_len = strlen(xattr_prefix(handler));
e409de99
AG
1007
1008 return name - prefix_len;
1009}
1010EXPORT_SYMBOL(xattr_full_name);
1011
38f38657
AR
1012/*
1013 * Allocate new xattr and copy in the value; but leave the name to callers.
1014 */
1015struct simple_xattr *simple_xattr_alloc(const void *value, size_t size)
1016{
1017 struct simple_xattr *new_xattr;
1018 size_t len;
1019
1020 /* wrap around? */
1021 len = sizeof(*new_xattr) + size;
4e66d445 1022 if (len < sizeof(*new_xattr))
38f38657
AR
1023 return NULL;
1024
fdc85222 1025 new_xattr = kvmalloc(len, GFP_KERNEL);
38f38657
AR
1026 if (!new_xattr)
1027 return NULL;
1028
1029 new_xattr->size = size;
1030 memcpy(new_xattr->value, value, size);
1031 return new_xattr;
1032}
1033
1034/*
1035 * xattr GET operation for in-memory/pseudo filesystems
1036 */
1037int simple_xattr_get(struct simple_xattrs *xattrs, const char *name,
1038 void *buffer, size_t size)
1039{
1040 struct simple_xattr *xattr;
1041 int ret = -ENODATA;
1042
1043 spin_lock(&xattrs->lock);
1044 list_for_each_entry(xattr, &xattrs->head, list) {
1045 if (strcmp(name, xattr->name))
1046 continue;
1047
1048 ret = xattr->size;
1049 if (buffer) {
1050 if (size < xattr->size)
1051 ret = -ERANGE;
1052 else
1053 memcpy(buffer, xattr->value, xattr->size);
1054 }
1055 break;
1056 }
1057 spin_unlock(&xattrs->lock);
1058 return ret;
1059}
1060
aa7c5241
AG
1061/**
1062 * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems
1063 * @xattrs: target simple_xattr list
1064 * @name: name of the extended attribute
1065 * @value: value of the xattr. If %NULL, will remove the attribute.
1066 * @size: size of the new xattr
1067 * @flags: %XATTR_{CREATE|REPLACE}
a46a2295 1068 * @removed_size: returns size of the removed xattr, -1 if none removed
aa7c5241
AG
1069 *
1070 * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails
1071 * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist;
1072 * otherwise, fails with -ENODATA.
1073 *
1074 * Returns 0 on success, -errno on failure.
1075 */
1076int simple_xattr_set(struct simple_xattrs *xattrs, const char *name,
a46a2295
DX
1077 const void *value, size_t size, int flags,
1078 ssize_t *removed_size)
38f38657
AR
1079{
1080 struct simple_xattr *xattr;
43385846 1081 struct simple_xattr *new_xattr = NULL;
38f38657
AR
1082 int err = 0;
1083
772b3140
DX
1084 if (removed_size)
1085 *removed_size = -1;
1086
38f38657
AR
1087 /* value == NULL means remove */
1088 if (value) {
1089 new_xattr = simple_xattr_alloc(value, size);
1090 if (!new_xattr)
1091 return -ENOMEM;
1092
1093 new_xattr->name = kstrdup(name, GFP_KERNEL);
1094 if (!new_xattr->name) {
fdc85222 1095 kvfree(new_xattr);
38f38657
AR
1096 return -ENOMEM;
1097 }
1098 }
1099
1100 spin_lock(&xattrs->lock);
1101 list_for_each_entry(xattr, &xattrs->head, list) {
1102 if (!strcmp(name, xattr->name)) {
1103 if (flags & XATTR_CREATE) {
1104 xattr = new_xattr;
1105 err = -EEXIST;
1106 } else if (new_xattr) {
1107 list_replace(&xattr->list, &new_xattr->list);
a46a2295
DX
1108 if (removed_size)
1109 *removed_size = xattr->size;
38f38657
AR
1110 } else {
1111 list_del(&xattr->list);
a46a2295
DX
1112 if (removed_size)
1113 *removed_size = xattr->size;
38f38657
AR
1114 }
1115 goto out;
1116 }
1117 }
1118 if (flags & XATTR_REPLACE) {
1119 xattr = new_xattr;
1120 err = -ENODATA;
1121 } else {
1122 list_add(&new_xattr->list, &xattrs->head);
1123 xattr = NULL;
1124 }
1125out:
1126 spin_unlock(&xattrs->lock);
1127 if (xattr) {
1128 kfree(xattr->name);
fdc85222 1129 kvfree(xattr);
38f38657
AR
1130 }
1131 return err;
1132
1133}
1134
38f38657
AR
1135static bool xattr_is_trusted(const char *name)
1136{
1137 return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN);
1138}
1139
786534b9
AG
1140static int xattr_list_one(char **buffer, ssize_t *remaining_size,
1141 const char *name)
1142{
1143 size_t len = strlen(name) + 1;
1144 if (*buffer) {
1145 if (*remaining_size < len)
1146 return -ERANGE;
1147 memcpy(*buffer, name, len);
1148 *buffer += len;
1149 }
1150 *remaining_size -= len;
1151 return 0;
1152}
1153
38f38657
AR
1154/*
1155 * xattr LIST operation for in-memory/pseudo filesystems
1156 */
786534b9
AG
1157ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs,
1158 char *buffer, size_t size)
38f38657
AR
1159{
1160 bool trusted = capable(CAP_SYS_ADMIN);
1161 struct simple_xattr *xattr;
786534b9 1162 ssize_t remaining_size = size;
0e9a7da5 1163 int err = 0;
786534b9
AG
1164
1165#ifdef CONFIG_FS_POSIX_ACL
ffc4c922
AG
1166 if (IS_POSIXACL(inode)) {
1167 if (inode->i_acl) {
1168 err = xattr_list_one(&buffer, &remaining_size,
1169 XATTR_NAME_POSIX_ACL_ACCESS);
1170 if (err)
1171 return err;
1172 }
1173 if (inode->i_default_acl) {
1174 err = xattr_list_one(&buffer, &remaining_size,
1175 XATTR_NAME_POSIX_ACL_DEFAULT);
1176 if (err)
1177 return err;
1178 }
786534b9
AG
1179 }
1180#endif
38f38657
AR
1181
1182 spin_lock(&xattrs->lock);
1183 list_for_each_entry(xattr, &xattrs->head, list) {
38f38657
AR
1184 /* skip "trusted." attributes for unprivileged callers */
1185 if (!trusted && xattr_is_trusted(xattr->name))
1186 continue;
1187
786534b9
AG
1188 err = xattr_list_one(&buffer, &remaining_size, xattr->name);
1189 if (err)
0e9a7da5 1190 break;
38f38657
AR
1191 }
1192 spin_unlock(&xattrs->lock);
1193
0e9a7da5 1194 return err ? err : size - remaining_size;
38f38657
AR
1195}
1196
4895768b
AR
1197/*
1198 * Adds an extended attribute to the list
1199 */
38f38657
AR
1200void simple_xattr_list_add(struct simple_xattrs *xattrs,
1201 struct simple_xattr *new_xattr)
1202{
1203 spin_lock(&xattrs->lock);
1204 list_add(&new_xattr->list, &xattrs->head);
1205 spin_unlock(&xattrs->lock);
1206}