sched: fix defined-but-unused warning
[linux-2.6.git] / security / keys / request_key.c
blobba32ca6469bdf2e74a8b0f9cb94d27604b7251af
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"
23 * wait_on_bit() sleep function for uninterruptible waiting
25 static int key_wait_bit(void *flags)
27 schedule();
28 return 0;
32 * wait_on_bit() sleep function for interruptible waiting
34 static int key_wait_bit_intr(void *flags)
36 schedule();
37 return signal_pending(current) ? -ERESTARTSYS : 0;
41 * call to complete the construction of a key
43 void complete_request_key(struct key_construction *cons, int error)
45 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
47 if (error < 0)
48 key_negate_and_link(cons->key, key_negative_timeout, NULL,
49 cons->authkey);
50 else
51 key_revoke(cons->authkey);
53 key_put(cons->key);
54 key_put(cons->authkey);
55 kfree(cons);
57 EXPORT_SYMBOL(complete_request_key);
60 * request userspace finish the construction of a key
61 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
63 static int call_sbin_request_key(struct key_construction *cons,
64 const char *op,
65 void *aux)
67 struct task_struct *tsk = current;
68 key_serial_t prkey, sskey;
69 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
70 char *argv[9], *envp[3], uid_str[12], gid_str[12];
71 char key_str[12], keyring_str[3][12];
72 char desc[20];
73 int ret, i;
75 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
77 /* allocate a new session keyring */
78 sprintf(desc, "_req.%u", key->serial);
80 keyring = keyring_alloc(desc, current->fsuid, current->fsgid, current,
81 KEY_ALLOC_QUOTA_OVERRUN, NULL);
82 if (IS_ERR(keyring)) {
83 ret = PTR_ERR(keyring);
84 goto error_alloc;
87 /* attach the auth key to the session keyring */
88 ret = __key_link(keyring, authkey);
89 if (ret < 0)
90 goto error_link;
92 /* record the UID and GID */
93 sprintf(uid_str, "%d", current->fsuid);
94 sprintf(gid_str, "%d", current->fsgid);
96 /* we say which key is under construction */
97 sprintf(key_str, "%d", key->serial);
99 /* we specify the process's default keyrings */
100 sprintf(keyring_str[0], "%d",
101 tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
103 prkey = 0;
104 if (tsk->signal->process_keyring)
105 prkey = tsk->signal->process_keyring->serial;
107 sprintf(keyring_str[1], "%d", prkey);
109 if (tsk->signal->session_keyring) {
110 rcu_read_lock();
111 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
112 rcu_read_unlock();
113 } else {
114 sskey = tsk->user->session_keyring->serial;
117 sprintf(keyring_str[2], "%d", sskey);
119 /* set up a minimal environment */
120 i = 0;
121 envp[i++] = "HOME=/";
122 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
123 envp[i] = NULL;
125 /* set up the argument list */
126 i = 0;
127 argv[i++] = "/sbin/request-key";
128 argv[i++] = (char *) op;
129 argv[i++] = key_str;
130 argv[i++] = uid_str;
131 argv[i++] = gid_str;
132 argv[i++] = keyring_str[0];
133 argv[i++] = keyring_str[1];
134 argv[i++] = keyring_str[2];
135 argv[i] = NULL;
137 /* do it */
138 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
139 UMH_WAIT_PROC);
140 kdebug("usermode -> 0x%x", ret);
141 if (ret >= 0) {
142 /* ret is the exit/wait code */
143 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
144 key_validate(key) < 0)
145 ret = -ENOKEY;
146 else
147 /* ignore any errors from userspace if the key was
148 * instantiated */
149 ret = 0;
152 error_link:
153 key_put(keyring);
155 error_alloc:
156 kleave(" = %d", ret);
157 complete_request_key(cons, ret);
158 return ret;
162 * call out to userspace for key construction
163 * - we ignore program failure and go on key status instead
165 static int construct_key(struct key *key, const void *callout_info,
166 size_t callout_len, void *aux)
168 struct key_construction *cons;
169 request_key_actor_t actor;
170 struct key *authkey;
171 int ret;
173 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
175 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
176 if (!cons)
177 return -ENOMEM;
179 /* allocate an authorisation key */
180 authkey = request_key_auth_new(key, callout_info, callout_len);
181 if (IS_ERR(authkey)) {
182 kfree(cons);
183 ret = PTR_ERR(authkey);
184 authkey = NULL;
185 } else {
186 cons->authkey = key_get(authkey);
187 cons->key = key_get(key);
189 /* make the call */
190 actor = call_sbin_request_key;
191 if (key->type->request_key)
192 actor = key->type->request_key;
194 ret = actor(cons, "create", aux);
196 /* check that the actor called complete_request_key() prior to
197 * returning an error */
198 WARN_ON(ret < 0 &&
199 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
200 key_put(authkey);
203 kleave(" = %d", ret);
204 return ret;
208 * link a key to the appropriate destination keyring
209 * - the caller must hold a write lock on the destination keyring
211 static void construct_key_make_link(struct key *key, struct key *dest_keyring)
213 struct task_struct *tsk = current;
214 struct key *drop = NULL;
216 kenter("{%d},%p", key->serial, dest_keyring);
218 /* find the appropriate keyring */
219 if (!dest_keyring) {
220 switch (tsk->jit_keyring) {
221 case KEY_REQKEY_DEFL_DEFAULT:
222 case KEY_REQKEY_DEFL_THREAD_KEYRING:
223 dest_keyring = tsk->thread_keyring;
224 if (dest_keyring)
225 break;
227 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
228 dest_keyring = tsk->signal->process_keyring;
229 if (dest_keyring)
230 break;
232 case KEY_REQKEY_DEFL_SESSION_KEYRING:
233 rcu_read_lock();
234 dest_keyring = key_get(
235 rcu_dereference(tsk->signal->session_keyring));
236 rcu_read_unlock();
237 drop = dest_keyring;
239 if (dest_keyring)
240 break;
242 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
243 dest_keyring = tsk->user->session_keyring;
244 break;
246 case KEY_REQKEY_DEFL_USER_KEYRING:
247 dest_keyring = tsk->user->uid_keyring;
248 break;
250 case KEY_REQKEY_DEFL_GROUP_KEYRING:
251 default:
252 BUG();
256 /* and attach the key to it */
257 __key_link(dest_keyring, key);
258 key_put(drop);
259 kleave("");
263 * allocate a new key in under-construction state and attempt to link it in to
264 * the requested place
265 * - may return a key that's already under construction instead
267 static int construct_alloc_key(struct key_type *type,
268 const char *description,
269 struct key *dest_keyring,
270 unsigned long flags,
271 struct key_user *user,
272 struct key **_key)
274 struct key *key;
275 key_ref_t key_ref;
277 kenter("%s,%s,,,", type->name, description);
279 mutex_lock(&user->cons_lock);
281 key = key_alloc(type, description,
282 current->fsuid, current->fsgid, current, KEY_POS_ALL,
283 flags);
284 if (IS_ERR(key))
285 goto alloc_failed;
287 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
289 if (dest_keyring)
290 down_write(&dest_keyring->sem);
292 /* attach the key to the destination keyring under lock, but we do need
293 * to do another check just in case someone beat us to it whilst we
294 * waited for locks */
295 mutex_lock(&key_construction_mutex);
297 key_ref = search_process_keyrings(type, description, type->match,
298 current);
299 if (!IS_ERR(key_ref))
300 goto key_already_present;
302 if (dest_keyring)
303 construct_key_make_link(key, dest_keyring);
305 mutex_unlock(&key_construction_mutex);
306 if (dest_keyring)
307 up_write(&dest_keyring->sem);
308 mutex_unlock(&user->cons_lock);
309 *_key = key;
310 kleave(" = 0 [%d]", key_serial(key));
311 return 0;
313 key_already_present:
314 mutex_unlock(&key_construction_mutex);
315 if (dest_keyring)
316 up_write(&dest_keyring->sem);
317 mutex_unlock(&user->cons_lock);
318 key_put(key);
319 *_key = key = key_ref_to_ptr(key_ref);
320 kleave(" = -EINPROGRESS [%d]", key_serial(key));
321 return -EINPROGRESS;
323 alloc_failed:
324 mutex_unlock(&user->cons_lock);
325 *_key = NULL;
326 kleave(" = %ld", PTR_ERR(key));
327 return PTR_ERR(key);
331 * commence key construction
333 static struct key *construct_key_and_link(struct key_type *type,
334 const char *description,
335 const char *callout_info,
336 size_t callout_len,
337 void *aux,
338 struct key *dest_keyring,
339 unsigned long flags)
341 struct key_user *user;
342 struct key *key;
343 int ret;
345 user = key_user_lookup(current->fsuid);
346 if (!user)
347 return ERR_PTR(-ENOMEM);
349 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
350 &key);
351 key_user_put(user);
353 if (ret == 0) {
354 ret = construct_key(key, callout_info, callout_len, aux);
355 if (ret < 0)
356 goto construction_failed;
359 return key;
361 construction_failed:
362 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
363 key_put(key);
364 return ERR_PTR(ret);
368 * request a key
369 * - search the process's keyrings
370 * - check the list of keys being created or updated
371 * - call out to userspace for a key if supplementary info was provided
372 * - cache the key in an appropriate keyring
374 struct key *request_key_and_link(struct key_type *type,
375 const char *description,
376 const void *callout_info,
377 size_t callout_len,
378 void *aux,
379 struct key *dest_keyring,
380 unsigned long flags)
382 struct key *key;
383 key_ref_t key_ref;
385 kenter("%s,%s,%p,%zu,%p,%p,%lx",
386 type->name, description, callout_info, callout_len, aux,
387 dest_keyring, flags);
389 /* search all the process keyrings for a key */
390 key_ref = search_process_keyrings(type, description, type->match,
391 current);
393 if (!IS_ERR(key_ref)) {
394 key = key_ref_to_ptr(key_ref);
395 } else if (PTR_ERR(key_ref) != -EAGAIN) {
396 key = ERR_CAST(key_ref);
397 } else {
398 /* the search failed, but the keyrings were searchable, so we
399 * should consult userspace if we can */
400 key = ERR_PTR(-ENOKEY);
401 if (!callout_info)
402 goto error;
404 key = construct_key_and_link(type, description, callout_info,
405 callout_len, aux, dest_keyring,
406 flags);
409 error:
410 kleave(" = %p", key);
411 return key;
415 * wait for construction of a key to complete
417 int wait_for_key_construction(struct key *key, bool intr)
419 int ret;
421 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
422 intr ? key_wait_bit_intr : key_wait_bit,
423 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
424 if (ret < 0)
425 return ret;
426 return key_validate(key);
428 EXPORT_SYMBOL(wait_for_key_construction);
431 * request a key
432 * - search the process's keyrings
433 * - check the list of keys being created or updated
434 * - call out to userspace for a key if supplementary info was provided
435 * - waits uninterruptible for creation to complete
437 struct key *request_key(struct key_type *type,
438 const char *description,
439 const char *callout_info)
441 struct key *key;
442 size_t callout_len = 0;
443 int ret;
445 if (callout_info)
446 callout_len = strlen(callout_info);
447 key = request_key_and_link(type, description, callout_info, callout_len,
448 NULL, NULL, KEY_ALLOC_IN_QUOTA);
449 if (!IS_ERR(key)) {
450 ret = wait_for_key_construction(key, false);
451 if (ret < 0) {
452 key_put(key);
453 return ERR_PTR(ret);
456 return key;
458 EXPORT_SYMBOL(request_key);
461 * request a key with auxiliary data for the upcaller
462 * - search the process's keyrings
463 * - check the list of keys being created or updated
464 * - call out to userspace for a key if supplementary info was provided
465 * - waits uninterruptible for creation to complete
467 struct key *request_key_with_auxdata(struct key_type *type,
468 const char *description,
469 const void *callout_info,
470 size_t callout_len,
471 void *aux)
473 struct key *key;
474 int ret;
476 key = request_key_and_link(type, description, callout_info, callout_len,
477 aux, NULL, KEY_ALLOC_IN_QUOTA);
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);
485 return key;
487 EXPORT_SYMBOL(request_key_with_auxdata);
490 * request a key (allow async construction)
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
495 struct key *request_key_async(struct key_type *type,
496 const char *description,
497 const void *callout_info,
498 size_t callout_len)
500 return request_key_and_link(type, description, callout_info,
501 callout_len, NULL, NULL,
502 KEY_ALLOC_IN_QUOTA);
504 EXPORT_SYMBOL(request_key_async);
507 * request a key with auxiliary data for the upcaller (allow async construction)
508 * - search the process's keyrings
509 * - check the list of keys being created or updated
510 * - call out to userspace for a key if supplementary info was provided
512 struct key *request_key_async_with_auxdata(struct key_type *type,
513 const char *description,
514 const void *callout_info,
515 size_t callout_len,
516 void *aux)
518 return request_key_and_link(type, description, callout_info,
519 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
521 EXPORT_SYMBOL(request_key_async_with_auxdata);