x86, mce, AMD: Fix leaving freed data in a list
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / security / keys / request_key.c
blob9ac7bfd3bbdd386fedd677077f9ac834fd643c17
1 /* Request a key from userspace
3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 * See Documentation/keys-request-key.txt
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kmod.h>
17 #include <linux/err.h>
18 #include <linux/keyctl.h>
19 #include <linux/slab.h>
20 #include "internal.h"
22 #define key_negative_timeout 60 /* default timeout on a negative key's existence */
25 * wait_on_bit() sleep function for uninterruptible waiting
27 static int key_wait_bit(void *flags)
29 schedule();
30 return 0;
34 * wait_on_bit() sleep function for interruptible waiting
36 static int key_wait_bit_intr(void *flags)
38 schedule();
39 return signal_pending(current) ? -ERESTARTSYS : 0;
43 * call to complete the construction of a key
45 void complete_request_key(struct key_construction *cons, int error)
47 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
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);
55 key_put(cons->key);
56 key_put(cons->authkey);
57 kfree(cons);
59 EXPORT_SYMBOL(complete_request_key);
62 * request userspace finish the construction of a key
63 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
65 static int call_sbin_request_key(struct key_construction *cons,
66 const char *op,
67 void *aux)
69 const struct cred *cred = current_cred();
70 key_serial_t prkey, sskey;
71 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
72 char *argv[9], *envp[3], uid_str[12], gid_str[12];
73 char key_str[12], keyring_str[3][12];
74 char desc[20];
75 int ret, i;
77 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
79 ret = install_user_keyrings();
80 if (ret < 0)
81 goto error_alloc;
83 /* allocate a new session keyring */
84 sprintf(desc, "_req.%u", key->serial);
86 cred = get_current_cred();
87 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
88 KEY_ALLOC_QUOTA_OVERRUN, NULL);
89 put_cred(cred);
90 if (IS_ERR(keyring)) {
91 ret = PTR_ERR(keyring);
92 goto error_alloc;
95 /* attach the auth key to the session keyring */
96 ret = __key_link(keyring, authkey);
97 if (ret < 0)
98 goto error_link;
100 /* record the UID and GID */
101 sprintf(uid_str, "%d", cred->fsuid);
102 sprintf(gid_str, "%d", cred->fsgid);
104 /* we say which key is under construction */
105 sprintf(key_str, "%d", key->serial);
107 /* we specify the process's default keyrings */
108 sprintf(keyring_str[0], "%d",
109 cred->thread_keyring ? cred->thread_keyring->serial : 0);
111 prkey = 0;
112 if (cred->tgcred->process_keyring)
113 prkey = cred->tgcred->process_keyring->serial;
115 if (cred->tgcred->session_keyring)
116 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
117 else
118 sskey = cred->user->session_keyring->serial;
120 sprintf(keyring_str[2], "%d", sskey);
122 /* set up a minimal environment */
123 i = 0;
124 envp[i++] = "HOME=/";
125 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
126 envp[i] = NULL;
128 /* set up the argument list */
129 i = 0;
130 argv[i++] = "/sbin/request-key";
131 argv[i++] = (char *) op;
132 argv[i++] = key_str;
133 argv[i++] = uid_str;
134 argv[i++] = gid_str;
135 argv[i++] = keyring_str[0];
136 argv[i++] = keyring_str[1];
137 argv[i++] = keyring_str[2];
138 argv[i] = NULL;
140 /* do it */
141 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
142 UMH_WAIT_PROC);
143 kdebug("usermode -> 0x%x", ret);
144 if (ret >= 0) {
145 /* ret is the exit/wait code */
146 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
147 key_validate(key) < 0)
148 ret = -ENOKEY;
149 else
150 /* ignore any errors from userspace if the key was
151 * instantiated */
152 ret = 0;
155 error_link:
156 key_put(keyring);
158 error_alloc:
159 complete_request_key(cons, ret);
160 kleave(" = %d", ret);
161 return ret;
165 * call out to userspace for key construction
166 * - we ignore program failure and go on key status instead
168 static int construct_key(struct key *key, const void *callout_info,
169 size_t callout_len, void *aux,
170 struct key *dest_keyring)
172 struct key_construction *cons;
173 request_key_actor_t actor;
174 struct key *authkey;
175 int ret;
177 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
179 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
180 if (!cons)
181 return -ENOMEM;
183 /* allocate an authorisation key */
184 authkey = request_key_auth_new(key, callout_info, callout_len,
185 dest_keyring);
186 if (IS_ERR(authkey)) {
187 kfree(cons);
188 ret = PTR_ERR(authkey);
189 authkey = NULL;
190 } else {
191 cons->authkey = key_get(authkey);
192 cons->key = key_get(key);
194 /* make the call */
195 actor = call_sbin_request_key;
196 if (key->type->request_key)
197 actor = key->type->request_key;
199 ret = actor(cons, "create", aux);
201 /* check that the actor called complete_request_key() prior to
202 * returning an error */
203 WARN_ON(ret < 0 &&
204 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
205 key_put(authkey);
208 kleave(" = %d", ret);
209 return ret;
213 * get the appropriate destination keyring for the request
214 * - we return whatever keyring we select with an extra reference upon it which
215 * the caller must release
217 static void construct_get_dest_keyring(struct key **_dest_keyring)
219 struct request_key_auth *rka;
220 const struct cred *cred = current_cred();
221 struct key *dest_keyring = *_dest_keyring, *authkey;
223 kenter("%p", dest_keyring);
225 /* find the appropriate keyring */
226 if (dest_keyring) {
227 /* the caller supplied one */
228 key_get(dest_keyring);
229 } else {
230 /* use a default keyring; falling through the cases until we
231 * find one that we actually have */
232 switch (cred->jit_keyring) {
233 case KEY_REQKEY_DEFL_DEFAULT:
234 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
235 if (cred->request_key_auth) {
236 authkey = cred->request_key_auth;
237 down_read(&authkey->sem);
238 rka = authkey->payload.data;
239 if (!test_bit(KEY_FLAG_REVOKED,
240 &authkey->flags))
241 dest_keyring =
242 key_get(rka->dest_keyring);
243 up_read(&authkey->sem);
244 if (dest_keyring)
245 break;
248 case KEY_REQKEY_DEFL_THREAD_KEYRING:
249 dest_keyring = key_get(cred->thread_keyring);
250 if (dest_keyring)
251 break;
253 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
254 dest_keyring = key_get(cred->tgcred->process_keyring);
255 if (dest_keyring)
256 break;
258 case KEY_REQKEY_DEFL_SESSION_KEYRING:
259 rcu_read_lock();
260 dest_keyring = key_get(
261 rcu_dereference(cred->tgcred->session_keyring));
262 rcu_read_unlock();
264 if (dest_keyring)
265 break;
267 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
268 dest_keyring =
269 key_get(cred->user->session_keyring);
270 break;
272 case KEY_REQKEY_DEFL_USER_KEYRING:
273 dest_keyring = key_get(cred->user->uid_keyring);
274 break;
276 case KEY_REQKEY_DEFL_GROUP_KEYRING:
277 default:
278 BUG();
282 *_dest_keyring = dest_keyring;
283 kleave(" [dk %d]", key_serial(dest_keyring));
284 return;
288 * allocate a new key in under-construction state and attempt to link it in to
289 * the requested place
290 * - may return a key that's already under construction instead
292 static int construct_alloc_key(struct key_type *type,
293 const char *description,
294 struct key *dest_keyring,
295 unsigned long flags,
296 struct key_user *user,
297 struct key **_key)
299 const struct cred *cred = current_cred();
300 struct key *key;
301 key_ref_t key_ref;
303 kenter("%s,%s,,,", type->name, description);
305 mutex_lock(&user->cons_lock);
307 key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
308 KEY_POS_ALL, flags);
309 if (IS_ERR(key))
310 goto alloc_failed;
312 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
314 if (dest_keyring)
315 down_write(&dest_keyring->sem);
317 /* attach the key to the destination keyring under lock, but we do need
318 * to do another check just in case someone beat us to it whilst we
319 * waited for locks */
320 mutex_lock(&key_construction_mutex);
322 key_ref = search_process_keyrings(type, description, type->match, cred);
323 if (!IS_ERR(key_ref))
324 goto key_already_present;
326 if (dest_keyring)
327 __key_link(dest_keyring, key);
329 mutex_unlock(&key_construction_mutex);
330 if (dest_keyring)
331 up_write(&dest_keyring->sem);
332 mutex_unlock(&user->cons_lock);
333 *_key = key;
334 kleave(" = 0 [%d]", key_serial(key));
335 return 0;
337 key_already_present:
338 mutex_unlock(&key_construction_mutex);
339 if (dest_keyring) {
340 __key_link(dest_keyring, key_ref_to_ptr(key_ref));
341 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;
349 alloc_failed:
350 mutex_unlock(&user->cons_lock);
351 *_key = NULL;
352 kleave(" = %ld", PTR_ERR(key));
353 return PTR_ERR(key);
357 * commence key construction
359 static struct key *construct_key_and_link(struct key_type *type,
360 const char *description,
361 const char *callout_info,
362 size_t callout_len,
363 void *aux,
364 struct key *dest_keyring,
365 unsigned long flags)
367 struct key_user *user;
368 struct key *key;
369 int ret;
371 kenter("");
373 user = key_user_lookup(current_fsuid(), current_user_ns());
374 if (!user)
375 return ERR_PTR(-ENOMEM);
377 construct_get_dest_keyring(&dest_keyring);
379 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
380 &key);
381 key_user_put(user);
383 if (ret == 0) {
384 ret = construct_key(key, callout_info, callout_len, aux,
385 dest_keyring);
386 if (ret < 0) {
387 kdebug("cons failed");
388 goto construction_failed;
392 key_put(dest_keyring);
393 kleave(" = key %d", key_serial(key));
394 return key;
396 construction_failed:
397 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
398 key_put(key);
399 key_put(dest_keyring);
400 kleave(" = %d", ret);
401 return ERR_PTR(ret);
405 * request a key
406 * - search the process's keyrings
407 * - check the list of keys being created or updated
408 * - call out to userspace for a key if supplementary info was provided
409 * - cache the key in an appropriate keyring
411 struct key *request_key_and_link(struct key_type *type,
412 const char *description,
413 const void *callout_info,
414 size_t callout_len,
415 void *aux,
416 struct key *dest_keyring,
417 unsigned long flags)
419 const struct cred *cred = current_cred();
420 struct key *key;
421 key_ref_t key_ref;
423 kenter("%s,%s,%p,%zu,%p,%p,%lx",
424 type->name, description, callout_info, callout_len, aux,
425 dest_keyring, flags);
427 /* search all the process keyrings for a key */
428 key_ref = search_process_keyrings(type, description, type->match,
429 cred);
431 if (!IS_ERR(key_ref)) {
432 key = key_ref_to_ptr(key_ref);
433 if (dest_keyring) {
434 construct_get_dest_keyring(&dest_keyring);
435 key_link(dest_keyring, key);
436 key_put(dest_keyring);
438 } else if (PTR_ERR(key_ref) != -EAGAIN) {
439 key = ERR_CAST(key_ref);
440 } else {
441 /* the search failed, but the keyrings were searchable, so we
442 * should consult userspace if we can */
443 key = ERR_PTR(-ENOKEY);
444 if (!callout_info)
445 goto error;
447 key = construct_key_and_link(type, description, callout_info,
448 callout_len, aux, dest_keyring,
449 flags);
452 error:
453 kleave(" = %p", key);
454 return key;
458 * wait for construction of a key to complete
460 int wait_for_key_construction(struct key *key, bool intr)
462 int ret;
464 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
465 intr ? key_wait_bit_intr : key_wait_bit,
466 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
467 if (ret < 0)
468 return ret;
469 return key_validate(key);
471 EXPORT_SYMBOL(wait_for_key_construction);
474 * request a key
475 * - search the process's keyrings
476 * - check the list of keys being created or updated
477 * - call out to userspace for a key if supplementary info was provided
478 * - waits uninterruptible for creation to complete
480 struct key *request_key(struct key_type *type,
481 const char *description,
482 const char *callout_info)
484 struct key *key;
485 size_t callout_len = 0;
486 int ret;
488 if (callout_info)
489 callout_len = strlen(callout_info);
490 key = request_key_and_link(type, description, callout_info, callout_len,
491 NULL, NULL, KEY_ALLOC_IN_QUOTA);
492 if (!IS_ERR(key)) {
493 ret = wait_for_key_construction(key, false);
494 if (ret < 0) {
495 key_put(key);
496 return ERR_PTR(ret);
499 return key;
501 EXPORT_SYMBOL(request_key);
504 * request a key with auxiliary data for the upcaller
505 * - search the process's keyrings
506 * - check the list of keys being created or updated
507 * - call out to userspace for a key if supplementary info was provided
508 * - waits uninterruptible for creation to complete
510 struct key *request_key_with_auxdata(struct key_type *type,
511 const char *description,
512 const void *callout_info,
513 size_t callout_len,
514 void *aux)
516 struct key *key;
517 int ret;
519 key = request_key_and_link(type, description, callout_info, callout_len,
520 aux, NULL, KEY_ALLOC_IN_QUOTA);
521 if (!IS_ERR(key)) {
522 ret = wait_for_key_construction(key, false);
523 if (ret < 0) {
524 key_put(key);
525 return ERR_PTR(ret);
528 return key;
530 EXPORT_SYMBOL(request_key_with_auxdata);
533 * request a key (allow async construction)
534 * - search the process's keyrings
535 * - check the list of keys being created or updated
536 * - call out to userspace for a key if supplementary info was provided
538 struct key *request_key_async(struct key_type *type,
539 const char *description,
540 const void *callout_info,
541 size_t callout_len)
543 return request_key_and_link(type, description, callout_info,
544 callout_len, NULL, NULL,
545 KEY_ALLOC_IN_QUOTA);
547 EXPORT_SYMBOL(request_key_async);
550 * request a key with auxiliary data for the upcaller (allow async construction)
551 * - search the process's keyrings
552 * - check the list of keys being created or updated
553 * - call out to userspace for a key if supplementary info was provided
555 struct key *request_key_async_with_auxdata(struct key_type *type,
556 const char *description,
557 const void *callout_info,
558 size_t callout_len,
559 void *aux)
561 return request_key_and_link(type, description, callout_info,
562 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
564 EXPORT_SYMBOL(request_key_async_with_auxdata);