1 /* request_key.c: request a key from userspace
3 * Copyright (C) 2004-5 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>
21 struct key_construction {
22 struct list_head link; /* link in construction queue */
23 struct key *key; /* key being constructed */
26 /* when waiting for someone else's keys, you get added to this */
27 DECLARE_WAIT_QUEUE_HEAD(request_key_conswq);
29 /*****************************************************************************/
31 * request userspace finish the construction of a key
32 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
34 static int call_sbin_request_key(struct key *key,
38 struct task_struct *tsk = current;
39 key_serial_t prkey, sskey;
41 char *argv[9], *envp[3], uid_str[12], gid_str[12];
42 char key_str[12], keyring_str[3][12];
46 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
48 /* allocate a new session keyring */
49 sprintf(desc, "_req.%u", key->serial);
51 keyring = keyring_alloc(desc, current->fsuid, current->fsgid,
53 if (IS_ERR(keyring)) {
54 ret = PTR_ERR(keyring);
58 /* attach the auth key to the session keyring */
59 ret = __key_link(keyring, authkey);
63 /* record the UID and GID */
64 sprintf(uid_str, "%d", current->fsuid);
65 sprintf(gid_str, "%d", current->fsgid);
67 /* we say which key is under construction */
68 sprintf(key_str, "%d", key->serial);
70 /* we specify the process's default keyrings */
71 sprintf(keyring_str[0], "%d",
72 tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
75 if (tsk->signal->process_keyring)
76 prkey = tsk->signal->process_keyring->serial;
78 sprintf(keyring_str[1], "%d", prkey);
80 if (tsk->signal->session_keyring) {
82 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
86 sskey = tsk->user->session_keyring->serial;
89 sprintf(keyring_str[2], "%d", sskey);
91 /* set up a minimal environment */
94 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
97 /* set up the argument list */
99 argv[i++] = "/sbin/request-key";
100 argv[i++] = (char *) op;
104 argv[i++] = keyring_str[0];
105 argv[i++] = keyring_str[1];
106 argv[i++] = keyring_str[2];
110 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring, 1);
116 kleave(" = %d", ret);
119 } /* end call_sbin_request_key() */
121 /*****************************************************************************/
123 * call out to userspace for the key
124 * - called with the construction sem held, but the sem is dropped here
125 * - we ignore program failure and go on key status instead
127 static struct key *__request_key_construction(struct key_type *type,
128 const char *description,
129 const char *callout_info)
131 request_key_actor_t actor;
132 struct key_construction cons;
134 struct key *key, *authkey;
137 kenter("%s,%s,%s", type->name, description, callout_info);
139 /* create a key and add it to the queue */
140 key = key_alloc(type, description,
141 current->fsuid, current->fsgid,
142 current, KEY_POS_ALL, 0);
146 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
149 list_add_tail(&cons.link, &key->user->consq);
151 /* we drop the construction sem here on behalf of the caller */
152 up_write(&key_construction_sem);
154 /* allocate an authorisation key */
155 authkey = request_key_auth_new(key, callout_info);
156 if (IS_ERR(authkey)) {
157 ret = PTR_ERR(authkey);
159 goto alloc_authkey_failed;
163 actor = call_sbin_request_key;
164 if (type->request_key)
165 actor = type->request_key;
166 ret = actor(key, authkey, "create");
170 /* if the key wasn't instantiated, then we want to give an error */
172 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
178 down_write(&key_construction_sem);
179 list_del(&cons.link);
180 up_write(&key_construction_sem);
182 /* also give an error if the key was negatively instantiated */
184 if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
186 key = ERR_PTR(-ENOKEY);
190 kleave(" = %p", key);
197 alloc_authkey_failed:
198 /* it wasn't instantiated
199 * - remove from construction queue
200 * - mark the key as dead
203 down_write(&key_construction_sem);
205 list_del(&cons.link);
207 /* check it didn't get instantiated between the check and the down */
208 if (!test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
209 set_bit(KEY_FLAG_NEGATIVE, &key->flags);
210 set_bit(KEY_FLAG_INSTANTIATED, &key->flags);
214 clear_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
216 up_write(&key_construction_sem);
219 goto check_not_negative; /* surprisingly, the key got
222 /* set the timeout and store in the session keyring if we can */
223 now = current_kernel_time();
224 key->expiry = now.tv_sec + key_negative_timeout;
226 if (current->signal->session_keyring) {
230 keyring = rcu_dereference(current->signal->session_keyring);
231 atomic_inc(&keyring->usage);
234 key_link(keyring, key);
240 /* notify anyone who was waiting */
241 wake_up_all(&request_key_conswq);
247 up_write(&key_construction_sem);
250 } /* end __request_key_construction() */
252 /*****************************************************************************/
254 * call out to userspace to request the key
255 * - we check the construction queue first to see if an appropriate key is
256 * already being constructed by userspace
258 static struct key *request_key_construction(struct key_type *type,
259 const char *description,
260 struct key_user *user,
261 const char *callout_info)
263 struct key_construction *pcons;
264 struct key *key, *ckey;
266 DECLARE_WAITQUEUE(myself, current);
268 kenter("%s,%s,{%d},%s",
269 type->name, description, user->uid, callout_info);
271 /* see if there's such a key under construction already */
272 down_write(&key_construction_sem);
274 list_for_each_entry(pcons, &user->consq, link) {
277 if (ckey->type != type)
280 if (type->match(ckey, description))
281 goto found_key_under_construction;
284 /* see about getting userspace to construct the key */
285 key = __request_key_construction(type, description, callout_info);
287 kleave(" = %p", key);
290 /* someone else has the same key under construction
291 * - we want to keep an eye on their key
293 found_key_under_construction:
294 atomic_inc(&ckey->usage);
295 up_write(&key_construction_sem);
297 /* wait for the key to be completed one way or another */
298 add_wait_queue(&request_key_conswq, &myself);
301 set_current_state(TASK_INTERRUPTIBLE);
302 if (!test_bit(KEY_FLAG_USER_CONSTRUCT, &ckey->flags))
304 if (signal_pending(current))
309 set_current_state(TASK_RUNNING);
310 remove_wait_queue(&request_key_conswq, &myself);
312 /* we'll need to search this process's keyrings to see if the key is
313 * now there since we can't automatically assume it's also available
318 key = NULL; /* request a retry */
321 } /* end request_key_construction() */
323 /*****************************************************************************/
325 * link a freshly minted key to an appropriate destination keyring
327 static void request_key_link(struct key *key, struct key *dest_keyring)
329 struct task_struct *tsk = current;
330 struct key *drop = NULL;
332 kenter("{%d},%p", key->serial, dest_keyring);
334 /* find the appropriate keyring */
336 switch (tsk->jit_keyring) {
337 case KEY_REQKEY_DEFL_DEFAULT:
338 case KEY_REQKEY_DEFL_THREAD_KEYRING:
339 dest_keyring = tsk->thread_keyring;
343 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
344 dest_keyring = tsk->signal->process_keyring;
348 case KEY_REQKEY_DEFL_SESSION_KEYRING:
350 dest_keyring = key_get(
351 rcu_dereference(tsk->signal->session_keyring));
358 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
359 dest_keyring = current->user->session_keyring;
362 case KEY_REQKEY_DEFL_USER_KEYRING:
363 dest_keyring = current->user->uid_keyring;
366 case KEY_REQKEY_DEFL_GROUP_KEYRING:
372 /* and attach the key to it */
373 key_link(dest_keyring, key);
379 } /* end request_key_link() */
381 /*****************************************************************************/
384 * - search the process's keyrings
385 * - check the list of keys being created or updated
386 * - call out to userspace for a key if supplementary info was provided
387 * - cache the key in an appropriate keyring
389 struct key *request_key_and_link(struct key_type *type,
390 const char *description,
391 const char *callout_info,
392 struct key *dest_keyring)
394 struct key_user *user;
398 kenter("%s,%s,%s,%p",
399 type->name, description, callout_info, dest_keyring);
401 /* search all the process keyrings for a key */
402 key_ref = search_process_keyrings(type, description, type->match,
405 kdebug("search 1: %p", key_ref);
407 if (!IS_ERR(key_ref)) {
408 key = key_ref_to_ptr(key_ref);
410 else if (PTR_ERR(key_ref) != -EAGAIN) {
411 key = ERR_PTR(PTR_ERR(key_ref));
414 /* the search failed, but the keyrings were searchable, so we
415 * should consult userspace if we can */
416 key = ERR_PTR(-ENOKEY);
420 /* - get hold of the user's construction queue */
421 user = key_user_lookup(current->fsuid);
426 if (signal_pending(current))
429 /* ask userspace (returns NULL if it waited on a key
430 * being constructed) */
431 key = request_key_construction(type, description,
436 /* someone else made the key we want, so we need to
437 * search again as it might now be available to us */
438 key_ref = search_process_keyrings(type, description,
442 kdebug("search 2: %p", key_ref);
444 if (!IS_ERR(key_ref)) {
445 key = key_ref_to_ptr(key_ref);
449 if (PTR_ERR(key_ref) != -EAGAIN) {
450 key = ERR_PTR(PTR_ERR(key_ref));
457 /* link the new key into the appropriate keyring */
459 request_key_link(key, dest_keyring);
463 kleave(" = %p", key);
467 key = ERR_PTR(-ENOMEM);
472 key = ERR_PTR(-EINTR);
475 } /* end request_key_and_link() */
477 /*****************************************************************************/
480 * - search the process's keyrings
481 * - check the list of keys being created or updated
482 * - call out to userspace for a key if supplementary info was provided
484 struct key *request_key(struct key_type *type,
485 const char *description,
486 const char *callout_info)
488 return request_key_and_link(type, description, callout_info, NULL);
490 } /* end request_key() */
492 EXPORT_SYMBOL(request_key);