^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Userspace key control operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Written by David Howells (dhowells@redhat.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/sched/task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/key.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/keyctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/vmalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/security.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/uio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <keys/request_key_auth-type.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define KEY_MAX_DESC_SIZE 4096
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static const unsigned char keyrings_capabilities[2] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) [0] = (KEYCTL_CAPS0_CAPABILITIES |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) KEYCTL_CAPS0_INVALIDATE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) KEYCTL_CAPS0_RESTRICT_KEYRING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) KEYCTL_CAPS0_MOVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) [1] = (KEYCTL_CAPS1_NS_KEYRING_NAME |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) KEYCTL_CAPS1_NS_KEY_TAG |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) (IS_ENABLED(CONFIG_KEY_NOTIFICATIONS) ? KEYCTL_CAPS1_NOTIFICATIONS : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int key_get_type_from_user(char *type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) const char __user *_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) ret = strncpy_from_user(type, _type, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (ret == 0 || ret >= len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (type[0] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) type[len - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * Extract the description of a new key from userspace and either add it as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) * new key to the specified keyring or update a matching key in that keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * If the description is NULL or an empty string, the key type is asked to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * generate one from the payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * The keyring must be writable so that we can attach the key to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * If successful, the new key's serial number is returned, otherwise an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * code is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) SYSCALL_DEFINE5(add_key, const char __user *, _type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) const char __user *, _description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) const void __user *, _payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) size_t, plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) key_serial_t, ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) key_ref_t keyring_ref, key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) char type[32], *description;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) void *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (plen > 1024 * 1024 - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* draw all the data into kernel space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = key_get_type_from_user(type, _type, sizeof(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) description = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (_description) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) description = strndup_user(_description, KEY_MAX_DESC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (IS_ERR(description)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) ret = PTR_ERR(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (!*description) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kfree(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) description = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) } else if ((description[0] == '.') &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) (strncmp(type, "keyring", 7) == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* pull the payload in if one was supplied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) payload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) payload = kvmalloc(plen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (copy_from_user(payload, _payload, plen) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /* find the target keyring (which must be writable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (IS_ERR(keyring_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ret = PTR_ERR(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* create or update the requested key and add it to the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) key_ref = key_create_or_update(keyring_ref, type, description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) payload, plen, KEY_PERM_UNDEF,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) KEY_ALLOC_IN_QUOTA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (!IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) ret = key_ref_to_ptr(key_ref)->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) key_ref_put(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) kvfree_sensitive(payload, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) kfree(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Search the process keyrings and keyring trees linked from those for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * matching key. Keyrings must have appropriate Search permission to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * searched.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * If a key is found, it will be attached to the destination keyring if there's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * one specified and the serial number of the key will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * If no key is found, /sbin/request-key will be invoked if _callout_info is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * non-NULL in an attempt to create a key. The _callout_info string will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * passed to /sbin/request-key to aid with completing the request. If the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * _callout_info string is "" then it will be changed to "-".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) SYSCALL_DEFINE4(request_key, const char __user *, _type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) const char __user *, _description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) const char __user *, _callout_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) key_serial_t, destringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct key_type *ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) key_ref_t dest_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) size_t callout_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) char type[32], *description, *callout_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* pull the type into kernel space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) ret = key_get_type_from_user(type, _type, sizeof(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /* pull the description into kernel space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) description = strndup_user(_description, KEY_MAX_DESC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (IS_ERR(description)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) ret = PTR_ERR(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* pull the callout info into kernel space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) callout_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) callout_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (_callout_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) callout_info = strndup_user(_callout_info, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (IS_ERR(callout_info)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) ret = PTR_ERR(callout_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) callout_len = strlen(callout_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /* get the destination keyring if specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dest_ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (destringid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (IS_ERR(dest_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) ret = PTR_ERR(dest_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /* find the key type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) ktype = key_type_lookup(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (IS_ERR(ktype)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) ret = PTR_ERR(ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) goto error4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* do the search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) key = request_key_and_link(ktype, description, NULL, callout_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) callout_len, NULL, key_ref_to_ptr(dest_ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) KEY_ALLOC_IN_QUOTA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (IS_ERR(key)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) ret = PTR_ERR(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) goto error5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* wait for the key to finish being constructed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) ret = wait_for_key_construction(key, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) goto error6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ret = key->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) error6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) error5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) key_type_put(ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) error4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) key_ref_put(dest_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) kfree(callout_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) kfree(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * Get the ID of the specified process keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * The requested keyring must have search permission to be found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * If successful, the ID of the requested keyring will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) long keyctl_get_keyring_ID(key_serial_t id, int create)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long lflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) lflags = create ? KEY_LOOKUP_CREATE : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = key_ref_to_ptr(key_ref)->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * Join a (named) session keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * Create and join an anonymous session keyring or join a named session
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * keyring, creating it if necessary. A named session keyring must have Search
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * permission for it to be joined. Session keyrings without this permit will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * be skipped over. It is not permitted for userspace to create or join
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) * keyrings whose name begin with a dot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * If successful, the ID of the joined session keyring will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) long keyctl_join_session_keyring(const char __user *_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) /* fetch the name from userspace */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) name = strndup_user(_name, KEY_MAX_DESC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (IS_ERR(name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ret = PTR_ERR(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (name[0] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto error_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /* join the session */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) ret = join_session_keyring(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) error_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) kfree(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * Update a key's data payload from the given data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * The key must grant the caller Write permission and the key type must support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * updating for this to work. A negative key can be positively instantiated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * with this call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * If successful, 0 will be returned. If the key type does not support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * updating, then -EOPNOTSUPP will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) long keyctl_update_key(key_serial_t id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) const void __user *_payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) size_t plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) void *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (plen > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /* pull the payload in if one was supplied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) payload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) if (plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) payload = kvmalloc(plen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (!payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (copy_from_user(payload, _payload, plen) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* find the target key (which must be writable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /* update the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ret = key_update(key_ref, payload, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) kvfree_sensitive(payload, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * Revoke a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * The key must be grant the caller Write or Setattr permission for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * work. The key type should give up its quota claim when revoked. The key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * and any links to the key will be automatically garbage collected after a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) * certain amount of time (/proc/sys/kernel/keys/gc_delay).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) * Keys with KEY_FLAG_KEEP set should not be revoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) * If successful, 0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) long keyctl_revoke_key(key_serial_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (ret != -EACCES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) if (test_bit(KEY_FLAG_KEEP, &key->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) key_revoke(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * Invalidate a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * The key must be grant the caller Invalidate permission for this to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * The key and any links to the key will be automatically garbage collected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * immediately.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * Keys with KEY_FLAG_KEEP set should not be invalidated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) * If successful, 0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) long keyctl_invalidate_key(key_serial_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) kenter("%d", id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /* Root is permitted to invalidate certain special keys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) key_ref = lookup_user_key(id, 0, KEY_SYSADMIN_OVERRIDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (test_bit(KEY_FLAG_ROOT_CAN_INVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) &key_ref_to_ptr(key_ref)->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) goto invalidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) invalidate:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (test_bit(KEY_FLAG_KEEP, &key->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) key_invalidate(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) error_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) kleave(" = %ld", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Clear the specified keyring, creating an empty process keyring if one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) * special keyring IDs is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) * The keyring must grant the caller Write permission and not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * KEY_FLAG_KEEP set for this to work. If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) long keyctl_keyring_clear(key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) key_ref_t keyring_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) struct key *keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (IS_ERR(keyring_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ret = PTR_ERR(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /* Root is permitted to invalidate certain special keyrings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) keyring_ref = lookup_user_key(ringid, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) KEY_SYSADMIN_OVERRIDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (IS_ERR(keyring_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) &key_ref_to_ptr(keyring_ref)->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) goto clear;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) clear:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) keyring = key_ref_to_ptr(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (test_bit(KEY_FLAG_KEEP, &keyring->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) ret = keyring_clear(keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) error_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) key_ref_put(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * Create a link from a keyring to a key if there's no matching key in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * keyring, otherwise replace the link to the matching key with a link to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * new key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * The key must grant the caller Link permission and the the keyring must grant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * the caller Write permission. Furthermore, if an additional link is created,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) * the keyring's quota will be extended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) long keyctl_keyring_link(key_serial_t id, key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) key_ref_t keyring_ref, key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (IS_ERR(keyring_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) ret = PTR_ERR(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) ret = key_link(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) key_ref_put(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * Unlink a key from a keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * The keyring must grant the caller Write permission for this to work; the key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * itself need not grant the caller anything. If the last link to a key is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) * removed then that key will be scheduled for destruction.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) key_ref_t keyring_ref, key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct key *keyring, *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (IS_ERR(keyring_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = PTR_ERR(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) key_ref = lookup_user_key(id, KEY_LOOKUP_PARTIAL, KEY_NEED_UNLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) keyring = key_ref_to_ptr(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (test_bit(KEY_FLAG_KEEP, &keyring->flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) test_bit(KEY_FLAG_KEEP, &key->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) ret = key_unlink(keyring, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) key_ref_put(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) * Move a link to a key from one keyring to another, displacing any matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) * key from the destination keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) * The key must grant the caller Link permission and both keyrings must grant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) * the caller Write permission. There must also be a link in the from keyring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) * to the key. If both keyrings are the same, nothing is done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) key_serial_t to_ringid, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) key_ref_t key_ref, from_ref, to_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (flags & ~KEYCTL_MOVE_EXCL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (IS_ERR(from_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) ret = PTR_ERR(from_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (IS_ERR(to_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) ret = PTR_ERR(to_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) key_ref_to_ptr(to_ref), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) key_ref_put(to_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) key_ref_put(from_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) * Return a description of a key to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) * The key must grant the caller View permission for this to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * If there's a buffer, we place up to buflen bytes of data into it formatted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) * in the following way:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) * type;uid;gid;perm;description<NUL>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) * If successful, we return the amount of description available, irrespective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) * of how much we may have copied into the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) long keyctl_describe_key(key_serial_t keyid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct key *key, *instkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) char *infobuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) int desclen, infolen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) /* viewing a key under construction is permitted if we have the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) * authorisation token handy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) if (PTR_ERR(key_ref) == -EACCES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) instkey = key_get_instantiation_authkey(keyid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!IS_ERR(instkey)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) key_put(instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) key_ref = lookup_user_key(keyid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) KEY_AUTHTOKEN_OVERRIDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) goto okay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) okay:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) desclen = strlen(key->description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) /* calculate how much information we're going to return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) infobuf = kasprintf(GFP_KERNEL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) "%s;%d;%d;%08x;",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) key->type->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) from_kuid_munged(current_user_ns(), key->uid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) from_kgid_munged(current_user_ns(), key->gid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) key->perm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) if (!infobuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) infolen = strlen(infobuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) ret = infolen + desclen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) /* consider returning the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (buffer && buflen >= ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) if (copy_to_user(buffer, infobuf, infolen) != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) copy_to_user(buffer + infolen, key->description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) desclen + 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) kfree(infobuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) * Search the specified keyring and any keyrings it links to for a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) * key. Only keyrings that grant the caller Search permission will be searched
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) * (this includes the starting keyring). Only keys with Search permission can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) * be found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) * If successful, the found key will be linked to the destination keyring if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) * supplied and the key has Link permission, and the found key ID will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) * returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) long keyctl_keyring_search(key_serial_t ringid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) const char __user *_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) const char __user *_description,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) key_serial_t destringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) struct key_type *ktype;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) key_ref_t keyring_ref, key_ref, dest_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) char type[32], *description;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) /* pull the type and description into kernel space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) ret = key_get_type_from_user(type, _type, sizeof(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) description = strndup_user(_description, KEY_MAX_DESC_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (IS_ERR(description)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) ret = PTR_ERR(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /* get the keyring at which to begin the search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) if (IS_ERR(keyring_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) ret = PTR_ERR(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) /* get the destination keyring if specified */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) dest_ref = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (destringid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) if (IS_ERR(dest_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) ret = PTR_ERR(dest_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) goto error3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) /* find the key type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) ktype = key_type_lookup(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (IS_ERR(ktype)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) ret = PTR_ERR(ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) goto error4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) /* do the search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) key_ref = keyring_search(keyring_ref, ktype, description, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) /* treat lack or presence of a negative key the same */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (ret == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) goto error5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) /* link the resulting key to the destination keyring if we can */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) if (dest_ref) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ret = key_permission(key_ref, KEY_NEED_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) goto error6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) ret = key_link(key_ref_to_ptr(dest_ref), key_ref_to_ptr(key_ref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) goto error6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) ret = key_ref_to_ptr(key_ref)->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) error6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) error5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) key_type_put(ktype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) error4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) key_ref_put(dest_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) error3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) key_ref_put(keyring_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) kfree(description);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * Call the read method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) static long __keyctl_read_key(struct key *key, char *buffer, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) down_read(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) ret = key_validate(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) ret = key->type->read(key, buffer, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) up_read(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) * Read a key's payload.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) * The key must either grant the caller Read permission, or it must grant the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) * caller Search permission when searched for from the process keyrings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) * If successful, we place up to buflen bytes of data into the buffer, if one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) * is provided, and return the amount of data that is available in the key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * irrespective of how much we copied into the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) char *key_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) size_t key_data_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) /* find the key first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) key_ref = lookup_user_key(keyid, 0, KEY_DEFER_PERM_CHECK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) ret = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) ret = key_read_state(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) goto key_put_out; /* Negatively instantiated */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) /* see if we can read it directly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) ret = key_permission(key_ref, KEY_NEED_READ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) goto can_read_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (ret != -EACCES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) goto key_put_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) /* we can't; see if it's searchable from this process's keyrings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) * - we automatically take account of the fact that it may be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) * dangling off an instantiation key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) if (!is_key_possessed(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) goto key_put_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) /* the key is probably readable - now try to read it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) can_read_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) if (!key->type->read) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ret = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) goto key_put_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) if (!buffer || !buflen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) /* Get the key length from the read method */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) ret = __keyctl_read_key(key, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) goto key_put_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) * Read the data with the semaphore held (since we might sleep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) * to protect against the key being updated or revoked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) * Allocating a temporary buffer to hold the keys before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) * transferring them to user buffer to avoid potential
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) * deadlock involving page fault and mmap_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * key_data_len = (buflen <= PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * ? buflen : actual length of key data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * This prevents allocating arbitrary large buffer which can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * be much larger than the actual key length. In the latter case,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * at least 2 passes of this loop is required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) key_data_len = (buflen <= PAGE_SIZE) ? buflen : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) if (key_data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) key_data = kvmalloc(key_data_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) if (!key_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) goto key_put_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) ret = __keyctl_read_key(key, key_data, key_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) * Read methods will just return the required length without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) * any copying if the provided length isn't large enough.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (ret <= 0 || ret > buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) * The key may change (unlikely) in between 2 consecutive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) * __keyctl_read_key() calls. In this case, we reallocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) * a larger buffer and redo the key read when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) * key_data_len < ret <= buflen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) if (ret > key_data_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) if (unlikely(key_data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) kvfree_sensitive(key_data, key_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) key_data_len = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) continue; /* Allocate buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) if (copy_to_user(buffer, key_data, ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) kvfree_sensitive(key_data, key_data_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) key_put_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * Change the ownership of a key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) * The key must grant the caller Setattr permission for this to work, though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * the key need not be fully instantiated yet. For the UID to be changed, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * for the GID to be changed to a group the caller is not a member of, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) * caller must have sysadmin capability. If either uid or gid is -1 then that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) * attribute is not changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) * If the UID is to be changed, the new user must have sufficient quota to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) * accept the key. The quota deduction will be removed from the old user to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) * the new user should the attribute be changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) long keyctl_chown_key(key_serial_t id, uid_t user, gid_t group)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) struct key_user *newowner, *zapowner = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) kuid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) kgid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) uid = make_kuid(current_user_ns(), user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) gid = make_kgid(current_user_ns(), group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) if ((user != (uid_t) -1) && !uid_valid(uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) if ((group != (gid_t) -1) && !gid_valid(gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) if (user == (uid_t) -1 && group == (gid_t) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) KEY_NEED_SETATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) /* make the changes with the locks held to prevent chown/chown races */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) down_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) if (!capable(CAP_SYS_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) /* only the sysadmin can chown a key to some other UID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) if (user != (uid_t) -1 && !uid_eq(key->uid, uid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) /* only the sysadmin can set the key's GID to a group other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) * than one of those that the current process subscribes to */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) if (group != (gid_t) -1 && !gid_eq(gid, key->gid) && !in_group_p(gid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) /* change the UID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) newowner = key_user_lookup(uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) if (!newowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) /* transfer the quota burden to the new user */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) key_quota_root_maxkeys : key_quota_maxkeys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) key_quota_root_maxbytes : key_quota_maxbytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) spin_lock(&newowner->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) if (newowner->qnkeys + 1 > maxkeys ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) newowner->qnbytes + key->quotalen > maxbytes ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) newowner->qnbytes + key->quotalen <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) newowner->qnbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) goto quota_overrun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) newowner->qnkeys++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) newowner->qnbytes += key->quotalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) spin_unlock(&newowner->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) spin_lock(&key->user->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) key->user->qnkeys--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) key->user->qnbytes -= key->quotalen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) spin_unlock(&key->user->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) atomic_dec(&key->user->nkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) atomic_inc(&newowner->nkeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) if (key->state != KEY_IS_UNINSTANTIATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) atomic_dec(&key->user->nikeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) atomic_inc(&newowner->nikeys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) zapowner = key->user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) key->user = newowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) key->uid = uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) /* change the GID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (group != (gid_t) -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) key->gid = gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) notify_key(key, NOTIFY_KEY_SETATTR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) error_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) up_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (zapowner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) key_user_put(zapowner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) quota_overrun:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) spin_unlock(&newowner->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) zapowner = newowner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) ret = -EDQUOT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) goto error_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * Change the permission mask on a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * The key must grant the caller Setattr permission for this to work, though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * the key need not be fully instantiated yet. If the caller does not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * sysadmin capability, it may only change the permission on keys that it owns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) long keyctl_setperm_key(key_serial_t id, key_perm_t perm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) KEY_NEED_SETATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) /* make the changes with the locks held to prevent chown/chmod races */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) ret = -EACCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) down_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) /* if we're not the sysadmin, we can only change a key that we own */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) key->perm = perm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) notify_key(key, NOTIFY_KEY_SETATTR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) up_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * Get the destination keyring for instantiation and check that the caller has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) * Write permission on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) static long get_instantiation_keyring(key_serial_t ringid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct request_key_auth *rka,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) struct key **_dest_keyring)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) key_ref_t dkref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) *_dest_keyring = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) /* just return a NULL pointer if we weren't asked to make a link */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (ringid == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) /* if a specific keyring is nominated by ID, then use that */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (ringid > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) if (IS_ERR(dkref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) return PTR_ERR(dkref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) *_dest_keyring = key_ref_to_ptr(dkref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) if (ringid == KEY_SPEC_REQKEY_AUTH_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) /* otherwise specify the destination keyring recorded in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) * authorisation key (any KEY_SPEC_*_KEYRING) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) *_dest_keyring = key_get(rka->dest_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) * Change the request_key authorisation key on the current process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) static int keyctl_change_reqkey_auth(struct key *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) key_put(new->request_key_auth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) new->request_key_auth = key_get(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) return commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * Instantiate a key with the specified payload and link the key into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) * destination keyring if one is given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * The caller must have the appropriate instantiation permit set for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * work (see keyctl_assume_authority). No other permissions are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static long keyctl_instantiate_key_common(key_serial_t id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) struct iov_iter *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) const struct cred *cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) struct request_key_auth *rka;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) struct key *instkey, *dest_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) size_t plen = from ? iov_iter_count(from) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) void *payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) kenter("%d,,%zu,%d", id, plen, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) if (!plen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) from = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) if (plen > 1024 * 1024 - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) /* the appropriate instantiation authorisation key must have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) * assumed before calling this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) instkey = cred->request_key_auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) if (!instkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) rka = instkey->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) if (rka->target_key->serial != id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) /* pull the payload in if one was supplied */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) payload = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (from) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) payload = kvmalloc(plen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) if (!payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) if (!copy_from_iter_full(payload, plen, from))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) /* find the destination keyring amongst those belonging to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) * requesting task */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) goto error2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) /* instantiate the key and link it into a keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) ret = key_instantiate_and_link(rka->target_key, payload, plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) dest_keyring, instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) key_put(dest_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) /* discard the assumed authority if it's just been disabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * instantiation of the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) keyctl_change_reqkey_auth(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) error2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) kvfree_sensitive(payload, plen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * Instantiate a key with the specified payload and link the key into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) * destination keyring if one is given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) * The caller must have the appropriate instantiation permit set for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * work (see keyctl_assume_authority). No other permissions are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) long keyctl_instantiate_key(key_serial_t id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) const void __user *_payload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) size_t plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) if (_payload && plen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) struct iovec iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) struct iov_iter from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) ret = import_single_range(WRITE, (void __user *)_payload, plen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) &iov, &from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) if (unlikely(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) return keyctl_instantiate_key_common(id, &from, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) return keyctl_instantiate_key_common(id, NULL, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) * Instantiate a key with the specified multipart payload and link the key into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) * the destination keyring if one is given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) * The caller must have the appropriate instantiation permit set for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) * work (see keyctl_assume_authority). No other permissions are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) long keyctl_instantiate_key_iov(key_serial_t id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) const struct iovec __user *_payload_iov,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) unsigned ioc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) struct iovec iovstack[UIO_FASTIOV], *iov = iovstack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) struct iov_iter from;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) if (!_payload_iov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) ioc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) ret = import_iovec(WRITE, _payload_iov, ioc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) ARRAY_SIZE(iovstack), &iov, &from);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) ret = keyctl_instantiate_key_common(id, &from, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) kfree(iov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) * Negatively instantiate the key with the given timeout (in seconds) and link
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) * the key into the destination keyring if one is given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) * The caller must have the appropriate instantiation permit set for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) * work (see keyctl_assume_authority). No other permissions are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) * The key and any links to the key will be automatically garbage collected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) * after the timeout expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) * Negative keys are used to rate limit repeated request_key() calls by causing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) * them to return -ENOKEY until the negative key expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) return keyctl_reject_key(id, timeout, ENOKEY, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) * Negatively instantiate the key with the given timeout (in seconds) and error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) * code and link the key into the destination keyring if one is given.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) * The caller must have the appropriate instantiation permit set for this to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) * work (see keyctl_assume_authority). No other permissions are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) * The key and any links to the key will be automatically garbage collected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) * after the timeout expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) * Negative keys are used to rate limit repeated request_key() calls by causing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) * them to return the specified error code until the negative key expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) key_serial_t ringid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) const struct cred *cred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) struct request_key_auth *rka;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct key *instkey, *dest_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) kenter("%d,%u,%u,%d", id, timeout, error, ringid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) /* must be a valid error code and mustn't be a kernel special */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) if (error <= 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) error >= MAX_ERRNO ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) error == ERESTARTSYS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) error == ERESTARTNOINTR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) error == ERESTARTNOHAND ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) error == ERESTART_RESTARTBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) /* the appropriate instantiation authorisation key must have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) * assumed before calling this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) instkey = cred->request_key_auth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) if (!instkey)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) rka = instkey->payload.data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) if (rka->target_key->serial != id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) /* find the destination keyring if present (which must also be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) * writable) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) ret = get_instantiation_keyring(ringid, rka, &dest_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) /* instantiate the key and link it into a keyring */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) ret = key_reject_and_link(rka->target_key, timeout, error,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) dest_keyring, instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) key_put(dest_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) /* discard the assumed authority if it's just been disabled by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) * instantiation of the key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) keyctl_change_reqkey_auth(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) * Read or set the default keyring in which request_key() will cache keys and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) * return the old setting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) * If a thread or process keyring is specified then it will be created if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) * doesn't yet exist. The old setting will be returned if successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) long keyctl_set_reqkey_keyring(int reqkey_defl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) struct cred *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) int ret, old_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) old_setting = current_cred_xxx(jit_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) return old_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) new = prepare_creds();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) if (!new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) switch (reqkey_defl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) case KEY_REQKEY_DEFL_THREAD_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) ret = install_thread_keyring_to_cred(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) goto set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) case KEY_REQKEY_DEFL_PROCESS_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) ret = install_process_keyring_to_cred(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) goto set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) case KEY_REQKEY_DEFL_DEFAULT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) case KEY_REQKEY_DEFL_SESSION_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) case KEY_REQKEY_DEFL_USER_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) goto set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) case KEY_REQKEY_DEFL_NO_CHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) case KEY_REQKEY_DEFL_GROUP_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) set:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) new->jit_keyring = reqkey_defl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) commit_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) return old_setting;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) abort_creds(new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) * Set or clear the timeout on a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) * Either the key must grant the caller Setattr permission or else the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) * must hold an instantiation authorisation token for the key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) * The timeout is either 0 to clear the timeout, or a number of seconds from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) * the current time. The key and any links to the key will be automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) * garbage collected after the timeout expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) * Keys with KEY_FLAG_KEEP set should not be timed out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) * If successful, 0 is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) long keyctl_set_timeout(key_serial_t id, unsigned timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) struct key *key, *instkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) KEY_NEED_SETATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) /* setting the timeout on a key under construction is permitted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) * if we have the authorisation token handy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) if (PTR_ERR(key_ref) == -EACCES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) instkey = key_get_instantiation_authkey(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) if (!IS_ERR(instkey)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) key_put(instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) key_ref = lookup_user_key(id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) KEY_AUTHTOKEN_OVERRIDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) if (!IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) goto okay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) ret = PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) okay:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) if (test_bit(KEY_FLAG_KEEP, &key->flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) key_set_timeout(key, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) notify_key(key, NOTIFY_KEY_SETATTR, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) * Assume (or clear) the authority to instantiate the specified key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) * This sets the authoritative token currently in force for key instantiation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) * This must be done for a key to be instantiated. It has the effect of making
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) * available all the keys from the caller of the request_key() that created a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) * key to request_key() calls made by the caller of this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) * The caller must have the instantiation key in their process keyrings with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) * Search permission grant available to the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) * If the ID given is 0, then the setting will be cleared and 0 returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) * If the ID given has a matching an authorisation key, then that key will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) * set and its ID will be returned. The authorisation key can be read to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) * the callout information passed to request_key().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) long keyctl_assume_authority(key_serial_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) struct key *authkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) /* special key IDs aren't permitted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (id < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) /* we divest ourselves of authority if given an ID of 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) if (id == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) ret = keyctl_change_reqkey_auth(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) /* attempt to assume the authority temporarily granted to us whilst we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) * instantiate the specified key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) * - the authorisation key must be in the current task's keyrings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) * somewhere
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) authkey = key_get_instantiation_authkey(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (IS_ERR(authkey)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) ret = PTR_ERR(authkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) ret = keyctl_change_reqkey_auth(authkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) ret = authkey->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) key_put(authkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) * Get a key's the LSM security label.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) * The key must grant the caller View permission for this to work.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) * If there's a buffer, then up to buflen bytes of data will be placed into it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * If successful, the amount of information available will be returned,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) * irrespective of how much was copied (including the terminal NUL).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) long keyctl_get_security(key_serial_t keyid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) struct key *key, *instkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) char *context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) if (IS_ERR(key_ref)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) if (PTR_ERR(key_ref) != -EACCES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) return PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) /* viewing a key under construction is also permitted if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) * have the authorisation token handy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) instkey = key_get_instantiation_authkey(keyid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) if (IS_ERR(instkey))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) return PTR_ERR(instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) key_put(instkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) KEY_AUTHTOKEN_OVERRIDE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) return PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) ret = security_key_getsecurity(key, &context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) if (ret == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) /* if no information was returned, give userspace an empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) * string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) if (buffer && buflen > 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) copy_to_user(buffer, "", 1) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) } else if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) /* return as much data as there's room for */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (buffer && buflen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) if (buflen > ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) buflen = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) if (copy_to_user(buffer, context, buflen) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) kfree(context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) * Attempt to install the calling process's session keyring on the process's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) * parent process.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) * The keyring must exist and must grant the caller LINK permission, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) * parent process must be single-threaded and must have the same effective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) * ownership as this process and mustn't be SUID/SGID.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) * The keyring will be emplaced on the parent when it next resumes userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) * If successful, 0 will be returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) long keyctl_session_to_parent(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) struct task_struct *me, *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) const struct cred *mycred, *pcred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) struct callback_head *newwork, *oldwork;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) key_ref_t keyring_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) struct cred *cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) keyring_r = lookup_user_key(KEY_SPEC_SESSION_KEYRING, 0, KEY_NEED_LINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) if (IS_ERR(keyring_r))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) return PTR_ERR(keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) /* our parent is going to need a new cred struct, a new tgcred struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * and new security data, so we allocate them here to prevent ENOMEM in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * our parent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) cred = cred_alloc_blank();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) if (!cred)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) goto error_keyring;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) newwork = &cred->rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) cred->session_keyring = key_ref_to_ptr(keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) keyring_r = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) init_task_work(newwork, key_change_session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) me = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) write_lock_irq(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) ret = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) oldwork = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) parent = rcu_dereference_protected(me->real_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) lockdep_is_held(&tasklist_lock));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) /* the parent mustn't be init and mustn't be a kernel thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) if (parent->pid <= 1 || !parent->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) /* the parent must be single threaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) if (!thread_group_empty(parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) /* the parent and the child must have different session keyrings or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) * there's no point */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) mycred = current_cred();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) pcred = __task_cred(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) if (mycred == pcred ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) mycred->session_keyring == pcred->session_keyring) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) /* the parent must have the same effective ownership and mustn't be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) * SUID/SGID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) if (!uid_eq(pcred->uid, mycred->euid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) !uid_eq(pcred->euid, mycred->euid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) !uid_eq(pcred->suid, mycred->euid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) !gid_eq(pcred->gid, mycred->egid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) !gid_eq(pcred->egid, mycred->egid) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) !gid_eq(pcred->sgid, mycred->egid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) /* the keyrings must have the same UID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) if ((pcred->session_keyring &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) !uid_eq(pcred->session_keyring->uid, mycred->euid)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) !uid_eq(mycred->session_keyring->uid, mycred->euid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) /* cancel an already pending keyring replacement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) oldwork = task_work_cancel(parent, key_change_session_keyring);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) /* the replacement session keyring is applied just prior to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) * restarting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) ret = task_work_add(parent, newwork, TWA_RESUME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) newwork = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) write_unlock_irq(&tasklist_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) if (oldwork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) put_cred(container_of(oldwork, struct cred, rcu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) if (newwork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) put_cred(cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) error_keyring:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) key_ref_put(keyring_r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) * Apply a restriction to a given keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) * The caller must have Setattr permission to change keyring restrictions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) * The requested type name may be a NULL pointer to reject all attempts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) * to link to the keyring. In this case, _restriction must also be NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) * Otherwise, both _type and _restriction must be non-NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) * Returns 0 if successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) const char __user *_restriction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) char type[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) char *restriction = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) if (IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) return PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) if (_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) if (!_restriction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) ret = key_get_type_from_user(type, _type, sizeof(type));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) restriction = strndup_user(_restriction, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) if (IS_ERR(restriction)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) ret = PTR_ERR(restriction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) if (_restriction)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) kfree(restriction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) key_ref_put(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) #ifdef CONFIG_KEY_NOTIFICATIONS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) * Watch for changes to a key.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) * The caller must have View permission to watch a key or keyring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) long keyctl_watch_key(key_serial_t id, int watch_queue_fd, int watch_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) struct watch_queue *wqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) struct watch_list *wlist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) struct watch *watch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) struct key *key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) key_ref_t key_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) if (watch_id < -1 || watch_id > 0xff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_VIEW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) if (IS_ERR(key_ref))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) return PTR_ERR(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) key = key_ref_to_ptr(key_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) wqueue = get_watch_queue(watch_queue_fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) if (IS_ERR(wqueue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) ret = PTR_ERR(wqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) goto err_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) if (watch_id >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) if (!key->watchers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) wlist = kzalloc(sizeof(*wlist), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) if (!wlist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) goto err_wqueue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) init_watch_list(wlist, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) watch = kzalloc(sizeof(*watch), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) if (!watch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) goto err_wlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) init_watch(watch, wqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) watch->id = key->serial;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) watch->info_id = (u32)watch_id << WATCH_INFO_ID__SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) ret = security_watch_key(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) goto err_watch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) down_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) if (!key->watchers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) key->watchers = wlist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) wlist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) ret = add_watch_to_object(watch, key->watchers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) up_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) if (ret == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) watch = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) ret = -EBADSLT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) if (key->watchers) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) down_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) ret = remove_watch_from_object(key->watchers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) wqueue, key_serial(key),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) up_write(&key->sem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) err_watch:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) kfree(watch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) err_wlist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) kfree(wlist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) err_wqueue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) put_watch_queue(wqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) err_key:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) key_put(key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) #endif /* CONFIG_KEY_NOTIFICATIONS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) * Get keyrings subsystem capabilities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) size_t size = buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) if (size > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) if (size > sizeof(keyrings_capabilities))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) size = sizeof(keyrings_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) if (copy_to_user(_buffer, keyrings_capabilities, size) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) if (size < buflen &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) clear_user(_buffer + size, buflen - size) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) return sizeof(keyrings_capabilities);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) * The key control system call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) unsigned long, arg4, unsigned long, arg5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) switch (option) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) case KEYCTL_GET_KEYRING_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) return keyctl_get_keyring_ID((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) (int) arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) case KEYCTL_JOIN_SESSION_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) return keyctl_join_session_keyring((const char __user *) arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) case KEYCTL_UPDATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) return keyctl_update_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) (const void __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) (size_t) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) case KEYCTL_REVOKE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return keyctl_revoke_key((key_serial_t) arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) case KEYCTL_DESCRIBE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) return keyctl_describe_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) (char __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) (unsigned) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) case KEYCTL_CLEAR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) return keyctl_keyring_clear((key_serial_t) arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) case KEYCTL_LINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) return keyctl_keyring_link((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) (key_serial_t) arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) case KEYCTL_UNLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) return keyctl_keyring_unlink((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) (key_serial_t) arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) case KEYCTL_SEARCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) return keyctl_keyring_search((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) (const char __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) (const char __user *) arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) (key_serial_t) arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) case KEYCTL_READ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) return keyctl_read_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) (char __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) (size_t) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) case KEYCTL_CHOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) return keyctl_chown_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) (uid_t) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) (gid_t) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) case KEYCTL_SETPERM:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) return keyctl_setperm_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) (key_perm_t) arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) case KEYCTL_INSTANTIATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) return keyctl_instantiate_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) (const void __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) (size_t) arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) (key_serial_t) arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) case KEYCTL_NEGATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) return keyctl_negate_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) (unsigned) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) (key_serial_t) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) case KEYCTL_SET_REQKEY_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) return keyctl_set_reqkey_keyring(arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) case KEYCTL_SET_TIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) return keyctl_set_timeout((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) (unsigned) arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) case KEYCTL_ASSUME_AUTHORITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) return keyctl_assume_authority((key_serial_t) arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) case KEYCTL_GET_SECURITY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) return keyctl_get_security((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) (char __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) (size_t) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) case KEYCTL_SESSION_TO_PARENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) return keyctl_session_to_parent();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) case KEYCTL_REJECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) return keyctl_reject_key((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) (unsigned) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) (unsigned) arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) (key_serial_t) arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) case KEYCTL_INSTANTIATE_IOV:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) return keyctl_instantiate_key_iov(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) (key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) (const struct iovec __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) (unsigned) arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) (key_serial_t) arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) case KEYCTL_INVALIDATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) return keyctl_invalidate_key((key_serial_t) arg2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) case KEYCTL_GET_PERSISTENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) case KEYCTL_DH_COMPUTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) (char __user *) arg3, (size_t) arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) (struct keyctl_kdf_params __user *) arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) case KEYCTL_RESTRICT_KEYRING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) return keyctl_restrict_keyring((key_serial_t) arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) (const char __user *) arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) (const char __user *) arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) case KEYCTL_PKEY_QUERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) if (arg3 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) return keyctl_pkey_query((key_serial_t)arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) (const char __user *)arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) (struct keyctl_pkey_query __user *)arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) case KEYCTL_PKEY_ENCRYPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) case KEYCTL_PKEY_DECRYPT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) case KEYCTL_PKEY_SIGN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) return keyctl_pkey_e_d_s(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) option,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) (const struct keyctl_pkey_params __user *)arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) (const char __user *)arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) (const void __user *)arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) (void __user *)arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) case KEYCTL_PKEY_VERIFY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) return keyctl_pkey_verify(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) (const struct keyctl_pkey_params __user *)arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) (const char __user *)arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) (const void __user *)arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) (const void __user *)arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) case KEYCTL_MOVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) return keyctl_keyring_move((key_serial_t)arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) (key_serial_t)arg3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) (key_serial_t)arg4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) (unsigned int)arg5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) case KEYCTL_CAPABILITIES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) case KEYCTL_WATCH_KEY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return keyctl_watch_key((key_serial_t)arg2, (int)arg3, (int)arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) }