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/string.h>
21 #include <linux/err.h>
22 #include <linux/vmalloc.h>
23 #include <asm/uaccess.h>
26 static int key_get_type_from_user(char *type,
27 const char __user *_type,
32 ret = strncpy_from_user(type, _type, len);
37 if (ret == 0 || ret >= len)
48 /*****************************************************************************/
50 * extract the description of a new key from userspace and either add it as a
51 * new key to the specified keyring or update a matching key in that keyring
52 * - the keyring must be writable
53 * - returns the new key's serial number
54 * - implements add_key()
56 asmlinkage long sys_add_key(const char __user *_type,
57 const char __user *_description,
58 const void __user *_payload,
62 key_ref_t keyring_ref, key_ref;
63 char type[32], *description;
69 if (plen > 1024 * 1024 - 1)
72 /* draw all the data into kernel space */
73 ret = key_get_type_from_user(type, _type, sizeof(type));
77 description = strndup_user(_description, PAGE_SIZE);
78 if (IS_ERR(description)) {
79 ret = PTR_ERR(description);
83 /* pull the payload in if one was supplied */
89 payload = kmalloc(plen, GFP_KERNEL);
91 if (plen <= PAGE_SIZE)
94 payload = vmalloc(plen);
100 if (copy_from_user(payload, _payload, plen) != 0)
104 /* find the target keyring (which must be writable) */
105 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
106 if (IS_ERR(keyring_ref)) {
107 ret = PTR_ERR(keyring_ref);
111 /* create or update the requested key and add it to the target
113 key_ref = key_create_or_update(keyring_ref, type, description,
114 payload, plen, KEY_ALLOC_IN_QUOTA);
115 if (!IS_ERR(key_ref)) {
116 ret = key_ref_to_ptr(key_ref)->serial;
117 key_ref_put(key_ref);
120 ret = PTR_ERR(key_ref);
123 key_ref_put(keyring_ref);
134 } /* end sys_add_key() */
136 /*****************************************************************************/
138 * search the process keyrings for a matching key
139 * - nested keyrings may also be searched if they have Search permission
140 * - if a key is found, it will be attached to the destination keyring if
141 * there's one specified
142 * - /sbin/request-key will be invoked if _callout_info is non-NULL
143 * - the _callout_info string will be passed to /sbin/request-key
144 * - if the _callout_info string is empty, it will be rendered as "-"
145 * - implements request_key()
147 asmlinkage long sys_request_key(const char __user *_type,
148 const char __user *_description,
149 const char __user *_callout_info,
150 key_serial_t destringid)
152 struct key_type *ktype;
155 char type[32], *description, *callout_info;
158 /* pull the type into kernel space */
159 ret = key_get_type_from_user(type, _type, sizeof(type));
163 /* pull the description into kernel space */
164 description = strndup_user(_description, PAGE_SIZE);
165 if (IS_ERR(description)) {
166 ret = PTR_ERR(description);
170 /* pull the callout info into kernel space */
173 callout_info = strndup_user(_callout_info, PAGE_SIZE);
174 if (IS_ERR(callout_info)) {
175 ret = PTR_ERR(callout_info);
180 /* get the destination keyring if specified */
183 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
184 if (IS_ERR(dest_ref)) {
185 ret = PTR_ERR(dest_ref);
190 /* find the key type */
191 ktype = key_type_lookup(type);
193 ret = PTR_ERR(ktype);
198 key = request_key_and_link(ktype, description, callout_info, NULL,
199 key_ref_to_ptr(dest_ref),
212 key_ref_put(dest_ref);
220 } /* end sys_request_key() */
222 /*****************************************************************************/
224 * get the ID of the specified process keyring
225 * - the keyring must have search permission to be found
226 * - implements keyctl(KEYCTL_GET_KEYRING_ID)
228 long keyctl_get_keyring_ID(key_serial_t id, int create)
233 key_ref = lookup_user_key(NULL, id, create, 0, KEY_SEARCH);
234 if (IS_ERR(key_ref)) {
235 ret = PTR_ERR(key_ref);
239 ret = key_ref_to_ptr(key_ref)->serial;
240 key_ref_put(key_ref);
244 } /* end keyctl_get_keyring_ID() */
246 /*****************************************************************************/
248 * join the session keyring
249 * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING)
251 long keyctl_join_session_keyring(const char __user *_name)
256 /* fetch the name from userspace */
259 name = strndup_user(_name, PAGE_SIZE);
266 /* join the session */
267 ret = join_session_keyring(name);
272 } /* end keyctl_join_session_keyring() */
274 /*****************************************************************************/
276 * update a key's data payload
277 * - the key must be writable
278 * - implements keyctl(KEYCTL_UPDATE)
280 long keyctl_update_key(key_serial_t id,
281 const void __user *_payload,
289 if (plen > PAGE_SIZE)
292 /* pull the payload in if one was supplied */
296 payload = kmalloc(plen, GFP_KERNEL);
301 if (copy_from_user(payload, _payload, plen) != 0)
305 /* find the target key (which must be writable) */
306 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
307 if (IS_ERR(key_ref)) {
308 ret = PTR_ERR(key_ref);
313 ret = key_update(key_ref, payload, plen);
315 key_ref_put(key_ref);
321 } /* end keyctl_update_key() */
323 /*****************************************************************************/
326 * - the key must be writable
327 * - implements keyctl(KEYCTL_REVOKE)
329 long keyctl_revoke_key(key_serial_t id)
334 key_ref = lookup_user_key(NULL, id, 0, 0, KEY_WRITE);
335 if (IS_ERR(key_ref)) {
336 ret = PTR_ERR(key_ref);
340 key_revoke(key_ref_to_ptr(key_ref));
343 key_ref_put(key_ref);
347 } /* end keyctl_revoke_key() */
349 /*****************************************************************************/
351 * clear the specified process keyring
352 * - the keyring must be writable
353 * - implements keyctl(KEYCTL_CLEAR)
355 long keyctl_keyring_clear(key_serial_t ringid)
357 key_ref_t keyring_ref;
360 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
361 if (IS_ERR(keyring_ref)) {
362 ret = PTR_ERR(keyring_ref);
366 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
368 key_ref_put(keyring_ref);
372 } /* end keyctl_keyring_clear() */
374 /*****************************************************************************/
376 * link a key into a keyring
377 * - the keyring must be writable
378 * - the key must be linkable
379 * - implements keyctl(KEYCTL_LINK)
381 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
383 key_ref_t keyring_ref, key_ref;
386 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
387 if (IS_ERR(keyring_ref)) {
388 ret = PTR_ERR(keyring_ref);
392 key_ref = lookup_user_key(NULL, id, 1, 0, KEY_LINK);
393 if (IS_ERR(key_ref)) {
394 ret = PTR_ERR(key_ref);
398 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
400 key_ref_put(key_ref);
402 key_ref_put(keyring_ref);
406 } /* end keyctl_keyring_link() */
408 /*****************************************************************************/
410 * unlink the first attachment of a key from a keyring
411 * - the keyring must be writable
412 * - we don't need any permissions on the key
413 * - implements keyctl(KEYCTL_UNLINK)
415 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
417 key_ref_t keyring_ref, key_ref;
420 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_WRITE);
421 if (IS_ERR(keyring_ref)) {
422 ret = PTR_ERR(keyring_ref);
426 key_ref = lookup_user_key(NULL, id, 0, 0, 0);
427 if (IS_ERR(key_ref)) {
428 ret = PTR_ERR(key_ref);
432 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
434 key_ref_put(key_ref);
436 key_ref_put(keyring_ref);
440 } /* end keyctl_keyring_unlink() */
442 /*****************************************************************************/
444 * describe a user key
445 * - the key must have view permission
446 * - if there's a buffer, we place up to buflen bytes of data into it
447 * - unless there's an error, we return the amount of description available,
448 * irrespective of how much we may have copied
449 * - the description is formatted thus:
450 * type;uid;gid;perm;description<NUL>
451 * - implements keyctl(KEYCTL_DESCRIBE)
453 long keyctl_describe_key(key_serial_t keyid,
457 struct key *key, *instkey;
462 key_ref = lookup_user_key(NULL, keyid, 0, 1, KEY_VIEW);
463 if (IS_ERR(key_ref)) {
464 /* viewing a key under construction is permitted if we have the
465 * authorisation token handy */
466 if (PTR_ERR(key_ref) == -EACCES) {
467 instkey = key_get_instantiation_authkey(keyid);
468 if (!IS_ERR(instkey)) {
470 key_ref = lookup_user_key(NULL, keyid,
472 if (!IS_ERR(key_ref))
477 ret = PTR_ERR(key_ref);
482 /* calculate how much description we're going to return */
484 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
488 key = key_ref_to_ptr(key_ref);
490 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
492 key_ref_to_ptr(key_ref)->type->name,
493 key_ref_to_ptr(key_ref)->uid,
494 key_ref_to_ptr(key_ref)->gid,
495 key_ref_to_ptr(key_ref)->perm,
496 key_ref_to_ptr(key_ref)->description ?
497 key_ref_to_ptr(key_ref)->description : ""
500 /* include a NUL char at the end of the data */
501 if (ret > PAGE_SIZE - 1)
506 /* consider returning the data */
507 if (buffer && buflen > 0) {
511 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
517 key_ref_put(key_ref);
521 } /* end keyctl_describe_key() */
523 /*****************************************************************************/
525 * search the specified keyring for a matching key
526 * - the start keyring must be searchable
527 * - nested keyrings may also be searched if they are searchable
528 * - only keys with search permission may be found
529 * - if a key is found, it will be attached to the destination keyring if
530 * there's one specified
531 * - implements keyctl(KEYCTL_SEARCH)
533 long keyctl_keyring_search(key_serial_t ringid,
534 const char __user *_type,
535 const char __user *_description,
536 key_serial_t destringid)
538 struct key_type *ktype;
539 key_ref_t keyring_ref, key_ref, dest_ref;
540 char type[32], *description;
543 /* pull the type and description into kernel space */
544 ret = key_get_type_from_user(type, _type, sizeof(type));
548 description = strndup_user(_description, PAGE_SIZE);
549 if (IS_ERR(description)) {
550 ret = PTR_ERR(description);
554 /* get the keyring at which to begin the search */
555 keyring_ref = lookup_user_key(NULL, ringid, 0, 0, KEY_SEARCH);
556 if (IS_ERR(keyring_ref)) {
557 ret = PTR_ERR(keyring_ref);
561 /* get the destination keyring if specified */
564 dest_ref = lookup_user_key(NULL, destringid, 1, 0, KEY_WRITE);
565 if (IS_ERR(dest_ref)) {
566 ret = PTR_ERR(dest_ref);
571 /* find the key type */
572 ktype = key_type_lookup(type);
574 ret = PTR_ERR(ktype);
579 key_ref = keyring_search(keyring_ref, ktype, description);
580 if (IS_ERR(key_ref)) {
581 ret = PTR_ERR(key_ref);
583 /* treat lack or presence of a negative key the same */
589 /* link the resulting key to the destination keyring if we can */
591 ret = key_permission(key_ref, KEY_LINK);
595 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
600 ret = key_ref_to_ptr(key_ref)->serial;
603 key_ref_put(key_ref);
607 key_ref_put(dest_ref);
609 key_ref_put(keyring_ref);
615 } /* end keyctl_keyring_search() */
617 /*****************************************************************************/
619 * read a user key's payload
620 * - the keyring must be readable or the key must be searchable from the
622 * - if there's a buffer, we place up to buflen bytes of data into it
623 * - unless there's an error, we return the amount of data in the key,
624 * irrespective of how much we may have copied
625 * - implements keyctl(KEYCTL_READ)
627 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
633 /* find the key first */
634 key_ref = lookup_user_key(NULL, keyid, 0, 0, 0);
635 if (IS_ERR(key_ref)) {
640 key = key_ref_to_ptr(key_ref);
642 /* see if we can read it directly */
643 ret = key_permission(key_ref, KEY_READ);
649 /* we can't; see if it's searchable from this process's keyrings
650 * - we automatically take account of the fact that it may be
651 * dangling off an instantiation key
653 if (!is_key_possessed(key_ref)) {
658 /* the key is probably readable - now try to read it */
660 ret = key_validate(key);
663 if (key->type->read) {
664 /* read the data with the semaphore held (since we
666 down_read(&key->sem);
667 ret = key->type->read(key, buffer, buflen);
677 } /* end keyctl_read_key() */
679 /*****************************************************************************/
681 * change the ownership of a key
682 * - the keyring owned by the changer
683 * - if the uid or gid is -1, then that parameter is not changed
684 * - implements keyctl(KEYCTL_CHOWN)
686 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
688 struct key_user *newowner, *zapowner = NULL;
694 if (uid == (uid_t) -1 && gid == (gid_t) -1)
697 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
698 if (IS_ERR(key_ref)) {
699 ret = PTR_ERR(key_ref);
703 key = key_ref_to_ptr(key_ref);
705 /* make the changes with the locks held to prevent chown/chown races */
707 down_write(&key->sem);
709 if (!capable(CAP_SYS_ADMIN)) {
710 /* only the sysadmin can chown a key to some other UID */
711 if (uid != (uid_t) -1 && key->uid != uid)
714 /* only the sysadmin can set the key's GID to a group other
715 * than one of those that the current process subscribes to */
716 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
721 if (uid != (uid_t) -1 && uid != key->uid) {
723 newowner = key_user_lookup(uid);
727 /* transfer the quota burden to the new user */
728 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
729 spin_lock(&newowner->lock);
730 if (newowner->qnkeys + 1 >= KEYQUOTA_MAX_KEYS ||
731 newowner->qnbytes + key->quotalen >=
736 newowner->qnbytes += key->quotalen;
737 spin_unlock(&newowner->lock);
739 spin_lock(&key->user->lock);
741 key->user->qnbytes -= key->quotalen;
742 spin_unlock(&key->user->lock);
745 atomic_dec(&key->user->nkeys);
746 atomic_inc(&newowner->nkeys);
748 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
749 atomic_dec(&key->user->nikeys);
750 atomic_inc(&newowner->nikeys);
753 zapowner = key->user;
754 key->user = newowner;
759 if (gid != (gid_t) -1)
768 key_user_put(zapowner);
773 spin_unlock(&newowner->lock);
778 } /* end keyctl_chown_key() */
780 /*****************************************************************************/
782 * change the permission mask on a key
783 * - the keyring owned by the changer
784 * - implements keyctl(KEYCTL_SETPERM)
786 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
793 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
796 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
797 if (IS_ERR(key_ref)) {
798 ret = PTR_ERR(key_ref);
802 key = key_ref_to_ptr(key_ref);
804 /* make the changes with the locks held to prevent chown/chmod races */
806 down_write(&key->sem);
808 /* if we're not the sysadmin, we can only change a key that we own */
809 if (capable(CAP_SYS_ADMIN) || key->uid == current->fsuid) {
819 } /* end keyctl_setperm_key() */
821 /*****************************************************************************/
823 * instantiate the key with the specified payload, and, if one is given, link
824 * the key into the keyring
826 long keyctl_instantiate_key(key_serial_t id,
827 const void __user *_payload,
831 struct request_key_auth *rka;
833 key_ref_t keyring_ref;
839 if (plen > 1024 * 1024 - 1)
842 /* the appropriate instantiation authorisation key must have been
843 * assumed before calling this */
845 instkey = current->request_key_auth;
849 rka = instkey->payload.data;
850 if (rka->target_key->serial != id)
853 /* pull the payload in if one was supplied */
858 payload = kmalloc(plen, GFP_KERNEL);
860 if (plen <= PAGE_SIZE)
863 payload = vmalloc(plen);
869 if (copy_from_user(payload, _payload, plen) != 0)
873 /* find the destination keyring amongst those belonging to the
877 keyring_ref = lookup_user_key(rka->context, ringid, 1, 0,
879 if (IS_ERR(keyring_ref)) {
880 ret = PTR_ERR(keyring_ref);
885 /* instantiate the key and link it into a keyring */
886 ret = key_instantiate_and_link(rka->target_key, payload, plen,
887 key_ref_to_ptr(keyring_ref), instkey);
889 key_ref_put(keyring_ref);
891 /* discard the assumed authority if it's just been disabled by
892 * instantiation of the key */
894 key_put(current->request_key_auth);
895 current->request_key_auth = NULL;
906 } /* end keyctl_instantiate_key() */
908 /*****************************************************************************/
910 * negatively instantiate the key with the given timeout (in seconds), and, if
911 * one is given, link the key into the keyring
913 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
915 struct request_key_auth *rka;
917 key_ref_t keyring_ref;
920 /* the appropriate instantiation authorisation key must have been
921 * assumed before calling this */
923 instkey = current->request_key_auth;
927 rka = instkey->payload.data;
928 if (rka->target_key->serial != id)
931 /* find the destination keyring if present (which must also be
935 keyring_ref = lookup_user_key(NULL, ringid, 1, 0, KEY_WRITE);
936 if (IS_ERR(keyring_ref)) {
937 ret = PTR_ERR(keyring_ref);
942 /* instantiate the key and link it into a keyring */
943 ret = key_negate_and_link(rka->target_key, timeout,
944 key_ref_to_ptr(keyring_ref), instkey);
946 key_ref_put(keyring_ref);
948 /* discard the assumed authority if it's just been disabled by
949 * instantiation of the key */
951 key_put(current->request_key_auth);
952 current->request_key_auth = NULL;
958 } /* end keyctl_negate_key() */
960 /*****************************************************************************/
962 * set the default keyring in which request_key() will cache keys
963 * - return the old setting
965 long keyctl_set_reqkey_keyring(int reqkey_defl)
969 switch (reqkey_defl) {
970 case KEY_REQKEY_DEFL_THREAD_KEYRING:
971 ret = install_thread_keyring(current);
976 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
977 ret = install_process_keyring(current);
981 case KEY_REQKEY_DEFL_DEFAULT:
982 case KEY_REQKEY_DEFL_SESSION_KEYRING:
983 case KEY_REQKEY_DEFL_USER_KEYRING:
984 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
986 current->jit_keyring = reqkey_defl;
988 case KEY_REQKEY_DEFL_NO_CHANGE:
989 return current->jit_keyring;
991 case KEY_REQKEY_DEFL_GROUP_KEYRING:
996 } /* end keyctl_set_reqkey_keyring() */
998 /*****************************************************************************/
1000 * set or clear the timeout for a key
1002 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1004 struct timespec now;
1010 key_ref = lookup_user_key(NULL, id, 1, 1, KEY_SETATTR);
1011 if (IS_ERR(key_ref)) {
1012 ret = PTR_ERR(key_ref);
1016 key = key_ref_to_ptr(key_ref);
1018 /* make the changes with the locks held to prevent races */
1019 down_write(&key->sem);
1023 now = current_kernel_time();
1024 expiry = now.tv_sec + timeout;
1027 key->expiry = expiry;
1029 up_write(&key->sem);
1036 } /* end keyctl_set_timeout() */
1038 /*****************************************************************************/
1040 * assume the authority to instantiate the specified key
1042 long keyctl_assume_authority(key_serial_t id)
1044 struct key *authkey;
1047 /* special key IDs aren't permitted */
1052 /* we divest ourselves of authority if given an ID of 0 */
1054 key_put(current->request_key_auth);
1055 current->request_key_auth = NULL;
1060 /* attempt to assume the authority temporarily granted to us whilst we
1061 * instantiate the specified key
1062 * - the authorisation key must be in the current task's keyrings
1065 authkey = key_get_instantiation_authkey(id);
1066 if (IS_ERR(authkey)) {
1067 ret = PTR_ERR(authkey);
1071 key_put(current->request_key_auth);
1072 current->request_key_auth = authkey;
1073 ret = authkey->serial;
1078 } /* end keyctl_assume_authority() */
1080 /*****************************************************************************/
1082 * the key control system call
1084 asmlinkage long sys_keyctl(int option, unsigned long arg2, unsigned long arg3,
1085 unsigned long arg4, unsigned long arg5)
1088 case KEYCTL_GET_KEYRING_ID:
1089 return keyctl_get_keyring_ID((key_serial_t) arg2,
1092 case KEYCTL_JOIN_SESSION_KEYRING:
1093 return keyctl_join_session_keyring((const char __user *) arg2);
1096 return keyctl_update_key((key_serial_t) arg2,
1097 (const void __user *) arg3,
1101 return keyctl_revoke_key((key_serial_t) arg2);
1103 case KEYCTL_DESCRIBE:
1104 return keyctl_describe_key((key_serial_t) arg2,
1105 (char __user *) arg3,
1109 return keyctl_keyring_clear((key_serial_t) arg2);
1112 return keyctl_keyring_link((key_serial_t) arg2,
1113 (key_serial_t) arg3);
1116 return keyctl_keyring_unlink((key_serial_t) arg2,
1117 (key_serial_t) arg3);
1120 return keyctl_keyring_search((key_serial_t) arg2,
1121 (const char __user *) arg3,
1122 (const char __user *) arg4,
1123 (key_serial_t) arg5);
1126 return keyctl_read_key((key_serial_t) arg2,
1127 (char __user *) arg3,
1131 return keyctl_chown_key((key_serial_t) arg2,
1135 case KEYCTL_SETPERM:
1136 return keyctl_setperm_key((key_serial_t) arg2,
1139 case KEYCTL_INSTANTIATE:
1140 return keyctl_instantiate_key((key_serial_t) arg2,
1141 (const void __user *) arg3,
1143 (key_serial_t) arg5);
1146 return keyctl_negate_key((key_serial_t) arg2,
1148 (key_serial_t) arg4);
1150 case KEYCTL_SET_REQKEY_KEYRING:
1151 return keyctl_set_reqkey_keyring(arg2);
1153 case KEYCTL_SET_TIMEOUT:
1154 return keyctl_set_timeout((key_serial_t) arg2,
1157 case KEYCTL_ASSUME_AUTHORITY:
1158 return keyctl_assume_authority((key_serial_t) arg2);
1164 } /* end sys_keyctl() */