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/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/key.h>
18 #include <linux/keyctl.h>
20 #include <linux/capability.h>
21 #include <linux/string.h>
22 #include <linux/err.h>
23 #include <linux/vmalloc.h>
24 #include <linux/security.h>
25 #include <linux/uio.h>
26 #include <asm/uaccess.h>
29 static int key_get_type_from_user(char *type,
30 const char __user *_type,
35 ret = strncpy_from_user(type, _type, len);
38 if (ret == 0 || ret >= len)
45 * Extract the description of a new key from userspace and either add it as a
46 * new key to the specified keyring or update a matching key in that keyring.
48 * If the description is NULL or an empty string, the key type is asked to
49 * generate one from the payload.
51 * The keyring must be writable so that we can attach the key to it.
53 * If successful, the new key's serial number is returned, otherwise an error
56 SYSCALL_DEFINE5(add_key, const char __user *, _type,
57 const char __user *, _description,
58 const void __user *, _payload,
62 key_ref_t keyring_ref, key_ref;
63 char type[32], *description;
69 if (plen > 1024 * 1024 - 1)
72 /* draw all the data into kernel space */
73 ret = key_get_type_from_user(type, _type, sizeof(type));
79 description = strndup_user(_description, PAGE_SIZE);
80 if (IS_ERR(description)) {
81 ret = PTR_ERR(description);
87 } else if ((description[0] == '.') &&
88 (strncmp(type, "keyring", 7) == 0)) {
94 /* pull the payload in if one was supplied */
100 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
102 if (plen <= PAGE_SIZE)
105 payload = vmalloc(plen);
111 if (copy_from_user(payload, _payload, plen) != 0)
115 /* find the target keyring (which must be writable) */
116 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
117 if (IS_ERR(keyring_ref)) {
118 ret = PTR_ERR(keyring_ref);
122 /* create or update the requested key and add it to the target
124 key_ref = key_create_or_update(keyring_ref, type, description,
125 payload, plen, KEY_PERM_UNDEF,
127 if (!IS_ERR(key_ref)) {
128 ret = key_ref_to_ptr(key_ref)->serial;
129 key_ref_put(key_ref);
132 ret = PTR_ERR(key_ref);
135 key_ref_put(keyring_ref);
148 * Search the process keyrings and keyring trees linked from those for a
149 * matching key. Keyrings must have appropriate Search permission to be
152 * If a key is found, it will be attached to the destination keyring if there's
153 * one specified and the serial number of the key will be returned.
155 * If no key is found, /sbin/request-key will be invoked if _callout_info is
156 * non-NULL in an attempt to create a key. The _callout_info string will be
157 * passed to /sbin/request-key to aid with completing the request. If the
158 * _callout_info string is "" then it will be changed to "-".
160 SYSCALL_DEFINE4(request_key, const char __user *, _type,
161 const char __user *, _description,
162 const char __user *, _callout_info,
163 key_serial_t, destringid)
165 struct key_type *ktype;
169 char type[32], *description, *callout_info;
172 /* pull the type into kernel space */
173 ret = key_get_type_from_user(type, _type, sizeof(type));
177 /* pull the description into kernel space */
178 description = strndup_user(_description, PAGE_SIZE);
179 if (IS_ERR(description)) {
180 ret = PTR_ERR(description);
184 /* pull the callout info into kernel space */
188 callout_info = strndup_user(_callout_info, PAGE_SIZE);
189 if (IS_ERR(callout_info)) {
190 ret = PTR_ERR(callout_info);
193 callout_len = strlen(callout_info);
196 /* get the destination keyring if specified */
199 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
201 if (IS_ERR(dest_ref)) {
202 ret = PTR_ERR(dest_ref);
207 /* find the key type */
208 ktype = key_type_lookup(type);
210 ret = PTR_ERR(ktype);
215 key = request_key_and_link(ktype, description, callout_info,
216 callout_len, NULL, key_ref_to_ptr(dest_ref),
223 /* wait for the key to finish being constructed */
224 ret = wait_for_key_construction(key, 1);
235 key_ref_put(dest_ref);
245 * Get the ID of the specified process keyring.
247 * The requested keyring must have search permission to be found.
249 * If successful, the ID of the requested keyring will be returned.
251 long keyctl_get_keyring_ID(key_serial_t id, int create)
254 unsigned long lflags;
257 lflags = create ? KEY_LOOKUP_CREATE : 0;
258 key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
259 if (IS_ERR(key_ref)) {
260 ret = PTR_ERR(key_ref);
264 ret = key_ref_to_ptr(key_ref)->serial;
265 key_ref_put(key_ref);
271 * Join a (named) session keyring.
273 * Create and join an anonymous session keyring or join a named session
274 * keyring, creating it if necessary. A named session keyring must have Search
275 * permission for it to be joined. Session keyrings without this permit will
278 * If successful, the ID of the joined session keyring will be returned.
280 long keyctl_join_session_keyring(const char __user *_name)
285 /* fetch the name from userspace */
288 name = strndup_user(_name, PAGE_SIZE);
295 /* join the session */
296 ret = join_session_keyring(name);
304 * Update a key's data payload from the given data.
306 * The key must grant the caller Write permission and the key type must support
307 * updating for this to work. A negative key can be positively instantiated
310 * If successful, 0 will be returned. If the key type does not support
311 * updating, then -EOPNOTSUPP will be returned.
313 long keyctl_update_key(key_serial_t id,
314 const void __user *_payload,
322 if (plen > PAGE_SIZE)
325 /* pull the payload in if one was supplied */
329 payload = kmalloc(plen, GFP_KERNEL);
334 if (copy_from_user(payload, _payload, plen) != 0)
338 /* find the target key (which must be writable) */
339 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
340 if (IS_ERR(key_ref)) {
341 ret = PTR_ERR(key_ref);
346 ret = key_update(key_ref, payload, plen);
348 key_ref_put(key_ref);
358 * The key must be grant the caller Write or Setattr permission for this to
359 * work. The key type should give up its quota claim when revoked. The key
360 * and any links to the key will be automatically garbage collected after a
361 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
363 * If successful, 0 is returned.
365 long keyctl_revoke_key(key_serial_t id)
370 key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
371 if (IS_ERR(key_ref)) {
372 ret = PTR_ERR(key_ref);
375 key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
376 if (IS_ERR(key_ref)) {
377 ret = PTR_ERR(key_ref);
382 key_revoke(key_ref_to_ptr(key_ref));
385 key_ref_put(key_ref);
393 * The key must be grant the caller Invalidate permission for this to work.
394 * The key and any links to the key will be automatically garbage collected
397 * If successful, 0 is returned.
399 long keyctl_invalidate_key(key_serial_t id)
406 key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
407 if (IS_ERR(key_ref)) {
408 ret = PTR_ERR(key_ref);
410 /* Root is permitted to invalidate certain special keys */
411 if (capable(CAP_SYS_ADMIN)) {
412 key_ref = lookup_user_key(id, 0, 0);
415 if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
416 &key_ref_to_ptr(key_ref)->flags))
425 key_invalidate(key_ref_to_ptr(key_ref));
428 key_ref_put(key_ref);
430 kleave(" = %ld", ret);
435 * Clear the specified keyring, creating an empty process keyring if one of the
436 * special keyring IDs is used.
438 * The keyring must grant the caller Write permission for this to work. If
439 * successful, 0 will be returned.
441 long keyctl_keyring_clear(key_serial_t ringid)
443 key_ref_t keyring_ref;
446 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
447 if (IS_ERR(keyring_ref)) {
448 ret = PTR_ERR(keyring_ref);
450 /* Root is permitted to invalidate certain special keyrings */
451 if (capable(CAP_SYS_ADMIN)) {
452 keyring_ref = lookup_user_key(ringid, 0, 0);
453 if (IS_ERR(keyring_ref))
455 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
456 &key_ref_to_ptr(keyring_ref)->flags))
465 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
467 key_ref_put(keyring_ref);
473 * Create a link from a keyring to a key if there's no matching key in the
474 * keyring, otherwise replace the link to the matching key with a link to the
477 * The key must grant the caller Link permission and the the keyring must grant
478 * the caller Write permission. Furthermore, if an additional link is created,
479 * the keyring's quota will be extended.
481 * If successful, 0 will be returned.
483 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
485 key_ref_t keyring_ref, key_ref;
488 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
489 if (IS_ERR(keyring_ref)) {
490 ret = PTR_ERR(keyring_ref);
494 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
495 if (IS_ERR(key_ref)) {
496 ret = PTR_ERR(key_ref);
500 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
502 key_ref_put(key_ref);
504 key_ref_put(keyring_ref);
510 * Unlink a key from a keyring.
512 * The keyring must grant the caller Write permission for this to work; the key
513 * itself need not grant the caller anything. If the last link to a key is
514 * removed then that key will be scheduled for destruction.
516 * If successful, 0 will be returned.
518 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
520 key_ref_t keyring_ref, key_ref;
523 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
524 if (IS_ERR(keyring_ref)) {
525 ret = PTR_ERR(keyring_ref);
529 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
530 if (IS_ERR(key_ref)) {
531 ret = PTR_ERR(key_ref);
535 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
537 key_ref_put(key_ref);
539 key_ref_put(keyring_ref);
545 * Return a description of a key to userspace.
547 * The key must grant the caller View permission for this to work.
549 * If there's a buffer, we place up to buflen bytes of data into it formatted
550 * in the following way:
552 * type;uid;gid;perm;description<NUL>
554 * If successful, we return the amount of description available, irrespective
555 * of how much we may have copied into the buffer.
557 long keyctl_describe_key(key_serial_t keyid,
561 struct key *key, *instkey;
566 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
567 if (IS_ERR(key_ref)) {
568 /* viewing a key under construction is permitted if we have the
569 * authorisation token handy */
570 if (PTR_ERR(key_ref) == -EACCES) {
571 instkey = key_get_instantiation_authkey(keyid);
572 if (!IS_ERR(instkey)) {
574 key_ref = lookup_user_key(keyid,
577 if (!IS_ERR(key_ref))
582 ret = PTR_ERR(key_ref);
587 /* calculate how much description we're going to return */
589 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
593 key = key_ref_to_ptr(key_ref);
595 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
598 from_kuid_munged(current_user_ns(), key->uid),
599 from_kgid_munged(current_user_ns(), key->gid),
601 key->description ?: "");
603 /* include a NUL char at the end of the data */
604 if (ret > PAGE_SIZE - 1)
609 /* consider returning the data */
610 if (buffer && buflen > 0) {
614 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
620 key_ref_put(key_ref);
626 * Search the specified keyring and any keyrings it links to for a matching
627 * key. Only keyrings that grant the caller Search permission will be searched
628 * (this includes the starting keyring). Only keys with Search permission can
631 * If successful, the found key will be linked to the destination keyring if
632 * supplied and the key has Link permission, and the found key ID will be
635 long keyctl_keyring_search(key_serial_t ringid,
636 const char __user *_type,
637 const char __user *_description,
638 key_serial_t destringid)
640 struct key_type *ktype;
641 key_ref_t keyring_ref, key_ref, dest_ref;
642 char type[32], *description;
645 /* pull the type and description into kernel space */
646 ret = key_get_type_from_user(type, _type, sizeof(type));
650 description = strndup_user(_description, PAGE_SIZE);
651 if (IS_ERR(description)) {
652 ret = PTR_ERR(description);
656 /* get the keyring at which to begin the search */
657 keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
658 if (IS_ERR(keyring_ref)) {
659 ret = PTR_ERR(keyring_ref);
663 /* get the destination keyring if specified */
666 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
668 if (IS_ERR(dest_ref)) {
669 ret = PTR_ERR(dest_ref);
674 /* find the key type */
675 ktype = key_type_lookup(type);
677 ret = PTR_ERR(ktype);
682 key_ref = keyring_search(keyring_ref, ktype, description);
683 if (IS_ERR(key_ref)) {
684 ret = PTR_ERR(key_ref);
686 /* treat lack or presence of a negative key the same */
692 /* link the resulting key to the destination keyring if we can */
694 ret = key_permission(key_ref, KEY_NEED_LINK);
698 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
703 ret = key_ref_to_ptr(key_ref)->serial;
706 key_ref_put(key_ref);
710 key_ref_put(dest_ref);
712 key_ref_put(keyring_ref);
720 * Read a key's payload.
722 * The key must either grant the caller Read permission, or it must grant the
723 * caller Search permission when searched for from the process keyrings.
725 * If successful, we place up to buflen bytes of data into the buffer, if one
726 * is provided, and return the amount of data that is available in the key,
727 * irrespective of how much we copied into the buffer.
729 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
735 /* find the key first */
736 key_ref = lookup_user_key(keyid, 0, 0);
737 if (IS_ERR(key_ref)) {
742 key = key_ref_to_ptr(key_ref);
744 /* see if we can read it directly */
745 ret = key_permission(key_ref, KEY_NEED_READ);
751 /* we can't; see if it's searchable from this process's keyrings
752 * - we automatically take account of the fact that it may be
753 * dangling off an instantiation key
755 if (!is_key_possessed(key_ref)) {
760 /* the key is probably readable - now try to read it */
762 ret = key_validate(key);
765 if (key->type->read) {
766 /* read the data with the semaphore held (since we
768 down_read(&key->sem);
769 ret = key->type->read(key, buffer, buflen);
781 * Change the ownership of a key
783 * The key must grant the caller Setattr permission for this to work, though
784 * the key need not be fully instantiated yet. For the UID to be changed, or
785 * for the GID to be changed to a group the caller is not a member of, the
786 * caller must have sysadmin capability. If either uid or gid is -1 then that
787 * attribute is not changed.
789 * If the UID is to be changed, the new user must have sufficient quota to
790 * accept the key. The quota deduction will be removed from the old user to
791 * the new user should the attribute be changed.
793 * If successful, 0 will be returned.
795 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
797 struct key_user *newowner, *zapowner = NULL;
804 uid = make_kuid(current_user_ns(), user);
805 gid = make_kgid(current_user_ns(), group);
807 if ((user != (uid_t) -1) && !uid_valid(uid))
809 if ((group != (gid_t) -1) && !gid_valid(gid))
813 if (user == (uid_t) -1 && group == (gid_t) -1)
816 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
818 if (IS_ERR(key_ref)) {
819 ret = PTR_ERR(key_ref);
823 key = key_ref_to_ptr(key_ref);
825 /* make the changes with the locks held to prevent chown/chown races */
827 down_write(&key->sem);
829 if (!capable(CAP_SYS_ADMIN)) {
830 /* only the sysadmin can chown a key to some other UID */
831 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
834 /* only the sysadmin can set the key's GID to a group other
835 * than one of those that the current process subscribes to */
836 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
841 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
843 newowner = key_user_lookup(uid);
847 /* transfer the quota burden to the new user */
848 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
849 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
850 key_quota_root_maxkeys : key_quota_maxkeys;
851 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
852 key_quota_root_maxbytes : key_quota_maxbytes;
854 spin_lock(&newowner->lock);
855 if (newowner->qnkeys + 1 >= maxkeys ||
856 newowner->qnbytes + key->quotalen >= maxbytes ||
857 newowner->qnbytes + key->quotalen <
862 newowner->qnbytes += key->quotalen;
863 spin_unlock(&newowner->lock);
865 spin_lock(&key->user->lock);
867 key->user->qnbytes -= key->quotalen;
868 spin_unlock(&key->user->lock);
871 atomic_dec(&key->user->nkeys);
872 atomic_inc(&newowner->nkeys);
874 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
875 atomic_dec(&key->user->nikeys);
876 atomic_inc(&newowner->nikeys);
879 zapowner = key->user;
880 key->user = newowner;
885 if (group != (gid_t) -1)
894 key_user_put(zapowner);
899 spin_unlock(&newowner->lock);
906 * Change the permission mask on a key.
908 * The key must grant the caller Setattr permission for this to work, though
909 * the key need not be fully instantiated yet. If the caller does not have
910 * sysadmin capability, it may only change the permission on keys that it owns.
912 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
919 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
922 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
924 if (IS_ERR(key_ref)) {
925 ret = PTR_ERR(key_ref);
929 key = key_ref_to_ptr(key_ref);
931 /* make the changes with the locks held to prevent chown/chmod races */
933 down_write(&key->sem);
935 /* if we're not the sysadmin, we can only change a key that we own */
936 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
948 * Get the destination keyring for instantiation and check that the caller has
949 * Write permission on it.
951 static long get_instantiation_keyring(key_serial_t ringid,
952 struct request_key_auth *rka,
953 struct key **_dest_keyring)
957 *_dest_keyring = NULL;
959 /* just return a NULL pointer if we weren't asked to make a link */
963 /* if a specific keyring is nominated by ID, then use that */
965 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
967 return PTR_ERR(dkref);
968 *_dest_keyring = key_ref_to_ptr(dkref);
972 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
975 /* otherwise specify the destination keyring recorded in the
976 * authorisation key (any KEY_SPEC_*_KEYRING) */
977 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
978 *_dest_keyring = key_get(rka->dest_keyring);
986 * Change the request_key authorisation key on the current process.
988 static int keyctl_change_reqkey_auth(struct key *key)
992 new = prepare_creds();
996 key_put(new->request_key_auth);
997 new->request_key_auth = key_get(key);
999 return commit_creds(new);
1003 * Copy the iovec data from userspace
1005 static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
1008 for (; ioc > 0; ioc--) {
1009 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
1011 buffer += iov->iov_len;
1018 * Instantiate a key with the specified payload and link the key into the
1019 * destination keyring if one is given.
1021 * The caller must have the appropriate instantiation permit set for this to
1022 * work (see keyctl_assume_authority). No other permissions are required.
1024 * If successful, 0 will be returned.
1026 long keyctl_instantiate_key_common(key_serial_t id,
1027 const struct iovec *payload_iov,
1030 key_serial_t ringid)
1032 const struct cred *cred = current_cred();
1033 struct request_key_auth *rka;
1034 struct key *instkey, *dest_keyring;
1039 kenter("%d,,%zu,%d", id, plen, ringid);
1042 if (plen > 1024 * 1024 - 1)
1045 /* the appropriate instantiation authorisation key must have been
1046 * assumed before calling this */
1048 instkey = cred->request_key_auth;
1052 rka = instkey->payload.data;
1053 if (rka->target_key->serial != id)
1056 /* pull the payload in if one was supplied */
1061 payload = kmalloc(plen, GFP_KERNEL);
1063 if (plen <= PAGE_SIZE)
1066 payload = vmalloc(plen);
1071 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1076 /* find the destination keyring amongst those belonging to the
1077 * requesting task */
1078 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1082 /* instantiate the key and link it into a keyring */
1083 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1084 dest_keyring, instkey);
1086 key_put(dest_keyring);
1088 /* discard the assumed authority if it's just been disabled by
1089 * instantiation of the key */
1091 keyctl_change_reqkey_auth(NULL);
1103 * Instantiate a key with the specified payload and link the key into the
1104 * destination keyring if one is given.
1106 * The caller must have the appropriate instantiation permit set for this to
1107 * work (see keyctl_assume_authority). No other permissions are required.
1109 * If successful, 0 will be returned.
1111 long keyctl_instantiate_key(key_serial_t id,
1112 const void __user *_payload,
1114 key_serial_t ringid)
1116 if (_payload && plen) {
1117 struct iovec iov[1] = {
1118 [0].iov_base = (void __user *)_payload,
1122 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1125 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1129 * Instantiate a key with the specified multipart payload and link the key into
1130 * the destination keyring if one is given.
1132 * The caller must have the appropriate instantiation permit set for this to
1133 * work (see keyctl_assume_authority). No other permissions are required.
1135 * If successful, 0 will be returned.
1137 long keyctl_instantiate_key_iov(key_serial_t id,
1138 const struct iovec __user *_payload_iov,
1140 key_serial_t ringid)
1142 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1145 if (!_payload_iov || !ioc)
1148 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1149 ARRAY_SIZE(iovstack), iovstack, &iov);
1153 goto no_payload_free;
1155 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1157 if (iov != iovstack)
1162 if (iov != iovstack)
1165 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1169 * Negatively instantiate the key with the given timeout (in seconds) and link
1170 * the key into the destination keyring if one is given.
1172 * The caller must have the appropriate instantiation permit set for this to
1173 * work (see keyctl_assume_authority). No other permissions are required.
1175 * The key and any links to the key will be automatically garbage collected
1176 * after the timeout expires.
1178 * Negative keys are used to rate limit repeated request_key() calls by causing
1179 * them to return -ENOKEY until the negative key expires.
1181 * If successful, 0 will be returned.
1183 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1185 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1189 * Negatively instantiate the key with the given timeout (in seconds) and error
1190 * code and link the key into the destination keyring if one is given.
1192 * The caller must have the appropriate instantiation permit set for this to
1193 * work (see keyctl_assume_authority). No other permissions are required.
1195 * The key and any links to the key will be automatically garbage collected
1196 * after the timeout expires.
1198 * Negative keys are used to rate limit repeated request_key() calls by causing
1199 * them to return the specified error code until the negative key expires.
1201 * If successful, 0 will be returned.
1203 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1204 key_serial_t ringid)
1206 const struct cred *cred = current_cred();
1207 struct request_key_auth *rka;
1208 struct key *instkey, *dest_keyring;
1211 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1213 /* must be a valid error code and mustn't be a kernel special */
1215 error >= MAX_ERRNO ||
1216 error == ERESTARTSYS ||
1217 error == ERESTARTNOINTR ||
1218 error == ERESTARTNOHAND ||
1219 error == ERESTART_RESTARTBLOCK)
1222 /* the appropriate instantiation authorisation key must have been
1223 * assumed before calling this */
1225 instkey = cred->request_key_auth;
1229 rka = instkey->payload.data;
1230 if (rka->target_key->serial != id)
1233 /* find the destination keyring if present (which must also be
1235 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1239 /* instantiate the key and link it into a keyring */
1240 ret = key_reject_and_link(rka->target_key, timeout, error,
1241 dest_keyring, instkey);
1243 key_put(dest_keyring);
1245 /* discard the assumed authority if it's just been disabled by
1246 * instantiation of the key */
1248 keyctl_change_reqkey_auth(NULL);
1255 * Read or set the default keyring in which request_key() will cache keys and
1256 * return the old setting.
1258 * If a process keyring is specified then this will be created if it doesn't
1259 * yet exist. The old setting will be returned if successful.
1261 long keyctl_set_reqkey_keyring(int reqkey_defl)
1264 int ret, old_setting;
1266 old_setting = current_cred_xxx(jit_keyring);
1268 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1271 new = prepare_creds();
1275 switch (reqkey_defl) {
1276 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1277 ret = install_thread_keyring_to_cred(new);
1282 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1283 ret = install_process_keyring_to_cred(new);
1291 case KEY_REQKEY_DEFL_DEFAULT:
1292 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1293 case KEY_REQKEY_DEFL_USER_KEYRING:
1294 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1295 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1298 case KEY_REQKEY_DEFL_NO_CHANGE:
1299 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1306 new->jit_keyring = reqkey_defl;
1315 * Set or clear the timeout on a key.
1317 * Either the key must grant the caller Setattr permission or else the caller
1318 * must hold an instantiation authorisation token for the key.
1320 * The timeout is either 0 to clear the timeout, or a number of seconds from
1321 * the current time. The key and any links to the key will be automatically
1322 * garbage collected after the timeout expires.
1324 * If successful, 0 is returned.
1326 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1328 struct key *key, *instkey;
1332 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1334 if (IS_ERR(key_ref)) {
1335 /* setting the timeout on a key under construction is permitted
1336 * if we have the authorisation token handy */
1337 if (PTR_ERR(key_ref) == -EACCES) {
1338 instkey = key_get_instantiation_authkey(id);
1339 if (!IS_ERR(instkey)) {
1341 key_ref = lookup_user_key(id,
1344 if (!IS_ERR(key_ref))
1349 ret = PTR_ERR(key_ref);
1354 key = key_ref_to_ptr(key_ref);
1355 key_set_timeout(key, timeout);
1364 * Assume (or clear) the authority to instantiate the specified key.
1366 * This sets the authoritative token currently in force for key instantiation.
1367 * This must be done for a key to be instantiated. It has the effect of making
1368 * available all the keys from the caller of the request_key() that created a
1369 * key to request_key() calls made by the caller of this function.
1371 * The caller must have the instantiation key in their process keyrings with a
1372 * Search permission grant available to the caller.
1374 * If the ID given is 0, then the setting will be cleared and 0 returned.
1376 * If the ID given has a matching an authorisation key, then that key will be
1377 * set and its ID will be returned. The authorisation key can be read to get
1378 * the callout information passed to request_key().
1380 long keyctl_assume_authority(key_serial_t id)
1382 struct key *authkey;
1385 /* special key IDs aren't permitted */
1390 /* we divest ourselves of authority if given an ID of 0 */
1392 ret = keyctl_change_reqkey_auth(NULL);
1396 /* attempt to assume the authority temporarily granted to us whilst we
1397 * instantiate the specified key
1398 * - the authorisation key must be in the current task's keyrings
1401 authkey = key_get_instantiation_authkey(id);
1402 if (IS_ERR(authkey)) {
1403 ret = PTR_ERR(authkey);
1407 ret = keyctl_change_reqkey_auth(authkey);
1412 ret = authkey->serial;
1418 * Get a key's the LSM security label.
1420 * The key must grant the caller View permission for this to work.
1422 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1424 * If successful, the amount of information available will be returned,
1425 * irrespective of how much was copied (including the terminal NUL).
1427 long keyctl_get_security(key_serial_t keyid,
1428 char __user *buffer,
1431 struct key *key, *instkey;
1436 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
1437 if (IS_ERR(key_ref)) {
1438 if (PTR_ERR(key_ref) != -EACCES)
1439 return PTR_ERR(key_ref);
1441 /* viewing a key under construction is also permitted if we
1442 * have the authorisation token handy */
1443 instkey = key_get_instantiation_authkey(keyid);
1444 if (IS_ERR(instkey))
1445 return PTR_ERR(instkey);
1448 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1449 if (IS_ERR(key_ref))
1450 return PTR_ERR(key_ref);
1453 key = key_ref_to_ptr(key_ref);
1454 ret = security_key_getsecurity(key, &context);
1456 /* if no information was returned, give userspace an empty
1459 if (buffer && buflen > 0 &&
1460 copy_to_user(buffer, "", 1) != 0)
1462 } else if (ret > 0) {
1463 /* return as much data as there's room for */
1464 if (buffer && buflen > 0) {
1468 if (copy_to_user(buffer, context, buflen) != 0)
1475 key_ref_put(key_ref);
1480 * Attempt to install the calling process's session keyring on the process's
1483 * The keyring must exist and must grant the caller LINK permission, and the
1484 * parent process must be single-threaded and must have the same effective
1485 * ownership as this process and mustn't be SUID/SGID.
1487 * The keyring will be emplaced on the parent when it next resumes userspace.
1489 * If successful, 0 will be returned.
1491 long keyctl_session_to_parent(void)
1493 struct task_struct *me, *parent;
1494 const struct cred *mycred, *pcred;
1495 struct callback_head *newwork, *oldwork;
1496 key_ref_t keyring_r;
1500 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
1501 if (IS_ERR(keyring_r))
1502 return PTR_ERR(keyring_r);
1506 /* our parent is going to need a new cred struct, a new tgcred struct
1507 * and new security data, so we allocate them here to prevent ENOMEM in
1509 cred = cred_alloc_blank();
1512 newwork = &cred->rcu;
1514 cred->session_keyring = key_ref_to_ptr(keyring_r);
1516 init_task_work(newwork, key_change_session_keyring);
1520 write_lock_irq(&tasklist_lock);
1524 parent = me->real_parent;
1526 /* the parent mustn't be init and mustn't be a kernel thread */
1527 if (parent->pid <= 1 || !parent->mm)
1530 /* the parent must be single threaded */
1531 if (!thread_group_empty(parent))
1534 /* the parent and the child must have different session keyrings or
1535 * there's no point */
1536 mycred = current_cred();
1537 pcred = __task_cred(parent);
1538 if (mycred == pcred ||
1539 mycred->session_keyring == pcred->session_keyring) {
1544 /* the parent must have the same effective ownership and mustn't be
1546 if (!uid_eq(pcred->uid, mycred->euid) ||
1547 !uid_eq(pcred->euid, mycred->euid) ||
1548 !uid_eq(pcred->suid, mycred->euid) ||
1549 !gid_eq(pcred->gid, mycred->egid) ||
1550 !gid_eq(pcred->egid, mycred->egid) ||
1551 !gid_eq(pcred->sgid, mycred->egid))
1554 /* the keyrings must have the same UID */
1555 if ((pcred->session_keyring &&
1556 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1557 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1560 /* cancel an already pending keyring replacement */
1561 oldwork = task_work_cancel(parent, key_change_session_keyring);
1563 /* the replacement session keyring is applied just prior to userspace
1565 ret = task_work_add(parent, newwork, true);
1569 write_unlock_irq(&tasklist_lock);
1572 put_cred(container_of(oldwork, struct cred, rcu));
1578 key_ref_put(keyring_r);
1583 * The key control system call
1585 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1586 unsigned long, arg4, unsigned long, arg5)
1589 case KEYCTL_GET_KEYRING_ID:
1590 return keyctl_get_keyring_ID((key_serial_t) arg2,
1593 case KEYCTL_JOIN_SESSION_KEYRING:
1594 return keyctl_join_session_keyring((const char __user *) arg2);
1597 return keyctl_update_key((key_serial_t) arg2,
1598 (const void __user *) arg3,
1602 return keyctl_revoke_key((key_serial_t) arg2);
1604 case KEYCTL_DESCRIBE:
1605 return keyctl_describe_key((key_serial_t) arg2,
1606 (char __user *) arg3,
1610 return keyctl_keyring_clear((key_serial_t) arg2);
1613 return keyctl_keyring_link((key_serial_t) arg2,
1614 (key_serial_t) arg3);
1617 return keyctl_keyring_unlink((key_serial_t) arg2,
1618 (key_serial_t) arg3);
1621 return keyctl_keyring_search((key_serial_t) arg2,
1622 (const char __user *) arg3,
1623 (const char __user *) arg4,
1624 (key_serial_t) arg5);
1627 return keyctl_read_key((key_serial_t) arg2,
1628 (char __user *) arg3,
1632 return keyctl_chown_key((key_serial_t) arg2,
1636 case KEYCTL_SETPERM:
1637 return keyctl_setperm_key((key_serial_t) arg2,
1640 case KEYCTL_INSTANTIATE:
1641 return keyctl_instantiate_key((key_serial_t) arg2,
1642 (const void __user *) arg3,
1644 (key_serial_t) arg5);
1647 return keyctl_negate_key((key_serial_t) arg2,
1649 (key_serial_t) arg4);
1651 case KEYCTL_SET_REQKEY_KEYRING:
1652 return keyctl_set_reqkey_keyring(arg2);
1654 case KEYCTL_SET_TIMEOUT:
1655 return keyctl_set_timeout((key_serial_t) arg2,
1658 case KEYCTL_ASSUME_AUTHORITY:
1659 return keyctl_assume_authority((key_serial_t) arg2);
1661 case KEYCTL_GET_SECURITY:
1662 return keyctl_get_security((key_serial_t) arg2,
1663 (char __user *) arg3,
1666 case KEYCTL_SESSION_TO_PARENT:
1667 return keyctl_session_to_parent();
1670 return keyctl_reject_key((key_serial_t) arg2,
1673 (key_serial_t) arg5);
1675 case KEYCTL_INSTANTIATE_IOV:
1676 return keyctl_instantiate_key_iov(
1677 (key_serial_t) arg2,
1678 (const struct iovec __user *) arg3,
1680 (key_serial_t) arg5);
1682 case KEYCTL_INVALIDATE:
1683 return keyctl_invalidate_key((key_serial_t) arg2);
1685 case KEYCTL_GET_PERSISTENT:
1686 return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);