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