1 /* keyctl.c: userspace keyctl operations
3 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
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/keyctl.h>
19 #include <linux/capability.h>
20 #include <linux/err.h>
21 #include <asm/uaccess.h>
24 /*****************************************************************************/
26 * extract the description of a new key from userspace and either add it as a
27 * new key to the specified keyring or update a matching key in that keyring
28 * - the keyring must be writable
29 * - returns the new key's serial number
30 * - implements add_key()
32 asmlinkage long sys_add_key(const char __user *_type,
33 const char __user *_description,
34 const void __user *_payload,
38 key_ref_t keyring_ref, key_ref;
39 char type[32], *description;
47 /* draw all the data into kernel space */
48 ret = strncpy_from_user(type, _type, sizeof(type) - 1);
58 dlen = strnlen_user(_description, PAGE_SIZE - 1);
63 if (dlen > PAGE_SIZE - 1)
67 description = kmalloc(dlen + 1, GFP_KERNEL);
72 if (copy_from_user(description, _description, dlen + 1) != 0)
75 /* pull the payload in if one was supplied */
80 payload = kmalloc(plen, GFP_KERNEL);
85 if (copy_from_user(payload, _payload, plen) != 0)
89 /* find the target keyring (which must be writable) */
90 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
91 if (IS_ERR(keyring_ref)) {
92 ret = PTR_ERR(keyring_ref);
96 /* create or update the requested key and add it to the target
98 key_ref = key_create_or_update(keyring_ref, type, description,
100 if (!IS_ERR(key_ref)) {
101 ret = key_ref_to_ptr(key_ref)->serial;
102 key_ref_put(key_ref);
105 ret = PTR_ERR(key_ref);
108 key_ref_put(keyring_ref);
116 } /* end sys_add_key() */
118 /*****************************************************************************/
120 * search the process keyrings for a matching key
121 * - nested keyrings may also be searched if they have Search permission
122 * - if a key is found, it will be attached to the destination keyring if
123 * there's one specified
124 * - /sbin/request-key will be invoked if _callout_info is non-NULL
125 * - the _callout_info string will be passed to /sbin/request-key
126 * - if the _callout_info string is empty, it will be rendered as "-"
127 * - implements request_key()
129 asmlinkage long sys_request_key(const char __user *_type,
130 const char __user *_description,
131 const char __user *_callout_info,
132 key_serial_t destringid)
134 struct key_type *ktype;
137 char type[32], *description, *callout_info;
140 /* pull the type into kernel space */
141 ret = strncpy_from_user(type, _type, sizeof(type) - 1);
150 /* pull the description into kernel space */
152 dlen = strnlen_user(_description, PAGE_SIZE - 1);
157 if (dlen > PAGE_SIZE - 1)
161 description = kmalloc(dlen + 1, GFP_KERNEL);
166 if (copy_from_user(description, _description, dlen + 1) != 0)
169 /* pull the callout info into kernel space */
173 dlen = strnlen_user(_callout_info, PAGE_SIZE - 1);
178 if (dlen > PAGE_SIZE - 1)
182 callout_info = kmalloc(dlen + 1, GFP_KERNEL);
187 if (copy_from_user(callout_info, _callout_info, dlen + 1) != 0)
191 /* get the destination keyring if specified */
194 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
195 if (IS_ERR(dest_ref)) {
196 ret = PTR_ERR(dest_ref);
201 /* find the key type */
202 ktype = key_type_lookup(type);
204 ret = PTR_ERR(ktype);
209 key = request_key_and_link(ktype, description, callout_info,
210 key_ref_to_ptr(dest_ref));
222 key_ref_put(dest_ref);
230 } /* end sys_request_key() */
232 /*****************************************************************************/
234 * get the ID of the specified process keyring
235 * - the keyring must have search permission to be found
236 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
238 long keyctl_get_keyring_ID(key_serial_t id, int create)
243 key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
244 if (IS_ERR(key_ref)) {
245 ret = PTR_ERR(key_ref);
249 ret = key_ref_to_ptr(key_ref)->serial;
250 key_ref_put(key_ref);
254 } /* end keyctl_get_keyring_ID() */
256 /*****************************************************************************/
258 * join the session keyring
259 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
261 long keyctl_join_session_keyring(const char __user *_name)
266 /* fetch the name from userspace */
270 nlen = strnlen_user(_name, PAGE_SIZE - 1);
275 if (nlen > PAGE_SIZE - 1)
279 name = kmalloc(nlen + 1, GFP_KERNEL);
284 if (copy_from_user(name, _name, nlen + 1) != 0)
288 /* join the session */
289 ret = join_session_keyring(name);
296 } /* end keyctl_join_session_keyring() */
298 /*****************************************************************************/
300 * update a key's data payload
301 * - the key must be writable
302 * - implements keyctl(KEYCTL_UPDATE)
304 long keyctl_update_key(key_serial_t id,
305 const void __user *_payload,
313 if (plen > PAGE_SIZE)
316 /* pull the payload in if one was supplied */
320 payload = kmalloc(plen, GFP_KERNEL);
325 if (copy_from_user(payload, _payload, plen) != 0)
329 /* find the target key (which must be writable) */
330 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
331 if (IS_ERR(key_ref)) {
332 ret = PTR_ERR(key_ref);
337 ret = key_update(key_ref, payload, plen);
339 key_ref_put(key_ref);
345 } /* end keyctl_update_key() */
347 /*****************************************************************************/
350 * - the key must be writable
351 * - implements keyctl(KEYCTL_REVOKE)
353 long keyctl_revoke_key(key_serial_t id)
358 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
359 if (IS_ERR(key_ref)) {
360 ret = PTR_ERR(key_ref);
364 key_revoke(key_ref_to_ptr(key_ref));
367 key_ref_put(key_ref);
371 } /* end keyctl_revoke_key() */
373 /*****************************************************************************/
375 * clear the specified process keyring
376 * - the keyring must be writable
377 * - implements keyctl(KEYCTL_CLEAR)
379 long keyctl_keyring_clear(key_serial_t ringid)
381 key_ref_t keyring_ref;
384 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
385 if (IS_ERR(keyring_ref)) {
386 ret = PTR_ERR(keyring_ref);
390 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
392 key_ref_put(keyring_ref);
396 } /* end keyctl_keyring_clear() */
398 /*****************************************************************************/
400 * link a key into a keyring
401 * - the keyring must be writable
402 * - the key must be linkable
403 * - implements keyctl(KEYCTL_LINK)
405 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
407 key_ref_t keyring_ref, key_ref;
410 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
411 if (IS_ERR(keyring_ref)) {
412 ret = PTR_ERR(keyring_ref);
416 key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
417 if (IS_ERR(key_ref)) {
418 ret = PTR_ERR(key_ref);
422 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
424 key_ref_put(key_ref);
426 key_ref_put(keyring_ref);
430 } /* end keyctl_keyring_link() */
432 /*****************************************************************************/
434 * unlink the first attachment of a key from a keyring
435 * - the keyring must be writable
436 * - we don't need any permissions on the key
437 * - implements keyctl(KEYCTL_UNLINK)
439 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
441 key_ref_t keyring_ref, key_ref;
444 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
445 if (IS_ERR(keyring_ref)) {
446 ret = PTR_ERR(keyring_ref);
450 key_ref = lookup_user_key(NULL, id, 0, 0, 0);
451 if (IS_ERR(key_ref)) {
452 ret = PTR_ERR(key_ref);
456 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
458 key_ref_put(key_ref);
460 key_ref_put(keyring_ref);
464 } /* end keyctl_keyring_unlink() */
466 /*****************************************************************************/
468 * describe a user key
469 * - the key must have view permission
470 * - if there's a buffer, we place up to buflen bytes of data into it
471 * - unless there's an error, we return the amount of description available,
472 * irrespective of how much we may have copied
473 * - the description is formatted thus:
474 * type;uid;gid;perm;description<NUL>
475 * - implements keyctl(KEYCTL_DESCRIBE)
477 long keyctl_describe_key(key_serial_t keyid,
481 struct key *key, *instkey;
486 key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
487 if (IS_ERR(key_ref)) {
488 /* viewing a key under construction is permitted if we have the
489 * authorisation token handy */
490 if (PTR_ERR(key_ref) == -EACCES) {
491 instkey = key_get_instantiation_authkey(keyid);
492 if (!IS_ERR(instkey)) {
494 key_ref = lookup_user_key(NULL, keyid,
496 if (!IS_ERR(key_ref))
501 ret = PTR_ERR(key_ref);
506 /* calculate how much description we're going to return */
508 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
512 key = key_ref_to_ptr(key_ref);
514 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
516 key_ref_to_ptr(key_ref)->type->name,
517 key_ref_to_ptr(key_ref)->uid,
518 key_ref_to_ptr(key_ref)->gid,
519 key_ref_to_ptr(key_ref)->perm,
520 key_ref_to_ptr(key_ref)->description ?
521 key_ref_to_ptr(key_ref)->description : ""
524 /* include a NUL char at the end of the data */
525 if (ret > PAGE_SIZE - 1)
530 /* consider returning the data */
531 if (buffer && buflen > 0) {
535 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
541 key_ref_put(key_ref);
545 } /* end keyctl_describe_key() */
547 /*****************************************************************************/
549 * search the specified keyring for a matching key
550 * - the start keyring must be searchable
551 * - nested keyrings may also be searched if they are searchable
552 * - only keys with search permission may be found
553 * - if a key is found, it will be attached to the destination keyring if
554 * there's one specified
555 * - implements keyctl(KEYCTL_SEARCH)
557 long keyctl_keyring_search(key_serial_t ringid,
558 const char __user *_type,
559 const char __user *_description,
560 key_serial_t destringid)
562 struct key_type *ktype;
563 key_ref_t keyring_ref, key_ref, dest_ref;
564 char type[32], *description;
567 /* pull the type and description into kernel space */
568 ret = strncpy_from_user(type, _type, sizeof(type) - 1);
574 dlen = strnlen_user(_description, PAGE_SIZE - 1);
579 if (dlen > PAGE_SIZE - 1)
583 description = kmalloc(dlen + 1, GFP_KERNEL);
588 if (copy_from_user(description, _description, dlen + 1) != 0)
591 /* get the keyring at which to begin the search */
592 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
593 if (IS_ERR(keyring_ref)) {
594 ret = PTR_ERR(keyring_ref);
598 /* get the destination keyring if specified */
601 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
602 if (IS_ERR(dest_ref)) {
603 ret = PTR_ERR(dest_ref);
608 /* find the key type */
609 ktype = key_type_lookup(type);
611 ret = PTR_ERR(ktype);
616 key_ref = keyring_search(keyring_ref, ktype, description);
617 if (IS_ERR(key_ref)) {
618 ret = PTR_ERR(key_ref);
620 /* treat lack or presence of a negative key the same */
626 /* link the resulting key to the destination keyring if we can */
628 ret = key_permission(key_ref, KEY_LINK);
632 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
637 ret = key_ref_to_ptr(key_ref)->serial;
640 key_ref_put(key_ref);
644 key_ref_put(dest_ref);
646 key_ref_put(keyring_ref);
652 } /* end keyctl_keyring_search() */
654 /*****************************************************************************/
656 * read a user key's payload
657 * - the keyring must be readable or the key must be searchable from the
659 * - if there's a buffer, we place up to buflen bytes of data into it
660 * - unless there's an error, we return the amount of data in the key,
661 * irrespective of how much we may have copied
662 * - implements keyctl(KEYCTL_READ)
664 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
670 /* find the key first */
671 key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
672 if (IS_ERR(key_ref)) {
677 key = key_ref_to_ptr(key_ref);
679 /* see if we can read it directly */
680 ret = key_permission(key_ref, KEY_READ);
686 /* we can't; see if it's searchable from this process's keyrings
687 * - we automatically take account of the fact that it may be
688 * dangling off an instantiation key
690 if (!is_key_possessed(key_ref)) {
695 /* the key is probably readable - now try to read it */
697 ret = key_validate(key);
700 if (key->type->read) {
701 /* read the data with the semaphore held (since we
703 down_read(&key->sem);
704 ret = key->type->read(key, buffer, buflen);
714 } /* end keyctl_read_key() */
716 /*****************************************************************************/
718 * change the ownership of a key
719 * - the keyring owned by the changer
720 * - if the uid or gid is -1, then that parameter is not changed
721 * - implements keyctl(KEYCTL_CHOWN)
723 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
730 if (uid == (uid_t) -1 && gid == (gid_t) -1)
733 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
734 if (IS_ERR(key_ref)) {
735 ret = PTR_ERR(key_ref);
739 key = key_ref_to_ptr(key_ref);
741 /* make the changes with the locks held to prevent chown/chown races */
743 down_write(&key->sem);
745 if (!capable(CAP_SYS_ADMIN)) {
746 /* only the sysadmin can chown a key to some other UID */
747 if (uid != (uid_t) -1 && key->uid != uid)
750 /* only the sysadmin can set the key's GID to a group other
751 * than one of those that the current process subscribes to */
752 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
756 /* change the UID (have to update the quotas) */
757 if (uid != (uid_t) -1 && uid != key->uid) {
758 /* don't support UID changing yet */
764 if (gid != (gid_t) -1)
775 } /* end keyctl_chown_key() */
777 /*****************************************************************************/
779 * change the permission mask on a key
780 * - the keyring owned by the changer
781 * - implements keyctl(KEYCTL_SETPERM)
783 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
790 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
793 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
794 if (IS_ERR(key_ref)) {
795 ret = PTR_ERR(key_ref);
799 key = key_ref_to_ptr(key_ref);
801 /* make the changes with the locks held to prevent chown/chmod races */
803 down_write(&key->sem);
805 /* if we're not the sysadmin, we can only change a key that we own */
806 if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
816 } /* end keyctl_setperm_key() */
818 /*****************************************************************************/
820 * instantiate the key with the specified payload, and, if one is given, link
821 * the key into the keyring
823 long keyctl_instantiate_key(key_serial_t id,
824 const void __user *_payload,
828 struct request_key_auth *rka;
830 key_ref_t keyring_ref;
838 /* the appropriate instantiation authorisation key must have been
839 * assumed before calling this */
841 instkey = current->request_key_auth;
845 rka = instkey->payload.data;
846 if (rka->target_key->serial != id)
849 /* pull the payload in if one was supplied */
854 payload = kmalloc(plen, GFP_KERNEL);
859 if (copy_from_user(payload, _payload, plen) != 0)
863 /* find the destination keyring amongst those belonging to the
867 keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
869 if (IS_ERR(keyring_ref)) {
870 ret = PTR_ERR(keyring_ref);
875 /* instantiate the key and link it into a keyring */
876 ret = key_instantiate_and_link(rka->target_key, payload, plen,
877 key_ref_to_ptr(keyring_ref), instkey);
879 key_ref_put(keyring_ref);
881 /* discard the assumed authority if it's just been disabled by
882 * instantiation of the key */
884 key_put(current->request_key_auth);
885 current->request_key_auth = NULL;
893 } /* end keyctl_instantiate_key() */
895 /*****************************************************************************/
897 * negatively instantiate the key with the given timeout (in seconds), and, if
898 * one is given, link the key into the keyring
900 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
902 struct request_key_auth *rka;
904 key_ref_t keyring_ref;
907 /* the appropriate instantiation authorisation key must have been
908 * assumed before calling this */
910 instkey = current->request_key_auth;
914 rka = instkey->payload.data;
915 if (rka->target_key->serial != id)
918 /* find the destination keyring if present (which must also be
922 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
923 if (IS_ERR(keyring_ref)) {
924 ret = PTR_ERR(keyring_ref);
929 /* instantiate the key and link it into a keyring */
930 ret = key_negate_and_link(rka->target_key, timeout,
931 key_ref_to_ptr(keyring_ref), instkey);
933 key_ref_put(keyring_ref);
935 /* discard the assumed authority if it's just been disabled by
936 * instantiation of the key */
938 key_put(current->request_key_auth);
939 current->request_key_auth = NULL;
945 } /* end keyctl_negate_key() */
947 /*****************************************************************************/
949 * set the default keyring in which request_key() will cache keys
950 * - return the old setting
952 long keyctl_set_reqkey_keyring(int reqkey_defl)
956 switch (reqkey_defl) {
957 case KEY_REQKEY_DEFL_THREAD_KEYRING:
958 ret = install_thread_keyring(current);
963 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
964 ret = install_process_keyring(current);
968 case KEY_REQKEY_DEFL_DEFAULT:
969 case KEY_REQKEY_DEFL_SESSION_KEYRING:
970 case KEY_REQKEY_DEFL_USER_KEYRING:
971 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
973 current->jit_keyring = reqkey_defl;
975 case KEY_REQKEY_DEFL_NO_CHANGE:
976 return current->jit_keyring;
978 case KEY_REQKEY_DEFL_GROUP_KEYRING:
983 } /* end keyctl_set_reqkey_keyring() */
985 /*****************************************************************************/
987 * set or clear the timeout for a key
989 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
997 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
998 if (IS_ERR(key_ref)) {
999 ret = PTR_ERR(key_ref);
1003 key = key_ref_to_ptr(key_ref);
1005 /* make the changes with the locks held to prevent races */
1006 down_write(&key->sem);
1010 now = current_kernel_time();
1011 expiry = now.tv_sec + timeout;
1014 key->expiry = expiry;
1016 up_write(&key->sem);
1023 } /* end keyctl_set_timeout() */
1025 /*****************************************************************************/
1027 * assume the authority to instantiate the specified key
1029 long keyctl_assume_authority(key_serial_t id)
1031 struct key *authkey;
1034 /* special key IDs aren't permitted */
1039 /* we divest ourselves of authority if given an ID of 0 */
1041 key_put(current->request_key_auth);
1042 current->request_key_auth = NULL;
1047 /* attempt to assume the authority temporarily granted to us whilst we
1048 * instantiate the specified key
1049 * - the authorisation key must be in the current task's keyrings
1052 authkey = key_get_instantiation_authkey(id);
1053 if (IS_ERR(authkey)) {
1054 ret = PTR_ERR(authkey);
1058 key_put(current->request_key_auth);
1059 current->request_key_auth = authkey;
1060 ret = authkey->serial;
1065 } /* end keyctl_assume_authority() */
1067 /*****************************************************************************/
1069 * the key control system call
1071 asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
1072 unsigned long arg4, unsigned long arg5)
1075 case KEYCTL_GET_KEYRING_ID:
1076 return keyctl_get_keyring_ID((key_serial_t) arg2,
1079 case KEYCTL_JOIN_SESSION_KEYRING:
1080 return keyctl_join_session_keyring((const char __user *) arg2);
1083 return keyctl_update_key((key_serial_t) arg2,
1084 (const void __user *) arg3,
1088 return keyctl_revoke_key((key_serial_t) arg2);
1090 case KEYCTL_DESCRIBE:
1091 return keyctl_describe_key((key_serial_t) arg2,
1092 (char __user *) arg3,
1096 return keyctl_keyring_clear((key_serial_t) arg2);
1099 return keyctl_keyring_link((key_serial_t) arg2,
1100 (key_serial_t) arg3);
1103 return keyctl_keyring_unlink((key_serial_t) arg2,
1104 (key_serial_t) arg3);
1107 return keyctl_keyring_search((key_serial_t) arg2,
1108 (const char __user *) arg3,
1109 (const char __user *) arg4,
1110 (key_serial_t) arg5);
1113 return keyctl_read_key((key_serial_t) arg2,
1114 (char __user *) arg3,
1118 return keyctl_chown_key((key_serial_t) arg2,
1122 case KEYCTL_SETPERM:
1123 return keyctl_setperm_key((key_serial_t) arg2,
1126 case KEYCTL_INSTANTIATE:
1127 return keyctl_instantiate_key((key_serial_t) arg2,
1128 (const void __user *) arg3,
1130 (key_serial_t) arg5);
1133 return keyctl_negate_key((key_serial_t) arg2,
1135 (key_serial_t) arg4);
1137 case KEYCTL_SET_REQKEY_KEYRING:
1138 return keyctl_set_reqkey_keyring(arg2);
1140 case KEYCTL_SET_TIMEOUT:
1141 return keyctl_set_timeout((key_serial_t) arg2,
1144 case KEYCTL_ASSUME_AUTHORITY:
1145 return keyctl_assume_authority((key_serial_t) arg2);
1151 } /* end sys_keyctl() */