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