Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* keyctl.c: userspace keyctl operations |
2 | * | |
3e30148c | 3 | * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/module.h> | |
13 | #include <linux/init.h> | |
14 | #include <linux/sched.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/syscalls.h> | |
17 | #include <linux/keyctl.h> | |
18 | #include <linux/fs.h> | |
c59ede7b | 19 | #include <linux/capability.h> |
0cb409d9 | 20 | #include <linux/string.h> |
1da177e4 | 21 | #include <linux/err.h> |
38bbca6b | 22 | #include <linux/vmalloc.h> |
70a5bb72 | 23 | #include <linux/security.h> |
1da177e4 LT |
24 | #include <asm/uaccess.h> |
25 | #include "internal.h" | |
26 | ||
0cb409d9 DA |
27 | static int key_get_type_from_user(char *type, |
28 | const char __user *_type, | |
29 | unsigned len) | |
30 | { | |
31 | int ret; | |
32 | ||
33 | ret = strncpy_from_user(type, _type, len); | |
34 | ||
35 | if (ret < 0) | |
36 | return -EFAULT; | |
37 | ||
38 | if (ret == 0 || ret >= len) | |
39 | return -EINVAL; | |
40 | ||
41 | if (type[0] == '.') | |
42 | return -EPERM; | |
43 | ||
44 | type[len - 1] = '\0'; | |
45 | ||
46 | return 0; | |
47 | } | |
48 | ||
1da177e4 LT |
49 | /*****************************************************************************/ |
50 | /* | |
51 | * extract the description of a new key from userspace and either add it as a | |
52 | * new key to the specified keyring or update a matching key in that keyring | |
53 | * - the keyring must be writable | |
54 | * - returns the new key's serial number | |
55 | * - implements add_key() | |
56 | */ | |
1e7bfb21 HC |
57 | SYSCALL_DEFINE5(add_key, const char __user *, _type, |
58 | const char __user *, _description, | |
59 | const void __user *, _payload, | |
60 | size_t, plen, | |
61 | key_serial_t, ringid) | |
1da177e4 | 62 | { |
664cceb0 | 63 | key_ref_t keyring_ref, key_ref; |
1da177e4 LT |
64 | char type[32], *description; |
65 | void *payload; | |
0cb409d9 | 66 | long ret; |
38bbca6b | 67 | bool vm; |
1da177e4 LT |
68 | |
69 | ret = -EINVAL; | |
38bbca6b | 70 | if (plen > 1024 * 1024 - 1) |
1da177e4 LT |
71 | goto error; |
72 | ||
73 | /* draw all the data into kernel space */ | |
0cb409d9 | 74 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
1da177e4 LT |
75 | if (ret < 0) |
76 | goto error; | |
1da177e4 | 77 | |
0cb409d9 DA |
78 | description = strndup_user(_description, PAGE_SIZE); |
79 | if (IS_ERR(description)) { | |
80 | ret = PTR_ERR(description); | |
1da177e4 | 81 | goto error; |
0cb409d9 | 82 | } |
1da177e4 LT |
83 | |
84 | /* pull the payload in if one was supplied */ | |
85 | payload = NULL; | |
86 | ||
38bbca6b | 87 | vm = false; |
1da177e4 LT |
88 | if (_payload) { |
89 | ret = -ENOMEM; | |
90 | payload = kmalloc(plen, GFP_KERNEL); | |
38bbca6b DH |
91 | if (!payload) { |
92 | if (plen <= PAGE_SIZE) | |
93 | goto error2; | |
94 | vm = true; | |
95 | payload = vmalloc(plen); | |
96 | if (!payload) | |
97 | goto error2; | |
98 | } | |
1da177e4 LT |
99 | |
100 | ret = -EFAULT; | |
101 | if (copy_from_user(payload, _payload, plen) != 0) | |
102 | goto error3; | |
103 | } | |
104 | ||
105 | /* find the target keyring (which must be writable) */ | |
5593122e | 106 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
664cceb0 DH |
107 | if (IS_ERR(keyring_ref)) { |
108 | ret = PTR_ERR(keyring_ref); | |
1da177e4 LT |
109 | goto error3; |
110 | } | |
111 | ||
112 | /* create or update the requested key and add it to the target | |
113 | * keyring */ | |
664cceb0 | 114 | key_ref = key_create_or_update(keyring_ref, type, description, |
6b79ccb5 AR |
115 | payload, plen, KEY_PERM_UNDEF, |
116 | KEY_ALLOC_IN_QUOTA); | |
664cceb0 DH |
117 | if (!IS_ERR(key_ref)) { |
118 | ret = key_ref_to_ptr(key_ref)->serial; | |
119 | key_ref_put(key_ref); | |
1da177e4 LT |
120 | } |
121 | else { | |
664cceb0 | 122 | ret = PTR_ERR(key_ref); |
1da177e4 LT |
123 | } |
124 | ||
664cceb0 | 125 | key_ref_put(keyring_ref); |
1da177e4 | 126 | error3: |
38bbca6b DH |
127 | if (!vm) |
128 | kfree(payload); | |
129 | else | |
130 | vfree(payload); | |
1da177e4 LT |
131 | error2: |
132 | kfree(description); | |
133 | error: | |
134 | return ret; | |
135 | ||
136 | } /* end sys_add_key() */ | |
137 | ||
138 | /*****************************************************************************/ | |
139 | /* | |
140 | * search the process keyrings for a matching key | |
141 | * - nested keyrings may also be searched if they have Search permission | |
142 | * - if a key is found, it will be attached to the destination keyring if | |
143 | * there's one specified | |
144 | * - /sbin/request-key will be invoked if _callout_info is non-NULL | |
145 | * - the _callout_info string will be passed to /sbin/request-key | |
146 | * - if the _callout_info string is empty, it will be rendered as "-" | |
147 | * - implements request_key() | |
148 | */ | |
1e7bfb21 HC |
149 | SYSCALL_DEFINE4(request_key, const char __user *, _type, |
150 | const char __user *, _description, | |
151 | const char __user *, _callout_info, | |
152 | key_serial_t, destringid) | |
1da177e4 LT |
153 | { |
154 | struct key_type *ktype; | |
664cceb0 DH |
155 | struct key *key; |
156 | key_ref_t dest_ref; | |
4a38e122 | 157 | size_t callout_len; |
1da177e4 | 158 | char type[32], *description, *callout_info; |
0cb409d9 | 159 | long ret; |
1da177e4 LT |
160 | |
161 | /* pull the type into kernel space */ | |
0cb409d9 | 162 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
1da177e4 LT |
163 | if (ret < 0) |
164 | goto error; | |
1260f801 | 165 | |
1da177e4 | 166 | /* pull the description into kernel space */ |
0cb409d9 DA |
167 | description = strndup_user(_description, PAGE_SIZE); |
168 | if (IS_ERR(description)) { | |
169 | ret = PTR_ERR(description); | |
1da177e4 | 170 | goto error; |
0cb409d9 | 171 | } |
1da177e4 LT |
172 | |
173 | /* pull the callout info into kernel space */ | |
174 | callout_info = NULL; | |
4a38e122 | 175 | callout_len = 0; |
1da177e4 | 176 | if (_callout_info) { |
0cb409d9 DA |
177 | callout_info = strndup_user(_callout_info, PAGE_SIZE); |
178 | if (IS_ERR(callout_info)) { | |
179 | ret = PTR_ERR(callout_info); | |
1da177e4 | 180 | goto error2; |
0cb409d9 | 181 | } |
4a38e122 | 182 | callout_len = strlen(callout_info); |
1da177e4 LT |
183 | } |
184 | ||
185 | /* get the destination keyring if specified */ | |
664cceb0 | 186 | dest_ref = NULL; |
1da177e4 | 187 | if (destringid) { |
5593122e DH |
188 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
189 | KEY_WRITE); | |
664cceb0 DH |
190 | if (IS_ERR(dest_ref)) { |
191 | ret = PTR_ERR(dest_ref); | |
1da177e4 LT |
192 | goto error3; |
193 | } | |
194 | } | |
195 | ||
196 | /* find the key type */ | |
197 | ktype = key_type_lookup(type); | |
198 | if (IS_ERR(ktype)) { | |
199 | ret = PTR_ERR(ktype); | |
200 | goto error4; | |
201 | } | |
202 | ||
203 | /* do the search */ | |
4a38e122 DH |
204 | key = request_key_and_link(ktype, description, callout_info, |
205 | callout_len, NULL, key_ref_to_ptr(dest_ref), | |
7e047ef5 | 206 | KEY_ALLOC_IN_QUOTA); |
1da177e4 LT |
207 | if (IS_ERR(key)) { |
208 | ret = PTR_ERR(key); | |
209 | goto error5; | |
210 | } | |
211 | ||
1da177e4 LT |
212 | ret = key->serial; |
213 | ||
3e30148c | 214 | key_put(key); |
1da177e4 LT |
215 | error5: |
216 | key_type_put(ktype); | |
217 | error4: | |
664cceb0 | 218 | key_ref_put(dest_ref); |
1da177e4 LT |
219 | error3: |
220 | kfree(callout_info); | |
221 | error2: | |
222 | kfree(description); | |
223 | error: | |
224 | return ret; | |
225 | ||
226 | } /* end sys_request_key() */ | |
227 | ||
228 | /*****************************************************************************/ | |
229 | /* | |
230 | * get the ID of the specified process keyring | |
231 | * - the keyring must have search permission to be found | |
232 | * - implements keyctl(KEYCTL_GET_KEYRING_ID) | |
233 | */ | |
234 | long keyctl_get_keyring_ID(key_serial_t id, int create) | |
235 | { | |
664cceb0 | 236 | key_ref_t key_ref; |
5593122e | 237 | unsigned long lflags; |
1da177e4 LT |
238 | long ret; |
239 | ||
5593122e DH |
240 | lflags = create ? KEY_LOOKUP_CREATE : 0; |
241 | key_ref = lookup_user_key(id, lflags, KEY_SEARCH); | |
664cceb0 DH |
242 | if (IS_ERR(key_ref)) { |
243 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
244 | goto error; |
245 | } | |
246 | ||
664cceb0 DH |
247 | ret = key_ref_to_ptr(key_ref)->serial; |
248 | key_ref_put(key_ref); | |
1da177e4 LT |
249 | error: |
250 | return ret; | |
251 | ||
252 | } /* end keyctl_get_keyring_ID() */ | |
253 | ||
254 | /*****************************************************************************/ | |
255 | /* | |
256 | * join the session keyring | |
257 | * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) | |
258 | */ | |
259 | long keyctl_join_session_keyring(const char __user *_name) | |
260 | { | |
261 | char *name; | |
0cb409d9 | 262 | long ret; |
1da177e4 LT |
263 | |
264 | /* fetch the name from userspace */ | |
265 | name = NULL; | |
266 | if (_name) { | |
0cb409d9 DA |
267 | name = strndup_user(_name, PAGE_SIZE); |
268 | if (IS_ERR(name)) { | |
269 | ret = PTR_ERR(name); | |
1da177e4 | 270 | goto error; |
0cb409d9 | 271 | } |
1da177e4 LT |
272 | } |
273 | ||
274 | /* join the session */ | |
275 | ret = join_session_keyring(name); | |
0d54ee1c | 276 | kfree(name); |
1da177e4 | 277 | |
1da177e4 LT |
278 | error: |
279 | return ret; | |
280 | ||
281 | } /* end keyctl_join_session_keyring() */ | |
282 | ||
283 | /*****************************************************************************/ | |
284 | /* | |
285 | * update a key's data payload | |
286 | * - the key must be writable | |
287 | * - implements keyctl(KEYCTL_UPDATE) | |
288 | */ | |
289 | long keyctl_update_key(key_serial_t id, | |
290 | const void __user *_payload, | |
291 | size_t plen) | |
292 | { | |
664cceb0 | 293 | key_ref_t key_ref; |
1da177e4 LT |
294 | void *payload; |
295 | long ret; | |
296 | ||
297 | ret = -EINVAL; | |
298 | if (plen > PAGE_SIZE) | |
299 | goto error; | |
300 | ||
301 | /* pull the payload in if one was supplied */ | |
302 | payload = NULL; | |
303 | if (_payload) { | |
304 | ret = -ENOMEM; | |
305 | payload = kmalloc(plen, GFP_KERNEL); | |
306 | if (!payload) | |
307 | goto error; | |
308 | ||
309 | ret = -EFAULT; | |
310 | if (copy_from_user(payload, _payload, plen) != 0) | |
311 | goto error2; | |
312 | } | |
313 | ||
314 | /* find the target key (which must be writable) */ | |
5593122e | 315 | key_ref = lookup_user_key(id, 0, KEY_WRITE); |
664cceb0 DH |
316 | if (IS_ERR(key_ref)) { |
317 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
318 | goto error2; |
319 | } | |
320 | ||
321 | /* update the key */ | |
664cceb0 | 322 | ret = key_update(key_ref, payload, plen); |
1da177e4 | 323 | |
664cceb0 | 324 | key_ref_put(key_ref); |
1da177e4 LT |
325 | error2: |
326 | kfree(payload); | |
327 | error: | |
328 | return ret; | |
329 | ||
330 | } /* end keyctl_update_key() */ | |
331 | ||
332 | /*****************************************************************************/ | |
333 | /* | |
334 | * revoke a key | |
335 | * - the key must be writable | |
336 | * - implements keyctl(KEYCTL_REVOKE) | |
337 | */ | |
338 | long keyctl_revoke_key(key_serial_t id) | |
339 | { | |
664cceb0 | 340 | key_ref_t key_ref; |
1da177e4 LT |
341 | long ret; |
342 | ||
5593122e | 343 | key_ref = lookup_user_key(id, 0, KEY_WRITE); |
664cceb0 DH |
344 | if (IS_ERR(key_ref)) { |
345 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
346 | goto error; |
347 | } | |
348 | ||
664cceb0 | 349 | key_revoke(key_ref_to_ptr(key_ref)); |
1da177e4 LT |
350 | ret = 0; |
351 | ||
664cceb0 | 352 | key_ref_put(key_ref); |
1da177e4 | 353 | error: |
1260f801 | 354 | return ret; |
1da177e4 LT |
355 | |
356 | } /* end keyctl_revoke_key() */ | |
357 | ||
358 | /*****************************************************************************/ | |
359 | /* | |
360 | * clear the specified process keyring | |
361 | * - the keyring must be writable | |
362 | * - implements keyctl(KEYCTL_CLEAR) | |
363 | */ | |
364 | long keyctl_keyring_clear(key_serial_t ringid) | |
365 | { | |
664cceb0 | 366 | key_ref_t keyring_ref; |
1da177e4 LT |
367 | long ret; |
368 | ||
5593122e | 369 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
664cceb0 DH |
370 | if (IS_ERR(keyring_ref)) { |
371 | ret = PTR_ERR(keyring_ref); | |
1da177e4 LT |
372 | goto error; |
373 | } | |
374 | ||
664cceb0 | 375 | ret = keyring_clear(key_ref_to_ptr(keyring_ref)); |
1da177e4 | 376 | |
664cceb0 | 377 | key_ref_put(keyring_ref); |
1da177e4 LT |
378 | error: |
379 | return ret; | |
380 | ||
381 | } /* end keyctl_keyring_clear() */ | |
382 | ||
383 | /*****************************************************************************/ | |
384 | /* | |
385 | * link a key into a keyring | |
386 | * - the keyring must be writable | |
387 | * - the key must be linkable | |
388 | * - implements keyctl(KEYCTL_LINK) | |
389 | */ | |
390 | long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) | |
391 | { | |
664cceb0 | 392 | key_ref_t keyring_ref, key_ref; |
1da177e4 LT |
393 | long ret; |
394 | ||
5593122e | 395 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
664cceb0 DH |
396 | if (IS_ERR(keyring_ref)) { |
397 | ret = PTR_ERR(keyring_ref); | |
1da177e4 LT |
398 | goto error; |
399 | } | |
400 | ||
5593122e | 401 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK); |
664cceb0 DH |
402 | if (IS_ERR(key_ref)) { |
403 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
404 | goto error2; |
405 | } | |
406 | ||
664cceb0 | 407 | ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); |
1da177e4 | 408 | |
664cceb0 | 409 | key_ref_put(key_ref); |
1da177e4 | 410 | error2: |
664cceb0 | 411 | key_ref_put(keyring_ref); |
1da177e4 LT |
412 | error: |
413 | return ret; | |
414 | ||
415 | } /* end keyctl_keyring_link() */ | |
416 | ||
417 | /*****************************************************************************/ | |
418 | /* | |
419 | * unlink the first attachment of a key from a keyring | |
420 | * - the keyring must be writable | |
421 | * - we don't need any permissions on the key | |
422 | * - implements keyctl(KEYCTL_UNLINK) | |
423 | */ | |
424 | long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) | |
425 | { | |
664cceb0 | 426 | key_ref_t keyring_ref, key_ref; |
1da177e4 LT |
427 | long ret; |
428 | ||
5593122e | 429 | keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE); |
664cceb0 DH |
430 | if (IS_ERR(keyring_ref)) { |
431 | ret = PTR_ERR(keyring_ref); | |
1da177e4 LT |
432 | goto error; |
433 | } | |
434 | ||
5593122e | 435 | key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); |
664cceb0 DH |
436 | if (IS_ERR(key_ref)) { |
437 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
438 | goto error2; |
439 | } | |
440 | ||
664cceb0 | 441 | ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); |
1da177e4 | 442 | |
664cceb0 | 443 | key_ref_put(key_ref); |
1da177e4 | 444 | error2: |
664cceb0 | 445 | key_ref_put(keyring_ref); |
1da177e4 LT |
446 | error: |
447 | return ret; | |
448 | ||
449 | } /* end keyctl_keyring_unlink() */ | |
450 | ||
451 | /*****************************************************************************/ | |
452 | /* | |
453 | * describe a user key | |
454 | * - the key must have view permission | |
455 | * - if there's a buffer, we place up to buflen bytes of data into it | |
456 | * - unless there's an error, we return the amount of description available, | |
457 | * irrespective of how much we may have copied | |
458 | * - the description is formatted thus: | |
459 | * type;uid;gid;perm;description<NUL> | |
460 | * - implements keyctl(KEYCTL_DESCRIBE) | |
461 | */ | |
462 | long keyctl_describe_key(key_serial_t keyid, | |
463 | char __user *buffer, | |
464 | size_t buflen) | |
465 | { | |
3e30148c | 466 | struct key *key, *instkey; |
664cceb0 | 467 | key_ref_t key_ref; |
1da177e4 LT |
468 | char *tmpbuf; |
469 | long ret; | |
470 | ||
5593122e | 471 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); |
664cceb0 | 472 | if (IS_ERR(key_ref)) { |
3e30148c DH |
473 | /* viewing a key under construction is permitted if we have the |
474 | * authorisation token handy */ | |
664cceb0 | 475 | if (PTR_ERR(key_ref) == -EACCES) { |
3e30148c DH |
476 | instkey = key_get_instantiation_authkey(keyid); |
477 | if (!IS_ERR(instkey)) { | |
478 | key_put(instkey); | |
8bbf4976 | 479 | key_ref = lookup_user_key(keyid, |
5593122e DH |
480 | KEY_LOOKUP_PARTIAL, |
481 | 0); | |
664cceb0 | 482 | if (!IS_ERR(key_ref)) |
3e30148c DH |
483 | goto okay; |
484 | } | |
485 | } | |
486 | ||
664cceb0 | 487 | ret = PTR_ERR(key_ref); |
1da177e4 LT |
488 | goto error; |
489 | } | |
490 | ||
3e30148c | 491 | okay: |
1da177e4 LT |
492 | /* calculate how much description we're going to return */ |
493 | ret = -ENOMEM; | |
494 | tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
495 | if (!tmpbuf) | |
496 | goto error2; | |
497 | ||
664cceb0 DH |
498 | key = key_ref_to_ptr(key_ref); |
499 | ||
1da177e4 | 500 | ret = snprintf(tmpbuf, PAGE_SIZE - 1, |
664cceb0 DH |
501 | "%s;%d;%d;%08x;%s", |
502 | key_ref_to_ptr(key_ref)->type->name, | |
503 | key_ref_to_ptr(key_ref)->uid, | |
504 | key_ref_to_ptr(key_ref)->gid, | |
505 | key_ref_to_ptr(key_ref)->perm, | |
506 | key_ref_to_ptr(key_ref)->description ? | |
507 | key_ref_to_ptr(key_ref)->description : "" | |
1da177e4 LT |
508 | ); |
509 | ||
510 | /* include a NUL char at the end of the data */ | |
511 | if (ret > PAGE_SIZE - 1) | |
512 | ret = PAGE_SIZE - 1; | |
513 | tmpbuf[ret] = 0; | |
514 | ret++; | |
515 | ||
516 | /* consider returning the data */ | |
517 | if (buffer && buflen > 0) { | |
518 | if (buflen > ret) | |
519 | buflen = ret; | |
520 | ||
521 | if (copy_to_user(buffer, tmpbuf, buflen) != 0) | |
522 | ret = -EFAULT; | |
523 | } | |
524 | ||
525 | kfree(tmpbuf); | |
526 | error2: | |
664cceb0 | 527 | key_ref_put(key_ref); |
1da177e4 LT |
528 | error: |
529 | return ret; | |
530 | ||
531 | } /* end keyctl_describe_key() */ | |
532 | ||
533 | /*****************************************************************************/ | |
534 | /* | |
535 | * search the specified keyring for a matching key | |
536 | * - the start keyring must be searchable | |
537 | * - nested keyrings may also be searched if they are searchable | |
538 | * - only keys with search permission may be found | |
539 | * - if a key is found, it will be attached to the destination keyring if | |
540 | * there's one specified | |
541 | * - implements keyctl(KEYCTL_SEARCH) | |
542 | */ | |
543 | long keyctl_keyring_search(key_serial_t ringid, | |
544 | const char __user *_type, | |
545 | const char __user *_description, | |
546 | key_serial_t destringid) | |
547 | { | |
548 | struct key_type *ktype; | |
664cceb0 | 549 | key_ref_t keyring_ref, key_ref, dest_ref; |
1da177e4 | 550 | char type[32], *description; |
0cb409d9 | 551 | long ret; |
1da177e4 LT |
552 | |
553 | /* pull the type and description into kernel space */ | |
0cb409d9 | 554 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
1da177e4 LT |
555 | if (ret < 0) |
556 | goto error; | |
1da177e4 | 557 | |
0cb409d9 DA |
558 | description = strndup_user(_description, PAGE_SIZE); |
559 | if (IS_ERR(description)) { | |
560 | ret = PTR_ERR(description); | |
1da177e4 | 561 | goto error; |
0cb409d9 | 562 | } |
1da177e4 LT |
563 | |
564 | /* get the keyring at which to begin the search */ | |
5593122e | 565 | keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH); |
664cceb0 DH |
566 | if (IS_ERR(keyring_ref)) { |
567 | ret = PTR_ERR(keyring_ref); | |
1da177e4 LT |
568 | goto error2; |
569 | } | |
570 | ||
571 | /* get the destination keyring if specified */ | |
664cceb0 | 572 | dest_ref = NULL; |
1da177e4 | 573 | if (destringid) { |
5593122e DH |
574 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
575 | KEY_WRITE); | |
664cceb0 DH |
576 | if (IS_ERR(dest_ref)) { |
577 | ret = PTR_ERR(dest_ref); | |
1da177e4 LT |
578 | goto error3; |
579 | } | |
580 | } | |
581 | ||
582 | /* find the key type */ | |
583 | ktype = key_type_lookup(type); | |
584 | if (IS_ERR(ktype)) { | |
585 | ret = PTR_ERR(ktype); | |
586 | goto error4; | |
587 | } | |
588 | ||
589 | /* do the search */ | |
664cceb0 DH |
590 | key_ref = keyring_search(keyring_ref, ktype, description); |
591 | if (IS_ERR(key_ref)) { | |
592 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
593 | |
594 | /* treat lack or presence of a negative key the same */ | |
595 | if (ret == -EAGAIN) | |
596 | ret = -ENOKEY; | |
597 | goto error5; | |
598 | } | |
599 | ||
600 | /* link the resulting key to the destination keyring if we can */ | |
664cceb0 | 601 | if (dest_ref) { |
29db9190 DH |
602 | ret = key_permission(key_ref, KEY_LINK); |
603 | if (ret < 0) | |
1da177e4 LT |
604 | goto error6; |
605 | ||
664cceb0 | 606 | ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref)); |
1da177e4 LT |
607 | if (ret < 0) |
608 | goto error6; | |
609 | } | |
610 | ||
664cceb0 | 611 | ret = key_ref_to_ptr(key_ref)->serial; |
1da177e4 LT |
612 | |
613 | error6: | |
664cceb0 | 614 | key_ref_put(key_ref); |
1da177e4 LT |
615 | error5: |
616 | key_type_put(ktype); | |
617 | error4: | |
664cceb0 | 618 | key_ref_put(dest_ref); |
1da177e4 | 619 | error3: |
664cceb0 | 620 | key_ref_put(keyring_ref); |
1da177e4 LT |
621 | error2: |
622 | kfree(description); | |
623 | error: | |
624 | return ret; | |
625 | ||
626 | } /* end keyctl_keyring_search() */ | |
627 | ||
1da177e4 LT |
628 | /*****************************************************************************/ |
629 | /* | |
630 | * read a user key's payload | |
631 | * - the keyring must be readable or the key must be searchable from the | |
632 | * process's keyrings | |
633 | * - if there's a buffer, we place up to buflen bytes of data into it | |
634 | * - unless there's an error, we return the amount of data in the key, | |
635 | * irrespective of how much we may have copied | |
636 | * - implements keyctl(KEYCTL_READ) | |
637 | */ | |
638 | long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) | |
639 | { | |
664cceb0 DH |
640 | struct key *key; |
641 | key_ref_t key_ref; | |
1da177e4 LT |
642 | long ret; |
643 | ||
644 | /* find the key first */ | |
5593122e | 645 | key_ref = lookup_user_key(keyid, 0, 0); |
664cceb0 DH |
646 | if (IS_ERR(key_ref)) { |
647 | ret = -ENOKEY; | |
648 | goto error; | |
1da177e4 LT |
649 | } |
650 | ||
664cceb0 DH |
651 | key = key_ref_to_ptr(key_ref); |
652 | ||
653 | /* see if we can read it directly */ | |
29db9190 DH |
654 | ret = key_permission(key_ref, KEY_READ); |
655 | if (ret == 0) | |
664cceb0 | 656 | goto can_read_key; |
29db9190 DH |
657 | if (ret != -EACCES) |
658 | goto error; | |
664cceb0 DH |
659 | |
660 | /* we can't; see if it's searchable from this process's keyrings | |
661 | * - we automatically take account of the fact that it may be | |
662 | * dangling off an instantiation key | |
663 | */ | |
664 | if (!is_key_possessed(key_ref)) { | |
665 | ret = -EACCES; | |
666 | goto error2; | |
667 | } | |
1da177e4 LT |
668 | |
669 | /* the key is probably readable - now try to read it */ | |
1da177e4 LT |
670 | can_read_key: |
671 | ret = key_validate(key); | |
672 | if (ret == 0) { | |
673 | ret = -EOPNOTSUPP; | |
674 | if (key->type->read) { | |
675 | /* read the data with the semaphore held (since we | |
676 | * might sleep) */ | |
677 | down_read(&key->sem); | |
678 | ret = key->type->read(key, buffer, buflen); | |
679 | up_read(&key->sem); | |
680 | } | |
681 | } | |
682 | ||
683 | error2: | |
684 | key_put(key); | |
685 | error: | |
686 | return ret; | |
687 | ||
688 | } /* end keyctl_read_key() */ | |
689 | ||
690 | /*****************************************************************************/ | |
691 | /* | |
692 | * change the ownership of a key | |
693 | * - the keyring owned by the changer | |
694 | * - if the uid or gid is -1, then that parameter is not changed | |
695 | * - implements keyctl(KEYCTL_CHOWN) | |
696 | */ | |
697 | long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) | |
698 | { | |
5801649d | 699 | struct key_user *newowner, *zapowner = NULL; |
1da177e4 | 700 | struct key *key; |
664cceb0 | 701 | key_ref_t key_ref; |
1da177e4 LT |
702 | long ret; |
703 | ||
704 | ret = 0; | |
705 | if (uid == (uid_t) -1 && gid == (gid_t) -1) | |
706 | goto error; | |
707 | ||
5593122e DH |
708 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
709 | KEY_SETATTR); | |
664cceb0 DH |
710 | if (IS_ERR(key_ref)) { |
711 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
712 | goto error; |
713 | } | |
714 | ||
664cceb0 DH |
715 | key = key_ref_to_ptr(key_ref); |
716 | ||
1da177e4 LT |
717 | /* make the changes with the locks held to prevent chown/chown races */ |
718 | ret = -EACCES; | |
719 | down_write(&key->sem); | |
1da177e4 LT |
720 | |
721 | if (!capable(CAP_SYS_ADMIN)) { | |
722 | /* only the sysadmin can chown a key to some other UID */ | |
723 | if (uid != (uid_t) -1 && key->uid != uid) | |
5801649d | 724 | goto error_put; |
1da177e4 LT |
725 | |
726 | /* only the sysadmin can set the key's GID to a group other | |
727 | * than one of those that the current process subscribes to */ | |
728 | if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) | |
5801649d | 729 | goto error_put; |
1da177e4 LT |
730 | } |
731 | ||
5801649d | 732 | /* change the UID */ |
1da177e4 | 733 | if (uid != (uid_t) -1 && uid != key->uid) { |
5801649d | 734 | ret = -ENOMEM; |
1d1e9756 | 735 | newowner = key_user_lookup(uid, current_user_ns()); |
5801649d FT |
736 | if (!newowner) |
737 | goto error_put; | |
738 | ||
739 | /* transfer the quota burden to the new user */ | |
740 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { | |
0b77f5bf DH |
741 | unsigned maxkeys = (uid == 0) ? |
742 | key_quota_root_maxkeys : key_quota_maxkeys; | |
743 | unsigned maxbytes = (uid == 0) ? | |
744 | key_quota_root_maxbytes : key_quota_maxbytes; | |
745 | ||
5801649d | 746 | spin_lock(&newowner->lock); |
0b77f5bf DH |
747 | if (newowner->qnkeys + 1 >= maxkeys || |
748 | newowner->qnbytes + key->quotalen >= maxbytes || | |
749 | newowner->qnbytes + key->quotalen < | |
750 | newowner->qnbytes) | |
5801649d FT |
751 | goto quota_overrun; |
752 | ||
753 | newowner->qnkeys++; | |
754 | newowner->qnbytes += key->quotalen; | |
755 | spin_unlock(&newowner->lock); | |
756 | ||
757 | spin_lock(&key->user->lock); | |
758 | key->user->qnkeys--; | |
759 | key->user->qnbytes -= key->quotalen; | |
760 | spin_unlock(&key->user->lock); | |
761 | } | |
762 | ||
763 | atomic_dec(&key->user->nkeys); | |
764 | atomic_inc(&newowner->nkeys); | |
765 | ||
766 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { | |
767 | atomic_dec(&key->user->nikeys); | |
768 | atomic_inc(&newowner->nikeys); | |
769 | } | |
770 | ||
771 | zapowner = key->user; | |
772 | key->user = newowner; | |
773 | key->uid = uid; | |
1da177e4 LT |
774 | } |
775 | ||
776 | /* change the GID */ | |
777 | if (gid != (gid_t) -1) | |
778 | key->gid = gid; | |
779 | ||
780 | ret = 0; | |
781 | ||
5801649d | 782 | error_put: |
1da177e4 LT |
783 | up_write(&key->sem); |
784 | key_put(key); | |
5801649d FT |
785 | if (zapowner) |
786 | key_user_put(zapowner); | |
787 | error: | |
1da177e4 LT |
788 | return ret; |
789 | ||
5801649d FT |
790 | quota_overrun: |
791 | spin_unlock(&newowner->lock); | |
792 | zapowner = newowner; | |
793 | ret = -EDQUOT; | |
794 | goto error_put; | |
795 | ||
1da177e4 LT |
796 | } /* end keyctl_chown_key() */ |
797 | ||
798 | /*****************************************************************************/ | |
799 | /* | |
800 | * change the permission mask on a key | |
801 | * - the keyring owned by the changer | |
802 | * - implements keyctl(KEYCTL_SETPERM) | |
803 | */ | |
804 | long keyctl_setperm_key(key_serial_t id, key_perm_t perm) | |
805 | { | |
806 | struct key *key; | |
664cceb0 | 807 | key_ref_t key_ref; |
1da177e4 LT |
808 | long ret; |
809 | ||
810 | ret = -EINVAL; | |
664cceb0 | 811 | if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) |
1da177e4 LT |
812 | goto error; |
813 | ||
5593122e DH |
814 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
815 | KEY_SETATTR); | |
664cceb0 DH |
816 | if (IS_ERR(key_ref)) { |
817 | ret = PTR_ERR(key_ref); | |
1da177e4 LT |
818 | goto error; |
819 | } | |
820 | ||
664cceb0 DH |
821 | key = key_ref_to_ptr(key_ref); |
822 | ||
76d8aeab | 823 | /* make the changes with the locks held to prevent chown/chmod races */ |
1da177e4 LT |
824 | ret = -EACCES; |
825 | down_write(&key->sem); | |
1da177e4 | 826 | |
76d8aeab | 827 | /* if we're not the sysadmin, we can only change a key that we own */ |
47d804bf | 828 | if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { |
76d8aeab DH |
829 | key->perm = perm; |
830 | ret = 0; | |
831 | } | |
1da177e4 | 832 | |
1da177e4 LT |
833 | up_write(&key->sem); |
834 | key_put(key); | |
76d8aeab | 835 | error: |
1da177e4 LT |
836 | return ret; |
837 | ||
838 | } /* end keyctl_setperm_key() */ | |
839 | ||
8bbf4976 DH |
840 | /* |
841 | * get the destination keyring for instantiation | |
842 | */ | |
843 | static long get_instantiation_keyring(key_serial_t ringid, | |
844 | struct request_key_auth *rka, | |
845 | struct key **_dest_keyring) | |
846 | { | |
847 | key_ref_t dkref; | |
848 | ||
eca1bf5b DH |
849 | *_dest_keyring = NULL; |
850 | ||
8bbf4976 | 851 | /* just return a NULL pointer if we weren't asked to make a link */ |
eca1bf5b | 852 | if (ringid == 0) |
8bbf4976 | 853 | return 0; |
8bbf4976 DH |
854 | |
855 | /* if a specific keyring is nominated by ID, then use that */ | |
856 | if (ringid > 0) { | |
5593122e | 857 | dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE); |
8bbf4976 DH |
858 | if (IS_ERR(dkref)) |
859 | return PTR_ERR(dkref); | |
860 | *_dest_keyring = key_ref_to_ptr(dkref); | |
861 | return 0; | |
862 | } | |
863 | ||
864 | if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) | |
865 | return -EINVAL; | |
866 | ||
867 | /* otherwise specify the destination keyring recorded in the | |
868 | * authorisation key (any KEY_SPEC_*_KEYRING) */ | |
869 | if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { | |
870 | *_dest_keyring = rka->dest_keyring; | |
871 | return 0; | |
872 | } | |
873 | ||
874 | return -ENOKEY; | |
875 | } | |
876 | ||
d84f4f99 DH |
877 | /* |
878 | * change the request_key authorisation key on the current process | |
879 | */ | |
880 | static int keyctl_change_reqkey_auth(struct key *key) | |
881 | { | |
882 | struct cred *new; | |
883 | ||
884 | new = prepare_creds(); | |
885 | if (!new) | |
886 | return -ENOMEM; | |
887 | ||
888 | key_put(new->request_key_auth); | |
889 | new->request_key_auth = key_get(key); | |
890 | ||
891 | return commit_creds(new); | |
892 | } | |
893 | ||
1da177e4 LT |
894 | /*****************************************************************************/ |
895 | /* | |
896 | * instantiate the key with the specified payload, and, if one is given, link | |
897 | * the key into the keyring | |
898 | */ | |
899 | long keyctl_instantiate_key(key_serial_t id, | |
900 | const void __user *_payload, | |
901 | size_t plen, | |
902 | key_serial_t ringid) | |
903 | { | |
d84f4f99 | 904 | const struct cred *cred = current_cred(); |
3e30148c | 905 | struct request_key_auth *rka; |
8bbf4976 | 906 | struct key *instkey, *dest_keyring; |
1da177e4 LT |
907 | void *payload; |
908 | long ret; | |
38bbca6b | 909 | bool vm = false; |
1da177e4 | 910 | |
d84f4f99 DH |
911 | kenter("%d,,%zu,%d", id, plen, ringid); |
912 | ||
1da177e4 | 913 | ret = -EINVAL; |
38bbca6b | 914 | if (plen > 1024 * 1024 - 1) |
1da177e4 LT |
915 | goto error; |
916 | ||
b5f545c8 DH |
917 | /* the appropriate instantiation authorisation key must have been |
918 | * assumed before calling this */ | |
919 | ret = -EPERM; | |
d84f4f99 | 920 | instkey = cred->request_key_auth; |
b5f545c8 DH |
921 | if (!instkey) |
922 | goto error; | |
923 | ||
924 | rka = instkey->payload.data; | |
925 | if (rka->target_key->serial != id) | |
926 | goto error; | |
927 | ||
1da177e4 LT |
928 | /* pull the payload in if one was supplied */ |
929 | payload = NULL; | |
930 | ||
931 | if (_payload) { | |
932 | ret = -ENOMEM; | |
933 | payload = kmalloc(plen, GFP_KERNEL); | |
38bbca6b DH |
934 | if (!payload) { |
935 | if (plen <= PAGE_SIZE) | |
936 | goto error; | |
937 | vm = true; | |
938 | payload = vmalloc(plen); | |
939 | if (!payload) | |
940 | goto error; | |
941 | } | |
1da177e4 LT |
942 | |
943 | ret = -EFAULT; | |
944 | if (copy_from_user(payload, _payload, plen) != 0) | |
945 | goto error2; | |
946 | } | |
947 | ||
3e30148c DH |
948 | /* find the destination keyring amongst those belonging to the |
949 | * requesting task */ | |
8bbf4976 DH |
950 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
951 | if (ret < 0) | |
952 | goto error2; | |
1da177e4 LT |
953 | |
954 | /* instantiate the key and link it into a keyring */ | |
3e30148c | 955 | ret = key_instantiate_and_link(rka->target_key, payload, plen, |
8bbf4976 | 956 | dest_keyring, instkey); |
1da177e4 | 957 | |
8bbf4976 | 958 | key_put(dest_keyring); |
b5f545c8 DH |
959 | |
960 | /* discard the assumed authority if it's just been disabled by | |
961 | * instantiation of the key */ | |
d84f4f99 DH |
962 | if (ret == 0) |
963 | keyctl_change_reqkey_auth(NULL); | |
b5f545c8 DH |
964 | |
965 | error2: | |
38bbca6b DH |
966 | if (!vm) |
967 | kfree(payload); | |
968 | else | |
969 | vfree(payload); | |
b5f545c8 | 970 | error: |
1da177e4 LT |
971 | return ret; |
972 | ||
973 | } /* end keyctl_instantiate_key() */ | |
974 | ||
975 | /*****************************************************************************/ | |
976 | /* | |
977 | * negatively instantiate the key with the given timeout (in seconds), and, if | |
978 | * one is given, link the key into the keyring | |
979 | */ | |
980 | long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) | |
981 | { | |
d84f4f99 | 982 | const struct cred *cred = current_cred(); |
3e30148c | 983 | struct request_key_auth *rka; |
8bbf4976 | 984 | struct key *instkey, *dest_keyring; |
1da177e4 LT |
985 | long ret; |
986 | ||
d84f4f99 DH |
987 | kenter("%d,%u,%d", id, timeout, ringid); |
988 | ||
b5f545c8 DH |
989 | /* the appropriate instantiation authorisation key must have been |
990 | * assumed before calling this */ | |
991 | ret = -EPERM; | |
d84f4f99 | 992 | instkey = cred->request_key_auth; |
b5f545c8 | 993 | if (!instkey) |
1da177e4 | 994 | goto error; |
1da177e4 | 995 | |
3e30148c | 996 | rka = instkey->payload.data; |
b5f545c8 DH |
997 | if (rka->target_key->serial != id) |
998 | goto error; | |
3e30148c | 999 | |
1da177e4 LT |
1000 | /* find the destination keyring if present (which must also be |
1001 | * writable) */ | |
8bbf4976 DH |
1002 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
1003 | if (ret < 0) | |
1004 | goto error; | |
1da177e4 LT |
1005 | |
1006 | /* instantiate the key and link it into a keyring */ | |
664cceb0 | 1007 | ret = key_negate_and_link(rka->target_key, timeout, |
8bbf4976 | 1008 | dest_keyring, instkey); |
1da177e4 | 1009 | |
8bbf4976 | 1010 | key_put(dest_keyring); |
b5f545c8 DH |
1011 | |
1012 | /* discard the assumed authority if it's just been disabled by | |
1013 | * instantiation of the key */ | |
d84f4f99 DH |
1014 | if (ret == 0) |
1015 | keyctl_change_reqkey_auth(NULL); | |
b5f545c8 DH |
1016 | |
1017 | error: | |
1da177e4 LT |
1018 | return ret; |
1019 | ||
1020 | } /* end keyctl_negate_key() */ | |
1021 | ||
3e30148c DH |
1022 | /*****************************************************************************/ |
1023 | /* | |
1024 | * set the default keyring in which request_key() will cache keys | |
1025 | * - return the old setting | |
1026 | */ | |
1027 | long keyctl_set_reqkey_keyring(int reqkey_defl) | |
1028 | { | |
d84f4f99 DH |
1029 | struct cred *new; |
1030 | int ret, old_setting; | |
1031 | ||
1032 | old_setting = current_cred_xxx(jit_keyring); | |
1033 | ||
1034 | if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) | |
1035 | return old_setting; | |
1036 | ||
1037 | new = prepare_creds(); | |
1038 | if (!new) | |
1039 | return -ENOMEM; | |
3e30148c DH |
1040 | |
1041 | switch (reqkey_defl) { | |
1042 | case KEY_REQKEY_DEFL_THREAD_KEYRING: | |
d84f4f99 | 1043 | ret = install_thread_keyring_to_cred(new); |
3e30148c | 1044 | if (ret < 0) |
d84f4f99 | 1045 | goto error; |
3e30148c DH |
1046 | goto set; |
1047 | ||
1048 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: | |
d84f4f99 DH |
1049 | ret = install_process_keyring_to_cred(new); |
1050 | if (ret < 0) { | |
1051 | if (ret != -EEXIST) | |
1052 | goto error; | |
1053 | ret = 0; | |
1054 | } | |
1055 | goto set; | |
3e30148c DH |
1056 | |
1057 | case KEY_REQKEY_DEFL_DEFAULT: | |
1058 | case KEY_REQKEY_DEFL_SESSION_KEYRING: | |
1059 | case KEY_REQKEY_DEFL_USER_KEYRING: | |
1060 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: | |
d84f4f99 DH |
1061 | case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: |
1062 | goto set; | |
3e30148c DH |
1063 | |
1064 | case KEY_REQKEY_DEFL_NO_CHANGE: | |
3e30148c DH |
1065 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
1066 | default: | |
d84f4f99 DH |
1067 | ret = -EINVAL; |
1068 | goto error; | |
3e30148c DH |
1069 | } |
1070 | ||
d84f4f99 DH |
1071 | set: |
1072 | new->jit_keyring = reqkey_defl; | |
1073 | commit_creds(new); | |
1074 | return old_setting; | |
1075 | error: | |
1076 | abort_creds(new); | |
1077 | return -EINVAL; | |
1078 | ||
3e30148c DH |
1079 | } /* end keyctl_set_reqkey_keyring() */ |
1080 | ||
017679c4 DH |
1081 | /*****************************************************************************/ |
1082 | /* | |
1083 | * set or clear the timeout for a key | |
1084 | */ | |
1085 | long keyctl_set_timeout(key_serial_t id, unsigned timeout) | |
1086 | { | |
1087 | struct timespec now; | |
1088 | struct key *key; | |
1089 | key_ref_t key_ref; | |
1090 | time_t expiry; | |
1091 | long ret; | |
1092 | ||
5593122e DH |
1093 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
1094 | KEY_SETATTR); | |
017679c4 DH |
1095 | if (IS_ERR(key_ref)) { |
1096 | ret = PTR_ERR(key_ref); | |
1097 | goto error; | |
1098 | } | |
1099 | ||
1100 | key = key_ref_to_ptr(key_ref); | |
1101 | ||
1102 | /* make the changes with the locks held to prevent races */ | |
1103 | down_write(&key->sem); | |
1104 | ||
1105 | expiry = 0; | |
1106 | if (timeout > 0) { | |
1107 | now = current_kernel_time(); | |
1108 | expiry = now.tv_sec + timeout; | |
1109 | } | |
1110 | ||
1111 | key->expiry = expiry; | |
1112 | ||
1113 | up_write(&key->sem); | |
1114 | key_put(key); | |
1115 | ||
1116 | ret = 0; | |
1117 | error: | |
1118 | return ret; | |
1119 | ||
1120 | } /* end keyctl_set_timeout() */ | |
1121 | ||
b5f545c8 DH |
1122 | /*****************************************************************************/ |
1123 | /* | |
1124 | * assume the authority to instantiate the specified key | |
1125 | */ | |
1126 | long keyctl_assume_authority(key_serial_t id) | |
1127 | { | |
1128 | struct key *authkey; | |
1129 | long ret; | |
1130 | ||
1131 | /* special key IDs aren't permitted */ | |
1132 | ret = -EINVAL; | |
1133 | if (id < 0) | |
1134 | goto error; | |
1135 | ||
1136 | /* we divest ourselves of authority if given an ID of 0 */ | |
1137 | if (id == 0) { | |
d84f4f99 | 1138 | ret = keyctl_change_reqkey_auth(NULL); |
b5f545c8 DH |
1139 | goto error; |
1140 | } | |
1141 | ||
1142 | /* attempt to assume the authority temporarily granted to us whilst we | |
1143 | * instantiate the specified key | |
1144 | * - the authorisation key must be in the current task's keyrings | |
1145 | * somewhere | |
1146 | */ | |
1147 | authkey = key_get_instantiation_authkey(id); | |
1148 | if (IS_ERR(authkey)) { | |
1149 | ret = PTR_ERR(authkey); | |
1150 | goto error; | |
1151 | } | |
1152 | ||
d84f4f99 DH |
1153 | ret = keyctl_change_reqkey_auth(authkey); |
1154 | if (ret < 0) | |
1155 | goto error; | |
1156 | key_put(authkey); | |
b5f545c8 | 1157 | |
d84f4f99 | 1158 | ret = authkey->serial; |
b5f545c8 DH |
1159 | error: |
1160 | return ret; | |
1161 | ||
1162 | } /* end keyctl_assume_authority() */ | |
1163 | ||
70a5bb72 DH |
1164 | /* |
1165 | * get the security label of a key | |
1166 | * - the key must grant us view permission | |
1167 | * - if there's a buffer, we place up to buflen bytes of data into it | |
1168 | * - unless there's an error, we return the amount of information available, | |
1169 | * irrespective of how much we may have copied (including the terminal NUL) | |
1170 | * - implements keyctl(KEYCTL_GET_SECURITY) | |
1171 | */ | |
1172 | long keyctl_get_security(key_serial_t keyid, | |
1173 | char __user *buffer, | |
1174 | size_t buflen) | |
1175 | { | |
1176 | struct key *key, *instkey; | |
1177 | key_ref_t key_ref; | |
1178 | char *context; | |
1179 | long ret; | |
1180 | ||
5593122e | 1181 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW); |
70a5bb72 DH |
1182 | if (IS_ERR(key_ref)) { |
1183 | if (PTR_ERR(key_ref) != -EACCES) | |
1184 | return PTR_ERR(key_ref); | |
1185 | ||
1186 | /* viewing a key under construction is also permitted if we | |
1187 | * have the authorisation token handy */ | |
1188 | instkey = key_get_instantiation_authkey(keyid); | |
1189 | if (IS_ERR(instkey)) | |
1190 | return PTR_ERR(key_ref); | |
1191 | key_put(instkey); | |
1192 | ||
5593122e | 1193 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); |
70a5bb72 DH |
1194 | if (IS_ERR(key_ref)) |
1195 | return PTR_ERR(key_ref); | |
1196 | } | |
1197 | ||
1198 | key = key_ref_to_ptr(key_ref); | |
1199 | ret = security_key_getsecurity(key, &context); | |
1200 | if (ret == 0) { | |
1201 | /* if no information was returned, give userspace an empty | |
1202 | * string */ | |
1203 | ret = 1; | |
1204 | if (buffer && buflen > 0 && | |
1205 | copy_to_user(buffer, "", 1) != 0) | |
1206 | ret = -EFAULT; | |
1207 | } else if (ret > 0) { | |
1208 | /* return as much data as there's room for */ | |
1209 | if (buffer && buflen > 0) { | |
1210 | if (buflen > ret) | |
1211 | buflen = ret; | |
1212 | ||
1213 | if (copy_to_user(buffer, context, buflen) != 0) | |
1214 | ret = -EFAULT; | |
1215 | } | |
1216 | ||
1217 | kfree(context); | |
1218 | } | |
1219 | ||
1220 | key_ref_put(key_ref); | |
1221 | return ret; | |
1222 | } | |
1223 | ||
1da177e4 LT |
1224 | /*****************************************************************************/ |
1225 | /* | |
1226 | * the key control system call | |
1227 | */ | |
938bb9f5 HC |
1228 | SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, |
1229 | unsigned long, arg4, unsigned long, arg5) | |
1da177e4 LT |
1230 | { |
1231 | switch (option) { | |
1232 | case KEYCTL_GET_KEYRING_ID: | |
1233 | return keyctl_get_keyring_ID((key_serial_t) arg2, | |
1234 | (int) arg3); | |
1235 | ||
1236 | case KEYCTL_JOIN_SESSION_KEYRING: | |
1237 | return keyctl_join_session_keyring((const char __user *) arg2); | |
1238 | ||
1239 | case KEYCTL_UPDATE: | |
1240 | return keyctl_update_key((key_serial_t) arg2, | |
1241 | (const void __user *) arg3, | |
1242 | (size_t) arg4); | |
1243 | ||
1244 | case KEYCTL_REVOKE: | |
1245 | return keyctl_revoke_key((key_serial_t) arg2); | |
1246 | ||
1247 | case KEYCTL_DESCRIBE: | |
1248 | return keyctl_describe_key((key_serial_t) arg2, | |
1249 | (char __user *) arg3, | |
1250 | (unsigned) arg4); | |
1251 | ||
1252 | case KEYCTL_CLEAR: | |
1253 | return keyctl_keyring_clear((key_serial_t) arg2); | |
1254 | ||
1255 | case KEYCTL_LINK: | |
1256 | return keyctl_keyring_link((key_serial_t) arg2, | |
1257 | (key_serial_t) arg3); | |
1258 | ||
1259 | case KEYCTL_UNLINK: | |
1260 | return keyctl_keyring_unlink((key_serial_t) arg2, | |
1261 | (key_serial_t) arg3); | |
1262 | ||
1263 | case KEYCTL_SEARCH: | |
1264 | return keyctl_keyring_search((key_serial_t) arg2, | |
1265 | (const char __user *) arg3, | |
1266 | (const char __user *) arg4, | |
1267 | (key_serial_t) arg5); | |
1268 | ||
1269 | case KEYCTL_READ: | |
1270 | return keyctl_read_key((key_serial_t) arg2, | |
1271 | (char __user *) arg3, | |
1272 | (size_t) arg4); | |
1273 | ||
1274 | case KEYCTL_CHOWN: | |
1275 | return keyctl_chown_key((key_serial_t) arg2, | |
1276 | (uid_t) arg3, | |
1277 | (gid_t) arg4); | |
1278 | ||
1279 | case KEYCTL_SETPERM: | |
1280 | return keyctl_setperm_key((key_serial_t) arg2, | |
1281 | (key_perm_t) arg3); | |
1282 | ||
1283 | case KEYCTL_INSTANTIATE: | |
1284 | return keyctl_instantiate_key((key_serial_t) arg2, | |
1285 | (const void __user *) arg3, | |
1286 | (size_t) arg4, | |
1287 | (key_serial_t) arg5); | |
1288 | ||
1289 | case KEYCTL_NEGATE: | |
1290 | return keyctl_negate_key((key_serial_t) arg2, | |
1291 | (unsigned) arg3, | |
1292 | (key_serial_t) arg4); | |
1293 | ||
3e30148c DH |
1294 | case KEYCTL_SET_REQKEY_KEYRING: |
1295 | return keyctl_set_reqkey_keyring(arg2); | |
1296 | ||
017679c4 DH |
1297 | case KEYCTL_SET_TIMEOUT: |
1298 | return keyctl_set_timeout((key_serial_t) arg2, | |
1299 | (unsigned) arg3); | |
1300 | ||
b5f545c8 DH |
1301 | case KEYCTL_ASSUME_AUTHORITY: |
1302 | return keyctl_assume_authority((key_serial_t) arg2); | |
1303 | ||
70a5bb72 DH |
1304 | case KEYCTL_GET_SECURITY: |
1305 | return keyctl_get_security((key_serial_t) arg2, | |
90bd49ab | 1306 | (char __user *) arg3, |
70a5bb72 DH |
1307 | (size_t) arg4); |
1308 | ||
1da177e4 LT |
1309 | default: |
1310 | return -EOPNOTSUPP; | |
1311 | } | |
1312 | ||
1313 | } /* end sys_keyctl() */ |