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);
70 description[dlen] = '\0';
73 if (copy_from_user(description, _description, dlen) != 0)
76 /* pull the payload in if one was supplied */
81 payload = kmalloc(plen, GFP_KERNEL);
86 if (copy_from_user(payload, _payload, plen) != 0)
90 /* find the target keyring (which must be writable) */
91 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
92 if (IS_ERR(keyring_ref)) {
93 ret = PTR_ERR(keyring_ref);
97 /* create or update the requested key and add it to the target
99 key_ref = key_create_or_update(keyring_ref, type, description,
101 if (!IS_ERR(key_ref)) {
102 ret = key_ref_to_ptr(key_ref)->serial;
103 key_ref_put(key_ref);
106 ret = PTR_ERR(key_ref);
109 key_ref_put(keyring_ref);
117 } /* end sys_add_key() */
119 /*****************************************************************************/
121 * search the process keyrings for a matching key
122 * - nested keyrings may also be searched if they have Search permission
123 * - if a key is found, it will be attached to the destination keyring if
124 * there's one specified
125 * - /sbin/request-key will be invoked if _callout_info is non-NULL
126 * - the _callout_info string will be passed to /sbin/request-key
127 * - if the _callout_info string is empty, it will be rendered as "-"
128 * - implements request_key()
130 asmlinkage long sys_request_key(const char __user *_type,
131 const char __user *_description,
132 const char __user *_callout_info,
133 key_serial_t destringid)
135 struct key_type *ktype;
138 char type[32], *description, *callout_info;
141 /* pull the type into kernel space */
142 ret = strncpy_from_user(type, _type, sizeof(type) - 1);
151 /* pull the description into kernel space */
153 dlen = strnlen_user(_description, PAGE_SIZE - 1);
158 if (dlen > PAGE_SIZE - 1)
162 description = kmalloc(dlen + 1, GFP_KERNEL);
165 description[dlen] = '\0';
168 if (copy_from_user(description, _description, dlen) != 0)
171 /* pull the callout info into kernel space */
175 dlen = strnlen_user(_callout_info, PAGE_SIZE - 1);
180 if (dlen > PAGE_SIZE - 1)
184 callout_info = kmalloc(dlen + 1, GFP_KERNEL);
187 callout_info[dlen] = '\0';
190 if (copy_from_user(callout_info, _callout_info, dlen) != 0)
194 /* get the destination keyring if specified */
197 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
198 if (IS_ERR(dest_ref)) {
199 ret = PTR_ERR(dest_ref);
204 /* find the key type */
205 ktype = key_type_lookup(type);
207 ret = PTR_ERR(ktype);
212 key = request_key_and_link(ktype, description, callout_info,
213 key_ref_to_ptr(dest_ref));
225 key_ref_put(dest_ref);
233 } /* end sys_request_key() */
235 /*****************************************************************************/
237 * get the ID of the specified process keyring
238 * - the keyring must have search permission to be found
239 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
241 long keyctl_get_keyring_ID(key_serial_t id, int create)
246 key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
247 if (IS_ERR(key_ref)) {
248 ret = PTR_ERR(key_ref);
252 ret = key_ref_to_ptr(key_ref)->serial;
253 key_ref_put(key_ref);
257 } /* end keyctl_get_keyring_ID() */
259 /*****************************************************************************/
261 * join the session keyring
262 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
264 long keyctl_join_session_keyring(const char __user *_name)
269 /* fetch the name from userspace */
273 nlen = strnlen_user(_name, PAGE_SIZE - 1);
278 if (nlen > PAGE_SIZE - 1)
282 name = kmalloc(nlen + 1, GFP_KERNEL);
288 if (copy_from_user(name, _name, nlen) != 0)
292 /* join the session */
293 ret = join_session_keyring(name);
300 } /* end keyctl_join_session_keyring() */
302 /*****************************************************************************/
304 * update a key's data payload
305 * - the key must be writable
306 * - implements keyctl(KEYCTL_UPDATE)
308 long keyctl_update_key(key_serial_t id,
309 const void __user *_payload,
317 if (plen > PAGE_SIZE)
320 /* pull the payload in if one was supplied */
324 payload = kmalloc(plen, GFP_KERNEL);
329 if (copy_from_user(payload, _payload, plen) != 0)
333 /* find the target key (which must be writable) */
334 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
335 if (IS_ERR(key_ref)) {
336 ret = PTR_ERR(key_ref);
341 ret = key_update(key_ref, payload, plen);
343 key_ref_put(key_ref);
349 } /* end keyctl_update_key() */
351 /*****************************************************************************/
354 * - the key must be writable
355 * - implements keyctl(KEYCTL_REVOKE)
357 long keyctl_revoke_key(key_serial_t id)
362 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
363 if (IS_ERR(key_ref)) {
364 ret = PTR_ERR(key_ref);
368 key_revoke(key_ref_to_ptr(key_ref));
371 key_ref_put(key_ref);
375 } /* end keyctl_revoke_key() */
377 /*****************************************************************************/
379 * clear the specified process keyring
380 * - the keyring must be writable
381 * - implements keyctl(KEYCTL_CLEAR)
383 long keyctl_keyring_clear(key_serial_t ringid)
385 key_ref_t keyring_ref;
388 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
389 if (IS_ERR(keyring_ref)) {
390 ret = PTR_ERR(keyring_ref);
394 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
396 key_ref_put(keyring_ref);
400 } /* end keyctl_keyring_clear() */
402 /*****************************************************************************/
404 * link a key into a keyring
405 * - the keyring must be writable
406 * - the key must be linkable
407 * - implements keyctl(KEYCTL_LINK)
409 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
411 key_ref_t keyring_ref, key_ref;
414 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
415 if (IS_ERR(keyring_ref)) {
416 ret = PTR_ERR(keyring_ref);
420 key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
421 if (IS_ERR(key_ref)) {
422 ret = PTR_ERR(key_ref);
426 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
428 key_ref_put(key_ref);
430 key_ref_put(keyring_ref);
434 } /* end keyctl_keyring_link() */
436 /*****************************************************************************/
438 * unlink the first attachment of a key from a keyring
439 * - the keyring must be writable
440 * - we don't need any permissions on the key
441 * - implements keyctl(KEYCTL_UNLINK)
443 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
445 key_ref_t keyring_ref, key_ref;
448 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
449 if (IS_ERR(keyring_ref)) {
450 ret = PTR_ERR(keyring_ref);
454 key_ref = lookup_user_key(NULL, id, 0, 0, 0);
455 if (IS_ERR(key_ref)) {
456 ret = PTR_ERR(key_ref);
460 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
462 key_ref_put(key_ref);
464 key_ref_put(keyring_ref);
468 } /* end keyctl_keyring_unlink() */
470 /*****************************************************************************/
472 * describe a user key
473 * - the key must have view permission
474 * - if there's a buffer, we place up to buflen bytes of data into it
475 * - unless there's an error, we return the amount of description available,
476 * irrespective of how much we may have copied
477 * - the description is formatted thus:
478 * type;uid;gid;perm;description<NUL>
479 * - implements keyctl(KEYCTL_DESCRIBE)
481 long keyctl_describe_key(key_serial_t keyid,
485 struct key *key, *instkey;
490 key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
491 if (IS_ERR(key_ref)) {
492 /* viewing a key under construction is permitted if we have the
493 * authorisation token handy */
494 if (PTR_ERR(key_ref) == -EACCES) {
495 instkey = key_get_instantiation_authkey(keyid);
496 if (!IS_ERR(instkey)) {
498 key_ref = lookup_user_key(NULL, keyid,
500 if (!IS_ERR(key_ref))
505 ret = PTR_ERR(key_ref);
510 /* calculate how much description we're going to return */
512 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
516 key = key_ref_to_ptr(key_ref);
518 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
520 key_ref_to_ptr(key_ref)->type->name,
521 key_ref_to_ptr(key_ref)->uid,
522 key_ref_to_ptr(key_ref)->gid,
523 key_ref_to_ptr(key_ref)->perm,
524 key_ref_to_ptr(key_ref)->description ?
525 key_ref_to_ptr(key_ref)->description : ""
528 /* include a NUL char at the end of the data */
529 if (ret > PAGE_SIZE - 1)
534 /* consider returning the data */
535 if (buffer && buflen > 0) {
539 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
545 key_ref_put(key_ref);
549 } /* end keyctl_describe_key() */
551 /*****************************************************************************/
553 * search the specified keyring for a matching key
554 * - the start keyring must be searchable
555 * - nested keyrings may also be searched if they are searchable
556 * - only keys with search permission may be found
557 * - if a key is found, it will be attached to the destination keyring if
558 * there's one specified
559 * - implements keyctl(KEYCTL_SEARCH)
561 long keyctl_keyring_search(key_serial_t ringid,
562 const char __user *_type,
563 const char __user *_description,
564 key_serial_t destringid)
566 struct key_type *ktype;
567 key_ref_t keyring_ref, key_ref, dest_ref;
568 char type[32], *description;
571 /* pull the type and description into kernel space */
572 ret = strncpy_from_user(type, _type, sizeof(type) - 1);
578 dlen = strnlen_user(_description, PAGE_SIZE - 1);
583 if (dlen > PAGE_SIZE - 1)
587 description = kmalloc(dlen + 1, GFP_KERNEL);
590 description[dlen] = '\0';
593 if (copy_from_user(description, _description, dlen) != 0)
596 /* get the keyring at which to begin the search */
597 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
598 if (IS_ERR(keyring_ref)) {
599 ret = PTR_ERR(keyring_ref);
603 /* get the destination keyring if specified */
606 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
607 if (IS_ERR(dest_ref)) {
608 ret = PTR_ERR(dest_ref);
613 /* find the key type */
614 ktype = key_type_lookup(type);
616 ret = PTR_ERR(ktype);
621 key_ref = keyring_search(keyring_ref, ktype, description);
622 if (IS_ERR(key_ref)) {
623 ret = PTR_ERR(key_ref);
625 /* treat lack or presence of a negative key the same */
631 /* link the resulting key to the destination keyring if we can */
633 ret = key_permission(key_ref, KEY_LINK);
637 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
642 ret = key_ref_to_ptr(key_ref)->serial;
645 key_ref_put(key_ref);
649 key_ref_put(dest_ref);
651 key_ref_put(keyring_ref);
657 } /* end keyctl_keyring_search() */
659 /*****************************************************************************/
661 * read a user key's payload
662 * - the keyring must be readable or the key must be searchable from the
664 * - if there's a buffer, we place up to buflen bytes of data into it
665 * - unless there's an error, we return the amount of data in the key,
666 * irrespective of how much we may have copied
667 * - implements keyctl(KEYCTL_READ)
669 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
675 /* find the key first */
676 key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
677 if (IS_ERR(key_ref)) {
682 key = key_ref_to_ptr(key_ref);
684 /* see if we can read it directly */
685 ret = key_permission(key_ref, KEY_READ);
691 /* we can't; see if it's searchable from this process's keyrings
692 * - we automatically take account of the fact that it may be
693 * dangling off an instantiation key
695 if (!is_key_possessed(key_ref)) {
700 /* the key is probably readable - now try to read it */
702 ret = key_validate(key);
705 if (key->type->read) {
706 /* read the data with the semaphore held (since we
708 down_read(&key->sem);
709 ret = key->type->read(key, buffer, buflen);
719 } /* end keyctl_read_key() */
721 /*****************************************************************************/
723 * change the ownership of a key
724 * - the keyring owned by the changer
725 * - if the uid or gid is -1, then that parameter is not changed
726 * - implements keyctl(KEYCTL_CHOWN)
728 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
735 if (uid == (uid_t) -1 && gid == (gid_t) -1)
738 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
739 if (IS_ERR(key_ref)) {
740 ret = PTR_ERR(key_ref);
744 key = key_ref_to_ptr(key_ref);
746 /* make the changes with the locks held to prevent chown/chown races */
748 down_write(&key->sem);
750 if (!capable(CAP_SYS_ADMIN)) {
751 /* only the sysadmin can chown a key to some other UID */
752 if (uid != (uid_t) -1 && key->uid != uid)
755 /* only the sysadmin can set the key's GID to a group other
756 * than one of those that the current process subscribes to */
757 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
761 /* change the UID (have to update the quotas) */
762 if (uid != (uid_t) -1 && uid != key->uid) {
763 /* don't support UID changing yet */
769 if (gid != (gid_t) -1)
780 } /* end keyctl_chown_key() */
782 /*****************************************************************************/
784 * change the permission mask on a key
785 * - the keyring owned by the changer
786 * - implements keyctl(KEYCTL_SETPERM)
788 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
795 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
798 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
799 if (IS_ERR(key_ref)) {
800 ret = PTR_ERR(key_ref);
804 key = key_ref_to_ptr(key_ref);
806 /* make the changes with the locks held to prevent chown/chmod races */
808 down_write(&key->sem);
810 /* if we're not the sysadmin, we can only change a key that we own */
811 if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
821 } /* end keyctl_setperm_key() */
823 /*****************************************************************************/
825 * instantiate the key with the specified payload, and, if one is given, link
826 * the key into the keyring
828 long keyctl_instantiate_key(key_serial_t id,
829 const void __user *_payload,
833 struct request_key_auth *rka;
835 key_ref_t keyring_ref;
843 /* the appropriate instantiation authorisation key must have been
844 * assumed before calling this */
846 instkey = current->request_key_auth;
850 rka = instkey->payload.data;
851 if (rka->target_key->serial != id)
854 /* pull the payload in if one was supplied */
859 payload = kmalloc(plen, GFP_KERNEL);
864 if (copy_from_user(payload, _payload, plen) != 0)
868 /* find the destination keyring amongst those belonging to the
872 keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
874 if (IS_ERR(keyring_ref)) {
875 ret = PTR_ERR(keyring_ref);
880 /* instantiate the key and link it into a keyring */
881 ret = key_instantiate_and_link(rka->target_key, payload, plen,
882 key_ref_to_ptr(keyring_ref), instkey);
884 key_ref_put(keyring_ref);
886 /* discard the assumed authority if it's just been disabled by
887 * instantiation of the key */
889 key_put(current->request_key_auth);
890 current->request_key_auth = NULL;
898 } /* end keyctl_instantiate_key() */
900 /*****************************************************************************/
902 * negatively instantiate the key with the given timeout (in seconds), and, if
903 * one is given, link the key into the keyring
905 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
907 struct request_key_auth *rka;
909 key_ref_t keyring_ref;
912 /* the appropriate instantiation authorisation key must have been
913 * assumed before calling this */
915 instkey = current->request_key_auth;
919 rka = instkey->payload.data;
920 if (rka->target_key->serial != id)
923 /* find the destination keyring if present (which must also be
927 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
928 if (IS_ERR(keyring_ref)) {
929 ret = PTR_ERR(keyring_ref);
934 /* instantiate the key and link it into a keyring */
935 ret = key_negate_and_link(rka->target_key, timeout,
936 key_ref_to_ptr(keyring_ref), instkey);
938 key_ref_put(keyring_ref);
940 /* discard the assumed authority if it's just been disabled by
941 * instantiation of the key */
943 key_put(current->request_key_auth);
944 current->request_key_auth = NULL;
950 } /* end keyctl_negate_key() */
952 /*****************************************************************************/
954 * set the default keyring in which request_key() will cache keys
955 * - return the old setting
957 long keyctl_set_reqkey_keyring(int reqkey_defl)
961 switch (reqkey_defl) {
962 case KEY_REQKEY_DEFL_THREAD_KEYRING:
963 ret = install_thread_keyring(current);
968 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
969 ret = install_process_keyring(current);
973 case KEY_REQKEY_DEFL_DEFAULT:
974 case KEY_REQKEY_DEFL_SESSION_KEYRING:
975 case KEY_REQKEY_DEFL_USER_KEYRING:
976 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
978 current->jit_keyring = reqkey_defl;
980 case KEY_REQKEY_DEFL_NO_CHANGE:
981 return current->jit_keyring;
983 case KEY_REQKEY_DEFL_GROUP_KEYRING:
988 } /* end keyctl_set_reqkey_keyring() */
990 /*****************************************************************************/
992 * set or clear the timeout for a key
994 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1002 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
1003 if (IS_ERR(key_ref)) {
1004 ret = PTR_ERR(key_ref);
1008 key = key_ref_to_ptr(key_ref);
1010 /* make the changes with the locks held to prevent races */
1011 down_write(&key->sem);
1015 now = current_kernel_time();
1016 expiry = now.tv_sec + timeout;
1019 key->expiry = expiry;
1021 up_write(&key->sem);
1028 } /* end keyctl_set_timeout() */
1030 /*****************************************************************************/
1032 * assume the authority to instantiate the specified key
1034 long keyctl_assume_authority(key_serial_t id)
1036 struct key *authkey;
1039 /* special key IDs aren't permitted */
1044 /* we divest ourselves of authority if given an ID of 0 */
1046 key_put(current->request_key_auth);
1047 current->request_key_auth = NULL;
1052 /* attempt to assume the authority temporarily granted to us whilst we
1053 * instantiate the specified key
1054 * - the authorisation key must be in the current task's keyrings
1057 authkey = key_get_instantiation_authkey(id);
1058 if (IS_ERR(authkey)) {
1059 ret = PTR_ERR(authkey);
1063 key_put(current->request_key_auth);
1064 current->request_key_auth = authkey;
1065 ret = authkey->serial;
1070 } /* end keyctl_assume_authority() */
1072 /*****************************************************************************/
1074 * the key control system call
1076 asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
1077 unsigned long arg4, unsigned long arg5)
1080 case KEYCTL_GET_KEYRING_ID:
1081 return keyctl_get_keyring_ID((key_serial_t) arg2,
1084 case KEYCTL_JOIN_SESSION_KEYRING:
1085 return keyctl_join_session_keyring((const char __user *) arg2);
1088 return keyctl_update_key((key_serial_t) arg2,
1089 (const void __user *) arg3,
1093 return keyctl_revoke_key((key_serial_t) arg2);
1095 case KEYCTL_DESCRIBE:
1096 return keyctl_describe_key((key_serial_t) arg2,
1097 (char __user *) arg3,
1101 return keyctl_keyring_clear((key_serial_t) arg2);
1104 return keyctl_keyring_link((key_serial_t) arg2,
1105 (key_serial_t) arg3);
1108 return keyctl_keyring_unlink((key_serial_t) arg2,
1109 (key_serial_t) arg3);
1112 return keyctl_keyring_search((key_serial_t) arg2,
1113 (const char __user *) arg3,
1114 (const char __user *) arg4,
1115 (key_serial_t) arg5);
1118 return keyctl_read_key((key_serial_t) arg2,
1119 (char __user *) arg3,
1123 return keyctl_chown_key((key_serial_t) arg2,
1127 case KEYCTL_SETPERM:
1128 return keyctl_setperm_key((key_serial_t) arg2,
1131 case KEYCTL_INSTANTIATE:
1132 return keyctl_instantiate_key((key_serial_t) arg2,
1133 (const void __user *) arg3,
1135 (key_serial_t) arg5);
1138 return keyctl_negate_key((key_serial_t) arg2,
1140 (key_serial_t) arg4);
1142 case KEYCTL_SET_REQKEY_KEYRING:
1143 return keyctl_set_reqkey_keyring(arg2);
1145 case KEYCTL_SET_TIMEOUT:
1146 return keyctl_set_timeout((key_serial_t) arg2,
1149 case KEYCTL_ASSUME_AUTHORITY:
1150 return keyctl_assume_authority((key_serial_t) arg2);
1156 } /* end sys_keyctl() */