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)
47 * Extract the description of a new key from userspace and either add it as a
48 * new key to the specified keyring or update a matching key in that keyring.
50 * If the description is NULL or an empty string, the key type is asked to
51 * generate one from the payload.
53 * The keyring must be writable so that we can attach the key to it.
55 * If successful, the new key's serial number is returned, otherwise an error
58 SYSCALL_DEFINE5(add_key, const char __user *, _type,
59 const char __user *, _description,
60 const void __user *, _payload,
64 key_ref_t keyring_ref, key_ref;
65 char type[32], *description;
71 if (plen > 1024 * 1024 - 1)
74 /* draw all the data into kernel space */
75 ret = key_get_type_from_user(type, _type, sizeof(type));
81 description = strndup_user(_description, PAGE_SIZE);
82 if (IS_ERR(description)) {
83 ret = PTR_ERR(description);
92 /* pull the payload in if one was supplied */
98 payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN);
100 if (plen <= PAGE_SIZE)
103 payload = vmalloc(plen);
109 if (copy_from_user(payload, _payload, plen) != 0)
113 /* find the target keyring (which must be writable) */
114 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
115 if (IS_ERR(keyring_ref)) {
116 ret = PTR_ERR(keyring_ref);
120 /* create or update the requested key and add it to the target
122 key_ref = key_create_or_update(keyring_ref, type, description,
123 payload, plen, KEY_PERM_UNDEF,
125 if (!IS_ERR(key_ref)) {
126 ret = key_ref_to_ptr(key_ref)->serial;
127 key_ref_put(key_ref);
130 ret = PTR_ERR(key_ref);
133 key_ref_put(keyring_ref);
146 * Search the process keyrings and keyring trees linked from those for a
147 * matching key. Keyrings must have appropriate Search permission to be
150 * If a key is found, it will be attached to the destination keyring if there's
151 * one specified and the serial number of the key will be returned.
153 * If no key is found, /sbin/request-key will be invoked if _callout_info is
154 * non-NULL in an attempt to create a key. The _callout_info string will be
155 * passed to /sbin/request-key to aid with completing the request. If the
156 * _callout_info string is "" then it will be changed to "-".
158 SYSCALL_DEFINE4(request_key, const char __user *, _type,
159 const char __user *, _description,
160 const char __user *, _callout_info,
161 key_serial_t, destringid)
163 struct key_type *ktype;
167 char type[32], *description, *callout_info;
170 /* pull the type into kernel space */
171 ret = key_get_type_from_user(type, _type, sizeof(type));
175 /* pull the description into kernel space */
176 description = strndup_user(_description, PAGE_SIZE);
177 if (IS_ERR(description)) {
178 ret = PTR_ERR(description);
182 /* pull the callout info into kernel space */
186 callout_info = strndup_user(_callout_info, PAGE_SIZE);
187 if (IS_ERR(callout_info)) {
188 ret = PTR_ERR(callout_info);
191 callout_len = strlen(callout_info);
194 /* get the destination keyring if specified */
197 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
199 if (IS_ERR(dest_ref)) {
200 ret = PTR_ERR(dest_ref);
205 /* find the key type */
206 ktype = key_type_lookup(type);
208 ret = PTR_ERR(ktype);
213 key = request_key_and_link(ktype, description, callout_info,
214 callout_len, NULL, key_ref_to_ptr(dest_ref),
221 /* wait for the key to finish being constructed */
222 ret = wait_for_key_construction(key, 1);
233 key_ref_put(dest_ref);
243 * Get the ID of the specified process keyring.
245 * The requested keyring must have search permission to be found.
247 * If successful, the ID of the requested keyring will be returned.
249 long keyctl_get_keyring_ID(key_serial_t id, int create)
252 unsigned long lflags;
255 lflags = create ? KEY_LOOKUP_CREATE : 0;
256 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
257 if (IS_ERR(key_ref)) {
258 ret = PTR_ERR(key_ref);
262 ret = key_ref_to_ptr(key_ref)->serial;
263 key_ref_put(key_ref);
269 * Join a (named) session keyring.
271 * Create and join an anonymous session keyring or join a named session
272 * keyring, creating it if necessary. A named session keyring must have Search
273 * permission for it to be joined. Session keyrings without this permit will
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, PAGE_SIZE);
293 /* join the session */
294 ret = join_session_keyring(name);
302 * Update a key's data payload from the given data.
304 * The key must grant the caller Write permission and the key type must support
305 * updating for this to work. A negative key can be positively instantiated
308 * If successful, 0 will be returned. If the key type does not support
309 * updating, then -EOPNOTSUPP will be returned.
311 long keyctl_update_key(key_serial_t id,
312 const void __user *_payload,
320 if (plen > PAGE_SIZE)
323 /* pull the payload in if one was supplied */
327 payload = kmalloc(plen, GFP_KERNEL);
332 if (copy_from_user(payload, _payload, plen) != 0)
336 /* find the target key (which must be writable) */
337 key_ref = lookup_user_key(id, 0, KEY_WRITE);
338 if (IS_ERR(key_ref)) {
339 ret = PTR_ERR(key_ref);
344 ret = key_update(key_ref, payload, plen);
346 key_ref_put(key_ref);
356 * The key must be grant the caller Write or Setattr permission for this to
357 * work. The key type should give up its quota claim when revoked. The key
358 * and any links to the key will be automatically garbage collected after a
359 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
361 * If successful, 0 is returned.
363 long keyctl_revoke_key(key_serial_t id)
368 key_ref = lookup_user_key(id, 0, KEY_WRITE);
369 if (IS_ERR(key_ref)) {
370 ret = PTR_ERR(key_ref);
373 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
374 if (IS_ERR(key_ref)) {
375 ret = PTR_ERR(key_ref);
380 key_revoke(key_ref_to_ptr(key_ref));
383 key_ref_put(key_ref);
391 * The key must be grant the caller Invalidate permission for this to work.
392 * The key and any links to the key will be automatically garbage collected
395 * If successful, 0 is returned.
397 long keyctl_invalidate_key(key_serial_t id)
404 key_ref = lookup_user_key(id, 0, KEY_SEARCH);
405 if (IS_ERR(key_ref)) {
406 ret = PTR_ERR(key_ref);
410 key_invalidate(key_ref_to_ptr(key_ref));
413 key_ref_put(key_ref);
415 kleave(" = %ld", ret);
420 * Clear the specified keyring, creating an empty process keyring if one of the
421 * special keyring IDs is used.
423 * The keyring must grant the caller Write permission for this to work. If
424 * successful, 0 will be returned.
426 long keyctl_keyring_clear(key_serial_t ringid)
428 key_ref_t keyring_ref;
431 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
432 if (IS_ERR(keyring_ref)) {
433 ret = PTR_ERR(keyring_ref);
435 /* Root is permitted to invalidate certain special keyrings */
436 if (capable(CAP_SYS_ADMIN)) {
437 keyring_ref = lookup_user_key(ringid, 0, 0);
438 if (IS_ERR(keyring_ref))
440 if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
441 &key_ref_to_ptr(keyring_ref)->flags))
450 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
452 key_ref_put(keyring_ref);
458 * Create a link from a keyring to a key if there's no matching key in the
459 * keyring, otherwise replace the link to the matching key with a link to the
462 * The key must grant the caller Link permission and the the keyring must grant
463 * the caller Write permission. Furthermore, if an additional link is created,
464 * the keyring's quota will be extended.
466 * If successful, 0 will be returned.
468 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
470 key_ref_t keyring_ref, key_ref;
473 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
474 if (IS_ERR(keyring_ref)) {
475 ret = PTR_ERR(keyring_ref);
479 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
480 if (IS_ERR(key_ref)) {
481 ret = PTR_ERR(key_ref);
485 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
487 key_ref_put(key_ref);
489 key_ref_put(keyring_ref);
495 * Unlink a key from a keyring.
497 * The keyring must grant the caller Write permission for this to work; the key
498 * itself need not grant the caller anything. If the last link to a key is
499 * removed then that key will be scheduled for destruction.
501 * If successful, 0 will be returned.
503 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
505 key_ref_t keyring_ref, key_ref;
508 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
509 if (IS_ERR(keyring_ref)) {
510 ret = PTR_ERR(keyring_ref);
514 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
515 if (IS_ERR(key_ref)) {
516 ret = PTR_ERR(key_ref);
520 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
522 key_ref_put(key_ref);
524 key_ref_put(keyring_ref);
530 * Return a description of a key to userspace.
532 * The key must grant the caller View permission for this to work.
534 * If there's a buffer, we place up to buflen bytes of data into it formatted
535 * in the following way:
537 * type;uid;gid;perm;description<NUL>
539 * If successful, we return the amount of description available, irrespective
540 * of how much we may have copied into the buffer.
542 long keyctl_describe_key(key_serial_t keyid,
546 struct key *key, *instkey;
551 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
552 if (IS_ERR(key_ref)) {
553 /* viewing a key under construction is permitted if we have the
554 * authorisation token handy */
555 if (PTR_ERR(key_ref) == -EACCES) {
556 instkey = key_get_instantiation_authkey(keyid);
557 if (!IS_ERR(instkey)) {
559 key_ref = lookup_user_key(keyid,
562 if (!IS_ERR(key_ref))
567 ret = PTR_ERR(key_ref);
572 /* calculate how much description we're going to return */
574 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
578 key = key_ref_to_ptr(key_ref);
580 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
583 from_kuid_munged(current_user_ns(), key->uid),
584 from_kgid_munged(current_user_ns(), key->gid),
586 key->description ?: "");
588 /* include a NUL char at the end of the data */
589 if (ret > PAGE_SIZE - 1)
594 /* consider returning the data */
595 if (buffer && buflen > 0) {
599 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
605 key_ref_put(key_ref);
611 * Search the specified keyring and any keyrings it links to for a matching
612 * key. Only keyrings that grant the caller Search permission will be searched
613 * (this includes the starting keyring). Only keys with Search permission can
616 * If successful, the found key will be linked to the destination keyring if
617 * supplied and the key has Link permission, and the found key ID will be
620 long keyctl_keyring_search(key_serial_t ringid,
621 const char __user *_type,
622 const char __user *_description,
623 key_serial_t destringid)
625 struct key_type *ktype;
626 key_ref_t keyring_ref, key_ref, dest_ref;
627 char type[32], *description;
630 /* pull the type and description into kernel space */
631 ret = key_get_type_from_user(type, _type, sizeof(type));
635 description = strndup_user(_description, PAGE_SIZE);
636 if (IS_ERR(description)) {
637 ret = PTR_ERR(description);
641 /* get the keyring at which to begin the search */
642 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
643 if (IS_ERR(keyring_ref)) {
644 ret = PTR_ERR(keyring_ref);
648 /* get the destination keyring if specified */
651 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
653 if (IS_ERR(dest_ref)) {
654 ret = PTR_ERR(dest_ref);
659 /* find the key type */
660 ktype = key_type_lookup(type);
662 ret = PTR_ERR(ktype);
667 key_ref = keyring_search(keyring_ref, ktype, description);
668 if (IS_ERR(key_ref)) {
669 ret = PTR_ERR(key_ref);
671 /* treat lack or presence of a negative key the same */
677 /* link the resulting key to the destination keyring if we can */
679 ret = key_permission(key_ref, KEY_LINK);
683 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
688 ret = key_ref_to_ptr(key_ref)->serial;
691 key_ref_put(key_ref);
695 key_ref_put(dest_ref);
697 key_ref_put(keyring_ref);
705 * Read a key's payload.
707 * The key must either grant the caller Read permission, or it must grant the
708 * caller Search permission when searched for from the process keyrings.
710 * If successful, we place up to buflen bytes of data into the buffer, if one
711 * is provided, and return the amount of data that is available in the key,
712 * irrespective of how much we copied into the buffer.
714 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
720 /* find the key first */
721 key_ref = lookup_user_key(keyid, 0, 0);
722 if (IS_ERR(key_ref)) {
727 key = key_ref_to_ptr(key_ref);
729 /* see if we can read it directly */
730 ret = key_permission(key_ref, KEY_READ);
736 /* we can't; see if it's searchable from this process's keyrings
737 * - we automatically take account of the fact that it may be
738 * dangling off an instantiation key
740 if (!is_key_possessed(key_ref)) {
745 /* the key is probably readable - now try to read it */
747 ret = key_validate(key);
750 if (key->type->read) {
751 /* read the data with the semaphore held (since we
753 down_read(&key->sem);
754 ret = key->type->read(key, buffer, buflen);
766 * Change the ownership of a key
768 * The key must grant the caller Setattr permission for this to work, though
769 * the key need not be fully instantiated yet. For the UID to be changed, or
770 * for the GID to be changed to a group the caller is not a member of, the
771 * caller must have sysadmin capability. If either uid or gid is -1 then that
772 * attribute is not changed.
774 * If the UID is to be changed, the new user must have sufficient quota to
775 * accept the key. The quota deduction will be removed from the old user to
776 * the new user should the attribute be changed.
778 * If successful, 0 will be returned.
780 long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
782 struct key_user *newowner, *zapowner = NULL;
789 uid = make_kuid(current_user_ns(), user);
790 gid = make_kgid(current_user_ns(), group);
792 if ((user != (uid_t) -1) && !uid_valid(uid))
794 if ((group != (gid_t) -1) && !gid_valid(gid))
798 if (user == (uid_t) -1 && group == (gid_t) -1)
801 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
803 if (IS_ERR(key_ref)) {
804 ret = PTR_ERR(key_ref);
808 key = key_ref_to_ptr(key_ref);
810 /* make the changes with the locks held to prevent chown/chown races */
812 down_write(&key->sem);
814 if (!capable(CAP_SYS_ADMIN)) {
815 /* only the sysadmin can chown a key to some other UID */
816 if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
819 /* only the sysadmin can set the key's GID to a group other
820 * than one of those that the current process subscribes to */
821 if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
826 if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
828 newowner = key_user_lookup(uid);
832 /* transfer the quota burden to the new user */
833 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
834 unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
835 key_quota_root_maxkeys : key_quota_maxkeys;
836 unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
837 key_quota_root_maxbytes : key_quota_maxbytes;
839 spin_lock(&newowner->lock);
840 if (newowner->qnkeys + 1 >= maxkeys ||
841 newowner->qnbytes + key->quotalen >= maxbytes ||
842 newowner->qnbytes + key->quotalen <
847 newowner->qnbytes += key->quotalen;
848 spin_unlock(&newowner->lock);
850 spin_lock(&key->user->lock);
852 key->user->qnbytes -= key->quotalen;
853 spin_unlock(&key->user->lock);
856 atomic_dec(&key->user->nkeys);
857 atomic_inc(&newowner->nkeys);
859 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
860 atomic_dec(&key->user->nikeys);
861 atomic_inc(&newowner->nikeys);
864 zapowner = key->user;
865 key->user = newowner;
870 if (group != (gid_t) -1)
879 key_user_put(zapowner);
884 spin_unlock(&newowner->lock);
891 * Change the permission mask on a key.
893 * The key must grant the caller Setattr permission for this to work, though
894 * the key need not be fully instantiated yet. If the caller does not have
895 * sysadmin capability, it may only change the permission on keys that it owns.
897 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
904 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
907 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
909 if (IS_ERR(key_ref)) {
910 ret = PTR_ERR(key_ref);
914 key = key_ref_to_ptr(key_ref);
916 /* make the changes with the locks held to prevent chown/chmod races */
918 down_write(&key->sem);
920 /* if we're not the sysadmin, we can only change a key that we own */
921 if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
933 * Get the destination keyring for instantiation and check that the caller has
934 * Write permission on it.
936 static long get_instantiation_keyring(key_serial_t ringid,
937 struct request_key_auth *rka,
938 struct key **_dest_keyring)
942 *_dest_keyring = NULL;
944 /* just return a NULL pointer if we weren't asked to make a link */
948 /* if a specific keyring is nominated by ID, then use that */
950 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
952 return PTR_ERR(dkref);
953 *_dest_keyring = key_ref_to_ptr(dkref);
957 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
960 /* otherwise specify the destination keyring recorded in the
961 * authorisation key (any KEY_SPEC_*_KEYRING) */
962 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
963 *_dest_keyring = key_get(rka->dest_keyring);
971 * Change the request_key authorisation key on the current process.
973 static int keyctl_change_reqkey_auth(struct key *key)
977 new = prepare_creds();
981 key_put(new->request_key_auth);
982 new->request_key_auth = key_get(key);
984 return commit_creds(new);
988 * Copy the iovec data from userspace
990 static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
993 for (; ioc > 0; ioc--) {
994 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
996 buffer += iov->iov_len;
1003 * Instantiate a key with the specified payload and link the key into the
1004 * destination keyring if one is given.
1006 * The caller must have the appropriate instantiation permit set for this to
1007 * work (see keyctl_assume_authority). No other permissions are required.
1009 * If successful, 0 will be returned.
1011 long keyctl_instantiate_key_common(key_serial_t id,
1012 const struct iovec *payload_iov,
1015 key_serial_t ringid)
1017 const struct cred *cred = current_cred();
1018 struct request_key_auth *rka;
1019 struct key *instkey, *dest_keyring;
1024 kenter("%d,,%zu,%d", id, plen, ringid);
1027 if (plen > 1024 * 1024 - 1)
1030 /* the appropriate instantiation authorisation key must have been
1031 * assumed before calling this */
1033 instkey = cred->request_key_auth;
1037 rka = instkey->payload.data;
1038 if (rka->target_key->serial != id)
1041 /* pull the payload in if one was supplied */
1046 payload = kmalloc(plen, GFP_KERNEL);
1048 if (plen <= PAGE_SIZE)
1051 payload = vmalloc(plen);
1056 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1061 /* find the destination keyring amongst those belonging to the
1062 * requesting task */
1063 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1067 /* instantiate the key and link it into a keyring */
1068 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1069 dest_keyring, instkey);
1071 key_put(dest_keyring);
1073 /* discard the assumed authority if it's just been disabled by
1074 * instantiation of the key */
1076 keyctl_change_reqkey_auth(NULL);
1088 * Instantiate a key with the specified payload and link the key into the
1089 * destination keyring if one is given.
1091 * The caller must have the appropriate instantiation permit set for this to
1092 * work (see keyctl_assume_authority). No other permissions are required.
1094 * If successful, 0 will be returned.
1096 long keyctl_instantiate_key(key_serial_t id,
1097 const void __user *_payload,
1099 key_serial_t ringid)
1101 if (_payload && plen) {
1102 struct iovec iov[1] = {
1103 [0].iov_base = (void __user *)_payload,
1107 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1110 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1114 * Instantiate a key with the specified multipart payload and link the key into
1115 * the destination keyring if one is given.
1117 * The caller must have the appropriate instantiation permit set for this to
1118 * work (see keyctl_assume_authority). No other permissions are required.
1120 * If successful, 0 will be returned.
1122 long keyctl_instantiate_key_iov(key_serial_t id,
1123 const struct iovec __user *_payload_iov,
1125 key_serial_t ringid)
1127 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1130 if (!_payload_iov || !ioc)
1133 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1134 ARRAY_SIZE(iovstack), iovstack, &iov);
1138 goto no_payload_free;
1140 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1142 if (iov != iovstack)
1147 if (iov != iovstack)
1150 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1154 * Negatively instantiate the key with the given timeout (in seconds) and link
1155 * the key into the destination keyring if one is given.
1157 * The caller must have the appropriate instantiation permit set for this to
1158 * work (see keyctl_assume_authority). No other permissions are required.
1160 * The key and any links to the key will be automatically garbage collected
1161 * after the timeout expires.
1163 * Negative keys are used to rate limit repeated request_key() calls by causing
1164 * them to return -ENOKEY until the negative key expires.
1166 * If successful, 0 will be returned.
1168 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1170 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1174 * Negatively instantiate the key with the given timeout (in seconds) and error
1175 * code and link the key into the destination keyring if one is given.
1177 * The caller must have the appropriate instantiation permit set for this to
1178 * work (see keyctl_assume_authority). No other permissions are required.
1180 * The key and any links to the key will be automatically garbage collected
1181 * after the timeout expires.
1183 * Negative keys are used to rate limit repeated request_key() calls by causing
1184 * them to return the specified error code until the negative key expires.
1186 * If successful, 0 will be returned.
1188 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1189 key_serial_t ringid)
1191 const struct cred *cred = current_cred();
1192 struct request_key_auth *rka;
1193 struct key *instkey, *dest_keyring;
1196 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1198 /* must be a valid error code and mustn't be a kernel special */
1200 error >= MAX_ERRNO ||
1201 error == ERESTARTSYS ||
1202 error == ERESTARTNOINTR ||
1203 error == ERESTARTNOHAND ||
1204 error == ERESTART_RESTARTBLOCK)
1207 /* the appropriate instantiation authorisation key must have been
1208 * assumed before calling this */
1210 instkey = cred->request_key_auth;
1214 rka = instkey->payload.data;
1215 if (rka->target_key->serial != id)
1218 /* find the destination keyring if present (which must also be
1220 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1224 /* instantiate the key and link it into a keyring */
1225 ret = key_reject_and_link(rka->target_key, timeout, error,
1226 dest_keyring, instkey);
1228 key_put(dest_keyring);
1230 /* discard the assumed authority if it's just been disabled by
1231 * instantiation of the key */
1233 keyctl_change_reqkey_auth(NULL);
1240 * Read or set the default keyring in which request_key() will cache keys and
1241 * return the old setting.
1243 * If a process keyring is specified then this will be created if it doesn't
1244 * yet exist. The old setting will be returned if successful.
1246 long keyctl_set_reqkey_keyring(int reqkey_defl)
1249 int ret, old_setting;
1251 old_setting = current_cred_xxx(jit_keyring);
1253 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1256 new = prepare_creds();
1260 switch (reqkey_defl) {
1261 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1262 ret = install_thread_keyring_to_cred(new);
1267 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1268 ret = install_process_keyring_to_cred(new);
1276 case KEY_REQKEY_DEFL_DEFAULT:
1277 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1278 case KEY_REQKEY_DEFL_USER_KEYRING:
1279 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1280 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1283 case KEY_REQKEY_DEFL_NO_CHANGE:
1284 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1291 new->jit_keyring = reqkey_defl;
1300 * Set or clear the timeout on a key.
1302 * Either the key must grant the caller Setattr permission or else the caller
1303 * must hold an instantiation authorisation token for the key.
1305 * The timeout is either 0 to clear the timeout, or a number of seconds from
1306 * the current time. The key and any links to the key will be automatically
1307 * garbage collected after the timeout expires.
1309 * If successful, 0 is returned.
1311 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1313 struct key *key, *instkey;
1317 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1319 if (IS_ERR(key_ref)) {
1320 /* setting the timeout on a key under construction is permitted
1321 * if we have the authorisation token handy */
1322 if (PTR_ERR(key_ref) == -EACCES) {
1323 instkey = key_get_instantiation_authkey(id);
1324 if (!IS_ERR(instkey)) {
1326 key_ref = lookup_user_key(id,
1329 if (!IS_ERR(key_ref))
1334 ret = PTR_ERR(key_ref);
1339 key = key_ref_to_ptr(key_ref);
1340 key_set_timeout(key, timeout);
1349 * Assume (or clear) the authority to instantiate the specified key.
1351 * This sets the authoritative token currently in force for key instantiation.
1352 * This must be done for a key to be instantiated. It has the effect of making
1353 * available all the keys from the caller of the request_key() that created a
1354 * key to request_key() calls made by the caller of this function.
1356 * The caller must have the instantiation key in their process keyrings with a
1357 * Search permission grant available to the caller.
1359 * If the ID given is 0, then the setting will be cleared and 0 returned.
1361 * If the ID given has a matching an authorisation key, then that key will be
1362 * set and its ID will be returned. The authorisation key can be read to get
1363 * the callout information passed to request_key().
1365 long keyctl_assume_authority(key_serial_t id)
1367 struct key *authkey;
1370 /* special key IDs aren't permitted */
1375 /* we divest ourselves of authority if given an ID of 0 */
1377 ret = keyctl_change_reqkey_auth(NULL);
1381 /* attempt to assume the authority temporarily granted to us whilst we
1382 * instantiate the specified key
1383 * - the authorisation key must be in the current task's keyrings
1386 authkey = key_get_instantiation_authkey(id);
1387 if (IS_ERR(authkey)) {
1388 ret = PTR_ERR(authkey);
1392 ret = keyctl_change_reqkey_auth(authkey);
1397 ret = authkey->serial;
1403 * Get a key's the LSM security label.
1405 * The key must grant the caller View permission for this to work.
1407 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1409 * If successful, the amount of information available will be returned,
1410 * irrespective of how much was copied (including the terminal NUL).
1412 long keyctl_get_security(key_serial_t keyid,
1413 char __user *buffer,
1416 struct key *key, *instkey;
1421 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1422 if (IS_ERR(key_ref)) {
1423 if (PTR_ERR(key_ref) != -EACCES)
1424 return PTR_ERR(key_ref);
1426 /* viewing a key under construction is also permitted if we
1427 * have the authorisation token handy */
1428 instkey = key_get_instantiation_authkey(keyid);
1429 if (IS_ERR(instkey))
1430 return PTR_ERR(instkey);
1433 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1434 if (IS_ERR(key_ref))
1435 return PTR_ERR(key_ref);
1438 key = key_ref_to_ptr(key_ref);
1439 ret = security_key_getsecurity(key, &context);
1441 /* if no information was returned, give userspace an empty
1444 if (buffer && buflen > 0 &&
1445 copy_to_user(buffer, "", 1) != 0)
1447 } else if (ret > 0) {
1448 /* return as much data as there's room for */
1449 if (buffer && buflen > 0) {
1453 if (copy_to_user(buffer, context, buflen) != 0)
1460 key_ref_put(key_ref);
1465 * Attempt to install the calling process's session keyring on the process's
1468 * The keyring must exist and must grant the caller LINK permission, and the
1469 * parent process must be single-threaded and must have the same effective
1470 * ownership as this process and mustn't be SUID/SGID.
1472 * The keyring will be emplaced on the parent when it next resumes userspace.
1474 * If successful, 0 will be returned.
1476 long keyctl_session_to_parent(void)
1478 struct task_struct *me, *parent;
1479 const struct cred *mycred, *pcred;
1480 struct callback_head *newwork, *oldwork;
1481 key_ref_t keyring_r;
1485 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1486 if (IS_ERR(keyring_r))
1487 return PTR_ERR(keyring_r);
1491 /* our parent is going to need a new cred struct, a new tgcred struct
1492 * and new security data, so we allocate them here to prevent ENOMEM in
1494 cred = cred_alloc_blank();
1497 newwork = &cred->rcu;
1499 cred->session_keyring = key_ref_to_ptr(keyring_r);
1501 init_task_work(newwork, key_change_session_keyring);
1505 write_lock_irq(&tasklist_lock);
1509 parent = me->real_parent;
1511 /* the parent mustn't be init and mustn't be a kernel thread */
1512 if (parent->pid <= 1 || !parent->mm)
1515 /* the parent must be single threaded */
1516 if (!thread_group_empty(parent))
1519 /* the parent and the child must have different session keyrings or
1520 * there's no point */
1521 mycred = current_cred();
1522 pcred = __task_cred(parent);
1523 if (mycred == pcred ||
1524 mycred->session_keyring == pcred->session_keyring) {
1529 /* the parent must have the same effective ownership and mustn't be
1531 if (!uid_eq(pcred->uid, mycred->euid) ||
1532 !uid_eq(pcred->euid, mycred->euid) ||
1533 !uid_eq(pcred->suid, mycred->euid) ||
1534 !gid_eq(pcred->gid, mycred->egid) ||
1535 !gid_eq(pcred->egid, mycred->egid) ||
1536 !gid_eq(pcred->sgid, mycred->egid))
1539 /* the keyrings must have the same UID */
1540 if ((pcred->session_keyring &&
1541 !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
1542 !uid_eq(mycred->session_keyring->uid, mycred->euid))
1545 /* cancel an already pending keyring replacement */
1546 oldwork = task_work_cancel(parent, key_change_session_keyring);
1548 /* the replacement session keyring is applied just prior to userspace
1550 ret = task_work_add(parent, newwork, true);
1554 write_unlock_irq(&tasklist_lock);
1557 put_cred(container_of(oldwork, struct cred, rcu));
1563 key_ref_put(keyring_r);
1568 * The key control system call
1570 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1571 unsigned long, arg4, unsigned long, arg5)
1574 case KEYCTL_GET_KEYRING_ID:
1575 return keyctl_get_keyring_ID((key_serial_t) arg2,
1578 case KEYCTL_JOIN_SESSION_KEYRING:
1579 return keyctl_join_session_keyring((const char __user *) arg2);
1582 return keyctl_update_key((key_serial_t) arg2,
1583 (const void __user *) arg3,
1587 return keyctl_revoke_key((key_serial_t) arg2);
1589 case KEYCTL_DESCRIBE:
1590 return keyctl_describe_key((key_serial_t) arg2,
1591 (char __user *) arg3,
1595 return keyctl_keyring_clear((key_serial_t) arg2);
1598 return keyctl_keyring_link((key_serial_t) arg2,
1599 (key_serial_t) arg3);
1602 return keyctl_keyring_unlink((key_serial_t) arg2,
1603 (key_serial_t) arg3);
1606 return keyctl_keyring_search((key_serial_t) arg2,
1607 (const char __user *) arg3,
1608 (const char __user *) arg4,
1609 (key_serial_t) arg5);
1612 return keyctl_read_key((key_serial_t) arg2,
1613 (char __user *) arg3,
1617 return keyctl_chown_key((key_serial_t) arg2,
1621 case KEYCTL_SETPERM:
1622 return keyctl_setperm_key((key_serial_t) arg2,
1625 case KEYCTL_INSTANTIATE:
1626 return keyctl_instantiate_key((key_serial_t) arg2,
1627 (const void __user *) arg3,
1629 (key_serial_t) arg5);
1632 return keyctl_negate_key((key_serial_t) arg2,
1634 (key_serial_t) arg4);
1636 case KEYCTL_SET_REQKEY_KEYRING:
1637 return keyctl_set_reqkey_keyring(arg2);
1639 case KEYCTL_SET_TIMEOUT:
1640 return keyctl_set_timeout((key_serial_t) arg2,
1643 case KEYCTL_ASSUME_AUTHORITY:
1644 return keyctl_assume_authority((key_serial_t) arg2);
1646 case KEYCTL_GET_SECURITY:
1647 return keyctl_get_security((key_serial_t) arg2,
1648 (char __user *) arg3,
1651 case KEYCTL_SESSION_TO_PARENT:
1652 return keyctl_session_to_parent();
1655 return keyctl_reject_key((key_serial_t) arg2,
1658 (key_serial_t) arg5);
1660 case KEYCTL_INSTANTIATE_IOV:
1661 return keyctl_instantiate_key_iov(
1662 (key_serial_t) arg2,
1663 (const struct iovec __user *) arg3,
1665 (key_serial_t) arg5);
1667 case KEYCTL_INVALIDATE:
1668 return keyctl_invalidate_key((key_serial_t) arg2);