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