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>
23 * wait_on_bit() sleep function for uninterruptible waiting
25 static int key_wait_bit(void *flags
)
32 * wait_on_bit() sleep function for interruptible waiting
34 static int key_wait_bit_intr(void *flags
)
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
);
48 key_negate_and_link(cons
->key
, key_negative_timeout
, NULL
,
51 key_revoke(cons
->authkey
);
54 key_put(cons
->authkey
);
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
,
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];
75 kenter("{%d},{%d},%s", key
->serial
, authkey
->serial
, op
);
77 ret
= install_user_keyrings(tsk
);
81 /* allocate a new session keyring */
82 sprintf(desc
, "_req.%u", key
->serial
);
84 keyring
= keyring_alloc(desc
, current
->fsuid
, current
->fsgid
, current
,
85 KEY_ALLOC_QUOTA_OVERRUN
, NULL
);
86 if (IS_ERR(keyring
)) {
87 ret
= PTR_ERR(keyring
);
91 /* attach the auth key to the session keyring */
92 ret
= __key_link(keyring
, authkey
);
96 /* record the UID and GID */
97 sprintf(uid_str
, "%d", current
->fsuid
);
98 sprintf(gid_str
, "%d", current
->fsgid
);
100 /* we say which key is under construction */
101 sprintf(key_str
, "%d", key
->serial
);
103 /* we specify the process's default keyrings */
104 sprintf(keyring_str
[0], "%d",
105 tsk
->thread_keyring
? tsk
->thread_keyring
->serial
: 0);
108 if (tsk
->signal
->process_keyring
)
109 prkey
= tsk
->signal
->process_keyring
->serial
;
111 sprintf(keyring_str
[1], "%d", prkey
);
113 if (tsk
->signal
->session_keyring
) {
115 sskey
= rcu_dereference(tsk
->signal
->session_keyring
)->serial
;
118 sskey
= tsk
->user
->session_keyring
->serial
;
121 sprintf(keyring_str
[2], "%d", sskey
);
123 /* set up a minimal environment */
125 envp
[i
++] = "HOME=/";
126 envp
[i
++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
129 /* set up the argument list */
131 argv
[i
++] = "/sbin/request-key";
132 argv
[i
++] = (char *) op
;
136 argv
[i
++] = keyring_str
[0];
137 argv
[i
++] = keyring_str
[1];
138 argv
[i
++] = keyring_str
[2];
142 ret
= call_usermodehelper_keys(argv
[0], argv
, envp
, keyring
,
144 kdebug("usermode -> 0x%x", ret
);
146 /* ret is the exit/wait code */
147 if (test_bit(KEY_FLAG_USER_CONSTRUCT
, &key
->flags
) ||
148 key_validate(key
) < 0)
151 /* ignore any errors from userspace if the key was
160 kleave(" = %d", ret
);
161 complete_request_key(cons
, ret
);
166 * call out to userspace for key construction
167 * - we ignore program failure and go on key status instead
169 static int construct_key(struct key
*key
, const void *callout_info
,
170 size_t callout_len
, void *aux
)
172 struct key_construction
*cons
;
173 request_key_actor_t actor
;
177 kenter("%d,%p,%zu,%p", key
->serial
, callout_info
, callout_len
, aux
);
179 cons
= kmalloc(sizeof(*cons
), GFP_KERNEL
);
183 /* allocate an authorisation key */
184 authkey
= request_key_auth_new(key
, callout_info
, callout_len
);
185 if (IS_ERR(authkey
)) {
187 ret
= PTR_ERR(authkey
);
190 cons
->authkey
= key_get(authkey
);
191 cons
->key
= key_get(key
);
194 actor
= call_sbin_request_key
;
195 if (key
->type
->request_key
)
196 actor
= key
->type
->request_key
;
198 ret
= actor(cons
, "create", aux
);
200 /* check that the actor called complete_request_key() prior to
201 * returning an error */
203 !test_bit(KEY_FLAG_REVOKED
, &authkey
->flags
));
207 kleave(" = %d", ret
);
212 * link a key to the appropriate destination keyring
213 * - the caller must hold a write lock on the destination keyring
215 static void construct_key_make_link(struct key
*key
, struct key
*dest_keyring
)
217 struct task_struct
*tsk
= current
;
218 struct key
*drop
= NULL
;
220 kenter("{%d},%p", key
->serial
, dest_keyring
);
222 /* find the appropriate keyring */
224 switch (tsk
->jit_keyring
) {
225 case KEY_REQKEY_DEFL_DEFAULT
:
226 case KEY_REQKEY_DEFL_THREAD_KEYRING
:
227 dest_keyring
= tsk
->thread_keyring
;
231 case KEY_REQKEY_DEFL_PROCESS_KEYRING
:
232 dest_keyring
= tsk
->signal
->process_keyring
;
236 case KEY_REQKEY_DEFL_SESSION_KEYRING
:
238 dest_keyring
= key_get(
239 rcu_dereference(tsk
->signal
->session_keyring
));
246 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING
:
247 dest_keyring
= tsk
->user
->session_keyring
;
250 case KEY_REQKEY_DEFL_USER_KEYRING
:
251 dest_keyring
= tsk
->user
->uid_keyring
;
254 case KEY_REQKEY_DEFL_GROUP_KEYRING
:
260 /* and attach the key to it */
261 __key_link(dest_keyring
, key
);
267 * allocate a new key in under-construction state and attempt to link it in to
268 * the requested place
269 * - may return a key that's already under construction instead
271 static int construct_alloc_key(struct key_type
*type
,
272 const char *description
,
273 struct key
*dest_keyring
,
275 struct key_user
*user
,
281 kenter("%s,%s,,,", type
->name
, description
);
283 mutex_lock(&user
->cons_lock
);
285 key
= key_alloc(type
, description
,
286 current
->fsuid
, current
->fsgid
, current
, KEY_POS_ALL
,
291 set_bit(KEY_FLAG_USER_CONSTRUCT
, &key
->flags
);
294 down_write(&dest_keyring
->sem
);
296 /* attach the key to the destination keyring under lock, but we do need
297 * to do another check just in case someone beat us to it whilst we
298 * waited for locks */
299 mutex_lock(&key_construction_mutex
);
301 key_ref
= search_process_keyrings(type
, description
, type
->match
,
303 if (!IS_ERR(key_ref
))
304 goto key_already_present
;
307 construct_key_make_link(key
, dest_keyring
);
309 mutex_unlock(&key_construction_mutex
);
311 up_write(&dest_keyring
->sem
);
312 mutex_unlock(&user
->cons_lock
);
314 kleave(" = 0 [%d]", key_serial(key
));
318 mutex_unlock(&key_construction_mutex
);
320 up_write(&dest_keyring
->sem
);
321 mutex_unlock(&user
->cons_lock
);
323 *_key
= key
= key_ref_to_ptr(key_ref
);
324 kleave(" = -EINPROGRESS [%d]", key_serial(key
));
328 mutex_unlock(&user
->cons_lock
);
330 kleave(" = %ld", PTR_ERR(key
));
335 * commence key construction
337 static struct key
*construct_key_and_link(struct key_type
*type
,
338 const char *description
,
339 const char *callout_info
,
342 struct key
*dest_keyring
,
345 struct key_user
*user
;
349 user
= key_user_lookup(current
->fsuid
);
351 return ERR_PTR(-ENOMEM
);
353 ret
= construct_alloc_key(type
, description
, dest_keyring
, flags
, user
,
358 ret
= construct_key(key
, callout_info
, callout_len
, aux
);
360 goto construction_failed
;
366 key_negate_and_link(key
, key_negative_timeout
, NULL
, NULL
);
373 * - search the process's keyrings
374 * - check the list of keys being created or updated
375 * - call out to userspace for a key if supplementary info was provided
376 * - cache the key in an appropriate keyring
378 struct key
*request_key_and_link(struct key_type
*type
,
379 const char *description
,
380 const void *callout_info
,
383 struct key
*dest_keyring
,
389 kenter("%s,%s,%p,%zu,%p,%p,%lx",
390 type
->name
, description
, callout_info
, callout_len
, aux
,
391 dest_keyring
, flags
);
393 /* search all the process keyrings for a key */
394 key_ref
= search_process_keyrings(type
, description
, type
->match
,
397 if (!IS_ERR(key_ref
)) {
398 key
= key_ref_to_ptr(key_ref
);
399 } else if (PTR_ERR(key_ref
) != -EAGAIN
) {
400 key
= ERR_CAST(key_ref
);
402 /* the search failed, but the keyrings were searchable, so we
403 * should consult userspace if we can */
404 key
= ERR_PTR(-ENOKEY
);
408 key
= construct_key_and_link(type
, description
, callout_info
,
409 callout_len
, aux
, dest_keyring
,
414 kleave(" = %p", key
);
419 * wait for construction of a key to complete
421 int wait_for_key_construction(struct key
*key
, bool intr
)
425 ret
= wait_on_bit(&key
->flags
, KEY_FLAG_USER_CONSTRUCT
,
426 intr
? key_wait_bit_intr
: key_wait_bit
,
427 intr
? TASK_INTERRUPTIBLE
: TASK_UNINTERRUPTIBLE
);
430 return key_validate(key
);
432 EXPORT_SYMBOL(wait_for_key_construction
);
436 * - search the process's keyrings
437 * - check the list of keys being created or updated
438 * - call out to userspace for a key if supplementary info was provided
439 * - waits uninterruptible for creation to complete
441 struct key
*request_key(struct key_type
*type
,
442 const char *description
,
443 const char *callout_info
)
446 size_t callout_len
= 0;
450 callout_len
= strlen(callout_info
);
451 key
= request_key_and_link(type
, description
, callout_info
, callout_len
,
452 NULL
, NULL
, KEY_ALLOC_IN_QUOTA
);
454 ret
= wait_for_key_construction(key
, false);
462 EXPORT_SYMBOL(request_key
);
465 * request a key with auxiliary data for the upcaller
466 * - search the process's keyrings
467 * - check the list of keys being created or updated
468 * - call out to userspace for a key if supplementary info was provided
469 * - waits uninterruptible for creation to complete
471 struct key
*request_key_with_auxdata(struct key_type
*type
,
472 const char *description
,
473 const void *callout_info
,
480 key
= request_key_and_link(type
, description
, callout_info
, callout_len
,
481 aux
, NULL
, KEY_ALLOC_IN_QUOTA
);
483 ret
= wait_for_key_construction(key
, false);
491 EXPORT_SYMBOL(request_key_with_auxdata
);
494 * request a key (allow async construction)
495 * - search the process's keyrings
496 * - check the list of keys being created or updated
497 * - call out to userspace for a key if supplementary info was provided
499 struct key
*request_key_async(struct key_type
*type
,
500 const char *description
,
501 const void *callout_info
,
504 return request_key_and_link(type
, description
, callout_info
,
505 callout_len
, NULL
, NULL
,
508 EXPORT_SYMBOL(request_key_async
);
511 * request a key with auxiliary data for the upcaller (allow async construction)
512 * - search the process's keyrings
513 * - check the list of keys being created or updated
514 * - call out to userspace for a key if supplementary info was provided
516 struct key
*request_key_async_with_auxdata(struct key_type
*type
,
517 const char *description
,
518 const void *callout_info
,
522 return request_key_and_link(type
, description
, callout_info
,
523 callout_len
, aux
, NULL
, KEY_ALLOC_IN_QUOTA
);
525 EXPORT_SYMBOL(request_key_async_with_auxdata
);