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