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