xref: /openbmc/linux/security/keys/request_key.c (revision 7743c48e)
1 /* Request a key from userspace
2  *
3  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
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.
10  *
11  * See Documentation/security/keys/request-key.rst
12  */
13 
14 #include <linux/export.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"
21 #include <keys/request_key_auth-type.h>
22 
23 #define key_negative_timeout	60	/* default timeout on a negative key's existence */
24 
25 static struct key *check_cached_key(struct keyring_search_context *ctx)
26 {
27 #ifdef CONFIG_KEYS_REQUEST_CACHE
28 	struct key *key = current->cached_requested_key;
29 
30 	if (key &&
31 	    ctx->match_data.cmp(key, &ctx->match_data) &&
32 	    !(key->flags & ((1 << KEY_FLAG_INVALIDATED) |
33 			    (1 << KEY_FLAG_REVOKED))))
34 		return key_get(key);
35 #endif
36 	return NULL;
37 }
38 
39 static void cache_requested_key(struct key *key)
40 {
41 #ifdef CONFIG_KEYS_REQUEST_CACHE
42 	struct task_struct *t = current;
43 
44 	key_put(t->cached_requested_key);
45 	t->cached_requested_key = key_get(key);
46 	set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
47 #endif
48 }
49 
50 /**
51  * complete_request_key - Complete the construction of a key.
52  * @authkey: The authorisation key.
53  * @error: The success or failute of the construction.
54  *
55  * Complete the attempt to construct a key.  The key will be negated
56  * if an error is indicated.  The authorisation key will be revoked
57  * unconditionally.
58  */
59 void complete_request_key(struct key *authkey, int error)
60 {
61 	struct request_key_auth *rka = get_request_key_auth(authkey);
62 	struct key *key = rka->target_key;
63 
64 	kenter("%d{%d},%d", authkey->serial, key->serial, error);
65 
66 	if (error < 0)
67 		key_negate_and_link(key, key_negative_timeout, NULL, authkey);
68 	else
69 		key_revoke(authkey);
70 }
71 EXPORT_SYMBOL(complete_request_key);
72 
73 /*
74  * Initialise a usermode helper that is going to have a specific session
75  * keyring.
76  *
77  * This is called in context of freshly forked kthread before kernel_execve(),
78  * so we can simply install the desired session_keyring at this point.
79  */
80 static int umh_keys_init(struct subprocess_info *info, struct cred *cred)
81 {
82 	struct key *keyring = info->data;
83 
84 	return install_session_keyring_to_cred(cred, keyring);
85 }
86 
87 /*
88  * Clean up a usermode helper with session keyring.
89  */
90 static void umh_keys_cleanup(struct subprocess_info *info)
91 {
92 	struct key *keyring = info->data;
93 	key_put(keyring);
94 }
95 
96 /*
97  * Call a usermode helper with a specific session keyring.
98  */
99 static int call_usermodehelper_keys(const char *path, char **argv, char **envp,
100 					struct key *session_keyring, int wait)
101 {
102 	struct subprocess_info *info;
103 
104 	info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL,
105 					  umh_keys_init, umh_keys_cleanup,
106 					  session_keyring);
107 	if (!info)
108 		return -ENOMEM;
109 
110 	key_get(session_keyring);
111 	return call_usermodehelper_exec(info, wait);
112 }
113 
114 /*
115  * Request userspace finish the construction of a key
116  * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
117  */
118 static int call_sbin_request_key(struct key *authkey, void *aux)
119 {
120 	static char const request_key[] = "/sbin/request-key";
121 	struct request_key_auth *rka = get_request_key_auth(authkey);
122 	const struct cred *cred = current_cred();
123 	key_serial_t prkey, sskey;
124 	struct key *key = rka->target_key, *keyring, *session;
125 	char *argv[9], *envp[3], uid_str[12], gid_str[12];
126 	char key_str[12], keyring_str[3][12];
127 	char desc[20];
128 	int ret, i;
129 
130 	kenter("{%d},{%d},%s", key->serial, authkey->serial, rka->op);
131 
132 	ret = install_user_keyrings();
133 	if (ret < 0)
134 		goto error_alloc;
135 
136 	/* allocate a new session keyring */
137 	sprintf(desc, "_req.%u", key->serial);
138 
139 	cred = get_current_cred();
140 	keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
141 				KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
142 				KEY_ALLOC_QUOTA_OVERRUN, NULL, NULL);
143 	put_cred(cred);
144 	if (IS_ERR(keyring)) {
145 		ret = PTR_ERR(keyring);
146 		goto error_alloc;
147 	}
148 
149 	/* attach the auth key to the session keyring */
150 	ret = key_link(keyring, authkey);
151 	if (ret < 0)
152 		goto error_link;
153 
154 	/* record the UID and GID */
155 	sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid));
156 	sprintf(gid_str, "%d", from_kgid(&init_user_ns, cred->fsgid));
157 
158 	/* we say which key is under construction */
159 	sprintf(key_str, "%d", key->serial);
160 
161 	/* we specify the process's default keyrings */
162 	sprintf(keyring_str[0], "%d",
163 		cred->thread_keyring ? cred->thread_keyring->serial : 0);
164 
165 	prkey = 0;
166 	if (cred->process_keyring)
167 		prkey = cred->process_keyring->serial;
168 	sprintf(keyring_str[1], "%d", prkey);
169 
170 	session = cred->session_keyring;
171 	if (!session)
172 		session = cred->user->session_keyring;
173 	sskey = session->serial;
174 
175 	sprintf(keyring_str[2], "%d", sskey);
176 
177 	/* set up a minimal environment */
178 	i = 0;
179 	envp[i++] = "HOME=/";
180 	envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
181 	envp[i] = NULL;
182 
183 	/* set up the argument list */
184 	i = 0;
185 	argv[i++] = (char *)request_key;
186 	argv[i++] = (char *)rka->op;
187 	argv[i++] = key_str;
188 	argv[i++] = uid_str;
189 	argv[i++] = gid_str;
190 	argv[i++] = keyring_str[0];
191 	argv[i++] = keyring_str[1];
192 	argv[i++] = keyring_str[2];
193 	argv[i] = NULL;
194 
195 	/* do it */
196 	ret = call_usermodehelper_keys(request_key, argv, envp, keyring,
197 				       UMH_WAIT_PROC);
198 	kdebug("usermode -> 0x%x", ret);
199 	if (ret >= 0) {
200 		/* ret is the exit/wait code */
201 		if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
202 		    key_validate(key) < 0)
203 			ret = -ENOKEY;
204 		else
205 			/* ignore any errors from userspace if the key was
206 			 * instantiated */
207 			ret = 0;
208 	}
209 
210 error_link:
211 	key_put(keyring);
212 
213 error_alloc:
214 	complete_request_key(authkey, ret);
215 	kleave(" = %d", ret);
216 	return ret;
217 }
218 
219 /*
220  * Call out to userspace for key construction.
221  *
222  * Program failure is ignored in favour of key status.
223  */
224 static int construct_key(struct key *key, const void *callout_info,
225 			 size_t callout_len, void *aux,
226 			 struct key *dest_keyring)
227 {
228 	request_key_actor_t actor;
229 	struct key *authkey;
230 	int ret;
231 
232 	kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
233 
234 	/* allocate an authorisation key */
235 	authkey = request_key_auth_new(key, "create", callout_info, callout_len,
236 				       dest_keyring);
237 	if (IS_ERR(authkey))
238 		return PTR_ERR(authkey);
239 
240 	/* Make the call */
241 	actor = call_sbin_request_key;
242 	if (key->type->request_key)
243 		actor = key->type->request_key;
244 
245 	ret = actor(authkey, aux);
246 
247 	/* check that the actor called complete_request_key() prior to
248 	 * returning an error */
249 	WARN_ON(ret < 0 &&
250 		!test_bit(KEY_FLAG_INVALIDATED, &authkey->flags));
251 
252 	key_put(authkey);
253 	kleave(" = %d", ret);
254 	return ret;
255 }
256 
257 /*
258  * Get the appropriate destination keyring for the request.
259  *
260  * The keyring selected is returned with an extra reference upon it which the
261  * caller must release.
262  */
263 static int construct_get_dest_keyring(struct key **_dest_keyring)
264 {
265 	struct request_key_auth *rka;
266 	const struct cred *cred = current_cred();
267 	struct key *dest_keyring = *_dest_keyring, *authkey;
268 	int ret;
269 
270 	kenter("%p", dest_keyring);
271 
272 	/* find the appropriate keyring */
273 	if (dest_keyring) {
274 		/* the caller supplied one */
275 		key_get(dest_keyring);
276 	} else {
277 		bool do_perm_check = true;
278 
279 		/* use a default keyring; falling through the cases until we
280 		 * find one that we actually have */
281 		switch (cred->jit_keyring) {
282 		case KEY_REQKEY_DEFL_DEFAULT:
283 		case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
284 			if (cred->request_key_auth) {
285 				authkey = cred->request_key_auth;
286 				down_read(&authkey->sem);
287 				rka = get_request_key_auth(authkey);
288 				if (!test_bit(KEY_FLAG_REVOKED,
289 					      &authkey->flags))
290 					dest_keyring =
291 						key_get(rka->dest_keyring);
292 				up_read(&authkey->sem);
293 				if (dest_keyring) {
294 					do_perm_check = false;
295 					break;
296 				}
297 			}
298 
299 			/* fall through */
300 		case KEY_REQKEY_DEFL_THREAD_KEYRING:
301 			dest_keyring = key_get(cred->thread_keyring);
302 			if (dest_keyring)
303 				break;
304 
305 			/* fall through */
306 		case KEY_REQKEY_DEFL_PROCESS_KEYRING:
307 			dest_keyring = key_get(cred->process_keyring);
308 			if (dest_keyring)
309 				break;
310 
311 			/* fall through */
312 		case KEY_REQKEY_DEFL_SESSION_KEYRING:
313 			dest_keyring = key_get(cred->session_keyring);
314 
315 			if (dest_keyring)
316 				break;
317 
318 			/* fall through */
319 		case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
320 			dest_keyring =
321 				key_get(READ_ONCE(cred->user->session_keyring));
322 			break;
323 
324 		case KEY_REQKEY_DEFL_USER_KEYRING:
325 			dest_keyring =
326 				key_get(READ_ONCE(cred->user->uid_keyring));
327 			break;
328 
329 		case KEY_REQKEY_DEFL_GROUP_KEYRING:
330 		default:
331 			BUG();
332 		}
333 
334 		/*
335 		 * Require Write permission on the keyring.  This is essential
336 		 * because the default keyring may be the session keyring, and
337 		 * joining a keyring only requires Search permission.
338 		 *
339 		 * However, this check is skipped for the "requestor keyring" so
340 		 * that /sbin/request-key can itself use request_key() to add
341 		 * keys to the original requestor's destination keyring.
342 		 */
343 		if (dest_keyring && do_perm_check) {
344 			ret = key_permission(make_key_ref(dest_keyring, 1),
345 					     KEY_NEED_WRITE);
346 			if (ret) {
347 				key_put(dest_keyring);
348 				return ret;
349 			}
350 		}
351 	}
352 
353 	*_dest_keyring = dest_keyring;
354 	kleave(" [dk %d]", key_serial(dest_keyring));
355 	return 0;
356 }
357 
358 /*
359  * Allocate a new key in under-construction state and attempt to link it in to
360  * the requested keyring.
361  *
362  * May return a key that's already under construction instead if there was a
363  * race between two thread calling request_key().
364  */
365 static int construct_alloc_key(struct keyring_search_context *ctx,
366 			       struct key *dest_keyring,
367 			       unsigned long flags,
368 			       struct key_user *user,
369 			       struct key **_key)
370 {
371 	struct assoc_array_edit *edit = NULL;
372 	struct key *key;
373 	key_perm_t perm;
374 	key_ref_t key_ref;
375 	int ret;
376 
377 	kenter("%s,%s,,,",
378 	       ctx->index_key.type->name, ctx->index_key.description);
379 
380 	*_key = NULL;
381 	mutex_lock(&user->cons_lock);
382 
383 	perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
384 	perm |= KEY_USR_VIEW;
385 	if (ctx->index_key.type->read)
386 		perm |= KEY_POS_READ;
387 	if (ctx->index_key.type == &key_type_keyring ||
388 	    ctx->index_key.type->update)
389 		perm |= KEY_POS_WRITE;
390 
391 	key = key_alloc(ctx->index_key.type, ctx->index_key.description,
392 			ctx->cred->fsuid, ctx->cred->fsgid, ctx->cred,
393 			perm, flags, NULL);
394 	if (IS_ERR(key))
395 		goto alloc_failed;
396 
397 	set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
398 
399 	if (dest_keyring) {
400 		ret = __key_link_lock(dest_keyring, &ctx->index_key);
401 		if (ret < 0)
402 			goto link_lock_failed;
403 		ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit);
404 		if (ret < 0)
405 			goto link_prealloc_failed;
406 	}
407 
408 	/* attach the key to the destination keyring under lock, but we do need
409 	 * to do another check just in case someone beat us to it whilst we
410 	 * waited for locks */
411 	mutex_lock(&key_construction_mutex);
412 
413 	rcu_read_lock();
414 	key_ref = search_process_keyrings_rcu(ctx);
415 	rcu_read_unlock();
416 	if (!IS_ERR(key_ref))
417 		goto key_already_present;
418 
419 	if (dest_keyring)
420 		__key_link(key, &edit);
421 
422 	mutex_unlock(&key_construction_mutex);
423 	if (dest_keyring)
424 		__key_link_end(dest_keyring, &ctx->index_key, edit);
425 	mutex_unlock(&user->cons_lock);
426 	*_key = key;
427 	kleave(" = 0 [%d]", key_serial(key));
428 	return 0;
429 
430 	/* the key is now present - we tell the caller that we found it by
431 	 * returning -EINPROGRESS  */
432 key_already_present:
433 	key_put(key);
434 	mutex_unlock(&key_construction_mutex);
435 	key = key_ref_to_ptr(key_ref);
436 	if (dest_keyring) {
437 		ret = __key_link_check_live_key(dest_keyring, key);
438 		if (ret == 0)
439 			__key_link(key, &edit);
440 		__key_link_end(dest_keyring, &ctx->index_key, edit);
441 		if (ret < 0)
442 			goto link_check_failed;
443 	}
444 	mutex_unlock(&user->cons_lock);
445 	*_key = key;
446 	kleave(" = -EINPROGRESS [%d]", key_serial(key));
447 	return -EINPROGRESS;
448 
449 link_check_failed:
450 	mutex_unlock(&user->cons_lock);
451 	key_put(key);
452 	kleave(" = %d [linkcheck]", ret);
453 	return ret;
454 
455 link_prealloc_failed:
456 	__key_link_end(dest_keyring, &ctx->index_key, edit);
457 link_lock_failed:
458 	mutex_unlock(&user->cons_lock);
459 	key_put(key);
460 	kleave(" = %d [prelink]", ret);
461 	return ret;
462 
463 alloc_failed:
464 	mutex_unlock(&user->cons_lock);
465 	kleave(" = %ld", PTR_ERR(key));
466 	return PTR_ERR(key);
467 }
468 
469 /*
470  * Commence key construction.
471  */
472 static struct key *construct_key_and_link(struct keyring_search_context *ctx,
473 					  const char *callout_info,
474 					  size_t callout_len,
475 					  void *aux,
476 					  struct key *dest_keyring,
477 					  unsigned long flags)
478 {
479 	struct key_user *user;
480 	struct key *key;
481 	int ret;
482 
483 	kenter("");
484 
485 	if (ctx->index_key.type == &key_type_keyring)
486 		return ERR_PTR(-EPERM);
487 
488 	ret = construct_get_dest_keyring(&dest_keyring);
489 	if (ret)
490 		goto error;
491 
492 	user = key_user_lookup(current_fsuid());
493 	if (!user) {
494 		ret = -ENOMEM;
495 		goto error_put_dest_keyring;
496 	}
497 
498 	ret = construct_alloc_key(ctx, dest_keyring, flags, user, &key);
499 	key_user_put(user);
500 
501 	if (ret == 0) {
502 		ret = construct_key(key, callout_info, callout_len, aux,
503 				    dest_keyring);
504 		if (ret < 0) {
505 			kdebug("cons failed");
506 			goto construction_failed;
507 		}
508 	} else if (ret == -EINPROGRESS) {
509 		ret = 0;
510 	} else {
511 		goto error_put_dest_keyring;
512 	}
513 
514 	key_put(dest_keyring);
515 	kleave(" = key %d", key_serial(key));
516 	return key;
517 
518 construction_failed:
519 	key_negate_and_link(key, key_negative_timeout, NULL, NULL);
520 	key_put(key);
521 error_put_dest_keyring:
522 	key_put(dest_keyring);
523 error:
524 	kleave(" = %d", ret);
525 	return ERR_PTR(ret);
526 }
527 
528 /**
529  * request_key_and_link - Request a key and cache it in a keyring.
530  * @type: The type of key we want.
531  * @description: The searchable description of the key.
532  * @callout_info: The data to pass to the instantiation upcall (or NULL).
533  * @callout_len: The length of callout_info.
534  * @aux: Auxiliary data for the upcall.
535  * @dest_keyring: Where to cache the key.
536  * @flags: Flags to key_alloc().
537  *
538  * A key matching the specified criteria is searched for in the process's
539  * keyrings and returned with its usage count incremented if found.  Otherwise,
540  * if callout_info is not NULL, a key will be allocated and some service
541  * (probably in userspace) will be asked to instantiate it.
542  *
543  * If successfully found or created, the key will be linked to the destination
544  * keyring if one is provided.
545  *
546  * Returns a pointer to the key if successful; -EACCES, -ENOKEY, -EKEYREVOKED
547  * or -EKEYEXPIRED if an inaccessible, negative, revoked or expired key was
548  * found; -ENOKEY if no key was found and no @callout_info was given; -EDQUOT
549  * if insufficient key quota was available to create a new key; or -ENOMEM if
550  * insufficient memory was available.
551  *
552  * If the returned key was created, then it may still be under construction,
553  * and wait_for_key_construction() should be used to wait for that to complete.
554  */
555 struct key *request_key_and_link(struct key_type *type,
556 				 const char *description,
557 				 const void *callout_info,
558 				 size_t callout_len,
559 				 void *aux,
560 				 struct key *dest_keyring,
561 				 unsigned long flags)
562 {
563 	struct keyring_search_context ctx = {
564 		.index_key.type		= type,
565 		.index_key.description	= description,
566 		.index_key.desc_len	= strlen(description),
567 		.cred			= current_cred(),
568 		.match_data.cmp		= key_default_cmp,
569 		.match_data.raw_data	= description,
570 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
571 		.flags			= (KEYRING_SEARCH_DO_STATE_CHECK |
572 					   KEYRING_SEARCH_SKIP_EXPIRED),
573 	};
574 	struct key *key;
575 	key_ref_t key_ref;
576 	int ret;
577 
578 	kenter("%s,%s,%p,%zu,%p,%p,%lx",
579 	       ctx.index_key.type->name, ctx.index_key.description,
580 	       callout_info, callout_len, aux, dest_keyring, flags);
581 
582 	if (type->match_preparse) {
583 		ret = type->match_preparse(&ctx.match_data);
584 		if (ret < 0) {
585 			key = ERR_PTR(ret);
586 			goto error;
587 		}
588 	}
589 
590 	key = check_cached_key(&ctx);
591 	if (key)
592 		return key;
593 
594 	/* search all the process keyrings for a key */
595 	rcu_read_lock();
596 	key_ref = search_process_keyrings_rcu(&ctx);
597 	rcu_read_unlock();
598 
599 	if (!IS_ERR(key_ref)) {
600 		if (dest_keyring) {
601 			ret = key_task_permission(key_ref, current_cred(),
602 						  KEY_NEED_LINK);
603 			if (ret < 0) {
604 				key_ref_put(key_ref);
605 				key = ERR_PTR(ret);
606 				goto error_free;
607 			}
608 		}
609 
610 		key = key_ref_to_ptr(key_ref);
611 		if (dest_keyring) {
612 			ret = key_link(dest_keyring, key);
613 			if (ret < 0) {
614 				key_put(key);
615 				key = ERR_PTR(ret);
616 				goto error_free;
617 			}
618 		}
619 
620 		/* Only cache the key on immediate success */
621 		cache_requested_key(key);
622 	} else if (PTR_ERR(key_ref) != -EAGAIN) {
623 		key = ERR_CAST(key_ref);
624 	} else  {
625 		/* the search failed, but the keyrings were searchable, so we
626 		 * should consult userspace if we can */
627 		key = ERR_PTR(-ENOKEY);
628 		if (!callout_info)
629 			goto error_free;
630 
631 		key = construct_key_and_link(&ctx, callout_info, callout_len,
632 					     aux, dest_keyring, flags);
633 	}
634 
635 error_free:
636 	if (type->match_free)
637 		type->match_free(&ctx.match_data);
638 error:
639 	kleave(" = %p", key);
640 	return key;
641 }
642 
643 /**
644  * wait_for_key_construction - Wait for construction of a key to complete
645  * @key: The key being waited for.
646  * @intr: Whether to wait interruptibly.
647  *
648  * Wait for a key to finish being constructed.
649  *
650  * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY
651  * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was
652  * revoked or expired.
653  */
654 int wait_for_key_construction(struct key *key, bool intr)
655 {
656 	int ret;
657 
658 	ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
659 			  intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
660 	if (ret)
661 		return -ERESTARTSYS;
662 	ret = key_read_state(key);
663 	if (ret < 0)
664 		return ret;
665 	return key_validate(key);
666 }
667 EXPORT_SYMBOL(wait_for_key_construction);
668 
669 /**
670  * request_key - Request a key and wait for construction
671  * @type: Type of key.
672  * @description: The searchable description of the key.
673  * @callout_info: The data to pass to the instantiation upcall (or NULL).
674  *
675  * As for request_key_and_link() except that it does not add the returned key
676  * to a keyring if found, new keys are always allocated in the user's quota,
677  * the callout_info must be a NUL-terminated string and no auxiliary data can
678  * be passed.
679  *
680  * Furthermore, it then works as wait_for_key_construction() to wait for the
681  * completion of keys undergoing construction with a non-interruptible wait.
682  */
683 struct key *request_key(struct key_type *type,
684 			const char *description,
685 			const char *callout_info)
686 {
687 	struct key *key;
688 	size_t callout_len = 0;
689 	int ret;
690 
691 	if (callout_info)
692 		callout_len = strlen(callout_info);
693 	key = request_key_and_link(type, description, callout_info, callout_len,
694 				   NULL, NULL, KEY_ALLOC_IN_QUOTA);
695 	if (!IS_ERR(key)) {
696 		ret = wait_for_key_construction(key, false);
697 		if (ret < 0) {
698 			key_put(key);
699 			return ERR_PTR(ret);
700 		}
701 	}
702 	return key;
703 }
704 EXPORT_SYMBOL(request_key);
705 
706 /**
707  * request_key_with_auxdata - Request a key with auxiliary data for the upcaller
708  * @type: The type of key we want.
709  * @description: The searchable description of the key.
710  * @callout_info: The data to pass to the instantiation upcall (or NULL).
711  * @callout_len: The length of callout_info.
712  * @aux: Auxiliary data for the upcall.
713  *
714  * As for request_key_and_link() except that it does not add the returned key
715  * to a keyring if found and new keys are always allocated in the user's quota.
716  *
717  * Furthermore, it then works as wait_for_key_construction() to wait for the
718  * completion of keys undergoing construction with a non-interruptible wait.
719  */
720 struct key *request_key_with_auxdata(struct key_type *type,
721 				     const char *description,
722 				     const void *callout_info,
723 				     size_t callout_len,
724 				     void *aux)
725 {
726 	struct key *key;
727 	int ret;
728 
729 	key = request_key_and_link(type, description, callout_info, callout_len,
730 				   aux, NULL, KEY_ALLOC_IN_QUOTA);
731 	if (!IS_ERR(key)) {
732 		ret = wait_for_key_construction(key, false);
733 		if (ret < 0) {
734 			key_put(key);
735 			return ERR_PTR(ret);
736 		}
737 	}
738 	return key;
739 }
740 EXPORT_SYMBOL(request_key_with_auxdata);
741 
742 /*
743  * request_key_async - Request a key (allow async construction)
744  * @type: Type of key.
745  * @description: The searchable description of the key.
746  * @callout_info: The data to pass to the instantiation upcall (or NULL).
747  * @callout_len: The length of callout_info.
748  *
749  * As for request_key_and_link() except that it does not add the returned key
750  * to a keyring if found, new keys are always allocated in the user's quota and
751  * no auxiliary data can be passed.
752  *
753  * The caller should call wait_for_key_construction() to wait for the
754  * completion of the returned key if it is still undergoing construction.
755  */
756 struct key *request_key_async(struct key_type *type,
757 			      const char *description,
758 			      const void *callout_info,
759 			      size_t callout_len)
760 {
761 	return request_key_and_link(type, description, callout_info,
762 				    callout_len, NULL, NULL,
763 				    KEY_ALLOC_IN_QUOTA);
764 }
765 EXPORT_SYMBOL(request_key_async);
766 
767 /*
768  * request a key with auxiliary data for the upcaller (allow async construction)
769  * @type: Type of key.
770  * @description: The searchable description of the key.
771  * @callout_info: The data to pass to the instantiation upcall (or NULL).
772  * @callout_len: The length of callout_info.
773  * @aux: Auxiliary data for the upcall.
774  *
775  * As for request_key_and_link() except that it does not add the returned key
776  * to a keyring if found and new keys are always allocated in the user's quota.
777  *
778  * The caller should call wait_for_key_construction() to wait for the
779  * completion of the returned key if it is still undergoing construction.
780  */
781 struct key *request_key_async_with_auxdata(struct key_type *type,
782 					   const char *description,
783 					   const void *callout_info,
784 					   size_t callout_len,
785 					   void *aux)
786 {
787 	return request_key_and_link(type, description, callout_info,
788 				    callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
789 }
790 EXPORT_SYMBOL(request_key_async_with_auxdata);
791 
792 /**
793  * request_key_rcu - Request key from RCU-read-locked context
794  * @type: The type of key we want.
795  * @description: The name of the key we want.
796  *
797  * Request a key from a context that we may not sleep in (such as RCU-mode
798  * pathwalk).  Keys under construction are ignored.
799  *
800  * Return a pointer to the found key if successful, -ENOKEY if we couldn't find
801  * a key or some other error if the key found was unsuitable or inaccessible.
802  */
803 struct key *request_key_rcu(struct key_type *type, const char *description)
804 {
805 	struct keyring_search_context ctx = {
806 		.index_key.type		= type,
807 		.index_key.description	= description,
808 		.index_key.desc_len	= strlen(description),
809 		.cred			= current_cred(),
810 		.match_data.cmp		= key_default_cmp,
811 		.match_data.raw_data	= description,
812 		.match_data.lookup_type	= KEYRING_SEARCH_LOOKUP_DIRECT,
813 		.flags			= (KEYRING_SEARCH_DO_STATE_CHECK |
814 					   KEYRING_SEARCH_SKIP_EXPIRED),
815 	};
816 	struct key *key;
817 	key_ref_t key_ref;
818 
819 	kenter("%s,%s", type->name, description);
820 
821 	key = check_cached_key(&ctx);
822 	if (key)
823 		return key;
824 
825 	/* search all the process keyrings for a key */
826 	key_ref = search_process_keyrings_rcu(&ctx);
827 	if (IS_ERR(key_ref)) {
828 		key = ERR_CAST(key_ref);
829 		if (PTR_ERR(key_ref) == -EAGAIN)
830 			key = ERR_PTR(-ENOKEY);
831 	} else {
832 		key = key_ref_to_ptr(key_ref);
833 		cache_requested_key(key);
834 	}
835 
836 	kleave(" = %p", key);
837 	return key;
838 }
839 EXPORT_SYMBOL(request_key_rcu);
840