• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Userspace key control operations
2  *
3  * Copyright (C) 2004-5 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 
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/key.h>
18 #include <linux/keyctl.h>
19 #include <linux/fs.h>
20 #include <linux/capability.h>
21 #include <linux/string.h>
22 #include <linux/err.h>
23 #include <linux/vmalloc.h>
24 #include <linux/security.h>
25 #include <linux/uio.h>
26 #include <asm/uaccess.h>
27 #include "internal.h"
28 
29 #define KEY_MAX_DESC_SIZE 4096
30 
key_get_type_from_user(char * type,const char __user * _type,unsigned len)31 static int key_get_type_from_user(char *type,
32 				  const char __user *_type,
33 				  unsigned len)
34 {
35 	int ret;
36 
37 	ret = strncpy_from_user(type, _type, len);
38 	if (ret < 0)
39 		return ret;
40 	if (ret == 0 || ret >= len)
41 		return -EINVAL;
42 	if (type[0] == '.')
43 		return -EPERM;
44 	type[len - 1] = '\0';
45 	return 0;
46 }
47 
48 /*
49  * Extract the description of a new key from userspace and either add it as a
50  * new key to the specified keyring or update a matching key in that keyring.
51  *
52  * If the description is NULL or an empty string, the key type is asked to
53  * generate one from the payload.
54  *
55  * The keyring must be writable so that we can attach the key to it.
56  *
57  * If successful, the new key's serial number is returned, otherwise an error
58  * code is returned.
59  */
SYSCALL_DEFINE5(add_key,const char __user *,_type,const char __user *,_description,const void __user *,_payload,size_t,plen,key_serial_t,ringid)60 SYSCALL_DEFINE5(add_key, const char __user *, _type,
61 		const char __user *, _description,
62 		const void __user *, _payload,
63 		size_t, plen,
64 		key_serial_t, ringid)
65 {
66 	key_ref_t keyring_ref, key_ref;
67 	char type[32], *description;
68 	void *payload;
69 	long ret;
70 	bool vm;
71 
72 	ret = -EINVAL;
73 	if (plen > 1024 * 1024 - 1)
74 		goto error;
75 
76 	/* draw all the data into kernel space */
77 	ret = key_get_type_from_user(type, _type, sizeof(type));
78 	if (ret < 0)
79 		goto error;
80 
81 	description = NULL;
82 	if (_description) {
83 		description = strndup_user(_description, KEY_MAX_DESC_SIZE);
84 		if (IS_ERR(description)) {
85 			ret = PTR_ERR(description);
86 			goto error;
87 		}
88 		if (!*description) {
89 			kfree(description);
90 			description = NULL;
91 		} else if ((description[0] == '.') &&
92 			   (strncmp(type, "keyring", 7) == 0)) {
93 			ret = -EPERM;
94 			goto error2;
95 		}
96 	}
97 
98 	/* pull the payload in if one was supplied */
99 	payload = NULL;
100 
101 	vm = false;
102 	if (plen) {
103 		ret = -ENOMEM;
104 		payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
105 		if (!payload) {
106 			if (plen <= PAGE_SIZE)
107 				goto error2;
108 			vm = true;
109 			payload = vmalloc(plen);
110 			if (!payload)
111 				goto error2;
112 		}
113 
114 		ret = -EFAULT;
115 		if (copy_from_user(payload, _payload, plen) != 0)
116 			goto error3;
117 	}
118 
119 	/* find the target keyring (which must be writable) */
120 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
121 	if (IS_ERR(keyring_ref)) {
122 		ret = PTR_ERR(keyring_ref);
123 		goto error3;
124 	}
125 
126 	/* create or update the requested key and add it to the target
127 	 * keyring */
128 	key_ref = key_create_or_update(keyring_ref, type, description,
129 				       payload, plen, KEY_PERM_UNDEF,
130 				       KEY_ALLOC_IN_QUOTA);
131 	if (!IS_ERR(key_ref)) {
132 		ret = key_ref_to_ptr(key_ref)->serial;
133 		key_ref_put(key_ref);
134 	}
135 	else {
136 		ret = PTR_ERR(key_ref);
137 	}
138 
139 	key_ref_put(keyring_ref);
140  error3:
141 	if (!vm)
142 		kfree(payload);
143 	else
144 		vfree(payload);
145  error2:
146 	kfree(description);
147  error:
148 	return ret;
149 }
150 
151 /*
152  * Search the process keyrings and keyring trees linked from those for a
153  * matching key.  Keyrings must have appropriate Search permission to be
154  * searched.
155  *
156  * If a key is found, it will be attached to the destination keyring if there's
157  * one specified and the serial number of the key will be returned.
158  *
159  * If no key is found, /sbin/request-key will be invoked if _callout_info is
160  * non-NULL in an attempt to create a key.  The _callout_info string will be
161  * passed to /sbin/request-key to aid with completing the request.  If the
162  * _callout_info string is "" then it will be changed to "-".
163  */
SYSCALL_DEFINE4(request_key,const char __user *,_type,const char __user *,_description,const char __user *,_callout_info,key_serial_t,destringid)164 SYSCALL_DEFINE4(request_key, const char __user *, _type,
165 		const char __user *, _description,
166 		const char __user *, _callout_info,
167 		key_serial_t, destringid)
168 {
169 	struct key_type *ktype;
170 	struct key *key;
171 	key_ref_t dest_ref;
172 	size_t callout_len;
173 	char type[32], *description, *callout_info;
174 	long ret;
175 
176 	/* pull the type into kernel space */
177 	ret = key_get_type_from_user(type, _type, sizeof(type));
178 	if (ret < 0)
179 		goto error;
180 
181 	/* pull the description into kernel space */
182 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
183 	if (IS_ERR(description)) {
184 		ret = PTR_ERR(description);
185 		goto error;
186 	}
187 
188 	/* pull the callout info into kernel space */
189 	callout_info = NULL;
190 	callout_len = 0;
191 	if (_callout_info) {
192 		callout_info = strndup_user(_callout_info, PAGE_SIZE);
193 		if (IS_ERR(callout_info)) {
194 			ret = PTR_ERR(callout_info);
195 			goto error2;
196 		}
197 		callout_len = strlen(callout_info);
198 	}
199 
200 	/* get the destination keyring if specified */
201 	dest_ref = NULL;
202 	if (destringid) {
203 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
204 					   KEY_NEED_WRITE);
205 		if (IS_ERR(dest_ref)) {
206 			ret = PTR_ERR(dest_ref);
207 			goto error3;
208 		}
209 	}
210 
211 	/* find the key type */
212 	ktype = key_type_lookup(type);
213 	if (IS_ERR(ktype)) {
214 		ret = PTR_ERR(ktype);
215 		goto error4;
216 	}
217 
218 	/* do the search */
219 	key = request_key_and_link(ktype, description, callout_info,
220 				   callout_len, NULL, key_ref_to_ptr(dest_ref),
221 				   KEY_ALLOC_IN_QUOTA);
222 	if (IS_ERR(key)) {
223 		ret = PTR_ERR(key);
224 		goto error5;
225 	}
226 
227 	/* wait for the key to finish being constructed */
228 	ret = wait_for_key_construction(key, 1);
229 	if (ret < 0)
230 		goto error6;
231 
232 	ret = key->serial;
233 
234 error6:
235  	key_put(key);
236 error5:
237 	key_type_put(ktype);
238 error4:
239 	key_ref_put(dest_ref);
240 error3:
241 	kfree(callout_info);
242 error2:
243 	kfree(description);
244 error:
245 	return ret;
246 }
247 
248 /*
249  * Get the ID of the specified process keyring.
250  *
251  * The requested keyring must have search permission to be found.
252  *
253  * If successful, the ID of the requested keyring will be returned.
254  */
keyctl_get_keyring_ID(key_serial_t id,int create)255 long keyctl_get_keyring_ID(key_serial_t id, int create)
256 {
257 	key_ref_t key_ref;
258 	unsigned long lflags;
259 	long ret;
260 
261 	lflags = create ? KEY_LOOKUP_CREATE : 0;
262 	key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
263 	if (IS_ERR(key_ref)) {
264 		ret = PTR_ERR(key_ref);
265 		goto error;
266 	}
267 
268 	ret = key_ref_to_ptr(key_ref)->serial;
269 	key_ref_put(key_ref);
270 error:
271 	return ret;
272 }
273 
274 /*
275  * Join a (named) session keyring.
276  *
277  * Create and join an anonymous session keyring or join a named session
278  * keyring, creating it if necessary.  A named session keyring must have Search
279  * permission for it to be joined.  Session keyrings without this permit will
280  * be skipped over.  It is not permitted for userspace to create or join
281  * keyrings whose name begin with a dot.
282  *
283  * If successful, the ID of the joined session keyring will be returned.
284  */
keyctl_join_session_keyring(const char __user * _name)285 long keyctl_join_session_keyring(const char __user *_name)
286 {
287 	char *name;
288 	long ret;
289 
290 	/* fetch the name from userspace */
291 	name = NULL;
292 	if (_name) {
293 		name = strndup_user(_name, KEY_MAX_DESC_SIZE);
294 		if (IS_ERR(name)) {
295 			ret = PTR_ERR(name);
296 			goto error;
297 		}
298 
299 		ret = -EPERM;
300 		if (name[0] == '.')
301 			goto error_name;
302 	}
303 
304 	/* join the session */
305 	ret = join_session_keyring(name);
306 error_name:
307 	kfree(name);
308 error:
309 	return ret;
310 }
311 
312 /*
313  * Update a key's data payload from the given data.
314  *
315  * The key must grant the caller Write permission and the key type must support
316  * updating for this to work.  A negative key can be positively instantiated
317  * with this call.
318  *
319  * If successful, 0 will be returned.  If the key type does not support
320  * updating, then -EOPNOTSUPP will be returned.
321  */
keyctl_update_key(key_serial_t id,const void __user * _payload,size_t plen)322 long keyctl_update_key(key_serial_t id,
323 		       const void __user *_payload,
324 		       size_t plen)
325 {
326 	key_ref_t key_ref;
327 	void *payload;
328 	long ret;
329 
330 	ret = -EINVAL;
331 	if (plen > PAGE_SIZE)
332 		goto error;
333 
334 	/* pull the payload in if one was supplied */
335 	payload = NULL;
336 	if (plen) {
337 		ret = -ENOMEM;
338 		payload = kmalloc(plen, GFP_KERNEL);
339 		if (!payload)
340 			goto error;
341 
342 		ret = -EFAULT;
343 		if (copy_from_user(payload, _payload, plen) != 0)
344 			goto error2;
345 	}
346 
347 	/* find the target key (which must be writable) */
348 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
349 	if (IS_ERR(key_ref)) {
350 		ret = PTR_ERR(key_ref);
351 		goto error2;
352 	}
353 
354 	/* update the key */
355 	ret = key_update(key_ref, payload, plen);
356 
357 	key_ref_put(key_ref);
358 error2:
359 	kfree(payload);
360 error:
361 	return ret;
362 }
363 
364 /*
365  * Revoke a key.
366  *
367  * The key must be grant the caller Write or Setattr permission for this to
368  * work.  The key type should give up its quota claim when revoked.  The key
369  * and any links to the key will be automatically garbage collected after a
370  * certain amount of time (/proc/sys/kernel/keys/gc_delay).
371  *
372  * If successful, 0 is returned.
373  */
keyctl_revoke_key(key_serial_t id)374 long keyctl_revoke_key(key_serial_t id)
375 {
376 	key_ref_t key_ref;
377 	long ret;
378 
379 	key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
380 	if (IS_ERR(key_ref)) {
381 		ret = PTR_ERR(key_ref);
382 		if (ret != -EACCES)
383 			goto error;
384 		key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
385 		if (IS_ERR(key_ref)) {
386 			ret = PTR_ERR(key_ref);
387 			goto error;
388 		}
389 	}
390 
391 	key_revoke(key_ref_to_ptr(key_ref));
392 	ret = 0;
393 
394 	key_ref_put(key_ref);
395 error:
396 	return ret;
397 }
398 
399 /*
400  * Invalidate a key.
401  *
402  * The key must be grant the caller Invalidate permission for this to work.
403  * The key and any links to the key will be automatically garbage collected
404  * immediately.
405  *
406  * If successful, 0 is returned.
407  */
keyctl_invalidate_key(key_serial_t id)408 long keyctl_invalidate_key(key_serial_t id)
409 {
410 	key_ref_t key_ref;
411 	long ret;
412 
413 	kenter("%d", id);
414 
415 	key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
416 	if (IS_ERR(key_ref)) {
417 		ret = PTR_ERR(key_ref);
418 
419 		/* Root is permitted to invalidate certain special keys */
420 		if (capable(CAP_SYS_ADMIN)) {
421 			key_ref = lookup_user_key(id, 0, 0);
422 			if (IS_ERR(key_ref))
423 				goto error;
424 			if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
425 				     &key_ref_to_ptr(key_ref)->flags))
426 				goto invalidate;
427 			goto error_put;
428 		}
429 
430 		goto error;
431 	}
432 
433 invalidate:
434 	key_invalidate(key_ref_to_ptr(key_ref));
435 	ret = 0;
436 error_put:
437 	key_ref_put(key_ref);
438 error:
439 	kleave(" = %ld", ret);
440 	return ret;
441 }
442 
443 /*
444  * Clear the specified keyring, creating an empty process keyring if one of the
445  * special keyring IDs is used.
446  *
447  * The keyring must grant the caller Write permission for this to work.  If
448  * successful, 0 will be returned.
449  */
keyctl_keyring_clear(key_serial_t ringid)450 long keyctl_keyring_clear(key_serial_t ringid)
451 {
452 	key_ref_t keyring_ref;
453 	long ret;
454 
455 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
456 	if (IS_ERR(keyring_ref)) {
457 		ret = PTR_ERR(keyring_ref);
458 
459 		/* Root is permitted to invalidate certain special keyrings */
460 		if (capable(CAP_SYS_ADMIN)) {
461 			keyring_ref = lookup_user_key(ringid, 0, 0);
462 			if (IS_ERR(keyring_ref))
463 				goto error;
464 			if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
465 				     &key_ref_to_ptr(keyring_ref)->flags))
466 				goto clear;
467 			goto error_put;
468 		}
469 
470 		goto error;
471 	}
472 
473 clear:
474 	ret = keyring_clear(key_ref_to_ptr(keyring_ref));
475 error_put:
476 	key_ref_put(keyring_ref);
477 error:
478 	return ret;
479 }
480 
481 /*
482  * Create a link from a keyring to a key if there's no matching key in the
483  * keyring, otherwise replace the link to the matching key with a link to the
484  * new key.
485  *
486  * The key must grant the caller Link permission and the the keyring must grant
487  * the caller Write permission.  Furthermore, if an additional link is created,
488  * the keyring's quota will be extended.
489  *
490  * If successful, 0 will be returned.
491  */
keyctl_keyring_link(key_serial_t id,key_serial_t ringid)492 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
493 {
494 	key_ref_t keyring_ref, key_ref;
495 	long ret;
496 
497 	keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
498 	if (IS_ERR(keyring_ref)) {
499 		ret = PTR_ERR(keyring_ref);
500 		goto error;
501 	}
502 
503 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
504 	if (IS_ERR(key_ref)) {
505 		ret = PTR_ERR(key_ref);
506 		goto error2;
507 	}
508 
509 	ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
510 
511 	key_ref_put(key_ref);
512 error2:
513 	key_ref_put(keyring_ref);
514 error:
515 	return ret;
516 }
517 
518 /*
519  * Unlink a key from a keyring.
520  *
521  * The keyring must grant the caller Write permission for this to work; the key
522  * itself need not grant the caller anything.  If the last link to a key is
523  * removed then that key will be scheduled for destruction.
524  *
525  * If successful, 0 will be returned.
526  */
keyctl_keyring_unlink(key_serial_t id,key_serial_t ringid)527 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
528 {
529 	key_ref_t keyring_ref, key_ref;
530 	long ret;
531 
532 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
533 	if (IS_ERR(keyring_ref)) {
534 		ret = PTR_ERR(keyring_ref);
535 		goto error;
536 	}
537 
538 	key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
539 	if (IS_ERR(key_ref)) {
540 		ret = PTR_ERR(key_ref);
541 		goto error2;
542 	}
543 
544 	ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
545 
546 	key_ref_put(key_ref);
547 error2:
548 	key_ref_put(keyring_ref);
549 error:
550 	return ret;
551 }
552 
553 /*
554  * Return a description of a key to userspace.
555  *
556  * The key must grant the caller View permission for this to work.
557  *
558  * If there's a buffer, we place up to buflen bytes of data into it formatted
559  * in the following way:
560  *
561  *	type;uid;gid;perm;description<NUL>
562  *
563  * If successful, we return the amount of description available, irrespective
564  * of how much we may have copied into the buffer.
565  */
keyctl_describe_key(key_serial_t keyid,char __user * buffer,size_t buflen)566 long keyctl_describe_key(key_serial_t keyid,
567 			 char __user *buffer,
568 			 size_t buflen)
569 {
570 	struct key *key, *instkey;
571 	key_ref_t key_ref;
572 	char *infobuf;
573 	long ret;
574 	int desclen, infolen;
575 
576 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
577 	if (IS_ERR(key_ref)) {
578 		/* viewing a key under construction is permitted if we have the
579 		 * authorisation token handy */
580 		if (PTR_ERR(key_ref) == -EACCES) {
581 			instkey = key_get_instantiation_authkey(keyid);
582 			if (!IS_ERR(instkey)) {
583 				key_put(instkey);
584 				key_ref = lookup_user_key(keyid,
585 							  KEY_LOOKUP_PARTIAL,
586 							  0);
587 				if (!IS_ERR(key_ref))
588 					goto okay;
589 			}
590 		}
591 
592 		ret = PTR_ERR(key_ref);
593 		goto error;
594 	}
595 
596 okay:
597 	key = key_ref_to_ptr(key_ref);
598 	desclen = strlen(key->description);
599 
600 	/* calculate how much information we're going to return */
601 	ret = -ENOMEM;
602 	infobuf = kasprintf(GFP_KERNEL,
603 			    "%s;%d;%d;%08x;",
604 			    key->type->name,
605 			    from_kuid_munged(current_user_ns(), key->uid),
606 			    from_kgid_munged(current_user_ns(), key->gid),
607 			    key->perm);
608 	if (!infobuf)
609 		goto error2;
610 	infolen = strlen(infobuf);
611 	ret = infolen + desclen + 1;
612 
613 	/* consider returning the data */
614 	if (buffer && buflen >= ret) {
615 		if (copy_to_user(buffer, infobuf, infolen) != 0 ||
616 		    copy_to_user(buffer + infolen, key->description,
617 				 desclen + 1) != 0)
618 			ret = -EFAULT;
619 	}
620 
621 	kfree(infobuf);
622 error2:
623 	key_ref_put(key_ref);
624 error:
625 	return ret;
626 }
627 
628 /*
629  * Search the specified keyring and any keyrings it links to for a matching
630  * key.  Only keyrings that grant the caller Search permission will be searched
631  * (this includes the starting keyring).  Only keys with Search permission can
632  * be found.
633  *
634  * If successful, the found key will be linked to the destination keyring if
635  * supplied and the key has Link permission, and the found key ID will be
636  * returned.
637  */
keyctl_keyring_search(key_serial_t ringid,const char __user * _type,const char __user * _description,key_serial_t destringid)638 long keyctl_keyring_search(key_serial_t ringid,
639 			   const char __user *_type,
640 			   const char __user *_description,
641 			   key_serial_t destringid)
642 {
643 	struct key_type *ktype;
644 	key_ref_t keyring_ref, key_ref, dest_ref;
645 	char type[32], *description;
646 	long ret;
647 
648 	/* pull the type and description into kernel space */
649 	ret = key_get_type_from_user(type, _type, sizeof(type));
650 	if (ret < 0)
651 		goto error;
652 
653 	description = strndup_user(_description, KEY_MAX_DESC_SIZE);
654 	if (IS_ERR(description)) {
655 		ret = PTR_ERR(description);
656 		goto error;
657 	}
658 
659 	/* get the keyring at which to begin the search */
660 	keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
661 	if (IS_ERR(keyring_ref)) {
662 		ret = PTR_ERR(keyring_ref);
663 		goto error2;
664 	}
665 
666 	/* get the destination keyring if specified */
667 	dest_ref = NULL;
668 	if (destringid) {
669 		dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
670 					   KEY_NEED_WRITE);
671 		if (IS_ERR(dest_ref)) {
672 			ret = PTR_ERR(dest_ref);
673 			goto error3;
674 		}
675 	}
676 
677 	/* find the key type */
678 	ktype = key_type_lookup(type);
679 	if (IS_ERR(ktype)) {
680 		ret = PTR_ERR(ktype);
681 		goto error4;
682 	}
683 
684 	/* do the search */
685 	key_ref = keyring_search(keyring_ref, ktype, description);
686 	if (IS_ERR(key_ref)) {
687 		ret = PTR_ERR(key_ref);
688 
689 		/* treat lack or presence of a negative key the same */
690 		if (ret == -EAGAIN)
691 			ret = -ENOKEY;
692 		goto error5;
693 	}
694 
695 	/* link the resulting key to the destination keyring if we can */
696 	if (dest_ref) {
697 		ret = key_permission(key_ref, KEY_NEED_LINK);
698 		if (ret < 0)
699 			goto error6;
700 
701 		ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
702 		if (ret < 0)
703 			goto error6;
704 	}
705 
706 	ret = key_ref_to_ptr(key_ref)->serial;
707 
708 error6:
709 	key_ref_put(key_ref);
710 error5:
711 	key_type_put(ktype);
712 error4:
713 	key_ref_put(dest_ref);
714 error3:
715 	key_ref_put(keyring_ref);
716 error2:
717 	kfree(description);
718 error:
719 	return ret;
720 }
721 
722 /*
723  * Read a key's payload.
724  *
725  * The key must either grant the caller Read permission, or it must grant the
726  * caller Search permission when searched for from the process keyrings.
727  *
728  * If successful, we place up to buflen bytes of data into the buffer, if one
729  * is provided, and return the amount of data that is available in the key,
730  * irrespective of how much we copied into the buffer.
731  */
keyctl_read_key(key_serial_t keyid,char __user * buffer,size_t buflen)732 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
733 {
734 	struct key *key;
735 	key_ref_t key_ref;
736 	long ret;
737 
738 	/* find the key first */
739 	key_ref = lookup_user_key(keyid, 0, 0);
740 	if (IS_ERR(key_ref)) {
741 		ret = -ENOKEY;
742 		goto error;
743 	}
744 
745 	key = key_ref_to_ptr(key_ref);
746 
747 	if (test_bit(KEY_FLAG_NEGATIVE, &key->flags)) {
748 		ret = -ENOKEY;
749 		goto error2;
750 	}
751 
752 	/* see if we can read it directly */
753 	ret = key_permission(key_ref, KEY_NEED_READ);
754 	if (ret == 0)
755 		goto can_read_key;
756 	if (ret != -EACCES)
757 		goto error;
758 
759 	/* we can't; see if it's searchable from this process's keyrings
760 	 * - we automatically take account of the fact that it may be
761 	 *   dangling off an instantiation key
762 	 */
763 	if (!is_key_possessed(key_ref)) {
764 		ret = -EACCES;
765 		goto error2;
766 	}
767 
768 	/* the key is probably readable - now try to read it */
769 can_read_key:
770 	ret = -EOPNOTSUPP;
771 	if (key->type->read) {
772 		/* Read the data with the semaphore held (since we might sleep)
773 		 * to protect against the key being updated or revoked.
774 		 */
775 		down_read(&key->sem);
776 		ret = key_validate(key);
777 		if (ret == 0)
778 			ret = key->type->read(key, buffer, buflen);
779 		up_read(&key->sem);
780 	}
781 
782 error2:
783 	key_put(key);
784 error:
785 	return ret;
786 }
787 
788 /*
789  * Change the ownership of a key
790  *
791  * The key must grant the caller Setattr permission for this to work, though
792  * the key need not be fully instantiated yet.  For the UID to be changed, or
793  * for the GID to be changed to a group the caller is not a member of, the
794  * caller must have sysadmin capability.  If either uid or gid is -1 then that
795  * attribute is not changed.
796  *
797  * If the UID is to be changed, the new user must have sufficient quota to
798  * accept the key.  The quota deduction will be removed from the old user to
799  * the new user should the attribute be changed.
800  *
801  * If successful, 0 will be returned.
802  */
keyctl_chown_key(key_serial_t id,uid_t user,gid_t group)803 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
804 {
805 	struct key_user *newowner, *zapowner = NULL;
806 	struct key *key;
807 	key_ref_t key_ref;
808 	long ret;
809 	kuid_t uid;
810 	kgid_t gid;
811 
812 	uid = make_kuid(current_user_ns(), user);
813 	gid = make_kgid(current_user_ns(), group);
814 	ret = -EINVAL;
815 	if ((user != (uid_t) -1) && !uid_valid(uid))
816 		goto error;
817 	if ((group != (gid_t) -1) && !gid_valid(gid))
818 		goto error;
819 
820 	ret = 0;
821 	if (user == (uid_t) -1 && group == (gid_t) -1)
822 		goto error;
823 
824 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
825 				  KEY_NEED_SETATTR);
826 	if (IS_ERR(key_ref)) {
827 		ret = PTR_ERR(key_ref);
828 		goto error;
829 	}
830 
831 	key = key_ref_to_ptr(key_ref);
832 
833 	/* make the changes with the locks held to prevent chown/chown races */
834 	ret = -EACCES;
835 	down_write(&key->sem);
836 
837 	if (!capable(CAP_SYS_ADMIN)) {
838 		/* only the sysadmin can chown a key to some other UID */
839 		if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
840 			goto error_put;
841 
842 		/* only the sysadmin can set the key's GID to a group other
843 		 * than one of those that the current process subscribes to */
844 		if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
845 			goto error_put;
846 	}
847 
848 	/* change the UID */
849 	if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
850 		ret = -ENOMEM;
851 		newowner = key_user_lookup(uid);
852 		if (!newowner)
853 			goto error_put;
854 
855 		/* transfer the quota burden to the new user */
856 		if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
857 			unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
858 				key_quota_root_maxkeys : key_quota_maxkeys;
859 			unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
860 				key_quota_root_maxbytes : key_quota_maxbytes;
861 
862 			spin_lock(&newowner->lock);
863 			if (newowner->qnkeys + 1 >= maxkeys ||
864 			    newowner->qnbytes + key->quotalen >= maxbytes ||
865 			    newowner->qnbytes + key->quotalen <
866 			    newowner->qnbytes)
867 				goto quota_overrun;
868 
869 			newowner->qnkeys++;
870 			newowner->qnbytes += key->quotalen;
871 			spin_unlock(&newowner->lock);
872 
873 			spin_lock(&key->user->lock);
874 			key->user->qnkeys--;
875 			key->user->qnbytes -= key->quotalen;
876 			spin_unlock(&key->user->lock);
877 		}
878 
879 		atomic_dec(&key->user->nkeys);
880 		atomic_inc(&newowner->nkeys);
881 
882 		if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
883 			atomic_dec(&key->user->nikeys);
884 			atomic_inc(&newowner->nikeys);
885 		}
886 
887 		zapowner = key->user;
888 		key->user = newowner;
889 		key->uid = uid;
890 	}
891 
892 	/* change the GID */
893 	if (group != (gid_t) -1)
894 		key->gid = gid;
895 
896 	ret = 0;
897 
898 error_put:
899 	up_write(&key->sem);
900 	key_put(key);
901 	if (zapowner)
902 		key_user_put(zapowner);
903 error:
904 	return ret;
905 
906 quota_overrun:
907 	spin_unlock(&newowner->lock);
908 	zapowner = newowner;
909 	ret = -EDQUOT;
910 	goto error_put;
911 }
912 
913 /*
914  * Change the permission mask on a key.
915  *
916  * The key must grant the caller Setattr permission for this to work, though
917  * the key need not be fully instantiated yet.  If the caller does not have
918  * sysadmin capability, it may only change the permission on keys that it owns.
919  */
keyctl_setperm_key(key_serial_t id,key_perm_t perm)920 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
921 {
922 	struct key *key;
923 	key_ref_t key_ref;
924 	long ret;
925 
926 	ret = -EINVAL;
927 	if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
928 		goto error;
929 
930 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
931 				  KEY_NEED_SETATTR);
932 	if (IS_ERR(key_ref)) {
933 		ret = PTR_ERR(key_ref);
934 		goto error;
935 	}
936 
937 	key = key_ref_to_ptr(key_ref);
938 
939 	/* make the changes with the locks held to prevent chown/chmod races */
940 	ret = -EACCES;
941 	down_write(&key->sem);
942 
943 	/* if we're not the sysadmin, we can only change a key that we own */
944 	if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
945 		key->perm = perm;
946 		ret = 0;
947 	}
948 
949 	up_write(&key->sem);
950 	key_put(key);
951 error:
952 	return ret;
953 }
954 
955 /*
956  * Get the destination keyring for instantiation and check that the caller has
957  * Write permission on it.
958  */
get_instantiation_keyring(key_serial_t ringid,struct request_key_auth * rka,struct key ** _dest_keyring)959 static long get_instantiation_keyring(key_serial_t ringid,
960 				      struct request_key_auth *rka,
961 				      struct key **_dest_keyring)
962 {
963 	key_ref_t dkref;
964 
965 	*_dest_keyring = NULL;
966 
967 	/* just return a NULL pointer if we weren't asked to make a link */
968 	if (ringid == 0)
969 		return 0;
970 
971 	/* if a specific keyring is nominated by ID, then use that */
972 	if (ringid > 0) {
973 		dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
974 		if (IS_ERR(dkref))
975 			return PTR_ERR(dkref);
976 		*_dest_keyring = key_ref_to_ptr(dkref);
977 		return 0;
978 	}
979 
980 	if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
981 		return -EINVAL;
982 
983 	/* otherwise specify the destination keyring recorded in the
984 	 * authorisation key (any KEY_SPEC_*_KEYRING) */
985 	if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
986 		*_dest_keyring = key_get(rka->dest_keyring);
987 		return 0;
988 	}
989 
990 	return -ENOKEY;
991 }
992 
993 /*
994  * Change the request_key authorisation key on the current process.
995  */
keyctl_change_reqkey_auth(struct key * key)996 static int keyctl_change_reqkey_auth(struct key *key)
997 {
998 	struct cred *new;
999 
1000 	new = prepare_creds();
1001 	if (!new)
1002 		return -ENOMEM;
1003 
1004 	key_put(new->request_key_auth);
1005 	new->request_key_auth = key_get(key);
1006 
1007 	return commit_creds(new);
1008 }
1009 
1010 /*
1011  * Copy the iovec data from userspace
1012  */
copy_from_user_iovec(void * buffer,const struct iovec * iov,unsigned ioc)1013 static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
1014 				 unsigned ioc)
1015 {
1016 	for (; ioc > 0; ioc--) {
1017 		if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1018 			return -EFAULT;
1019 		buffer += iov->iov_len;
1020 		iov++;
1021 	}
1022 	return 0;
1023 }
1024 
1025 /*
1026  * Instantiate a key with the specified payload and link the key into the
1027  * destination keyring if one is given.
1028  *
1029  * The caller must have the appropriate instantiation permit set for this to
1030  * work (see keyctl_assume_authority).  No other permissions are required.
1031  *
1032  * If successful, 0 will be returned.
1033  */
keyctl_instantiate_key_common(key_serial_t id,const struct iovec * payload_iov,unsigned ioc,size_t plen,key_serial_t ringid)1034 long keyctl_instantiate_key_common(key_serial_t id,
1035 				   const struct iovec *payload_iov,
1036 				   unsigned ioc,
1037 				   size_t plen,
1038 				   key_serial_t ringid)
1039 {
1040 	const struct cred *cred = current_cred();
1041 	struct request_key_auth *rka;
1042 	struct key *instkey, *dest_keyring;
1043 	void *payload;
1044 	long ret;
1045 	bool vm = false;
1046 
1047 	kenter("%d,,%zu,%d", id, plen, ringid);
1048 
1049 	ret = -EINVAL;
1050 	if (plen > 1024 * 1024 - 1)
1051 		goto error;
1052 
1053 	/* the appropriate instantiation authorisation key must have been
1054 	 * assumed before calling this */
1055 	ret = -EPERM;
1056 	instkey = cred->request_key_auth;
1057 	if (!instkey)
1058 		goto error;
1059 
1060 	rka = instkey->payload.data;
1061 	if (rka->target_key->serial != id)
1062 		goto error;
1063 
1064 	/* pull the payload in if one was supplied */
1065 	payload = NULL;
1066 
1067 	if (payload_iov) {
1068 		ret = -ENOMEM;
1069 		payload = kmalloc(plen, GFP_KERNEL);
1070 		if (!payload) {
1071 			if (plen <= PAGE_SIZE)
1072 				goto error;
1073 			vm = true;
1074 			payload = vmalloc(plen);
1075 			if (!payload)
1076 				goto error;
1077 		}
1078 
1079 		ret = copy_from_user_iovec(payload, payload_iov, ioc);
1080 		if (ret < 0)
1081 			goto error2;
1082 	}
1083 
1084 	/* find the destination keyring amongst those belonging to the
1085 	 * requesting task */
1086 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1087 	if (ret < 0)
1088 		goto error2;
1089 
1090 	/* instantiate the key and link it into a keyring */
1091 	ret = key_instantiate_and_link(rka->target_key, payload, plen,
1092 				       dest_keyring, instkey);
1093 
1094 	key_put(dest_keyring);
1095 
1096 	/* discard the assumed authority if it's just been disabled by
1097 	 * instantiation of the key */
1098 	if (ret == 0)
1099 		keyctl_change_reqkey_auth(NULL);
1100 
1101 error2:
1102 	if (!vm)
1103 		kfree(payload);
1104 	else
1105 		vfree(payload);
1106 error:
1107 	return ret;
1108 }
1109 
1110 /*
1111  * Instantiate a key with the specified payload and link the key into the
1112  * destination keyring if one is given.
1113  *
1114  * The caller must have the appropriate instantiation permit set for this to
1115  * work (see keyctl_assume_authority).  No other permissions are required.
1116  *
1117  * If successful, 0 will be returned.
1118  */
keyctl_instantiate_key(key_serial_t id,const void __user * _payload,size_t plen,key_serial_t ringid)1119 long keyctl_instantiate_key(key_serial_t id,
1120 			    const void __user *_payload,
1121 			    size_t plen,
1122 			    key_serial_t ringid)
1123 {
1124 	if (_payload && plen) {
1125 		struct iovec iov[1] = {
1126 			[0].iov_base = (void __user *)_payload,
1127 			[0].iov_len  = plen
1128 		};
1129 
1130 		return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1131 	}
1132 
1133 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1134 }
1135 
1136 /*
1137  * Instantiate a key with the specified multipart payload and link the key into
1138  * the destination keyring if one is given.
1139  *
1140  * The caller must have the appropriate instantiation permit set for this to
1141  * work (see keyctl_assume_authority).  No other permissions are required.
1142  *
1143  * If successful, 0 will be returned.
1144  */
keyctl_instantiate_key_iov(key_serial_t id,const struct iovec __user * _payload_iov,unsigned ioc,key_serial_t ringid)1145 long keyctl_instantiate_key_iov(key_serial_t id,
1146 				const struct iovec __user *_payload_iov,
1147 				unsigned ioc,
1148 				key_serial_t ringid)
1149 {
1150 	struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1151 	long ret;
1152 
1153 	if (!_payload_iov || !ioc)
1154 		goto no_payload;
1155 
1156 	ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1157 				    ARRAY_SIZE(iovstack), iovstack, &iov);
1158 	if (ret < 0)
1159 		goto err;
1160 	if (ret == 0)
1161 		goto no_payload_free;
1162 
1163 	ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1164 err:
1165 	if (iov != iovstack)
1166 		kfree(iov);
1167 	return ret;
1168 
1169 no_payload_free:
1170 	if (iov != iovstack)
1171 		kfree(iov);
1172 no_payload:
1173 	return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1174 }
1175 
1176 /*
1177  * Negatively instantiate the key with the given timeout (in seconds) and link
1178  * the key into the destination keyring if one is given.
1179  *
1180  * The caller must have the appropriate instantiation permit set for this to
1181  * work (see keyctl_assume_authority).  No other permissions are required.
1182  *
1183  * The key and any links to the key will be automatically garbage collected
1184  * after the timeout expires.
1185  *
1186  * Negative keys are used to rate limit repeated request_key() calls by causing
1187  * them to return -ENOKEY until the negative key expires.
1188  *
1189  * If successful, 0 will be returned.
1190  */
keyctl_negate_key(key_serial_t id,unsigned timeout,key_serial_t ringid)1191 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1192 {
1193 	return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1194 }
1195 
1196 /*
1197  * Negatively instantiate the key with the given timeout (in seconds) and error
1198  * code and link the key into the destination keyring if one is given.
1199  *
1200  * The caller must have the appropriate instantiation permit set for this to
1201  * work (see keyctl_assume_authority).  No other permissions are required.
1202  *
1203  * The key and any links to the key will be automatically garbage collected
1204  * after the timeout expires.
1205  *
1206  * Negative keys are used to rate limit repeated request_key() calls by causing
1207  * them to return the specified error code until the negative key expires.
1208  *
1209  * If successful, 0 will be returned.
1210  */
keyctl_reject_key(key_serial_t id,unsigned timeout,unsigned error,key_serial_t ringid)1211 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1212 		       key_serial_t ringid)
1213 {
1214 	const struct cred *cred = current_cred();
1215 	struct request_key_auth *rka;
1216 	struct key *instkey, *dest_keyring;
1217 	long ret;
1218 
1219 	kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1220 
1221 	/* must be a valid error code and mustn't be a kernel special */
1222 	if (error <= 0 ||
1223 	    error >= MAX_ERRNO ||
1224 	    error == ERESTARTSYS ||
1225 	    error == ERESTARTNOINTR ||
1226 	    error == ERESTARTNOHAND ||
1227 	    error == ERESTART_RESTARTBLOCK)
1228 		return -EINVAL;
1229 
1230 	/* the appropriate instantiation authorisation key must have been
1231 	 * assumed before calling this */
1232 	ret = -EPERM;
1233 	instkey = cred->request_key_auth;
1234 	if (!instkey)
1235 		goto error;
1236 
1237 	rka = instkey->payload.data;
1238 	if (rka->target_key->serial != id)
1239 		goto error;
1240 
1241 	/* find the destination keyring if present (which must also be
1242 	 * writable) */
1243 	ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1244 	if (ret < 0)
1245 		goto error;
1246 
1247 	/* instantiate the key and link it into a keyring */
1248 	ret = key_reject_and_link(rka->target_key, timeout, error,
1249 				  dest_keyring, instkey);
1250 
1251 	key_put(dest_keyring);
1252 
1253 	/* discard the assumed authority if it's just been disabled by
1254 	 * instantiation of the key */
1255 	if (ret == 0)
1256 		keyctl_change_reqkey_auth(NULL);
1257 
1258 error:
1259 	return ret;
1260 }
1261 
1262 /*
1263  * Read or set the default keyring in which request_key() will cache keys and
1264  * return the old setting.
1265  *
1266  * If a thread or process keyring is specified then it will be created if it
1267  * doesn't yet exist.  The old setting will be returned if successful.
1268  */
keyctl_set_reqkey_keyring(int reqkey_defl)1269 long keyctl_set_reqkey_keyring(int reqkey_defl)
1270 {
1271 	struct cred *new;
1272 	int ret, old_setting;
1273 
1274 	old_setting = current_cred_xxx(jit_keyring);
1275 
1276 	if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1277 		return old_setting;
1278 
1279 	new = prepare_creds();
1280 	if (!new)
1281 		return -ENOMEM;
1282 
1283 	switch (reqkey_defl) {
1284 	case KEY_REQKEY_DEFL_THREAD_KEYRING:
1285 		ret = install_thread_keyring_to_cred(new);
1286 		if (ret < 0)
1287 			goto error;
1288 		goto set;
1289 
1290 	case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1291 		ret = install_process_keyring_to_cred(new);
1292 		if (ret < 0)
1293 			goto error;
1294 		goto set;
1295 
1296 	case KEY_REQKEY_DEFL_DEFAULT:
1297 	case KEY_REQKEY_DEFL_SESSION_KEYRING:
1298 	case KEY_REQKEY_DEFL_USER_KEYRING:
1299 	case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1300 	case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1301 		goto set;
1302 
1303 	case KEY_REQKEY_DEFL_NO_CHANGE:
1304 	case KEY_REQKEY_DEFL_GROUP_KEYRING:
1305 	default:
1306 		ret = -EINVAL;
1307 		goto error;
1308 	}
1309 
1310 set:
1311 	new->jit_keyring = reqkey_defl;
1312 	commit_creds(new);
1313 	return old_setting;
1314 error:
1315 	abort_creds(new);
1316 	return ret;
1317 }
1318 
1319 /*
1320  * Set or clear the timeout on a key.
1321  *
1322  * Either the key must grant the caller Setattr permission or else the caller
1323  * must hold an instantiation authorisation token for the key.
1324  *
1325  * The timeout is either 0 to clear the timeout, or a number of seconds from
1326  * the current time.  The key and any links to the key will be automatically
1327  * garbage collected after the timeout expires.
1328  *
1329  * If successful, 0 is returned.
1330  */
keyctl_set_timeout(key_serial_t id,unsigned timeout)1331 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1332 {
1333 	struct key *key, *instkey;
1334 	key_ref_t key_ref;
1335 	long ret;
1336 
1337 	key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1338 				  KEY_NEED_SETATTR);
1339 	if (IS_ERR(key_ref)) {
1340 		/* setting the timeout on a key under construction is permitted
1341 		 * if we have the authorisation token handy */
1342 		if (PTR_ERR(key_ref) == -EACCES) {
1343 			instkey = key_get_instantiation_authkey(id);
1344 			if (!IS_ERR(instkey)) {
1345 				key_put(instkey);
1346 				key_ref = lookup_user_key(id,
1347 							  KEY_LOOKUP_PARTIAL,
1348 							  0);
1349 				if (!IS_ERR(key_ref))
1350 					goto okay;
1351 			}
1352 		}
1353 
1354 		ret = PTR_ERR(key_ref);
1355 		goto error;
1356 	}
1357 
1358 okay:
1359 	key = key_ref_to_ptr(key_ref);
1360 	key_set_timeout(key, timeout);
1361 	key_put(key);
1362 
1363 	ret = 0;
1364 error:
1365 	return ret;
1366 }
1367 
1368 /*
1369  * Assume (or clear) the authority to instantiate the specified key.
1370  *
1371  * This sets the authoritative token currently in force for key instantiation.
1372  * This must be done for a key to be instantiated.  It has the effect of making
1373  * available all the keys from the caller of the request_key() that created a
1374  * key to request_key() calls made by the caller of this function.
1375  *
1376  * The caller must have the instantiation key in their process keyrings with a
1377  * Search permission grant available to the caller.
1378  *
1379  * If the ID given is 0, then the setting will be cleared and 0 returned.
1380  *
1381  * If the ID given has a matching an authorisation key, then that key will be
1382  * set and its ID will be returned.  The authorisation key can be read to get
1383  * the callout information passed to request_key().
1384  */
keyctl_assume_authority(key_serial_t id)1385 long keyctl_assume_authority(key_serial_t id)
1386 {
1387 	struct key *authkey;
1388 	long ret;
1389 
1390 	/* special key IDs aren't permitted */
1391 	ret = -EINVAL;
1392 	if (id < 0)
1393 		goto error;
1394 
1395 	/* we divest ourselves of authority if given an ID of 0 */
1396 	if (id == 0) {
1397 		ret = keyctl_change_reqkey_auth(NULL);
1398 		goto error;
1399 	}
1400 
1401 	/* attempt to assume the authority temporarily granted to us whilst we
1402 	 * instantiate the specified key
1403 	 * - the authorisation key must be in the current task's keyrings
1404 	 *   somewhere
1405 	 */
1406 	authkey = key_get_instantiation_authkey(id);
1407 	if (IS_ERR(authkey)) {
1408 		ret = PTR_ERR(authkey);
1409 		goto error;
1410 	}
1411 
1412 	ret = keyctl_change_reqkey_auth(authkey);
1413 	if (ret < 0)
1414 		goto error;
1415 	key_put(authkey);
1416 
1417 	ret = authkey->serial;
1418 error:
1419 	return ret;
1420 }
1421 
1422 /*
1423  * Get a key's the LSM security label.
1424  *
1425  * The key must grant the caller View permission for this to work.
1426  *
1427  * If there's a buffer, then up to buflen bytes of data will be placed into it.
1428  *
1429  * If successful, the amount of information available will be returned,
1430  * irrespective of how much was copied (including the terminal NUL).
1431  */
keyctl_get_security(key_serial_t keyid,char __user * buffer,size_t buflen)1432 long keyctl_get_security(key_serial_t keyid,
1433 			 char __user *buffer,
1434 			 size_t buflen)
1435 {
1436 	struct key *key, *instkey;
1437 	key_ref_t key_ref;
1438 	char *context;
1439 	long ret;
1440 
1441 	key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1442 	if (IS_ERR(key_ref)) {
1443 		if (PTR_ERR(key_ref) != -EACCES)
1444 			return PTR_ERR(key_ref);
1445 
1446 		/* viewing a key under construction is also permitted if we
1447 		 * have the authorisation token handy */
1448 		instkey = key_get_instantiation_authkey(keyid);
1449 		if (IS_ERR(instkey))
1450 			return PTR_ERR(instkey);
1451 		key_put(instkey);
1452 
1453 		key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1454 		if (IS_ERR(key_ref))
1455 			return PTR_ERR(key_ref);
1456 	}
1457 
1458 	key = key_ref_to_ptr(key_ref);
1459 	ret = security_key_getsecurity(key, &context);
1460 	if (ret == 0) {
1461 		/* if no information was returned, give userspace an empty
1462 		 * string */
1463 		ret = 1;
1464 		if (buffer && buflen > 0 &&
1465 		    copy_to_user(buffer, "", 1) != 0)
1466 			ret = -EFAULT;
1467 	} else if (ret > 0) {
1468 		/* return as much data as there's room for */
1469 		if (buffer && buflen > 0) {
1470 			if (buflen > ret)
1471 				buflen = ret;
1472 
1473 			if (copy_to_user(buffer, context, buflen) != 0)
1474 				ret = -EFAULT;
1475 		}
1476 
1477 		kfree(context);
1478 	}
1479 
1480 	key_ref_put(key_ref);
1481 	return ret;
1482 }
1483 
1484 /*
1485  * Attempt to install the calling process's session keyring on the process's
1486  * parent process.
1487  *
1488  * The keyring must exist and must grant the caller LINK permission, and the
1489  * parent process must be single-threaded and must have the same effective
1490  * ownership as this process and mustn't be SUID/SGID.
1491  *
1492  * The keyring will be emplaced on the parent when it next resumes userspace.
1493  *
1494  * If successful, 0 will be returned.
1495  */
keyctl_session_to_parent(void)1496 long keyctl_session_to_parent(void)
1497 {
1498 	struct task_struct *me, *parent;
1499 	const struct cred *mycred, *pcred;
1500 	struct callback_head *newwork, *oldwork;
1501 	key_ref_t keyring_r;
1502 	struct cred *cred;
1503 	int ret;
1504 
1505 	keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1506 	if (IS_ERR(keyring_r))
1507 		return PTR_ERR(keyring_r);
1508 
1509 	ret = -ENOMEM;
1510 
1511 	/* our parent is going to need a new cred struct, a new tgcred struct
1512 	 * and new security data, so we allocate them here to prevent ENOMEM in
1513 	 * our parent */
1514 	cred = cred_alloc_blank();
1515 	if (!cred)
1516 		goto error_keyring;
1517 	newwork = &cred->rcu;
1518 
1519 	cred->session_keyring = key_ref_to_ptr(keyring_r);
1520 	keyring_r = NULL;
1521 	init_task_work(newwork, key_change_session_keyring);
1522 
1523 	me = current;
1524 	rcu_read_lock();
1525 	write_lock_irq(&tasklist_lock);
1526 
1527 	ret = -EPERM;
1528 	oldwork = NULL;
1529 	parent = me->real_parent;
1530 
1531 	/* the parent mustn't be init and mustn't be a kernel thread */
1532 	if (parent->pid <= 1 || !parent->mm)
1533 		goto unlock;
1534 
1535 	/* the parent must be single threaded */
1536 	if (!thread_group_empty(parent))
1537 		goto unlock;
1538 
1539 	/* the parent and the child must have different session keyrings or
1540 	 * there's no point */
1541 	mycred = current_cred();
1542 	pcred = __task_cred(parent);
1543 	if (mycred == pcred ||
1544 	    mycred->session_keyring == pcred->session_keyring) {
1545 		ret = 0;
1546 		goto unlock;
1547 	}
1548 
1549 	/* the parent must have the same effective ownership and mustn't be
1550 	 * SUID/SGID */
1551 	if (!uid_eq(pcred->uid,	 mycred->euid) ||
1552 	    !uid_eq(pcred->euid, mycred->euid) ||
1553 	    !uid_eq(pcred->suid, mycred->euid) ||
1554 	    !gid_eq(pcred->gid,	 mycred->egid) ||
1555 	    !gid_eq(pcred->egid, mycred->egid) ||
1556 	    !gid_eq(pcred->sgid, mycred->egid))
1557 		goto unlock;
1558 
1559 	/* the keyrings must have the same UID */
1560 	if ((pcred->session_keyring &&
1561 	     !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1562 	    !uid_eq(mycred->session_keyring->uid, mycred->euid))
1563 		goto unlock;
1564 
1565 	/* cancel an already pending keyring replacement */
1566 	oldwork = task_work_cancel(parent, key_change_session_keyring);
1567 
1568 	/* the replacement session keyring is applied just prior to userspace
1569 	 * restarting */
1570 	ret = task_work_add(parent, newwork, true);
1571 	if (!ret)
1572 		newwork = NULL;
1573 unlock:
1574 	write_unlock_irq(&tasklist_lock);
1575 	rcu_read_unlock();
1576 	if (oldwork)
1577 		put_cred(container_of(oldwork, struct cred, rcu));
1578 	if (newwork)
1579 		put_cred(cred);
1580 	return ret;
1581 
1582 error_keyring:
1583 	key_ref_put(keyring_r);
1584 	return ret;
1585 }
1586 
1587 /*
1588  * The key control system call
1589  */
SYSCALL_DEFINE5(keyctl,int,option,unsigned long,arg2,unsigned long,arg3,unsigned long,arg4,unsigned long,arg5)1590 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1591 		unsigned long, arg4, unsigned long, arg5)
1592 {
1593 	switch (option) {
1594 	case KEYCTL_GET_KEYRING_ID:
1595 		return keyctl_get_keyring_ID((key_serial_t) arg2,
1596 					     (int) arg3);
1597 
1598 	case KEYCTL_JOIN_SESSION_KEYRING:
1599 		return keyctl_join_session_keyring((const char __user *) arg2);
1600 
1601 	case KEYCTL_UPDATE:
1602 		return keyctl_update_key((key_serial_t) arg2,
1603 					 (const void __user *) arg3,
1604 					 (size_t) arg4);
1605 
1606 	case KEYCTL_REVOKE:
1607 		return keyctl_revoke_key((key_serial_t) arg2);
1608 
1609 	case KEYCTL_DESCRIBE:
1610 		return keyctl_describe_key((key_serial_t) arg2,
1611 					   (char __user *) arg3,
1612 					   (unsigned) arg4);
1613 
1614 	case KEYCTL_CLEAR:
1615 		return keyctl_keyring_clear((key_serial_t) arg2);
1616 
1617 	case KEYCTL_LINK:
1618 		return keyctl_keyring_link((key_serial_t) arg2,
1619 					   (key_serial_t) arg3);
1620 
1621 	case KEYCTL_UNLINK:
1622 		return keyctl_keyring_unlink((key_serial_t) arg2,
1623 					     (key_serial_t) arg3);
1624 
1625 	case KEYCTL_SEARCH:
1626 		return keyctl_keyring_search((key_serial_t) arg2,
1627 					     (const char __user *) arg3,
1628 					     (const char __user *) arg4,
1629 					     (key_serial_t) arg5);
1630 
1631 	case KEYCTL_READ:
1632 		return keyctl_read_key((key_serial_t) arg2,
1633 				       (char __user *) arg3,
1634 				       (size_t) arg4);
1635 
1636 	case KEYCTL_CHOWN:
1637 		return keyctl_chown_key((key_serial_t) arg2,
1638 					(uid_t) arg3,
1639 					(gid_t) arg4);
1640 
1641 	case KEYCTL_SETPERM:
1642 		return keyctl_setperm_key((key_serial_t) arg2,
1643 					  (key_perm_t) arg3);
1644 
1645 	case KEYCTL_INSTANTIATE:
1646 		return keyctl_instantiate_key((key_serial_t) arg2,
1647 					      (const void __user *) arg3,
1648 					      (size_t) arg4,
1649 					      (key_serial_t) arg5);
1650 
1651 	case KEYCTL_NEGATE:
1652 		return keyctl_negate_key((key_serial_t) arg2,
1653 					 (unsigned) arg3,
1654 					 (key_serial_t) arg4);
1655 
1656 	case KEYCTL_SET_REQKEY_KEYRING:
1657 		return keyctl_set_reqkey_keyring(arg2);
1658 
1659 	case KEYCTL_SET_TIMEOUT:
1660 		return keyctl_set_timeout((key_serial_t) arg2,
1661 					  (unsigned) arg3);
1662 
1663 	case KEYCTL_ASSUME_AUTHORITY:
1664 		return keyctl_assume_authority((key_serial_t) arg2);
1665 
1666 	case KEYCTL_GET_SECURITY:
1667 		return keyctl_get_security((key_serial_t) arg2,
1668 					   (char __user *) arg3,
1669 					   (size_t) arg4);
1670 
1671 	case KEYCTL_SESSION_TO_PARENT:
1672 		return keyctl_session_to_parent();
1673 
1674 	case KEYCTL_REJECT:
1675 		return keyctl_reject_key((key_serial_t) arg2,
1676 					 (unsigned) arg3,
1677 					 (unsigned) arg4,
1678 					 (key_serial_t) arg5);
1679 
1680 	case KEYCTL_INSTANTIATE_IOV:
1681 		return keyctl_instantiate_key_iov(
1682 			(key_serial_t) arg2,
1683 			(const struct iovec __user *) arg3,
1684 			(unsigned) arg4,
1685 			(key_serial_t) arg5);
1686 
1687 	case KEYCTL_INVALIDATE:
1688 		return keyctl_invalidate_key((key_serial_t) arg2);
1689 
1690 	case KEYCTL_GET_PERSISTENT:
1691 		return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1692 
1693 	default:
1694 		return -EOPNOTSUPP;
1695 	}
1696 }
1697