gpio: fix pca953x set_type 'scheduling while atomic' bug
[deliverable/linux.git] / security / keys / request_key.c
CommitLineData
76181c13 1/* Request a key from userspace
1da177e4 2 *
76181c13 3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
f1a9badc
DH
10 *
11 * See Documentation/keys-request-key.txt
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/kmod.h>
17#include <linux/err.h>
3e30148c 18#include <linux/keyctl.h>
fdb89bce 19#include <linux/slab.h>
1da177e4
LT
20#include "internal.h"
21
e9e349b0
DH
22#define key_negative_timeout 60 /* default timeout on a negative key's existence */
23
76181c13
DH
24/*
25 * wait_on_bit() sleep function for uninterruptible waiting
26 */
27static int key_wait_bit(void *flags)
28{
29 schedule();
30 return 0;
31}
32
33/*
34 * wait_on_bit() sleep function for interruptible waiting
35 */
36static int key_wait_bit_intr(void *flags)
37{
38 schedule();
39 return signal_pending(current) ? -ERESTARTSYS : 0;
40}
41
42/*
43 * call to complete the construction of a key
44 */
45void complete_request_key(struct key_construction *cons, int error)
46{
47 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
1da177e4 48
76181c13
DH
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);
54
55 key_put(cons->key);
56 key_put(cons->authkey);
57 kfree(cons);
58}
59EXPORT_SYMBOL(complete_request_key);
1da177e4 60
1da177e4
LT
61/*
62 * request userspace finish the construction of a key
b5f545c8 63 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
1da177e4 64 */
76181c13 65static int call_sbin_request_key(struct key_construction *cons,
4e54f085
DH
66 const char *op,
67 void *aux)
1da177e4 68{
86a264ab 69 const struct cred *cred = current_cred();
1da177e4 70 key_serial_t prkey, sskey;
93b4a44f
DH
71 struct key *key = cons->key, *authkey = cons->authkey, *keyring,
72 *session;
b5f545c8 73 char *argv[9], *envp[3], uid_str[12], gid_str[12];
1da177e4 74 char key_str[12], keyring_str[3][12];
b5f545c8 75 char desc[20];
3e30148c
DH
76 int ret, i;
77
b5f545c8 78 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
3e30148c 79
8bbf4976
DH
80 ret = install_user_keyrings();
81 if (ret < 0)
82 goto error_alloc;
83
b5f545c8
DH
84 /* allocate a new session keyring */
85 sprintf(desc, "_req.%u", key->serial);
86
d84f4f99
DH
87 cred = get_current_cred();
88 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
7e047ef5 89 KEY_ALLOC_QUOTA_OVERRUN, NULL);
d84f4f99 90 put_cred(cred);
b5f545c8
DH
91 if (IS_ERR(keyring)) {
92 ret = PTR_ERR(keyring);
93 goto error_alloc;
3e30148c 94 }
1da177e4 95
b5f545c8
DH
96 /* attach the auth key to the session keyring */
97 ret = __key_link(keyring, authkey);
98 if (ret < 0)
99 goto error_link;
100
1da177e4 101 /* record the UID and GID */
86a264ab
DH
102 sprintf(uid_str, "%d", cred->fsuid);
103 sprintf(gid_str, "%d", cred->fsgid);
1da177e4
LT
104
105 /* we say which key is under construction */
106 sprintf(key_str, "%d", key->serial);
107
108 /* we specify the process's default keyrings */
109 sprintf(keyring_str[0], "%d",
d84f4f99 110 cred->thread_keyring ? cred->thread_keyring->serial : 0);
1da177e4
LT
111
112 prkey = 0;
bb952bb9
DH
113 if (cred->tgcred->process_keyring)
114 prkey = cred->tgcred->process_keyring->serial;
1da177e4 115
93b4a44f
DH
116 rcu_read_lock();
117 session = rcu_dereference(cred->tgcred->session_keyring);
118 if (!session)
119 session = cred->user->session_keyring;
120 sskey = session->serial;
121 rcu_read_unlock();
1da177e4 122
1da177e4
LT
123 sprintf(keyring_str[2], "%d", sskey);
124
125 /* set up a minimal environment */
126 i = 0;
127 envp[i++] = "HOME=/";
128 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
129 envp[i] = NULL;
130
131 /* set up the argument list */
132 i = 0;
133 argv[i++] = "/sbin/request-key";
134 argv[i++] = (char *) op;
135 argv[i++] = key_str;
136 argv[i++] = uid_str;
137 argv[i++] = gid_str;
138 argv[i++] = keyring_str[0];
139 argv[i++] = keyring_str[1];
140 argv[i++] = keyring_str[2];
1da177e4
LT
141 argv[i] = NULL;
142
143 /* do it */
86313c48
JF
144 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
145 UMH_WAIT_PROC);
76181c13
DH
146 kdebug("usermode -> 0x%x", ret);
147 if (ret >= 0) {
148 /* ret is the exit/wait code */
149 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
150 key_validate(key) < 0)
151 ret = -ENOKEY;
152 else
153 /* ignore any errors from userspace if the key was
154 * instantiated */
155 ret = 0;
156 }
3e30148c 157
b5f545c8
DH
158error_link:
159 key_put(keyring);
3e30148c 160
b5f545c8 161error_alloc:
76181c13 162 complete_request_key(cons, ret);
d84f4f99 163 kleave(" = %d", ret);
3e30148c 164 return ret;
76181c13 165}
1da177e4 166
1da177e4 167/*
76181c13 168 * call out to userspace for key construction
1da177e4
LT
169 * - we ignore program failure and go on key status instead
170 */
4a38e122 171static int construct_key(struct key *key, const void *callout_info,
8bbf4976
DH
172 size_t callout_len, void *aux,
173 struct key *dest_keyring)
1da177e4 174{
76181c13 175 struct key_construction *cons;
b5f545c8 176 request_key_actor_t actor;
76181c13
DH
177 struct key *authkey;
178 int ret;
1da177e4 179
4a38e122 180 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
3e30148c 181
76181c13
DH
182 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
183 if (!cons)
184 return -ENOMEM;
1da177e4 185
b5f545c8 186 /* allocate an authorisation key */
8bbf4976
DH
187 authkey = request_key_auth_new(key, callout_info, callout_len,
188 dest_keyring);
b5f545c8 189 if (IS_ERR(authkey)) {
76181c13 190 kfree(cons);
b5f545c8
DH
191 ret = PTR_ERR(authkey);
192 authkey = NULL;
76181c13
DH
193 } else {
194 cons->authkey = key_get(authkey);
195 cons->key = key_get(key);
196
197 /* make the call */
198 actor = call_sbin_request_key;
199 if (key->type->request_key)
200 actor = key->type->request_key;
201
202 ret = actor(cons, "create", aux);
203
204 /* check that the actor called complete_request_key() prior to
205 * returning an error */
206 WARN_ON(ret < 0 &&
207 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
208 key_put(authkey);
1da177e4
LT
209 }
210
76181c13
DH
211 kleave(" = %d", ret);
212 return ret;
213}
1da177e4 214
3e30148c 215/*
8bbf4976
DH
216 * get the appropriate destination keyring for the request
217 * - we return whatever keyring we select with an extra reference upon it which
218 * the caller must release
3e30148c 219 */
8bbf4976 220static void construct_get_dest_keyring(struct key **_dest_keyring)
3e30148c 221{
8bbf4976 222 struct request_key_auth *rka;
bb952bb9 223 const struct cred *cred = current_cred();
8bbf4976 224 struct key *dest_keyring = *_dest_keyring, *authkey;
3e30148c 225
8bbf4976 226 kenter("%p", dest_keyring);
3e30148c
DH
227
228 /* find the appropriate keyring */
8bbf4976
DH
229 if (dest_keyring) {
230 /* the caller supplied one */
231 key_get(dest_keyring);
232 } else {
233 /* use a default keyring; falling through the cases until we
234 * find one that we actually have */
bb952bb9 235 switch (cred->jit_keyring) {
3e30148c 236 case KEY_REQKEY_DEFL_DEFAULT:
8bbf4976 237 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
bb952bb9
DH
238 if (cred->request_key_auth) {
239 authkey = cred->request_key_auth;
8bbf4976
DH
240 down_read(&authkey->sem);
241 rka = authkey->payload.data;
242 if (!test_bit(KEY_FLAG_REVOKED,
243 &authkey->flags))
244 dest_keyring =
245 key_get(rka->dest_keyring);
246 up_read(&authkey->sem);
247 if (dest_keyring)
248 break;
249 }
250
3e30148c 251 case KEY_REQKEY_DEFL_THREAD_KEYRING:
bb952bb9 252 dest_keyring = key_get(cred->thread_keyring);
3e30148c
DH
253 if (dest_keyring)
254 break;
255
256 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
bb952bb9 257 dest_keyring = key_get(cred->tgcred->process_keyring);
3e30148c
DH
258 if (dest_keyring)
259 break;
260
261 case KEY_REQKEY_DEFL_SESSION_KEYRING:
262 rcu_read_lock();
263 dest_keyring = key_get(
bb952bb9 264 rcu_dereference(cred->tgcred->session_keyring));
3e30148c 265 rcu_read_unlock();
3e30148c
DH
266
267 if (dest_keyring)
268 break;
269
270 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
b6dff3ec 271 dest_keyring =
bb952bb9 272 key_get(cred->user->session_keyring);
3e30148c
DH
273 break;
274
275 case KEY_REQKEY_DEFL_USER_KEYRING:
bb952bb9 276 dest_keyring = key_get(cred->user->uid_keyring);
3e30148c
DH
277 break;
278
279 case KEY_REQKEY_DEFL_GROUP_KEYRING:
280 default:
281 BUG();
282 }
283 }
284
8bbf4976
DH
285 *_dest_keyring = dest_keyring;
286 kleave(" [dk %d]", key_serial(dest_keyring));
287 return;
76181c13 288}
3e30148c 289
76181c13
DH
290/*
291 * allocate a new key in under-construction state and attempt to link it in to
292 * the requested place
293 * - may return a key that's already under construction instead
294 */
295static int construct_alloc_key(struct key_type *type,
296 const char *description,
297 struct key *dest_keyring,
298 unsigned long flags,
299 struct key_user *user,
300 struct key **_key)
301{
d84f4f99 302 const struct cred *cred = current_cred();
76181c13
DH
303 struct key *key;
304 key_ref_t key_ref;
305
306 kenter("%s,%s,,,", type->name, description);
307
308 mutex_lock(&user->cons_lock);
309
d84f4f99
DH
310 key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
311 KEY_POS_ALL, flags);
76181c13
DH
312 if (IS_ERR(key))
313 goto alloc_failed;
314
315 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
316
34574dd1
DH
317 if (dest_keyring)
318 down_write(&dest_keyring->sem);
76181c13
DH
319
320 /* attach the key to the destination keyring under lock, but we do need
321 * to do another check just in case someone beat us to it whilst we
322 * waited for locks */
323 mutex_lock(&key_construction_mutex);
324
d84f4f99 325 key_ref = search_process_keyrings(type, description, type->match, cred);
76181c13
DH
326 if (!IS_ERR(key_ref))
327 goto key_already_present;
328
34574dd1
DH
329 if (dest_keyring)
330 __key_link(dest_keyring, key);
76181c13
DH
331
332 mutex_unlock(&key_construction_mutex);
34574dd1
DH
333 if (dest_keyring)
334 up_write(&dest_keyring->sem);
76181c13
DH
335 mutex_unlock(&user->cons_lock);
336 *_key = key;
337 kleave(" = 0 [%d]", key_serial(key));
338 return 0;
339
340key_already_present:
341 mutex_unlock(&key_construction_mutex);
342 if (dest_keyring)
343 up_write(&dest_keyring->sem);
344 mutex_unlock(&user->cons_lock);
345 key_put(key);
346 *_key = key = key_ref_to_ptr(key_ref);
347 kleave(" = -EINPROGRESS [%d]", key_serial(key));
348 return -EINPROGRESS;
349
350alloc_failed:
351 mutex_unlock(&user->cons_lock);
352 *_key = NULL;
353 kleave(" = %ld", PTR_ERR(key));
354 return PTR_ERR(key);
355}
356
357/*
358 * commence key construction
359 */
360static struct key *construct_key_and_link(struct key_type *type,
361 const char *description,
362 const char *callout_info,
4a38e122 363 size_t callout_len,
76181c13
DH
364 void *aux,
365 struct key *dest_keyring,
366 unsigned long flags)
367{
368 struct key_user *user;
369 struct key *key;
370 int ret;
371
d84f4f99
DH
372 kenter("");
373
1d1e9756 374 user = key_user_lookup(current_fsuid(), current_user_ns());
76181c13
DH
375 if (!user)
376 return ERR_PTR(-ENOMEM);
377
8bbf4976
DH
378 construct_get_dest_keyring(&dest_keyring);
379
76181c13
DH
380 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
381 &key);
382 key_user_put(user);
383
384 if (ret == 0) {
8bbf4976
DH
385 ret = construct_key(key, callout_info, callout_len, aux,
386 dest_keyring);
d84f4f99
DH
387 if (ret < 0) {
388 kdebug("cons failed");
76181c13 389 goto construction_failed;
d84f4f99 390 }
76181c13
DH
391 }
392
8bbf4976 393 key_put(dest_keyring);
d84f4f99 394 kleave(" = key %d", key_serial(key));
76181c13
DH
395 return key;
396
397construction_failed:
398 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
399 key_put(key);
8bbf4976 400 key_put(dest_keyring);
d84f4f99 401 kleave(" = %d", ret);
76181c13
DH
402 return ERR_PTR(ret);
403}
3e30148c 404
1da177e4
LT
405/*
406 * request a key
407 * - search the process's keyrings
408 * - check the list of keys being created or updated
3e30148c
DH
409 * - call out to userspace for a key if supplementary info was provided
410 * - cache the key in an appropriate keyring
1da177e4 411 */
3e30148c
DH
412struct key *request_key_and_link(struct key_type *type,
413 const char *description,
4a38e122
DH
414 const void *callout_info,
415 size_t callout_len,
4e54f085 416 void *aux,
7e047ef5
DH
417 struct key *dest_keyring,
418 unsigned long flags)
1da177e4 419{
d84f4f99 420 const struct cred *cred = current_cred();
1da177e4 421 struct key *key;
664cceb0 422 key_ref_t key_ref;
1da177e4 423
4a38e122
DH
424 kenter("%s,%s,%p,%zu,%p,%p,%lx",
425 type->name, description, callout_info, callout_len, aux,
4e54f085 426 dest_keyring, flags);
3e30148c 427
1da177e4 428 /* search all the process keyrings for a key */
664cceb0 429 key_ref = search_process_keyrings(type, description, type->match,
d84f4f99 430 cred);
1da177e4 431
664cceb0
DH
432 if (!IS_ERR(key_ref)) {
433 key = key_ref_to_ptr(key_ref);
76181c13 434 } else if (PTR_ERR(key_ref) != -EAGAIN) {
e231c2ee 435 key = ERR_CAST(key_ref);
76181c13 436 } else {
1da177e4
LT
437 /* the search failed, but the keyrings were searchable, so we
438 * should consult userspace if we can */
439 key = ERR_PTR(-ENOKEY);
440 if (!callout_info)
441 goto error;
442
76181c13 443 key = construct_key_and_link(type, description, callout_info,
4a38e122
DH
444 callout_len, aux, dest_keyring,
445 flags);
1da177e4
LT
446 }
447
3e30148c
DH
448error:
449 kleave(" = %p", key);
1da177e4 450 return key;
76181c13 451}
1da177e4 452
76181c13
DH
453/*
454 * wait for construction of a key to complete
455 */
456int wait_for_key_construction(struct key *key, bool intr)
457{
458 int ret;
3e30148c 459
76181c13
DH
460 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
461 intr ? key_wait_bit_intr : key_wait_bit,
462 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
463 if (ret < 0)
464 return ret;
465 return key_validate(key);
466}
467EXPORT_SYMBOL(wait_for_key_construction);
3e30148c 468
3e30148c
DH
469/*
470 * request a key
471 * - search the process's keyrings
472 * - check the list of keys being created or updated
473 * - call out to userspace for a key if supplementary info was provided
76181c13 474 * - waits uninterruptible for creation to complete
3e30148c
DH
475 */
476struct key *request_key(struct key_type *type,
477 const char *description,
478 const char *callout_info)
479{
76181c13 480 struct key *key;
4a38e122 481 size_t callout_len = 0;
76181c13
DH
482 int ret;
483
4a38e122
DH
484 if (callout_info)
485 callout_len = strlen(callout_info);
486 key = request_key_and_link(type, description, callout_info, callout_len,
487 NULL, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
488 if (!IS_ERR(key)) {
489 ret = wait_for_key_construction(key, false);
490 if (ret < 0) {
491 key_put(key);
492 return ERR_PTR(ret);
493 }
494 }
495 return key;
496}
1da177e4 497EXPORT_SYMBOL(request_key);
4e54f085 498
4e54f085
DH
499/*
500 * request a key with auxiliary data for the upcaller
501 * - search the process's keyrings
502 * - check the list of keys being created or updated
503 * - call out to userspace for a key if supplementary info was provided
76181c13 504 * - waits uninterruptible for creation to complete
4e54f085
DH
505 */
506struct key *request_key_with_auxdata(struct key_type *type,
507 const char *description,
4a38e122
DH
508 const void *callout_info,
509 size_t callout_len,
4e54f085
DH
510 void *aux)
511{
76181c13
DH
512 struct key *key;
513 int ret;
514
4a38e122
DH
515 key = request_key_and_link(type, description, callout_info, callout_len,
516 aux, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
517 if (!IS_ERR(key)) {
518 ret = wait_for_key_construction(key, false);
519 if (ret < 0) {
520 key_put(key);
521 return ERR_PTR(ret);
522 }
523 }
524 return key;
525}
526EXPORT_SYMBOL(request_key_with_auxdata);
4e54f085 527
76181c13
DH
528/*
529 * request a key (allow async construction)
530 * - search the process's keyrings
531 * - check the list of keys being created or updated
532 * - call out to userspace for a key if supplementary info was provided
533 */
534struct key *request_key_async(struct key_type *type,
535 const char *description,
4a38e122
DH
536 const void *callout_info,
537 size_t callout_len)
76181c13 538{
4a38e122
DH
539 return request_key_and_link(type, description, callout_info,
540 callout_len, NULL, NULL,
541 KEY_ALLOC_IN_QUOTA);
76181c13
DH
542}
543EXPORT_SYMBOL(request_key_async);
4e54f085 544
76181c13
DH
545/*
546 * request a key with auxiliary data for the upcaller (allow async construction)
547 * - search the process's keyrings
548 * - check the list of keys being created or updated
549 * - call out to userspace for a key if supplementary info was provided
550 */
551struct key *request_key_async_with_auxdata(struct key_type *type,
552 const char *description,
4a38e122
DH
553 const void *callout_info,
554 size_t callout_len,
76181c13
DH
555 void *aux)
556{
4a38e122
DH
557 return request_key_and_link(type, description, callout_info,
558 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
76181c13
DH
559}
560EXPORT_SYMBOL(request_key_async_with_auxdata);
This page took 0.431863 seconds and 5 git commands to generate.