David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 1 | /* Request a key from userspace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 3 | * Copyright (C) 2004-2007 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. |
David Howells | f1a9bad | 2005-10-07 15:04:52 +0100 | [diff] [blame] | 10 | * |
| 11 | * See Documentation/keys-request-key.txt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/kmod.h> |
| 17 | #include <linux/err.h> |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 18 | #include <linux/keyctl.h> |
Robert P. J. Day | fdb89bc | 2008-04-29 01:01:32 -0700 | [diff] [blame] | 19 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 22 | /* |
| 23 | * wait_on_bit() sleep function for uninterruptible waiting |
| 24 | */ |
| 25 | static int key_wait_bit(void *flags) |
| 26 | { |
| 27 | schedule(); |
| 28 | return 0; |
| 29 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 31 | /* |
| 32 | * wait_on_bit() sleep function for interruptible waiting |
| 33 | */ |
| 34 | static int key_wait_bit_intr(void *flags) |
| 35 | { |
| 36 | schedule(); |
| 37 | return signal_pending(current) ? -ERESTARTSYS : 0; |
| 38 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 40 | /* |
| 41 | * call to complete the construction of a key |
| 42 | */ |
| 43 | void complete_request_key(struct key_construction *cons, int error) |
| 44 | { |
| 45 | kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error); |
| 46 | |
| 47 | if (error < 0) |
| 48 | key_negate_and_link(cons->key, key_negative_timeout, NULL, |
| 49 | cons->authkey); |
| 50 | else |
| 51 | key_revoke(cons->authkey); |
| 52 | |
| 53 | key_put(cons->key); |
| 54 | key_put(cons->authkey); |
| 55 | kfree(cons); |
| 56 | } |
| 57 | EXPORT_SYMBOL(complete_request_key); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | /* |
| 60 | * request userspace finish the construction of a key |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 61 | * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | */ |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 63 | static int call_sbin_request_key(struct key_construction *cons, |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 64 | const char *op, |
| 65 | void *aux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | struct task_struct *tsk = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | key_serial_t prkey, sskey; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 69 | struct key *key = cons->key, *authkey = cons->authkey, *keyring; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 70 | char *argv[9], *envp[3], uid_str[12], gid_str[12]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | char key_str[12], keyring_str[3][12]; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 72 | char desc[20]; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 73 | int ret, i; |
| 74 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 75 | kenter("{%d},{%d},%s", key->serial, authkey->serial, op); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 76 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 77 | /* allocate a new session keyring */ |
| 78 | sprintf(desc, "_req.%u", key->serial); |
| 79 | |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 80 | keyring = keyring_alloc(desc, current->fsuid, current->fsgid, current, |
| 81 | KEY_ALLOC_QUOTA_OVERRUN, NULL); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 82 | if (IS_ERR(keyring)) { |
| 83 | ret = PTR_ERR(keyring); |
| 84 | goto error_alloc; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 85 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 87 | /* attach the auth key to the session keyring */ |
| 88 | ret = __key_link(keyring, authkey); |
| 89 | if (ret < 0) |
| 90 | goto error_link; |
| 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* record the UID and GID */ |
| 93 | sprintf(uid_str, "%d", current->fsuid); |
| 94 | sprintf(gid_str, "%d", current->fsgid); |
| 95 | |
| 96 | /* we say which key is under construction */ |
| 97 | sprintf(key_str, "%d", key->serial); |
| 98 | |
| 99 | /* we specify the process's default keyrings */ |
| 100 | sprintf(keyring_str[0], "%d", |
| 101 | tsk->thread_keyring ? tsk->thread_keyring->serial : 0); |
| 102 | |
| 103 | prkey = 0; |
| 104 | if (tsk->signal->process_keyring) |
| 105 | prkey = tsk->signal->process_keyring->serial; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | sprintf(keyring_str[1], "%d", prkey); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 108 | |
| 109 | if (tsk->signal->session_keyring) { |
| 110 | rcu_read_lock(); |
| 111 | sskey = rcu_dereference(tsk->signal->session_keyring)->serial; |
| 112 | rcu_read_unlock(); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 113 | } else { |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 114 | sskey = tsk->user->session_keyring->serial; |
| 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | sprintf(keyring_str[2], "%d", sskey); |
| 118 | |
| 119 | /* set up a minimal environment */ |
| 120 | i = 0; |
| 121 | envp[i++] = "HOME=/"; |
| 122 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
| 123 | envp[i] = NULL; |
| 124 | |
| 125 | /* set up the argument list */ |
| 126 | i = 0; |
| 127 | argv[i++] = "/sbin/request-key"; |
| 128 | argv[i++] = (char *) op; |
| 129 | argv[i++] = key_str; |
| 130 | argv[i++] = uid_str; |
| 131 | argv[i++] = gid_str; |
| 132 | argv[i++] = keyring_str[0]; |
| 133 | argv[i++] = keyring_str[1]; |
| 134 | argv[i++] = keyring_str[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | argv[i] = NULL; |
| 136 | |
| 137 | /* do it */ |
Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 138 | ret = call_usermodehelper_keys(argv[0], argv, envp, keyring, |
| 139 | UMH_WAIT_PROC); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 140 | kdebug("usermode -> 0x%x", ret); |
| 141 | if (ret >= 0) { |
| 142 | /* ret is the exit/wait code */ |
| 143 | if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) || |
| 144 | key_validate(key) < 0) |
| 145 | ret = -ENOKEY; |
| 146 | else |
| 147 | /* ignore any errors from userspace if the key was |
| 148 | * instantiated */ |
| 149 | ret = 0; |
| 150 | } |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 151 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 152 | error_link: |
| 153 | key_put(keyring); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 154 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 155 | error_alloc: |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 156 | kleave(" = %d", ret); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 157 | complete_request_key(cons, ret); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 158 | return ret; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 162 | * call out to userspace for key construction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | * - we ignore program failure and go on key status instead |
| 164 | */ |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 165 | static int construct_key(struct key *key, const void *callout_info, |
| 166 | size_t callout_len, void *aux) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 168 | struct key_construction *cons; |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 169 | request_key_actor_t actor; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 170 | struct key *authkey; |
| 171 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 173 | kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 174 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 175 | cons = kmalloc(sizeof(*cons), GFP_KERNEL); |
| 176 | if (!cons) |
| 177 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 179 | /* allocate an authorisation key */ |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 180 | authkey = request_key_auth_new(key, callout_info, callout_len); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 181 | if (IS_ERR(authkey)) { |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 182 | kfree(cons); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 183 | ret = PTR_ERR(authkey); |
| 184 | authkey = NULL; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 185 | } else { |
| 186 | cons->authkey = key_get(authkey); |
| 187 | cons->key = key_get(key); |
| 188 | |
| 189 | /* make the call */ |
| 190 | actor = call_sbin_request_key; |
| 191 | if (key->type->request_key) |
| 192 | actor = key->type->request_key; |
| 193 | |
| 194 | ret = actor(cons, "create", aux); |
| 195 | |
| 196 | /* check that the actor called complete_request_key() prior to |
| 197 | * returning an error */ |
| 198 | WARN_ON(ret < 0 && |
| 199 | !test_bit(KEY_FLAG_REVOKED, &authkey->flags)); |
| 200 | key_put(authkey); |
David Howells | b5f545c | 2006-01-08 01:02:47 -0800 | [diff] [blame] | 201 | } |
| 202 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 203 | kleave(" = %d", ret); |
| 204 | return ret; |
| 205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | /* |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 208 | * link a key to the appropriate destination keyring |
| 209 | * - the caller must hold a write lock on the destination keyring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 211 | static void construct_key_make_link(struct key *key, struct key *dest_keyring) |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 212 | { |
| 213 | struct task_struct *tsk = current; |
| 214 | struct key *drop = NULL; |
| 215 | |
| 216 | kenter("{%d},%p", key->serial, dest_keyring); |
| 217 | |
| 218 | /* find the appropriate keyring */ |
| 219 | if (!dest_keyring) { |
| 220 | switch (tsk->jit_keyring) { |
| 221 | case KEY_REQKEY_DEFL_DEFAULT: |
| 222 | case KEY_REQKEY_DEFL_THREAD_KEYRING: |
| 223 | dest_keyring = tsk->thread_keyring; |
| 224 | if (dest_keyring) |
| 225 | break; |
| 226 | |
| 227 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: |
| 228 | dest_keyring = tsk->signal->process_keyring; |
| 229 | if (dest_keyring) |
| 230 | break; |
| 231 | |
| 232 | case KEY_REQKEY_DEFL_SESSION_KEYRING: |
| 233 | rcu_read_lock(); |
| 234 | dest_keyring = key_get( |
| 235 | rcu_dereference(tsk->signal->session_keyring)); |
| 236 | rcu_read_unlock(); |
| 237 | drop = dest_keyring; |
| 238 | |
| 239 | if (dest_keyring) |
| 240 | break; |
| 241 | |
| 242 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 243 | dest_keyring = tsk->user->session_keyring; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 244 | break; |
| 245 | |
| 246 | case KEY_REQKEY_DEFL_USER_KEYRING: |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 247 | dest_keyring = tsk->user->uid_keyring; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 248 | break; |
| 249 | |
| 250 | case KEY_REQKEY_DEFL_GROUP_KEYRING: |
| 251 | default: |
| 252 | BUG(); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | /* and attach the key to it */ |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 257 | __key_link(dest_keyring, key); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 258 | key_put(drop); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 259 | kleave(""); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 260 | } |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 261 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 262 | /* |
| 263 | * allocate a new key in under-construction state and attempt to link it in to |
| 264 | * the requested place |
| 265 | * - may return a key that's already under construction instead |
| 266 | */ |
| 267 | static int construct_alloc_key(struct key_type *type, |
| 268 | const char *description, |
| 269 | struct key *dest_keyring, |
| 270 | unsigned long flags, |
| 271 | struct key_user *user, |
| 272 | struct key **_key) |
| 273 | { |
| 274 | struct key *key; |
| 275 | key_ref_t key_ref; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 276 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 277 | kenter("%s,%s,,,", type->name, description); |
| 278 | |
| 279 | mutex_lock(&user->cons_lock); |
| 280 | |
| 281 | key = key_alloc(type, description, |
| 282 | current->fsuid, current->fsgid, current, KEY_POS_ALL, |
| 283 | flags); |
| 284 | if (IS_ERR(key)) |
| 285 | goto alloc_failed; |
| 286 | |
| 287 | set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags); |
| 288 | |
| 289 | if (dest_keyring) |
| 290 | down_write(&dest_keyring->sem); |
| 291 | |
| 292 | /* attach the key to the destination keyring under lock, but we do need |
| 293 | * to do another check just in case someone beat us to it whilst we |
| 294 | * waited for locks */ |
| 295 | mutex_lock(&key_construction_mutex); |
| 296 | |
| 297 | key_ref = search_process_keyrings(type, description, type->match, |
| 298 | current); |
| 299 | if (!IS_ERR(key_ref)) |
| 300 | goto key_already_present; |
| 301 | |
| 302 | if (dest_keyring) |
| 303 | construct_key_make_link(key, dest_keyring); |
| 304 | |
| 305 | mutex_unlock(&key_construction_mutex); |
| 306 | if (dest_keyring) |
| 307 | up_write(&dest_keyring->sem); |
| 308 | mutex_unlock(&user->cons_lock); |
| 309 | *_key = key; |
| 310 | kleave(" = 0 [%d]", key_serial(key)); |
| 311 | return 0; |
| 312 | |
| 313 | key_already_present: |
| 314 | mutex_unlock(&key_construction_mutex); |
| 315 | if (dest_keyring) |
| 316 | up_write(&dest_keyring->sem); |
| 317 | mutex_unlock(&user->cons_lock); |
| 318 | key_put(key); |
| 319 | *_key = key = key_ref_to_ptr(key_ref); |
| 320 | kleave(" = -EINPROGRESS [%d]", key_serial(key)); |
| 321 | return -EINPROGRESS; |
| 322 | |
| 323 | alloc_failed: |
| 324 | mutex_unlock(&user->cons_lock); |
| 325 | *_key = NULL; |
| 326 | kleave(" = %ld", PTR_ERR(key)); |
| 327 | return PTR_ERR(key); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * commence key construction |
| 332 | */ |
| 333 | static struct key *construct_key_and_link(struct key_type *type, |
| 334 | const char *description, |
| 335 | const char *callout_info, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 336 | size_t callout_len, |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 337 | void *aux, |
| 338 | struct key *dest_keyring, |
| 339 | unsigned long flags) |
| 340 | { |
| 341 | struct key_user *user; |
| 342 | struct key *key; |
| 343 | int ret; |
| 344 | |
| 345 | user = key_user_lookup(current->fsuid); |
| 346 | if (!user) |
| 347 | return ERR_PTR(-ENOMEM); |
| 348 | |
| 349 | ret = construct_alloc_key(type, description, dest_keyring, flags, user, |
| 350 | &key); |
| 351 | key_user_put(user); |
| 352 | |
| 353 | if (ret == 0) { |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 354 | ret = construct_key(key, callout_info, callout_len, aux); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 355 | if (ret < 0) |
| 356 | goto construction_failed; |
| 357 | } |
| 358 | |
| 359 | return key; |
| 360 | |
| 361 | construction_failed: |
| 362 | key_negate_and_link(key, key_negative_timeout, NULL, NULL); |
| 363 | key_put(key); |
| 364 | return ERR_PTR(ret); |
| 365 | } |
| 366 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 367 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | * request a key |
| 369 | * - search the process's keyrings |
| 370 | * - check the list of keys being created or updated |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 371 | * - call out to userspace for a key if supplementary info was provided |
| 372 | * - cache the key in an appropriate keyring |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | */ |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 374 | struct key *request_key_and_link(struct key_type *type, |
| 375 | const char *description, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 376 | const void *callout_info, |
| 377 | size_t callout_len, |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 378 | void *aux, |
David Howells | 7e047ef | 2006-06-26 00:24:50 -0700 | [diff] [blame] | 379 | struct key *dest_keyring, |
| 380 | unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | struct key *key; |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 383 | key_ref_t key_ref; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 385 | kenter("%s,%s,%p,%zu,%p,%p,%lx", |
| 386 | type->name, description, callout_info, callout_len, aux, |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 387 | dest_keyring, flags); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* search all the process keyrings for a key */ |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 390 | key_ref = search_process_keyrings(type, description, type->match, |
| 391 | current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
David Howells | 664cceb | 2005-09-28 17:03:15 +0100 | [diff] [blame] | 393 | if (!IS_ERR(key_ref)) { |
| 394 | key = key_ref_to_ptr(key_ref); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 395 | } else if (PTR_ERR(key_ref) != -EAGAIN) { |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 396 | key = ERR_CAST(key_ref); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 397 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /* the search failed, but the keyrings were searchable, so we |
| 399 | * should consult userspace if we can */ |
| 400 | key = ERR_PTR(-ENOKEY); |
| 401 | if (!callout_info) |
| 402 | goto error; |
| 403 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 404 | key = construct_key_and_link(type, description, callout_info, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 405 | callout_len, aux, dest_keyring, |
| 406 | flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | } |
| 408 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 409 | error: |
| 410 | kleave(" = %p", key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | return key; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 414 | /* |
| 415 | * wait for construction of a key to complete |
| 416 | */ |
| 417 | int wait_for_key_construction(struct key *key, bool intr) |
| 418 | { |
| 419 | int ret; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 420 | |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 421 | ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT, |
| 422 | intr ? key_wait_bit_intr : key_wait_bit, |
| 423 | intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); |
| 424 | if (ret < 0) |
| 425 | return ret; |
| 426 | return key_validate(key); |
| 427 | } |
| 428 | EXPORT_SYMBOL(wait_for_key_construction); |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 429 | |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 430 | /* |
| 431 | * request a key |
| 432 | * - search the process's keyrings |
| 433 | * - check the list of keys being created or updated |
| 434 | * - call out to userspace for a key if supplementary info was provided |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 435 | * - waits uninterruptible for creation to complete |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 436 | */ |
| 437 | struct key *request_key(struct key_type *type, |
| 438 | const char *description, |
| 439 | const char *callout_info) |
| 440 | { |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 441 | struct key *key; |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 442 | size_t callout_len = 0; |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 443 | int ret; |
David Howells | 3e30148 | 2005-06-23 22:00:56 -0700 | [diff] [blame] | 444 | |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 445 | if (callout_info) |
| 446 | callout_len = strlen(callout_info); |
| 447 | key = request_key_and_link(type, description, callout_info, callout_len, |
| 448 | NULL, NULL, KEY_ALLOC_IN_QUOTA); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 449 | if (!IS_ERR(key)) { |
| 450 | ret = wait_for_key_construction(key, false); |
| 451 | if (ret < 0) { |
| 452 | key_put(key); |
| 453 | return ERR_PTR(ret); |
| 454 | } |
| 455 | } |
| 456 | return key; |
| 457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | EXPORT_SYMBOL(request_key); |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 459 | |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 460 | /* |
| 461 | * request a key with auxiliary data for the upcaller |
| 462 | * - search the process's keyrings |
| 463 | * - check the list of keys being created or updated |
| 464 | * - call out to userspace for a key if supplementary info was provided |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 465 | * - waits uninterruptible for creation to complete |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 466 | */ |
| 467 | struct key *request_key_with_auxdata(struct key_type *type, |
| 468 | const char *description, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 469 | const void *callout_info, |
| 470 | size_t callout_len, |
David Howells | 4e54f08 | 2006-06-29 02:24:28 -0700 | [diff] [blame] | 471 | void *aux) |
| 472 | { |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 473 | struct key *key; |
| 474 | int ret; |
| 475 | |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 476 | key = request_key_and_link(type, description, callout_info, callout_len, |
| 477 | aux, NULL, KEY_ALLOC_IN_QUOTA); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 478 | if (!IS_ERR(key)) { |
| 479 | ret = wait_for_key_construction(key, false); |
| 480 | if (ret < 0) { |
| 481 | key_put(key); |
| 482 | return ERR_PTR(ret); |
| 483 | } |
| 484 | } |
| 485 | return key; |
| 486 | } |
| 487 | EXPORT_SYMBOL(request_key_with_auxdata); |
| 488 | |
| 489 | /* |
| 490 | * request a key (allow async construction) |
| 491 | * - search the process's keyrings |
| 492 | * - check the list of keys being created or updated |
| 493 | * - call out to userspace for a key if supplementary info was provided |
| 494 | */ |
| 495 | struct key *request_key_async(struct key_type *type, |
| 496 | const char *description, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 497 | const void *callout_info, |
| 498 | size_t callout_len) |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 499 | { |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 500 | return request_key_and_link(type, description, callout_info, |
| 501 | callout_len, NULL, NULL, |
| 502 | KEY_ALLOC_IN_QUOTA); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 503 | } |
| 504 | EXPORT_SYMBOL(request_key_async); |
| 505 | |
| 506 | /* |
| 507 | * request a key with auxiliary data for the upcaller (allow async construction) |
| 508 | * - search the process's keyrings |
| 509 | * - check the list of keys being created or updated |
| 510 | * - call out to userspace for a key if supplementary info was provided |
| 511 | */ |
| 512 | struct key *request_key_async_with_auxdata(struct key_type *type, |
| 513 | const char *description, |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 514 | const void *callout_info, |
| 515 | size_t callout_len, |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 516 | void *aux) |
| 517 | { |
David Howells | 4a38e12 | 2008-04-29 01:01:24 -0700 | [diff] [blame] | 518 | return request_key_and_link(type, description, callout_info, |
| 519 | callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA); |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 520 | } |
| 521 | EXPORT_SYMBOL(request_key_async_with_auxdata); |