David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1 | /* Userspace key control operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004-5 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/init.h> |
| 13 | #include <linux/sched.h> |
Ingo Molnar | 2993002 | 2017-02-08 18:51:36 +0100 | [diff] [blame] | 14 | #include <linux/sched/task.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
| 16 | #include <linux/syscalls.h> |
Bryan Schumaker | 59e6b9c | 2012-02-24 14:14:50 -0500 | [diff] [blame] | 17 | #include <linux/key.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/keyctl.h> |
| 19 | #include <linux/fs.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 20 | #include <linux/capability.h> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 21 | #include <linux/cred.h> |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 22 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/err.h> |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 25 | #include <linux/security.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 26 | #include <linux/uio.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
David Howells | 822ad64 | 2019-02-14 16:20:25 +0000 | [diff] [blame] | 28 | #include <keys/request_key_auth-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include "internal.h" |
| 30 | |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 31 | #define KEY_MAX_DESC_SIZE 4096 |
| 32 | |
David Howells | 45e0f30 | 2019-05-30 14:53:10 +0100 | [diff] [blame] | 33 | static const unsigned char keyrings_capabilities[1] = { |
| 34 | [0] = (KEYCTL_CAPS0_CAPABILITIES | |
| 35 | (IS_ENABLED(CONFIG_PERSISTENT_KEYRINGS) ? KEYCTL_CAPS0_PERSISTENT_KEYRINGS : 0) | |
| 36 | (IS_ENABLED(CONFIG_KEY_DH_OPERATIONS) ? KEYCTL_CAPS0_DIFFIE_HELLMAN : 0) | |
| 37 | (IS_ENABLED(CONFIG_ASYMMETRIC_KEY_TYPE) ? KEYCTL_CAPS0_PUBLIC_KEY : 0) | |
| 38 | (IS_ENABLED(CONFIG_BIG_KEYS) ? KEYCTL_CAPS0_BIG_KEY : 0) | |
| 39 | KEYCTL_CAPS0_INVALIDATE | |
| 40 | KEYCTL_CAPS0_RESTRICT_KEYRING | |
| 41 | KEYCTL_CAPS0_MOVE |
| 42 | ), |
| 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, |
Arun Raghavan | 6b79ccb | 2008-04-29 01:01:28 -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 | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 225 | key = request_key_and_link(ktype, description, callout_info, |
| 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; |
| 344 | payload = kmalloc(plen, GFP_KERNEL); |
| 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: |
Eric Biggers | 57070c8 | 2017-06-08 14:48:57 +0100 | [diff] [blame] | 365 | kzfree(payload); |
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 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [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); |
David Howells | 0c2c9a3 | 2009-09-02 09:13:50 +0100 | [diff] [blame] | 391 | if (ret != -EACCES) |
| 392 | goto error; |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 393 | key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); |
David Howells | 0c2c9a3 | 2009-09-02 09:13:50 +0100 | [diff] [blame] | 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 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [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)) { |
| 437 | key_ref = lookup_user_key(id, 0, 0); |
| 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 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [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)) { |
| 482 | keyring_ref = lookup_user_key(ringid, 0, 0); |
| 483 | if (IS_ERR(keyring_ref)) |
| 484 | goto error; |
| 485 | if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, |
| 486 | &key_ref_to_ptr(keyring_ref)->flags)) |
| 487 | goto clear; |
| 488 | goto error_put; |
| 489 | } |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | goto error; |
| 492 | } |
| 493 | |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 494 | clear: |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 495 | keyring = key_ref_to_ptr(keyring_ref); |
| 496 | if (test_bit(KEY_FLAG_KEEP, &keyring->flags)) |
| 497 | ret = -EPERM; |
| 498 | else |
| 499 | ret = keyring_clear(keyring); |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 500 | error_put: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 501 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 502 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 504 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 507 | * Create a link from a keyring to a key if there's no matching key in the |
| 508 | * keyring, otherwise replace the link to the matching key with a link to the |
| 509 | * new key. |
| 510 | * |
| 511 | * The key must grant the caller Link permission and the the keyring must grant |
| 512 | * the caller Write permission. Furthermore, if an additional link is created, |
| 513 | * the keyring's quota will be extended. |
| 514 | * |
| 515 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | */ |
| 517 | long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) |
| 518 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 519 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | long ret; |
| 521 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 522 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 523 | if (IS_ERR(keyring_ref)) { |
| 524 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | goto error; |
| 526 | } |
| 527 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 528 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 529 | if (IS_ERR(key_ref)) { |
| 530 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | goto error2; |
| 532 | } |
| 533 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 534 | 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] | 535 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 536 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 537 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 538 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 539 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 544 | * Unlink a key from a keyring. |
| 545 | * |
| 546 | * The keyring must grant the caller Write permission for this to work; the key |
| 547 | * itself need not grant the caller anything. If the last link to a key is |
| 548 | * removed then that key will be scheduled for destruction. |
| 549 | * |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 550 | * Keys or keyrings with KEY_FLAG_KEEP set should not be unlinked. |
| 551 | * |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 552 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | */ |
| 554 | long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) |
| 555 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 556 | key_ref_t keyring_ref, key_ref; |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 557 | struct key *keyring, *key; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | long ret; |
| 559 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 560 | keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 561 | if (IS_ERR(keyring_ref)) { |
| 562 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | goto error; |
| 564 | } |
| 565 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 566 | key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 567 | if (IS_ERR(key_ref)) { |
| 568 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | goto error2; |
| 570 | } |
| 571 | |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 572 | keyring = key_ref_to_ptr(keyring_ref); |
| 573 | key = key_ref_to_ptr(key_ref); |
| 574 | if (test_bit(KEY_FLAG_KEEP, &keyring->flags) && |
| 575 | test_bit(KEY_FLAG_KEEP, &key->flags)) |
| 576 | ret = -EPERM; |
| 577 | else |
| 578 | ret = key_unlink(keyring, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 580 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 581 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 582 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 583 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 585 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | /* |
David Howells | ed0ac5c | 2019-05-20 21:51:50 +0100 | [diff] [blame] | 588 | * Move a link to a key from one keyring to another, displacing any matching |
| 589 | * key from the destination keyring. |
| 590 | * |
| 591 | * The key must grant the caller Link permission and both keyrings must grant |
| 592 | * the caller Write permission. There must also be a link in the from keyring |
| 593 | * to the key. If both keyrings are the same, nothing is done. |
| 594 | * |
| 595 | * If successful, 0 will be returned. |
| 596 | */ |
| 597 | long keyctl_keyring_move(key_serial_t id, key_serial_t from_ringid, |
| 598 | key_serial_t to_ringid, unsigned int flags) |
| 599 | { |
| 600 | key_ref_t key_ref, from_ref, to_ref; |
| 601 | long ret; |
| 602 | |
| 603 | if (flags & ~KEYCTL_MOVE_EXCL) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); |
| 607 | if (IS_ERR(key_ref)) |
| 608 | return PTR_ERR(key_ref); |
| 609 | |
| 610 | from_ref = lookup_user_key(from_ringid, 0, KEY_NEED_WRITE); |
| 611 | if (IS_ERR(from_ref)) { |
| 612 | ret = PTR_ERR(from_ref); |
| 613 | goto error2; |
| 614 | } |
| 615 | |
| 616 | to_ref = lookup_user_key(to_ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
| 617 | if (IS_ERR(to_ref)) { |
| 618 | ret = PTR_ERR(to_ref); |
| 619 | goto error3; |
| 620 | } |
| 621 | |
| 622 | ret = key_move(key_ref_to_ptr(key_ref), key_ref_to_ptr(from_ref), |
| 623 | key_ref_to_ptr(to_ref), flags); |
| 624 | |
| 625 | key_ref_put(to_ref); |
| 626 | error3: |
| 627 | key_ref_put(from_ref); |
| 628 | error2: |
| 629 | key_ref_put(key_ref); |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 634 | * Return a description of a key to userspace. |
| 635 | * |
| 636 | * The key must grant the caller View permission for this to work. |
| 637 | * |
| 638 | * If there's a buffer, we place up to buflen bytes of data into it formatted |
| 639 | * in the following way: |
| 640 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | * type;uid;gid;perm;description<NUL> |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 642 | * |
| 643 | * If successful, we return the amount of description available, irrespective |
| 644 | * of how much we may have copied into the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | */ |
| 646 | long keyctl_describe_key(key_serial_t keyid, |
| 647 | char __user *buffer, |
| 648 | size_t buflen) |
| 649 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 650 | struct key *key, *instkey; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 651 | key_ref_t key_ref; |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 652 | char *infobuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | long ret; |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 654 | int desclen, infolen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 656 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 657 | if (IS_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 658 | /* viewing a key under construction is permitted if we have the |
| 659 | * authorisation token handy */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 660 | if (PTR_ERR(key_ref) == -EACCES) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 661 | instkey = key_get_instantiation_authkey(keyid); |
| 662 | if (!IS_ERR(instkey)) { |
| 663 | key_put(instkey); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 664 | key_ref = lookup_user_key(keyid, |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 665 | KEY_LOOKUP_PARTIAL, |
| 666 | 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 667 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 668 | goto okay; |
| 669 | } |
| 670 | } |
| 671 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 672 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | goto error; |
| 674 | } |
| 675 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 676 | okay: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 677 | key = key_ref_to_ptr(key_ref); |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 678 | desclen = strlen(key->description); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 679 | |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 680 | /* calculate how much information we're going to return */ |
| 681 | ret = -ENOMEM; |
| 682 | infobuf = kasprintf(GFP_KERNEL, |
| 683 | "%s;%d;%d;%08x;", |
| 684 | key->type->name, |
| 685 | from_kuid_munged(current_user_ns(), key->uid), |
| 686 | from_kgid_munged(current_user_ns(), key->gid), |
| 687 | key->perm); |
| 688 | if (!infobuf) |
| 689 | goto error2; |
| 690 | infolen = strlen(infobuf); |
| 691 | ret = infolen + desclen + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
| 693 | /* consider returning the data */ |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 694 | if (buffer && buflen >= ret) { |
| 695 | if (copy_to_user(buffer, infobuf, infolen) != 0 || |
| 696 | copy_to_user(buffer + infolen, key->description, |
| 697 | desclen + 1) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | ret = -EFAULT; |
| 699 | } |
| 700 | |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 701 | kfree(infobuf); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 702 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 703 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 704 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 706 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 709 | * Search the specified keyring and any keyrings it links to for a matching |
| 710 | * key. Only keyrings that grant the caller Search permission will be searched |
| 711 | * (this includes the starting keyring). Only keys with Search permission can |
| 712 | * be found. |
| 713 | * |
| 714 | * If successful, the found key will be linked to the destination keyring if |
| 715 | * supplied and the key has Link permission, and the found key ID will be |
| 716 | * returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | */ |
| 718 | long keyctl_keyring_search(key_serial_t ringid, |
| 719 | const char __user *_type, |
| 720 | const char __user *_description, |
| 721 | key_serial_t destringid) |
| 722 | { |
| 723 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 724 | key_ref_t keyring_ref, key_ref, dest_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | char type[32], *description; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 726 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
| 728 | /* pull the type and description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 729 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (ret < 0) |
| 731 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
David Howells | aa9d443 | 2014-12-01 22:52:45 +0000 | [diff] [blame] | 733 | description = strndup_user(_description, KEY_MAX_DESC_SIZE); |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 734 | if (IS_ERR(description)) { |
| 735 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 737 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
| 739 | /* get the keyring at which to begin the search */ |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 740 | keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 741 | if (IS_ERR(keyring_ref)) { |
| 742 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | goto error2; |
| 744 | } |
| 745 | |
| 746 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 747 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | if (destringid) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 749 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 750 | KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 751 | if (IS_ERR(dest_ref)) { |
| 752 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | goto error3; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /* find the key type */ |
| 758 | ktype = key_type_lookup(type); |
| 759 | if (IS_ERR(ktype)) { |
| 760 | ret = PTR_ERR(ktype); |
| 761 | goto error4; |
| 762 | } |
| 763 | |
| 764 | /* do the search */ |
David Howells | dcf49db | 2019-06-26 21:02:32 +0100 | [diff] [blame^] | 765 | key_ref = keyring_search(keyring_ref, ktype, description, true); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 766 | if (IS_ERR(key_ref)) { |
| 767 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | |
| 769 | /* treat lack or presence of a negative key the same */ |
| 770 | if (ret == -EAGAIN) |
| 771 | ret = -ENOKEY; |
| 772 | goto error5; |
| 773 | } |
| 774 | |
| 775 | /* link the resulting key to the destination keyring if we can */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 776 | if (dest_ref) { |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 777 | ret = key_permission(key_ref, KEY_NEED_LINK); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 778 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | goto error6; |
| 780 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 781 | 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] | 782 | if (ret < 0) |
| 783 | goto error6; |
| 784 | } |
| 785 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 786 | ret = key_ref_to_ptr(key_ref)->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 787 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 788 | error6: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 789 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 790 | error5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | key_type_put(ktype); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 792 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 793 | key_ref_put(dest_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 794 | error3: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 795 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 796 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | kfree(description); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 798 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 800 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 803 | * Read a key's payload. |
| 804 | * |
| 805 | * The key must either grant the caller Read permission, or it must grant the |
| 806 | * caller Search permission when searched for from the process keyrings. |
| 807 | * |
| 808 | * If successful, we place up to buflen bytes of data into the buffer, if one |
| 809 | * is provided, and return the amount of data that is available in the key, |
| 810 | * irrespective of how much we copied into the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | */ |
| 812 | long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) |
| 813 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 814 | struct key *key; |
| 815 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | long ret; |
| 817 | |
| 818 | /* find the key first */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 819 | key_ref = lookup_user_key(keyid, 0, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 820 | if (IS_ERR(key_ref)) { |
| 821 | ret = -ENOKEY; |
| 822 | goto error; |
| 823 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 825 | key = key_ref_to_ptr(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 827 | ret = key_read_state(key); |
| 828 | if (ret < 0) |
| 829 | goto error2; /* Negatively instantiated */ |
Eric Biggers | 37863c4 | 2017-09-18 11:37:23 -0700 | [diff] [blame] | 830 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 831 | /* see if we can read it directly */ |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 832 | ret = key_permission(key_ref, KEY_NEED_READ); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 833 | if (ret == 0) |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 834 | goto can_read_key; |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 835 | if (ret != -EACCES) |
Eric Biggers | 7fc0786 | 2017-09-18 11:36:31 -0700 | [diff] [blame] | 836 | goto error2; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 837 | |
| 838 | /* we can't; see if it's searchable from this process's keyrings |
| 839 | * - we automatically take account of the fact that it may be |
| 840 | * dangling off an instantiation key |
| 841 | */ |
| 842 | if (!is_key_possessed(key_ref)) { |
| 843 | ret = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | goto error2; |
| 845 | } |
| 846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | /* the key is probably readable - now try to read it */ |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 848 | can_read_key: |
David Howells | b4a1b4f | 2015-12-18 01:34:26 +0000 | [diff] [blame] | 849 | ret = -EOPNOTSUPP; |
| 850 | if (key->type->read) { |
| 851 | /* Read the data with the semaphore held (since we might sleep) |
| 852 | * to protect against the key being updated or revoked. |
| 853 | */ |
| 854 | down_read(&key->sem); |
| 855 | ret = key_validate(key); |
| 856 | if (ret == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | ret = key->type->read(key, buffer, buflen); |
David Howells | b4a1b4f | 2015-12-18 01:34:26 +0000 | [diff] [blame] | 858 | up_read(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 861 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | key_put(key); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 863 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 865 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 868 | * Change the ownership of a key |
| 869 | * |
| 870 | * The key must grant the caller Setattr permission for this to work, though |
| 871 | * the key need not be fully instantiated yet. For the UID to be changed, or |
| 872 | * for the GID to be changed to a group the caller is not a member of, the |
| 873 | * caller must have sysadmin capability. If either uid or gid is -1 then that |
| 874 | * attribute is not changed. |
| 875 | * |
| 876 | * If the UID is to be changed, the new user must have sufficient quota to |
| 877 | * accept the key. The quota deduction will be removed from the old user to |
| 878 | * the new user should the attribute be changed. |
| 879 | * |
| 880 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 882 | 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] | 883 | { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 884 | struct key_user *newowner, *zapowner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 886 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | long ret; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 888 | kuid_t uid; |
| 889 | kgid_t gid; |
| 890 | |
| 891 | uid = make_kuid(current_user_ns(), user); |
| 892 | gid = make_kgid(current_user_ns(), group); |
| 893 | ret = -EINVAL; |
| 894 | if ((user != (uid_t) -1) && !uid_valid(uid)) |
| 895 | goto error; |
| 896 | if ((group != (gid_t) -1) && !gid_valid(gid)) |
| 897 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | |
| 899 | ret = 0; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 900 | if (user == (uid_t) -1 && group == (gid_t) -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | goto error; |
| 902 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 903 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 904 | KEY_NEED_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 905 | if (IS_ERR(key_ref)) { |
| 906 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | goto error; |
| 908 | } |
| 909 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 910 | key = key_ref_to_ptr(key_ref); |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | /* make the changes with the locks held to prevent chown/chown races */ |
| 913 | ret = -EACCES; |
| 914 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
| 916 | if (!capable(CAP_SYS_ADMIN)) { |
| 917 | /* only the sysadmin can chown a key to some other UID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 918 | if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 919 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | |
| 921 | /* only the sysadmin can set the key's GID to a group other |
| 922 | * than one of those that the current process subscribes to */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 923 | 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] | 924 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 927 | /* change the UID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 928 | if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 929 | ret = -ENOMEM; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 930 | newowner = key_user_lookup(uid); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 931 | if (!newowner) |
| 932 | goto error_put; |
| 933 | |
| 934 | /* transfer the quota burden to the new user */ |
| 935 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 936 | unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 937 | key_quota_root_maxkeys : key_quota_maxkeys; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 938 | unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 939 | key_quota_root_maxbytes : key_quota_maxbytes; |
| 940 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 941 | spin_lock(&newowner->lock); |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 942 | if (newowner->qnkeys + 1 >= maxkeys || |
| 943 | newowner->qnbytes + key->quotalen >= maxbytes || |
| 944 | newowner->qnbytes + key->quotalen < |
| 945 | newowner->qnbytes) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 946 | goto quota_overrun; |
| 947 | |
| 948 | newowner->qnkeys++; |
| 949 | newowner->qnbytes += key->quotalen; |
| 950 | spin_unlock(&newowner->lock); |
| 951 | |
| 952 | spin_lock(&key->user->lock); |
| 953 | key->user->qnkeys--; |
| 954 | key->user->qnbytes -= key->quotalen; |
| 955 | spin_unlock(&key->user->lock); |
| 956 | } |
| 957 | |
| 958 | atomic_dec(&key->user->nkeys); |
| 959 | atomic_inc(&newowner->nkeys); |
| 960 | |
David Howells | 363b02d | 2017-10-04 16:43:25 +0100 | [diff] [blame] | 961 | if (key->state != KEY_IS_UNINSTANTIATED) { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 962 | atomic_dec(&key->user->nikeys); |
| 963 | atomic_inc(&newowner->nikeys); |
| 964 | } |
| 965 | |
| 966 | zapowner = key->user; |
| 967 | key->user = newowner; |
| 968 | key->uid = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* change the GID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 972 | if (group != (gid_t) -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | key->gid = gid; |
| 974 | |
| 975 | ret = 0; |
| 976 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 977 | error_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | up_write(&key->sem); |
| 979 | key_put(key); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 980 | if (zapowner) |
| 981 | key_user_put(zapowner); |
| 982 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return ret; |
| 984 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 985 | quota_overrun: |
| 986 | spin_unlock(&newowner->lock); |
| 987 | zapowner = newowner; |
| 988 | ret = -EDQUOT; |
| 989 | goto error_put; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 990 | } |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 993 | * Change the permission mask on a key. |
| 994 | * |
| 995 | * The key must grant the caller Setattr permission for this to work, though |
| 996 | * the key need not be fully instantiated yet. If the caller does not have |
| 997 | * 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] | 998 | */ |
| 999 | long keyctl_setperm_key(key_serial_t id, key_perm_t perm) |
| 1000 | { |
| 1001 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1002 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | long ret; |
| 1004 | |
| 1005 | ret = -EINVAL; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1006 | if (perm & ~(KEY_POS_ALL | KEY_USR_ALL | KEY_GRP_ALL | KEY_OTH_ALL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | goto error; |
| 1008 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1009 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1010 | KEY_NEED_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1011 | if (IS_ERR(key_ref)) { |
| 1012 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | goto error; |
| 1014 | } |
| 1015 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1016 | key = key_ref_to_ptr(key_ref); |
| 1017 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1018 | /* make the changes with the locks held to prevent chown/chmod races */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | ret = -EACCES; |
| 1020 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1022 | /* if we're not the sysadmin, we can only change a key that we own */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 1023 | if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1024 | key->perm = perm; |
| 1025 | ret = 0; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | up_write(&key->sem); |
| 1029 | key_put(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 1030 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1032 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1034 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1035 | * Get the destination keyring for instantiation and check that the caller has |
| 1036 | * Write permission on it. |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1037 | */ |
| 1038 | static long get_instantiation_keyring(key_serial_t ringid, |
| 1039 | struct request_key_auth *rka, |
| 1040 | struct key **_dest_keyring) |
| 1041 | { |
| 1042 | key_ref_t dkref; |
| 1043 | |
David Howells | eca1bf5 | 2008-12-29 00:41:51 +0000 | [diff] [blame] | 1044 | *_dest_keyring = NULL; |
| 1045 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1046 | /* 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] | 1047 | if (ringid == 0) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1048 | return 0; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1049 | |
| 1050 | /* if a specific keyring is nominated by ID, then use that */ |
| 1051 | if (ringid > 0) { |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1052 | dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1053 | if (IS_ERR(dkref)) |
| 1054 | return PTR_ERR(dkref); |
| 1055 | *_dest_keyring = key_ref_to_ptr(dkref); |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) |
| 1060 | return -EINVAL; |
| 1061 | |
| 1062 | /* otherwise specify the destination keyring recorded in the |
| 1063 | * authorisation key (any KEY_SPEC_*_KEYRING) */ |
| 1064 | if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { |
David Howells | 21279cf | 2009-10-15 10:14:35 +0100 | [diff] [blame] | 1065 | *_dest_keyring = key_get(rka->dest_keyring); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | return -ENOKEY; |
| 1070 | } |
| 1071 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1072 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1073 | * Change the request_key authorisation key on the current process. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1074 | */ |
| 1075 | static int keyctl_change_reqkey_auth(struct key *key) |
| 1076 | { |
| 1077 | struct cred *new; |
| 1078 | |
| 1079 | new = prepare_creds(); |
| 1080 | if (!new) |
| 1081 | return -ENOMEM; |
| 1082 | |
| 1083 | key_put(new->request_key_auth); |
| 1084 | new->request_key_auth = key_get(key); |
| 1085 | |
| 1086 | return commit_creds(new); |
| 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1090 | * Instantiate a key with the specified payload and link the key into the |
| 1091 | * destination keyring if one is given. |
| 1092 | * |
| 1093 | * The caller must have the appropriate instantiation permit set for this to |
| 1094 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1095 | * |
| 1096 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | */ |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1098 | long keyctl_instantiate_key_common(key_serial_t id, |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1099 | struct iov_iter *from, |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1100 | key_serial_t ringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1102 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1103 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1104 | struct key *instkey, *dest_keyring; |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1105 | size_t plen = from ? iov_iter_count(from) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | void *payload; |
| 1107 | long ret; |
| 1108 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1109 | kenter("%d,,%zu,%d", id, plen, ringid); |
| 1110 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1111 | if (!plen) |
| 1112 | from = NULL; |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 1115 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | goto error; |
| 1117 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1118 | /* the appropriate instantiation authorisation key must have been |
| 1119 | * assumed before calling this */ |
| 1120 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1121 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1122 | if (!instkey) |
| 1123 | goto error; |
| 1124 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1125 | rka = instkey->payload.data[0]; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1126 | if (rka->target_key->serial != id) |
| 1127 | goto error; |
| 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | /* pull the payload in if one was supplied */ |
| 1130 | payload = NULL; |
| 1131 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1132 | if (from) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | ret = -ENOMEM; |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 1134 | payload = kvmalloc(plen, GFP_KERNEL); |
| 1135 | if (!payload) |
| 1136 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1138 | ret = -EFAULT; |
Al Viro | cbbd26b | 2016-11-01 22:09:04 -0400 | [diff] [blame] | 1139 | if (!copy_from_iter_full(payload, plen, from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | goto error2; |
| 1141 | } |
| 1142 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1143 | /* find the destination keyring amongst those belonging to the |
| 1144 | * requesting task */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1145 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 1146 | if (ret < 0) |
| 1147 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | |
| 1149 | /* instantiate the key and link it into a keyring */ |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1150 | ret = key_instantiate_and_link(rka->target_key, payload, plen, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1151 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1153 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1154 | |
| 1155 | /* discard the assumed authority if it's just been disabled by |
| 1156 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1157 | if (ret == 0) |
| 1158 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1159 | |
| 1160 | error2: |
Eric Biggers | 57070c8 | 2017-06-08 14:48:57 +0100 | [diff] [blame] | 1161 | if (payload) { |
| 1162 | memzero_explicit(payload, plen); |
| 1163 | kvfree(payload); |
| 1164 | } |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1165 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1167 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | |
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 | * Instantiate a key with the specified payload and link the key into the |
| 1171 | * destination keyring if one is given. |
| 1172 | * |
| 1173 | * The caller must have the appropriate instantiation permit set for this to |
| 1174 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1175 | * |
| 1176 | * If successful, 0 will be returned. |
| 1177 | */ |
| 1178 | long keyctl_instantiate_key(key_serial_t id, |
| 1179 | const void __user *_payload, |
| 1180 | size_t plen, |
| 1181 | key_serial_t ringid) |
| 1182 | { |
| 1183 | if (_payload && plen) { |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1184 | struct iovec iov; |
| 1185 | struct iov_iter from; |
| 1186 | int ret; |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1187 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1188 | ret = import_single_range(WRITE, (void __user *)_payload, plen, |
| 1189 | &iov, &from); |
| 1190 | if (unlikely(ret)) |
| 1191 | return ret; |
| 1192 | |
| 1193 | return keyctl_instantiate_key_common(id, &from, ringid); |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1196 | return keyctl_instantiate_key_common(id, NULL, ringid); |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * Instantiate a key with the specified multipart payload and link the key into |
| 1201 | * the destination keyring if one is given. |
| 1202 | * |
| 1203 | * The caller must have the appropriate instantiation permit set for this to |
| 1204 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1205 | * |
| 1206 | * If successful, 0 will be returned. |
| 1207 | */ |
| 1208 | long keyctl_instantiate_key_iov(key_serial_t id, |
| 1209 | const struct iovec __user *_payload_iov, |
| 1210 | unsigned ioc, |
| 1211 | key_serial_t ringid) |
| 1212 | { |
| 1213 | struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1214 | struct iov_iter from; |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1215 | long ret; |
| 1216 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1217 | if (!_payload_iov) |
| 1218 | ioc = 0; |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1219 | |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1220 | ret = import_iovec(WRITE, _payload_iov, ioc, |
| 1221 | ARRAY_SIZE(iovstack), &iov, &from); |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1222 | if (ret < 0) |
Al Viro | b353a1f | 2015-03-17 09:59:38 -0400 | [diff] [blame] | 1223 | return ret; |
| 1224 | ret = keyctl_instantiate_key_common(id, &from, ringid); |
| 1225 | kfree(iov); |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1226 | return ret; |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1230 | * Negatively instantiate the key with the given timeout (in seconds) and link |
| 1231 | * the key into the destination keyring if one is given. |
| 1232 | * |
| 1233 | * The caller must have the appropriate instantiation permit set for this to |
| 1234 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1235 | * |
| 1236 | * The key and any links to the key will be automatically garbage collected |
| 1237 | * after the timeout expires. |
| 1238 | * |
| 1239 | * Negative keys are used to rate limit repeated request_key() calls by causing |
| 1240 | * them to return -ENOKEY until the negative key expires. |
| 1241 | * |
| 1242 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | */ |
| 1244 | long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) |
| 1245 | { |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1246 | return keyctl_reject_key(id, timeout, ENOKEY, ringid); |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Negatively instantiate the key with the given timeout (in seconds) and error |
| 1251 | * code and link the key into the destination keyring if one is given. |
| 1252 | * |
| 1253 | * The caller must have the appropriate instantiation permit set for this to |
| 1254 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1255 | * |
| 1256 | * The key and any links to the key will be automatically garbage collected |
| 1257 | * after the timeout expires. |
| 1258 | * |
| 1259 | * Negative keys are used to rate limit repeated request_key() calls by causing |
| 1260 | * them to return the specified error code until the negative key expires. |
| 1261 | * |
| 1262 | * If successful, 0 will be returned. |
| 1263 | */ |
| 1264 | long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, |
| 1265 | key_serial_t ringid) |
| 1266 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1267 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1268 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1269 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | long ret; |
| 1271 | |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1272 | kenter("%d,%u,%u,%d", id, timeout, error, ringid); |
| 1273 | |
| 1274 | /* must be a valid error code and mustn't be a kernel special */ |
| 1275 | if (error <= 0 || |
| 1276 | error >= MAX_ERRNO || |
| 1277 | error == ERESTARTSYS || |
| 1278 | error == ERESTARTNOINTR || |
| 1279 | error == ERESTARTNOHAND || |
| 1280 | error == ERESTART_RESTARTBLOCK) |
| 1281 | return -EINVAL; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1282 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1283 | /* the appropriate instantiation authorisation key must have been |
| 1284 | * assumed before calling this */ |
| 1285 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1286 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1287 | if (!instkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1290 | rka = instkey->payload.data[0]; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1291 | if (rka->target_key->serial != id) |
| 1292 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | /* find the destination keyring if present (which must also be |
| 1295 | * writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1296 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 1297 | if (ret < 0) |
| 1298 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | |
| 1300 | /* instantiate the key and link it into a keyring */ |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1301 | ret = key_reject_and_link(rka->target_key, timeout, error, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1302 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1304 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1305 | |
| 1306 | /* discard the assumed authority if it's just been disabled by |
| 1307 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1308 | if (ret == 0) |
| 1309 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1310 | |
| 1311 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1313 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1316 | * Read or set the default keyring in which request_key() will cache keys and |
| 1317 | * return the old setting. |
| 1318 | * |
Eric Biggers | c9f838d | 2017-04-18 15:31:09 +0100 | [diff] [blame] | 1319 | * If a thread or process keyring is specified then it will be created if it |
| 1320 | * doesn't yet exist. The old setting will be returned if successful. |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1321 | */ |
| 1322 | long keyctl_set_reqkey_keyring(int reqkey_defl) |
| 1323 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1324 | struct cred *new; |
| 1325 | int ret, old_setting; |
| 1326 | |
| 1327 | old_setting = current_cred_xxx(jit_keyring); |
| 1328 | |
| 1329 | if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) |
| 1330 | return old_setting; |
| 1331 | |
| 1332 | new = prepare_creds(); |
| 1333 | if (!new) |
| 1334 | return -ENOMEM; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1335 | |
| 1336 | switch (reqkey_defl) { |
| 1337 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1338 | ret = install_thread_keyring_to_cred(new); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1339 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1340 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1341 | goto set; |
| 1342 | |
| 1343 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1344 | ret = install_process_keyring_to_cred(new); |
Eric Biggers | c9f838d | 2017-04-18 15:31:09 +0100 | [diff] [blame] | 1345 | if (ret < 0) |
| 1346 | goto error; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1347 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1348 | |
| 1349 | case KEY_REQKEY_DEFL_DEFAULT: |
| 1350 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 1351 | case KEY_REQKEY_DEFL_USER_KEYRING: |
| 1352 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1353 | case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: |
| 1354 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1355 | |
| 1356 | case KEY_REQKEY_DEFL_NO_CHANGE: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1357 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
| 1358 | default: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1359 | ret = -EINVAL; |
| 1360 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1361 | } |
| 1362 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1363 | set: |
| 1364 | new->jit_keyring = reqkey_defl; |
| 1365 | commit_creds(new); |
| 1366 | return old_setting; |
| 1367 | error: |
| 1368 | abort_creds(new); |
Dan Carpenter | 4303ef1 | 2010-06-11 17:30:05 +0100 | [diff] [blame] | 1369 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1370 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1371 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1372 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1373 | * Set or clear the timeout on a key. |
| 1374 | * |
| 1375 | * Either the key must grant the caller Setattr permission or else the caller |
| 1376 | * must hold an instantiation authorisation token for the key. |
| 1377 | * |
| 1378 | * The timeout is either 0 to clear the timeout, or a number of seconds from |
| 1379 | * the current time. The key and any links to the key will be automatically |
| 1380 | * garbage collected after the timeout expires. |
| 1381 | * |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 1382 | * Keys with KEY_FLAG_KEEP set should not be timed out. |
| 1383 | * |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1384 | * If successful, 0 is returned. |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1385 | */ |
| 1386 | long keyctl_set_timeout(key_serial_t id, unsigned timeout) |
| 1387 | { |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1388 | struct key *key, *instkey; |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1389 | key_ref_t key_ref; |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1390 | long ret; |
| 1391 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1392 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1393 | KEY_NEED_SETATTR); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1394 | if (IS_ERR(key_ref)) { |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1395 | /* setting the timeout on a key under construction is permitted |
| 1396 | * if we have the authorisation token handy */ |
| 1397 | if (PTR_ERR(key_ref) == -EACCES) { |
| 1398 | instkey = key_get_instantiation_authkey(id); |
| 1399 | if (!IS_ERR(instkey)) { |
| 1400 | key_put(instkey); |
| 1401 | key_ref = lookup_user_key(id, |
| 1402 | KEY_LOOKUP_PARTIAL, |
| 1403 | 0); |
| 1404 | if (!IS_ERR(key_ref)) |
| 1405 | goto okay; |
| 1406 | } |
| 1407 | } |
| 1408 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1409 | ret = PTR_ERR(key_ref); |
| 1410 | goto error; |
| 1411 | } |
| 1412 | |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1413 | okay: |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1414 | key = key_ref_to_ptr(key_ref); |
Mimi Zohar | 1d6d167 | 2016-01-07 07:46:36 -0500 | [diff] [blame] | 1415 | ret = 0; |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 1416 | if (test_bit(KEY_FLAG_KEEP, &key->flags)) |
| 1417 | ret = -EPERM; |
Mimi Zohar | 1d6d167 | 2016-01-07 07:46:36 -0500 | [diff] [blame] | 1418 | else |
Mimi Zohar | d3600bc | 2015-11-10 08:34:46 -0500 | [diff] [blame] | 1419 | key_set_timeout(key, timeout); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1420 | key_put(key); |
| 1421 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1422 | error: |
| 1423 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1424 | } |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1425 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1426 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1427 | * Assume (or clear) the authority to instantiate the specified key. |
| 1428 | * |
| 1429 | * This sets the authoritative token currently in force for key instantiation. |
| 1430 | * This must be done for a key to be instantiated. It has the effect of making |
| 1431 | * available all the keys from the caller of the request_key() that created a |
| 1432 | * key to request_key() calls made by the caller of this function. |
| 1433 | * |
| 1434 | * The caller must have the instantiation key in their process keyrings with a |
| 1435 | * Search permission grant available to the caller. |
| 1436 | * |
| 1437 | * If the ID given is 0, then the setting will be cleared and 0 returned. |
| 1438 | * |
| 1439 | * If the ID given has a matching an authorisation key, then that key will be |
| 1440 | * set and its ID will be returned. The authorisation key can be read to get |
| 1441 | * the callout information passed to request_key(). |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1442 | */ |
| 1443 | long keyctl_assume_authority(key_serial_t id) |
| 1444 | { |
| 1445 | struct key *authkey; |
| 1446 | long ret; |
| 1447 | |
| 1448 | /* special key IDs aren't permitted */ |
| 1449 | ret = -EINVAL; |
| 1450 | if (id < 0) |
| 1451 | goto error; |
| 1452 | |
| 1453 | /* we divest ourselves of authority if given an ID of 0 */ |
| 1454 | if (id == 0) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1455 | ret = keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1456 | goto error; |
| 1457 | } |
| 1458 | |
| 1459 | /* attempt to assume the authority temporarily granted to us whilst we |
| 1460 | * instantiate the specified key |
| 1461 | * - the authorisation key must be in the current task's keyrings |
| 1462 | * somewhere |
| 1463 | */ |
| 1464 | authkey = key_get_instantiation_authkey(id); |
| 1465 | if (IS_ERR(authkey)) { |
| 1466 | ret = PTR_ERR(authkey); |
| 1467 | goto error; |
| 1468 | } |
| 1469 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1470 | ret = keyctl_change_reqkey_auth(authkey); |
Eric Biggers | 884bee0 | 2017-09-18 11:36:12 -0700 | [diff] [blame] | 1471 | if (ret == 0) |
| 1472 | ret = authkey->serial; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1473 | key_put(authkey); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1474 | error: |
| 1475 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1476 | } |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1477 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1478 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1479 | * Get a key's the LSM security label. |
| 1480 | * |
| 1481 | * The key must grant the caller View permission for this to work. |
| 1482 | * |
| 1483 | * If there's a buffer, then up to buflen bytes of data will be placed into it. |
| 1484 | * |
| 1485 | * If successful, the amount of information available will be returned, |
| 1486 | * irrespective of how much was copied (including the terminal NUL). |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1487 | */ |
| 1488 | long keyctl_get_security(key_serial_t keyid, |
| 1489 | char __user *buffer, |
| 1490 | size_t buflen) |
| 1491 | { |
| 1492 | struct key *key, *instkey; |
| 1493 | key_ref_t key_ref; |
| 1494 | char *context; |
| 1495 | long ret; |
| 1496 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1497 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1498 | if (IS_ERR(key_ref)) { |
| 1499 | if (PTR_ERR(key_ref) != -EACCES) |
| 1500 | return PTR_ERR(key_ref); |
| 1501 | |
| 1502 | /* viewing a key under construction is also permitted if we |
| 1503 | * have the authorisation token handy */ |
| 1504 | instkey = key_get_instantiation_authkey(keyid); |
| 1505 | if (IS_ERR(instkey)) |
Roel Kluin | fa1cc7b | 2009-12-15 15:05:12 -0800 | [diff] [blame] | 1506 | return PTR_ERR(instkey); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1507 | key_put(instkey); |
| 1508 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1509 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, 0); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1510 | if (IS_ERR(key_ref)) |
| 1511 | return PTR_ERR(key_ref); |
| 1512 | } |
| 1513 | |
| 1514 | key = key_ref_to_ptr(key_ref); |
| 1515 | ret = security_key_getsecurity(key, &context); |
| 1516 | if (ret == 0) { |
| 1517 | /* if no information was returned, give userspace an empty |
| 1518 | * string */ |
| 1519 | ret = 1; |
| 1520 | if (buffer && buflen > 0 && |
| 1521 | copy_to_user(buffer, "", 1) != 0) |
| 1522 | ret = -EFAULT; |
| 1523 | } else if (ret > 0) { |
| 1524 | /* return as much data as there's room for */ |
| 1525 | if (buffer && buflen > 0) { |
| 1526 | if (buflen > ret) |
| 1527 | buflen = ret; |
| 1528 | |
| 1529 | if (copy_to_user(buffer, context, buflen) != 0) |
| 1530 | ret = -EFAULT; |
| 1531 | } |
| 1532 | |
| 1533 | kfree(context); |
| 1534 | } |
| 1535 | |
| 1536 | key_ref_put(key_ref); |
| 1537 | return ret; |
| 1538 | } |
| 1539 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1540 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1541 | * Attempt to install the calling process's session keyring on the process's |
| 1542 | * parent process. |
| 1543 | * |
| 1544 | * The keyring must exist and must grant the caller LINK permission, and the |
| 1545 | * parent process must be single-threaded and must have the same effective |
| 1546 | * ownership as this process and mustn't be SUID/SGID. |
| 1547 | * |
| 1548 | * The keyring will be emplaced on the parent when it next resumes userspace. |
| 1549 | * |
| 1550 | * If successful, 0 will be returned. |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1551 | */ |
| 1552 | long keyctl_session_to_parent(void) |
| 1553 | { |
| 1554 | struct task_struct *me, *parent; |
| 1555 | const struct cred *mycred, *pcred; |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 1556 | struct callback_head *newwork, *oldwork; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1557 | key_ref_t keyring_r; |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1558 | struct cred *cred; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1559 | int ret; |
| 1560 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1561 | 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] | 1562 | if (IS_ERR(keyring_r)) |
| 1563 | return PTR_ERR(keyring_r); |
| 1564 | |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1565 | ret = -ENOMEM; |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1566 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1567 | /* our parent is going to need a new cred struct, a new tgcred struct |
| 1568 | * and new security data, so we allocate them here to prevent ENOMEM in |
| 1569 | * our parent */ |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1570 | cred = cred_alloc_blank(); |
| 1571 | if (!cred) |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 1572 | goto error_keyring; |
| 1573 | newwork = &cred->rcu; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1574 | |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 1575 | cred->session_keyring = key_ref_to_ptr(keyring_r); |
| 1576 | keyring_r = NULL; |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 1577 | init_task_work(newwork, key_change_session_keyring); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1578 | |
| 1579 | me = current; |
David Howells | 9d1ac65 | 2010-09-10 09:59:46 +0100 | [diff] [blame] | 1580 | rcu_read_lock(); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1581 | write_lock_irq(&tasklist_lock); |
| 1582 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1583 | ret = -EPERM; |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1584 | oldwork = NULL; |
David Howells | 7936d16 | 2019-05-22 14:09:29 +0100 | [diff] [blame] | 1585 | parent = rcu_dereference_protected(me->real_parent, |
| 1586 | lockdep_is_held(&tasklist_lock)); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* the parent mustn't be init and mustn't be a kernel thread */ |
| 1589 | if (parent->pid <= 1 || !parent->mm) |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1590 | goto unlock; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1591 | |
| 1592 | /* the parent must be single threaded */ |
Oleg Nesterov | dd98acf | 2010-05-26 14:43:23 -0700 | [diff] [blame] | 1593 | if (!thread_group_empty(parent)) |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1594 | goto unlock; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1595 | |
| 1596 | /* the parent and the child must have different session keyrings or |
| 1597 | * there's no point */ |
| 1598 | mycred = current_cred(); |
| 1599 | pcred = __task_cred(parent); |
| 1600 | if (mycred == pcred || |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 1601 | mycred->session_keyring == pcred->session_keyring) { |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1602 | ret = 0; |
| 1603 | goto unlock; |
| 1604 | } |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1605 | |
| 1606 | /* the parent must have the same effective ownership and mustn't be |
| 1607 | * SUID/SGID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 1608 | if (!uid_eq(pcred->uid, mycred->euid) || |
| 1609 | !uid_eq(pcred->euid, mycred->euid) || |
| 1610 | !uid_eq(pcred->suid, mycred->euid) || |
| 1611 | !gid_eq(pcred->gid, mycred->egid) || |
| 1612 | !gid_eq(pcred->egid, mycred->egid) || |
| 1613 | !gid_eq(pcred->sgid, mycred->egid)) |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1614 | goto unlock; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1615 | |
| 1616 | /* the keyrings must have the same UID */ |
David Howells | 3a50597 | 2012-10-02 19:24:29 +0100 | [diff] [blame] | 1617 | if ((pcred->session_keyring && |
Linus Torvalds | 2a74dbb | 2012-12-16 15:40:50 -0800 | [diff] [blame] | 1618 | !uid_eq(pcred->session_keyring->uid, mycred->euid)) || |
| 1619 | !uid_eq(mycred->session_keyring->uid, mycred->euid)) |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1620 | goto unlock; |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1621 | |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1622 | /* cancel an already pending keyring replacement */ |
| 1623 | oldwork = task_work_cancel(parent, key_change_session_keyring); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1624 | |
| 1625 | /* the replacement session keyring is applied just prior to userspace |
| 1626 | * restarting */ |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 1627 | ret = task_work_add(parent, newwork, true); |
Oleg Nesterov | 413cd3d | 2012-05-11 10:59:08 +1000 | [diff] [blame] | 1628 | if (!ret) |
| 1629 | newwork = NULL; |
| 1630 | unlock: |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1631 | write_unlock_irq(&tasklist_lock); |
David Howells | 9d1ac65 | 2010-09-10 09:59:46 +0100 | [diff] [blame] | 1632 | rcu_read_unlock(); |
Al Viro | 67d1214 | 2012-06-27 11:07:19 +0400 | [diff] [blame] | 1633 | if (oldwork) |
| 1634 | put_cred(container_of(oldwork, struct cred, rcu)); |
| 1635 | if (newwork) |
| 1636 | put_cred(cred); |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1637 | return ret; |
| 1638 | |
| 1639 | error_keyring: |
| 1640 | key_ref_put(keyring_r); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1644 | /* |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1645 | * Apply a restriction to a given keyring. |
| 1646 | * |
| 1647 | * The caller must have Setattr permission to change keyring restrictions. |
| 1648 | * |
| 1649 | * 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] | 1650 | * to link to the keyring. In this case, _restriction must also be NULL. |
| 1651 | * Otherwise, both _type and _restriction must be non-NULL. |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1652 | * |
| 1653 | * Returns 0 if successful. |
| 1654 | */ |
| 1655 | long keyctl_restrict_keyring(key_serial_t id, const char __user *_type, |
| 1656 | const char __user *_restriction) |
| 1657 | { |
| 1658 | key_ref_t key_ref; |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1659 | char type[32]; |
| 1660 | char *restriction = NULL; |
| 1661 | long ret; |
| 1662 | |
| 1663 | key_ref = lookup_user_key(id, 0, KEY_NEED_SETATTR); |
| 1664 | if (IS_ERR(key_ref)) |
| 1665 | return PTR_ERR(key_ref); |
| 1666 | |
Eric Biggers | 18026d8 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 1667 | ret = -EINVAL; |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1668 | if (_type) { |
Eric Biggers | 18026d8 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 1669 | if (!_restriction) |
| 1670 | goto error; |
| 1671 | |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1672 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
| 1673 | if (ret < 0) |
| 1674 | goto error; |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1675 | |
| 1676 | restriction = strndup_user(_restriction, PAGE_SIZE); |
| 1677 | if (IS_ERR(restriction)) { |
| 1678 | ret = PTR_ERR(restriction); |
| 1679 | goto error; |
| 1680 | } |
Eric Biggers | 18026d8 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 1681 | } else { |
| 1682 | if (_restriction) |
| 1683 | goto error; |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1684 | } |
| 1685 | |
Eric Biggers | 18026d8 | 2017-12-08 15:13:29 +0000 | [diff] [blame] | 1686 | ret = keyring_restrict(key_ref, _type ? type : NULL, restriction); |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1687 | kfree(restriction); |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1688 | error: |
| 1689 | key_ref_put(key_ref); |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1690 | return ret; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
David Howells | 45e0f30 | 2019-05-30 14:53:10 +0100 | [diff] [blame] | 1694 | * Get keyrings subsystem capabilities. |
| 1695 | */ |
| 1696 | long keyctl_capabilities(unsigned char __user *_buffer, size_t buflen) |
| 1697 | { |
| 1698 | size_t size = buflen; |
| 1699 | |
| 1700 | if (size > 0) { |
| 1701 | if (size > sizeof(keyrings_capabilities)) |
| 1702 | size = sizeof(keyrings_capabilities); |
| 1703 | if (copy_to_user(_buffer, keyrings_capabilities, size) != 0) |
| 1704 | return -EFAULT; |
| 1705 | if (size < buflen && |
| 1706 | clear_user(_buffer + size, buflen - size) != 0) |
| 1707 | return -EFAULT; |
| 1708 | } |
| 1709 | |
| 1710 | return sizeof(keyrings_capabilities); |
| 1711 | } |
| 1712 | |
| 1713 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1714 | * The key control system call |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1716 | SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, |
| 1717 | unsigned long, arg4, unsigned long, arg5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
| 1719 | switch (option) { |
| 1720 | case KEYCTL_GET_KEYRING_ID: |
| 1721 | return keyctl_get_keyring_ID((key_serial_t) arg2, |
| 1722 | (int) arg3); |
| 1723 | |
| 1724 | case KEYCTL_JOIN_SESSION_KEYRING: |
| 1725 | return keyctl_join_session_keyring((const char __user *) arg2); |
| 1726 | |
| 1727 | case KEYCTL_UPDATE: |
| 1728 | return keyctl_update_key((key_serial_t) arg2, |
| 1729 | (const void __user *) arg3, |
| 1730 | (size_t) arg4); |
| 1731 | |
| 1732 | case KEYCTL_REVOKE: |
| 1733 | return keyctl_revoke_key((key_serial_t) arg2); |
| 1734 | |
| 1735 | case KEYCTL_DESCRIBE: |
| 1736 | return keyctl_describe_key((key_serial_t) arg2, |
| 1737 | (char __user *) arg3, |
| 1738 | (unsigned) arg4); |
| 1739 | |
| 1740 | case KEYCTL_CLEAR: |
| 1741 | return keyctl_keyring_clear((key_serial_t) arg2); |
| 1742 | |
| 1743 | case KEYCTL_LINK: |
| 1744 | return keyctl_keyring_link((key_serial_t) arg2, |
| 1745 | (key_serial_t) arg3); |
| 1746 | |
| 1747 | case KEYCTL_UNLINK: |
| 1748 | return keyctl_keyring_unlink((key_serial_t) arg2, |
| 1749 | (key_serial_t) arg3); |
| 1750 | |
| 1751 | case KEYCTL_SEARCH: |
| 1752 | return keyctl_keyring_search((key_serial_t) arg2, |
| 1753 | (const char __user *) arg3, |
| 1754 | (const char __user *) arg4, |
| 1755 | (key_serial_t) arg5); |
| 1756 | |
| 1757 | case KEYCTL_READ: |
| 1758 | return keyctl_read_key((key_serial_t) arg2, |
| 1759 | (char __user *) arg3, |
| 1760 | (size_t) arg4); |
| 1761 | |
| 1762 | case KEYCTL_CHOWN: |
| 1763 | return keyctl_chown_key((key_serial_t) arg2, |
| 1764 | (uid_t) arg3, |
| 1765 | (gid_t) arg4); |
| 1766 | |
| 1767 | case KEYCTL_SETPERM: |
| 1768 | return keyctl_setperm_key((key_serial_t) arg2, |
| 1769 | (key_perm_t) arg3); |
| 1770 | |
| 1771 | case KEYCTL_INSTANTIATE: |
| 1772 | return keyctl_instantiate_key((key_serial_t) arg2, |
| 1773 | (const void __user *) arg3, |
| 1774 | (size_t) arg4, |
| 1775 | (key_serial_t) arg5); |
| 1776 | |
| 1777 | case KEYCTL_NEGATE: |
| 1778 | return keyctl_negate_key((key_serial_t) arg2, |
| 1779 | (unsigned) arg3, |
| 1780 | (key_serial_t) arg4); |
| 1781 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1782 | case KEYCTL_SET_REQKEY_KEYRING: |
| 1783 | return keyctl_set_reqkey_keyring(arg2); |
| 1784 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1785 | case KEYCTL_SET_TIMEOUT: |
| 1786 | return keyctl_set_timeout((key_serial_t) arg2, |
| 1787 | (unsigned) arg3); |
| 1788 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1789 | case KEYCTL_ASSUME_AUTHORITY: |
| 1790 | return keyctl_assume_authority((key_serial_t) arg2); |
| 1791 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1792 | case KEYCTL_GET_SECURITY: |
| 1793 | return keyctl_get_security((key_serial_t) arg2, |
James Morris | 90bd49a | 2008-12-29 14:35:35 +1100 | [diff] [blame] | 1794 | (char __user *) arg3, |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1795 | (size_t) arg4); |
| 1796 | |
David Howells | ee18d64 | 2009-09-02 09:14:21 +0100 | [diff] [blame] | 1797 | case KEYCTL_SESSION_TO_PARENT: |
| 1798 | return keyctl_session_to_parent(); |
| 1799 | |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1800 | case KEYCTL_REJECT: |
| 1801 | return keyctl_reject_key((key_serial_t) arg2, |
| 1802 | (unsigned) arg3, |
| 1803 | (unsigned) arg4, |
| 1804 | (key_serial_t) arg5); |
| 1805 | |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1806 | case KEYCTL_INSTANTIATE_IOV: |
| 1807 | return keyctl_instantiate_key_iov( |
| 1808 | (key_serial_t) arg2, |
| 1809 | (const struct iovec __user *) arg3, |
| 1810 | (unsigned) arg4, |
| 1811 | (key_serial_t) arg5); |
| 1812 | |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 1813 | case KEYCTL_INVALIDATE: |
| 1814 | return keyctl_invalidate_key((key_serial_t) arg2); |
| 1815 | |
David Howells | f36f8c7 | 2013-09-24 10:35:19 +0100 | [diff] [blame] | 1816 | case KEYCTL_GET_PERSISTENT: |
| 1817 | return keyctl_get_persistent((uid_t)arg2, (key_serial_t)arg3); |
| 1818 | |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 1819 | case KEYCTL_DH_COMPUTE: |
| 1820 | return keyctl_dh_compute((struct keyctl_dh_params __user *) arg2, |
Stephan Mueller | 4693fc7 | 2016-05-26 23:38:12 +0200 | [diff] [blame] | 1821 | (char __user *) arg3, (size_t) arg4, |
Stephan Mueller | f1c316a | 2016-08-19 20:39:09 +0200 | [diff] [blame] | 1822 | (struct keyctl_kdf_params __user *) arg5); |
Mat Martineau | ddbb411 | 2016-04-12 19:54:58 +0100 | [diff] [blame] | 1823 | |
Mat Martineau | 6563c91 | 2017-03-01 16:44:09 -0800 | [diff] [blame] | 1824 | case KEYCTL_RESTRICT_KEYRING: |
| 1825 | return keyctl_restrict_keyring((key_serial_t) arg2, |
| 1826 | (const char __user *) arg3, |
| 1827 | (const char __user *) arg4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | |
David Howells | 00d60fd | 2018-10-09 17:46:59 +0100 | [diff] [blame] | 1829 | case KEYCTL_PKEY_QUERY: |
| 1830 | if (arg3 != 0) |
| 1831 | return -EINVAL; |
| 1832 | return keyctl_pkey_query((key_serial_t)arg2, |
| 1833 | (const char __user *)arg4, |
Ben Dooks | 468e91c | 2019-03-01 11:30:26 +0000 | [diff] [blame] | 1834 | (struct keyctl_pkey_query __user *)arg5); |
David Howells | 00d60fd | 2018-10-09 17:46:59 +0100 | [diff] [blame] | 1835 | |
| 1836 | case KEYCTL_PKEY_ENCRYPT: |
| 1837 | case KEYCTL_PKEY_DECRYPT: |
| 1838 | case KEYCTL_PKEY_SIGN: |
| 1839 | return keyctl_pkey_e_d_s( |
| 1840 | option, |
| 1841 | (const struct keyctl_pkey_params __user *)arg2, |
| 1842 | (const char __user *)arg3, |
| 1843 | (const void __user *)arg4, |
| 1844 | (void __user *)arg5); |
| 1845 | |
| 1846 | case KEYCTL_PKEY_VERIFY: |
| 1847 | return keyctl_pkey_verify( |
| 1848 | (const struct keyctl_pkey_params __user *)arg2, |
| 1849 | (const char __user *)arg3, |
| 1850 | (const void __user *)arg4, |
| 1851 | (const void __user *)arg5); |
| 1852 | |
David Howells | ed0ac5c | 2019-05-20 21:51:50 +0100 | [diff] [blame] | 1853 | case KEYCTL_MOVE: |
| 1854 | return keyctl_keyring_move((key_serial_t)arg2, |
| 1855 | (key_serial_t)arg3, |
| 1856 | (key_serial_t)arg4, |
| 1857 | (unsigned int)arg5); |
| 1858 | |
David Howells | 45e0f30 | 2019-05-30 14:53:10 +0100 | [diff] [blame] | 1859 | case KEYCTL_CAPABILITIES: |
| 1860 | return keyctl_capabilities((unsigned char __user *)arg2, (size_t)arg3); |
| 1861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | default: |
| 1863 | return -EOPNOTSUPP; |
| 1864 | } |
| 1865 | } |