]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - security/keys/process_keys.c
apparmor: reset pos on failure to unpack for various functions
[mirror_ubuntu-bionic-kernel.git] / security / keys / process_keys.c
CommitLineData
973c9f4f 1/* Manage a process's keyrings
1da177e4 2 *
69664cf1 3 * Copyright (C) 2004-2005, 2008 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.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
8703e8a4 15#include <linux/sched/user.h>
1da177e4
LT
16#include <linux/keyctl.h>
17#include <linux/fs.h>
18#include <linux/err.h>
bb003079 19#include <linux/mutex.h>
ee18d64c 20#include <linux/security.h>
1d1e9756 21#include <linux/user_namespace.h>
7c0f6ba6 22#include <linux/uaccess.h>
f6052007 23#include <keys/request_key_auth-type.h>
1da177e4
LT
24#include "internal.h"
25
973c9f4f 26/* Session keyring create vs join semaphore */
bb003079 27static DEFINE_MUTEX(key_session_mutex);
1da177e4 28
973c9f4f 29/* User keyring creation semaphore */
69664cf1
DH
30static DEFINE_MUTEX(key_user_keyring_mutex);
31
973c9f4f 32/* The root user's tracking struct */
1da177e4 33struct key_user root_key_user = {
ddb99e11 34 .usage = REFCOUNT_INIT(3),
76181c13 35 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
6cfd76a2 36 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
1da177e4
LT
37 .nkeys = ATOMIC_INIT(2),
38 .nikeys = ATOMIC_INIT(2),
9a56c2db 39 .uid = GLOBAL_ROOT_UID,
1da177e4
LT
40};
41
1da177e4 42/*
973c9f4f 43 * Install the user and user session keyrings for the current process's UID.
1da177e4 44 */
8bbf4976 45int install_user_keyrings(void)
1da177e4 46{
d84f4f99
DH
47 struct user_struct *user;
48 const struct cred *cred;
1da177e4 49 struct key *uid_keyring, *session_keyring;
96b5c8fe 50 key_perm_t user_keyring_perm;
1da177e4
LT
51 char buf[20];
52 int ret;
9a56c2db 53 uid_t uid;
1da177e4 54
96b5c8fe 55 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
d84f4f99
DH
56 cred = current_cred();
57 user = cred->user;
9a56c2db 58 uid = from_kuid(cred->user_ns, user->uid);
d84f4f99 59
9a56c2db 60 kenter("%p{%u}", user, uid);
1da177e4 61
0da9dfdd 62 if (user->uid_keyring && user->session_keyring) {
69664cf1
DH
63 kleave(" = 0 [exist]");
64 return 0;
1da177e4
LT
65 }
66
69664cf1
DH
67 mutex_lock(&key_user_keyring_mutex);
68 ret = 0;
1da177e4 69
69664cf1
DH
70 if (!user->uid_keyring) {
71 /* get the UID-specific keyring
72 * - there may be one in existence already as it may have been
73 * pinned by a session, but the user_struct pointing to it
74 * may have been destroyed by setuid */
9a56c2db 75 sprintf(buf, "_uid.%u", uid);
69664cf1
DH
76
77 uid_keyring = find_keyring_by_name(buf, true);
78 if (IS_ERR(uid_keyring)) {
9a56c2db 79 uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
96b5c8fe 80 cred, user_keyring_perm,
237bbd29
EB
81 KEY_ALLOC_UID_KEYRING |
82 KEY_ALLOC_IN_QUOTA,
5ac7eace 83 NULL, NULL);
69664cf1
DH
84 if (IS_ERR(uid_keyring)) {
85 ret = PTR_ERR(uid_keyring);
86 goto error;
87 }
88 }
89
90 /* get a default session keyring (which might also exist
91 * already) */
9a56c2db 92 sprintf(buf, "_uid_ses.%u", uid);
69664cf1
DH
93
94 session_keyring = find_keyring_by_name(buf, true);
95 if (IS_ERR(session_keyring)) {
96 session_keyring =
9a56c2db 97 keyring_alloc(buf, user->uid, INVALID_GID,
96b5c8fe 98 cred, user_keyring_perm,
237bbd29
EB
99 KEY_ALLOC_UID_KEYRING |
100 KEY_ALLOC_IN_QUOTA,
5ac7eace 101 NULL, NULL);
69664cf1
DH
102 if (IS_ERR(session_keyring)) {
103 ret = PTR_ERR(session_keyring);
104 goto error_release;
105 }
106
107 /* we install a link from the user session keyring to
108 * the user keyring */
109 ret = key_link(session_keyring, uid_keyring);
110 if (ret < 0)
111 goto error_release_both;
112 }
113
114 /* install the keyrings */
115 user->uid_keyring = uid_keyring;
116 user->session_keyring = session_keyring;
1da177e4
LT
117 }
118
69664cf1
DH
119 mutex_unlock(&key_user_keyring_mutex);
120 kleave(" = 0");
121 return 0;
1da177e4 122
69664cf1
DH
123error_release_both:
124 key_put(session_keyring);
125error_release:
126 key_put(uid_keyring);
664cceb0 127error:
69664cf1
DH
128 mutex_unlock(&key_user_keyring_mutex);
129 kleave(" = %d", ret);
1da177e4 130 return ret;
69664cf1 131}
1da177e4 132
1da177e4 133/*
c9f838d1
EB
134 * Install a thread keyring to the given credentials struct if it didn't have
135 * one already. This is allowed to overrun the quota.
136 *
137 * Return: 0 if a thread keyring is now present; -errno on failure.
1da177e4 138 */
d84f4f99 139int install_thread_keyring_to_cred(struct cred *new)
1da177e4 140{
d84f4f99 141 struct key *keyring;
1da177e4 142
c9f838d1
EB
143 if (new->thread_keyring)
144 return 0;
145
d84f4f99 146 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
96b5c8fe 147 KEY_POS_ALL | KEY_USR_VIEW,
5ac7eace
DH
148 KEY_ALLOC_QUOTA_OVERRUN,
149 NULL, NULL);
d84f4f99
DH
150 if (IS_ERR(keyring))
151 return PTR_ERR(keyring);
1da177e4 152
d84f4f99
DH
153 new->thread_keyring = keyring;
154 return 0;
155}
1da177e4 156
1da177e4 157/*
c9f838d1
EB
158 * Install a thread keyring to the current task if it didn't have one already.
159 *
160 * Return: 0 if a thread keyring is now present; -errno on failure.
1da177e4 161 */
d84f4f99 162static int install_thread_keyring(void)
1da177e4 163{
d84f4f99 164 struct cred *new;
1da177e4
LT
165 int ret;
166
d84f4f99
DH
167 new = prepare_creds();
168 if (!new)
169 return -ENOMEM;
1da177e4 170
d84f4f99
DH
171 ret = install_thread_keyring_to_cred(new);
172 if (ret < 0) {
173 abort_creds(new);
174 return ret;
1da177e4
LT
175 }
176
d84f4f99
DH
177 return commit_creds(new);
178}
1da177e4 179
d84f4f99 180/*
c9f838d1
EB
181 * Install a process keyring to the given credentials struct if it didn't have
182 * one already. This is allowed to overrun the quota.
973c9f4f 183 *
c9f838d1 184 * Return: 0 if a process keyring is now present; -errno on failure.
d84f4f99
DH
185 */
186int install_process_keyring_to_cred(struct cred *new)
187{
188 struct key *keyring;
1da177e4 189
3a50597d 190 if (new->process_keyring)
c9f838d1 191 return 0;
d84f4f99 192
96b5c8fe
DH
193 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
194 KEY_POS_ALL | KEY_USR_VIEW,
5ac7eace
DH
195 KEY_ALLOC_QUOTA_OVERRUN,
196 NULL, NULL);
d84f4f99
DH
197 if (IS_ERR(keyring))
198 return PTR_ERR(keyring);
199
3a50597d
DH
200 new->process_keyring = keyring;
201 return 0;
d84f4f99 202}
1da177e4 203
1da177e4 204/*
c9f838d1 205 * Install a process keyring to the current task if it didn't have one already.
973c9f4f 206 *
c9f838d1 207 * Return: 0 if a process keyring is now present; -errno on failure.
1da177e4 208 */
d84f4f99 209static int install_process_keyring(void)
1da177e4 210{
d84f4f99 211 struct cred *new;
1da177e4
LT
212 int ret;
213
d84f4f99
DH
214 new = prepare_creds();
215 if (!new)
216 return -ENOMEM;
1da177e4 217
d84f4f99
DH
218 ret = install_process_keyring_to_cred(new);
219 if (ret < 0) {
220 abort_creds(new);
c9f838d1 221 return ret;
1da177e4
LT
222 }
223
d84f4f99
DH
224 return commit_creds(new);
225}
1da177e4 226
1da177e4 227/*
c9f838d1
EB
228 * Install the given keyring as the session keyring of the given credentials
229 * struct, replacing the existing one if any. If the given keyring is NULL,
230 * then install a new anonymous session keyring.
231 *
232 * Return: 0 on success; -errno on failure.
1da177e4 233 */
685bfd2c 234int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
1da177e4 235{
7e047ef5 236 unsigned long flags;
1da177e4 237 struct key *old;
1a26feb9
DH
238
239 might_sleep();
1da177e4
LT
240
241 /* create an empty session keyring */
242 if (!keyring) {
7e047ef5 243 flags = KEY_ALLOC_QUOTA_OVERRUN;
3a50597d 244 if (cred->session_keyring)
7e047ef5
DH
245 flags = KEY_ALLOC_IN_QUOTA;
246
96b5c8fe
DH
247 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
248 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
5ac7eace 249 flags, NULL, NULL);
1a26feb9
DH
250 if (IS_ERR(keyring))
251 return PTR_ERR(keyring);
d84f4f99 252 } else {
ccc3e6d9 253 __key_get(keyring);
1da177e4
LT
254 }
255
256 /* install the keyring */
3a50597d
DH
257 old = cred->session_keyring;
258 rcu_assign_pointer(cred->session_keyring, keyring);
259
260 if (old)
1a26feb9 261 key_put(old);
1da177e4 262
1a26feb9 263 return 0;
d84f4f99 264}
1da177e4 265
1da177e4 266/*
c9f838d1
EB
267 * Install the given keyring as the session keyring of the current task,
268 * replacing the existing one if any. If the given keyring is NULL, then
269 * install a new anonymous session keyring.
270 *
271 * Return: 0 on success; -errno on failure.
1da177e4 272 */
d84f4f99 273static int install_session_keyring(struct key *keyring)
1da177e4 274{
d84f4f99
DH
275 struct cred *new;
276 int ret;
1da177e4 277
d84f4f99
DH
278 new = prepare_creds();
279 if (!new)
280 return -ENOMEM;
1da177e4 281
99599537 282 ret = install_session_keyring_to_cred(new, keyring);
d84f4f99
DH
283 if (ret < 0) {
284 abort_creds(new);
285 return ret;
286 }
1da177e4 287
d84f4f99
DH
288 return commit_creds(new);
289}
1da177e4 290
1da177e4 291/*
973c9f4f 292 * Handle the fsuid changing.
1da177e4
LT
293 */
294void key_fsuid_changed(struct task_struct *tsk)
295{
296 /* update the ownership of the thread keyring */
b6dff3ec
DH
297 BUG_ON(!tsk->cred);
298 if (tsk->cred->thread_keyring) {
299 down_write(&tsk->cred->thread_keyring->sem);
300 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
301 up_write(&tsk->cred->thread_keyring->sem);
1da177e4 302 }
a8b17ed0 303}
1da177e4 304
1da177e4 305/*
973c9f4f 306 * Handle the fsgid changing.
1da177e4
LT
307 */
308void key_fsgid_changed(struct task_struct *tsk)
309{
310 /* update the ownership of the thread keyring */
b6dff3ec
DH
311 BUG_ON(!tsk->cred);
312 if (tsk->cred->thread_keyring) {
313 down_write(&tsk->cred->thread_keyring->sem);
314 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
315 up_write(&tsk->cred->thread_keyring->sem);
1da177e4 316 }
a8b17ed0 317}
1da177e4 318
1da177e4 319/*
973c9f4f
DH
320 * Search the process keyrings attached to the supplied cred for the first
321 * matching key.
322 *
323 * The search criteria are the type and the match function. The description is
324 * given to the match function as a parameter, but doesn't otherwise influence
325 * the search. Typically the match function will compare the description
326 * parameter to the key's description.
327 *
328 * This can only search keyrings that grant Search permission to the supplied
329 * credentials. Keyrings linked to searched keyrings will also be searched if
330 * they grant Search permission too. Keys can only be found if they grant
331 * Search permission to the credentials.
332 *
333 * Returns a pointer to the key with the key usage count incremented if
334 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
335 * matched negative keys.
336 *
337 * In the case of a successful return, the possession attribute is set on the
338 * returned key reference.
1da177e4 339 */
4bdf0bc3 340key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
1da177e4 341{
b5f545c8 342 key_ref_t key_ref, ret, err;
1da177e4
LT
343
344 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
345 * searchable, but we failed to find a key or we found a negative key;
346 * otherwise we want to return a sample error (probably -EACCES) if
347 * none of the keyrings were searchable
348 *
349 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
350 */
664cceb0 351 key_ref = NULL;
1da177e4
LT
352 ret = NULL;
353 err = ERR_PTR(-EAGAIN);
354
355 /* search the thread keyring first */
4bdf0bc3 356 if (ctx->cred->thread_keyring) {
664cceb0 357 key_ref = keyring_search_aux(
4bdf0bc3 358 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
664cceb0 359 if (!IS_ERR(key_ref))
1da177e4
LT
360 goto found;
361
664cceb0 362 switch (PTR_ERR(key_ref)) {
1da177e4 363 case -EAGAIN: /* no key */
1da177e4 364 case -ENOKEY: /* negative key */
664cceb0 365 ret = key_ref;
1da177e4
LT
366 break;
367 default:
664cceb0 368 err = key_ref;
1da177e4
LT
369 break;
370 }
371 }
372
373 /* search the process keyring second */
4bdf0bc3 374 if (ctx->cred->process_keyring) {
664cceb0 375 key_ref = keyring_search_aux(
4bdf0bc3 376 make_key_ref(ctx->cred->process_keyring, 1), ctx);
664cceb0 377 if (!IS_ERR(key_ref))
1da177e4
LT
378 goto found;
379
664cceb0 380 switch (PTR_ERR(key_ref)) {
1da177e4 381 case -EAGAIN: /* no key */
fe9453a1
DH
382 if (ret)
383 break;
1da177e4 384 case -ENOKEY: /* negative key */
664cceb0 385 ret = key_ref;
1da177e4
LT
386 break;
387 default:
664cceb0 388 err = key_ref;
1da177e4
LT
389 break;
390 }
391 }
392
3e30148c 393 /* search the session keyring */
4bdf0bc3 394 if (ctx->cred->session_keyring) {
8589b4e0 395 rcu_read_lock();
664cceb0 396 key_ref = keyring_search_aux(
4bdf0bc3
DH
397 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
398 ctx);
8589b4e0 399 rcu_read_unlock();
3e30148c 400
664cceb0 401 if (!IS_ERR(key_ref))
3e30148c
DH
402 goto found;
403
664cceb0 404 switch (PTR_ERR(key_ref)) {
3e30148c
DH
405 case -EAGAIN: /* no key */
406 if (ret)
407 break;
408 case -ENOKEY: /* negative key */
664cceb0 409 ret = key_ref;
3e30148c
DH
410 break;
411 default:
664cceb0 412 err = key_ref;
3e30148c
DH
413 break;
414 }
b5f545c8
DH
415 }
416 /* or search the user-session keyring */
4bdf0bc3 417 else if (ctx->cred->user->session_keyring) {
b5f545c8 418 key_ref = keyring_search_aux(
4bdf0bc3
DH
419 make_key_ref(ctx->cred->user->session_keyring, 1),
420 ctx);
664cceb0 421 if (!IS_ERR(key_ref))
3e30148c
DH
422 goto found;
423
664cceb0 424 switch (PTR_ERR(key_ref)) {
3e30148c
DH
425 case -EAGAIN: /* no key */
426 if (ret)
427 break;
428 case -ENOKEY: /* negative key */
664cceb0 429 ret = key_ref;
3e30148c
DH
430 break;
431 default:
664cceb0 432 err = key_ref;
3e30148c
DH
433 break;
434 }
8589b4e0 435 }
b5f545c8 436
927942aa
DH
437 /* no key - decide on the error we're going to go for */
438 key_ref = ret ? ret : err;
439
440found:
441 return key_ref;
442}
443
927942aa 444/*
973c9f4f
DH
445 * Search the process keyrings attached to the supplied cred for the first
446 * matching key in the manner of search_my_process_keyrings(), but also search
447 * the keys attached to the assumed authorisation key using its credentials if
448 * one is available.
449 *
450 * Return same as search_my_process_keyrings().
927942aa 451 */
4bdf0bc3 452key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
927942aa
DH
453{
454 struct request_key_auth *rka;
455 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
456
457 might_sleep();
458
4bdf0bc3 459 key_ref = search_my_process_keyrings(ctx);
927942aa
DH
460 if (!IS_ERR(key_ref))
461 goto found;
462 err = key_ref;
463
b5f545c8
DH
464 /* if this process has an instantiation authorisation key, then we also
465 * search the keyrings of the process mentioned there
466 * - we don't permit access to request_key auth keys via this method
467 */
4bdf0bc3
DH
468 if (ctx->cred->request_key_auth &&
469 ctx->cred == current_cred() &&
470 ctx->index_key.type != &key_type_request_key_auth
b5f545c8 471 ) {
4bdf0bc3
DH
472 const struct cred *cred = ctx->cred;
473
04c567d9 474 /* defend against the auth key being revoked */
c69e8d9c 475 down_read(&cred->request_key_auth->sem);
b5f545c8 476
4bdf0bc3 477 if (key_validate(ctx->cred->request_key_auth) == 0) {
146aa8b1 478 rka = ctx->cred->request_key_auth->payload.data[0];
b5f545c8 479
4bdf0bc3
DH
480 ctx->cred = rka->cred;
481 key_ref = search_process_keyrings(ctx);
482 ctx->cred = cred;
1da177e4 483
c69e8d9c 484 up_read(&cred->request_key_auth->sem);
04c567d9
DH
485
486 if (!IS_ERR(key_ref))
487 goto found;
488
927942aa 489 ret = key_ref;
04c567d9 490 } else {
c69e8d9c 491 up_read(&cred->request_key_auth->sem);
3e30148c 492 }
1da177e4
LT
493 }
494
495 /* no key - decide on the error we're going to go for */
927942aa
DH
496 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
497 key_ref = ERR_PTR(-ENOKEY);
498 else if (err == ERR_PTR(-EACCES))
499 key_ref = ret;
500 else
501 key_ref = err;
1da177e4 502
3e30148c 503found:
664cceb0 504 return key_ref;
a8b17ed0 505}
1da177e4 506
664cceb0 507/*
973c9f4f 508 * See if the key we're looking at is the target key.
664cceb0 509 */
0c903ab6
DH
510bool lookup_user_key_possessed(const struct key *key,
511 const struct key_match_data *match_data)
664cceb0 512{
46291959 513 return key == match_data->raw_data;
a8b17ed0 514}
664cceb0 515
1da177e4 516/*
973c9f4f
DH
517 * Look up a key ID given us by userspace with a given permissions mask to get
518 * the key it refers to.
519 *
520 * Flags can be passed to request that special keyrings be created if referred
521 * to directly, to permit partially constructed keys to be found and to skip
522 * validity and permission checks on the found key.
523 *
524 * Returns a pointer to the key with an incremented usage count if successful;
525 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
526 * to a key or the best found key was a negative key; -EKEYREVOKED or
527 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
528 * found key doesn't grant the requested permit or the LSM denied access to it;
529 * or -ENOMEM if a special keyring couldn't be created.
530 *
531 * In the case of a successful return, the possession attribute is set on the
532 * returned key reference.
1da177e4 533 */
5593122e 534key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
8bbf4976 535 key_perm_t perm)
1da177e4 536{
4bdf0bc3 537 struct keyring_search_context ctx = {
46291959
DH
538 .match_data.cmp = lookup_user_key_possessed,
539 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
540 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
4bdf0bc3 541 };
8bbf4976 542 struct request_key_auth *rka;
1da177e4 543 struct key *key;
b6dff3ec 544 key_ref_t key_ref, skey_ref;
1da177e4
LT
545 int ret;
546
bb952bb9 547try_again:
4bdf0bc3 548 ctx.cred = get_current_cred();
664cceb0 549 key_ref = ERR_PTR(-ENOKEY);
1da177e4
LT
550
551 switch (id) {
552 case KEY_SPEC_THREAD_KEYRING:
4bdf0bc3 553 if (!ctx.cred->thread_keyring) {
5593122e 554 if (!(lflags & KEY_LOOKUP_CREATE))
1da177e4
LT
555 goto error;
556
8bbf4976 557 ret = install_thread_keyring();
1da177e4 558 if (ret < 0) {
4d09ec0f 559 key_ref = ERR_PTR(ret);
1da177e4
LT
560 goto error;
561 }
bb952bb9 562 goto reget_creds;
1da177e4
LT
563 }
564
4bdf0bc3 565 key = ctx.cred->thread_keyring;
ccc3e6d9 566 __key_get(key);
664cceb0 567 key_ref = make_key_ref(key, 1);
1da177e4
LT
568 break;
569
570 case KEY_SPEC_PROCESS_KEYRING:
4bdf0bc3 571 if (!ctx.cred->process_keyring) {
5593122e 572 if (!(lflags & KEY_LOOKUP_CREATE))
1da177e4
LT
573 goto error;
574
8bbf4976 575 ret = install_process_keyring();
1da177e4 576 if (ret < 0) {
4d09ec0f 577 key_ref = ERR_PTR(ret);
1da177e4
LT
578 goto error;
579 }
bb952bb9 580 goto reget_creds;
1da177e4
LT
581 }
582
4bdf0bc3 583 key = ctx.cred->process_keyring;
ccc3e6d9 584 __key_get(key);
664cceb0 585 key_ref = make_key_ref(key, 1);
1da177e4
LT
586 break;
587
588 case KEY_SPEC_SESSION_KEYRING:
4bdf0bc3 589 if (!ctx.cred->session_keyring) {
1da177e4
LT
590 /* always install a session keyring upon access if one
591 * doesn't exist yet */
8bbf4976 592 ret = install_user_keyrings();
69664cf1
DH
593 if (ret < 0)
594 goto error;
3ecf1b4f
DH
595 if (lflags & KEY_LOOKUP_CREATE)
596 ret = join_session_keyring(NULL);
597 else
598 ret = install_session_keyring(
4bdf0bc3 599 ctx.cred->user->session_keyring);
d84f4f99 600
1da177e4
LT
601 if (ret < 0)
602 goto error;
bb952bb9 603 goto reget_creds;
4bdf0bc3
DH
604 } else if (ctx.cred->session_keyring ==
605 ctx.cred->user->session_keyring &&
3ecf1b4f
DH
606 lflags & KEY_LOOKUP_CREATE) {
607 ret = join_session_keyring(NULL);
608 if (ret < 0)
609 goto error;
610 goto reget_creds;
1da177e4
LT
611 }
612
3e30148c 613 rcu_read_lock();
4bdf0bc3 614 key = rcu_dereference(ctx.cred->session_keyring);
ccc3e6d9 615 __key_get(key);
3e30148c 616 rcu_read_unlock();
664cceb0 617 key_ref = make_key_ref(key, 1);
1da177e4
LT
618 break;
619
620 case KEY_SPEC_USER_KEYRING:
4bdf0bc3 621 if (!ctx.cred->user->uid_keyring) {
8bbf4976 622 ret = install_user_keyrings();
69664cf1
DH
623 if (ret < 0)
624 goto error;
625 }
626
4bdf0bc3 627 key = ctx.cred->user->uid_keyring;
ccc3e6d9 628 __key_get(key);
664cceb0 629 key_ref = make_key_ref(key, 1);
1da177e4
LT
630 break;
631
632 case KEY_SPEC_USER_SESSION_KEYRING:
4bdf0bc3 633 if (!ctx.cred->user->session_keyring) {
8bbf4976 634 ret = install_user_keyrings();
69664cf1
DH
635 if (ret < 0)
636 goto error;
637 }
638
4bdf0bc3 639 key = ctx.cred->user->session_keyring;
ccc3e6d9 640 __key_get(key);
664cceb0 641 key_ref = make_key_ref(key, 1);
1da177e4
LT
642 break;
643
644 case KEY_SPEC_GROUP_KEYRING:
645 /* group keyrings are not yet supported */
4d09ec0f 646 key_ref = ERR_PTR(-EINVAL);
1da177e4
LT
647 goto error;
648
b5f545c8 649 case KEY_SPEC_REQKEY_AUTH_KEY:
4bdf0bc3 650 key = ctx.cred->request_key_auth;
b5f545c8
DH
651 if (!key)
652 goto error;
653
ccc3e6d9 654 __key_get(key);
b5f545c8
DH
655 key_ref = make_key_ref(key, 1);
656 break;
657
8bbf4976 658 case KEY_SPEC_REQUESTOR_KEYRING:
4bdf0bc3 659 if (!ctx.cred->request_key_auth)
8bbf4976
DH
660 goto error;
661
4bdf0bc3 662 down_read(&ctx.cred->request_key_auth->sem);
f67dabbd 663 if (test_bit(KEY_FLAG_REVOKED,
4bdf0bc3 664 &ctx.cred->request_key_auth->flags)) {
8bbf4976
DH
665 key_ref = ERR_PTR(-EKEYREVOKED);
666 key = NULL;
667 } else {
146aa8b1 668 rka = ctx.cred->request_key_auth->payload.data[0];
8bbf4976 669 key = rka->dest_keyring;
ccc3e6d9 670 __key_get(key);
8bbf4976 671 }
4bdf0bc3 672 up_read(&ctx.cred->request_key_auth->sem);
8bbf4976
DH
673 if (!key)
674 goto error;
675 key_ref = make_key_ref(key, 1);
676 break;
677
1da177e4 678 default:
664cceb0 679 key_ref = ERR_PTR(-EINVAL);
1da177e4
LT
680 if (id < 1)
681 goto error;
682
683 key = key_lookup(id);
664cceb0 684 if (IS_ERR(key)) {
e231c2ee 685 key_ref = ERR_CAST(key);
1da177e4 686 goto error;
664cceb0
DH
687 }
688
689 key_ref = make_key_ref(key, 0);
690
691 /* check to see if we possess the key */
4bdf0bc3
DH
692 ctx.index_key.type = key->type;
693 ctx.index_key.description = key->description;
694 ctx.index_key.desc_len = strlen(key->description);
46291959 695 ctx.match_data.raw_data = key;
4bdf0bc3
DH
696 kdebug("check possessed");
697 skey_ref = search_process_keyrings(&ctx);
698 kdebug("possessed=%p", skey_ref);
664cceb0
DH
699
700 if (!IS_ERR(skey_ref)) {
701 key_put(key);
702 key_ref = skey_ref;
703 }
704
1da177e4
LT
705 break;
706 }
707
5593122e
DH
708 /* unlink does not use the nominated key in any way, so can skip all
709 * the permission checks as it is only concerned with the keyring */
710 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
711 ret = 0;
712 goto error;
713 }
714
715 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
76181c13
DH
716 ret = wait_for_key_construction(key, true);
717 switch (ret) {
718 case -ERESTARTSYS:
719 goto invalid_key;
720 default:
721 if (perm)
722 goto invalid_key;
723 case 0:
724 break;
725 }
726 } else if (perm) {
1da177e4
LT
727 ret = key_validate(key);
728 if (ret < 0)
729 goto invalid_key;
730 }
731
732 ret = -EIO;
5593122e 733 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
363b02da 734 key_read_state(key) == KEY_IS_UNINSTANTIATED)
1da177e4
LT
735 goto invalid_key;
736
3e30148c 737 /* check the permissions */
4bdf0bc3 738 ret = key_task_permission(key_ref, ctx.cred, perm);
29db9190 739 if (ret < 0)
1da177e4
LT
740 goto invalid_key;
741
074d5898 742 key->last_used_at = ktime_get_real_seconds();
31d5a79d 743
664cceb0 744error:
4bdf0bc3 745 put_cred(ctx.cred);
664cceb0 746 return key_ref;
1da177e4 747
664cceb0
DH
748invalid_key:
749 key_ref_put(key_ref);
750 key_ref = ERR_PTR(ret);
1da177e4
LT
751 goto error;
752
bb952bb9
DH
753 /* if we attempted to install a keyring, then it may have caused new
754 * creds to be installed */
755reget_creds:
4bdf0bc3 756 put_cred(ctx.cred);
bb952bb9 757 goto try_again;
a8b17ed0 758}
bb952bb9 759
1da177e4 760/*
973c9f4f
DH
761 * Join the named keyring as the session keyring if possible else attempt to
762 * create a new one of that name and join that.
763 *
764 * If the name is NULL, an empty anonymous keyring will be installed as the
765 * session keyring.
766 *
767 * Named session keyrings are joined with a semaphore held to prevent the
768 * keyrings from going away whilst the attempt is made to going them and also
769 * to prevent a race in creating compatible session keyrings.
1da177e4
LT
770 */
771long join_session_keyring(const char *name)
772{
d84f4f99
DH
773 const struct cred *old;
774 struct cred *new;
1da177e4 775 struct key *keyring;
d84f4f99
DH
776 long ret, serial;
777
d84f4f99
DH
778 new = prepare_creds();
779 if (!new)
780 return -ENOMEM;
781 old = current_cred();
1da177e4
LT
782
783 /* if no name is provided, install an anonymous keyring */
784 if (!name) {
d84f4f99 785 ret = install_session_keyring_to_cred(new, NULL);
1da177e4
LT
786 if (ret < 0)
787 goto error;
788
3a50597d 789 serial = new->session_keyring->serial;
d84f4f99
DH
790 ret = commit_creds(new);
791 if (ret == 0)
792 ret = serial;
793 goto okay;
1da177e4
LT
794 }
795
796 /* allow the user to join or create a named keyring */
bb003079 797 mutex_lock(&key_session_mutex);
1da177e4
LT
798
799 /* look for an existing keyring of this name */
69664cf1 800 keyring = find_keyring_by_name(name, false);
1da177e4
LT
801 if (PTR_ERR(keyring) == -ENOKEY) {
802 /* not found - try and create a new one */
96b5c8fe
DH
803 keyring = keyring_alloc(
804 name, old->uid, old->gid, old,
805 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
5ac7eace 806 KEY_ALLOC_IN_QUOTA, NULL, NULL);
1da177e4
LT
807 if (IS_ERR(keyring)) {
808 ret = PTR_ERR(keyring);
bcf945d3 809 goto error2;
1da177e4 810 }
d84f4f99 811 } else if (IS_ERR(keyring)) {
1da177e4
LT
812 ret = PTR_ERR(keyring);
813 goto error2;
3a50597d
DH
814 } else if (keyring == new->session_keyring) {
815 ret = 0;
d636bd9f 816 goto error3;
1da177e4
LT
817 }
818
819 /* we've got a keyring - now to install it */
d84f4f99 820 ret = install_session_keyring_to_cred(new, keyring);
1da177e4 821 if (ret < 0)
d636bd9f 822 goto error3;
1da177e4 823
d84f4f99
DH
824 commit_creds(new);
825 mutex_unlock(&key_session_mutex);
826
1da177e4
LT
827 ret = keyring->serial;
828 key_put(keyring);
d84f4f99
DH
829okay:
830 return ret;
1da177e4 831
d636bd9f
EB
832error3:
833 key_put(keyring);
664cceb0 834error2:
bb003079 835 mutex_unlock(&key_session_mutex);
664cceb0 836error:
d84f4f99 837 abort_creds(new);
1da177e4 838 return ret;
d84f4f99 839}
ee18d64c
DH
840
841/*
973c9f4f
DH
842 * Replace a process's session keyring on behalf of one of its children when
843 * the target process is about to resume userspace execution.
ee18d64c 844 */
67d12145 845void key_change_session_keyring(struct callback_head *twork)
ee18d64c 846{
413cd3d9 847 const struct cred *old = current_cred();
67d12145 848 struct cred *new = container_of(twork, struct cred, rcu);
ee18d64c 849
413cd3d9
ON
850 if (unlikely(current->flags & PF_EXITING)) {
851 put_cred(new);
ee18d64c 852 return;
413cd3d9 853 }
ee18d64c 854
ee18d64c
DH
855 new-> uid = old-> uid;
856 new-> euid = old-> euid;
857 new-> suid = old-> suid;
858 new->fsuid = old->fsuid;
859 new-> gid = old-> gid;
860 new-> egid = old-> egid;
861 new-> sgid = old-> sgid;
862 new->fsgid = old->fsgid;
863 new->user = get_uid(old->user);
ba0e3427 864 new->user_ns = get_user_ns(old->user_ns);
ee18d64c
DH
865 new->group_info = get_group_info(old->group_info);
866
867 new->securebits = old->securebits;
868 new->cap_inheritable = old->cap_inheritable;
869 new->cap_permitted = old->cap_permitted;
870 new->cap_effective = old->cap_effective;
58319057 871 new->cap_ambient = old->cap_ambient;
ee18d64c
DH
872 new->cap_bset = old->cap_bset;
873
874 new->jit_keyring = old->jit_keyring;
875 new->thread_keyring = key_get(old->thread_keyring);
3a50597d 876 new->process_keyring = key_get(old->process_keyring);
ee18d64c
DH
877
878 security_transfer_creds(new, old);
879
880 commit_creds(new);
881}
c124bde2
MZ
882
883/*
884 * Make sure that root's user and user-session keyrings exist.
885 */
886static int __init init_root_keyring(void)
887{
888 return install_user_keyrings();
889}
890
891late_initcall(init_root_keyring);