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