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