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