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