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