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