1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Userspace key control operations
4 * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/sched/task.h>
11 #include <linux/slab.h>
12 #include <linux/syscalls.h>
13 #include <linux/key.h>
14 #include <linux/keyctl.h>
16 #include <linux/capability.h>
17 #include <linux/cred.h>
18 #include <linux/string.h>
19 #include <linux/err.h>
20 #include <linux/vmalloc.h>
21 #include <linux/security.h>
22 #include <linux/uio.h>
23 #include <linux/uaccess.h>
24 #include <keys/request_key_auth-type.h>
27 #define KEY_MAX_DESC_SIZE 4096
29 static const unsigned char keyrings_capabilities[1] = {
30 [0] = (KEYCTL_CAPS0_CAPABILITIES |
31 (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
32 (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
33 (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
34 (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
35 KEYCTL_CAPS0_INVALIDATE |
36 KEYCTL_CAPS0_RESTRICT_KEYRING |
41 static int key_get_type_from_user(char *type,
42 const char __user *_type,
47 ret = strncpy_from_user(type, _type, len);
50 if (ret == 0 || ret >= len)
59 * Extract the description of a new key from userspace and either add it as a
60 * new key to the specified keyring or update a matching key in that keyring.
62 * If the description is NULL or an empty string, the key type is asked to
63 * generate one from the payload.
65 * The keyring must be writable so that we can attach the key to it.
67 * If successful, the new key's serial number is returned, otherwise an error
70 SYSCALL_DEFINE5(add_key, const char __user *, _type,
71 const char __user *, _description,
72 const void __user *, _payload,
76 key_ref_t keyring_ref, key_ref;
77 char type[32], *description;
82 if (plen > 1024 * 1024 - 1)
85 /* draw all the data into kernel space */
86 ret = key_get_type_from_user(type, _type, sizeof(type));
92 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
93 if (IS_ERR(description)) {
94 ret = PTR_ERR(description);
100 } else if ((description[0] == '.') &&
101 (strncmp(type, "keyring", 7) == 0)) {
107 /* pull the payload in if one was supplied */
112 payload = kvmalloc(plen, GFP_KERNEL);
117 if (copy_from_user(payload, _payload, plen) != 0)
121 /* find the target keyring (which must be writable) */
122 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
123 if (IS_ERR(keyring_ref)) {
124 ret = PTR_ERR(keyring_ref);
128 /* create or update the requested key and add it to the target
130 key_ref = key_create_or_update(keyring_ref, type, description,
131 payload, plen, KEY_PERM_UNDEF,
133 if (!IS_ERR(key_ref)) {
134 ret = key_ref_to_ptr(key_ref)->serial;
135 key_ref_put(key_ref);
138 ret = PTR_ERR(key_ref);
141 key_ref_put(keyring_ref);
144 memzero_explicit(payload, plen);
154 * Search the process keyrings and keyring trees linked from those for a
155 * matching key. Keyrings must have appropriate Search permission to be
158 * If a key is found, it will be attached to the destination keyring if there's
159 * one specified and the serial number of the key will be returned.
161 * If no key is found, /sbin/request-key will be invoked if _callout_info is
162 * non-NULL in an attempt to create a key. The _callout_info string will be
163 * passed to /sbin/request-key to aid with completing the request. If the
164 * _callout_info string is "" then it will be changed to "-".
166 SYSCALL_DEFINE4(request_key, const char __user *, _type,
167 const char __user *, _description,
168 const char __user *, _callout_info,
169 key_serial_t, destringid)
171 struct key_type *ktype;
175 char type[32], *description, *callout_info;
178 /* pull the type into kernel space */
179 ret = key_get_type_from_user(type, _type, sizeof(type));
183 /* pull the description into kernel space */
184 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
185 if (IS_ERR(description)) {
186 ret = PTR_ERR(description);
190 /* pull the callout info into kernel space */
194 callout_info = strndup_user(_callout_info, PAGE_SIZE);
195 if (IS_ERR(callout_info)) {
196 ret = PTR_ERR(callout_info);
199 callout_len = strlen(callout_info);
202 /* get the destination keyring if specified */
205 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
207 if (IS_ERR(dest_ref)) {
208 ret = PTR_ERR(dest_ref);
213 /* find the key type */
214 ktype = key_type_lookup(type);
216 ret = PTR_ERR(ktype);
221 key = request_key_and_link(ktype, description, callout_info,
222 callout_len, NULL, key_ref_to_ptr(dest_ref),
229 /* wait for the key to finish being constructed */
230 ret = wait_for_key_construction(key, 1);
241 key_ref_put(dest_ref);
251 * Get the ID of the specified process keyring.
253 * The requested keyring must have search permission to be found.
255 * If successful, the ID of the requested keyring will be returned.
257 long keyctl_get_keyring_ID(key_serial_t id, int create)
260 unsigned long lflags;
263 lflags = create ? KEY_LOOKUP_CREATE : 0;
264 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
265 if (IS_ERR(key_ref)) {
266 ret = PTR_ERR(key_ref);
270 ret = key_ref_to_ptr(key_ref)->serial;
271 key_ref_put(key_ref);
277 * Join a (named) session keyring.
279 * Create and join an anonymous session keyring or join a named session
280 * keyring, creating it if necessary. A named session keyring must have Search
281 * permission for it to be joined. Session keyrings without this permit will
282 * be skipped over. It is not permitted for userspace to create or join
283 * keyrings whose name begin with a dot.
285 * If successful, the ID of the joined session keyring will be returned.
287 long keyctl_join_session_keyring(const char __user *_name)
292 /* fetch the name from userspace */
295 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
306 /* join the session */
307 ret = join_session_keyring(name);
315 * Update a key's data payload from the given data.
317 * The key must grant the caller Write permission and the key type must support
318 * updating for this to work. A negative key can be positively instantiated
321 * If successful, 0 will be returned. If the key type does not support
322 * updating, then -EOPNOTSUPP will be returned.
324 long keyctl_update_key(key_serial_t id,
325 const void __user *_payload,
333 if (plen > PAGE_SIZE)
336 /* pull the payload in if one was supplied */
340 payload = kmalloc(plen, GFP_KERNEL);
345 if (copy_from_user(payload, _payload, plen) != 0)
349 /* find the target key (which must be writable) */
350 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
351 if (IS_ERR(key_ref)) {
352 ret = PTR_ERR(key_ref);
357 ret = key_update(key_ref, payload, plen);
359 key_ref_put(key_ref);
369 * The key must be grant the caller Write or Setattr permission for this to
370 * work. The key type should give up its quota claim when revoked. The key
371 * and any links to the key will be automatically garbage collected after a
372 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
374 * Keys with KEY_FLAG_KEEP set should not be revoked.
376 * If successful, 0 is returned.
378 long keyctl_revoke_key(key_serial_t id)
384 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
385 if (IS_ERR(key_ref)) {
386 ret = PTR_ERR(key_ref);
389 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
390 if (IS_ERR(key_ref)) {
391 ret = PTR_ERR(key_ref);
396 key = key_ref_to_ptr(key_ref);
398 if (test_bit(KEY_FLAG_KEEP, &key->flags))
403 key_ref_put(key_ref);
411 * The key must be grant the caller Invalidate permission for this to work.
412 * The key and any links to the key will be automatically garbage collected
415 * Keys with KEY_FLAG_KEEP set should not be invalidated.
417 * If successful, 0 is returned.
419 long keyctl_invalidate_key(key_serial_t id)
427 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
428 if (IS_ERR(key_ref)) {
429 ret = PTR_ERR(key_ref);
431 /* Root is permitted to invalidate certain special keys */
432 if (capable(CAP_SYS_ADMIN)) {
433 key_ref = lookup_user_key(id, 0, 0);
436 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
437 &key_ref_to_ptr(key_ref)->flags))
446 key = key_ref_to_ptr(key_ref);
448 if (test_bit(KEY_FLAG_KEEP, &key->flags))
453 key_ref_put(key_ref);
455 kleave(" = %ld", ret);
460 * Clear the specified keyring, creating an empty process keyring if one of the
461 * special keyring IDs is used.
463 * The keyring must grant the caller Write permission and not have
464 * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
466 long keyctl_keyring_clear(key_serial_t ringid)
468 key_ref_t keyring_ref;
472 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
473 if (IS_ERR(keyring_ref)) {
474 ret = PTR_ERR(keyring_ref);
476 /* Root is permitted to invalidate certain special keyrings */
477 if (capable(CAP_SYS_ADMIN)) {
478 keyring_ref = lookup_user_key(ringid, 0, 0);
479 if (IS_ERR(keyring_ref))
481 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
482 &key_ref_to_ptr(keyring_ref)->flags))
491 keyring = key_ref_to_ptr(keyring_ref);
492 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
495 ret = keyring_clear(keyring);
497 key_ref_put(keyring_ref);
503 * Create a link from a keyring to a key if there's no matching key in the
504 * keyring, otherwise replace the link to the matching key with a link to the
507 * The key must grant the caller Link permission and the the keyring must grant
508 * the caller Write permission. Furthermore, if an additional link is created,
509 * the keyring's quota will be extended.
511 * If successful, 0 will be returned.
513 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
515 key_ref_t keyring_ref, key_ref;
518 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
519 if (IS_ERR(keyring_ref)) {
520 ret = PTR_ERR(keyring_ref);
524 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
525 if (IS_ERR(key_ref)) {
526 ret = PTR_ERR(key_ref);
530 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
532 key_ref_put(key_ref);
534 key_ref_put(keyring_ref);
540 * Unlink a key from a keyring.
542 * The keyring must grant the caller Write permission for this to work; the key
543 * itself need not grant the caller anything. If the last link to a key is
544 * removed then that key will be scheduled for destruction.
546 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
548 * If successful, 0 will be returned.
550 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
552 key_ref_t keyring_ref, key_ref;
553 struct key *keyring, *key;
556 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
557 if (IS_ERR(keyring_ref)) {
558 ret = PTR_ERR(keyring_ref);
562 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
563 if (IS_ERR(key_ref)) {
564 ret = PTR_ERR(key_ref);
568 keyring = key_ref_to_ptr(keyring_ref);
569 key = key_ref_to_ptr(key_ref);
570 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
571 test_bit(KEY_FLAG_KEEP, &key->flags))
574 ret = key_unlink(keyring, key);
576 key_ref_put(key_ref);
578 key_ref_put(keyring_ref);
584 * Move a link to a key from one keyring to another, displacing any matching
585 * key from the destination keyring.
587 * The key must grant the caller Link permission and both keyrings must grant
588 * the caller Write permission. There must also be a link in the from keyring
589 * to the key. If both keyrings are the same, nothing is done.
591 * If successful, 0 will be returned.
593 long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
594 key_serial_t to_ringid, unsigned int flags)
596 key_ref_t key_ref, from_ref, to_ref;
599 if (flags & ~KEYCTL_MOVE_EXCL)
602 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
604 return PTR_ERR(key_ref);
606 from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
607 if (IS_ERR(from_ref)) {
608 ret = PTR_ERR(from_ref);
612 to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
613 if (IS_ERR(to_ref)) {
614 ret = PTR_ERR(to_ref);
618 ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
619 key_ref_to_ptr(to_ref), flags);
623 key_ref_put(from_ref);
625 key_ref_put(key_ref);
630 * Return a description of a key to userspace.
632 * The key must grant the caller View permission for this to work.
634 * If there's a buffer, we place up to buflen bytes of data into it formatted
635 * in the following way:
637 * type;uid;gid;perm;description<NUL>
639 * If successful, we return the amount of description available, irrespective
640 * of how much we may have copied into the buffer.
642 long keyctl_describe_key(key_serial_t keyid,
646 struct key *key, *instkey;
650 int desclen, infolen;
652 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
653 if (IS_ERR(key_ref)) {
654 /* viewing a key under construction is permitted if we have the
655 * authorisation token handy */
656 if (PTR_ERR(key_ref) == -EACCES) {
657 instkey = key_get_instantiation_authkey(keyid);
658 if (!IS_ERR(instkey)) {
660 key_ref = lookup_user_key(keyid,
663 if (!IS_ERR(key_ref))
668 ret = PTR_ERR(key_ref);
673 key = key_ref_to_ptr(key_ref);
674 desclen = strlen(key->description);
676 /* calculate how much information we're going to return */
678 infobuf = kasprintf(GFP_KERNEL,
681 from_kuid_munged(current_user_ns(), key->uid),
682 from_kgid_munged(current_user_ns(), key->gid),
686 infolen = strlen(infobuf);
687 ret = infolen + desclen + 1;
689 /* consider returning the data */
690 if (buffer && buflen >= ret) {
691 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
692 copy_to_user(buffer + infolen, key->description,
699 key_ref_put(key_ref);
705 * Search the specified keyring and any keyrings it links to for a matching
706 * key. Only keyrings that grant the caller Search permission will be searched
707 * (this includes the starting keyring). Only keys with Search permission can
710 * If successful, the found key will be linked to the destination keyring if
711 * supplied and the key has Link permission, and the found key ID will be
714 long keyctl_keyring_search(key_serial_t ringid,
715 const char __user *_type,
716 const char __user *_description,
717 key_serial_t destringid)
719 struct key_type *ktype;
720 key_ref_t keyring_ref, key_ref, dest_ref;
721 char type[32], *description;
724 /* pull the type and description into kernel space */
725 ret = key_get_type_from_user(type, _type, sizeof(type));
729 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
730 if (IS_ERR(description)) {
731 ret = PTR_ERR(description);
735 /* get the keyring at which to begin the search */
736 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
737 if (IS_ERR(keyring_ref)) {
738 ret = PTR_ERR(keyring_ref);
742 /* get the destination keyring if specified */
745 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
747 if (IS_ERR(dest_ref)) {
748 ret = PTR_ERR(dest_ref);
753 /* find the key type */
754 ktype = key_type_lookup(type);
756 ret = PTR_ERR(ktype);
761 key_ref = keyring_search(keyring_ref, ktype, description);
762 if (IS_ERR(key_ref)) {
763 ret = PTR_ERR(key_ref);
765 /* treat lack or presence of a negative key the same */
771 /* link the resulting key to the destination keyring if we can */
773 ret = key_permission(key_ref, KEY_NEED_LINK);
777 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
782 ret = key_ref_to_ptr(key_ref)->serial;
785 key_ref_put(key_ref);
789 key_ref_put(dest_ref);
791 key_ref_put(keyring_ref);
799 * Read a key's payload.
801 * The key must either grant the caller Read permission, or it must grant the
802 * caller Search permission when searched for from the process keyrings.
804 * If successful, we place up to buflen bytes of data into the buffer, if one
805 * is provided, and return the amount of data that is available in the key,
806 * irrespective of how much we copied into the buffer.
808 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
814 /* find the key first */
815 key_ref = lookup_user_key(keyid, 0, 0);
816 if (IS_ERR(key_ref)) {
821 key = key_ref_to_ptr(key_ref);
823 ret = key_read_state(key);
825 goto error2; /* Negatively instantiated */
827 /* see if we can read it directly */
828 ret = key_permission(key_ref, KEY_NEED_READ);
834 /* we can't; see if it's searchable from this process's keyrings
835 * - we automatically take account of the fact that it may be
836 * dangling off an instantiation key
838 if (!is_key_possessed(key_ref)) {
843 /* the key is probably readable - now try to read it */
846 if (key->type->read) {
847 /* Read the data with the semaphore held (since we might sleep)
848 * to protect against the key being updated or revoked.
850 down_read(&key->sem);
851 ret = key_validate(key);
853 ret = key->type->read(key, buffer, buflen);
864 * Change the ownership of a key
866 * The key must grant the caller Setattr permission for this to work, though
867 * the key need not be fully instantiated yet. For the UID to be changed, or
868 * for the GID to be changed to a group the caller is not a member of, the
869 * caller must have sysadmin capability. If either uid or gid is -1 then that
870 * attribute is not changed.
872 * If the UID is to be changed, the new user must have sufficient quota to
873 * accept the key. The quota deduction will be removed from the old user to
874 * the new user should the attribute be changed.
876 * If successful, 0 will be returned.
878 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
880 struct key_user *newowner, *zapowner = NULL;
887 uid = make_kuid(current_user_ns(), user);
888 gid = make_kgid(current_user_ns(), group);
890 if ((user != (uid_t) -1) && !uid_valid(uid))
892 if ((group != (gid_t) -1) && !gid_valid(gid))
896 if (user == (uid_t) -1 && group == (gid_t) -1)
899 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
901 if (IS_ERR(key_ref)) {
902 ret = PTR_ERR(key_ref);
906 key = key_ref_to_ptr(key_ref);
908 /* make the changes with the locks held to prevent chown/chown races */
910 down_write(&key->sem);
912 if (!capable(CAP_SYS_ADMIN)) {
913 /* only the sysadmin can chown a key to some other UID */
914 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
917 /* only the sysadmin can set the key's GID to a group other
918 * than one of those that the current process subscribes to */
919 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
924 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
926 newowner = key_user_lookup(uid);
930 /* transfer the quota burden to the new user */
931 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
932 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
933 key_quota_root_maxkeys : key_quota_maxkeys;
934 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
935 key_quota_root_maxbytes : key_quota_maxbytes;
937 spin_lock(&newowner->lock);
938 if (newowner->qnkeys + 1 >= maxkeys ||
939 newowner->qnbytes + key->quotalen >= maxbytes ||
940 newowner->qnbytes + key->quotalen <
945 newowner->qnbytes += key->quotalen;
946 spin_unlock(&newowner->lock);
948 spin_lock(&key->user->lock);
950 key->user->qnbytes -= key->quotalen;
951 spin_unlock(&key->user->lock);
954 atomic_dec(&key->user->nkeys);
955 atomic_inc(&newowner->nkeys);
957 if (key->state != KEY_IS_UNINSTANTIATED) {
958 atomic_dec(&key->user->nikeys);
959 atomic_inc(&newowner->nikeys);
962 zapowner = key->user;
963 key->user = newowner;
968 if (group != (gid_t) -1)
977 key_user_put(zapowner);
982 spin_unlock(&newowner->lock);
989 * Change the permission mask on a key.
991 * The key must grant the caller Setattr permission for this to work, though
992 * the key need not be fully instantiated yet. If the caller does not have
993 * sysadmin capability, it may only change the permission on keys that it owns.
995 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
1002 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
1005 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1007 if (IS_ERR(key_ref)) {
1008 ret = PTR_ERR(key_ref);
1012 key = key_ref_to_ptr(key_ref);
1014 /* make the changes with the locks held to prevent chown/chmod races */
1016 down_write(&key->sem);
1018 /* if we're not the sysadmin, we can only change a key that we own */
1019 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
1024 up_write(&key->sem);
1031 * Get the destination keyring for instantiation and check that the caller has
1032 * Write permission on it.
1034 static long get_instantiation_keyring(key_serial_t ringid,
1035 struct request_key_auth *rka,
1036 struct key **_dest_keyring)
1040 *_dest_keyring = NULL;
1042 /* just return a NULL pointer if we weren't asked to make a link */
1046 /* if a specific keyring is nominated by ID, then use that */
1048 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
1050 return PTR_ERR(dkref);
1051 *_dest_keyring = key_ref_to_ptr(dkref);
1055 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1058 /* otherwise specify the destination keyring recorded in the
1059 * authorisation key (any KEY_SPEC_*_KEYRING) */
1060 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
1061 *_dest_keyring = key_get(rka->dest_keyring);
1069 * Change the request_key authorisation key on the current process.
1071 static int keyctl_change_reqkey_auth(struct key *key)
1075 new = prepare_creds();
1079 key_put(new->request_key_auth);
1080 new->request_key_auth = key_get(key);
1082 return commit_creds(new);
1086 * Instantiate a key with the specified payload and link the key into the
1087 * destination keyring if one is given.
1089 * The caller must have the appropriate instantiation permit set for this to
1090 * work (see keyctl_assume_authority). No other permissions are required.
1092 * If successful, 0 will be returned.
1094 long keyctl_instantiate_key_common(key_serial_t id,
1095 struct iov_iter *from,
1096 key_serial_t ringid)
1098 const struct cred *cred = current_cred();
1099 struct request_key_auth *rka;
1100 struct key *instkey, *dest_keyring;
1101 size_t plen = from ? iov_iter_count(from) : 0;
1105 kenter("%d,,%zu,%d", id, plen, ringid);
1111 if (plen > 1024 * 1024 - 1)
1114 /* the appropriate instantiation authorisation key must have been
1115 * assumed before calling this */
1117 instkey = cred->request_key_auth;
1121 rka = instkey->payload.data[0];
1122 if (rka->target_key->serial != id)
1125 /* pull the payload in if one was supplied */
1130 payload = kvmalloc(plen, GFP_KERNEL);
1135 if (!copy_from_iter_full(payload, plen, from))
1139 /* find the destination keyring amongst those belonging to the
1140 * requesting task */
1141 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1145 /* instantiate the key and link it into a keyring */
1146 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1147 dest_keyring, instkey);
1149 key_put(dest_keyring);
1151 /* discard the assumed authority if it's just been disabled by
1152 * instantiation of the key */
1154 keyctl_change_reqkey_auth(NULL);
1158 memzero_explicit(payload, plen);
1166 * Instantiate a key with the specified payload and link the key into the
1167 * destination keyring if one is given.
1169 * The caller must have the appropriate instantiation permit set for this to
1170 * work (see keyctl_assume_authority). No other permissions are required.
1172 * If successful, 0 will be returned.
1174 long keyctl_instantiate_key(key_serial_t id,
1175 const void __user *_payload,
1177 key_serial_t ringid)
1179 if (_payload && plen) {
1181 struct iov_iter from;
1184 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1189 return keyctl_instantiate_key_common(id, &from, ringid);
1192 return keyctl_instantiate_key_common(id, NULL, ringid);
1196 * Instantiate a key with the specified multipart payload and link the key into
1197 * the destination keyring if one is given.
1199 * The caller must have the appropriate instantiation permit set for this to
1200 * work (see keyctl_assume_authority). No other permissions are required.
1202 * If successful, 0 will be returned.
1204 long keyctl_instantiate_key_iov(key_serial_t id,
1205 const struct iovec __user *_payload_iov,
1207 key_serial_t ringid)
1209 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1210 struct iov_iter from;
1216 ret = import_iovec(WRITE, _payload_iov, ioc,
1217 ARRAY_SIZE(iovstack), &iov, &from);
1220 ret = keyctl_instantiate_key_common(id, &from, ringid);
1226 * Negatively instantiate the key with the given timeout (in seconds) and link
1227 * the key into the destination keyring if one is given.
1229 * The caller must have the appropriate instantiation permit set for this to
1230 * work (see keyctl_assume_authority). No other permissions are required.
1232 * The key and any links to the key will be automatically garbage collected
1233 * after the timeout expires.
1235 * Negative keys are used to rate limit repeated request_key() calls by causing
1236 * them to return -ENOKEY until the negative key expires.
1238 * If successful, 0 will be returned.
1240 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1242 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1246 * Negatively instantiate the key with the given timeout (in seconds) and error
1247 * code and link the key into the destination keyring if one is given.
1249 * The caller must have the appropriate instantiation permit set for this to
1250 * work (see keyctl_assume_authority). No other permissions are required.
1252 * The key and any links to the key will be automatically garbage collected
1253 * after the timeout expires.
1255 * Negative keys are used to rate limit repeated request_key() calls by causing
1256 * them to return the specified error code until the negative key expires.
1258 * If successful, 0 will be returned.
1260 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1261 key_serial_t ringid)
1263 const struct cred *cred = current_cred();
1264 struct request_key_auth *rka;
1265 struct key *instkey, *dest_keyring;
1268 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1270 /* must be a valid error code and mustn't be a kernel special */
1272 error >= MAX_ERRNO ||
1273 error == ERESTARTSYS ||
1274 error == ERESTARTNOINTR ||
1275 error == ERESTARTNOHAND ||
1276 error == ERESTART_RESTARTBLOCK)
1279 /* the appropriate instantiation authorisation key must have been
1280 * assumed before calling this */
1282 instkey = cred->request_key_auth;
1286 rka = instkey->payload.data[0];
1287 if (rka->target_key->serial != id)
1290 /* find the destination keyring if present (which must also be
1292 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1296 /* instantiate the key and link it into a keyring */
1297 ret = key_reject_and_link(rka->target_key, timeout, error,
1298 dest_keyring, instkey);
1300 key_put(dest_keyring);
1302 /* discard the assumed authority if it's just been disabled by
1303 * instantiation of the key */
1305 keyctl_change_reqkey_auth(NULL);
1312 * Read or set the default keyring in which request_key() will cache keys and
1313 * return the old setting.
1315 * If a thread or process keyring is specified then it will be created if it
1316 * doesn't yet exist. The old setting will be returned if successful.
1318 long keyctl_set_reqkey_keyring(int reqkey_defl)
1321 int ret, old_setting;
1323 old_setting = current_cred_xxx(jit_keyring);
1325 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1328 new = prepare_creds();
1332 switch (reqkey_defl) {
1333 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1334 ret = install_thread_keyring_to_cred(new);
1339 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1340 ret = install_process_keyring_to_cred(new);
1345 case KEY_REQKEY_DEFL_DEFAULT:
1346 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1347 case KEY_REQKEY_DEFL_USER_KEYRING:
1348 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1349 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1352 case KEY_REQKEY_DEFL_NO_CHANGE:
1353 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1360 new->jit_keyring = reqkey_defl;
1369 * Set or clear the timeout on a key.
1371 * Either the key must grant the caller Setattr permission or else the caller
1372 * must hold an instantiation authorisation token for the key.
1374 * The timeout is either 0 to clear the timeout, or a number of seconds from
1375 * the current time. The key and any links to the key will be automatically
1376 * garbage collected after the timeout expires.
1378 * Keys with KEY_FLAG_KEEP set should not be timed out.
1380 * If successful, 0 is returned.
1382 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1384 struct key *key, *instkey;
1388 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1390 if (IS_ERR(key_ref)) {
1391 /* setting the timeout on a key under construction is permitted
1392 * if we have the authorisation token handy */
1393 if (PTR_ERR(key_ref) == -EACCES) {
1394 instkey = key_get_instantiation_authkey(id);
1395 if (!IS_ERR(instkey)) {
1397 key_ref = lookup_user_key(id,
1400 if (!IS_ERR(key_ref))
1405 ret = PTR_ERR(key_ref);
1410 key = key_ref_to_ptr(key_ref);
1412 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1415 key_set_timeout(key, timeout);
1423 * Assume (or clear) the authority to instantiate the specified key.
1425 * This sets the authoritative token currently in force for key instantiation.
1426 * This must be done for a key to be instantiated. It has the effect of making
1427 * available all the keys from the caller of the request_key() that created a
1428 * key to request_key() calls made by the caller of this function.
1430 * The caller must have the instantiation key in their process keyrings with a
1431 * Search permission grant available to the caller.
1433 * If the ID given is 0, then the setting will be cleared and 0 returned.
1435 * If the ID given has a matching an authorisation key, then that key will be
1436 * set and its ID will be returned. The authorisation key can be read to get
1437 * the callout information passed to request_key().
1439 long keyctl_assume_authority(key_serial_t id)
1441 struct key *authkey;
1444 /* special key IDs aren't permitted */
1449 /* we divest ourselves of authority if given an ID of 0 */
1451 ret = keyctl_change_reqkey_auth(NULL);
1455 /* attempt to assume the authority temporarily granted to us whilst we
1456 * instantiate the specified key
1457 * - the authorisation key must be in the current task's keyrings
1460 authkey = key_get_instantiation_authkey(id);
1461 if (IS_ERR(authkey)) {
1462 ret = PTR_ERR(authkey);
1466 ret = keyctl_change_reqkey_auth(authkey);
1468 ret = authkey->serial;
1475 * Get a key's the LSM security label.
1477 * The key must grant the caller View permission for this to work.
1479 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1481 * If successful, the amount of information available will be returned,
1482 * irrespective of how much was copied (including the terminal NUL).
1484 long keyctl_get_security(key_serial_t keyid,
1485 char __user *buffer,
1488 struct key *key, *instkey;
1493 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1494 if (IS_ERR(key_ref)) {
1495 if (PTR_ERR(key_ref) != -EACCES)
1496 return PTR_ERR(key_ref);
1498 /* viewing a key under construction is also permitted if we
1499 * have the authorisation token handy */
1500 instkey = key_get_instantiation_authkey(keyid);
1501 if (IS_ERR(instkey))
1502 return PTR_ERR(instkey);
1505 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1506 if (IS_ERR(key_ref))
1507 return PTR_ERR(key_ref);
1510 key = key_ref_to_ptr(key_ref);
1511 ret = security_key_getsecurity(key, &context);
1513 /* if no information was returned, give userspace an empty
1516 if (buffer && buflen > 0 &&
1517 copy_to_user(buffer, "", 1) != 0)
1519 } else if (ret > 0) {
1520 /* return as much data as there's room for */
1521 if (buffer && buflen > 0) {
1525 if (copy_to_user(buffer, context, buflen) != 0)
1532 key_ref_put(key_ref);
1537 * Attempt to install the calling process's session keyring on the process's
1540 * The keyring must exist and must grant the caller LINK permission, and the
1541 * parent process must be single-threaded and must have the same effective
1542 * ownership as this process and mustn't be SUID/SGID.
1544 * The keyring will be emplaced on the parent when it next resumes userspace.
1546 * If successful, 0 will be returned.
1548 long keyctl_session_to_parent(void)
1550 struct task_struct *me, *parent;
1551 const struct cred *mycred, *pcred;
1552 struct callback_head *newwork, *oldwork;
1553 key_ref_t keyring_r;
1557 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1558 if (IS_ERR(keyring_r))
1559 return PTR_ERR(keyring_r);
1563 /* our parent is going to need a new cred struct, a new tgcred struct
1564 * and new security data, so we allocate them here to prevent ENOMEM in
1566 cred = cred_alloc_blank();
1569 newwork = &cred->rcu;
1571 cred->session_keyring = key_ref_to_ptr(keyring_r);
1573 init_task_work(newwork, key_change_session_keyring);
1577 write_lock_irq(&tasklist_lock);
1581 parent = rcu_dereference_protected(me->real_parent,
1582 lockdep_is_held(&tasklist_lock));
1584 /* the parent mustn't be init and mustn't be a kernel thread */
1585 if (parent->pid <= 1 || !parent->mm)
1588 /* the parent must be single threaded */
1589 if (!thread_group_empty(parent))
1592 /* the parent and the child must have different session keyrings or
1593 * there's no point */
1594 mycred = current_cred();
1595 pcred = __task_cred(parent);
1596 if (mycred == pcred ||
1597 mycred->session_keyring == pcred->session_keyring) {
1602 /* the parent must have the same effective ownership and mustn't be
1604 if (!uid_eq(pcred->uid, mycred->euid) ||
1605 !uid_eq(pcred->euid, mycred->euid) ||
1606 !uid_eq(pcred->suid, mycred->euid) ||
1607 !gid_eq(pcred->gid, mycred->egid) ||
1608 !gid_eq(pcred->egid, mycred->egid) ||
1609 !gid_eq(pcred->sgid, mycred->egid))
1612 /* the keyrings must have the same UID */
1613 if ((pcred->session_keyring &&
1614 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1615 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1618 /* cancel an already pending keyring replacement */
1619 oldwork = task_work_cancel(parent, key_change_session_keyring);
1621 /* the replacement session keyring is applied just prior to userspace
1623 ret = task_work_add(parent, newwork, true);
1627 write_unlock_irq(&tasklist_lock);
1630 put_cred(container_of(oldwork, struct cred, rcu));
1636 key_ref_put(keyring_r);
1641 * Apply a restriction to a given keyring.
1643 * The caller must have Setattr permission to change keyring restrictions.
1645 * The requested type name may be a NULL pointer to reject all attempts
1646 * to link to the keyring. In this case, _restriction must also be NULL.
1647 * Otherwise, both _type and _restriction must be non-NULL.
1649 * Returns 0 if successful.
1651 long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1652 const char __user *_restriction)
1656 char *restriction = NULL;
1659 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
1660 if (IS_ERR(key_ref))
1661 return PTR_ERR(key_ref);
1668 ret = key_get_type_from_user(type, _type, sizeof(type));
1672 restriction = strndup_user(_restriction, PAGE_SIZE);
1673 if (IS_ERR(restriction)) {
1674 ret = PTR_ERR(restriction);
1682 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
1685 key_ref_put(key_ref);
1690 * Get keyrings subsystem capabilities.
1692 long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
1694 size_t size = buflen;
1697 if (size > sizeof(keyrings_capabilities))
1698 size = sizeof(keyrings_capabilities);
1699 if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
1701 if (size < buflen &&
1702 clear_user(_buffer + size, buflen - size) != 0)
1706 return sizeof(keyrings_capabilities);
1710 * The key control system call
1712 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1713 unsigned long, arg4, unsigned long, arg5)
1716 case KEYCTL_GET_KEYRING_ID:
1717 return keyctl_get_keyring_ID((key_serial_t) arg2,
1720 case KEYCTL_JOIN_SESSION_KEYRING:
1721 return keyctl_join_session_keyring((const char __user *) arg2);
1724 return keyctl_update_key((key_serial_t) arg2,
1725 (const void __user *) arg3,
1729 return keyctl_revoke_key((key_serial_t) arg2);
1731 case KEYCTL_DESCRIBE:
1732 return keyctl_describe_key((key_serial_t) arg2,
1733 (char __user *) arg3,
1737 return keyctl_keyring_clear((key_serial_t) arg2);
1740 return keyctl_keyring_link((key_serial_t) arg2,
1741 (key_serial_t) arg3);
1744 return keyctl_keyring_unlink((key_serial_t) arg2,
1745 (key_serial_t) arg3);
1748 return keyctl_keyring_search((key_serial_t) arg2,
1749 (const char __user *) arg3,
1750 (const char __user *) arg4,
1751 (key_serial_t) arg5);
1754 return keyctl_read_key((key_serial_t) arg2,
1755 (char __user *) arg3,
1759 return keyctl_chown_key((key_serial_t) arg2,
1763 case KEYCTL_SETPERM:
1764 return keyctl_setperm_key((key_serial_t) arg2,
1767 case KEYCTL_INSTANTIATE:
1768 return keyctl_instantiate_key((key_serial_t) arg2,
1769 (const void __user *) arg3,
1771 (key_serial_t) arg5);
1774 return keyctl_negate_key((key_serial_t) arg2,
1776 (key_serial_t) arg4);
1778 case KEYCTL_SET_REQKEY_KEYRING:
1779 return keyctl_set_reqkey_keyring(arg2);
1781 case KEYCTL_SET_TIMEOUT:
1782 return keyctl_set_timeout((key_serial_t) arg2,
1785 case KEYCTL_ASSUME_AUTHORITY:
1786 return keyctl_assume_authority((key_serial_t) arg2);
1788 case KEYCTL_GET_SECURITY:
1789 return keyctl_get_security((key_serial_t) arg2,
1790 (char __user *) arg3,
1793 case KEYCTL_SESSION_TO_PARENT:
1794 return keyctl_session_to_parent();
1797 return keyctl_reject_key((key_serial_t) arg2,
1800 (key_serial_t) arg5);
1802 case KEYCTL_INSTANTIATE_IOV:
1803 return keyctl_instantiate_key_iov(
1804 (key_serial_t) arg2,
1805 (const struct iovec __user *) arg3,
1807 (key_serial_t) arg5);
1809 case KEYCTL_INVALIDATE:
1810 return keyctl_invalidate_key((key_serial_t) arg2);
1812 case KEYCTL_GET_PERSISTENT:
1813 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1815 case KEYCTL_DH_COMPUTE:
1816 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
1817 (char __user *) arg3, (size_t) arg4,
1818 (struct keyctl_kdf_params __user *) arg5);
1820 case KEYCTL_RESTRICT_KEYRING:
1821 return keyctl_restrict_keyring((key_serial_t) arg2,
1822 (const char __user *) arg3,
1823 (const char __user *) arg4);
1825 case KEYCTL_PKEY_QUERY:
1828 return keyctl_pkey_query((key_serial_t)arg2,
1829 (const char __user *)arg4,
1830 (struct keyctl_pkey_query __user *)arg5);
1832 case KEYCTL_PKEY_ENCRYPT:
1833 case KEYCTL_PKEY_DECRYPT:
1834 case KEYCTL_PKEY_SIGN:
1835 return keyctl_pkey_e_d_s(
1837 (const struct keyctl_pkey_params __user *)arg2,
1838 (const char __user *)arg3,
1839 (const void __user *)arg4,
1840 (void __user *)arg5);
1842 case KEYCTL_PKEY_VERIFY:
1843 return keyctl_pkey_verify(
1844 (const struct keyctl_pkey_params __user *)arg2,
1845 (const char __user *)arg3,
1846 (const void __user *)arg4,
1847 (const void __user *)arg5);
1850 return keyctl_keyring_move((key_serial_t)arg2,
1853 (unsigned int)arg5);
1855 case KEYCTL_CAPABILITIES:
1856 return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);