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