1 /* request_key.c: request a key from userspace
3 * Copyright (C) 2004 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.
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/kmod.h>
15 #include <linux/err.h>
18 struct key_construction
{
19 struct list_head link
; /* link in construction queue */
20 struct key
*key
; /* key being constructed */
23 /* when waiting for someone else's keys, you get added to this */
24 DECLARE_WAIT_QUEUE_HEAD(request_key_conswq
);
26 /*****************************************************************************/
28 * request userspace finish the construction of a key
29 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring> <info>"
30 * - if callout_info is an empty string, it'll be rendered as a "-" instead
32 static int call_request_key(struct key
*key
,
34 const char *callout_info
)
36 struct task_struct
*tsk
= current
;
38 key_serial_t prkey
, sskey
;
39 char *argv
[10], *envp
[3], uid_str
[12], gid_str
[12];
40 char key_str
[12], keyring_str
[3][12];
43 /* record the UID and GID */
44 sprintf(uid_str
, "%d", current
->fsuid
);
45 sprintf(gid_str
, "%d", current
->fsgid
);
47 /* we say which key is under construction */
48 sprintf(key_str
, "%d", key
->serial
);
50 /* we specify the process's default keyrings */
51 sprintf(keyring_str
[0], "%d",
52 tsk
->thread_keyring
? tsk
->thread_keyring
->serial
: 0);
55 if (tsk
->signal
->process_keyring
)
56 prkey
= tsk
->signal
->process_keyring
->serial
;
59 spin_lock_irqsave(&tsk
->sighand
->siglock
, flags
);
60 if (tsk
->signal
->session_keyring
)
61 sskey
= tsk
->signal
->session_keyring
->serial
;
62 spin_unlock_irqrestore(&tsk
->sighand
->siglock
, flags
);
66 sskey
= tsk
->user
->session_keyring
->serial
;
68 sprintf(keyring_str
[1], "%d", prkey
);
69 sprintf(keyring_str
[2], "%d", sskey
);
71 /* set up a minimal environment */
74 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
77 /* set up the argument list */
79 argv
[i
++] = "/sbin/request-key";
80 argv
[i
++] = (char *) op
;
84 argv
[i
++] = keyring_str
[0];
85 argv
[i
++] = keyring_str
[1];
86 argv
[i
++] = keyring_str
[2];
87 argv
[i
++] = callout_info
[0] ? (char *) callout_info
: "-";
91 return call_usermodehelper_keys(argv
[0], argv
, envp
, NULL
, 1);
93 } /* end call_request_key() */
95 /*****************************************************************************/
97 * call out to userspace for the key
98 * - called with the construction sem held, but the sem is dropped here
99 * - we ignore program failure and go on key status instead
101 static struct key
*__request_key_construction(struct key_type
*type
,
102 const char *description
,
103 const char *callout_info
)
105 struct key_construction cons
;
110 /* create a key and add it to the queue */
111 key
= key_alloc(type
, description
,
112 current
->fsuid
, current
->fsgid
, KEY_USR_ALL
, 0);
116 set_bit(KEY_FLAG_USER_CONSTRUCT
, &key
->flags
);
119 list_add_tail(&cons
.link
, &key
->user
->consq
);
121 /* we drop the construction sem here on behalf of the caller */
122 up_write(&key_construction_sem
);
125 ret
= call_request_key(key
, "create", callout_info
);
129 /* if the key wasn't instantiated, then we want to give an error */
131 if (!test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
))
134 down_write(&key_construction_sem
);
135 list_del(&cons
.link
);
136 up_write(&key_construction_sem
);
138 /* also give an error if the key was negatively instantiated */
140 if (test_bit(KEY_FLAG_NEGATIVE
, &key
->flags
)) {
142 key
= ERR_PTR(-ENOKEY
);
149 /* it wasn't instantiated
150 * - remove from construction queue
151 * - mark the key as dead
154 down_write(&key_construction_sem
);
156 list_del(&cons
.link
);
158 /* check it didn't get instantiated between the check and the down */
159 if (!test_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
)) {
160 set_bit(KEY_FLAG_NEGATIVE
, &key
->flags
);
161 set_bit(KEY_FLAG_INSTANTIATED
, &key
->flags
);
165 clear_bit(KEY_FLAG_USER_CONSTRUCT
, &key
->flags
);
167 up_write(&key_construction_sem
);
170 goto check_not_negative
; /* surprisingly, the key got
173 /* set the timeout and store in the session keyring if we can */
174 now
= current_kernel_time();
175 key
->expiry
= now
.tv_sec
+ key_negative_timeout
;
177 if (current
->signal
->session_keyring
) {
181 keyring
= rcu_dereference(current
->signal
->session_keyring
);
182 atomic_inc(&keyring
->usage
);
185 key_link(keyring
, key
);
191 /* notify anyone who was waiting */
192 wake_up_all(&request_key_conswq
);
198 up_write(&key_construction_sem
);
201 } /* end __request_key_construction() */
203 /*****************************************************************************/
205 * call out to userspace to request the key
206 * - we check the construction queue first to see if an appropriate key is
207 * already being constructed by userspace
209 static struct key
*request_key_construction(struct key_type
*type
,
210 const char *description
,
211 struct key_user
*user
,
212 const char *callout_info
)
214 struct key_construction
*pcons
;
215 struct key
*key
, *ckey
;
217 DECLARE_WAITQUEUE(myself
, current
);
219 /* see if there's such a key under construction already */
220 down_write(&key_construction_sem
);
222 list_for_each_entry(pcons
, &user
->consq
, link
) {
225 if (ckey
->type
!= type
)
228 if (type
->match(ckey
, description
))
229 goto found_key_under_construction
;
232 /* see about getting userspace to construct the key */
233 key
= __request_key_construction(type
, description
, callout_info
);
237 /* someone else has the same key under construction
238 * - we want to keep an eye on their key
240 found_key_under_construction
:
241 atomic_inc(&ckey
->usage
);
242 up_write(&key_construction_sem
);
244 /* wait for the key to be completed one way or another */
245 add_wait_queue(&request_key_conswq
, &myself
);
248 set_current_state(TASK_UNINTERRUPTIBLE
);
249 if (!test_bit(KEY_FLAG_USER_CONSTRUCT
, &ckey
->flags
))
254 set_current_state(TASK_RUNNING
);
255 remove_wait_queue(&request_key_conswq
, &myself
);
257 /* we'll need to search this process's keyrings to see if the key is
258 * now there since we can't automatically assume it's also available
263 key
= NULL
; /* request a retry */
266 } /* end request_key_construction() */
268 /*****************************************************************************/
271 * - search the process's keyrings
272 * - check the list of keys being created or updated
273 * - call out to userspace for a key if requested (supplementary info can be
276 struct key
*request_key(struct key_type
*type
,
277 const char *description
,
278 const char *callout_info
)
280 struct key_user
*user
;
283 /* search all the process keyrings for a key */
284 key
= search_process_keyrings_aux(type
, description
, type
->match
);
286 if (PTR_ERR(key
) == -EAGAIN
) {
287 /* the search failed, but the keyrings were searchable, so we
288 * should consult userspace if we can */
289 key
= ERR_PTR(-ENOKEY
);
293 /* - get hold of the user's construction queue */
294 user
= key_user_lookup(current
->fsuid
);
296 key
= ERR_PTR(-ENOMEM
);
301 /* ask userspace (returns NULL if it waited on a key
302 * being constructed) */
303 key
= request_key_construction(type
, description
,
308 /* someone else made the key we want, so we need to
309 * search again as it might now be available to us */
310 key
= search_process_keyrings_aux(type
, description
,
312 if (PTR_ERR(key
) != -EAGAIN
)
322 } /* end request_key() */
324 EXPORT_SYMBOL(request_key
);
326 /*****************************************************************************/
330 int key_validate(struct key
*key
)
336 /* check it's still accessible */
338 if (test_bit(KEY_FLAG_REVOKED
, &key
->flags
) ||
339 test_bit(KEY_FLAG_DEAD
, &key
->flags
))
342 /* check it hasn't expired */
345 now
= current_kernel_time();
346 if (now
.tv_sec
>= key
->expiry
)
354 } /* end key_validate() */
356 EXPORT_SYMBOL(key_validate
);