Blame view
fs/xattr.c
25.2 KB
457c89965
|
1 |
// SPDX-License-Identifier: GPL-2.0-only |
1da177e4c
|
2 3 4 5 6 7 8 9 10 11 12 |
/* File: fs/xattr.c Extended attribute handling. Copyright (C) 2001 by Andreas Gruenbacher <a.gruenbacher@computer.org> Copyright (C) 2001 SGI - Silicon Graphics, Inc <linux-xfs@oss.sgi.com> Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> */ #include <linux/fs.h> #include <linux/slab.h> |
1da177e4c
|
13 14 |
#include <linux/file.h> #include <linux/xattr.h> |
18f335aff
|
15 |
#include <linux/mount.h> |
1da177e4c
|
16 17 |
#include <linux/namei.h> #include <linux/security.h> |
c7b87de23
|
18 |
#include <linux/evm.h> |
1da177e4c
|
19 |
#include <linux/syscalls.h> |
630d9c472
|
20 |
#include <linux/export.h> |
0eeca2830
|
21 |
#include <linux/fsnotify.h> |
73241ccca
|
22 |
#include <linux/audit.h> |
0d08d7b7e
|
23 |
#include <linux/vmalloc.h> |
2f6f0654a
|
24 |
#include <linux/posix_acl_xattr.h> |
1da177e4c
|
25 |
|
7c0f6ba68
|
26 |
#include <linux/uaccess.h> |
5be196e5f
|
27 |
|
b6ba11773
|
28 29 30 31 32 33 34 35 36 37 38 39 |
static const char * strcmp_prefix(const char *a, const char *a_prefix) { while (*a_prefix && *a == *a_prefix) { a++; a_prefix++; } return *a_prefix ? NULL : a; } /* * In order to implement different sets of xattr operations for each xattr |
6c6ef9f26
|
40 41 42 |
* prefix, a filesystem should create a null-terminated array of struct * xattr_handler (one for each prefix) and hang a pointer to it off of the * s_xattr field of the superblock. |
b6ba11773
|
43 44 45 46 47 48 49 50 51 52 53 |
*/ #define for_each_xattr_handler(handlers, handler) \ if (handlers) \ for ((handler) = *(handlers)++; \ (handler) != NULL; \ (handler) = *(handlers)++) /* * Find the xattr_handler with the matching prefix. */ static const struct xattr_handler * |
d0a5b995a
|
54 |
xattr_resolve_name(struct inode *inode, const char **name) |
b6ba11773
|
55 |
{ |
d0a5b995a
|
56 |
const struct xattr_handler **handlers = inode->i_sb->s_xattr; |
b6ba11773
|
57 |
const struct xattr_handler *handler; |
5f6e59ae8
|
58 59 60 |
if (!(inode->i_opflags & IOP_XATTR)) { if (unlikely(is_bad_inode(inode))) return ERR_PTR(-EIO); |
d0a5b995a
|
61 |
return ERR_PTR(-EOPNOTSUPP); |
5f6e59ae8
|
62 |
} |
b6ba11773
|
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
for_each_xattr_handler(handlers, handler) { const char *n; n = strcmp_prefix(*name, xattr_prefix(handler)); if (n) { if (!handler->prefix ^ !*n) { if (*n) continue; return ERR_PTR(-EINVAL); } *name = n; return handler; } } return ERR_PTR(-EOPNOTSUPP); } |
e0ad7b073
|
79 80 81 82 83 84 85 86 87 88 89 90 |
/* * Check permissions for extended attribute access. This is a bit complicated * because different namespaces have very different rules. */ static int xattr_permission(struct inode *inode, const char *name, int mask) { /* * We can never set or remove an extended attribute on a read-only * filesystem or on an immutable / append-only inode. */ if (mask & MAY_WRITE) { |
e0ad7b073
|
91 92 |
if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) return -EPERM; |
0bd23d09b
|
93 94 95 96 97 98 99 |
/* * Updating an xattr will likely cause i_uid and i_gid * to be writen back improperly if their true value is * unknown to the vfs. */ if (HAS_UNMAPPED_ID(inode)) return -EPERM; |
e0ad7b073
|
100 101 102 103 104 105 106 107 108 109 110 |
} /* * No restriction for security.* and system.* from the VFS. Decision * on these is left to the underlying filesystem / security module. */ if (!strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) || !strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN)) return 0; /* |
55b23bde1
|
111 |
* The trusted.* namespace can only be accessed by privileged users. |
e0ad7b073
|
112 |
*/ |
55b23bde1
|
113 114 115 116 117 |
if (!strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN)) { if (!capable(CAP_SYS_ADMIN)) return (mask & MAY_WRITE) ? -EPERM : -ENODATA; return 0; } |
e0ad7b073
|
118 |
|
55b23bde1
|
119 120 |
/* * In the user.* namespace, only regular files and directories can have |
f1f2d8713
|
121 |
* extended attributes. For sticky directories, only the owner and |
55b23bde1
|
122 |
* privileged users can write attributes. |
f1f2d8713
|
123 |
*/ |
e0ad7b073
|
124 |
if (!strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN)) { |
f1f2d8713
|
125 |
if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode)) |
55b23bde1
|
126 |
return (mask & MAY_WRITE) ? -EPERM : -ENODATA; |
f1f2d8713
|
127 |
if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) && |
2e1496707
|
128 |
(mask & MAY_WRITE) && !inode_owner_or_capable(inode)) |
e0ad7b073
|
129 130 |
return -EPERM; } |
f419a2e3b
|
131 |
return inode_permission(inode, mask); |
e0ad7b073
|
132 |
} |
5d6c31910
|
133 134 135 136 |
int __vfs_setxattr(struct dentry *dentry, struct inode *inode, const char *name, const void *value, size_t size, int flags) { |
6c6ef9f26
|
137 138 139 140 141 142 |
const struct xattr_handler *handler; handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler)) return PTR_ERR(handler); if (!handler->set) |
5d6c31910
|
143 |
return -EOPNOTSUPP; |
6c6ef9f26
|
144 145 146 |
if (size == 0) value = ""; /* empty EA, do not remove */ return handler->set(handler, dentry, inode, name, value, size, flags); |
5d6c31910
|
147 148 |
} EXPORT_SYMBOL(__vfs_setxattr); |
b1ab7e4b2
|
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
/** * __vfs_setxattr_noperm - perform setxattr operation without performing * permission checks. * * @dentry - object to perform setxattr on * @name - xattr name to set * @value - value to set @name to * @size - size of @value * @flags - flags to pass into filesystem operations * * returns the result of the internal setxattr or setsecurity operations. * * This function requires the caller to lock the inode's i_mutex before it * is executed. It also assumes that the caller will make the appropriate * permission checks. */ int __vfs_setxattr_noperm(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) |
5be196e5f
|
167 168 |
{ struct inode *inode = dentry->d_inode; |
4a5901537
|
169 |
int error = -EAGAIN; |
69b457329
|
170 171 |
int issec = !strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN); |
e0ad7b073
|
172 |
|
69b457329
|
173 174 |
if (issec) inode->i_flags &= ~S_NOSEC; |
6c6ef9f26
|
175 |
if (inode->i_opflags & IOP_XATTR) { |
5d6c31910
|
176 |
error = __vfs_setxattr(dentry, inode, name, value, size, flags); |
5be196e5f
|
177 178 179 180 181 |
if (!error) { fsnotify_xattr(dentry); security_inode_post_setxattr(dentry, name, value, size, flags); } |
4a5901537
|
182 |
} else { |
5f6e59ae8
|
183 184 |
if (unlikely(is_bad_inode(inode))) return -EIO; |
4a5901537
|
185 186 187 188 189 190 191 192 193 194 195 196 |
} if (error == -EAGAIN) { error = -EOPNOTSUPP; if (issec) { const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; error = security_inode_setsecurity(inode, suffix, value, size, flags); if (!error) fsnotify_xattr(dentry); } |
5be196e5f
|
197 |
} |
b1ab7e4b2
|
198 199 200 |
return error; } |
11e64146d
|
201 202 203 204 205 206 207 208 209 210 211 212 |
/** * __vfs_setxattr_locked: set an extended attribute while holding the inode * lock * * @dentry - object to perform setxattr on * @name - xattr name to set * @value - value to set @name to * @size - size of @value * @flags - flags to pass into filesystem operations * @delegated_inode - on return, will contain an inode pointer that * a delegation was broken on, NULL if none. */ |
b1ab7e4b2
|
213 |
int |
11e64146d
|
214 215 216 |
__vfs_setxattr_locked(struct dentry *dentry, const char *name, const void *value, size_t size, int flags, struct inode **delegated_inode) |
b1ab7e4b2
|
217 218 219 220 221 222 223 |
{ struct inode *inode = dentry->d_inode; int error; error = xattr_permission(inode, name, MAY_WRITE); if (error) return error; |
b1ab7e4b2
|
224 225 226 |
error = security_inode_setxattr(dentry, name, value, size, flags); if (error) goto out; |
11e64146d
|
227 228 229 |
error = try_break_deleg(inode, delegated_inode); if (error) goto out; |
b1ab7e4b2
|
230 |
error = __vfs_setxattr_noperm(dentry, name, value, size, flags); |
5be196e5f
|
231 |
out: |
11e64146d
|
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
return error; } EXPORT_SYMBOL_GPL(__vfs_setxattr_locked); int vfs_setxattr(struct dentry *dentry, const char *name, const void *value, size_t size, int flags) { struct inode *inode = dentry->d_inode; struct inode *delegated_inode = NULL; int error; retry_deleg: inode_lock(inode); error = __vfs_setxattr_locked(dentry, name, value, size, flags, &delegated_inode); |
5955102c9
|
248 |
inode_unlock(inode); |
11e64146d
|
249 250 251 252 253 254 |
if (delegated_inode) { error = break_deleg_wait(&delegated_inode); if (!error) goto retry_deleg; } |
5be196e5f
|
255 256 257 |
return error; } EXPORT_SYMBOL_GPL(vfs_setxattr); |
2220c5b0a
|
258 |
static ssize_t |
424925940
|
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
xattr_getsecurity(struct inode *inode, const char *name, void *value, size_t size) { void *buffer = NULL; ssize_t len; if (!value || !size) { len = security_inode_getsecurity(inode, name, &buffer, false); goto out_noalloc; } len = security_inode_getsecurity(inode, name, &buffer, true); if (len < 0) return len; if (size < len) { len = -ERANGE; goto out; } memcpy(value, buffer, len); out: |
57e7ba04d
|
279 |
kfree(buffer); |
424925940
|
280 281 282 |
out_noalloc: return len; } |
424925940
|
283 |
|
1601fbad2
|
284 285 286 287 288 289 290 291 292 293 294 295 |
/* * vfs_getxattr_alloc - allocate memory, if necessary, before calling getxattr * * Allocate memory, if not already allocated, or re-allocate correct size, * before retrieving the extended attribute. * * Returns the result of alloc, if failed, or the getxattr operation. */ ssize_t vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value, size_t xattr_size, gfp_t flags) { |
6c6ef9f26
|
296 |
const struct xattr_handler *handler; |
1601fbad2
|
297 298 299 300 301 302 303 |
struct inode *inode = dentry->d_inode; char *value = *xattr_value; int error; error = xattr_permission(inode, name, MAY_READ); if (error) return error; |
6c6ef9f26
|
304 305 306 307 |
handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler)) return PTR_ERR(handler); if (!handler->get) |
1601fbad2
|
308 |
return -EOPNOTSUPP; |
6c6ef9f26
|
309 |
error = handler->get(handler, dentry, inode, name, NULL, 0); |
1601fbad2
|
310 311 312 313 314 315 316 317 318 |
if (error < 0) return error; if (!value || (error > xattr_size)) { value = krealloc(*xattr_value, error + 1, flags); if (!value) return -ENOMEM; memset(value, 0, error + 1); } |
6c6ef9f26
|
319 |
error = handler->get(handler, dentry, inode, name, value, error); |
1601fbad2
|
320 321 322 |
*xattr_value = value; return error; } |
424925940
|
323 |
ssize_t |
5d6c31910
|
324 325 326 |
__vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name, void *value, size_t size) { |
6c6ef9f26
|
327 328 329 330 331 332 |
const struct xattr_handler *handler; handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler)) return PTR_ERR(handler); if (!handler->get) |
5d6c31910
|
333 |
return -EOPNOTSUPP; |
6c6ef9f26
|
334 |
return handler->get(handler, dentry, inode, name, value, size); |
5d6c31910
|
335 336 337 338 |
} EXPORT_SYMBOL(__vfs_getxattr); ssize_t |
8f0cfa52a
|
339 |
vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size) |
5be196e5f
|
340 341 342 |
{ struct inode *inode = dentry->d_inode; int error; |
e0ad7b073
|
343 344 345 |
error = xattr_permission(inode, name, MAY_READ); if (error) return error; |
5be196e5f
|
346 347 348 |
error = security_inode_getxattr(dentry, name); if (error) return error; |
5be196e5f
|
349 |
if (!strncmp(name, XATTR_SECURITY_PREFIX, |
e0ad7b073
|
350 351 |
XATTR_SECURITY_PREFIX_LEN)) { const char *suffix = name + XATTR_SECURITY_PREFIX_LEN; |
424925940
|
352 |
int ret = xattr_getsecurity(inode, suffix, value, size); |
5be196e5f
|
353 354 355 356 |
/* * Only overwrite the return value if a security module * is actually active. */ |
4bea58053
|
357 358 359 |
if (ret == -EOPNOTSUPP) goto nolsm; return ret; |
5be196e5f
|
360 |
} |
4bea58053
|
361 |
nolsm: |
5d6c31910
|
362 |
return __vfs_getxattr(dentry, inode, name, value, size); |
5be196e5f
|
363 364 |
} EXPORT_SYMBOL_GPL(vfs_getxattr); |
659564c8a
|
365 |
ssize_t |
bf3ee7136
|
366 |
vfs_listxattr(struct dentry *dentry, char *list, size_t size) |
659564c8a
|
367 |
{ |
bf3ee7136
|
368 |
struct inode *inode = d_inode(dentry); |
659564c8a
|
369 |
ssize_t error; |
bf3ee7136
|
370 |
error = security_inode_listxattr(dentry); |
659564c8a
|
371 372 |
if (error) return error; |
bf3ee7136
|
373 |
if (inode->i_op->listxattr && (inode->i_opflags & IOP_XATTR)) { |
bf3ee7136
|
374 |
error = inode->i_op->listxattr(dentry, list, size); |
659564c8a
|
375 |
} else { |
bf3ee7136
|
376 |
error = security_inode_listsecurity(inode, list, size); |
659564c8a
|
377 378 379 380 381 382 |
if (size && error > size) error = -ERANGE; } return error; } EXPORT_SYMBOL_GPL(vfs_listxattr); |
5be196e5f
|
383 |
int |
5d6c31910
|
384 |
__vfs_removexattr(struct dentry *dentry, const char *name) |
5be196e5f
|
385 |
{ |
6c6ef9f26
|
386 387 |
struct inode *inode = d_inode(dentry); const struct xattr_handler *handler; |
5be196e5f
|
388 |
|
6c6ef9f26
|
389 390 391 392 |
handler = xattr_resolve_name(inode, &name); if (IS_ERR(handler)) return PTR_ERR(handler); if (!handler->set) |
5be196e5f
|
393 |
return -EOPNOTSUPP; |
6c6ef9f26
|
394 |
return handler->set(handler, dentry, inode, name, NULL, 0, XATTR_REPLACE); |
5d6c31910
|
395 396 |
} EXPORT_SYMBOL(__vfs_removexattr); |
11e64146d
|
397 398 399 400 401 402 403 404 405 |
/** * __vfs_removexattr_locked: set an extended attribute while holding the inode * lock * * @dentry - object to perform setxattr on * @name - name of xattr to remove * @delegated_inode - on return, will contain an inode pointer that * a delegation was broken on, NULL if none. */ |
5d6c31910
|
406 |
int |
11e64146d
|
407 408 |
__vfs_removexattr_locked(struct dentry *dentry, const char *name, struct inode **delegated_inode) |
5d6c31910
|
409 410 411 |
{ struct inode *inode = dentry->d_inode; int error; |
5be196e5f
|
412 |
|
e0ad7b073
|
413 414 415 |
error = xattr_permission(inode, name, MAY_WRITE); if (error) return error; |
5be196e5f
|
416 |
error = security_inode_removexattr(dentry, name); |
7c51bb00c
|
417 418 |
if (error) goto out; |
5be196e5f
|
419 |
|
11e64146d
|
420 421 422 |
error = try_break_deleg(inode, delegated_inode); if (error) goto out; |
5d6c31910
|
423 |
error = __vfs_removexattr(dentry, name); |
5be196e5f
|
424 |
|
c7b87de23
|
425 |
if (!error) { |
5be196e5f
|
426 |
fsnotify_xattr(dentry); |
c7b87de23
|
427 428 |
evm_inode_post_removexattr(dentry, name); } |
7c51bb00c
|
429 430 |
out: |
11e64146d
|
431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
return error; } EXPORT_SYMBOL_GPL(__vfs_removexattr_locked); int vfs_removexattr(struct dentry *dentry, const char *name) { struct inode *inode = dentry->d_inode; struct inode *delegated_inode = NULL; int error; retry_deleg: inode_lock(inode); error = __vfs_removexattr_locked(dentry, name, &delegated_inode); |
5955102c9
|
445 |
inode_unlock(inode); |
11e64146d
|
446 447 448 449 450 451 |
if (delegated_inode) { error = break_deleg_wait(&delegated_inode); if (!error) goto retry_deleg; } |
5be196e5f
|
452 453 454 |
return error; } EXPORT_SYMBOL_GPL(vfs_removexattr); |
1da177e4c
|
455 456 457 458 |
/* * Extended attribute SET operations */ static long |
8f0cfa52a
|
459 |
setxattr(struct dentry *d, const char __user *name, const void __user *value, |
1da177e4c
|
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
size_t size, int flags) { int error; void *kvalue = NULL; char kname[XATTR_NAME_MAX + 1]; if (flags & ~(XATTR_CREATE|XATTR_REPLACE)) return -EINVAL; error = strncpy_from_user(kname, name, sizeof(kname)); if (error == 0 || error == sizeof(kname)) error = -ERANGE; if (error < 0) return error; if (size) { if (size > XATTR_SIZE_MAX) return -E2BIG; |
752ade68c
|
478 479 480 |
kvalue = kvmalloc(size, GFP_KERNEL); if (!kvalue) return -ENOMEM; |
44c824982
|
481 482 483 484 |
if (copy_from_user(kvalue, value, size)) { error = -EFAULT; goto out; } |
2f6f0654a
|
485 486 487 |
if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) || (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) posix_acl_fix_xattr_from_user(kvalue, size); |
8db6c34f1
|
488 489 490 491 492 493 |
else if (strcmp(kname, XATTR_NAME_CAPS) == 0) { error = cap_convert_nscap(d, &kvalue, size); if (error < 0) goto out; size = error; } |
1da177e4c
|
494 |
} |
5be196e5f
|
495 |
error = vfs_setxattr(d, kname, kvalue, size, flags); |
44c824982
|
496 |
out: |
0b2a6f231
|
497 |
kvfree(kvalue); |
1da177e4c
|
498 499 |
return error; } |
8cc431165
|
500 501 502 |
static int path_setxattr(const char __user *pathname, const char __user *name, const void __user *value, size_t size, int flags, unsigned int lookup_flags) |
1da177e4c
|
503 |
{ |
2d8f30380
|
504 |
struct path path; |
1da177e4c
|
505 |
int error; |
68f1bb8bb
|
506 507 |
retry: error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); |
1da177e4c
|
508 509 |
if (error) return error; |
2d8f30380
|
510 |
error = mnt_want_write(path.mnt); |
18f335aff
|
511 |
if (!error) { |
2d8f30380
|
512 513 |
error = setxattr(path.dentry, name, value, size, flags); mnt_drop_write(path.mnt); |
18f335aff
|
514 |
} |
2d8f30380
|
515 |
path_put(&path); |
68f1bb8bb
|
516 517 518 519 |
if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL; goto retry; } |
1da177e4c
|
520 521 |
return error; } |
8cc431165
|
522 523 524 525 526 527 |
SYSCALL_DEFINE5(setxattr, const char __user *, pathname, const char __user *, name, const void __user *, value, size_t, size, int, flags) { return path_setxattr(pathname, name, value, size, flags, LOOKUP_FOLLOW); } |
64fd1de3d
|
528 529 530 |
SYSCALL_DEFINE5(lsetxattr, const char __user *, pathname, const char __user *, name, const void __user *, value, size_t, size, int, flags) |
1da177e4c
|
531 |
{ |
8cc431165
|
532 |
return path_setxattr(pathname, name, value, size, flags, 0); |
1da177e4c
|
533 |
} |
64fd1de3d
|
534 535 |
SYSCALL_DEFINE5(fsetxattr, int, fd, const char __user *, name, const void __user *,value, size_t, size, int, flags) |
1da177e4c
|
536 |
{ |
2903ff019
|
537 |
struct fd f = fdget(fd); |
1da177e4c
|
538 |
int error = -EBADF; |
2903ff019
|
539 |
if (!f.file) |
1da177e4c
|
540 |
return error; |
9f45f5bf3
|
541 |
audit_file(f.file); |
6742cee04
|
542 |
error = mnt_want_write_file(f.file); |
18f335aff
|
543 |
if (!error) { |
9f45f5bf3
|
544 |
error = setxattr(f.file->f_path.dentry, name, value, size, flags); |
6742cee04
|
545 |
mnt_drop_write_file(f.file); |
18f335aff
|
546 |
} |
2903ff019
|
547 |
fdput(f); |
1da177e4c
|
548 549 550 551 552 553 554 |
return error; } /* * Extended attribute GET operations */ static ssize_t |
8f0cfa52a
|
555 556 |
getxattr(struct dentry *d, const char __user *name, void __user *value, size_t size) |
1da177e4c
|
557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
{ ssize_t error; void *kvalue = NULL; char kname[XATTR_NAME_MAX + 1]; error = strncpy_from_user(kname, name, sizeof(kname)); if (error == 0 || error == sizeof(kname)) error = -ERANGE; if (error < 0) return error; if (size) { if (size > XATTR_SIZE_MAX) size = XATTR_SIZE_MAX; |
752ade68c
|
571 572 573 |
kvalue = kvzalloc(size, GFP_KERNEL); if (!kvalue) return -ENOMEM; |
1da177e4c
|
574 |
} |
5be196e5f
|
575 |
error = vfs_getxattr(d, kname, kvalue, size); |
f549d6c18
|
576 |
if (error > 0) { |
2f6f0654a
|
577 578 |
if ((strcmp(kname, XATTR_NAME_POSIX_ACL_ACCESS) == 0) || (strcmp(kname, XATTR_NAME_POSIX_ACL_DEFAULT) == 0)) |
82c9a927b
|
579 |
posix_acl_fix_xattr_to_user(kvalue, error); |
f549d6c18
|
580 581 582 583 584 585 |
if (size && copy_to_user(value, kvalue, error)) error = -EFAULT; } else if (error == -ERANGE && size >= XATTR_SIZE_MAX) { /* The file system tried to returned a value bigger than XATTR_SIZE_MAX bytes. Not possible. */ error = -E2BIG; |
1da177e4c
|
586 |
} |
0b2a6f231
|
587 588 |
kvfree(kvalue); |
1da177e4c
|
589 590 |
return error; } |
8cc431165
|
591 592 593 |
static ssize_t path_getxattr(const char __user *pathname, const char __user *name, void __user *value, size_t size, unsigned int lookup_flags) |
1da177e4c
|
594 |
{ |
2d8f30380
|
595 |
struct path path; |
1da177e4c
|
596 |
ssize_t error; |
60e66b48c
|
597 598 |
retry: error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); |
1da177e4c
|
599 600 |
if (error) return error; |
2d8f30380
|
601 602 |
error = getxattr(path.dentry, name, value, size); path_put(&path); |
60e66b48c
|
603 604 605 606 |
if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL; goto retry; } |
1da177e4c
|
607 608 |
return error; } |
8cc431165
|
609 610 611 612 613 |
SYSCALL_DEFINE4(getxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) { return path_getxattr(pathname, name, value, size, LOOKUP_FOLLOW); } |
64fd1de3d
|
614 615 |
SYSCALL_DEFINE4(lgetxattr, const char __user *, pathname, const char __user *, name, void __user *, value, size_t, size) |
1da177e4c
|
616 |
{ |
8cc431165
|
617 |
return path_getxattr(pathname, name, value, size, 0); |
1da177e4c
|
618 |
} |
64fd1de3d
|
619 620 |
SYSCALL_DEFINE4(fgetxattr, int, fd, const char __user *, name, void __user *, value, size_t, size) |
1da177e4c
|
621 |
{ |
2903ff019
|
622 |
struct fd f = fdget(fd); |
1da177e4c
|
623 |
ssize_t error = -EBADF; |
2903ff019
|
624 |
if (!f.file) |
1da177e4c
|
625 |
return error; |
9f45f5bf3
|
626 |
audit_file(f.file); |
2903ff019
|
627 628 |
error = getxattr(f.file->f_path.dentry, name, value, size); fdput(f); |
1da177e4c
|
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 |
return error; } /* * Extended attribute LIST operations */ static ssize_t listxattr(struct dentry *d, char __user *list, size_t size) { ssize_t error; char *klist = NULL; if (size) { if (size > XATTR_LIST_MAX) size = XATTR_LIST_MAX; |
752ade68c
|
644 645 646 |
klist = kvmalloc(size, GFP_KERNEL); if (!klist) return -ENOMEM; |
1da177e4c
|
647 |
} |
659564c8a
|
648 |
error = vfs_listxattr(d, klist, size); |
f549d6c18
|
649 650 651 652 653 654 655 |
if (error > 0) { if (size && copy_to_user(list, klist, error)) error = -EFAULT; } else if (error == -ERANGE && size >= XATTR_LIST_MAX) { /* The file system tried to returned a list bigger than XATTR_LIST_MAX bytes. Not possible. */ error = -E2BIG; |
1da177e4c
|
656 |
} |
0b2a6f231
|
657 658 |
kvfree(klist); |
1da177e4c
|
659 660 |
return error; } |
8cc431165
|
661 662 |
static ssize_t path_listxattr(const char __user *pathname, char __user *list, size_t size, unsigned int lookup_flags) |
1da177e4c
|
663 |
{ |
2d8f30380
|
664 |
struct path path; |
1da177e4c
|
665 |
ssize_t error; |
10a90cf36
|
666 667 |
retry: error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); |
1da177e4c
|
668 669 |
if (error) return error; |
2d8f30380
|
670 671 |
error = listxattr(path.dentry, list, size); path_put(&path); |
10a90cf36
|
672 673 674 675 |
if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL; goto retry; } |
1da177e4c
|
676 677 |
return error; } |
8cc431165
|
678 679 680 681 682 |
SYSCALL_DEFINE3(listxattr, const char __user *, pathname, char __user *, list, size_t, size) { return path_listxattr(pathname, list, size, LOOKUP_FOLLOW); } |
64fd1de3d
|
683 684 |
SYSCALL_DEFINE3(llistxattr, const char __user *, pathname, char __user *, list, size_t, size) |
1da177e4c
|
685 |
{ |
8cc431165
|
686 |
return path_listxattr(pathname, list, size, 0); |
1da177e4c
|
687 |
} |
64fd1de3d
|
688 |
SYSCALL_DEFINE3(flistxattr, int, fd, char __user *, list, size_t, size) |
1da177e4c
|
689 |
{ |
2903ff019
|
690 |
struct fd f = fdget(fd); |
1da177e4c
|
691 |
ssize_t error = -EBADF; |
2903ff019
|
692 |
if (!f.file) |
1da177e4c
|
693 |
return error; |
9f45f5bf3
|
694 |
audit_file(f.file); |
2903ff019
|
695 696 |
error = listxattr(f.file->f_path.dentry, list, size); fdput(f); |
1da177e4c
|
697 698 699 700 701 702 703 |
return error; } /* * Extended attribute REMOVE operations */ static long |
8f0cfa52a
|
704 |
removexattr(struct dentry *d, const char __user *name) |
1da177e4c
|
705 706 707 708 709 710 711 712 713 |
{ int error; char kname[XATTR_NAME_MAX + 1]; error = strncpy_from_user(kname, name, sizeof(kname)); if (error == 0 || error == sizeof(kname)) error = -ERANGE; if (error < 0) return error; |
5be196e5f
|
714 |
return vfs_removexattr(d, kname); |
1da177e4c
|
715 |
} |
8cc431165
|
716 717 |
static int path_removexattr(const char __user *pathname, const char __user *name, unsigned int lookup_flags) |
1da177e4c
|
718 |
{ |
2d8f30380
|
719 |
struct path path; |
1da177e4c
|
720 |
int error; |
12f062129
|
721 722 |
retry: error = user_path_at(AT_FDCWD, pathname, lookup_flags, &path); |
1da177e4c
|
723 724 |
if (error) return error; |
2d8f30380
|
725 |
error = mnt_want_write(path.mnt); |
18f335aff
|
726 |
if (!error) { |
2d8f30380
|
727 728 |
error = removexattr(path.dentry, name); mnt_drop_write(path.mnt); |
18f335aff
|
729 |
} |
2d8f30380
|
730 |
path_put(&path); |
12f062129
|
731 732 733 734 |
if (retry_estale(error, lookup_flags)) { lookup_flags |= LOOKUP_REVAL; goto retry; } |
1da177e4c
|
735 736 |
return error; } |
8cc431165
|
737 738 739 740 741 |
SYSCALL_DEFINE2(removexattr, const char __user *, pathname, const char __user *, name) { return path_removexattr(pathname, name, LOOKUP_FOLLOW); } |
6a6160a7b
|
742 743 |
SYSCALL_DEFINE2(lremovexattr, const char __user *, pathname, const char __user *, name) |
1da177e4c
|
744 |
{ |
8cc431165
|
745 |
return path_removexattr(pathname, name, 0); |
1da177e4c
|
746 |
} |
6a6160a7b
|
747 |
SYSCALL_DEFINE2(fremovexattr, int, fd, const char __user *, name) |
1da177e4c
|
748 |
{ |
2903ff019
|
749 |
struct fd f = fdget(fd); |
1da177e4c
|
750 |
int error = -EBADF; |
2903ff019
|
751 |
if (!f.file) |
1da177e4c
|
752 |
return error; |
9f45f5bf3
|
753 |
audit_file(f.file); |
6742cee04
|
754 |
error = mnt_want_write_file(f.file); |
18f335aff
|
755 |
if (!error) { |
9f45f5bf3
|
756 |
error = removexattr(f.file->f_path.dentry, name); |
6742cee04
|
757 |
mnt_drop_write_file(f.file); |
18f335aff
|
758 |
} |
2903ff019
|
759 |
fdput(f); |
1da177e4c
|
760 761 |
return error; } |
1da177e4c
|
762 |
/* |
1da177e4c
|
763 764 765 766 767 768 |
* Combine the results of the list() operation from every xattr_handler in the * list. */ ssize_t generic_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size) { |
bb4354538
|
769 |
const struct xattr_handler *handler, **handlers = dentry->d_sb->s_xattr; |
1da177e4c
|
770 771 772 |
unsigned int size = 0; if (!buffer) { |
431547b3c
|
773 |
for_each_xattr_handler(handlers, handler) { |
764a5c6b1
|
774 775 |
if (!handler->name || (handler->list && !handler->list(dentry))) |
c4803c497
|
776 |
continue; |
764a5c6b1
|
777 |
size += strlen(handler->name) + 1; |
431547b3c
|
778 |
} |
1da177e4c
|
779 780 |
} else { char *buf = buffer; |
764a5c6b1
|
781 |
size_t len; |
1da177e4c
|
782 783 |
for_each_xattr_handler(handlers, handler) { |
764a5c6b1
|
784 785 |
if (!handler->name || (handler->list && !handler->list(dentry))) |
c4803c497
|
786 |
continue; |
764a5c6b1
|
787 788 |
len = strlen(handler->name); if (len + 1 > buffer_size) |
1da177e4c
|
789 |
return -ERANGE; |
764a5c6b1
|
790 791 792 |
memcpy(buf, handler->name, len + 1); buf += len + 1; buffer_size -= len + 1; |
1da177e4c
|
793 794 795 796 797 |
} size = buf - buffer; } return size; } |
1da177e4c
|
798 |
EXPORT_SYMBOL(generic_listxattr); |
38f386574
|
799 |
|
e409de992
|
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 |
/** * xattr_full_name - Compute full attribute name from suffix * * @handler: handler of the xattr_handler operation * @name: name passed to the xattr_handler operation * * The get and set xattr handler operations are called with the remainder of * the attribute name after skipping the handler's prefix: for example, "foo" * is passed to the get operation of a handler with prefix "user." to get * attribute "user.foo". The full name is still "there" in the name though. * * Note: the list xattr handler operation when called from the vfs is passed a * NULL name; some file systems use this operation internally, with varying * semantics. */ const char *xattr_full_name(const struct xattr_handler *handler, const char *name) { |
98e9cb571
|
818 |
size_t prefix_len = strlen(xattr_prefix(handler)); |
e409de992
|
819 820 821 822 |
return name - prefix_len; } EXPORT_SYMBOL(xattr_full_name); |
38f386574
|
823 824 825 826 827 828 829 830 831 832 |
/* * Allocate new xattr and copy in the value; but leave the name to callers. */ struct simple_xattr *simple_xattr_alloc(const void *value, size_t size) { struct simple_xattr *new_xattr; size_t len; /* wrap around? */ len = sizeof(*new_xattr) + size; |
4e66d445d
|
833 |
if (len < sizeof(*new_xattr)) |
38f386574
|
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
return NULL; new_xattr = kmalloc(len, GFP_KERNEL); if (!new_xattr) return NULL; new_xattr->size = size; memcpy(new_xattr->value, value, size); return new_xattr; } /* * xattr GET operation for in-memory/pseudo filesystems */ int simple_xattr_get(struct simple_xattrs *xattrs, const char *name, void *buffer, size_t size) { struct simple_xattr *xattr; int ret = -ENODATA; spin_lock(&xattrs->lock); list_for_each_entry(xattr, &xattrs->head, list) { if (strcmp(name, xattr->name)) continue; ret = xattr->size; if (buffer) { if (size < xattr->size) ret = -ERANGE; else memcpy(buffer, xattr->value, xattr->size); } break; } spin_unlock(&xattrs->lock); return ret; } |
aa7c5241c
|
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 |
/** * simple_xattr_set - xattr SET operation for in-memory/pseudo filesystems * @xattrs: target simple_xattr list * @name: name of the extended attribute * @value: value of the xattr. If %NULL, will remove the attribute. * @size: size of the new xattr * @flags: %XATTR_{CREATE|REPLACE} * * %XATTR_CREATE is set, the xattr shouldn't exist already; otherwise fails * with -EEXIST. If %XATTR_REPLACE is set, the xattr should exist; * otherwise, fails with -ENODATA. * * Returns 0 on success, -errno on failure. */ int simple_xattr_set(struct simple_xattrs *xattrs, const char *name, const void *value, size_t size, int flags) |
38f386574
|
887 888 |
{ struct simple_xattr *xattr; |
433858469
|
889 |
struct simple_xattr *new_xattr = NULL; |
38f386574
|
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 |
int err = 0; /* value == NULL means remove */ if (value) { new_xattr = simple_xattr_alloc(value, size); if (!new_xattr) return -ENOMEM; new_xattr->name = kstrdup(name, GFP_KERNEL); if (!new_xattr->name) { kfree(new_xattr); return -ENOMEM; } } spin_lock(&xattrs->lock); list_for_each_entry(xattr, &xattrs->head, list) { if (!strcmp(name, xattr->name)) { if (flags & XATTR_CREATE) { xattr = new_xattr; err = -EEXIST; } else if (new_xattr) { list_replace(&xattr->list, &new_xattr->list); } else { list_del(&xattr->list); } goto out; } } if (flags & XATTR_REPLACE) { xattr = new_xattr; err = -ENODATA; } else { list_add(&new_xattr->list, &xattrs->head); xattr = NULL; } out: spin_unlock(&xattrs->lock); if (xattr) { kfree(xattr->name); kfree(xattr); } return err; } |
38f386574
|
935 936 937 938 |
static bool xattr_is_trusted(const char *name) { return !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN); } |
786534b92
|
939 940 941 942 943 944 945 946 947 948 949 950 951 |
static int xattr_list_one(char **buffer, ssize_t *remaining_size, const char *name) { size_t len = strlen(name) + 1; if (*buffer) { if (*remaining_size < len) return -ERANGE; memcpy(*buffer, name, len); *buffer += len; } *remaining_size -= len; return 0; } |
38f386574
|
952 953 954 |
/* * xattr LIST operation for in-memory/pseudo filesystems */ |
786534b92
|
955 956 |
ssize_t simple_xattr_list(struct inode *inode, struct simple_xattrs *xattrs, char *buffer, size_t size) |
38f386574
|
957 958 959 |
{ bool trusted = capable(CAP_SYS_ADMIN); struct simple_xattr *xattr; |
786534b92
|
960 |
ssize_t remaining_size = size; |
0e9a7da51
|
961 |
int err = 0; |
786534b92
|
962 963 |
#ifdef CONFIG_FS_POSIX_ACL |
ffc4c9222
|
964 965 966 967 968 969 970 971 972 973 974 975 976 |
if (IS_POSIXACL(inode)) { if (inode->i_acl) { err = xattr_list_one(&buffer, &remaining_size, XATTR_NAME_POSIX_ACL_ACCESS); if (err) return err; } if (inode->i_default_acl) { err = xattr_list_one(&buffer, &remaining_size, XATTR_NAME_POSIX_ACL_DEFAULT); if (err) return err; } |
786534b92
|
977 978 |
} #endif |
38f386574
|
979 980 981 |
spin_lock(&xattrs->lock); list_for_each_entry(xattr, &xattrs->head, list) { |
38f386574
|
982 983 984 |
/* skip "trusted." attributes for unprivileged callers */ if (!trusted && xattr_is_trusted(xattr->name)) continue; |
786534b92
|
985 986 |
err = xattr_list_one(&buffer, &remaining_size, xattr->name); if (err) |
0e9a7da51
|
987 |
break; |
38f386574
|
988 989 |
} spin_unlock(&xattrs->lock); |
0e9a7da51
|
990 |
return err ? err : size - remaining_size; |
38f386574
|
991 |
} |
4895768b6
|
992 993 994 |
/* * Adds an extended attribute to the list */ |
38f386574
|
995 996 997 998 999 1000 1001 |
void simple_xattr_list_add(struct simple_xattrs *xattrs, struct simple_xattr *new_xattr) { spin_lock(&xattrs->lock); list_add(&new_xattr->list, &xattrs->head); spin_unlock(&xattrs->lock); } |