]> bbs.cooldavid.org Git - net-next-2.6.git/blame - security/keys/request_key.c
CRED: Separate task security context from task_struct
[net-next-2.6.git] / security / keys / request_key.c
CommitLineData
76181c13 1/* Request a key from userspace
1da177e4 2 *
76181c13 3 * Copyright (C) 2004-2007 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.
f1a9badc
DH
10 *
11 * See Documentation/keys-request-key.txt
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/kmod.h>
17#include <linux/err.h>
3e30148c 18#include <linux/keyctl.h>
fdb89bce 19#include <linux/slab.h>
1da177e4
LT
20#include "internal.h"
21
e9e349b0
DH
22#define key_negative_timeout 60 /* default timeout on a negative key's existence */
23
76181c13
DH
24/*
25 * wait_on_bit() sleep function for uninterruptible waiting
26 */
27static int key_wait_bit(void *flags)
28{
29 schedule();
30 return 0;
31}
32
33/*
34 * wait_on_bit() sleep function for interruptible waiting
35 */
36static int key_wait_bit_intr(void *flags)
37{
38 schedule();
39 return signal_pending(current) ? -ERESTARTSYS : 0;
40}
41
42/*
43 * call to complete the construction of a key
44 */
45void complete_request_key(struct key_construction *cons, int error)
46{
47 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
1da177e4 48
76181c13
DH
49 if (error < 0)
50 key_negate_and_link(cons->key, key_negative_timeout, NULL,
51 cons->authkey);
52 else
53 key_revoke(cons->authkey);
54
55 key_put(cons->key);
56 key_put(cons->authkey);
57 kfree(cons);
58}
59EXPORT_SYMBOL(complete_request_key);
1da177e4 60
1da177e4
LT
61/*
62 * request userspace finish the construction of a key
b5f545c8 63 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
1da177e4 64 */
76181c13 65static int call_sbin_request_key(struct key_construction *cons,
4e54f085
DH
66 const char *op,
67 void *aux)
1da177e4
LT
68{
69 struct task_struct *tsk = current;
1da177e4 70 key_serial_t prkey, sskey;
76181c13 71 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
b5f545c8 72 char *argv[9], *envp[3], uid_str[12], gid_str[12];
1da177e4 73 char key_str[12], keyring_str[3][12];
b5f545c8 74 char desc[20];
3e30148c
DH
75 int ret, i;
76
b5f545c8 77 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
3e30148c 78
8bbf4976
DH
79 ret = install_user_keyrings();
80 if (ret < 0)
81 goto error_alloc;
82
b5f545c8
DH
83 /* allocate a new session keyring */
84 sprintf(desc, "_req.%u", key->serial);
85
47d804bf 86 keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
7e047ef5 87 KEY_ALLOC_QUOTA_OVERRUN, NULL);
b5f545c8
DH
88 if (IS_ERR(keyring)) {
89 ret = PTR_ERR(keyring);
90 goto error_alloc;
3e30148c 91 }
1da177e4 92
b5f545c8
DH
93 /* attach the auth key to the session keyring */
94 ret = __key_link(keyring, authkey);
95 if (ret < 0)
96 goto error_link;
97
1da177e4 98 /* record the UID and GID */
47d804bf
DH
99 sprintf(uid_str, "%d", current_fsuid());
100 sprintf(gid_str, "%d", current_fsgid());
1da177e4
LT
101
102 /* we say which key is under construction */
103 sprintf(key_str, "%d", key->serial);
104
105 /* we specify the process's default keyrings */
106 sprintf(keyring_str[0], "%d",
b6dff3ec
DH
107 tsk->cred->thread_keyring ?
108 tsk->cred->thread_keyring->serial : 0);
1da177e4
LT
109
110 prkey = 0;
111 if (tsk->signal->process_keyring)
112 prkey = tsk->signal->process_keyring->serial;
113
3e30148c 114 sprintf(keyring_str[1], "%d", prkey);
1da177e4 115
3e30148c
DH
116 if (tsk->signal->session_keyring) {
117 rcu_read_lock();
118 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
119 rcu_read_unlock();
76181c13 120 } else {
b6dff3ec 121 sskey = tsk->cred->user->session_keyring->serial;
3e30148c 122 }
1da177e4 123
1da177e4
LT
124 sprintf(keyring_str[2], "%d", sskey);
125
126 /* set up a minimal environment */
127 i = 0;
128 envp[i++] = "HOME=/";
129 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
130 envp[i] = NULL;
131
132 /* set up the argument list */
133 i = 0;
134 argv[i++] = "/sbin/request-key";
135 argv[i++] = (char *) op;
136 argv[i++] = key_str;
137 argv[i++] = uid_str;
138 argv[i++] = gid_str;
139 argv[i++] = keyring_str[0];
140 argv[i++] = keyring_str[1];
141 argv[i++] = keyring_str[2];
1da177e4
LT
142 argv[i] = NULL;
143
144 /* do it */
86313c48
JF
145 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
146 UMH_WAIT_PROC);
76181c13
DH
147 kdebug("usermode -> 0x%x", ret);
148 if (ret >= 0) {
149 /* ret is the exit/wait code */
150 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
151 key_validate(key) < 0)
152 ret = -ENOKEY;
153 else
154 /* ignore any errors from userspace if the key was
155 * instantiated */
156 ret = 0;
157 }
3e30148c 158
b5f545c8
DH
159error_link:
160 key_put(keyring);
3e30148c 161
b5f545c8 162error_alloc:
3e30148c 163 kleave(" = %d", ret);
76181c13 164 complete_request_key(cons, ret);
3e30148c 165 return ret;
76181c13 166}
1da177e4 167
1da177e4 168/*
76181c13 169 * call out to userspace for key construction
1da177e4
LT
170 * - we ignore program failure and go on key status instead
171 */
4a38e122 172static int construct_key(struct key *key, const void *callout_info,
8bbf4976
DH
173 size_t callout_len, void *aux,
174 struct key *dest_keyring)
1da177e4 175{
76181c13 176 struct key_construction *cons;
b5f545c8 177 request_key_actor_t actor;
76181c13
DH
178 struct key *authkey;
179 int ret;
1da177e4 180
4a38e122 181 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
3e30148c 182
76181c13
DH
183 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
184 if (!cons)
185 return -ENOMEM;
1da177e4 186
b5f545c8 187 /* allocate an authorisation key */
8bbf4976
DH
188 authkey = request_key_auth_new(key, callout_info, callout_len,
189 dest_keyring);
b5f545c8 190 if (IS_ERR(authkey)) {
76181c13 191 kfree(cons);
b5f545c8
DH
192 ret = PTR_ERR(authkey);
193 authkey = NULL;
76181c13
DH
194 } else {
195 cons->authkey = key_get(authkey);
196 cons->key = key_get(key);
197
198 /* make the call */
199 actor = call_sbin_request_key;
200 if (key->type->request_key)
201 actor = key->type->request_key;
202
203 ret = actor(cons, "create", aux);
204
205 /* check that the actor called complete_request_key() prior to
206 * returning an error */
207 WARN_ON(ret < 0 &&
208 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
209 key_put(authkey);
1da177e4
LT
210 }
211
76181c13
DH
212 kleave(" = %d", ret);
213 return ret;
214}
1da177e4 215
3e30148c 216/*
8bbf4976
DH
217 * get the appropriate destination keyring for the request
218 * - we return whatever keyring we select with an extra reference upon it which
219 * the caller must release
3e30148c 220 */
8bbf4976 221static void construct_get_dest_keyring(struct key **_dest_keyring)
3e30148c 222{
8bbf4976 223 struct request_key_auth *rka;
3e30148c 224 struct task_struct *tsk = current;
8bbf4976 225 struct key *dest_keyring = *_dest_keyring, *authkey;
3e30148c 226
8bbf4976 227 kenter("%p", dest_keyring);
3e30148c
DH
228
229 /* find the appropriate keyring */
8bbf4976
DH
230 if (dest_keyring) {
231 /* the caller supplied one */
232 key_get(dest_keyring);
233 } else {
234 /* use a default keyring; falling through the cases until we
235 * find one that we actually have */
b6dff3ec 236 switch (tsk->cred->jit_keyring) {
3e30148c 237 case KEY_REQKEY_DEFL_DEFAULT:
8bbf4976 238 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
b6dff3ec
DH
239 if (tsk->cred->request_key_auth) {
240 authkey = tsk->cred->request_key_auth;
8bbf4976
DH
241 down_read(&authkey->sem);
242 rka = authkey->payload.data;
243 if (!test_bit(KEY_FLAG_REVOKED,
244 &authkey->flags))
245 dest_keyring =
246 key_get(rka->dest_keyring);
247 up_read(&authkey->sem);
248 if (dest_keyring)
249 break;
250 }
251
3e30148c 252 case KEY_REQKEY_DEFL_THREAD_KEYRING:
b6dff3ec 253 dest_keyring = key_get(tsk->cred->thread_keyring);
3e30148c
DH
254 if (dest_keyring)
255 break;
256
257 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
8bbf4976 258 dest_keyring = key_get(tsk->signal->process_keyring);
3e30148c
DH
259 if (dest_keyring)
260 break;
261
262 case KEY_REQKEY_DEFL_SESSION_KEYRING:
263 rcu_read_lock();
264 dest_keyring = key_get(
265 rcu_dereference(tsk->signal->session_keyring));
266 rcu_read_unlock();
3e30148c
DH
267
268 if (dest_keyring)
269 break;
270
271 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
b6dff3ec
DH
272 dest_keyring =
273 key_get(tsk->cred->user->session_keyring);
3e30148c
DH
274 break;
275
276 case KEY_REQKEY_DEFL_USER_KEYRING:
b6dff3ec 277 dest_keyring = key_get(tsk->cred->user->uid_keyring);
3e30148c
DH
278 break;
279
280 case KEY_REQKEY_DEFL_GROUP_KEYRING:
281 default:
282 BUG();
283 }
284 }
285
8bbf4976
DH
286 *_dest_keyring = dest_keyring;
287 kleave(" [dk %d]", key_serial(dest_keyring));
288 return;
76181c13 289}
3e30148c 290
76181c13
DH
291/*
292 * allocate a new key in under-construction state and attempt to link it in to
293 * the requested place
294 * - may return a key that's already under construction instead
295 */
296static int construct_alloc_key(struct key_type *type,
297 const char *description,
298 struct key *dest_keyring,
299 unsigned long flags,
300 struct key_user *user,
301 struct key **_key)
302{
303 struct key *key;
304 key_ref_t key_ref;
305
306 kenter("%s,%s,,,", type->name, description);
307
308 mutex_lock(&user->cons_lock);
309
310 key = key_alloc(type, description,
47d804bf 311 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
76181c13
DH
312 flags);
313 if (IS_ERR(key))
314 goto alloc_failed;
315
316 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
317
8bbf4976 318 down_write(&dest_keyring->sem);
76181c13
DH
319
320 /* attach the key to the destination keyring under lock, but we do need
321 * to do another check just in case someone beat us to it whilst we
322 * waited for locks */
323 mutex_lock(&key_construction_mutex);
324
325 key_ref = search_process_keyrings(type, description, type->match,
326 current);
327 if (!IS_ERR(key_ref))
328 goto key_already_present;
329
8bbf4976 330 __key_link(dest_keyring, key);
76181c13
DH
331
332 mutex_unlock(&key_construction_mutex);
8bbf4976 333 up_write(&dest_keyring->sem);
76181c13
DH
334 mutex_unlock(&user->cons_lock);
335 *_key = key;
336 kleave(" = 0 [%d]", key_serial(key));
337 return 0;
338
339key_already_present:
340 mutex_unlock(&key_construction_mutex);
341 if (dest_keyring)
342 up_write(&dest_keyring->sem);
343 mutex_unlock(&user->cons_lock);
344 key_put(key);
345 *_key = key = key_ref_to_ptr(key_ref);
346 kleave(" = -EINPROGRESS [%d]", key_serial(key));
347 return -EINPROGRESS;
348
349alloc_failed:
350 mutex_unlock(&user->cons_lock);
351 *_key = NULL;
352 kleave(" = %ld", PTR_ERR(key));
353 return PTR_ERR(key);
354}
355
356/*
357 * commence key construction
358 */
359static struct key *construct_key_and_link(struct key_type *type,
360 const char *description,
361 const char *callout_info,
4a38e122 362 size_t callout_len,
76181c13
DH
363 void *aux,
364 struct key *dest_keyring,
365 unsigned long flags)
366{
367 struct key_user *user;
368 struct key *key;
369 int ret;
370
47d804bf 371 user = key_user_lookup(current_fsuid());
76181c13
DH
372 if (!user)
373 return ERR_PTR(-ENOMEM);
374
8bbf4976
DH
375 construct_get_dest_keyring(&dest_keyring);
376
76181c13
DH
377 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
378 &key);
379 key_user_put(user);
380
381 if (ret == 0) {
8bbf4976
DH
382 ret = construct_key(key, callout_info, callout_len, aux,
383 dest_keyring);
76181c13
DH
384 if (ret < 0)
385 goto construction_failed;
386 }
387
8bbf4976 388 key_put(dest_keyring);
76181c13
DH
389 return key;
390
391construction_failed:
392 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
393 key_put(key);
8bbf4976 394 key_put(dest_keyring);
76181c13
DH
395 return ERR_PTR(ret);
396}
3e30148c 397
1da177e4
LT
398/*
399 * request a key
400 * - search the process's keyrings
401 * - check the list of keys being created or updated
3e30148c
DH
402 * - call out to userspace for a key if supplementary info was provided
403 * - cache the key in an appropriate keyring
1da177e4 404 */
3e30148c
DH
405struct key *request_key_and_link(struct key_type *type,
406 const char *description,
4a38e122
DH
407 const void *callout_info,
408 size_t callout_len,
4e54f085 409 void *aux,
7e047ef5
DH
410 struct key *dest_keyring,
411 unsigned long flags)
1da177e4 412{
1da177e4 413 struct key *key;
664cceb0 414 key_ref_t key_ref;
1da177e4 415
4a38e122
DH
416 kenter("%s,%s,%p,%zu,%p,%p,%lx",
417 type->name, description, callout_info, callout_len, aux,
4e54f085 418 dest_keyring, flags);
3e30148c 419
1da177e4 420 /* search all the process keyrings for a key */
664cceb0
DH
421 key_ref = search_process_keyrings(type, description, type->match,
422 current);
1da177e4 423
664cceb0
DH
424 if (!IS_ERR(key_ref)) {
425 key = key_ref_to_ptr(key_ref);
76181c13 426 } else if (PTR_ERR(key_ref) != -EAGAIN) {
e231c2ee 427 key = ERR_CAST(key_ref);
76181c13 428 } else {
1da177e4
LT
429 /* the search failed, but the keyrings were searchable, so we
430 * should consult userspace if we can */
431 key = ERR_PTR(-ENOKEY);
432 if (!callout_info)
433 goto error;
434
76181c13 435 key = construct_key_and_link(type, description, callout_info,
4a38e122
DH
436 callout_len, aux, dest_keyring,
437 flags);
1da177e4
LT
438 }
439
3e30148c
DH
440error:
441 kleave(" = %p", key);
1da177e4 442 return key;
76181c13 443}
1da177e4 444
76181c13
DH
445/*
446 * wait for construction of a key to complete
447 */
448int wait_for_key_construction(struct key *key, bool intr)
449{
450 int ret;
3e30148c 451
76181c13
DH
452 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
453 intr ? key_wait_bit_intr : key_wait_bit,
454 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
455 if (ret < 0)
456 return ret;
457 return key_validate(key);
458}
459EXPORT_SYMBOL(wait_for_key_construction);
3e30148c 460
3e30148c
DH
461/*
462 * request a key
463 * - search the process's keyrings
464 * - check the list of keys being created or updated
465 * - call out to userspace for a key if supplementary info was provided
76181c13 466 * - waits uninterruptible for creation to complete
3e30148c
DH
467 */
468struct key *request_key(struct key_type *type,
469 const char *description,
470 const char *callout_info)
471{
76181c13 472 struct key *key;
4a38e122 473 size_t callout_len = 0;
76181c13
DH
474 int ret;
475
4a38e122
DH
476 if (callout_info)
477 callout_len = strlen(callout_info);
478 key = request_key_and_link(type, description, callout_info, callout_len,
479 NULL, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
480 if (!IS_ERR(key)) {
481 ret = wait_for_key_construction(key, false);
482 if (ret < 0) {
483 key_put(key);
484 return ERR_PTR(ret);
485 }
486 }
487 return key;
488}
1da177e4 489EXPORT_SYMBOL(request_key);
4e54f085 490
4e54f085
DH
491/*
492 * request a key with auxiliary data for the upcaller
493 * - search the process's keyrings
494 * - check the list of keys being created or updated
495 * - call out to userspace for a key if supplementary info was provided
76181c13 496 * - waits uninterruptible for creation to complete
4e54f085
DH
497 */
498struct key *request_key_with_auxdata(struct key_type *type,
499 const char *description,
4a38e122
DH
500 const void *callout_info,
501 size_t callout_len,
4e54f085
DH
502 void *aux)
503{
76181c13
DH
504 struct key *key;
505 int ret;
506
4a38e122
DH
507 key = request_key_and_link(type, description, callout_info, callout_len,
508 aux, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
509 if (!IS_ERR(key)) {
510 ret = wait_for_key_construction(key, false);
511 if (ret < 0) {
512 key_put(key);
513 return ERR_PTR(ret);
514 }
515 }
516 return key;
517}
518EXPORT_SYMBOL(request_key_with_auxdata);
4e54f085 519
76181c13
DH
520/*
521 * request a key (allow async construction)
522 * - search the process's keyrings
523 * - check the list of keys being created or updated
524 * - call out to userspace for a key if supplementary info was provided
525 */
526struct key *request_key_async(struct key_type *type,
527 const char *description,
4a38e122
DH
528 const void *callout_info,
529 size_t callout_len)
76181c13 530{
4a38e122
DH
531 return request_key_and_link(type, description, callout_info,
532 callout_len, NULL, NULL,
533 KEY_ALLOC_IN_QUOTA);
76181c13
DH
534}
535EXPORT_SYMBOL(request_key_async);
4e54f085 536
76181c13
DH
537/*
538 * request a key with auxiliary data for the upcaller (allow async construction)
539 * - search the process's keyrings
540 * - check the list of keys being created or updated
541 * - call out to userspace for a key if supplementary info was provided
542 */
543struct key *request_key_async_with_auxdata(struct key_type *type,
544 const char *description,
4a38e122
DH
545 const void *callout_info,
546 size_t callout_len,
76181c13
DH
547 void *aux)
548{
4a38e122
DH
549 return request_key_and_link(type, description, callout_info,
550 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
551}
552EXPORT_SYMBOL(request_key_async_with_auxdata);