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>
30 #define KEY_MAX_DESC_SIZE 4096
32 static int key_get_type_from_user(char *type,
33 const char __user *_type,
38 ret = strncpy_from_user(type, _type, len);
41 if (ret == 0 || ret >= len)
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.
53 * If the description is NULL or an empty string, the key type is asked to
54 * generate one from the payload.
56 * The keyring must be writable so that we can attach the key to it.
58 * If successful, the new key's serial number is returned, otherwise an error
61 SYSCALL_DEFINE5(add_key, const char __user *, _type,
62 const char __user *, _description,
63 const void __user *, _payload,
67 key_ref_t keyring_ref, key_ref;
68 char type[32], *description;
73 if (plen > 1024 * 1024 - 1)
76 /* draw all the data into kernel space */
77 ret = key_get_type_from_user(type, _type, sizeof(type));
83 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
84 if (IS_ERR(description)) {
85 ret = PTR_ERR(description);
91 } else if ((description[0] == '.') &&
92 (strncmp(type, "keyring", 7) == 0)) {
98 /* pull the payload in if one was supplied */
103 payload = kvmalloc(plen, GFP_KERNEL);
108 if (copy_from_user(payload, _payload, plen) != 0)
112 /* find the target keyring (which must be writable) */
113 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
114 if (IS_ERR(keyring_ref)) {
115 ret = PTR_ERR(keyring_ref);
119 /* create or update the requested key and add it to the target
121 key_ref = key_create_or_update(keyring_ref, type, description,
122 payload, plen, KEY_PERM_UNDEF,
124 if (!IS_ERR(key_ref)) {
125 ret = key_ref_to_ptr(key_ref)->serial;
126 key_ref_put(key_ref);
129 ret = PTR_ERR(key_ref);
132 key_ref_put(keyring_ref);
135 memzero_explicit(payload, plen);
145 * Search the process keyrings and keyring trees linked from those for a
146 * matching key. Keyrings must have appropriate Search permission to be
149 * If a key is found, it will be attached to the destination keyring if there's
150 * one specified and the serial number of the key will be returned.
152 * If no key is found, /sbin/request-key will be invoked if _callout_info is
153 * non-NULL in an attempt to create a key. The _callout_info string will be
154 * passed to /sbin/request-key to aid with completing the request. If the
155 * _callout_info string is "" then it will be changed to "-".
157 SYSCALL_DEFINE4(request_key, const char __user *, _type,
158 const char __user *, _description,
159 const char __user *, _callout_info,
160 key_serial_t, destringid)
162 struct key_type *ktype;
166 char type[32], *description, *callout_info;
169 /* pull the type into kernel space */
170 ret = key_get_type_from_user(type, _type, sizeof(type));
174 /* pull the description into kernel space */
175 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
176 if (IS_ERR(description)) {
177 ret = PTR_ERR(description);
181 /* pull the callout info into kernel space */
185 callout_info = strndup_user(_callout_info, PAGE_SIZE);
186 if (IS_ERR(callout_info)) {
187 ret = PTR_ERR(callout_info);
190 callout_len = strlen(callout_info);
193 /* get the destination keyring if specified */
196 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
198 if (IS_ERR(dest_ref)) {
199 ret = PTR_ERR(dest_ref);
204 /* find the key type */
205 ktype = key_type_lookup(type);
207 ret = PTR_ERR(ktype);
212 key = request_key_and_link(ktype, description, callout_info,
213 callout_len, NULL, key_ref_to_ptr(dest_ref),
220 /* wait for the key to finish being constructed */
221 ret = wait_for_key_construction(key, 1);
232 key_ref_put(dest_ref);
242 * Get the ID of the specified process keyring.
244 * The requested keyring must have search permission to be found.
246 * If successful, the ID of the requested keyring will be returned.
248 long keyctl_get_keyring_ID(key_serial_t id, int create)
251 unsigned long lflags;
254 lflags = create ? KEY_LOOKUP_CREATE : 0;
255 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
256 if (IS_ERR(key_ref)) {
257 ret = PTR_ERR(key_ref);
261 ret = key_ref_to_ptr(key_ref)->serial;
262 key_ref_put(key_ref);
268 * Join a (named) session keyring.
270 * Create and join an anonymous session keyring or join a named session
271 * keyring, creating it if necessary. A named session keyring must have Search
272 * permission for it to be joined. Session keyrings without this permit will
273 * be skipped over. It is not permitted for userspace to create or join
274 * keyrings whose name begin with a dot.
276 * If successful, the ID of the joined session keyring will be returned.
278 long keyctl_join_session_keyring(const char __user *_name)
283 /* fetch the name from userspace */
286 name = strndup_user(_name, KEY_MAX_DESC_SIZE);
297 /* join the session */
298 ret = join_session_keyring(name);
306 * Update a key's data payload from the given data.
308 * The key must grant the caller Write permission and the key type must support
309 * updating for this to work. A negative key can be positively instantiated
312 * If successful, 0 will be returned. If the key type does not support
313 * updating, then -EOPNOTSUPP will be returned.
315 long keyctl_update_key(key_serial_t id,
316 const void __user *_payload,
324 if (plen > PAGE_SIZE)
327 /* pull the payload in if one was supplied */
331 payload = kmalloc(plen, GFP_KERNEL);
336 if (copy_from_user(payload, _payload, plen) != 0)
340 /* find the target key (which must be writable) */
341 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
342 if (IS_ERR(key_ref)) {
343 ret = PTR_ERR(key_ref);
348 ret = key_update(key_ref, payload, plen);
350 key_ref_put(key_ref);
360 * The key must be grant the caller Write or Setattr permission for this to
361 * work. The key type should give up its quota claim when revoked. The key
362 * and any links to the key will be automatically garbage collected after a
363 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
365 * Keys with KEY_FLAG_KEEP set should not be revoked.
367 * If successful, 0 is returned.
369 long keyctl_revoke_key(key_serial_t id)
375 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
376 if (IS_ERR(key_ref)) {
377 ret = PTR_ERR(key_ref);
380 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
381 if (IS_ERR(key_ref)) {
382 ret = PTR_ERR(key_ref);
387 key = key_ref_to_ptr(key_ref);
389 if (test_bit(KEY_FLAG_KEEP, &key->flags))
394 key_ref_put(key_ref);
402 * The key must be grant the caller Invalidate permission for this to work.
403 * The key and any links to the key will be automatically garbage collected
406 * Keys with KEY_FLAG_KEEP set should not be invalidated.
408 * If successful, 0 is returned.
410 long keyctl_invalidate_key(key_serial_t id)
418 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
419 if (IS_ERR(key_ref)) {
420 ret = PTR_ERR(key_ref);
422 /* Root is permitted to invalidate certain special keys */
423 if (capable(CAP_SYS_ADMIN)) {
424 key_ref = lookup_user_key(id, 0, 0);
427 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
428 &key_ref_to_ptr(key_ref)->flags))
437 key = key_ref_to_ptr(key_ref);
439 if (test_bit(KEY_FLAG_KEEP, &key->flags))
444 key_ref_put(key_ref);
446 kleave(" = %ld", ret);
451 * Clear the specified keyring, creating an empty process keyring if one of the
452 * special keyring IDs is used.
454 * The keyring must grant the caller Write permission and not have
455 * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
457 long keyctl_keyring_clear(key_serial_t ringid)
459 key_ref_t keyring_ref;
463 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
464 if (IS_ERR(keyring_ref)) {
465 ret = PTR_ERR(keyring_ref);
467 /* Root is permitted to invalidate certain special keyrings */
468 if (capable(CAP_SYS_ADMIN)) {
469 keyring_ref = lookup_user_key(ringid, 0, 0);
470 if (IS_ERR(keyring_ref))
472 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
473 &key_ref_to_ptr(keyring_ref)->flags))
482 keyring = key_ref_to_ptr(keyring_ref);
483 if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
486 ret = keyring_clear(keyring);
488 key_ref_put(keyring_ref);
494 * Create a link from a keyring to a key if there's no matching key in the
495 * keyring, otherwise replace the link to the matching key with a link to the
498 * The key must grant the caller Link permission and the the keyring must grant
499 * the caller Write permission. Furthermore, if an additional link is created,
500 * the keyring's quota will be extended.
502 * If successful, 0 will be returned.
504 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
506 key_ref_t keyring_ref, key_ref;
509 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
510 if (IS_ERR(keyring_ref)) {
511 ret = PTR_ERR(keyring_ref);
515 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
516 if (IS_ERR(key_ref)) {
517 ret = PTR_ERR(key_ref);
521 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
523 key_ref_put(key_ref);
525 key_ref_put(keyring_ref);
531 * Unlink a key from a keyring.
533 * The keyring must grant the caller Write permission for this to work; the key
534 * itself need not grant the caller anything. If the last link to a key is
535 * removed then that key will be scheduled for destruction.
537 * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
539 * If successful, 0 will be returned.
541 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
543 key_ref_t keyring_ref, key_ref;
544 struct key *keyring, *key;
547 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
548 if (IS_ERR(keyring_ref)) {
549 ret = PTR_ERR(keyring_ref);
553 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
554 if (IS_ERR(key_ref)) {
555 ret = PTR_ERR(key_ref);
559 keyring = key_ref_to_ptr(keyring_ref);
560 key = key_ref_to_ptr(key_ref);
561 if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
562 test_bit(KEY_FLAG_KEEP, &key->flags))
565 ret = key_unlink(keyring, key);
567 key_ref_put(key_ref);
569 key_ref_put(keyring_ref);
575 * Return a description of a key to userspace.
577 * The key must grant the caller View permission for this to work.
579 * If there's a buffer, we place up to buflen bytes of data into it formatted
580 * in the following way:
582 * type;uid;gid;perm;description<NUL>
584 * If successful, we return the amount of description available, irrespective
585 * of how much we may have copied into the buffer.
587 long keyctl_describe_key(key_serial_t keyid,
591 struct key *key, *instkey;
595 int desclen, infolen;
597 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
598 if (IS_ERR(key_ref)) {
599 /* viewing a key under construction is permitted if we have the
600 * authorisation token handy */
601 if (PTR_ERR(key_ref) == -EACCES) {
602 instkey = key_get_instantiation_authkey(keyid);
603 if (!IS_ERR(instkey)) {
605 key_ref = lookup_user_key(keyid,
608 if (!IS_ERR(key_ref))
613 ret = PTR_ERR(key_ref);
618 key = key_ref_to_ptr(key_ref);
619 desclen = strlen(key->description);
621 /* calculate how much information we're going to return */
623 infobuf = kasprintf(GFP_KERNEL,
626 from_kuid_munged(current_user_ns(), key->uid),
627 from_kgid_munged(current_user_ns(), key->gid),
631 infolen = strlen(infobuf);
632 ret = infolen + desclen + 1;
634 /* consider returning the data */
635 if (buffer && buflen >= ret) {
636 if (copy_to_user(buffer, infobuf, infolen) != 0 ||
637 copy_to_user(buffer + infolen, key->description,
644 key_ref_put(key_ref);
650 * Search the specified keyring and any keyrings it links to for a matching
651 * key. Only keyrings that grant the caller Search permission will be searched
652 * (this includes the starting keyring). Only keys with Search permission can
655 * If successful, the found key will be linked to the destination keyring if
656 * supplied and the key has Link permission, and the found key ID will be
659 long keyctl_keyring_search(key_serial_t ringid,
660 const char __user *_type,
661 const char __user *_description,
662 key_serial_t destringid)
664 struct key_type *ktype;
665 key_ref_t keyring_ref, key_ref, dest_ref;
666 char type[32], *description;
669 /* pull the type and description into kernel space */
670 ret = key_get_type_from_user(type, _type, sizeof(type));
674 description = strndup_user(_description, KEY_MAX_DESC_SIZE);
675 if (IS_ERR(description)) {
676 ret = PTR_ERR(description);
680 /* get the keyring at which to begin the search */
681 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
682 if (IS_ERR(keyring_ref)) {
683 ret = PTR_ERR(keyring_ref);
687 /* get the destination keyring if specified */
690 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
692 if (IS_ERR(dest_ref)) {
693 ret = PTR_ERR(dest_ref);
698 /* find the key type */
699 ktype = key_type_lookup(type);
701 ret = PTR_ERR(ktype);
706 key_ref = keyring_search(keyring_ref, ktype, description);
707 if (IS_ERR(key_ref)) {
708 ret = PTR_ERR(key_ref);
710 /* treat lack or presence of a negative key the same */
716 /* link the resulting key to the destination keyring if we can */
718 ret = key_permission(key_ref, KEY_NEED_LINK);
722 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
727 ret = key_ref_to_ptr(key_ref)->serial;
730 key_ref_put(key_ref);
734 key_ref_put(dest_ref);
736 key_ref_put(keyring_ref);
744 * Read a key's payload.
746 * The key must either grant the caller Read permission, or it must grant the
747 * caller Search permission when searched for from the process keyrings.
749 * If successful, we place up to buflen bytes of data into the buffer, if one
750 * is provided, and return the amount of data that is available in the key,
751 * irrespective of how much we copied into the buffer.
753 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
759 /* find the key first */
760 key_ref = lookup_user_key(keyid, 0, 0);
761 if (IS_ERR(key_ref)) {
766 key = key_ref_to_ptr(key_ref);
768 ret = key_read_state(key);
770 goto error2; /* Negatively instantiated */
772 /* see if we can read it directly */
773 ret = key_permission(key_ref, KEY_NEED_READ);
779 /* we can't; see if it's searchable from this process's keyrings
780 * - we automatically take account of the fact that it may be
781 * dangling off an instantiation key
783 if (!is_key_possessed(key_ref)) {
788 /* the key is probably readable - now try to read it */
791 if (key->type->read) {
792 /* Read the data with the semaphore held (since we might sleep)
793 * to protect against the key being updated or revoked.
795 down_read(&key->sem);
796 ret = key_validate(key);
798 ret = key->type->read(key, buffer, buflen);
809 * Change the ownership of a key
811 * The key must grant the caller Setattr permission for this to work, though
812 * the key need not be fully instantiated yet. For the UID to be changed, or
813 * for the GID to be changed to a group the caller is not a member of, the
814 * caller must have sysadmin capability. If either uid or gid is -1 then that
815 * attribute is not changed.
817 * If the UID is to be changed, the new user must have sufficient quota to
818 * accept the key. The quota deduction will be removed from the old user to
819 * the new user should the attribute be changed.
821 * If successful, 0 will be returned.
823 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
825 struct key_user *newowner, *zapowner = NULL;
832 uid = make_kuid(current_user_ns(), user);
833 gid = make_kgid(current_user_ns(), group);
835 if ((user != (uid_t) -1) && !uid_valid(uid))
837 if ((group != (gid_t) -1) && !gid_valid(gid))
841 if (user == (uid_t) -1 && group == (gid_t) -1)
844 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
846 if (IS_ERR(key_ref)) {
847 ret = PTR_ERR(key_ref);
851 key = key_ref_to_ptr(key_ref);
853 /* make the changes with the locks held to prevent chown/chown races */
855 down_write(&key->sem);
857 if (!capable(CAP_SYS_ADMIN)) {
858 /* only the sysadmin can chown a key to some other UID */
859 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
862 /* only the sysadmin can set the key's GID to a group other
863 * than one of those that the current process subscribes to */
864 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
869 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
871 newowner = key_user_lookup(uid);
875 /* transfer the quota burden to the new user */
876 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
877 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
878 key_quota_root_maxkeys : key_quota_maxkeys;
879 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
880 key_quota_root_maxbytes : key_quota_maxbytes;
882 spin_lock(&newowner->lock);
883 if (newowner->qnkeys + 1 >= maxkeys ||
884 newowner->qnbytes + key->quotalen >= maxbytes ||
885 newowner->qnbytes + key->quotalen <
890 newowner->qnbytes += key->quotalen;
891 spin_unlock(&newowner->lock);
893 spin_lock(&key->user->lock);
895 key->user->qnbytes -= key->quotalen;
896 spin_unlock(&key->user->lock);
899 atomic_dec(&key->user->nkeys);
900 atomic_inc(&newowner->nkeys);
902 if (key->state != KEY_IS_UNINSTANTIATED) {
903 atomic_dec(&key->user->nikeys);
904 atomic_inc(&newowner->nikeys);
907 zapowner = key->user;
908 key->user = newowner;
913 if (group != (gid_t) -1)
922 key_user_put(zapowner);
927 spin_unlock(&newowner->lock);
934 * Change the permission mask on a key.
936 * The key must grant the caller Setattr permission for this to work, though
937 * the key need not be fully instantiated yet. If the caller does not have
938 * sysadmin capability, it may only change the permission on keys that it owns.
940 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
947 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
950 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
952 if (IS_ERR(key_ref)) {
953 ret = PTR_ERR(key_ref);
957 key = key_ref_to_ptr(key_ref);
959 /* make the changes with the locks held to prevent chown/chmod races */
961 down_write(&key->sem);
963 /* if we're not the sysadmin, we can only change a key that we own */
964 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
976 * Get the destination keyring for instantiation and check that the caller has
977 * Write permission on it.
979 static long get_instantiation_keyring(key_serial_t ringid,
980 struct request_key_auth *rka,
981 struct key **_dest_keyring)
985 *_dest_keyring = NULL;
987 /* just return a NULL pointer if we weren't asked to make a link */
991 /* if a specific keyring is nominated by ID, then use that */
993 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
995 return PTR_ERR(dkref);
996 *_dest_keyring = key_ref_to_ptr(dkref);
1000 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
1003 /* otherwise specify the destination keyring recorded in the
1004 * authorisation key (any KEY_SPEC_*_KEYRING) */
1005 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
1006 *_dest_keyring = key_get(rka->dest_keyring);
1014 * Change the request_key authorisation key on the current process.
1016 static int keyctl_change_reqkey_auth(struct key *key)
1020 new = prepare_creds();
1024 key_put(new->request_key_auth);
1025 new->request_key_auth = key_get(key);
1027 return commit_creds(new);
1031 * Instantiate a key with the specified payload and link the key into the
1032 * destination keyring if one is given.
1034 * The caller must have the appropriate instantiation permit set for this to
1035 * work (see keyctl_assume_authority). No other permissions are required.
1037 * If successful, 0 will be returned.
1039 long keyctl_instantiate_key_common(key_serial_t id,
1040 struct iov_iter *from,
1041 key_serial_t ringid)
1043 const struct cred *cred = current_cred();
1044 struct request_key_auth *rka;
1045 struct key *instkey, *dest_keyring;
1046 size_t plen = from ? iov_iter_count(from) : 0;
1050 kenter("%d,,%zu,%d", id, plen, ringid);
1056 if (plen > 1024 * 1024 - 1)
1059 /* the appropriate instantiation authorisation key must have been
1060 * assumed before calling this */
1062 instkey = cred->request_key_auth;
1066 rka = instkey->payload.data[0];
1067 if (rka->target_key->serial != id)
1070 /* pull the payload in if one was supplied */
1075 payload = kvmalloc(plen, GFP_KERNEL);
1080 if (!copy_from_iter_full(payload, plen, from))
1084 /* find the destination keyring amongst those belonging to the
1085 * requesting task */
1086 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1090 /* instantiate the key and link it into a keyring */
1091 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1092 dest_keyring, instkey);
1094 key_put(dest_keyring);
1096 /* discard the assumed authority if it's just been disabled by
1097 * instantiation of the key */
1099 keyctl_change_reqkey_auth(NULL);
1103 memzero_explicit(payload, plen);
1111 * Instantiate a key with the specified payload and link the key into the
1112 * destination keyring if one is given.
1114 * The caller must have the appropriate instantiation permit set for this to
1115 * work (see keyctl_assume_authority). No other permissions are required.
1117 * If successful, 0 will be returned.
1119 long keyctl_instantiate_key(key_serial_t id,
1120 const void __user *_payload,
1122 key_serial_t ringid)
1124 if (_payload && plen) {
1126 struct iov_iter from;
1129 ret = import_single_range(WRITE, (void __user *)_payload, plen,
1134 return keyctl_instantiate_key_common(id, &from, ringid);
1137 return keyctl_instantiate_key_common(id, NULL, ringid);
1141 * Instantiate a key with the specified multipart payload and link the key into
1142 * the destination keyring if one is given.
1144 * The caller must have the appropriate instantiation permit set for this to
1145 * work (see keyctl_assume_authority). No other permissions are required.
1147 * If successful, 0 will be returned.
1149 long keyctl_instantiate_key_iov(key_serial_t id,
1150 const struct iovec __user *_payload_iov,
1152 key_serial_t ringid)
1154 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1155 struct iov_iter from;
1161 ret = import_iovec(WRITE, _payload_iov, ioc,
1162 ARRAY_SIZE(iovstack), &iov, &from);
1165 ret = keyctl_instantiate_key_common(id, &from, ringid);
1171 * Negatively instantiate the key with the given timeout (in seconds) and link
1172 * the key into the destination keyring if one is given.
1174 * The caller must have the appropriate instantiation permit set for this to
1175 * work (see keyctl_assume_authority). No other permissions are required.
1177 * The key and any links to the key will be automatically garbage collected
1178 * after the timeout expires.
1180 * Negative keys are used to rate limit repeated request_key() calls by causing
1181 * them to return -ENOKEY until the negative key expires.
1183 * If successful, 0 will be returned.
1185 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1187 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1191 * Negatively instantiate the key with the given timeout (in seconds) and error
1192 * code and link the key into the destination keyring if one is given.
1194 * The caller must have the appropriate instantiation permit set for this to
1195 * work (see keyctl_assume_authority). No other permissions are required.
1197 * The key and any links to the key will be automatically garbage collected
1198 * after the timeout expires.
1200 * Negative keys are used to rate limit repeated request_key() calls by causing
1201 * them to return the specified error code until the negative key expires.
1203 * If successful, 0 will be returned.
1205 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1206 key_serial_t ringid)
1208 const struct cred *cred = current_cred();
1209 struct request_key_auth *rka;
1210 struct key *instkey, *dest_keyring;
1213 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1215 /* must be a valid error code and mustn't be a kernel special */
1217 error >= MAX_ERRNO ||
1218 error == ERESTARTSYS ||
1219 error == ERESTARTNOINTR ||
1220 error == ERESTARTNOHAND ||
1221 error == ERESTART_RESTARTBLOCK)
1224 /* the appropriate instantiation authorisation key must have been
1225 * assumed before calling this */
1227 instkey = cred->request_key_auth;
1231 rka = instkey->payload.data[0];
1232 if (rka->target_key->serial != id)
1235 /* find the destination keyring if present (which must also be
1237 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1241 /* instantiate the key and link it into a keyring */
1242 ret = key_reject_and_link(rka->target_key, timeout, error,
1243 dest_keyring, instkey);
1245 key_put(dest_keyring);
1247 /* discard the assumed authority if it's just been disabled by
1248 * instantiation of the key */
1250 keyctl_change_reqkey_auth(NULL);
1257 * Read or set the default keyring in which request_key() will cache keys and
1258 * return the old setting.
1260 * If a thread or process keyring is specified then it will be created if it
1261 * doesn't yet exist. The old setting will be returned if successful.
1263 long keyctl_set_reqkey_keyring(int reqkey_defl)
1266 int ret, old_setting;
1268 old_setting = current_cred_xxx(jit_keyring);
1270 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1273 new = prepare_creds();
1277 switch (reqkey_defl) {
1278 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1279 ret = install_thread_keyring_to_cred(new);
1284 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1285 ret = install_process_keyring_to_cred(new);
1290 case KEY_REQKEY_DEFL_DEFAULT:
1291 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1292 case KEY_REQKEY_DEFL_USER_KEYRING:
1293 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1294 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1297 case KEY_REQKEY_DEFL_NO_CHANGE:
1298 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1305 new->jit_keyring = reqkey_defl;
1314 * Set or clear the timeout on a key.
1316 * Either the key must grant the caller Setattr permission or else the caller
1317 * must hold an instantiation authorisation token for the key.
1319 * The timeout is either 0 to clear the timeout, or a number of seconds from
1320 * the current time. The key and any links to the key will be automatically
1321 * garbage collected after the timeout expires.
1323 * Keys with KEY_FLAG_KEEP set should not be timed out.
1325 * If successful, 0 is returned.
1327 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1329 struct key *key, *instkey;
1333 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1335 if (IS_ERR(key_ref)) {
1336 /* setting the timeout on a key under construction is permitted
1337 * if we have the authorisation token handy */
1338 if (PTR_ERR(key_ref) == -EACCES) {
1339 instkey = key_get_instantiation_authkey(id);
1340 if (!IS_ERR(instkey)) {
1342 key_ref = lookup_user_key(id,
1345 if (!IS_ERR(key_ref))
1350 ret = PTR_ERR(key_ref);
1355 key = key_ref_to_ptr(key_ref);
1357 if (test_bit(KEY_FLAG_KEEP, &key->flags))
1360 key_set_timeout(key, timeout);
1368 * Assume (or clear) the authority to instantiate the specified key.
1370 * This sets the authoritative token currently in force for key instantiation.
1371 * This must be done for a key to be instantiated. It has the effect of making
1372 * available all the keys from the caller of the request_key() that created a
1373 * key to request_key() calls made by the caller of this function.
1375 * The caller must have the instantiation key in their process keyrings with a
1376 * Search permission grant available to the caller.
1378 * If the ID given is 0, then the setting will be cleared and 0 returned.
1380 * If the ID given has a matching an authorisation key, then that key will be
1381 * set and its ID will be returned. The authorisation key can be read to get
1382 * the callout information passed to request_key().
1384 long keyctl_assume_authority(key_serial_t id)
1386 struct key *authkey;
1389 /* special key IDs aren't permitted */
1394 /* we divest ourselves of authority if given an ID of 0 */
1396 ret = keyctl_change_reqkey_auth(NULL);
1400 /* attempt to assume the authority temporarily granted to us whilst we
1401 * instantiate the specified key
1402 * - the authorisation key must be in the current task's keyrings
1405 authkey = key_get_instantiation_authkey(id);
1406 if (IS_ERR(authkey)) {
1407 ret = PTR_ERR(authkey);
1411 ret = keyctl_change_reqkey_auth(authkey);
1413 ret = authkey->serial;
1420 * Get a key's the LSM security label.
1422 * The key must grant the caller View permission for this to work.
1424 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1426 * If successful, the amount of information available will be returned,
1427 * irrespective of how much was copied (including the terminal NUL).
1429 long keyctl_get_security(key_serial_t keyid,
1430 char __user *buffer,
1433 struct key *key, *instkey;
1438 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1439 if (IS_ERR(key_ref)) {
1440 if (PTR_ERR(key_ref) != -EACCES)
1441 return PTR_ERR(key_ref);
1443 /* viewing a key under construction is also permitted if we
1444 * have the authorisation token handy */
1445 instkey = key_get_instantiation_authkey(keyid);
1446 if (IS_ERR(instkey))
1447 return PTR_ERR(instkey);
1450 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1451 if (IS_ERR(key_ref))
1452 return PTR_ERR(key_ref);
1455 key = key_ref_to_ptr(key_ref);
1456 ret = security_key_getsecurity(key, &context);
1458 /* if no information was returned, give userspace an empty
1461 if (buffer && buflen > 0 &&
1462 copy_to_user(buffer, "", 1) != 0)
1464 } else if (ret > 0) {
1465 /* return as much data as there's room for */
1466 if (buffer && buflen > 0) {
1470 if (copy_to_user(buffer, context, buflen) != 0)
1477 key_ref_put(key_ref);
1482 * Attempt to install the calling process's session keyring on the process's
1485 * The keyring must exist and must grant the caller LINK permission, and the
1486 * parent process must be single-threaded and must have the same effective
1487 * ownership as this process and mustn't be SUID/SGID.
1489 * The keyring will be emplaced on the parent when it next resumes userspace.
1491 * If successful, 0 will be returned.
1493 long keyctl_session_to_parent(void)
1495 struct task_struct *me, *parent;
1496 const struct cred *mycred, *pcred;
1497 struct callback_head *newwork, *oldwork;
1498 key_ref_t keyring_r;
1502 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1503 if (IS_ERR(keyring_r))
1504 return PTR_ERR(keyring_r);
1508 /* our parent is going to need a new cred struct, a new tgcred struct
1509 * and new security data, so we allocate them here to prevent ENOMEM in
1511 cred = cred_alloc_blank();
1514 newwork = &cred->rcu;
1516 cred->session_keyring = key_ref_to_ptr(keyring_r);
1518 init_task_work(newwork, key_change_session_keyring);
1522 write_lock_irq(&tasklist_lock);
1526 parent = me->real_parent;
1528 /* the parent mustn't be init and mustn't be a kernel thread */
1529 if (parent->pid <= 1 || !parent->mm)
1532 /* the parent must be single threaded */
1533 if (!thread_group_empty(parent))
1536 /* the parent and the child must have different session keyrings or
1537 * there's no point */
1538 mycred = current_cred();
1539 pcred = __task_cred(parent);
1540 if (mycred == pcred ||
1541 mycred->session_keyring == pcred->session_keyring) {
1546 /* the parent must have the same effective ownership and mustn't be
1548 if (!uid_eq(pcred->uid, mycred->euid) ||
1549 !uid_eq(pcred->euid, mycred->euid) ||
1550 !uid_eq(pcred->suid, mycred->euid) ||
1551 !gid_eq(pcred->gid, mycred->egid) ||
1552 !gid_eq(pcred->egid, mycred->egid) ||
1553 !gid_eq(pcred->sgid, mycred->egid))
1556 /* the keyrings must have the same UID */
1557 if ((pcred->session_keyring &&
1558 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1559 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1562 /* cancel an already pending keyring replacement */
1563 oldwork = task_work_cancel(parent, key_change_session_keyring);
1565 /* the replacement session keyring is applied just prior to userspace
1567 ret = task_work_add(parent, newwork, true);
1571 write_unlock_irq(&tasklist_lock);
1574 put_cred(container_of(oldwork, struct cred, rcu));
1580 key_ref_put(keyring_r);
1585 * Apply a restriction to a given keyring.
1587 * The caller must have Setattr permission to change keyring restrictions.
1589 * The requested type name may be a NULL pointer to reject all attempts
1590 * to link to the keyring. In this case, _restriction must also be NULL.
1591 * Otherwise, both _type and _restriction must be non-NULL.
1593 * Returns 0 if successful.
1595 long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
1596 const char __user *_restriction)
1600 char *restriction = NULL;
1603 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
1604 if (IS_ERR(key_ref))
1605 return PTR_ERR(key_ref);
1612 ret = key_get_type_from_user(type, _type, sizeof(type));
1616 restriction = strndup_user(_restriction, PAGE_SIZE);
1617 if (IS_ERR(restriction)) {
1618 ret = PTR_ERR(restriction);
1626 ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
1629 key_ref_put(key_ref);
1634 * The key control system call
1636 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1637 unsigned long, arg4, unsigned long, arg5)
1640 case KEYCTL_GET_KEYRING_ID:
1641 return keyctl_get_keyring_ID((key_serial_t) arg2,
1644 case KEYCTL_JOIN_SESSION_KEYRING:
1645 return keyctl_join_session_keyring((const char __user *) arg2);
1648 return keyctl_update_key((key_serial_t) arg2,
1649 (const void __user *) arg3,
1653 return keyctl_revoke_key((key_serial_t) arg2);
1655 case KEYCTL_DESCRIBE:
1656 return keyctl_describe_key((key_serial_t) arg2,
1657 (char __user *) arg3,
1661 return keyctl_keyring_clear((key_serial_t) arg2);
1664 return keyctl_keyring_link((key_serial_t) arg2,
1665 (key_serial_t) arg3);
1668 return keyctl_keyring_unlink((key_serial_t) arg2,
1669 (key_serial_t) arg3);
1672 return keyctl_keyring_search((key_serial_t) arg2,
1673 (const char __user *) arg3,
1674 (const char __user *) arg4,
1675 (key_serial_t) arg5);
1678 return keyctl_read_key((key_serial_t) arg2,
1679 (char __user *) arg3,
1683 return keyctl_chown_key((key_serial_t) arg2,
1687 case KEYCTL_SETPERM:
1688 return keyctl_setperm_key((key_serial_t) arg2,
1691 case KEYCTL_INSTANTIATE:
1692 return keyctl_instantiate_key((key_serial_t) arg2,
1693 (const void __user *) arg3,
1695 (key_serial_t) arg5);
1698 return keyctl_negate_key((key_serial_t) arg2,
1700 (key_serial_t) arg4);
1702 case KEYCTL_SET_REQKEY_KEYRING:
1703 return keyctl_set_reqkey_keyring(arg2);
1705 case KEYCTL_SET_TIMEOUT:
1706 return keyctl_set_timeout((key_serial_t) arg2,
1709 case KEYCTL_ASSUME_AUTHORITY:
1710 return keyctl_assume_authority((key_serial_t) arg2);
1712 case KEYCTL_GET_SECURITY:
1713 return keyctl_get_security((key_serial_t) arg2,
1714 (char __user *) arg3,
1717 case KEYCTL_SESSION_TO_PARENT:
1718 return keyctl_session_to_parent();
1721 return keyctl_reject_key((key_serial_t) arg2,
1724 (key_serial_t) arg5);
1726 case KEYCTL_INSTANTIATE_IOV:
1727 return keyctl_instantiate_key_iov(
1728 (key_serial_t) arg2,
1729 (const struct iovec __user *) arg3,
1731 (key_serial_t) arg5);
1733 case KEYCTL_INVALIDATE:
1734 return keyctl_invalidate_key((key_serial_t) arg2);
1736 case KEYCTL_GET_PERSISTENT:
1737 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
1739 case KEYCTL_DH_COMPUTE:
1740 return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
1741 (char __user *) arg3, (size_t) arg4,
1742 (struct keyctl_kdf_params __user *) arg5);
1744 case KEYCTL_RESTRICT_KEYRING:
1745 return keyctl_restrict_keyring((key_serial_t) arg2,
1746 (const char __user *) arg3,
1747 (const char __user *) arg4);
1749 case KEYCTL_PKEY_QUERY:
1752 return keyctl_pkey_query((key_serial_t)arg2,
1753 (const char __user *)arg4,
1754 (struct keyctl_pkey_query *)arg5);
1756 case KEYCTL_PKEY_ENCRYPT:
1757 case KEYCTL_PKEY_DECRYPT:
1758 case KEYCTL_PKEY_SIGN:
1759 return keyctl_pkey_e_d_s(
1761 (const struct keyctl_pkey_params __user *)arg2,
1762 (const char __user *)arg3,
1763 (const void __user *)arg4,
1764 (void __user *)arg5);
1766 case KEYCTL_PKEY_VERIFY:
1767 return keyctl_pkey_verify(
1768 (const struct keyctl_pkey_params __user *)arg2,
1769 (const char __user *)arg3,
1770 (const void __user *)arg4,
1771 (const void __user *)arg5);