Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* keyctl.c: userspace keyctl operations |
| 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> |
| 17 | #include <linux/keyctl.h> |
| 18 | #include <linux/fs.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 19 | #include <linux/capability.h> |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 20 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/err.h> |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 22 | #include <linux/vmalloc.h> |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 23 | #include <linux/security.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <asm/uaccess.h> |
| 25 | #include "internal.h" |
| 26 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 27 | static int key_get_type_from_user(char *type, |
| 28 | const char __user *_type, |
| 29 | unsigned len) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | ret = strncpy_from_user(type, _type, len); |
| 34 | |
| 35 | if (ret < 0) |
| 36 | return -EFAULT; |
| 37 | |
| 38 | if (ret == 0 || ret >= len) |
| 39 | return -EINVAL; |
| 40 | |
| 41 | if (type[0] == '.') |
| 42 | return -EPERM; |
| 43 | |
| 44 | type[len - 1] = '\0'; |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | /*****************************************************************************/ |
| 50 | /* |
| 51 | * extract the description of a new key from userspace and either add it as a |
| 52 | * new key to the specified keyring or update a matching key in that keyring |
| 53 | * - the keyring must be writable |
| 54 | * - returns the new key's serial number |
| 55 | * - implements add_key() |
| 56 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 57 | SYSCALL_DEFINE5(add_key, const char __user *, _type, |
| 58 | const char __user *, _description, |
| 59 | const void __user *, _payload, |
| 60 | size_t, plen, |
| 61 | key_serial_t, ringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 63 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | char type[32], *description; |
| 65 | void *payload; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 66 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 67 | bool vm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 70 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | goto error; |
| 72 | |
| 73 | /* draw all the data into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 74 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (ret < 0) |
| 76 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 78 | description = strndup_user(_description, PAGE_SIZE); |
| 79 | if (IS_ERR(description)) { |
| 80 | ret = PTR_ERR(description); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 81 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 82 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | /* pull the payload in if one was supplied */ |
| 85 | payload = NULL; |
| 86 | |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 87 | vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (_payload) { |
| 89 | ret = -ENOMEM; |
| 90 | payload = kmalloc(plen, GFP_KERNEL); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 91 | if (!payload) { |
| 92 | if (plen <= PAGE_SIZE) |
| 93 | goto error2; |
| 94 | vm = true; |
| 95 | payload = vmalloc(plen); |
| 96 | if (!payload) |
| 97 | goto error2; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | ret = -EFAULT; |
| 101 | if (copy_from_user(payload, _payload, plen) != 0) |
| 102 | goto error3; |
| 103 | } |
| 104 | |
| 105 | /* find the target keyring (which must be writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 106 | keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 107 | if (IS_ERR(keyring_ref)) { |
| 108 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | goto error3; |
| 110 | } |
| 111 | |
| 112 | /* create or update the requested key and add it to the target |
| 113 | * keyring */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 114 | key_ref = key_create_or_update(keyring_ref, type, description, |
Arun Raghavan | 6b79ccb | 2008-04-29 01:01:28 -0700 | [diff] [blame] | 115 | payload, plen, KEY_PERM_UNDEF, |
| 116 | KEY_ALLOC_IN_QUOTA); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 117 | if (!IS_ERR(key_ref)) { |
| 118 | ret = key_ref_to_ptr(key_ref)->serial; |
| 119 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | } |
| 121 | else { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 122 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 125 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | error3: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 127 | if (!vm) |
| 128 | kfree(payload); |
| 129 | else |
| 130 | vfree(payload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | error2: |
| 132 | kfree(description); |
| 133 | error: |
| 134 | return ret; |
| 135 | |
| 136 | } /* end sys_add_key() */ |
| 137 | |
| 138 | /*****************************************************************************/ |
| 139 | /* |
| 140 | * search the process keyrings for a matching key |
| 141 | * - nested keyrings may also be searched if they have Search permission |
| 142 | * - if a key is found, it will be attached to the destination keyring if |
| 143 | * there's one specified |
| 144 | * - /sbin/request-key will be invoked if _callout_info is non-NULL |
| 145 | * - the _callout_info string will be passed to /sbin/request-key |
| 146 | * - if the _callout_info string is empty, it will be rendered as "-" |
| 147 | * - implements request_key() |
| 148 | */ |
Heiko Carstens | 1e7bfb2 | 2009-01-14 14:14:29 +0100 | [diff] [blame] | 149 | SYSCALL_DEFINE4(request_key, const char __user *, _type, |
| 150 | const char __user *, _description, |
| 151 | const char __user *, _callout_info, |
| 152 | key_serial_t, destringid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 155 | struct key *key; |
| 156 | key_ref_t dest_ref; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 157 | size_t callout_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | char type[32], *description, *callout_info; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 159 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | /* pull the type into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 162 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (ret < 0) |
| 164 | goto error; |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* pull the description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 167 | description = strndup_user(_description, PAGE_SIZE); |
| 168 | if (IS_ERR(description)) { |
| 169 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 171 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* pull the callout info into kernel space */ |
| 174 | callout_info = NULL; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 175 | callout_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (_callout_info) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 177 | callout_info = strndup_user(_callout_info, PAGE_SIZE); |
| 178 | if (IS_ERR(callout_info)) { |
| 179 | ret = PTR_ERR(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | goto error2; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 181 | } |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 182 | callout_len = strlen(callout_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 186 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | if (destringid) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 188 | dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 189 | if (IS_ERR(dest_ref)) { |
| 190 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | goto error3; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* find the key type */ |
| 196 | ktype = key_type_lookup(type); |
| 197 | if (IS_ERR(ktype)) { |
| 198 | ret = PTR_ERR(ktype); |
| 199 | goto error4; |
| 200 | } |
| 201 | |
| 202 | /* do the search */ |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 203 | key = request_key_and_link(ktype, description, callout_info, |
| 204 | callout_len, NULL, key_ref_to_ptr(dest_ref), |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 205 | KEY_ALLOC_IN_QUOTA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | if (IS_ERR(key)) { |
| 207 | ret = PTR_ERR(key); |
| 208 | goto error5; |
| 209 | } |
| 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | ret = key->serial; |
| 212 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 213 | key_put(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | error5: |
| 215 | key_type_put(ktype); |
| 216 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 217 | key_ref_put(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | error3: |
| 219 | kfree(callout_info); |
| 220 | error2: |
| 221 | kfree(description); |
| 222 | error: |
| 223 | return ret; |
| 224 | |
| 225 | } /* end sys_request_key() */ |
| 226 | |
| 227 | /*****************************************************************************/ |
| 228 | /* |
| 229 | * get the ID of the specified process keyring |
| 230 | * - the keyring must have search permission to be found |
| 231 | * - implements keyctl(KEYCTL_GET_KEYRING_ID) |
| 232 | */ |
| 233 | long keyctl_get_keyring_ID(key_serial_t id, int create) |
| 234 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 235 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | long ret; |
| 237 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 238 | key_ref = lookup_user_key(id, create, 0, KEY_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 239 | if (IS_ERR(key_ref)) { |
| 240 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | goto error; |
| 242 | } |
| 243 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 244 | ret = key_ref_to_ptr(key_ref)->serial; |
| 245 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | error: |
| 247 | return ret; |
| 248 | |
| 249 | } /* end keyctl_get_keyring_ID() */ |
| 250 | |
| 251 | /*****************************************************************************/ |
| 252 | /* |
| 253 | * join the session keyring |
| 254 | * - implements keyctl(KEYCTL_JOIN_SESSION_KEYRING) |
| 255 | */ |
| 256 | long keyctl_join_session_keyring(const char __user *_name) |
| 257 | { |
| 258 | char *name; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 259 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | /* fetch the name from userspace */ |
| 262 | name = NULL; |
| 263 | if (_name) { |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 264 | name = strndup_user(_name, PAGE_SIZE); |
| 265 | if (IS_ERR(name)) { |
| 266 | ret = PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 268 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* join the session */ |
| 272 | ret = join_session_keyring(name); |
Vegard Nossum | 0d54ee1 | 2009-01-17 17:45:45 +0100 | [diff] [blame] | 273 | kfree(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | error: |
| 276 | return ret; |
| 277 | |
| 278 | } /* end keyctl_join_session_keyring() */ |
| 279 | |
| 280 | /*****************************************************************************/ |
| 281 | /* |
| 282 | * update a key's data payload |
| 283 | * - the key must be writable |
| 284 | * - implements keyctl(KEYCTL_UPDATE) |
| 285 | */ |
| 286 | long keyctl_update_key(key_serial_t id, |
| 287 | const void __user *_payload, |
| 288 | size_t plen) |
| 289 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 290 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | void *payload; |
| 292 | long ret; |
| 293 | |
| 294 | ret = -EINVAL; |
| 295 | if (plen > PAGE_SIZE) |
| 296 | goto error; |
| 297 | |
| 298 | /* pull the payload in if one was supplied */ |
| 299 | payload = NULL; |
| 300 | if (_payload) { |
| 301 | ret = -ENOMEM; |
| 302 | payload = kmalloc(plen, GFP_KERNEL); |
| 303 | if (!payload) |
| 304 | goto error; |
| 305 | |
| 306 | ret = -EFAULT; |
| 307 | if (copy_from_user(payload, _payload, plen) != 0) |
| 308 | goto error2; |
| 309 | } |
| 310 | |
| 311 | /* find the target key (which must be writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 312 | key_ref = lookup_user_key(id, 0, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 313 | if (IS_ERR(key_ref)) { |
| 314 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | goto error2; |
| 316 | } |
| 317 | |
| 318 | /* update the key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 319 | ret = key_update(key_ref, payload, plen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 321 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | error2: |
| 323 | kfree(payload); |
| 324 | error: |
| 325 | return ret; |
| 326 | |
| 327 | } /* end keyctl_update_key() */ |
| 328 | |
| 329 | /*****************************************************************************/ |
| 330 | /* |
| 331 | * revoke a key |
| 332 | * - the key must be writable |
| 333 | * - implements keyctl(KEYCTL_REVOKE) |
| 334 | */ |
| 335 | long keyctl_revoke_key(key_serial_t id) |
| 336 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 337 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | long ret; |
| 339 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 340 | key_ref = lookup_user_key(id, 0, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 341 | if (IS_ERR(key_ref)) { |
| 342 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | goto error; |
| 344 | } |
| 345 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 346 | key_revoke(key_ref_to_ptr(key_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | ret = 0; |
| 348 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 349 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | error: |
David Howells | 1260f80 | 2005-08-04 11:50:01 +0100 | [diff] [blame] | 351 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
| 353 | } /* end keyctl_revoke_key() */ |
| 354 | |
| 355 | /*****************************************************************************/ |
| 356 | /* |
| 357 | * clear the specified process keyring |
| 358 | * - the keyring must be writable |
| 359 | * - implements keyctl(KEYCTL_CLEAR) |
| 360 | */ |
| 361 | long keyctl_keyring_clear(key_serial_t ringid) |
| 362 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 363 | key_ref_t keyring_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | long ret; |
| 365 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 366 | keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 367 | if (IS_ERR(keyring_ref)) { |
| 368 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | goto error; |
| 370 | } |
| 371 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 372 | ret = keyring_clear(key_ref_to_ptr(keyring_ref)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 374 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | error: |
| 376 | return ret; |
| 377 | |
| 378 | } /* end keyctl_keyring_clear() */ |
| 379 | |
| 380 | /*****************************************************************************/ |
| 381 | /* |
| 382 | * link a key into a keyring |
| 383 | * - the keyring must be writable |
| 384 | * - the key must be linkable |
| 385 | * - implements keyctl(KEYCTL_LINK) |
| 386 | */ |
| 387 | long keyctl_keyring_link(key_serial_t id, key_serial_t ringid) |
| 388 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 389 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | long ret; |
| 391 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 392 | keyring_ref = lookup_user_key(ringid, 1, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 393 | if (IS_ERR(keyring_ref)) { |
| 394 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | goto error; |
| 396 | } |
| 397 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 398 | key_ref = lookup_user_key(id, 1, 0, KEY_LINK); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 399 | if (IS_ERR(key_ref)) { |
| 400 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | goto error2; |
| 402 | } |
| 403 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 404 | 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] | 405 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 406 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 408 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | error: |
| 410 | return ret; |
| 411 | |
| 412 | } /* end keyctl_keyring_link() */ |
| 413 | |
| 414 | /*****************************************************************************/ |
| 415 | /* |
| 416 | * unlink the first attachment of a key from a keyring |
| 417 | * - the keyring must be writable |
| 418 | * - we don't need any permissions on the key |
| 419 | * - implements keyctl(KEYCTL_UNLINK) |
| 420 | */ |
| 421 | long keyctl_keyring_unlink(key_serial_t id, key_serial_t ringid) |
| 422 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 423 | key_ref_t keyring_ref, key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | long ret; |
| 425 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 426 | keyring_ref = lookup_user_key(ringid, 0, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 427 | if (IS_ERR(keyring_ref)) { |
| 428 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | goto error; |
| 430 | } |
| 431 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 432 | key_ref = lookup_user_key(id, 0, 0, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 433 | if (IS_ERR(key_ref)) { |
| 434 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | goto error2; |
| 436 | } |
| 437 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 438 | 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] | 439 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 440 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 442 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | error: |
| 444 | return ret; |
| 445 | |
| 446 | } /* end keyctl_keyring_unlink() */ |
| 447 | |
| 448 | /*****************************************************************************/ |
| 449 | /* |
| 450 | * describe a user key |
| 451 | * - the key must have view permission |
| 452 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 453 | * - unless there's an error, we return the amount of description available, |
| 454 | * irrespective of how much we may have copied |
| 455 | * - the description is formatted thus: |
| 456 | * type;uid;gid;perm;description<NUL> |
| 457 | * - implements keyctl(KEYCTL_DESCRIBE) |
| 458 | */ |
| 459 | long keyctl_describe_key(key_serial_t keyid, |
| 460 | char __user *buffer, |
| 461 | size_t buflen) |
| 462 | { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 463 | struct key *key, *instkey; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 464 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | char *tmpbuf; |
| 466 | long ret; |
| 467 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 468 | key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 469 | if (IS_ERR(key_ref)) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 470 | /* viewing a key under construction is permitted if we have the |
| 471 | * authorisation token handy */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 472 | if (PTR_ERR(key_ref) == -EACCES) { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 473 | instkey = key_get_instantiation_authkey(keyid); |
| 474 | if (!IS_ERR(instkey)) { |
| 475 | key_put(instkey); |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 476 | key_ref = lookup_user_key(keyid, |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 477 | 0, 1, 0); |
| 478 | if (!IS_ERR(key_ref)) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 479 | goto okay; |
| 480 | } |
| 481 | } |
| 482 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 483 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | goto error; |
| 485 | } |
| 486 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 487 | okay: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | /* calculate how much description we're going to return */ |
| 489 | ret = -ENOMEM; |
| 490 | tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 491 | if (!tmpbuf) |
| 492 | goto error2; |
| 493 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 494 | key = key_ref_to_ptr(key_ref); |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | ret = snprintf(tmpbuf, PAGE_SIZE - 1, |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 497 | "%s;%d;%d;%08x;%s", |
| 498 | key_ref_to_ptr(key_ref)->type->name, |
| 499 | key_ref_to_ptr(key_ref)->uid, |
| 500 | key_ref_to_ptr(key_ref)->gid, |
| 501 | key_ref_to_ptr(key_ref)->perm, |
| 502 | key_ref_to_ptr(key_ref)->description ? |
| 503 | key_ref_to_ptr(key_ref)->description : "" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | ); |
| 505 | |
| 506 | /* include a NUL char at the end of the data */ |
| 507 | if (ret > PAGE_SIZE - 1) |
| 508 | ret = PAGE_SIZE - 1; |
| 509 | tmpbuf[ret] = 0; |
| 510 | ret++; |
| 511 | |
| 512 | /* consider returning the data */ |
| 513 | if (buffer && buflen > 0) { |
| 514 | if (buflen > ret) |
| 515 | buflen = ret; |
| 516 | |
| 517 | if (copy_to_user(buffer, tmpbuf, buflen) != 0) |
| 518 | ret = -EFAULT; |
| 519 | } |
| 520 | |
| 521 | kfree(tmpbuf); |
| 522 | error2: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 523 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | error: |
| 525 | return ret; |
| 526 | |
| 527 | } /* end keyctl_describe_key() */ |
| 528 | |
| 529 | /*****************************************************************************/ |
| 530 | /* |
| 531 | * search the specified keyring for a matching key |
| 532 | * - the start keyring must be searchable |
| 533 | * - nested keyrings may also be searched if they are searchable |
| 534 | * - only keys with search permission may be found |
| 535 | * - if a key is found, it will be attached to the destination keyring if |
| 536 | * there's one specified |
| 537 | * - implements keyctl(KEYCTL_SEARCH) |
| 538 | */ |
| 539 | long keyctl_keyring_search(key_serial_t ringid, |
| 540 | const char __user *_type, |
| 541 | const char __user *_description, |
| 542 | key_serial_t destringid) |
| 543 | { |
| 544 | struct key_type *ktype; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 545 | key_ref_t keyring_ref, key_ref, dest_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | char type[32], *description; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 547 | long ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | |
| 549 | /* pull the type and description into kernel space */ |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 550 | ret = key_get_type_from_user(type, _type, sizeof(type)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 554 | description = strndup_user(_description, PAGE_SIZE); |
| 555 | if (IS_ERR(description)) { |
| 556 | ret = PTR_ERR(description); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | goto error; |
Davi Arnaut | 0cb409d | 2006-03-24 03:18:43 -0800 | [diff] [blame] | 558 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | |
| 560 | /* get the keyring at which to begin the search */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 561 | keyring_ref = lookup_user_key(ringid, 0, 0, KEY_SEARCH); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 562 | if (IS_ERR(keyring_ref)) { |
| 563 | ret = PTR_ERR(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | goto error2; |
| 565 | } |
| 566 | |
| 567 | /* get the destination keyring if specified */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 568 | dest_ref = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | if (destringid) { |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 570 | dest_ref = lookup_user_key(destringid, 1, 0, KEY_WRITE); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 571 | if (IS_ERR(dest_ref)) { |
| 572 | ret = PTR_ERR(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | goto error3; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | /* find the key type */ |
| 578 | ktype = key_type_lookup(type); |
| 579 | if (IS_ERR(ktype)) { |
| 580 | ret = PTR_ERR(ktype); |
| 581 | goto error4; |
| 582 | } |
| 583 | |
| 584 | /* do the search */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 585 | key_ref = keyring_search(keyring_ref, ktype, description); |
| 586 | if (IS_ERR(key_ref)) { |
| 587 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | |
| 589 | /* treat lack or presence of a negative key the same */ |
| 590 | if (ret == -EAGAIN) |
| 591 | ret = -ENOKEY; |
| 592 | goto error5; |
| 593 | } |
| 594 | |
| 595 | /* link the resulting key to the destination keyring if we can */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 596 | if (dest_ref) { |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 597 | ret = key_permission(key_ref, KEY_LINK); |
| 598 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | goto error6; |
| 600 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 601 | 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] | 602 | if (ret < 0) |
| 603 | goto error6; |
| 604 | } |
| 605 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 606 | ret = key_ref_to_ptr(key_ref)->serial; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | error6: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 609 | key_ref_put(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | error5: |
| 611 | key_type_put(ktype); |
| 612 | error4: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 613 | key_ref_put(dest_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | error3: |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 615 | key_ref_put(keyring_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | error2: |
| 617 | kfree(description); |
| 618 | error: |
| 619 | return ret; |
| 620 | |
| 621 | } /* end keyctl_keyring_search() */ |
| 622 | |
| 623 | /*****************************************************************************/ |
| 624 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | * read a user key's payload |
| 626 | * - the keyring must be readable or the key must be searchable from the |
| 627 | * process's keyrings |
| 628 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 629 | * - unless there's an error, we return the amount of data in the key, |
| 630 | * irrespective of how much we may have copied |
| 631 | * - implements keyctl(KEYCTL_READ) |
| 632 | */ |
| 633 | long keyctl_read_key(key_serial_t keyid, char __user *buffer, size_t buflen) |
| 634 | { |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 635 | struct key *key; |
| 636 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | long ret; |
| 638 | |
| 639 | /* find the key first */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 640 | key_ref = lookup_user_key(keyid, 0, 0, 0); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 641 | if (IS_ERR(key_ref)) { |
| 642 | ret = -ENOKEY; |
| 643 | goto error; |
| 644 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 646 | key = key_ref_to_ptr(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 648 | /* see if we can read it directly */ |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 649 | ret = key_permission(key_ref, KEY_READ); |
| 650 | if (ret == 0) |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 651 | goto can_read_key; |
David Howells | 29db919 | 2005-10-30 15:02:44 -0800 | [diff] [blame] | 652 | if (ret != -EACCES) |
| 653 | goto error; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 654 | |
| 655 | /* we can't; see if it's searchable from this process's keyrings |
| 656 | * - we automatically take account of the fact that it may be |
| 657 | * dangling off an instantiation key |
| 658 | */ |
| 659 | if (!is_key_possessed(key_ref)) { |
| 660 | ret = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | goto error2; |
| 662 | } |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | /* the key is probably readable - now try to read it */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | can_read_key: |
| 666 | ret = key_validate(key); |
| 667 | if (ret == 0) { |
| 668 | ret = -EOPNOTSUPP; |
| 669 | if (key->type->read) { |
| 670 | /* read the data with the semaphore held (since we |
| 671 | * might sleep) */ |
| 672 | down_read(&key->sem); |
| 673 | ret = key->type->read(key, buffer, buflen); |
| 674 | up_read(&key->sem); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | error2: |
| 679 | key_put(key); |
| 680 | error: |
| 681 | return ret; |
| 682 | |
| 683 | } /* end keyctl_read_key() */ |
| 684 | |
| 685 | /*****************************************************************************/ |
| 686 | /* |
| 687 | * change the ownership of a key |
| 688 | * - the keyring owned by the changer |
| 689 | * - if the uid or gid is -1, then that parameter is not changed |
| 690 | * - implements keyctl(KEYCTL_CHOWN) |
| 691 | */ |
| 692 | long keyctl_chown_key(key_serial_t id, uid_t uid, gid_t gid) |
| 693 | { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 694 | struct key_user *newowner, *zapowner = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 696 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | long ret; |
| 698 | |
| 699 | ret = 0; |
| 700 | if (uid == (uid_t) -1 && gid == (gid_t) -1) |
| 701 | goto error; |
| 702 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 703 | key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 704 | if (IS_ERR(key_ref)) { |
| 705 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | goto error; |
| 707 | } |
| 708 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 709 | key = key_ref_to_ptr(key_ref); |
| 710 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | /* make the changes with the locks held to prevent chown/chown races */ |
| 712 | ret = -EACCES; |
| 713 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
| 715 | if (!capable(CAP_SYS_ADMIN)) { |
| 716 | /* only the sysadmin can chown a key to some other UID */ |
| 717 | if (uid != (uid_t) -1 && key->uid != uid) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 718 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
| 720 | /* only the sysadmin can set the key's GID to a group other |
| 721 | * than one of those that the current process subscribes to */ |
| 722 | if (gid != (gid_t) -1 && gid != key->gid && !in_group_p(gid)) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 723 | goto error_put; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 726 | /* change the UID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (uid != (uid_t) -1 && uid != key->uid) { |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 728 | ret = -ENOMEM; |
Serge E. Hallyn | 1d1e975 | 2009-02-26 18:27:38 -0600 | [diff] [blame] | 729 | newowner = key_user_lookup(uid, current_user_ns()); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 730 | if (!newowner) |
| 731 | goto error_put; |
| 732 | |
| 733 | /* transfer the quota burden to the new user */ |
| 734 | if (test_bit(KEY_FLAG_IN_QUOTA, &key->flags)) { |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 735 | unsigned maxkeys = (uid == 0) ? |
| 736 | key_quota_root_maxkeys : key_quota_maxkeys; |
| 737 | unsigned maxbytes = (uid == 0) ? |
| 738 | key_quota_root_maxbytes : key_quota_maxbytes; |
| 739 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 740 | spin_lock(&newowner->lock); |
David Howells | 0b77f5b | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 741 | if (newowner->qnkeys + 1 >= maxkeys || |
| 742 | newowner->qnbytes + key->quotalen >= maxbytes || |
| 743 | newowner->qnbytes + key->quotalen < |
| 744 | newowner->qnbytes) |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 745 | goto quota_overrun; |
| 746 | |
| 747 | newowner->qnkeys++; |
| 748 | newowner->qnbytes += key->quotalen; |
| 749 | spin_unlock(&newowner->lock); |
| 750 | |
| 751 | spin_lock(&key->user->lock); |
| 752 | key->user->qnkeys--; |
| 753 | key->user->qnbytes -= key->quotalen; |
| 754 | spin_unlock(&key->user->lock); |
| 755 | } |
| 756 | |
| 757 | atomic_dec(&key->user->nkeys); |
| 758 | atomic_inc(&newowner->nkeys); |
| 759 | |
| 760 | if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) { |
| 761 | atomic_dec(&key->user->nikeys); |
| 762 | atomic_inc(&newowner->nikeys); |
| 763 | } |
| 764 | |
| 765 | zapowner = key->user; |
| 766 | key->user = newowner; |
| 767 | key->uid = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* change the GID */ |
| 771 | if (gid != (gid_t) -1) |
| 772 | key->gid = gid; |
| 773 | |
| 774 | ret = 0; |
| 775 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 776 | error_put: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | up_write(&key->sem); |
| 778 | key_put(key); |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 779 | if (zapowner) |
| 780 | key_user_put(zapowner); |
| 781 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | return ret; |
| 783 | |
Fredrik Tolf | 5801649 | 2006-06-26 00:24:51 -0700 | [diff] [blame] | 784 | quota_overrun: |
| 785 | spin_unlock(&newowner->lock); |
| 786 | zapowner = newowner; |
| 787 | ret = -EDQUOT; |
| 788 | goto error_put; |
| 789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } /* end keyctl_chown_key() */ |
| 791 | |
| 792 | /*****************************************************************************/ |
| 793 | /* |
| 794 | * change the permission mask on a key |
| 795 | * - the keyring owned by the changer |
| 796 | * - implements keyctl(KEYCTL_SETPERM) |
| 797 | */ |
| 798 | long keyctl_setperm_key(key_serial_t id, key_perm_t perm) |
| 799 | { |
| 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; |
| 803 | |
| 804 | ret = -EINVAL; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 805 | 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] | 806 | goto error; |
| 807 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 808 | key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 809 | if (IS_ERR(key_ref)) { |
| 810 | ret = PTR_ERR(key_ref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | goto error; |
| 812 | } |
| 813 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 814 | key = key_ref_to_ptr(key_ref); |
| 815 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 816 | /* make the changes with the locks held to prevent chown/chmod races */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | ret = -EACCES; |
| 818 | down_write(&key->sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 820 | /* if we're not the sysadmin, we can only change a key that we own */ |
David Howells | 47d804b | 2008-11-14 10:39:11 +1100 | [diff] [blame] | 821 | if (capable(CAP_SYS_ADMIN) || key->uid == current_fsuid()) { |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 822 | key->perm = perm; |
| 823 | ret = 0; |
| 824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | up_write(&key->sem); |
| 827 | key_put(key); |
David Howells | 76d8aea | 2005-06-23 22:00:49 -0700 | [diff] [blame] | 828 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | return ret; |
| 830 | |
| 831 | } /* end keyctl_setperm_key() */ |
| 832 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 833 | /* |
| 834 | * get the destination keyring for instantiation |
| 835 | */ |
| 836 | static long get_instantiation_keyring(key_serial_t ringid, |
| 837 | struct request_key_auth *rka, |
| 838 | struct key **_dest_keyring) |
| 839 | { |
| 840 | key_ref_t dkref; |
| 841 | |
David Howells | eca1bf5 | 2008-12-29 00:41:51 +0000 | [diff] [blame] | 842 | *_dest_keyring = NULL; |
| 843 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 844 | /* 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] | 845 | if (ringid == 0) |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 846 | return 0; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 847 | |
| 848 | /* if a specific keyring is nominated by ID, then use that */ |
| 849 | if (ringid > 0) { |
| 850 | dkref = lookup_user_key(ringid, 1, 0, KEY_WRITE); |
| 851 | if (IS_ERR(dkref)) |
| 852 | return PTR_ERR(dkref); |
| 853 | *_dest_keyring = key_ref_to_ptr(dkref); |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | if (ringid == KEY_SPEC_REQKEY_AUTH_KEY) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | /* otherwise specify the destination keyring recorded in the |
| 861 | * authorisation key (any KEY_SPEC_*_KEYRING) */ |
| 862 | if (ringid >= KEY_SPEC_REQUESTOR_KEYRING) { |
| 863 | *_dest_keyring = rka->dest_keyring; |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | return -ENOKEY; |
| 868 | } |
| 869 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 870 | /* |
| 871 | * change the request_key authorisation key on the current process |
| 872 | */ |
| 873 | static int keyctl_change_reqkey_auth(struct key *key) |
| 874 | { |
| 875 | struct cred *new; |
| 876 | |
| 877 | new = prepare_creds(); |
| 878 | if (!new) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | key_put(new->request_key_auth); |
| 882 | new->request_key_auth = key_get(key); |
| 883 | |
| 884 | return commit_creds(new); |
| 885 | } |
| 886 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | /*****************************************************************************/ |
| 888 | /* |
| 889 | * instantiate the key with the specified payload, and, if one is given, link |
| 890 | * the key into the keyring |
| 891 | */ |
| 892 | long keyctl_instantiate_key(key_serial_t id, |
| 893 | const void __user *_payload, |
| 894 | size_t plen, |
| 895 | key_serial_t ringid) |
| 896 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 897 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 898 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 899 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | void *payload; |
| 901 | long ret; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 902 | bool vm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 904 | kenter("%d,,%zu,%d", id, plen, ringid); |
| 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | ret = -EINVAL; |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 907 | if (plen > 1024 * 1024 - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | goto error; |
| 909 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 910 | /* the appropriate instantiation authorisation key must have been |
| 911 | * assumed before calling this */ |
| 912 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 913 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 914 | if (!instkey) |
| 915 | goto error; |
| 916 | |
| 917 | rka = instkey->payload.data; |
| 918 | if (rka->target_key->serial != id) |
| 919 | goto error; |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | /* pull the payload in if one was supplied */ |
| 922 | payload = NULL; |
| 923 | |
| 924 | if (_payload) { |
| 925 | ret = -ENOMEM; |
| 926 | payload = kmalloc(plen, GFP_KERNEL); |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 927 | if (!payload) { |
| 928 | if (plen <= PAGE_SIZE) |
| 929 | goto error; |
| 930 | vm = true; |
| 931 | payload = vmalloc(plen); |
| 932 | if (!payload) |
| 933 | goto error; |
| 934 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | |
| 936 | ret = -EFAULT; |
| 937 | if (copy_from_user(payload, _payload, plen) != 0) |
| 938 | goto error2; |
| 939 | } |
| 940 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 941 | /* find the destination keyring amongst those belonging to the |
| 942 | * requesting task */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 943 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 944 | if (ret < 0) |
| 945 | goto error2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
| 947 | /* instantiate the key and link it into a keyring */ |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 948 | ret = key_instantiate_and_link(rka->target_key, payload, plen, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 949 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 951 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 952 | |
| 953 | /* discard the assumed authority if it's just been disabled by |
| 954 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 955 | if (ret == 0) |
| 956 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 957 | |
| 958 | error2: |
David Howells | 38bbca6 | 2008-04-29 01:01:19 -0700 | [diff] [blame] | 959 | if (!vm) |
| 960 | kfree(payload); |
| 961 | else |
| 962 | vfree(payload); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 963 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | return ret; |
| 965 | |
| 966 | } /* end keyctl_instantiate_key() */ |
| 967 | |
| 968 | /*****************************************************************************/ |
| 969 | /* |
| 970 | * negatively instantiate the key with the given timeout (in seconds), and, if |
| 971 | * one is given, link the key into the keyring |
| 972 | */ |
| 973 | long keyctl_negate_key(key_serial_t id, unsigned timeout, key_serial_t ringid) |
| 974 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 975 | const struct cred *cred = current_cred(); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 976 | struct request_key_auth *rka; |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 977 | struct key *instkey, *dest_keyring; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | long ret; |
| 979 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 980 | kenter("%d,%u,%d", id, timeout, ringid); |
| 981 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 982 | /* the appropriate instantiation authorisation key must have been |
| 983 | * assumed before calling this */ |
| 984 | ret = -EPERM; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 985 | instkey = cred->request_key_auth; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 986 | if (!instkey) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 989 | rka = instkey->payload.data; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 990 | if (rka->target_key->serial != id) |
| 991 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | /* find the destination keyring if present (which must also be |
| 994 | * writable) */ |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 995 | ret = get_instantiation_keyring(ringid, rka, &dest_keyring); |
| 996 | if (ret < 0) |
| 997 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
| 999 | /* instantiate the key and link it into a keyring */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 1000 | ret = key_negate_and_link(rka->target_key, timeout, |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1001 | dest_keyring, instkey); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1003 | key_put(dest_keyring); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1004 | |
| 1005 | /* discard the assumed authority if it's just been disabled by |
| 1006 | * instantiation of the key */ |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1007 | if (ret == 0) |
| 1008 | keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1009 | |
| 1010 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | return ret; |
| 1012 | |
| 1013 | } /* end keyctl_negate_key() */ |
| 1014 | |
| 1015 | /*****************************************************************************/ |
| 1016 | /* |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1017 | * set the default keyring in which request_key() will cache keys |
| 1018 | * - return the old setting |
| 1019 | */ |
| 1020 | long keyctl_set_reqkey_keyring(int reqkey_defl) |
| 1021 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1022 | struct cred *new; |
| 1023 | int ret, old_setting; |
| 1024 | |
| 1025 | old_setting = current_cred_xxx(jit_keyring); |
| 1026 | |
| 1027 | if (reqkey_defl == KEY_REQKEY_DEFL_NO_CHANGE) |
| 1028 | return old_setting; |
| 1029 | |
| 1030 | new = prepare_creds(); |
| 1031 | if (!new) |
| 1032 | return -ENOMEM; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1033 | |
| 1034 | switch (reqkey_defl) { |
| 1035 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1036 | ret = install_thread_keyring_to_cred(new); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1037 | if (ret < 0) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1038 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1039 | goto set; |
| 1040 | |
| 1041 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1042 | ret = install_process_keyring_to_cred(new); |
| 1043 | if (ret < 0) { |
| 1044 | if (ret != -EEXIST) |
| 1045 | goto error; |
| 1046 | ret = 0; |
| 1047 | } |
| 1048 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1049 | |
| 1050 | case KEY_REQKEY_DEFL_DEFAULT: |
| 1051 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 1052 | case KEY_REQKEY_DEFL_USER_KEYRING: |
| 1053 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1054 | case KEY_REQKEY_DEFL_REQUESTOR_KEYRING: |
| 1055 | goto set; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1056 | |
| 1057 | case KEY_REQKEY_DEFL_NO_CHANGE: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1058 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
| 1059 | default: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1060 | ret = -EINVAL; |
| 1061 | goto error; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1064 | set: |
| 1065 | new->jit_keyring = reqkey_defl; |
| 1066 | commit_creds(new); |
| 1067 | return old_setting; |
| 1068 | error: |
| 1069 | abort_creds(new); |
| 1070 | return -EINVAL; |
| 1071 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1072 | } /* end keyctl_set_reqkey_keyring() */ |
| 1073 | |
| 1074 | /*****************************************************************************/ |
| 1075 | /* |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1076 | * set or clear the timeout for a key |
| 1077 | */ |
| 1078 | long keyctl_set_timeout(key_serial_t id, unsigned timeout) |
| 1079 | { |
| 1080 | struct timespec now; |
| 1081 | struct key *key; |
| 1082 | key_ref_t key_ref; |
| 1083 | time_t expiry; |
| 1084 | long ret; |
| 1085 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1086 | key_ref = lookup_user_key(id, 1, 1, KEY_SETATTR); |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1087 | if (IS_ERR(key_ref)) { |
| 1088 | ret = PTR_ERR(key_ref); |
| 1089 | goto error; |
| 1090 | } |
| 1091 | |
| 1092 | key = key_ref_to_ptr(key_ref); |
| 1093 | |
| 1094 | /* make the changes with the locks held to prevent races */ |
| 1095 | down_write(&key->sem); |
| 1096 | |
| 1097 | expiry = 0; |
| 1098 | if (timeout > 0) { |
| 1099 | now = current_kernel_time(); |
| 1100 | expiry = now.tv_sec + timeout; |
| 1101 | } |
| 1102 | |
| 1103 | key->expiry = expiry; |
| 1104 | |
| 1105 | up_write(&key->sem); |
| 1106 | key_put(key); |
| 1107 | |
| 1108 | ret = 0; |
| 1109 | error: |
| 1110 | return ret; |
| 1111 | |
| 1112 | } /* end keyctl_set_timeout() */ |
| 1113 | |
| 1114 | /*****************************************************************************/ |
| 1115 | /* |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1116 | * assume the authority to instantiate the specified key |
| 1117 | */ |
| 1118 | long keyctl_assume_authority(key_serial_t id) |
| 1119 | { |
| 1120 | struct key *authkey; |
| 1121 | long ret; |
| 1122 | |
| 1123 | /* special key IDs aren't permitted */ |
| 1124 | ret = -EINVAL; |
| 1125 | if (id < 0) |
| 1126 | goto error; |
| 1127 | |
| 1128 | /* we divest ourselves of authority if given an ID of 0 */ |
| 1129 | if (id == 0) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1130 | ret = keyctl_change_reqkey_auth(NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1131 | goto error; |
| 1132 | } |
| 1133 | |
| 1134 | /* attempt to assume the authority temporarily granted to us whilst we |
| 1135 | * instantiate the specified key |
| 1136 | * - the authorisation key must be in the current task's keyrings |
| 1137 | * somewhere |
| 1138 | */ |
| 1139 | authkey = key_get_instantiation_authkey(id); |
| 1140 | if (IS_ERR(authkey)) { |
| 1141 | ret = PTR_ERR(authkey); |
| 1142 | goto error; |
| 1143 | } |
| 1144 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1145 | ret = keyctl_change_reqkey_auth(authkey); |
| 1146 | if (ret < 0) |
| 1147 | goto error; |
| 1148 | key_put(authkey); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1149 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1150 | ret = authkey->serial; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1151 | error: |
| 1152 | return ret; |
| 1153 | |
| 1154 | } /* end keyctl_assume_authority() */ |
| 1155 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1156 | /* |
| 1157 | * get the security label of a key |
| 1158 | * - the key must grant us view permission |
| 1159 | * - if there's a buffer, we place up to buflen bytes of data into it |
| 1160 | * - unless there's an error, we return the amount of information available, |
| 1161 | * irrespective of how much we may have copied (including the terminal NUL) |
| 1162 | * - implements keyctl(KEYCTL_GET_SECURITY) |
| 1163 | */ |
| 1164 | long keyctl_get_security(key_serial_t keyid, |
| 1165 | char __user *buffer, |
| 1166 | size_t buflen) |
| 1167 | { |
| 1168 | struct key *key, *instkey; |
| 1169 | key_ref_t key_ref; |
| 1170 | char *context; |
| 1171 | long ret; |
| 1172 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1173 | key_ref = lookup_user_key(keyid, 0, 1, KEY_VIEW); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1174 | if (IS_ERR(key_ref)) { |
| 1175 | if (PTR_ERR(key_ref) != -EACCES) |
| 1176 | return PTR_ERR(key_ref); |
| 1177 | |
| 1178 | /* viewing a key under construction is also permitted if we |
| 1179 | * have the authorisation token handy */ |
| 1180 | instkey = key_get_instantiation_authkey(keyid); |
| 1181 | if (IS_ERR(instkey)) |
| 1182 | return PTR_ERR(key_ref); |
| 1183 | key_put(instkey); |
| 1184 | |
David Howells | 8bbf4976 | 2008-11-14 10:39:14 +1100 | [diff] [blame] | 1185 | key_ref = lookup_user_key(keyid, 0, 1, 0); |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1186 | if (IS_ERR(key_ref)) |
| 1187 | return PTR_ERR(key_ref); |
| 1188 | } |
| 1189 | |
| 1190 | key = key_ref_to_ptr(key_ref); |
| 1191 | ret = security_key_getsecurity(key, &context); |
| 1192 | if (ret == 0) { |
| 1193 | /* if no information was returned, give userspace an empty |
| 1194 | * string */ |
| 1195 | ret = 1; |
| 1196 | if (buffer && buflen > 0 && |
| 1197 | copy_to_user(buffer, "", 1) != 0) |
| 1198 | ret = -EFAULT; |
| 1199 | } else if (ret > 0) { |
| 1200 | /* return as much data as there's room for */ |
| 1201 | if (buffer && buflen > 0) { |
| 1202 | if (buflen > ret) |
| 1203 | buflen = ret; |
| 1204 | |
| 1205 | if (copy_to_user(buffer, context, buflen) != 0) |
| 1206 | ret = -EFAULT; |
| 1207 | } |
| 1208 | |
| 1209 | kfree(context); |
| 1210 | } |
| 1211 | |
| 1212 | key_ref_put(key_ref); |
| 1213 | return ret; |
| 1214 | } |
| 1215 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1216 | /*****************************************************************************/ |
| 1217 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | * the key control system call |
| 1219 | */ |
Heiko Carstens | 938bb9f | 2009-01-14 14:14:30 +0100 | [diff] [blame] | 1220 | SYSCALL_DEFINE5(keyctl, int, option, unsigned long, arg2, unsigned long, arg3, |
| 1221 | unsigned long, arg4, unsigned long, arg5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | { |
| 1223 | switch (option) { |
| 1224 | case KEYCTL_GET_KEYRING_ID: |
| 1225 | return keyctl_get_keyring_ID((key_serial_t) arg2, |
| 1226 | (int) arg3); |
| 1227 | |
| 1228 | case KEYCTL_JOIN_SESSION_KEYRING: |
| 1229 | return keyctl_join_session_keyring((const char __user *) arg2); |
| 1230 | |
| 1231 | case KEYCTL_UPDATE: |
| 1232 | return keyctl_update_key((key_serial_t) arg2, |
| 1233 | (const void __user *) arg3, |
| 1234 | (size_t) arg4); |
| 1235 | |
| 1236 | case KEYCTL_REVOKE: |
| 1237 | return keyctl_revoke_key((key_serial_t) arg2); |
| 1238 | |
| 1239 | case KEYCTL_DESCRIBE: |
| 1240 | return keyctl_describe_key((key_serial_t) arg2, |
| 1241 | (char __user *) arg3, |
| 1242 | (unsigned) arg4); |
| 1243 | |
| 1244 | case KEYCTL_CLEAR: |
| 1245 | return keyctl_keyring_clear((key_serial_t) arg2); |
| 1246 | |
| 1247 | case KEYCTL_LINK: |
| 1248 | return keyctl_keyring_link((key_serial_t) arg2, |
| 1249 | (key_serial_t) arg3); |
| 1250 | |
| 1251 | case KEYCTL_UNLINK: |
| 1252 | return keyctl_keyring_unlink((key_serial_t) arg2, |
| 1253 | (key_serial_t) arg3); |
| 1254 | |
| 1255 | case KEYCTL_SEARCH: |
| 1256 | return keyctl_keyring_search((key_serial_t) arg2, |
| 1257 | (const char __user *) arg3, |
| 1258 | (const char __user *) arg4, |
| 1259 | (key_serial_t) arg5); |
| 1260 | |
| 1261 | case KEYCTL_READ: |
| 1262 | return keyctl_read_key((key_serial_t) arg2, |
| 1263 | (char __user *) arg3, |
| 1264 | (size_t) arg4); |
| 1265 | |
| 1266 | case KEYCTL_CHOWN: |
| 1267 | return keyctl_chown_key((key_serial_t) arg2, |
| 1268 | (uid_t) arg3, |
| 1269 | (gid_t) arg4); |
| 1270 | |
| 1271 | case KEYCTL_SETPERM: |
| 1272 | return keyctl_setperm_key((key_serial_t) arg2, |
| 1273 | (key_perm_t) arg3); |
| 1274 | |
| 1275 | case KEYCTL_INSTANTIATE: |
| 1276 | return keyctl_instantiate_key((key_serial_t) arg2, |
| 1277 | (const void __user *) arg3, |
| 1278 | (size_t) arg4, |
| 1279 | (key_serial_t) arg5); |
| 1280 | |
| 1281 | case KEYCTL_NEGATE: |
| 1282 | return keyctl_negate_key((key_serial_t) arg2, |
| 1283 | (unsigned) arg3, |
| 1284 | (key_serial_t) arg4); |
| 1285 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 1286 | case KEYCTL_SET_REQKEY_KEYRING: |
| 1287 | return keyctl_set_reqkey_keyring(arg2); |
| 1288 | |
David Howells | 017679c | 2006-01-08 01:02:43 -0800 | [diff] [blame] | 1289 | case KEYCTL_SET_TIMEOUT: |
| 1290 | return keyctl_set_timeout((key_serial_t) arg2, |
| 1291 | (unsigned) arg3); |
| 1292 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 1293 | case KEYCTL_ASSUME_AUTHORITY: |
| 1294 | return keyctl_assume_authority((key_serial_t) arg2); |
| 1295 | |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1296 | case KEYCTL_GET_SECURITY: |
| 1297 | return keyctl_get_security((key_serial_t) arg2, |
James Morris | 90bd49a | 2008-12-29 14:35:35 +1100 | [diff] [blame] | 1298 | (char __user *) arg3, |
David Howells | 70a5bb7 | 2008-04-29 01:01:26 -0700 | [diff] [blame] | 1299 | (size_t) arg4); |
| 1300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | default: |
| 1302 | return -EOPNOTSUPP; |
| 1303 | } |
| 1304 | |
| 1305 | } /* end sys_keyctl() */ |