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