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> |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 21 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 23 | #include <linux/vmalloc.h> |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 24 | #include <linux/security.h> |
Kent Overstreet | a27bb33 | 2013-05-07 16:19:08 -0700 | [diff] [blame] | 25 | #include <linux/uio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/uaccess.h> |
| 27 | #include "internal.h" |
| 28 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 29 | static int key_get_type_from_user(char *type, |
| 30 | const char __user *_type, |
| 31 | unsigned len) |
| 32 | { |
| 33 | int ret; |
| 34 | |
| 35 | ret = strncpy_from_user(type, _type, len); |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 36 | if (ret < 0) |
Dan Carpenter | 4303ef1 | 2010-06-11 17:30:05 +0100 | [diff] [blame] | 37 | return ret; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 38 | if (ret == 0 || ret >= len) |
| 39 | return -EINVAL; |
David Howells | 54e2c2c | 2014-09-16 17:29:03 +0100 | [diff] [blame] | 40 | if (type[0] == '.') |
| 41 | return -EPERM; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 42 | type[len - 1] = '\0'; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 47 | * Extract the description of a new key from userspace and either add it as a |
| 48 | * new key to the specified keyring or update a matching key in that keyring. |
| 49 | * |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 50 | * If the description is NULL or an empty string, the key type is asked to |
| 51 | * generate one from the payload. |
| 52 | * |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 53 | * The keyring must be writable so that we can attach the key to it. |
| 54 | * |
| 55 | * If successful, the new key's serial number is returned, otherwise an error |
| 56 | * code is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 58 | SYSCALL_DEFINE5(add_key, const char __user *, _type, |
| 59 | const char __user *, _description, |
| 60 | const void __user *, _payload, |
| 61 | size_t, plen, |
| 62 | key_serial_t, ringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 64 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | char type[32], *description; |
| 66 | void *payload; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 67 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 68 | bool vm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 71 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | goto error; |
| 73 | |
| 74 | /* draw all the data into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 75 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | if (ret < 0) |
| 77 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 79 | description = NULL; |
| 80 | if (_description) { |
| 81 | description = strndup_user(_description, PAGE_SIZE); |
| 82 | if (IS_ERR(description)) { |
| 83 | ret = PTR_ERR(description); |
| 84 | goto error; |
| 85 | } |
| 86 | if (!*description) { |
| 87 | kfree(description); |
| 88 | description = NULL; |
Mimi Zohar | a4e3b8d | 2014-05-22 14:02:23 -0400 | [diff] [blame] | 89 | } else if ((description[0] == '.') && |
| 90 | (strncmp(type, "keyring", 7) == 0)) { |
| 91 | ret = -EPERM; |
| 92 | goto error2; |
David Howells | cf7f601 | 2012-09-13 13:06:29 +0100 | [diff] [blame] | 93 | } |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 94 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | /* pull the payload in if one was supplied */ |
| 97 | payload = NULL; |
| 98 | |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 99 | vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | if (_payload) { |
| 101 | ret = -ENOMEM; |
Andrew Morton | 4f1c28d | 2012-05-31 16:26:02 -0700 | [diff] [blame] | 102 | payload = kmalloc(plen, GFP_KERNEL | __GFP_NOWARN); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 103 | if (!payload) { |
| 104 | if (plen <= PAGE_SIZE) |
| 105 | goto error2; |
| 106 | vm = true; |
| 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: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 139 | if (!vm) |
| 140 | kfree(payload); |
| 141 | else |
| 142 | vfree(payload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | error2: |
| 144 | kfree(description); |
| 145 | error: |
| 146 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 147 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 150 | * Search the process keyrings and keyring trees linked from those for a |
| 151 | * matching key. Keyrings must have appropriate Search permission to be |
| 152 | * searched. |
| 153 | * |
| 154 | * If a key is found, it will be attached to the destination keyring if there's |
| 155 | * one specified and the serial number of the key will be returned. |
| 156 | * |
| 157 | * If no key is found, /sbin/request-key will be invoked if _callout_info is |
| 158 | * non-NULL in an attempt to create a key. The _callout_info string will be |
| 159 | * passed to /sbin/request-key to aid with completing the request. If the |
| 160 | * _callout_info string is "" then it will be changed to "-". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 162 | SYSCALL_DEFINE4(request_key, const char __user *, _type, |
| 163 | const char __user *, _description, |
| 164 | const char __user *, _callout_info, |
| 165 | key_serial_t, destringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
| 167 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 168 | struct key *key; |
| 169 | key_ref_t dest_ref; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 170 | size_t callout_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | char type[32], *description, *callout_info; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 172 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
| 174 | /* pull the type into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 175 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (ret < 0) |
| 177 | goto error; |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* pull the description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 180 | description = strndup_user(_description, PAGE_SIZE); |
| 181 | if (IS_ERR(description)) { |
| 182 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 184 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | /* pull the callout info into kernel space */ |
| 187 | callout_info = NULL; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 188 | callout_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (_callout_info) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 190 | callout_info = strndup_user(_callout_info, PAGE_SIZE); |
| 191 | if (IS_ERR(callout_info)) { |
| 192 | ret = PTR_ERR(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | goto error2; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 194 | } |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 195 | callout_len = strlen(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 199 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | if (destringid) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 201 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 202 | KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 203 | if (IS_ERR(dest_ref)) { |
| 204 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | goto error3; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* find the key type */ |
| 210 | ktype = key_type_lookup(type); |
| 211 | if (IS_ERR(ktype)) { |
| 212 | ret = PTR_ERR(ktype); |
| 213 | goto error4; |
| 214 | } |
| 215 | |
| 216 | /* do the search */ |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 217 | key = request_key_and_link(ktype, description, callout_info, |
| 218 | callout_len, NULL, key_ref_to_ptr(dest_ref), |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 219 | KEY_ALLOC_IN_QUOTA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (IS_ERR(key)) { |
| 221 | ret = PTR_ERR(key); |
| 222 | goto error5; |
| 223 | } |
| 224 | |
David Howells | 4aab1e8 | 2011-03-11 17:57:33 +0000 | [diff] [blame] | 225 | /* wait for the key to finish being constructed */ |
| 226 | ret = wait_for_key_construction(key, 1); |
| 227 | if (ret < 0) |
| 228 | goto error6; |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | ret = key->serial; |
| 231 | |
David Howells | 4aab1e8 | 2011-03-11 17:57:33 +0000 | [diff] [blame] | 232 | error6: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 233 | key_put(key); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 234 | error5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | key_type_put(ktype); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 236 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 237 | key_ref_put(dest_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 238 | error3: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | kfree(callout_info); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 240 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | kfree(description); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 242 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 247 | * Get the ID of the specified process keyring. |
| 248 | * |
| 249 | * The requested keyring must have search permission to be found. |
| 250 | * |
| 251 | * If successful, the ID of the requested keyring will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | */ |
| 253 | long keyctl_get_keyring_ID(key_serial_t id, int create) |
| 254 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 255 | key_ref_t key_ref; |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 256 | unsigned long lflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | long ret; |
| 258 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 259 | lflags = create ? KEY_LOOKUP_CREATE : 0; |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 260 | key_ref = lookup_user_key(id, lflags, KEY_NEED_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 261 | if (IS_ERR(key_ref)) { |
| 262 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | goto error; |
| 264 | } |
| 265 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 266 | ret = key_ref_to_ptr(key_ref)->serial; |
| 267 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 268 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | return ret; |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 270 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 273 | * Join a (named) session keyring. |
| 274 | * |
| 275 | * Create and join an anonymous session keyring or join a named session |
| 276 | * keyring, creating it if necessary. A named session keyring must have Search |
| 277 | * permission for it to be joined. Session keyrings without this permit will |
| 278 | * be skipped over. |
| 279 | * |
| 280 | * If successful, the ID of the joined session keyring will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | */ |
| 282 | long keyctl_join_session_keyring(const char __user *_name) |
| 283 | { |
| 284 | char *name; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 285 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
| 287 | /* fetch the name from userspace */ |
| 288 | name = NULL; |
| 289 | if (_name) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 290 | name = strndup_user(_name, PAGE_SIZE); |
| 291 | if (IS_ERR(name)) { |
| 292 | ret = PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /* join the session */ |
| 298 | ret = join_session_keyring(name); |
Vegard Nossum | 0d54ee1 | 2009-01-17 17:45:45 +0100 | [diff] [blame] | 299 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 301 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 306 | * Update a key's data payload from the given data. |
| 307 | * |
| 308 | * The key must grant the caller Write permission and the key type must support |
| 309 | * updating for this to work. A negative key can be positively instantiated |
| 310 | * with this call. |
| 311 | * |
| 312 | * If successful, 0 will be returned. If the key type does not support |
| 313 | * updating, then -EOPNOTSUPP will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | */ |
| 315 | long keyctl_update_key(key_serial_t id, |
| 316 | const void __user *_payload, |
| 317 | size_t plen) |
| 318 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 319 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | void *payload; |
| 321 | long ret; |
| 322 | |
| 323 | ret = -EINVAL; |
| 324 | if (plen > PAGE_SIZE) |
| 325 | goto error; |
| 326 | |
| 327 | /* pull the payload in if one was supplied */ |
| 328 | payload = NULL; |
| 329 | if (_payload) { |
| 330 | ret = -ENOMEM; |
| 331 | payload = kmalloc(plen, GFP_KERNEL); |
| 332 | if (!payload) |
| 333 | goto error; |
| 334 | |
| 335 | ret = -EFAULT; |
| 336 | if (copy_from_user(payload, _payload, plen) != 0) |
| 337 | goto error2; |
| 338 | } |
| 339 | |
| 340 | /* find the target key (which must be writable) */ |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 341 | key_ref = lookup_user_key(id, 0, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 342 | if (IS_ERR(key_ref)) { |
| 343 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | goto error2; |
| 345 | } |
| 346 | |
| 347 | /* update the key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 348 | ret = key_update(key_ref, payload, plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 350 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 351 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | kfree(payload); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 353 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 355 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 358 | * Revoke a key. |
| 359 | * |
| 360 | * The key must be grant the caller Write or Setattr permission for this to |
| 361 | * work. The key type should give up its quota claim when revoked. The key |
| 362 | * and any links to the key will be automatically garbage collected after a |
| 363 | * certain amount of time (/proc/sys/kernel/keys/gc_delay). |
| 364 | * |
| 365 | * If successful, 0 is returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | */ |
| 367 | long keyctl_revoke_key(key_serial_t id) |
| 368 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 369 | key_ref_t key_ref; |
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 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 384 | key_revoke(key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | ret = 0; |
| 386 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 387 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 388 | error: |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 389 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 393 | * Invalidate a key. |
| 394 | * |
| 395 | * The key must be grant the caller Invalidate permission for this to work. |
| 396 | * The key and any links to the key will be automatically garbage collected |
| 397 | * immediately. |
| 398 | * |
| 399 | * If successful, 0 is returned. |
| 400 | */ |
| 401 | long keyctl_invalidate_key(key_serial_t id) |
| 402 | { |
| 403 | key_ref_t key_ref; |
| 404 | long ret; |
| 405 | |
| 406 | kenter("%d", id); |
| 407 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 408 | key_ref = lookup_user_key(id, 0, KEY_NEED_SEARCH); |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 409 | if (IS_ERR(key_ref)) { |
| 410 | ret = PTR_ERR(key_ref); |
David Howells | 0c7774a | 2014-07-17 20:45:08 +0100 | [diff] [blame] | 411 | |
| 412 | /* Root is permitted to invalidate certain special keys */ |
| 413 | if (capable(CAP_SYS_ADMIN)) { |
| 414 | key_ref = lookup_user_key(id, 0, 0); |
| 415 | if (IS_ERR(key_ref)) |
| 416 | goto error; |
| 417 | if (test_bit(KEY_FLAG_ROOT_CAN_INVAL, |
| 418 | &key_ref_to_ptr(key_ref)->flags)) |
| 419 | goto invalidate; |
| 420 | goto error_put; |
| 421 | } |
| 422 | |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 423 | goto error; |
| 424 | } |
| 425 | |
David Howells | 0c7774a | 2014-07-17 20:45:08 +0100 | [diff] [blame] | 426 | invalidate: |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 427 | key_invalidate(key_ref_to_ptr(key_ref)); |
| 428 | ret = 0; |
David Howells | 0c7774a | 2014-07-17 20:45:08 +0100 | [diff] [blame] | 429 | error_put: |
David Howells | fd75815 | 2012-05-11 10:56:56 +0100 | [diff] [blame] | 430 | key_ref_put(key_ref); |
| 431 | error: |
| 432 | kleave(" = %ld", ret); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 437 | * Clear the specified keyring, creating an empty process keyring if one of the |
| 438 | * special keyring IDs is used. |
| 439 | * |
| 440 | * The keyring must grant the caller Write permission for this to work. If |
| 441 | * successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | */ |
| 443 | long keyctl_keyring_clear(key_serial_t ringid) |
| 444 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 445 | key_ref_t keyring_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | long ret; |
| 447 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 448 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 449 | if (IS_ERR(keyring_ref)) { |
| 450 | ret = PTR_ERR(keyring_ref); |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 451 | |
| 452 | /* Root is permitted to invalidate certain special keyrings */ |
| 453 | if (capable(CAP_SYS_ADMIN)) { |
| 454 | keyring_ref = lookup_user_key(ringid, 0, 0); |
| 455 | if (IS_ERR(keyring_ref)) |
| 456 | goto error; |
| 457 | if (test_bit(KEY_FLAG_ROOT_CAN_CLEAR, |
| 458 | &key_ref_to_ptr(keyring_ref)->flags)) |
| 459 | goto clear; |
| 460 | goto error_put; |
| 461 | } |
| 462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | goto error; |
| 464 | } |
| 465 | |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 466 | clear: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 467 | ret = keyring_clear(key_ref_to_ptr(keyring_ref)); |
David Howells | 700920e | 2012-01-18 15:31:45 +0000 | [diff] [blame] | 468 | error_put: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 469 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 470 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 475 | * Create a link from a keyring to a key if there's no matching key in the |
| 476 | * keyring, otherwise replace the link to the matching key with a link to the |
| 477 | * new key. |
| 478 | * |
| 479 | * The key must grant the caller Link permission and the the keyring must grant |
| 480 | * the caller Write permission. Furthermore, if an additional link is created, |
| 481 | * the keyring's quota will be extended. |
| 482 | * |
| 483 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | */ |
| 485 | long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) |
| 486 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 487 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | long ret; |
| 489 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 490 | keyring_ref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 491 | if (IS_ERR(keyring_ref)) { |
| 492 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | goto error; |
| 494 | } |
| 495 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 496 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE, KEY_NEED_LINK); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 497 | if (IS_ERR(key_ref)) { |
| 498 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | goto error2; |
| 500 | } |
| 501 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 502 | 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] | 503 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 504 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 505 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 506 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 507 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 512 | * Unlink a key from a keyring. |
| 513 | * |
| 514 | * The keyring must grant the caller Write permission for this to work; the key |
| 515 | * itself need not grant the caller anything. If the last link to a key is |
| 516 | * removed then that key will be scheduled for destruction. |
| 517 | * |
| 518 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | */ |
| 520 | long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) |
| 521 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 522 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | long ret; |
| 524 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 525 | keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 526 | if (IS_ERR(keyring_ref)) { |
| 527 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | goto error; |
| 529 | } |
| 530 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 531 | key_ref = lookup_user_key(id, KEY_LOOKUP_FOR_UNLINK, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 532 | if (IS_ERR(key_ref)) { |
| 533 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | goto error2; |
| 535 | } |
| 536 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 537 | ret = key_unlink(key_ref_to_ptr(keyring_ref), key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 539 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 540 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 541 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 542 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 544 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 547 | * Return a description of a key to userspace. |
| 548 | * |
| 549 | * The key must grant the caller View permission for this to work. |
| 550 | * |
| 551 | * If there's a buffer, we place up to buflen bytes of data into it formatted |
| 552 | * in the following way: |
| 553 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | * type;uid;gid;perm;description<NUL> |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 555 | * |
| 556 | * If successful, we return the amount of description available, irrespective |
| 557 | * of how much we may have copied into the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | */ |
| 559 | long keyctl_describe_key(key_serial_t keyid, |
| 560 | char __user *buffer, |
| 561 | size_t buflen) |
| 562 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 563 | struct key *key, *instkey; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 564 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | char *tmpbuf; |
| 566 | long ret; |
| 567 | |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 568 | key_ref = lookup_user_key(keyid, KEY_LOOKUP_PARTIAL, KEY_NEED_VIEW); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 569 | if (IS_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 570 | /* viewing a key under construction is permitted if we have the |
| 571 | * authorisation token handy */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 572 | if (PTR_ERR(key_ref) == -EACCES) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 573 | instkey = key_get_instantiation_authkey(keyid); |
| 574 | if (!IS_ERR(instkey)) { |
| 575 | key_put(instkey); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 576 | key_ref = lookup_user_key(keyid, |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 577 | KEY_LOOKUP_PARTIAL, |
| 578 | 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 579 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 580 | goto okay; |
| 581 | } |
| 582 | } |
| 583 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 584 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | goto error; |
| 586 | } |
| 587 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 588 | okay: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | /* calculate how much description we're going to return */ |
| 590 | ret = -ENOMEM; |
| 591 | tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 592 | if (!tmpbuf) |
| 593 | goto error2; |
| 594 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 595 | key = key_ref_to_ptr(key_ref); |
| 596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | ret = snprintf(tmpbuf, PAGE_SIZE - 1, |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 598 | "%s;%d;%d;%08x;%s", |
David Howells | 94fd840 | 2010-06-28 14:05:04 +0100 | [diff] [blame] | 599 | key->type->name, |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 600 | from_kuid_munged(current_user_ns(), key->uid), |
| 601 | from_kgid_munged(current_user_ns(), key->gid), |
David Howells | 94fd840 | 2010-06-28 14:05:04 +0100 | [diff] [blame] | 602 | key->perm, |
| 603 | key->description ?: ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* include a NUL char at the end of the data */ |
| 606 | if (ret > PAGE_SIZE - 1) |
| 607 | ret = PAGE_SIZE - 1; |
| 608 | tmpbuf[ret] = 0; |
| 609 | ret++; |
| 610 | |
| 611 | /* consider returning the data */ |
| 612 | if (buffer && buflen > 0) { |
| 613 | if (buflen > ret) |
| 614 | buflen = ret; |
| 615 | |
| 616 | if (copy_to_user(buffer, tmpbuf, buflen) != 0) |
| 617 | ret = -EFAULT; |
| 618 | } |
| 619 | |
| 620 | kfree(tmpbuf); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 621 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 622 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 623 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 628 | * Search the specified keyring and any keyrings it links to for a matching |
| 629 | * key. Only keyrings that grant the caller Search permission will be searched |
| 630 | * (this includes the starting keyring). Only keys with Search permission can |
| 631 | * be found. |
| 632 | * |
| 633 | * If successful, the found key will be linked to the destination keyring if |
| 634 | * supplied and the key has Link permission, and the found key ID will be |
| 635 | * returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | */ |
| 637 | long keyctl_keyring_search(key_serial_t ringid, |
| 638 | const char __user *_type, |
| 639 | const char __user *_description, |
| 640 | key_serial_t destringid) |
| 641 | { |
| 642 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 643 | key_ref_t keyring_ref, key_ref, dest_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | char type[32], *description; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 645 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
| 647 | /* pull the type and description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 648 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (ret < 0) |
| 650 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 652 | description = strndup_user(_description, PAGE_SIZE); |
| 653 | if (IS_ERR(description)) { |
| 654 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 656 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | /* get the keyring at which to begin the search */ |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 659 | keyring_ref = lookup_user_key(ringid, 0, KEY_NEED_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 660 | if (IS_ERR(keyring_ref)) { |
| 661 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | goto error2; |
| 663 | } |
| 664 | |
| 665 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 666 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | if (destringid) { |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 668 | dest_ref = lookup_user_key(destringid, KEY_LOOKUP_CREATE, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 669 | KEY_NEED_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 670 | if (IS_ERR(dest_ref)) { |
| 671 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | goto error3; |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | /* find the key type */ |
| 677 | ktype = key_type_lookup(type); |
| 678 | if (IS_ERR(ktype)) { |
| 679 | ret = PTR_ERR(ktype); |
| 680 | goto error4; |
| 681 | } |
| 682 | |
| 683 | /* do the search */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 684 | key_ref = keyring_search(keyring_ref, ktype, description); |
| 685 | if (IS_ERR(key_ref)) { |
| 686 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | |
| 688 | /* treat lack or presence of a negative key the same */ |
| 689 | if (ret == -EAGAIN) |
| 690 | ret = -ENOKEY; |
| 691 | goto error5; |
| 692 | } |
| 693 | |
| 694 | /* link the resulting key to the destination keyring if we can */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 695 | if (dest_ref) { |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 696 | ret = key_permission(key_ref, KEY_NEED_LINK); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 697 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | goto error6; |
| 699 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 700 | 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] | 701 | if (ret < 0) |
| 702 | goto error6; |
| 703 | } |
| 704 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 705 | ret = key_ref_to_ptr(key_ref)->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 707 | error6: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 708 | key_ref_put(key_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 709 | error5: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | key_type_put(ktype); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 711 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 712 | key_ref_put(dest_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 713 | error3: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 714 | key_ref_put(keyring_ref); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 715 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | kfree(description); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 717 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 722 | * Read a key's payload. |
| 723 | * |
| 724 | * The key must either grant the caller Read permission, or it must grant the |
| 725 | * caller Search permission when searched for from the process keyrings. |
| 726 | * |
| 727 | * If successful, we place up to buflen bytes of data into the buffer, if one |
| 728 | * is provided, and return the amount of data that is available in the key, |
| 729 | * irrespective of how much we copied into the buffer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | */ |
| 731 | long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) |
| 732 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 733 | struct key *key; |
| 734 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | long ret; |
| 736 | |
| 737 | /* find the key first */ |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 738 | key_ref = lookup_user_key(keyid, 0, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 739 | if (IS_ERR(key_ref)) { |
| 740 | ret = -ENOKEY; |
| 741 | goto error; |
| 742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 744 | key = key_ref_to_ptr(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 746 | /* see if we can read it directly */ |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 747 | ret = key_permission(key_ref, KEY_NEED_READ); |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 748 | if (ret == 0) |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 749 | goto can_read_key; |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 750 | if (ret != -EACCES) |
| 751 | goto error; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 752 | |
| 753 | /* we can't; see if it's searchable from this process's keyrings |
| 754 | * - we automatically take account of the fact that it may be |
| 755 | * dangling off an instantiation key |
| 756 | */ |
| 757 | if (!is_key_possessed(key_ref)) { |
| 758 | ret = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | goto error2; |
| 760 | } |
| 761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* the key is probably readable - now try to read it */ |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 763 | can_read_key: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | ret = key_validate(key); |
| 765 | if (ret == 0) { |
| 766 | ret = -EOPNOTSUPP; |
| 767 | if (key->type->read) { |
| 768 | /* read the data with the semaphore held (since we |
| 769 | * might sleep) */ |
| 770 | down_read(&key->sem); |
| 771 | ret = key->type->read(key, buffer, buflen); |
| 772 | up_read(&key->sem); |
| 773 | } |
| 774 | } |
| 775 | |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 776 | error2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | key_put(key); |
Justin P. Mattock | c5b60b5 | 2010-04-21 00:02:11 -0700 | [diff] [blame] | 778 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 783 | * Change the ownership of a key |
| 784 | * |
| 785 | * The key must grant the caller Setattr permission for this to work, though |
| 786 | * the key need not be fully instantiated yet. For the UID to be changed, or |
| 787 | * for the GID to be changed to a group the caller is not a member of, the |
| 788 | * caller must have sysadmin capability. If either uid or gid is -1 then that |
| 789 | * attribute is not changed. |
| 790 | * |
| 791 | * If the UID is to be changed, the new user must have sufficient quota to |
| 792 | * accept the key. The quota deduction will be removed from the old user to |
| 793 | * the new user should the attribute be changed. |
| 794 | * |
| 795 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 797 | 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] | 798 | { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 799 | struct key_user *newowner, *zapowner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 801 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | long ret; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 803 | kuid_t uid; |
| 804 | kgid_t gid; |
| 805 | |
| 806 | uid = make_kuid(current_user_ns(), user); |
| 807 | gid = make_kgid(current_user_ns(), group); |
| 808 | ret = -EINVAL; |
| 809 | if ((user != (uid_t) -1) && !uid_valid(uid)) |
| 810 | goto error; |
| 811 | if ((group != (gid_t) -1) && !gid_valid(gid)) |
| 812 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
| 814 | ret = 0; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 815 | if (user == (uid_t) -1 && group == (gid_t) -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | goto error; |
| 817 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 818 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 819 | KEY_NEED_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 820 | if (IS_ERR(key_ref)) { |
| 821 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | goto error; |
| 823 | } |
| 824 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 825 | key = key_ref_to_ptr(key_ref); |
| 826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | /* make the changes with the locks held to prevent chown/chown races */ |
| 828 | ret = -EACCES; |
| 829 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | if (!capable(CAP_SYS_ADMIN)) { |
| 832 | /* only the sysadmin can chown a key to some other UID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 833 | if (user != (uid_t) -1 && !uid_eq(key->uid, uid)) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 834 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | |
| 836 | /* only the sysadmin can set the key's GID to a group other |
| 837 | * than one of those that the current process subscribes to */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 838 | 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] | 839 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 842 | /* change the UID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 843 | if (user != (uid_t) -1 && !uid_eq(uid, key->uid)) { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 844 | ret = -ENOMEM; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 845 | newowner = key_user_lookup(uid); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 846 | if (!newowner) |
| 847 | goto error_put; |
| 848 | |
| 849 | /* transfer the quota burden to the new user */ |
| 850 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 851 | unsigned maxkeys = uid_eq(uid, GLOBAL_ROOT_UID) ? |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 852 | key_quota_root_maxkeys : key_quota_maxkeys; |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 853 | unsigned maxbytes = uid_eq(uid, GLOBAL_ROOT_UID) ? |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 854 | key_quota_root_maxbytes : key_quota_maxbytes; |
| 855 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 856 | spin_lock(&newowner->lock); |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 857 | if (newowner->qnkeys + 1 >= maxkeys || |
| 858 | newowner->qnbytes + key->quotalen >= maxbytes || |
| 859 | newowner->qnbytes + key->quotalen < |
| 860 | newowner->qnbytes) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 861 | goto quota_overrun; |
| 862 | |
| 863 | newowner->qnkeys++; |
| 864 | newowner->qnbytes += key->quotalen; |
| 865 | spin_unlock(&newowner->lock); |
| 866 | |
| 867 | spin_lock(&key->user->lock); |
| 868 | key->user->qnkeys--; |
| 869 | key->user->qnbytes -= key->quotalen; |
| 870 | spin_unlock(&key->user->lock); |
| 871 | } |
| 872 | |
| 873 | atomic_dec(&key->user->nkeys); |
| 874 | atomic_inc(&newowner->nkeys); |
| 875 | |
| 876 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
| 877 | atomic_dec(&key->user->nikeys); |
| 878 | atomic_inc(&newowner->nikeys); |
| 879 | } |
| 880 | |
| 881 | zapowner = key->user; |
| 882 | key->user = newowner; |
| 883 | key->uid = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* change the GID */ |
Eric W. Biederman | 9a56c2d | 2012-02-08 07:53:04 -0800 | [diff] [blame] | 887 | if (group != (gid_t) -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | key->gid = gid; |
| 889 | |
| 890 | ret = 0; |
| 891 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 892 | error_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | up_write(&key->sem); |
| 894 | key_put(key); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 895 | if (zapowner) |
| 896 | key_user_put(zapowner); |
| 897 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | return ret; |
| 899 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 900 | quota_overrun: |
| 901 | spin_unlock(&newowner->lock); |
| 902 | zapowner = newowner; |
| 903 | ret = -EDQUOT; |
| 904 | goto error_put; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 905 | } |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 908 | * Change the permission mask on a key. |
| 909 | * |
| 910 | * The key must grant the caller Setattr permission for this to work, though |
| 911 | * the key need not be fully instantiated yet. If the caller does not have |
| 912 | * 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] | 913 | */ |
| 914 | long keyctl_setperm_key(key_serial_t id, key_perm_t perm) |
| 915 | { |
| 916 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 917 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | long ret; |
| 919 | |
| 920 | ret = -EINVAL; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 921 | 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] | 922 | goto error; |
| 923 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 924 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 925 | KEY_NEED_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 926 | if (IS_ERR(key_ref)) { |
| 927 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | goto error; |
| 929 | } |
| 930 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 931 | key = key_ref_to_ptr(key_ref); |
| 932 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 933 | /* make the changes with the locks held to prevent chown/chmod races */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | ret = -EACCES; |
| 935 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 937 | /* 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] | 938 | if (capable(CAP_SYS_ADMIN) || uid_eq(key->uid, current_fsuid())) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 939 | key->perm = perm; |
| 940 | ret = 0; |
| 941 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | up_write(&key->sem); |
| 944 | key_put(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 945 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 949 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 950 | * Get the destination keyring for instantiation and check that the caller has |
| 951 | * Write permission on it. |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 952 | */ |
| 953 | static long get_instantiation_keyring(key_serial_t ringid, |
| 954 | struct request_key_auth *rka, |
| 955 | struct key **_dest_keyring) |
| 956 | { |
| 957 | key_ref_t dkref; |
| 958 | |
David Howells | eca1bf5 | 2008-12-29 00:41:51 +0000 | [diff] [blame] | 959 | *_dest_keyring = NULL; |
| 960 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 961 | /* 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] | 962 | if (ringid == 0) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 963 | return 0; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 964 | |
| 965 | /* if a specific keyring is nominated by ID, then use that */ |
| 966 | if (ringid > 0) { |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 967 | dkref = lookup_user_key(ringid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 968 | if (IS_ERR(dkref)) |
| 969 | return PTR_ERR(dkref); |
| 970 | *_dest_keyring = key_ref_to_ptr(dkref); |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) |
| 975 | return -EINVAL; |
| 976 | |
| 977 | /* otherwise specify the destination keyring recorded in the |
| 978 | * authorisation key (any KEY_SPEC_*_KEYRING) */ |
| 979 | if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { |
David Howells | 21279cf | 2009-10-15 10:14:35 +0100 | [diff] [blame] | 980 | *_dest_keyring = key_get(rka->dest_keyring); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 981 | return 0; |
| 982 | } |
| 983 | |
| 984 | return -ENOKEY; |
| 985 | } |
| 986 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 987 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 988 | * Change the request_key authorisation key on the current process. |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 989 | */ |
| 990 | static int keyctl_change_reqkey_auth(struct key *key) |
| 991 | { |
| 992 | struct cred *new; |
| 993 | |
| 994 | new = prepare_creds(); |
| 995 | if (!new) |
| 996 | return -ENOMEM; |
| 997 | |
| 998 | key_put(new->request_key_auth); |
| 999 | new->request_key_auth = key_get(key); |
| 1000 | |
| 1001 | return commit_creds(new); |
| 1002 | } |
| 1003 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | /* |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1005 | * Copy the iovec data from userspace |
| 1006 | */ |
| 1007 | static long copy_from_user_iovec(void *buffer, const struct iovec *iov, |
| 1008 | unsigned ioc) |
| 1009 | { |
| 1010 | for (; ioc > 0; ioc--) { |
| 1011 | if (copy_from_user(buffer, iov->iov_base, iov->iov_len) != 0) |
| 1012 | return -EFAULT; |
| 1013 | buffer += iov->iov_len; |
| 1014 | iov++; |
| 1015 | } |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1020 | * Instantiate a key with the specified payload and link the key into the |
| 1021 | * destination keyring if one is given. |
| 1022 | * |
| 1023 | * The caller must have the appropriate instantiation permit set for this to |
| 1024 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1025 | * |
| 1026 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | */ |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1028 | long keyctl_instantiate_key_common(key_serial_t id, |
| 1029 | const struct iovec *payload_iov, |
| 1030 | unsigned ioc, |
| 1031 | size_t plen, |
| 1032 | key_serial_t ringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1034 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1035 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1036 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | void *payload; |
| 1038 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 1039 | bool vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1041 | kenter("%d,,%zu,%d", id, plen, ringid); |
| 1042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 1044 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | goto error; |
| 1046 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1047 | /* the appropriate instantiation authorisation key must have been |
| 1048 | * assumed before calling this */ |
| 1049 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1050 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1051 | if (!instkey) |
| 1052 | goto error; |
| 1053 | |
| 1054 | rka = instkey->payload.data; |
| 1055 | if (rka->target_key->serial != id) |
| 1056 | goto error; |
| 1057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | /* pull the payload in if one was supplied */ |
| 1059 | payload = NULL; |
| 1060 | |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1061 | if (payload_iov) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | ret = -ENOMEM; |
| 1063 | payload = kmalloc(plen, GFP_KERNEL); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 1064 | if (!payload) { |
| 1065 | if (plen <= PAGE_SIZE) |
| 1066 | goto error; |
| 1067 | vm = true; |
| 1068 | payload = vmalloc(plen); |
| 1069 | if (!payload) |
| 1070 | goto error; |
| 1071 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1073 | ret = copy_from_user_iovec(payload, payload_iov, ioc); |
| 1074 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | goto error2; |
| 1076 | } |
| 1077 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1078 | /* find the destination keyring amongst those belonging to the |
| 1079 | * requesting task */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1080 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 1081 | if (ret < 0) |
| 1082 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
| 1084 | /* instantiate the key and link it into a keyring */ |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1085 | ret = key_instantiate_and_link(rka->target_key, payload, plen, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1086 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1088 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1089 | |
| 1090 | /* discard the assumed authority if it's just been disabled by |
| 1091 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1092 | if (ret == 0) |
| 1093 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1094 | |
| 1095 | error2: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 1096 | if (!vm) |
| 1097 | kfree(payload); |
| 1098 | else |
| 1099 | vfree(payload); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1100 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1102 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | /* |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1105 | * Instantiate a key with the specified payload and link the key into the |
| 1106 | * destination keyring if one is given. |
| 1107 | * |
| 1108 | * The caller must have the appropriate instantiation permit set for this to |
| 1109 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1110 | * |
| 1111 | * If successful, 0 will be returned. |
| 1112 | */ |
| 1113 | long keyctl_instantiate_key(key_serial_t id, |
| 1114 | const void __user *_payload, |
| 1115 | size_t plen, |
| 1116 | key_serial_t ringid) |
| 1117 | { |
| 1118 | if (_payload && plen) { |
| 1119 | struct iovec iov[1] = { |
| 1120 | [0].iov_base = (void __user *)_payload, |
| 1121 | [0].iov_len = plen |
| 1122 | }; |
| 1123 | |
| 1124 | return keyctl_instantiate_key_common(id, iov, 1, plen, ringid); |
| 1125 | } |
| 1126 | |
| 1127 | return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); |
| 1128 | } |
| 1129 | |
| 1130 | /* |
| 1131 | * Instantiate a key with the specified multipart payload and link the key into |
| 1132 | * the destination keyring if one is given. |
| 1133 | * |
| 1134 | * The caller must have the appropriate instantiation permit set for this to |
| 1135 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1136 | * |
| 1137 | * If successful, 0 will be returned. |
| 1138 | */ |
| 1139 | long keyctl_instantiate_key_iov(key_serial_t id, |
| 1140 | const struct iovec __user *_payload_iov, |
| 1141 | unsigned ioc, |
| 1142 | key_serial_t ringid) |
| 1143 | { |
| 1144 | struct iovec iovstack[UIO_FASTIOV], *iov = iovstack; |
| 1145 | long ret; |
| 1146 | |
David Howells | 423b9788 | 2012-05-21 12:32:13 +0100 | [diff] [blame] | 1147 | if (!_payload_iov || !ioc) |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1148 | goto no_payload; |
| 1149 | |
| 1150 | ret = rw_copy_check_uvector(WRITE, _payload_iov, ioc, |
Christopher Yeoh | ac34ebb | 2012-05-31 16:26:42 -0700 | [diff] [blame] | 1151 | ARRAY_SIZE(iovstack), iovstack, &iov); |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1152 | if (ret < 0) |
Alan Cox | a84a921 | 2012-09-28 12:20:02 +0100 | [diff] [blame] | 1153 | goto err; |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1154 | if (ret == 0) |
| 1155 | goto no_payload_free; |
| 1156 | |
| 1157 | ret = keyctl_instantiate_key_common(id, iov, ioc, ret, ringid); |
Alan Cox | a84a921 | 2012-09-28 12:20:02 +0100 | [diff] [blame] | 1158 | err: |
David Howells | ee009e4a0 | 2011-03-07 15:06:20 +0000 | [diff] [blame] | 1159 | if (iov != iovstack) |
| 1160 | kfree(iov); |
| 1161 | return ret; |
| 1162 | |
| 1163 | no_payload_free: |
| 1164 | if (iov != iovstack) |
| 1165 | kfree(iov); |
| 1166 | no_payload: |
| 1167 | return keyctl_instantiate_key_common(id, NULL, 0, 0, ringid); |
| 1168 | } |
| 1169 | |
| 1170 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1171 | * Negatively instantiate the key with the given timeout (in seconds) and link |
| 1172 | * the key into the destination keyring if one is given. |
| 1173 | * |
| 1174 | * The caller must have the appropriate instantiation permit set for this to |
| 1175 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1176 | * |
| 1177 | * The key and any links to the key will be automatically garbage collected |
| 1178 | * after the timeout expires. |
| 1179 | * |
| 1180 | * Negative keys are used to rate limit repeated request_key() calls by causing |
| 1181 | * them to return -ENOKEY until the negative key expires. |
| 1182 | * |
| 1183 | * If successful, 0 will be returned. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | */ |
| 1185 | long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) |
| 1186 | { |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1187 | return keyctl_reject_key(id, timeout, ENOKEY, ringid); |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Negatively instantiate the key with the given timeout (in seconds) and error |
| 1192 | * code and link the key into the destination keyring if one is given. |
| 1193 | * |
| 1194 | * The caller must have the appropriate instantiation permit set for this to |
| 1195 | * work (see keyctl_assume_authority). No other permissions are required. |
| 1196 | * |
| 1197 | * The key and any links to the key will be automatically garbage collected |
| 1198 | * after the timeout expires. |
| 1199 | * |
| 1200 | * Negative keys are used to rate limit repeated request_key() calls by causing |
| 1201 | * them to return the specified error code until the negative key expires. |
| 1202 | * |
| 1203 | * If successful, 0 will be returned. |
| 1204 | */ |
| 1205 | long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error, |
| 1206 | key_serial_t ringid) |
| 1207 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1208 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1209 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1210 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | long ret; |
| 1212 | |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1213 | kenter("%d,%u,%u,%d", id, timeout, error, ringid); |
| 1214 | |
| 1215 | /* must be a valid error code and mustn't be a kernel special */ |
| 1216 | if (error <= 0 || |
| 1217 | error >= MAX_ERRNO || |
| 1218 | error == ERESTARTSYS || |
| 1219 | error == ERESTARTNOINTR || |
| 1220 | error == ERESTARTNOHAND || |
| 1221 | error == ERESTART_RESTARTBLOCK) |
| 1222 | return -EINVAL; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1223 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1224 | /* the appropriate instantiation authorisation key must have been |
| 1225 | * assumed before calling this */ |
| 1226 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1227 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1228 | if (!instkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1231 | rka = instkey->payload.data; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1232 | if (rka->target_key->serial != id) |
| 1233 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | /* find the destination keyring if present (which must also be |
| 1236 | * writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1237 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 1238 | if (ret < 0) |
| 1239 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
| 1241 | /* instantiate the key and link it into a keyring */ |
David Howells | fdd1b94 | 2011-03-07 15:06:09 +0000 | [diff] [blame] | 1242 | ret = key_reject_and_link(rka->target_key, timeout, error, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1243 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1245 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1246 | |
| 1247 | /* discard the assumed authority if it's just been disabled by |
| 1248 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1249 | if (ret == 0) |
| 1250 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1251 | |
| 1252 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1254 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1257 | * Read or set the default keyring in which request_key() will cache keys and |
| 1258 | * return the old setting. |
| 1259 | * |
| 1260 | * If a process keyring is specified then this will be created if it doesn't |
| 1261 | * yet exist. The old setting will be returned if successful. |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1262 | */ |
| 1263 | long keyctl_set_reqkey_keyring(int reqkey_defl) |
| 1264 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1265 | struct cred *new; |
| 1266 | int ret, old_setting; |
| 1267 | |
| 1268 | old_setting = current_cred_xxx(jit_keyring); |
| 1269 | |
| 1270 | if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) |
| 1271 | return old_setting; |
| 1272 | |
| 1273 | new = prepare_creds(); |
| 1274 | if (!new) |
| 1275 | return -ENOMEM; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1276 | |
| 1277 | switch (reqkey_defl) { |
| 1278 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1279 | ret = install_thread_keyring_to_cred(new); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1280 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1281 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1282 | goto set; |
| 1283 | |
| 1284 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1285 | ret = install_process_keyring_to_cred(new); |
| 1286 | if (ret < 0) { |
| 1287 | if (ret != -EEXIST) |
| 1288 | goto error; |
| 1289 | ret = 0; |
| 1290 | } |
| 1291 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1292 | |
| 1293 | case KEY_REQKEY_DEFL_DEFAULT: |
| 1294 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 1295 | case KEY_REQKEY_DEFL_USER_KEYRING: |
| 1296 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1297 | case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: |
| 1298 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1299 | |
| 1300 | case KEY_REQKEY_DEFL_NO_CHANGE: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1301 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
| 1302 | default: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1303 | ret = -EINVAL; |
| 1304 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1305 | } |
| 1306 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1307 | set: |
| 1308 | new->jit_keyring = reqkey_defl; |
| 1309 | commit_creds(new); |
| 1310 | return old_setting; |
| 1311 | error: |
| 1312 | abort_creds(new); |
Dan Carpenter | 4303ef1 | 2010-06-11 17:30:05 +0100 | [diff] [blame] | 1313 | return ret; |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1314 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1315 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1316 | /* |
David Howells | 973c9f4 | 2011-01-20 16:38:33 +0000 | [diff] [blame] | 1317 | * Set or clear the timeout on a key. |
| 1318 | * |
| 1319 | * Either the key must grant the caller Setattr permission or else the caller |
| 1320 | * must hold an instantiation authorisation token for the key. |
| 1321 | * |
| 1322 | * The timeout is either 0 to clear the timeout, or a number of seconds from |
| 1323 | * the current time. The key and any links to the key will be automatically |
| 1324 | * garbage collected after the timeout expires. |
| 1325 | * |
| 1326 | * If successful, 0 is returned. |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1327 | */ |
| 1328 | long keyctl_set_timeout(key_serial_t id, unsigned timeout) |
| 1329 | { |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1330 | struct key *key, *instkey; |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1331 | key_ref_t key_ref; |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1332 | long ret; |
| 1333 | |
David Howells | 5593122 | 2009-09-02 09:13:45 +0100 | [diff] [blame] | 1334 | key_ref = lookup_user_key(id, KEY_LOOKUP_CREATE | KEY_LOOKUP_PARTIAL, |
David Howells | f589594 | 2014-03-14 17:44:49 +0000 | [diff] [blame] | 1335 | KEY_NEED_SETATTR); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1336 | if (IS_ERR(key_ref)) { |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1337 | /* setting the timeout on a key under construction is permitted |
| 1338 | * if we have the authorisation token handy */ |
| 1339 | if (PTR_ERR(key_ref) == -EACCES) { |
| 1340 | instkey = key_get_instantiation_authkey(id); |
| 1341 | if (!IS_ERR(instkey)) { |
| 1342 | key_put(instkey); |
| 1343 | key_ref = lookup_user_key(id, |
| 1344 | KEY_LOOKUP_PARTIAL, |
| 1345 | 0); |
| 1346 | if (!IS_ERR(key_ref)) |
| 1347 | goto okay; |
| 1348 | } |
| 1349 | } |
| 1350 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1351 | ret = PTR_ERR(key_ref); |
| 1352 | goto error; |
| 1353 | } |
| 1354 | |
David Howells | 9156235 | 2010-06-11 17:31:05 +0100 | [diff] [blame] | 1355 | okay: |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1356 | key = key_ref_to_ptr(key_ref); |
Bryan Schumaker | 59e6b9c | 2012-02-24 14:14:50 -0500 | [diff] [blame] | 1357 | key_set_timeout(key, timeout); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1358 | key_put(key); |
| 1359 | |
| 1360 | ret = 0; |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | default: |
| 1691 | return -EOPNOTSUPP; |
| 1692 | } |
David Howells | a8b17ed | 2011-01-20 16:38:27 +0000 | [diff] [blame] | 1693 | } |