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/keyctl.h>
19 #include <linux/capability.h>
20 #include <linux/string.h>
21 #include <linux/err.h>
22 #include <linux/vmalloc.h>
23 #include <linux/security.h>
24 #include <asm/uaccess.h>
27 static int key_get_type_from_user(char *type,
28 const char __user *_type,
33 ret = strncpy_from_user(type, _type, len);
36 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 * The keyring must be writable so that we can attach the key to it.
50 * If successful, the new key's serial number is returned, otherwise an error
53 SYSCALL_DEFINE5(add_key, const char __user *, _type,
54 const char __user *, _description,
55 const void __user *, _payload,
59 key_ref_t keyring_ref, key_ref;
60 char type[32], *description;
66 if (plen > 1024 * 1024 - 1)
69 /* draw all the data into kernel space */
70 ret = key_get_type_from_user(type, _type, sizeof(type));
74 description = strndup_user(_description, PAGE_SIZE);
75 if (IS_ERR(description)) {
76 ret = PTR_ERR(description);
78 } else if ((description[0] == '.') &&
79 (strncmp(type, "keyring", 7) == 0)) {
84 /* pull the payload in if one was supplied */
90 payload = kmalloc(plen, GFP_KERNEL);
92 if (plen <= PAGE_SIZE)
95 payload = vmalloc(plen);
101 if (copy_from_user(payload, _payload, plen) != 0)
105 /* find the target keyring (which must be writable) */
106 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
107 if (IS_ERR(keyring_ref)) {
108 ret = PTR_ERR(keyring_ref);
112 /* create or update the requested key and add it to the target
114 key_ref = key_create_or_update(keyring_ref, type, description,
115 payload, plen, KEY_PERM_UNDEF,
117 if (!IS_ERR(key_ref)) {
118 ret = key_ref_to_ptr(key_ref)->serial;
119 key_ref_put(key_ref);
122 ret = PTR_ERR(key_ref);
125 key_ref_put(keyring_ref);
138 * Search the process keyrings and keyring trees linked from those for a
139 * matching key. Keyrings must have appropriate Search permission to be
142 * If a key is found, it will be attached to the destination keyring if there's
143 * one specified and the serial number of the key will be returned.
145 * If no key is found, /sbin/request-key will be invoked if _callout_info is
146 * non-NULL in an attempt to create a key. The _callout_info string will be
147 * passed to /sbin/request-key to aid with completing the request. If the
148 * _callout_info string is "" then it will be changed to "-".
150 SYSCALL_DEFINE4(request_key, const char __user *, _type,
151 const char __user *, _description,
152 const char __user *, _callout_info,
153 key_serial_t, destringid)
155 struct key_type *ktype;
159 char type[32], *description, *callout_info;
162 /* pull the type into kernel space */
163 ret = key_get_type_from_user(type, _type, sizeof(type));
167 /* pull the description into kernel space */
168 description = strndup_user(_description, PAGE_SIZE);
169 if (IS_ERR(description)) {
170 ret = PTR_ERR(description);
174 /* pull the callout info into kernel space */
178 callout_info = strndup_user(_callout_info, PAGE_SIZE);
179 if (IS_ERR(callout_info)) {
180 ret = PTR_ERR(callout_info);
183 callout_len = strlen(callout_info);
186 /* get the destination keyring if specified */
189 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
191 if (IS_ERR(dest_ref)) {
192 ret = PTR_ERR(dest_ref);
197 /* find the key type */
198 ktype = key_type_lookup(type);
200 ret = PTR_ERR(ktype);
205 key = request_key_and_link(ktype, description, callout_info,
206 callout_len, NULL, key_ref_to_ptr(dest_ref),
213 /* wait for the key to finish being constructed */
214 ret = wait_for_key_construction(key, 1);
225 key_ref_put(dest_ref);
235 * Get the ID of the specified process keyring.
237 * The requested keyring must have search permission to be found.
239 * If successful, the ID of the requested keyring will be returned.
241 long keyctl_get_keyring_ID(key_serial_t id, int create)
244 unsigned long lflags;
247 lflags = create ? KEY_LOOKUP_CREATE : 0;
248 key_ref = lookup_user_key(id, lflags, KEY_SEARCH);
249 if (IS_ERR(key_ref)) {
250 ret = PTR_ERR(key_ref);
254 ret = key_ref_to_ptr(key_ref)->serial;
255 key_ref_put(key_ref);
261 * Join a (named) session keyring.
263 * Create and join an anonymous session keyring or join a named session
264 * keyring, creating it if necessary. A named session keyring must have Search
265 * permission for it to be joined. Session keyrings without this permit will
266 * be skipped over. It is not permitted for userspace to create or join
267 * keyrings whose name begin with a dot.
269 * If successful, the ID of the joined session keyring will be returned.
271 long keyctl_join_session_keyring(const char __user *_name)
276 /* fetch the name from userspace */
279 name = strndup_user(_name, PAGE_SIZE);
290 /* join the session */
291 ret = join_session_keyring(name);
299 * Update a key's data payload from the given data.
301 * The key must grant the caller Write permission and the key type must support
302 * updating for this to work. A negative key can be positively instantiated
305 * If successful, 0 will be returned. If the key type does not support
306 * updating, then -EOPNOTSUPP will be returned.
308 long keyctl_update_key(key_serial_t id,
309 const void __user *_payload,
317 if (plen > PAGE_SIZE)
320 /* pull the payload in if one was supplied */
324 payload = kmalloc(plen, GFP_KERNEL);
329 if (copy_from_user(payload, _payload, plen) != 0)
333 /* find the target key (which must be writable) */
334 key_ref = lookup_user_key(id, 0, KEY_WRITE);
335 if (IS_ERR(key_ref)) {
336 ret = PTR_ERR(key_ref);
341 ret = key_update(key_ref, payload, plen);
343 key_ref_put(key_ref);
353 * The key must be grant the caller Write or Setattr permission for this to
354 * work. The key type should give up its quota claim when revoked. The key
355 * and any links to the key will be automatically garbage collected after a
356 * certain amount of time (/proc/sys/kernel/keys/gc_delay).
358 * If successful, 0 is returned.
360 long keyctl_revoke_key(key_serial_t id)
365 key_ref = lookup_user_key(id, 0, KEY_WRITE);
366 if (IS_ERR(key_ref)) {
367 ret = PTR_ERR(key_ref);
370 key_ref = lookup_user_key(id, 0, KEY_SETATTR);
371 if (IS_ERR(key_ref)) {
372 ret = PTR_ERR(key_ref);
377 key_revoke(key_ref_to_ptr(key_ref));
380 key_ref_put(key_ref);
386 * Clear the specified keyring, creating an empty process keyring if one of the
387 * special keyring IDs is used.
389 * The keyring must grant the caller Write permission for this to work. If
390 * successful, 0 will be returned.
392 long keyctl_keyring_clear(key_serial_t ringid)
394 key_ref_t keyring_ref;
397 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
398 if (IS_ERR(keyring_ref)) {
399 ret = PTR_ERR(keyring_ref);
403 ret = keyring_clear(key_ref_to_ptr(keyring_ref));
405 key_ref_put(keyring_ref);
411 * Create a link from a keyring to a key if there's no matching key in the
412 * keyring, otherwise replace the link to the matching key with a link to the
415 * The key must grant the caller Link permission and the the keyring must grant
416 * the caller Write permission. Furthermore, if an additional link is created,
417 * the keyring's quota will be extended.
419 * If successful, 0 will be returned.
421 long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
423 key_ref_t keyring_ref, key_ref;
426 keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
427 if (IS_ERR(keyring_ref)) {
428 ret = PTR_ERR(keyring_ref);
432 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_LINK);
433 if (IS_ERR(key_ref)) {
434 ret = PTR_ERR(key_ref);
438 ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
440 key_ref_put(key_ref);
442 key_ref_put(keyring_ref);
448 * Unlink a key from a keyring.
450 * The keyring must grant the caller Write permission for this to work; the key
451 * itself need not grant the caller anything. If the last link to a key is
452 * removed then that key will be scheduled for destruction.
454 * If successful, 0 will be returned.
456 long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
458 key_ref_t keyring_ref, key_ref;
461 keyring_ref = lookup_user_key(ringid, 0, KEY_WRITE);
462 if (IS_ERR(keyring_ref)) {
463 ret = PTR_ERR(keyring_ref);
467 key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0);
468 if (IS_ERR(key_ref)) {
469 ret = PTR_ERR(key_ref);
473 ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
475 key_ref_put(key_ref);
477 key_ref_put(keyring_ref);
483 * Return a description of a key to userspace.
485 * The key must grant the caller View permission for this to work.
487 * If there's a buffer, we place up to buflen bytes of data into it formatted
488 * in the following way:
490 * type;uid;gid;perm;description<NUL>
492 * If successful, we return the amount of description available, irrespective
493 * of how much we may have copied into the buffer.
495 long keyctl_describe_key(key_serial_t keyid,
499 struct key *key, *instkey;
504 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
505 if (IS_ERR(key_ref)) {
506 /* viewing a key under construction is permitted if we have the
507 * authorisation token handy */
508 if (PTR_ERR(key_ref) == -EACCES) {
509 instkey = key_get_instantiation_authkey(keyid);
510 if (!IS_ERR(instkey)) {
512 key_ref = lookup_user_key(keyid,
515 if (!IS_ERR(key_ref))
520 ret = PTR_ERR(key_ref);
525 /* calculate how much description we're going to return */
527 tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
531 key = key_ref_to_ptr(key_ref);
533 ret = snprintf(tmpbuf, PAGE_SIZE - 1,
539 key->description ?: "");
541 /* include a NUL char at the end of the data */
542 if (ret > PAGE_SIZE - 1)
547 /* consider returning the data */
548 if (buffer && buflen > 0) {
552 if (copy_to_user(buffer, tmpbuf, buflen) != 0)
558 key_ref_put(key_ref);
564 * Search the specified keyring and any keyrings it links to for a matching
565 * key. Only keyrings that grant the caller Search permission will be searched
566 * (this includes the starting keyring). Only keys with Search permission can
569 * If successful, the found key will be linked to the destination keyring if
570 * supplied and the key has Link permission, and the found key ID will be
573 long keyctl_keyring_search(key_serial_t ringid,
574 const char __user *_type,
575 const char __user *_description,
576 key_serial_t destringid)
578 struct key_type *ktype;
579 key_ref_t keyring_ref, key_ref, dest_ref;
580 char type[32], *description;
583 /* pull the type and description into kernel space */
584 ret = key_get_type_from_user(type, _type, sizeof(type));
588 description = strndup_user(_description, PAGE_SIZE);
589 if (IS_ERR(description)) {
590 ret = PTR_ERR(description);
594 /* get the keyring at which to begin the search */
595 keyring_ref = lookup_user_key(ringid, 0, KEY_SEARCH);
596 if (IS_ERR(keyring_ref)) {
597 ret = PTR_ERR(keyring_ref);
601 /* get the destination keyring if specified */
604 dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
606 if (IS_ERR(dest_ref)) {
607 ret = PTR_ERR(dest_ref);
612 /* find the key type */
613 ktype = key_type_lookup(type);
615 ret = PTR_ERR(ktype);
620 key_ref = keyring_search(keyring_ref, ktype, description);
621 if (IS_ERR(key_ref)) {
622 ret = PTR_ERR(key_ref);
624 /* treat lack or presence of a negative key the same */
630 /* link the resulting key to the destination keyring if we can */
632 ret = key_permission(key_ref, KEY_LINK);
636 ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
641 ret = key_ref_to_ptr(key_ref)->serial;
644 key_ref_put(key_ref);
648 key_ref_put(dest_ref);
650 key_ref_put(keyring_ref);
658 * Read a key's payload.
660 * The key must either grant the caller Read permission, or it must grant the
661 * caller Search permission when searched for from the process keyrings.
663 * If successful, we place up to buflen bytes of data into the buffer, if one
664 * is provided, and return the amount of data that is available in the key,
665 * irrespective of how much we copied into the buffer.
667 long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
673 /* find the key first */
674 key_ref = lookup_user_key(keyid, 0, 0);
675 if (IS_ERR(key_ref)) {
680 key = key_ref_to_ptr(key_ref);
682 /* see if we can read it directly */
683 ret = key_permission(key_ref, KEY_READ);
689 /* we can't; see if it's searchable from this process's keyrings
690 * - we automatically take account of the fact that it may be
691 * dangling off an instantiation key
693 if (!is_key_possessed(key_ref)) {
698 /* the key is probably readable - now try to read it */
701 if (key->type->read) {
702 /* Read the data with the semaphore held (since we might sleep)
703 * to protect against the key being updated or revoked.
705 down_read(&key->sem);
706 ret = key_validate(key);
708 ret = key->type->read(key, buffer, buflen);
719 * Change the ownership of a key
721 * The key must grant the caller Setattr permission for this to work, though
722 * the key need not be fully instantiated yet. For the UID to be changed, or
723 * for the GID to be changed to a group the caller is not a member of, the
724 * caller must have sysadmin capability. If either uid or gid is -1 then that
725 * attribute is not changed.
727 * If the UID is to be changed, the new user must have sufficient quota to
728 * accept the key. The quota deduction will be removed from the old user to
729 * the new user should the attribute be changed.
731 * If successful, 0 will be returned.
733 long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid)
735 struct key_user *newowner, *zapowner = NULL;
741 if (uid == (uid_t) -1 && gid == (gid_t) -1)
744 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
746 if (IS_ERR(key_ref)) {
747 ret = PTR_ERR(key_ref);
751 key = key_ref_to_ptr(key_ref);
753 /* make the changes with the locks held to prevent chown/chown races */
755 down_write(&key->sem);
757 if (!capable(CAP_SYS_ADMIN)) {
758 /* only the sysadmin can chown a key to some other UID */
759 if (uid != (uid_t) -1 && key->uid != uid)
762 /* only the sysadmin can set the key's GID to a group other
763 * than one of those that the current process subscribes to */
764 if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid))
769 if (uid != (uid_t) -1 && uid != key->uid) {
771 newowner = key_user_lookup(uid, current_user_ns());
775 /* transfer the quota burden to the new user */
776 if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
777 unsigned maxkeys = (uid == 0) ?
778 key_quota_root_maxkeys : key_quota_maxkeys;
779 unsigned maxbytes = (uid == 0) ?
780 key_quota_root_maxbytes : key_quota_maxbytes;
782 spin_lock(&newowner->lock);
783 if (newowner->qnkeys + 1 >= maxkeys ||
784 newowner->qnbytes + key->quotalen >= maxbytes ||
785 newowner->qnbytes + key->quotalen <
790 newowner->qnbytes += key->quotalen;
791 spin_unlock(&newowner->lock);
793 spin_lock(&key->user->lock);
795 key->user->qnbytes -= key->quotalen;
796 spin_unlock(&key->user->lock);
799 atomic_dec(&key->user->nkeys);
800 atomic_inc(&newowner->nkeys);
802 if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) {
803 atomic_dec(&key->user->nikeys);
804 atomic_inc(&newowner->nikeys);
807 zapowner = key->user;
808 key->user = newowner;
813 if (gid != (gid_t) -1)
822 key_user_put(zapowner);
827 spin_unlock(&newowner->lock);
834 * Change the permission mask on a key.
836 * The key must grant the caller Setattr permission for this to work, though
837 * the key need not be fully instantiated yet. If the caller does not have
838 * sysadmin capability, it may only change the permission on keys that it owns.
840 long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
847 if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
850 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
852 if (IS_ERR(key_ref)) {
853 ret = PTR_ERR(key_ref);
857 key = key_ref_to_ptr(key_ref);
859 /* make the changes with the locks held to prevent chown/chmod races */
861 down_write(&key->sem);
863 /* if we're not the sysadmin, we can only change a key that we own */
864 if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) {
876 * Get the destination keyring for instantiation and check that the caller has
877 * Write permission on it.
879 static long get_instantiation_keyring(key_serial_t ringid,
880 struct request_key_auth *rka,
881 struct key **_dest_keyring)
885 *_dest_keyring = NULL;
887 /* just return a NULL pointer if we weren't asked to make a link */
891 /* if a specific keyring is nominated by ID, then use that */
893 dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_WRITE);
895 return PTR_ERR(dkref);
896 *_dest_keyring = key_ref_to_ptr(dkref);
900 if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
903 /* otherwise specify the destination keyring recorded in the
904 * authorisation key (any KEY_SPEC_*_KEYRING) */
905 if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
906 *_dest_keyring = key_get(rka->dest_keyring);
914 * Change the request_key authorisation key on the current process.
916 static int keyctl_change_reqkey_auth(struct key *key)
920 new = prepare_creds();
924 key_put(new->request_key_auth);
925 new->request_key_auth = key_get(key);
927 return commit_creds(new);
931 * Copy the iovec data from userspace
933 static long copy_from_user_iovec(void *buffer, const struct iovec *iov,
936 for (; ioc > 0; ioc--) {
937 if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0)
939 buffer += iov->iov_len;
946 * Instantiate a key with the specified payload and link the key into the
947 * destination keyring if one is given.
949 * The caller must have the appropriate instantiation permit set for this to
950 * work (see keyctl_assume_authority). No other permissions are required.
952 * If successful, 0 will be returned.
954 long keyctl_instantiate_key_common(key_serial_t id,
955 const struct iovec *payload_iov,
960 const struct cred *cred = current_cred();
961 struct request_key_auth *rka;
962 struct key *instkey, *dest_keyring;
967 kenter("%d,,%zu,%d", id, plen, ringid);
970 if (plen > 1024 * 1024 - 1)
973 /* the appropriate instantiation authorisation key must have been
974 * assumed before calling this */
976 instkey = cred->request_key_auth;
980 rka = instkey->payload.data;
981 if (rka->target_key->serial != id)
984 /* pull the payload in if one was supplied */
989 payload = kmalloc(plen, GFP_KERNEL);
991 if (plen <= PAGE_SIZE)
994 payload = vmalloc(plen);
999 ret = copy_from_user_iovec(payload, payload_iov, ioc);
1004 /* find the destination keyring amongst those belonging to the
1005 * requesting task */
1006 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1010 /* instantiate the key and link it into a keyring */
1011 ret = key_instantiate_and_link(rka->target_key, payload, plen,
1012 dest_keyring, instkey);
1014 key_put(dest_keyring);
1016 /* discard the assumed authority if it's just been disabled by
1017 * instantiation of the key */
1019 keyctl_change_reqkey_auth(NULL);
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(key_serial_t id,
1040 const void __user *_payload,
1042 key_serial_t ringid)
1044 if (_payload && plen) {
1045 struct iovec iov[1] = {
1046 [0].iov_base = (void __user *)_payload,
1050 return keyctl_instantiate_key_common(id, iov, 1, plen, ringid);
1053 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1057 * Instantiate a key with the specified multipart payload and link the key into
1058 * the destination keyring if one is given.
1060 * The caller must have the appropriate instantiation permit set for this to
1061 * work (see keyctl_assume_authority). No other permissions are required.
1063 * If successful, 0 will be returned.
1065 long keyctl_instantiate_key_iov(key_serial_t id,
1066 const struct iovec __user *_payload_iov,
1068 key_serial_t ringid)
1070 struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
1073 if (_payload_iov == 0 || ioc == 0)
1076 ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc,
1077 ARRAY_SIZE(iovstack), iovstack, &iov, 1);
1081 goto no_payload_free;
1083 ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid);
1085 if (iov != iovstack)
1090 if (iov != iovstack)
1093 return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid);
1097 * Negatively instantiate the key with the given timeout (in seconds) and link
1098 * the key into the destination keyring if one is given.
1100 * The caller must have the appropriate instantiation permit set for this to
1101 * work (see keyctl_assume_authority). No other permissions are required.
1103 * The key and any links to the key will be automatically garbage collected
1104 * after the timeout expires.
1106 * Negative keys are used to rate limit repeated request_key() calls by causing
1107 * them to return -ENOKEY until the negative key expires.
1109 * If successful, 0 will be returned.
1111 long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
1113 return keyctl_reject_key(id, timeout, ENOKEY, ringid);
1117 * Negatively instantiate the key with the given timeout (in seconds) and error
1118 * code and link the key into the destination keyring if one is given.
1120 * The caller must have the appropriate instantiation permit set for this to
1121 * work (see keyctl_assume_authority). No other permissions are required.
1123 * The key and any links to the key will be automatically garbage collected
1124 * after the timeout expires.
1126 * Negative keys are used to rate limit repeated request_key() calls by causing
1127 * them to return the specified error code until the negative key expires.
1129 * If successful, 0 will be returned.
1131 long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
1132 key_serial_t ringid)
1134 const struct cred *cred = current_cred();
1135 struct request_key_auth *rka;
1136 struct key *instkey, *dest_keyring;
1139 kenter("%d,%u,%u,%d", id, timeout, error, ringid);
1141 /* must be a valid error code and mustn't be a kernel special */
1143 error >= MAX_ERRNO ||
1144 error == ERESTARTSYS ||
1145 error == ERESTARTNOINTR ||
1146 error == ERESTARTNOHAND ||
1147 error == ERESTART_RESTARTBLOCK)
1150 /* the appropriate instantiation authorisation key must have been
1151 * assumed before calling this */
1153 instkey = cred->request_key_auth;
1157 rka = instkey->payload.data;
1158 if (rka->target_key->serial != id)
1161 /* find the destination keyring if present (which must also be
1163 ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
1167 /* instantiate the key and link it into a keyring */
1168 ret = key_reject_and_link(rka->target_key, timeout, error,
1169 dest_keyring, instkey);
1171 key_put(dest_keyring);
1173 /* discard the assumed authority if it's just been disabled by
1174 * instantiation of the key */
1176 keyctl_change_reqkey_auth(NULL);
1183 * Read or set the default keyring in which request_key() will cache keys and
1184 * return the old setting.
1186 * If a thread or process keyring is specified then it will be created if it
1187 * doesn't yet exist. The old setting will be returned if successful.
1189 long keyctl_set_reqkey_keyring(int reqkey_defl)
1192 int ret, old_setting;
1194 old_setting = current_cred_xxx(jit_keyring);
1196 if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
1199 new = prepare_creds();
1203 switch (reqkey_defl) {
1204 case KEY_REQKEY_DEFL_THREAD_KEYRING:
1205 ret = install_thread_keyring_to_cred(new);
1210 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
1211 ret = install_process_keyring_to_cred(new);
1216 case KEY_REQKEY_DEFL_DEFAULT:
1217 case KEY_REQKEY_DEFL_SESSION_KEYRING:
1218 case KEY_REQKEY_DEFL_USER_KEYRING:
1219 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
1220 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
1223 case KEY_REQKEY_DEFL_NO_CHANGE:
1224 case KEY_REQKEY_DEFL_GROUP_KEYRING:
1231 new->jit_keyring = reqkey_defl;
1240 * Set or clear the timeout on a key.
1242 * Either the key must grant the caller Setattr permission or else the caller
1243 * must hold an instantiation authorisation token for the key.
1245 * The timeout is either 0 to clear the timeout, or a number of seconds from
1246 * the current time. The key and any links to the key will be automatically
1247 * garbage collected after the timeout expires.
1249 * If successful, 0 is returned.
1251 long keyctl_set_timeout(key_serial_t id, unsigned timeout)
1253 struct timespec now;
1254 struct key *key, *instkey;
1259 key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
1261 if (IS_ERR(key_ref)) {
1262 /* setting the timeout on a key under construction is permitted
1263 * if we have the authorisation token handy */
1264 if (PTR_ERR(key_ref) == -EACCES) {
1265 instkey = key_get_instantiation_authkey(id);
1266 if (!IS_ERR(instkey)) {
1268 key_ref = lookup_user_key(id,
1271 if (!IS_ERR(key_ref))
1276 ret = PTR_ERR(key_ref);
1281 key = key_ref_to_ptr(key_ref);
1283 /* make the changes with the locks held to prevent races */
1284 down_write(&key->sem);
1288 now = current_kernel_time();
1289 expiry = now.tv_sec + timeout;
1292 key->expiry = expiry;
1293 key_schedule_gc(key->expiry + key_gc_delay);
1295 up_write(&key->sem);
1304 * Assume (or clear) the authority to instantiate the specified key.
1306 * This sets the authoritative token currently in force for key instantiation.
1307 * This must be done for a key to be instantiated. It has the effect of making
1308 * available all the keys from the caller of the request_key() that created a
1309 * key to request_key() calls made by the caller of this function.
1311 * The caller must have the instantiation key in their process keyrings with a
1312 * Search permission grant available to the caller.
1314 * If the ID given is 0, then the setting will be cleared and 0 returned.
1316 * If the ID given has a matching an authorisation key, then that key will be
1317 * set and its ID will be returned. The authorisation key can be read to get
1318 * the callout information passed to request_key().
1320 long keyctl_assume_authority(key_serial_t id)
1322 struct key *authkey;
1325 /* special key IDs aren't permitted */
1330 /* we divest ourselves of authority if given an ID of 0 */
1332 ret = keyctl_change_reqkey_auth(NULL);
1336 /* attempt to assume the authority temporarily granted to us whilst we
1337 * instantiate the specified key
1338 * - the authorisation key must be in the current task's keyrings
1341 authkey = key_get_instantiation_authkey(id);
1342 if (IS_ERR(authkey)) {
1343 ret = PTR_ERR(authkey);
1347 ret = keyctl_change_reqkey_auth(authkey);
1349 ret = authkey->serial;
1356 * Get a key's the LSM security label.
1358 * The key must grant the caller View permission for this to work.
1360 * If there's a buffer, then up to buflen bytes of data will be placed into it.
1362 * If successful, the amount of information available will be returned,
1363 * irrespective of how much was copied (including the terminal NUL).
1365 long keyctl_get_security(key_serial_t keyid,
1366 char __user *buffer,
1369 struct key *key, *instkey;
1374 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_VIEW);
1375 if (IS_ERR(key_ref)) {
1376 if (PTR_ERR(key_ref) != -EACCES)
1377 return PTR_ERR(key_ref);
1379 /* viewing a key under construction is also permitted if we
1380 * have the authorisation token handy */
1381 instkey = key_get_instantiation_authkey(keyid);
1382 if (IS_ERR(instkey))
1383 return PTR_ERR(instkey);
1386 key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0);
1387 if (IS_ERR(key_ref))
1388 return PTR_ERR(key_ref);
1391 key = key_ref_to_ptr(key_ref);
1392 ret = security_key_getsecurity(key, &context);
1394 /* if no information was returned, give userspace an empty
1397 if (buffer && buflen > 0 &&
1398 copy_to_user(buffer, "", 1) != 0)
1400 } else if (ret > 0) {
1401 /* return as much data as there's room for */
1402 if (buffer && buflen > 0) {
1406 if (copy_to_user(buffer, context, buflen) != 0)
1413 key_ref_put(key_ref);
1418 * Attempt to install the calling process's session keyring on the process's
1421 * The keyring must exist and must grant the caller LINK permission, and the
1422 * parent process must be single-threaded and must have the same effective
1423 * ownership as this process and mustn't be SUID/SGID.
1425 * The keyring will be emplaced on the parent when it next resumes userspace.
1427 * If successful, 0 will be returned.
1429 long keyctl_session_to_parent(void)
1431 #ifdef TIF_NOTIFY_RESUME
1432 struct task_struct *me, *parent;
1433 const struct cred *mycred, *pcred;
1434 struct cred *cred, *oldcred;
1435 key_ref_t keyring_r;
1438 keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_LINK);
1439 if (IS_ERR(keyring_r))
1440 return PTR_ERR(keyring_r);
1442 /* our parent is going to need a new cred struct, a new tgcred struct
1443 * and new security data, so we allocate them here to prevent ENOMEM in
1446 cred = cred_alloc_blank();
1450 cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r);
1455 write_lock_irq(&tasklist_lock);
1457 parent = me->real_parent;
1460 /* the parent mustn't be init and mustn't be a kernel thread */
1461 if (parent->pid <= 1 || !parent->mm)
1464 /* the parent must be single threaded */
1465 if (!thread_group_empty(parent))
1468 /* the parent and the child must have different session keyrings or
1469 * there's no point */
1470 mycred = current_cred();
1471 pcred = __task_cred(parent);
1472 if (mycred == pcred ||
1473 mycred->tgcred->session_keyring == pcred->tgcred->session_keyring)
1476 /* the parent must have the same effective ownership and mustn't be
1478 if (pcred->uid != mycred->euid ||
1479 pcred->euid != mycred->euid ||
1480 pcred->suid != mycred->euid ||
1481 pcred->gid != mycred->egid ||
1482 pcred->egid != mycred->egid ||
1483 pcred->sgid != mycred->egid)
1486 /* the keyrings must have the same UID */
1487 if ((pcred->tgcred->session_keyring &&
1488 pcred->tgcred->session_keyring->uid != mycred->euid) ||
1489 mycred->tgcred->session_keyring->uid != mycred->euid)
1492 /* if there's an already pending keyring replacement, then we replace
1494 oldcred = parent->replacement_session_keyring;
1496 /* the replacement session keyring is applied just prior to userspace
1498 parent->replacement_session_keyring = cred;
1500 set_ti_thread_flag(task_thread_info(parent), TIF_NOTIFY_RESUME);
1502 write_unlock_irq(&tasklist_lock);
1511 write_unlock_irq(&tasklist_lock);
1517 key_ref_put(keyring_r);
1520 #else /* !TIF_NOTIFY_RESUME */
1522 * To be removed when TIF_NOTIFY_RESUME has been implemented on
1525 #warning TIF_NOTIFY_RESUME not implemented
1527 #endif /* !TIF_NOTIFY_RESUME */
1531 * The key control system call
1533 SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
1534 unsigned long, arg4, unsigned long, arg5)
1537 case KEYCTL_GET_KEYRING_ID:
1538 return keyctl_get_keyring_ID((key_serial_t) arg2,
1541 case KEYCTL_JOIN_SESSION_KEYRING:
1542 return keyctl_join_session_keyring((const char __user *) arg2);
1545 return keyctl_update_key((key_serial_t) arg2,
1546 (const void __user *) arg3,
1550 return keyctl_revoke_key((key_serial_t) arg2);
1552 case KEYCTL_DESCRIBE:
1553 return keyctl_describe_key((key_serial_t) arg2,
1554 (char __user *) arg3,
1558 return keyctl_keyring_clear((key_serial_t) arg2);
1561 return keyctl_keyring_link((key_serial_t) arg2,
1562 (key_serial_t) arg3);
1565 return keyctl_keyring_unlink((key_serial_t) arg2,
1566 (key_serial_t) arg3);
1569 return keyctl_keyring_search((key_serial_t) arg2,
1570 (const char __user *) arg3,
1571 (const char __user *) arg4,
1572 (key_serial_t) arg5);
1575 return keyctl_read_key((key_serial_t) arg2,
1576 (char __user *) arg3,
1580 return keyctl_chown_key((key_serial_t) arg2,
1584 case KEYCTL_SETPERM:
1585 return keyctl_setperm_key((key_serial_t) arg2,
1588 case KEYCTL_INSTANTIATE:
1589 return keyctl_instantiate_key((key_serial_t) arg2,
1590 (const void __user *) arg3,
1592 (key_serial_t) arg5);
1595 return keyctl_negate_key((key_serial_t) arg2,
1597 (key_serial_t) arg4);
1599 case KEYCTL_SET_REQKEY_KEYRING:
1600 return keyctl_set_reqkey_keyring(arg2);
1602 case KEYCTL_SET_TIMEOUT:
1603 return keyctl_set_timeout((key_serial_t) arg2,
1606 case KEYCTL_ASSUME_AUTHORITY:
1607 return keyctl_assume_authority((key_serial_t) arg2);
1609 case KEYCTL_GET_SECURITY:
1610 return keyctl_get_security((key_serial_t) arg2,
1611 (char __user *) arg3,
1614 case KEYCTL_SESSION_TO_PARENT:
1615 return keyctl_session_to_parent();
1618 return keyctl_reject_key((key_serial_t) arg2,
1621 (key_serial_t) arg5);
1623 case KEYCTL_INSTANTIATE_IOV:
1624 return keyctl_instantiate_key_iov(
1625 (key_serial_t) arg2,
1626 (const struct iovec __user *) arg3,
1628 (key_serial_t) arg5);