1 /* Userspace key control 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/init.h>
13 #include <linux/sched.h>
14 #include <linux/sched/task.h>
15 #include <linux/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/key.h>
18 #include <linux/keyctl.h>
20 #include <linux/capability.h>
21 #include <linux/cred.h>
22 #include <linux/string.h>
23 #include <linux/err.h>
24 #include <linux/vmalloc.h>
25 #include <linux/security.h>
26 #include <linux/uio.h>
27 #include <linux/uaccess.h>
28 #include <keys/request_key_auth-type.h>
31 #define KEY_MAX_DESC_SIZE 4096
33 static const unsigned char keyrings_capabilities[2] = {
34 [0] = (KEYCTL_CAPS0_CAPABILITIES |
35 (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
36 (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
37 (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
38 (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
39 KEYCTL_CAPS0_INVALIDATE |
40 KEYCTL_CAPS0_RESTRICT_KEYRING |
43 [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
44 KEYCTL_CAPS1_NS_KEY_TAG |
45 KEYCTL_CAPS1_ACL_ALTERABLE),
48 static int key_get_type_from_user(char *type,
49 const char __user *_type,
54 ret = strncpy_from_user(type, _type, len);
57 if (ret == 0 || ret >= len)
66 * Extract the description of a new key from userspace and either add it as a
67 * new key to the specified keyring or update a matching key in that keyring.
69 * If the description is NULL or an empty string, the key type is asked to
70 * generate one from the payload.
72 * The keyring must be writable so that we can attach the key to it.
74 * If successful, the new key's serial number is returned, otherwise an error
77 SYSCALL_DEFINE5(add_key, const char __user *, _type,
78 const char __user *, _description,
79 const void __user *, _payload,
83 key_ref_t keyring_ref, key_ref;
84 char type[32], *description;
89 if (plen > 1024 * 1024 - 1)
92 /* draw all the data into kernel space */
93 ret = key_get_type_from_user(type, _type, sizeof(type));
99 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
100 if (IS_ERR(description)) {
101 ret = PTR_ERR(description);
107 } else if ((description[0] == '.') &&
108 (strncmp(type, "keyring", 7) == 0)) {
114 /* pull the payload in if one was supplied */
119 payload = kvmalloc(plen, GFP_KERNEL);
124 if (copy_from_user(payload, _payload, plen) != 0)
128 /* find the target keyring (which must be writable) */
129 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
130 if (IS_ERR(keyring_ref)) {
131 ret = PTR_ERR(keyring_ref);
135 /* create or update the requested key and add it to the target
137 key_ref = key_create_or_update(keyring_ref, type, description,
138 payload, plen, NULL, KEY_ALLOC_IN_QUOTA);
139 if (!IS_ERR(key_ref)) {
140 ret = key_ref_to_ptr(key_ref)->serial;
141 key_ref_put(key_ref);
144 ret = PTR_ERR(key_ref);
147 key_ref_put(keyring_ref);
150 memzero_explicit(payload, plen);
160 * Search the process keyrings and keyring trees linked from those for a
161 * matching key. Keyrings must have appropriate Search permission to be
164 * If a key is found, it will be attached to the destination keyring if there's
165 * one specified and the serial number of the key will be returned.
167 * If no key is found, /sbin/request-key will be invoked if _callout_info is
168 * non-NULL in an attempt to create a key. The _callout_info string will be
169 * passed to /sbin/request-key to aid with completing the request. If the
170 * _callout_info string is "" then it will be changed to "-".
172 SYSCALL_DEFINE4(request_key, const char __user *, _type,
173 const char __user *, _description,
174 const char __user *, _callout_info,
175 key_serial_t, destringid)
177 struct key_type *ktype;
181 char type[32], *description, *callout_info;
184 /* pull the type into kernel space */
185 ret = key_get_type_from_user(type, _type, sizeof(type));
189 /* pull the description into kernel space */
190 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
191 if (IS_ERR(description)) {
192 ret = PTR_ERR(description);
196 /* pull the callout info into kernel space */
200 callout_info = strndup_user(_callout_info, PAGE_SIZE);
201 if (IS_ERR(callout_info)) {
202 ret = PTR_ERR(callout_info);
205 callout_len = strlen(callout_info);
208 /* get the destination keyring if specified */
211 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
213 if (IS_ERR(dest_ref)) {
214 ret = PTR_ERR(dest_ref);
219 /* find the key type */
220 ktype = key_type_lookup(type);
222 ret = PTR_ERR(ktype);
227 key = request_key_and_link(ktype, description, NULL, callout_info,
228 callout_len, NULL, NULL,
229 key_ref_to_ptr(dest_ref),
236 /* wait for the key to finish being constructed */
237 ret = wait_for_key_construction(key, 1);
248 key_ref_put(dest_ref);
258 * Get the ID of the specified process keyring.
260 * The requested keyring must have search permission to be found.
262 * If successful, the ID of the requested keyring will be returned.
264 long keyctl_get_keyring_ID(key_serial_t id, int create)
267 unsigned long lflags;
270 lflags = create ? KEY_LOOKUP_CREATE : 0;
271 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
272 if (IS_ERR(key_ref)) {
273 ret = PTR_ERR(key_ref);
277 ret = key_ref_to_ptr(key_ref)->serial;
278 key_ref_put(key_ref);
284 * Join a (named) session keyring.
286 * Create and join an anonymous session keyring or join a named session
287 * keyring, creating it if necessary. A named session keyring must have Search
288 * permission for it to be joined. Session keyrings without this permit will
289 * be skipped over. It is not permitted for userspace to create or join
290 * keyrings whose name begin with a dot.
292 * If successful, the ID of the joined session keyring will be returned.
294 long keyctl_join_session_keyring(const char __user *_name)
299 /* fetch the name from userspace */
302 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
313 /* join the session */
314 ret = join_session_keyring(name);
322 * Update a key's data payload from the given data.
324 * The key must grant the caller Write permission and the key type must support
325 * updating for this to work. A negative key can be positively instantiated
328 * If successful, 0 will be returned. If the key type does not support
329 * updating, then -EOPNOTSUPP will be returned.
331 long keyctl_update_key(key_serial_t id,
332 const void __user *_payload,
340 if (plen > PAGE_SIZE)
343 /* pull the payload in if one was supplied */
347 payload = kmalloc(plen, GFP_KERNEL);
352 if (copy_from_user(payload, _payload, plen) != 0)
356 /* find the target key (which must be writable) */
357 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
358 if (IS_ERR(key_ref)) {
359 ret = PTR_ERR(key_ref);
364 ret = key_update(key_ref, payload, plen);
366 key_ref_put(key_ref);
376 * The key must be grant the caller Write or Setattr permission for this to
377 * work. The key type should give up its quota claim when revoked. The key
378 * and any links to the key will be automatically garbage collected after a
379 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
381 * Keys with KEY_FLAG_KEEP set should not be revoked.
383 * If successful, 0 is returned.
385 long keyctl_revoke_key(key_serial_t id)
391 key_ref = lookup_user_key(id, 0, KEY_NEED_REVOKE);
392 if (IS_ERR(key_ref)) {
393 ret = PTR_ERR(key_ref);
397 key = key_ref_to_ptr(key_ref);
399 if (test_bit(KEY_FLAG_KEEP, &key->flags))
404 key_ref_put(key_ref);
412 * The key must be grant the caller Invalidate permission for this to work.
413 * The key and any links to the key will be automatically garbage collected
416 * Keys with KEY_FLAG_KEEP set should not be invalidated.
418 * If successful, 0 is returned.
420 long keyctl_invalidate_key(key_serial_t id)
428 key_ref = lookup_user_key(id, 0, KEY_NEED_INVAL);
429 if (IS_ERR(key_ref)) {
430 ret = PTR_ERR(key_ref);
432 /* Root is permitted to invalidate certain special keys */
433 if (capable(CAP_SYS_ADMIN)) {
434 key_ref = lookup_user_key(id, 0, 0);
437 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
438 &key_ref_to_ptr(key_ref)->flags))
447 key = key_ref_to_ptr(key_ref);
449 if (test_bit(KEY_FLAG_KEEP, &key->flags))
454 key_ref_put(key_ref);
456 kleave(" = %ld", ret);
461 * Clear the specified keyring, creating an empty process keyring if one of the
462 * special keyring IDs is used.
464 * The keyring must grant the caller Write permission and not have
465 * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
467 long keyctl_keyring_clear(key_serial_t ringid)
469 key_ref_t keyring_ref;
473 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_CLEAR);
474 if (IS_ERR(keyring_ref)) {
475 ret = PTR_ERR(keyring_ref);
477 /* Root is permitted to invalidate certain special keyrings */
478 if (capable(CAP_SYS_ADMIN)) {
479 keyring_ref = lookup_user_key(ringid, 0, 0);
480 if (IS_ERR(keyring_ref))
482 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
483 &key_ref_to_ptr(keyring_ref)->flags))
492 keyring = key_ref_to_ptr(keyring_ref);
493 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
496 ret = keyring_clear(keyring);
498 key_ref_put(keyring_ref);
504 * Create a link from a keyring to a key if there's no matching key in the
505 * keyring, otherwise replace the link to the matching key with a link to the
508 * The key must grant the caller Link permission and the the keyring must grant
509 * the caller Write permission. Furthermore, if an additional link is created,
510 * the keyring's quota will be extended.
512 * If successful, 0 will be returned.
514 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
516 key_ref_t keyring_ref, key_ref;
519 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
520 if (IS_ERR(keyring_ref)) {
521 ret = PTR_ERR(keyring_ref);
525 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
526 if (IS_ERR(key_ref)) {
527 ret = PTR_ERR(key_ref);
531 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
533 key_ref_put(key_ref);
535 key_ref_put(keyring_ref);
541 * Unlink a key from a keyring.
543 * The keyring must grant the caller Write permission for this to work; the key
544 * itself need not grant the caller anything. If the last link to a key is
545 * removed then that key will be scheduled for destruction.
547 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
549 * If successful, 0 will be returned.
551 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
553 key_ref_t keyring_ref, key_ref;
554 struct key *keyring, *key;
557 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
558 if (IS_ERR(keyring_ref)) {
559 ret = PTR_ERR(keyring_ref);
563 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
564 if (IS_ERR(key_ref)) {
565 ret = PTR_ERR(key_ref);
569 keyring = key_ref_to_ptr(keyring_ref);
570 key = key_ref_to_ptr(key_ref);
571 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
572 test_bit(KEY_FLAG_KEEP, &key->flags))
575 ret = key_unlink(keyring, key);
577 key_ref_put(key_ref);
579 key_ref_put(keyring_ref);
585 * Move a link to a key from one keyring to another, displacing any matching
586 * key from the destination keyring.
588 * The key must grant the caller Link permission and both keyrings must grant
589 * the caller Write permission. There must also be a link in the from keyring
590 * to the key. If both keyrings are the same, nothing is done.
592 * If successful, 0 will be returned.
594 long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
595 key_serial_t to_ringid, unsigned int flags)
597 key_ref_t key_ref, from_ref, to_ref;
600 if (flags & ~KEYCTL_MOVE_EXCL)
603 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
605 return PTR_ERR(key_ref);
607 from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
608 if (IS_ERR(from_ref)) {
609 ret = PTR_ERR(from_ref);
613 to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
614 if (IS_ERR(to_ref)) {
615 ret = PTR_ERR(to_ref);
619 ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
620 key_ref_to_ptr(to_ref), flags);
624 key_ref_put(from_ref);
626 key_ref_put(key_ref);
631 * Return a description of a key to userspace.
633 * The key must grant the caller View permission for this to work.
635 * If there's a buffer, we place up to buflen bytes of data into it formatted
636 * in the following way:
638 * type;uid;gid;perm;description<NUL>
640 * If successful, we return the amount of description available, irrespective
641 * of how much we may have copied into the buffer.
643 long keyctl_describe_key(key_serial_t keyid,
647 struct key *key, *instkey;
652 int desclen, infolen;
654 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
655 if (IS_ERR(key_ref)) {
656 /* viewing a key under construction is permitted if we have the
657 * authorisation token handy */
658 if (PTR_ERR(key_ref) == -EACCES) {
659 instkey = key_get_instantiation_authkey(keyid);
660 if (!IS_ERR(instkey)) {
662 key_ref = lookup_user_key(keyid,
665 if (!IS_ERR(key_ref))
670 ret = PTR_ERR(key_ref);
675 key = key_ref_to_ptr(key_ref);
676 desclen = strlen(key->description);
679 perm = key_acl_to_perm(rcu_dereference(key->acl));
682 /* calculate how much information we're going to return */
684 infobuf = kasprintf(GFP_KERNEL,
687 from_kuid_munged(current_user_ns(), key->uid),
688 from_kgid_munged(current_user_ns(), key->gid),
692 infolen = strlen(infobuf);
693 ret = infolen + desclen + 1;
695 /* consider returning the data */
696 if (buffer && buflen >= ret) {
697 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
698 copy_to_user(buffer + infolen, key->description,
705 key_ref_put(key_ref);
711 * Search the specified keyring and any keyrings it links to for a matching
712 * key. Only keyrings that grant the caller Search permission will be searched
713 * (this includes the starting keyring). Only keys with Search permission can
716 * If successful, the found key will be linked to the destination keyring if
717 * supplied and the key has Link permission, and the found key ID will be
720 long keyctl_keyring_search(key_serial_t ringid,
721 const char __user *_type,
722 const char __user *_description,
723 key_serial_t destringid)
725 struct key_type *ktype;
726 key_ref_t keyring_ref, key_ref, dest_ref;
727 char type[32], *description;
730 /* pull the type and description into kernel space */
731 ret = key_get_type_from_user(type, _type, sizeof(type));
735 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
736 if (IS_ERR(description)) {
737 ret = PTR_ERR(description);
741 /* get the keyring at which to begin the search */
742 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
743 if (IS_ERR(keyring_ref)) {
744 ret = PTR_ERR(keyring_ref);
748 /* get the destination keyring if specified */
751 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
753 if (IS_ERR(dest_ref)) {
754 ret = PTR_ERR(dest_ref);
759 /* find the key type */
760 ktype = key_type_lookup(type);
762 ret = PTR_ERR(ktype);
767 key_ref = keyring_search(keyring_ref, ktype, description, true);
768 if (IS_ERR(key_ref)) {
769 ret = PTR_ERR(key_ref);
771 /* treat lack or presence of a negative key the same */
777 /* link the resulting key to the destination keyring if we can */
779 ret = key_permission(key_ref, KEY_NEED_LINK);
783 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
788 ret = key_ref_to_ptr(key_ref)->serial;
791 key_ref_put(key_ref);
795 key_ref_put(dest_ref);
797 key_ref_put(keyring_ref);
805 * Read a key's payload.
807 * The key must either grant the caller Read permission, or it must grant the
808 * caller Search permission when searched for from the process keyrings.
810 * If successful, we place up to buflen bytes of data into the buffer, if one
811 * is provided, and return the amount of data that is available in the key,
812 * irrespective of how much we copied into the buffer.
814 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
820 /* find the key first */
821 key_ref = lookup_user_key(keyid, 0, 0);
822 if (IS_ERR(key_ref)) {
827 key = key_ref_to_ptr(key_ref);
829 ret = key_read_state(key);
831 goto error2; /* Negatively instantiated */
833 /* see if we can read it directly */
834 ret = key_permission(key_ref, KEY_NEED_READ);
840 /* we can't; see if it's searchable from this process's keyrings
841 * - we automatically take account of the fact that it may be
842 * dangling off an instantiation key
844 if (!is_key_possessed(key_ref)) {
849 /* the key is probably readable - now try to read it */
852 if (key->type->read) {
853 /* Read the data with the semaphore held (since we might sleep)
854 * to protect against the key being updated or revoked.
856 down_read(&key->sem);
857 ret = key_validate(key);
859 ret = key->type->read(key, buffer, buflen);
870 * Change the ownership of a key
872 * The key must grant the caller Setattr permission for this to work, though
873 * the key need not be fully instantiated yet. For the UID to be changed, or
874 * for the GID to be changed to a group the caller is not a member of, the
875 * caller must have sysadmin capability. If either uid or gid is -1 then that
876 * attribute is not changed.
878 * If the UID is to be changed, the new user must have sufficient quota to
879 * accept the key. The quota deduction will be removed from the old user to
880 * the new user should the attribute be changed.
882 * If successful, 0 will be returned.
884 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
886 struct key_user *newowner, *zapowner = NULL;
893 uid = make_kuid(current_user_ns(), user);
894 gid = make_kgid(current_user_ns(), group);
896 if ((user != (uid_t) -1) && !uid_valid(uid))
898 if ((group != (gid_t) -1) && !gid_valid(gid))
902 if (user == (uid_t) -1 && group == (gid_t) -1)
905 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
907 if (IS_ERR(key_ref)) {
908 ret = PTR_ERR(key_ref);
912 key = key_ref_to_ptr(key_ref);
914 /* make the changes with the locks held to prevent chown/chown races */
916 down_write(&key->sem);
918 if (!capable(CAP_SYS_ADMIN)) {
919 /* only the sysadmin can chown a key to some other UID */
920 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
923 /* only the sysadmin can set the key's GID to a group other
924 * than one of those that the current process subscribes to */
925 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
930 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
932 newowner = key_user_lookup(uid);
936 /* transfer the quota burden to the new user */
937 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
938 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
939 key_quota_root_maxkeys : key_quota_maxkeys;
940 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
941 key_quota_root_maxbytes : key_quota_maxbytes;
943 spin_lock(&newowner->lock);
944 if (newowner->qnkeys + 1 >= maxkeys ||
945 newowner->qnbytes + key->quotalen >= maxbytes ||
946 newowner->qnbytes + key->quotalen <
951 newowner->qnbytes += key->quotalen;
952 spin_unlock(&newowner->lock);
954 spin_lock(&key->user->lock);
956 key->user->qnbytes -= key->quotalen;
957 spin_unlock(&key->user->lock);
960 atomic_dec(&key->user->nkeys);
961 atomic_inc(&newowner->nkeys);
963 if (key->state != KEY_IS_UNINSTANTIATED) {
964 atomic_dec(&key->user->nikeys);
965 atomic_inc(&newowner->nikeys);
968 zapowner = key->user;
969 key->user = newowner;
974 if (group != (gid_t) -1)
983 key_user_put(zapowner);
988 spin_unlock(&newowner->lock);
995 * Change the permission mask on a key.
997 * The key must grant the caller Setattr permission for this to work, though
998 * the key need not be fully instantiated yet. If the caller does not have
999 * sysadmin capability, it may only change the permission on keys that it owns.
1001 long keyctl_setperm_key(key_serial_t id, unsigned int perm)
1003 struct key_acl *acl;
1009 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1013 if (perm & KEY_POS_ALL) nr++;
1014 if (perm & KEY_USR_ALL) nr++;
1015 if (perm & KEY_GRP_ALL) nr++;
1016 if (perm & KEY_OTH_ALL) nr++;
1018 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1020 if (IS_ERR(key_ref)) {
1021 ret = PTR_ERR(key_ref);
1025 key = key_ref_to_ptr(key_ref);
1028 if (test_bit(KEY_FLAG_HAS_ACL, &key->flags))
1032 acl = kzalloc(struct_size(acl, aces, nr), GFP_KERNEL);
1036 refcount_set(&acl->usage, 1);
1039 for (i = 0; i < 4; i++) {
1040 struct key_ace *ace = &acl->aces[j];
1041 unsigned int subset = (perm >> (i * 8)) & KEY_OTH_ALL;
1045 ace->type = KEY_ACE_SUBJ_STANDARD;
1046 ace->subject_id = KEY_ACE_EVERYONE + i;
1048 if (subset & (KEY_OTH_WRITE | KEY_OTH_SETATTR))
1049 ace->perm |= KEY_ACE_REVOKE;
1050 if (subset & KEY_OTH_SEARCH)
1051 ace->perm |= KEY_ACE_INVAL;
1052 if (key->type == &key_type_keyring) {
1053 if (subset & KEY_OTH_SEARCH)
1054 ace->perm |= KEY_ACE_JOIN;
1055 if (subset & KEY_OTH_WRITE)
1056 ace->perm |= KEY_ACE_CLEAR;
1061 /* make the changes with the locks held to prevent chown/chmod races */
1062 down_write(&key->sem);
1063 ret = key_set_acl(key, acl);
1064 up_write(&key->sem);
1072 * Get the destination keyring for instantiation and check that the caller has
1073 * Write permission on it.
1075 static long get_instantiation_keyring(key_serial_t ringid,
1076 struct request_key_auth *rka,
1077 struct key **_dest_keyring)
1081 *_dest_keyring = NULL;
1083 /* just return a NULL pointer if we weren't asked to make a link */
1087 /* if a specific keyring is nominated by ID, then use that */
1089 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
1091 return PTR_ERR(dkref);
1092 *_dest_keyring = key_ref_to_ptr(dkref);
1096 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1099 /* otherwise specify the destination keyring recorded in the
1100 * authorisation key (any KEY_SPEC_*_KEYRING) */
1101 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
1102 *_dest_keyring = key_get(rka->dest_keyring);
1110 * Change the request_key authorisation key on the current process.
1112 static int keyctl_change_reqkey_auth(struct key *key)
1116 new = prepare_creds();
1120 key_put(new->request_key_auth);
1121 new->request_key_auth = key_get(key);
1123 return commit_creds(new);
1127 * Instantiate a key with the specified payload and link the key into the
1128 * destination keyring if one is given.
1130 * The caller must have the appropriate instantiation permit set for this to
1131 * work (see keyctl_assume_authority). No other permissions are required.
1133 * If successful, 0 will be returned.
1135 long keyctl_instantiate_key_common(key_serial_t id,
1136 struct iov_iter *from,
1137 key_serial_t ringid)
1139 const struct cred *cred = current_cred();
1140 struct request_key_auth *rka;
1141 struct key *instkey, *dest_keyring;
1142 size_t plen = from ? iov_iter_count(from) : 0;
1146 kenter("%d,,%zu,%d", id, plen, ringid);
1152 if (plen > 1024 * 1024 - 1)
1155 /* the appropriate instantiation authorisation key must have been
1156 * assumed before calling this */
1158 instkey = cred->request_key_auth;
1162 rka = instkey->payload.data[0];
1163 if (rka->target_key->serial != id)
1166 /* pull the payload in if one was supplied */
1171 payload = kvmalloc(plen, GFP_KERNEL);
1176 if (!copy_from_iter_full(payload, plen, from))
1180 /* find the destination keyring amongst those belonging to the
1181 * requesting task */
1182 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1186 /* instantiate the key and link it into a keyring */
1187 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1188 dest_keyring, instkey);
1190 key_put(dest_keyring);
1192 /* discard the assumed authority if it's just been disabled by
1193 * instantiation of the key */
1195 keyctl_change_reqkey_auth(NULL);
1199 memzero_explicit(payload, plen);
1207 * Instantiate a key with the specified payload and link the key into the
1208 * destination keyring if one is given.
1210 * The caller must have the appropriate instantiation permit set for this to
1211 * work (see keyctl_assume_authority). No other permissions are required.
1213 * If successful, 0 will be returned.
1215 long keyctl_instantiate_key(key_serial_t id,
1216 const void __user *_payload,
1218 key_serial_t ringid)
1220 if (_payload && plen) {
1222 struct iov_iter from;
1225 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1230 return keyctl_instantiate_key_common(id, &from, ringid);
1233 return keyctl_instantiate_key_common(id, NULL, ringid);
1237 * Instantiate a key with the specified multipart payload and link the key into
1238 * the destination keyring if one is given.
1240 * The caller must have the appropriate instantiation permit set for this to
1241 * work (see keyctl_assume_authority). No other permissions are required.
1243 * If successful, 0 will be returned.
1245 long keyctl_instantiate_key_iov(key_serial_t id,
1246 const struct iovec __user *_payload_iov,
1248 key_serial_t ringid)
1250 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1251 struct iov_iter from;
1257 ret = import_iovec(WRITE, _payload_iov, ioc,
1258 ARRAY_SIZE(iovstack), &iov, &from);
1261 ret = keyctl_instantiate_key_common(id, &from, ringid);
1267 * Negatively instantiate the key with the given timeout (in seconds) and link
1268 * the key into the destination keyring if one is given.
1270 * The caller must have the appropriate instantiation permit set for this to
1271 * work (see keyctl_assume_authority). No other permissions are required.
1273 * The key and any links to the key will be automatically garbage collected
1274 * after the timeout expires.
1276 * Negative keys are used to rate limit repeated request_key() calls by causing
1277 * them to return -ENOKEY until the negative key expires.
1279 * If successful, 0 will be returned.
1281 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1283 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1287 * Negatively instantiate the key with the given timeout (in seconds) and error
1288 * code and link the key into the destination keyring if one is given.
1290 * The caller must have the appropriate instantiation permit set for this to
1291 * work (see keyctl_assume_authority). No other permissions are required.
1293 * The key and any links to the key will be automatically garbage collected
1294 * after the timeout expires.
1296 * Negative keys are used to rate limit repeated request_key() calls by causing
1297 * them to return the specified error code until the negative key expires.
1299 * If successful, 0 will be returned.
1301 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1302 key_serial_t ringid)
1304 const struct cred *cred = current_cred();
1305 struct request_key_auth *rka;
1306 struct key *instkey, *dest_keyring;
1309 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1311 /* must be a valid error code and mustn't be a kernel special */
1313 error >= MAX_ERRNO ||
1314 error == ERESTARTSYS ||
1315 error == ERESTARTNOINTR ||
1316 error == ERESTARTNOHAND ||
1317 error == ERESTART_RESTARTBLOCK)
1320 /* the appropriate instantiation authorisation key must have been
1321 * assumed before calling this */
1323 instkey = cred->request_key_auth;
1327 rka = instkey->payload.data[0];
1328 if (rka->target_key->serial != id)
1331 /* find the destination keyring if present (which must also be
1333 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1337 /* instantiate the key and link it into a keyring */
1338 ret = key_reject_and_link(rka->target_key, timeout, error,
1339 dest_keyring, instkey);
1341 key_put(dest_keyring);
1343 /* discard the assumed authority if it's just been disabled by
1344 * instantiation of the key */
1346 keyctl_change_reqkey_auth(NULL);
1353 * Read or set the default keyring in which request_key() will cache keys and
1354 * return the old setting.
1356 * If a thread or process keyring is specified then it will be created if it
1357 * doesn't yet exist. The old setting will be returned if successful.
1359 long keyctl_set_reqkey_keyring(int reqkey_defl)
1362 int ret, old_setting;
1364 old_setting = current_cred_xxx(jit_keyring);
1366 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1369 new = prepare_creds();
1373 switch (reqkey_defl) {
1374 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1375 ret = install_thread_keyring_to_cred(new);
1380 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1381 ret = install_process_keyring_to_cred(new);
1386 case KEY_REQKEY_DEFL_DEFAULT:
1387 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1388 case KEY_REQKEY_DEFL_USER_KEYRING:
1389 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1390 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1393 case KEY_REQKEY_DEFL_NO_CHANGE:
1394 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1401 new->jit_keyring = reqkey_defl;
1410 * Set or clear the timeout on a key.
1412 * Either the key must grant the caller Setattr permission or else the caller
1413 * must hold an instantiation authorisation token for the key.
1415 * The timeout is either 0 to clear the timeout, or a number of seconds from
1416 * the current time. The key and any links to the key will be automatically
1417 * garbage collected after the timeout expires.
1419 * Keys with KEY_FLAG_KEEP set should not be timed out.
1421 * If successful, 0 is returned.
1423 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1425 struct key *key, *instkey;
1429 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1431 if (IS_ERR(key_ref)) {
1432 /* setting the timeout on a key under construction is permitted
1433 * if we have the authorisation token handy */
1434 if (PTR_ERR(key_ref) == -EACCES) {
1435 instkey = key_get_instantiation_authkey(id);
1436 if (!IS_ERR(instkey)) {
1438 key_ref = lookup_user_key(id,
1441 if (!IS_ERR(key_ref))
1446 ret = PTR_ERR(key_ref);
1451 key = key_ref_to_ptr(key_ref);
1453 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1456 key_set_timeout(key, timeout);
1464 * Assume (or clear) the authority to instantiate the specified key.
1466 * This sets the authoritative token currently in force for key instantiation.
1467 * This must be done for a key to be instantiated. It has the effect of making
1468 * available all the keys from the caller of the request_key() that created a
1469 * key to request_key() calls made by the caller of this function.
1471 * The caller must have the instantiation key in their process keyrings with a
1472 * Search permission grant available to the caller.
1474 * If the ID given is 0, then the setting will be cleared and 0 returned.
1476 * If the ID given has a matching an authorisation key, then that key will be
1477 * set and its ID will be returned. The authorisation key can be read to get
1478 * the callout information passed to request_key().
1480 long keyctl_assume_authority(key_serial_t id)
1482 struct key *authkey;
1485 /* special key IDs aren't permitted */
1490 /* we divest ourselves of authority if given an ID of 0 */
1492 ret = keyctl_change_reqkey_auth(NULL);
1496 /* attempt to assume the authority temporarily granted to us whilst we
1497 * instantiate the specified key
1498 * - the authorisation key must be in the current task's keyrings
1501 authkey = key_get_instantiation_authkey(id);
1502 if (IS_ERR(authkey)) {
1503 ret = PTR_ERR(authkey);
1507 ret = keyctl_change_reqkey_auth(authkey);
1509 ret = authkey->serial;
1516 * Get a key's the LSM security label.
1518 * The key must grant the caller View permission for this to work.
1520 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1522 * If successful, the amount of information available will be returned,
1523 * irrespective of how much was copied (including the terminal NUL).
1525 long keyctl_get_security(key_serial_t keyid,
1526 char __user *buffer,
1529 struct key *key, *instkey;
1534 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1535 if (IS_ERR(key_ref)) {
1536 if (PTR_ERR(key_ref) != -EACCES)
1537 return PTR_ERR(key_ref);
1539 /* viewing a key under construction is also permitted if we
1540 * have the authorisation token handy */
1541 instkey = key_get_instantiation_authkey(keyid);
1542 if (IS_ERR(instkey))
1543 return PTR_ERR(instkey);
1546 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1547 if (IS_ERR(key_ref))
1548 return PTR_ERR(key_ref);
1551 key = key_ref_to_ptr(key_ref);
1552 ret = security_key_getsecurity(key, &context);
1554 /* if no information was returned, give userspace an empty
1557 if (buffer && buflen > 0 &&
1558 copy_to_user(buffer, "", 1) != 0)
1560 } else if (ret > 0) {
1561 /* return as much data as there's room for */
1562 if (buffer && buflen > 0) {
1566 if (copy_to_user(buffer, context, buflen) != 0)
1573 key_ref_put(key_ref);
1578 * Attempt to install the calling process's session keyring on the process's
1581 * The keyring must exist and must grant the caller JOIN permission, and the
1582 * parent process must be single-threaded and must have the same effective
1583 * ownership as this process and mustn't be SUID/SGID.
1585 * The keyring will be emplaced on the parent when it next resumes userspace.
1587 * If successful, 0 will be returned.
1589 long keyctl_session_to_parent(void)
1591 struct task_struct *me, *parent;
1592 const struct cred *mycred, *pcred;
1593 struct callback_head *newwork, *oldwork;
1594 key_ref_t keyring_r;
1598 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_JOIN);
1599 if (IS_ERR(keyring_r))
1600 return PTR_ERR(keyring_r);
1604 /* our parent is going to need a new cred struct, a new tgcred struct
1605 * and new security data, so we allocate them here to prevent ENOMEM in
1607 cred = cred_alloc_blank();
1610 newwork = &cred->rcu;
1612 cred->session_keyring = key_ref_to_ptr(keyring_r);
1614 init_task_work(newwork, key_change_session_keyring);
1618 write_lock_irq(&tasklist_lock);
1622 parent = rcu_dereference_protected(me->real_parent,
1623 lockdep_is_held(&tasklist_lock));
1625 /* the parent mustn't be init and mustn't be a kernel thread */
1626 if (parent->pid <= 1 || !parent->mm)
1629 /* the parent must be single threaded */
1630 if (!thread_group_empty(parent))
1633 /* the parent and the child must have different session keyrings or
1634 * there's no point */
1635 mycred = current_cred();
1636 pcred = __task_cred(parent);
1637 if (mycred == pcred ||
1638 mycred->session_keyring == pcred->session_keyring) {
1643 /* the parent must have the same effective ownership and mustn't be
1645 if (!uid_eq(pcred->uid, mycred->euid) ||
1646 !uid_eq(pcred->euid, mycred->euid) ||
1647 !uid_eq(pcred->suid, mycred->euid) ||
1648 !gid_eq(pcred->gid, mycred->egid) ||
1649 !gid_eq(pcred->egid, mycred->egid) ||
1650 !gid_eq(pcred->sgid, mycred->egid))
1653 /* the keyrings must have the same UID */
1654 if ((pcred->session_keyring &&
1655 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1656 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1659 /* cancel an already pending keyring replacement */
1660 oldwork = task_work_cancel(parent, key_change_session_keyring);
1662 /* the replacement session keyring is applied just prior to userspace
1664 ret = task_work_add(parent, newwork, true);
1668 write_unlock_irq(&tasklist_lock);
1671 put_cred(container_of(oldwork, struct cred, rcu));
1677 key_ref_put(keyring_r);
1682 * Apply a restriction to a given keyring.
1684 * The caller must have Setattr permission to change keyring restrictions.
1686 * The requested type name may be a NULL pointer to reject all attempts
1687 * to link to the keyring. In this case, _restriction must also be NULL.
1688 * Otherwise, both _type and _restriction must be non-NULL.
1690 * Returns 0 if successful.
1692 long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1693 const char __user *_restriction)
1697 char *restriction = NULL;
1700 key_ref = lookup_user_key(id, 0, KEY_NEED_SETSEC);
1701 if (IS_ERR(key_ref))
1702 return PTR_ERR(key_ref);
1709 ret = key_get_type_from_user(type, _type, sizeof(type));
1713 restriction = strndup_user(_restriction, PAGE_SIZE);
1714 if (IS_ERR(restriction)) {
1715 ret = PTR_ERR(restriction);
1723 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
1726 key_ref_put(key_ref);
1731 * Get keyrings subsystem capabilities.
1733 long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1735 size_t size = buflen;
1738 if (size > sizeof(keyrings_capabilities))
1739 size = sizeof(keyrings_capabilities);
1740 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1742 if (size < buflen &&
1743 clear_user(_buffer + size, buflen - size) != 0)
1747 return sizeof(keyrings_capabilities);
1751 * The key control system call
1753 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1754 unsigned long, arg4, unsigned long, arg5)
1757 case KEYCTL_GET_KEYRING_ID:
1758 return keyctl_get_keyring_ID((key_serial_t) arg2,
1761 case KEYCTL_JOIN_SESSION_KEYRING:
1762 return keyctl_join_session_keyring((const char __user *) arg2);
1765 return keyctl_update_key((key_serial_t) arg2,
1766 (const void __user *) arg3,
1770 return keyctl_revoke_key((key_serial_t) arg2);
1772 case KEYCTL_DESCRIBE:
1773 return keyctl_describe_key((key_serial_t) arg2,
1774 (char __user *) arg3,
1778 return keyctl_keyring_clear((key_serial_t) arg2);
1781 return keyctl_keyring_link((key_serial_t) arg2,
1782 (key_serial_t) arg3);
1785 return keyctl_keyring_unlink((key_serial_t) arg2,
1786 (key_serial_t) arg3);
1789 return keyctl_keyring_search((key_serial_t) arg2,
1790 (const char __user *) arg3,
1791 (const char __user *) arg4,
1792 (key_serial_t) arg5);
1795 return keyctl_read_key((key_serial_t) arg2,
1796 (char __user *) arg3,
1800 return keyctl_chown_key((key_serial_t) arg2,
1804 case KEYCTL_SETPERM:
1805 return keyctl_setperm_key((key_serial_t) arg2,
1806 (unsigned int)arg3);
1808 case KEYCTL_INSTANTIATE:
1809 return keyctl_instantiate_key((key_serial_t) arg2,
1810 (const void __user *) arg3,
1812 (key_serial_t) arg5);
1815 return keyctl_negate_key((key_serial_t) arg2,
1817 (key_serial_t) arg4);
1819 case KEYCTL_SET_REQKEY_KEYRING:
1820 return keyctl_set_reqkey_keyring(arg2);
1822 case KEYCTL_SET_TIMEOUT:
1823 return keyctl_set_timeout((key_serial_t) arg2,
1826 case KEYCTL_ASSUME_AUTHORITY:
1827 return keyctl_assume_authority((key_serial_t) arg2);
1829 case KEYCTL_GET_SECURITY:
1830 return keyctl_get_security((key_serial_t) arg2,
1831 (char __user *) arg3,
1834 case KEYCTL_SESSION_TO_PARENT:
1835 return keyctl_session_to_parent();
1838 return keyctl_reject_key((key_serial_t) arg2,
1841 (key_serial_t) arg5);
1843 case KEYCTL_INSTANTIATE_IOV:
1844 return keyctl_instantiate_key_iov(
1845 (key_serial_t) arg2,
1846 (const struct iovec __user *) arg3,
1848 (key_serial_t) arg5);
1850 case KEYCTL_INVALIDATE:
1851 return keyctl_invalidate_key((key_serial_t) arg2);
1853 case KEYCTL_GET_PERSISTENT:
1854 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1856 case KEYCTL_DH_COMPUTE:
1857 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
1858 (char __user *) arg3, (size_t) arg4,
1859 (struct keyctl_kdf_params __user *) arg5);
1861 case KEYCTL_RESTRICT_KEYRING:
1862 return keyctl_restrict_keyring((key_serial_t) arg2,
1863 (const char __user *) arg3,
1864 (const char __user *) arg4);
1866 case KEYCTL_PKEY_QUERY:
1869 return keyctl_pkey_query((key_serial_t)arg2,
1870 (const char __user *)arg4,
1871 (struct keyctl_pkey_query __user *)arg5);
1873 case KEYCTL_PKEY_ENCRYPT:
1874 case KEYCTL_PKEY_DECRYPT:
1875 case KEYCTL_PKEY_SIGN:
1876 return keyctl_pkey_e_d_s(
1878 (const struct keyctl_pkey_params __user *)arg2,
1879 (const char __user *)arg3,
1880 (const void __user *)arg4,
1881 (void __user *)arg5);
1883 case KEYCTL_PKEY_VERIFY:
1884 return keyctl_pkey_verify(
1885 (const struct keyctl_pkey_params __user *)arg2,
1886 (const char __user *)arg3,
1887 (const void __user *)arg4,
1888 (const void __user *)arg5);
1891 return keyctl_keyring_move((key_serial_t)arg2,
1894 (unsigned int)arg5);
1895 case KEYCTL_GRANT_PERMISSION:
1896 return keyctl_grant_permission((key_serial_t)arg2,
1897 (enum key_ace_subject_type)arg3,
1899 (unsigned int)arg5);
1901 case KEYCTL_CAPABILITIES:
1902 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);