blob: 0b9406bf60e571bd088fafc48ee7c8abb7809583 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells973c9f42011-01-20 16:38:33 +00002/* Manage a process's keyrings
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
David Howells69664cf2008-04-29 01:01:31 -07004 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Written by David Howells (dhowells@redhat.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/sched.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010010#include <linux/sched/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/keyctl.h>
12#include <linux/fs.h>
13#include <linux/err.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080014#include <linux/mutex.h>
David Howellsee18d642009-09-02 09:14:21 +010015#include <linux/security.h>
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060016#include <linux/user_namespace.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080017#include <linux/uaccess.h>
David Howells822ad642019-02-14 16:20:25 +000018#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "internal.h"
20
David Howells973c9f42011-01-20 16:38:33 +000021/* Session keyring create vs join semaphore */
Ingo Molnarbb0030792006-03-22 00:09:14 -080022static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
David Howells973c9f42011-01-20 16:38:33 +000024/* User keyring creation semaphore */
David Howells69664cf2008-04-29 01:01:31 -070025static DEFINE_MUTEX(key_user_keyring_mutex);
26
David Howells973c9f42011-01-20 16:38:33 +000027/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028struct key_user root_key_user = {
Elena Reshetovaddb99e12017-03-31 15:20:49 +030029 .usage = REFCOUNT_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070030 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080031 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .nkeys = ATOMIC_INIT(2),
33 .nikeys = ATOMIC_INIT(2),
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080034 .uid = GLOBAL_ROOT_UID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
David Howells973c9f42011-01-20 16:38:33 +000038 * Install the user and user session keyrings for the current process's UID.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
David Howells8bbf49762008-11-14 10:39:14 +110040int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
David Howellsd84f4f92008-11-14 10:39:23 +110042 struct user_struct *user;
43 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct key *uid_keyring, *session_keyring;
David Howells96b5c8f2012-10-02 19:24:56 +010045 key_perm_t user_keyring_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 char buf[20];
47 int ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080048 uid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Howells96b5c8f2012-10-02 19:24:56 +010050 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
David Howellsd84f4f92008-11-14 10:39:23 +110051 cred = current_cred();
52 user = cred->user;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080053 uid = from_kuid(cred->user_ns, user->uid);
David Howellsd84f4f92008-11-14 10:39:23 +110054
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080055 kenter("%p{%u}", user, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jann Horn0b9dc6c2019-03-27 16:55:08 +010057 if (READ_ONCE(user->uid_keyring) && READ_ONCE(user->session_keyring)) {
David Howells69664cf2008-04-29 01:01:31 -070058 kleave(" = 0 [exist]");
59 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
David Howells69664cf2008-04-29 01:01:31 -070062 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ret = 0;
64
David Howells69664cf2008-04-29 01:01:31 -070065 if (!user->uid_keyring) {
66 /* get the UID-specific keyring
67 * - there may be one in existence already as it may have been
68 * pinned by a session, but the user_struct pointing to it
69 * may have been destroyed by setuid */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080070 sprintf(buf, "_uid.%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Howells69664cf2008-04-29 01:01:31 -070072 uid_keyring = find_keyring_by_name(buf, true);
73 if (IS_ERR(uid_keyring)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080074 uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010075 cred, user_keyring_perm,
Eric Biggers237bbd22017-09-18 11:37:03 -070076 KEY_ALLOC_UID_KEYRING |
77 KEY_ALLOC_IN_QUOTA,
David Howells5ac7eac2016-04-06 16:14:24 +010078 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070079 if (IS_ERR(uid_keyring)) {
80 ret = PTR_ERR(uid_keyring);
81 goto error;
82 }
83 }
84
85 /* get a default session keyring (which might also exist
86 * already) */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080087 sprintf(buf, "_uid_ses.%u", uid);
David Howells69664cf2008-04-29 01:01:31 -070088
89 session_keyring = find_keyring_by_name(buf, true);
90 if (IS_ERR(session_keyring)) {
91 session_keyring =
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080092 keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010093 cred, user_keyring_perm,
Eric Biggers237bbd22017-09-18 11:37:03 -070094 KEY_ALLOC_UID_KEYRING |
95 KEY_ALLOC_IN_QUOTA,
David Howells5ac7eac2016-04-06 16:14:24 +010096 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070097 if (IS_ERR(session_keyring)) {
98 ret = PTR_ERR(session_keyring);
99 goto error_release;
100 }
101
102 /* we install a link from the user session keyring to
103 * the user keyring */
104 ret = key_link(session_keyring, uid_keyring);
105 if (ret < 0)
106 goto error_release_both;
107 }
108
109 /* install the keyrings */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100110 /* paired with READ_ONCE() */
111 smp_store_release(&user->uid_keyring, uid_keyring);
112 /* paired with READ_ONCE() */
113 smp_store_release(&user->session_keyring, session_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700114 }
115
116 mutex_unlock(&key_user_keyring_mutex);
117 kleave(" = 0");
118 return 0;
119
120error_release_both:
121 key_put(session_keyring);
122error_release:
123 key_put(uid_keyring);
124error:
125 mutex_unlock(&key_user_keyring_mutex);
126 kleave(" = %d", ret);
127 return ret;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100131 * Install a thread keyring to the given credentials struct if it didn't have
132 * one already. This is allowed to overrun the quota.
133 *
134 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
David Howellsd84f4f92008-11-14 10:39:23 +1100136int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
David Howellsd84f4f92008-11-14 10:39:23 +1100138 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Eric Biggersc9f838d2017-04-18 15:31:09 +0100140 if (new->thread_keyring)
141 return 0;
142
David Howellsd84f4f92008-11-14 10:39:23 +1100143 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100144 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100145 KEY_ALLOC_QUOTA_OVERRUN,
146 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100147 if (IS_ERR(keyring))
148 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David Howellsd84f4f92008-11-14 10:39:23 +1100150 new->thread_keyring = keyring;
151 return 0;
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100155 * Install a thread keyring to the current task if it didn't have one already.
156 *
157 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
David Howellsd84f4f92008-11-14 10:39:23 +1100159static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
David Howellsd84f4f92008-11-14 10:39:23 +1100161 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int ret;
163
David Howellsd84f4f92008-11-14 10:39:23 +1100164 new = prepare_creds();
165 if (!new)
166 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howellsd84f4f92008-11-14 10:39:23 +1100168 ret = install_thread_keyring_to_cred(new);
169 if (ret < 0) {
170 abort_creds(new);
171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
David Howellsd84f4f92008-11-14 10:39:23 +1100174 return commit_creds(new);
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Howellsd84f4f92008-11-14 10:39:23 +1100177/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100178 * Install a process keyring to the given credentials struct if it didn't have
179 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000180 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100181 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100182 */
183int install_process_keyring_to_cred(struct cred *new)
184{
185 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howells3a505972012-10-02 19:24:29 +0100187 if (new->process_keyring)
Eric Biggersc9f838d2017-04-18 15:31:09 +0100188 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100189
David Howells96b5c8f2012-10-02 19:24:56 +0100190 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
191 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100192 KEY_ALLOC_QUOTA_OVERRUN,
193 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100194 if (IS_ERR(keyring))
195 return PTR_ERR(keyring);
196
David Howells3a505972012-10-02 19:24:29 +0100197 new->process_keyring = keyring;
198 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100202 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000203 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100204 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
David Howellsd84f4f92008-11-14 10:39:23 +1100206static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Howellsd84f4f92008-11-14 10:39:23 +1100208 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int ret;
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211 new = prepare_creds();
212 if (!new)
213 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700214
David Howellsd84f4f92008-11-14 10:39:23 +1100215 ret = install_process_keyring_to_cred(new);
216 if (ret < 0) {
217 abort_creds(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +0100218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 return commit_creds(new);
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100225 * Install the given keyring as the session keyring of the given credentials
226 * struct, replacing the existing one if any. If the given keyring is NULL,
227 * then install a new anonymous session keyring.
Jann Horn5c7e3722019-03-27 16:39:38 +0100228 * @cred can not be in use by any task yet.
Eric Biggersc9f838d2017-04-18 15:31:09 +0100229 *
230 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700232int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David Howells7e047ef2006-06-26 00:24:50 -0700234 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700236
237 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* create an empty session keyring */
240 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700241 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100242 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700243 flags = KEY_ALLOC_IN_QUOTA;
244
David Howells96b5c8f2012-10-02 19:24:56 +0100245 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
246 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100247 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700248 if (IS_ERR(keyring))
249 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100250 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100251 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100255 old = cred->session_keyring;
Jann Horn5c7e3722019-03-27 16:39:38 +0100256 cred->session_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Howells3a505972012-10-02 19:24:29 +0100258 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700259 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
David Howells1a26feb2006-04-10 22:54:26 -0700261 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100265 * Install the given keyring as the session keyring of the current task,
266 * replacing the existing one if any. If the given keyring is NULL, then
267 * install a new anonymous session keyring.
268 *
269 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
David Howellsd84f4f92008-11-14 10:39:23 +1100271static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
David Howellsd84f4f92008-11-14 10:39:23 +1100273 struct cred *new;
274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Howellsd84f4f92008-11-14 10:39:23 +1100276 new = prepare_creds();
277 if (!new)
278 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800279
David Howells995995372011-08-22 14:08:33 +0100280 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100281 if (ret < 0) {
282 abort_creds(new);
283 return ret;
284 }
David Howellsb5f545c2006-01-08 01:02:47 -0800285
David Howellsd84f4f92008-11-14 10:39:23 +1100286 return commit_creds(new);
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
David Howells973c9f42011-01-20 16:38:33 +0000290 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292void key_fsuid_changed(struct task_struct *tsk)
293{
294 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100295 BUG_ON(!tsk->cred);
296 if (tsk->cred->thread_keyring) {
297 down_write(&tsk->cred->thread_keyring->sem);
298 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
299 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
David Howells973c9f42011-01-20 16:38:33 +0000304 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306void key_fsgid_changed(struct task_struct *tsk)
307{
308 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100309 BUG_ON(!tsk->cred);
310 if (tsk->cred->thread_keyring) {
311 down_write(&tsk->cred->thread_keyring->sem);
312 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
313 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
David Howells973c9f42011-01-20 16:38:33 +0000318 * Search the process keyrings attached to the supplied cred for the first
319 * matching key.
320 *
321 * The search criteria are the type and the match function. The description is
322 * given to the match function as a parameter, but doesn't otherwise influence
323 * the search. Typically the match function will compare the description
324 * parameter to the key's description.
325 *
326 * This can only search keyrings that grant Search permission to the supplied
327 * credentials. Keyrings linked to searched keyrings will also be searched if
328 * they grant Search permission too. Keys can only be found if they grant
329 * Search permission to the credentials.
330 *
331 * Returns a pointer to the key with the key usage count incremented if
332 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
333 * matched negative keys.
334 *
335 * In the case of a successful return, the possession attribute is set on the
336 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100338key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
David Howellsb5f545c2006-01-08 01:02:47 -0800340 key_ref_t key_ref, ret, err;
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100341 const struct cred *cred = ctx->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
344 * searchable, but we failed to find a key or we found a negative key;
345 * otherwise we want to return a sample error (probably -EACCES) if
346 * none of the keyrings were searchable
347 *
348 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
349 */
David Howells664cceb2005-09-28 17:03:15 +0100350 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 ret = NULL;
352 err = ERR_PTR(-EAGAIN);
353
354 /* search the thread keyring first */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100355 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100356 key_ref = keyring_search_aux(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100357 make_key_ref(cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100358 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto found;
360
David Howells664cceb2005-09-28 17:03:15 +0100361 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100364 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 break;
366 default:
David Howells664cceb2005-09-28 17:03:15 +0100367 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369 }
370 }
371
372 /* search the process keyring second */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100373 if (cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100374 key_ref = keyring_search_aux(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100375 make_key_ref(cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100376 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto found;
378
David Howells664cceb2005-09-28 17:03:15 +0100379 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000381 if (ret)
382 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100383 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100385 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 default:
David Howells664cceb2005-09-28 17:03:15 +0100388 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390 }
391 }
392
David Howells3e301482005-06-23 22:00:56 -0700393 /* search the session keyring */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100394 if (cred->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100395 key_ref = keyring_search_aux(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100396 make_key_ref(cred->session_keyring, 1), ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Howells664cceb2005-09-28 17:03:15 +0100398 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700399 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howells664cceb2005-09-28 17:03:15 +0100401 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700402 case -EAGAIN: /* no key */
403 if (ret)
404 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100405 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700406 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100407 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
David Howells3e301482005-06-23 22:00:56 -0700409 default:
David Howells664cceb2005-09-28 17:03:15 +0100410 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700411 break;
412 }
David Howells3e301482005-06-23 22:00:56 -0700413 }
414 /* or search the user-session keyring */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100415 else if (READ_ONCE(cred->user->session_keyring)) {
David Howells664cceb2005-09-28 17:03:15 +0100416 key_ref = keyring_search_aux(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100417 make_key_ref(READ_ONCE(cred->user->session_keyring), 1),
David Howells4bdf0bc2013-09-24 10:35:15 +0100418 ctx);
David Howells664cceb2005-09-28 17:03:15 +0100419 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700420 goto found;
421
David Howells664cceb2005-09-28 17:03:15 +0100422 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700423 case -EAGAIN: /* no key */
424 if (ret)
425 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100426 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700427 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100428 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700429 break;
430 default:
David Howells664cceb2005-09-28 17:03:15 +0100431 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700432 break;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435
David Howells927942a2010-06-11 17:31:10 +0100436 /* no key - decide on the error we're going to go for */
437 key_ref = ret ? ret : err;
438
439found:
440 return key_ref;
441}
442
David Howells927942a2010-06-11 17:31:10 +0100443/*
David Howells973c9f42011-01-20 16:38:33 +0000444 * Search the process keyrings attached to the supplied cred for the first
445 * matching key in the manner of search_my_process_keyrings(), but also search
446 * the keys attached to the assumed authorisation key using its credentials if
447 * one is available.
448 *
449 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100450 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100451key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100452{
453 struct request_key_auth *rka;
454 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
455
456 might_sleep();
457
David Howells4bdf0bc2013-09-24 10:35:15 +0100458 key_ref = search_my_process_keyrings(ctx);
David Howells927942a2010-06-11 17:31:10 +0100459 if (!IS_ERR(key_ref))
460 goto found;
461 err = key_ref;
462
David Howellsb5f545c2006-01-08 01:02:47 -0800463 /* if this process has an instantiation authorisation key, then we also
464 * search the keyrings of the process mentioned there
465 * - we don't permit access to request_key auth keys via this method
466 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100467 if (ctx->cred->request_key_auth &&
468 ctx->cred == current_cred() &&
469 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800470 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100471 const struct cred *cred = ctx->cred;
472
David Howells04c567d2006-06-22 14:47:18 -0700473 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100474 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700475
David Howells4bdf0bc2013-09-24 10:35:15 +0100476 if (key_validate(ctx->cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100477 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800478
David Howells4bdf0bc2013-09-24 10:35:15 +0100479 ctx->cred = rka->cred;
480 key_ref = search_process_keyrings(ctx);
481 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800482
David Howellsc69e8d92008-11-14 10:39:19 +1100483 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700484
485 if (!IS_ERR(key_ref))
486 goto found;
487
David Howells927942a2010-06-11 17:31:10 +0100488 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700489 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100490 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800491 }
492 }
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100495 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
496 key_ref = ERR_PTR(-ENOKEY);
497 else if (err == ERR_PTR(-EACCES))
498 key_ref = ret;
499 else
500 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
David Howells3e301482005-06-23 22:00:56 -0700502found:
David Howells664cceb2005-09-28 17:03:15 +0100503 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000504}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/*
David Howells973c9f42011-01-20 16:38:33 +0000507 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100508 */
David Howells0c903ab2014-09-16 17:36:08 +0100509bool lookup_user_key_possessed(const struct key *key,
510 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100511{
David Howells46291952014-09-16 17:36:02 +0100512 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000513}
David Howells664cceb2005-09-28 17:03:15 +0100514
David Howells664cceb2005-09-28 17:03:15 +0100515/*
David Howells973c9f42011-01-20 16:38:33 +0000516 * Look up a key ID given us by userspace with a given permissions mask to get
517 * the key it refers to.
518 *
519 * Flags can be passed to request that special keyrings be created if referred
520 * to directly, to permit partially constructed keys to be found and to skip
521 * validity and permission checks on the found key.
522 *
523 * Returns a pointer to the key with an incremented usage count if successful;
524 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
525 * to a key or the best found key was a negative key; -EKEYREVOKED or
526 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
527 * found key doesn't grant the requested permit or the LSM denied access to it;
528 * or -ENOMEM if a special keyring couldn't be created.
529 *
530 * In the case of a successful return, the possession attribute is set on the
531 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
David Howells55931222009-09-02 09:13:45 +0100533key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100534 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
David Howells4bdf0bc2013-09-24 10:35:15 +0100536 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100537 .match_data.cmp = lookup_user_key_possessed,
538 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
539 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100540 };
David Howells8bbf49762008-11-14 10:39:14 +1100541 struct request_key_auth *rka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100543 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 int ret;
545
David Howellsbb952bb2008-11-14 10:39:20 +1100546try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100547 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100548 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 switch (id) {
551 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100552 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100553 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto error;
555
David Howells8bbf49762008-11-14 10:39:14 +1100556 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100558 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto error;
560 }
David Howellsbb952bb2008-11-14 10:39:20 +1100561 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563
David Howells4bdf0bc2013-09-24 10:35:15 +0100564 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100565 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100566 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 break;
568
569 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100570 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100571 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 goto error;
573
David Howells8bbf49762008-11-14 10:39:14 +1100574 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100576 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto error;
578 }
David Howellsbb952bb2008-11-14 10:39:20 +1100579 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
David Howells4bdf0bc2013-09-24 10:35:15 +0100582 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100583 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100584 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586
587 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100588 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* always install a session keyring upon access if one
590 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100591 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700592 if (ret < 0)
593 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100594 if (lflags & KEY_LOOKUP_CREATE)
595 ret = join_session_keyring(NULL);
596 else
597 ret = install_session_keyring(
David Howells4bdf0bc2013-09-24 10:35:15 +0100598 ctx.cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (ret < 0)
601 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100602 goto reget_creds;
David Howells4bdf0bc2013-09-24 10:35:15 +0100603 } else if (ctx.cred->session_keyring ==
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100604 READ_ONCE(ctx.cred->user->session_keyring) &&
David Howells3ecf1b42011-08-22 14:08:43 +0100605 lflags & KEY_LOOKUP_CREATE) {
606 ret = join_session_keyring(NULL);
607 if (ret < 0)
608 goto error;
609 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Jann Horn5c7e3722019-03-27 16:39:38 +0100612 key = ctx.cred->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100613 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100614 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
616
617 case KEY_SPEC_USER_KEYRING:
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100618 if (!READ_ONCE(ctx.cred->user->uid_keyring)) {
David Howells8bbf49762008-11-14 10:39:14 +1100619 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700620 if (ret < 0)
621 goto error;
622 }
623
David Howells4bdf0bc2013-09-24 10:35:15 +0100624 key = ctx.cred->user->uid_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100625 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100626 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628
629 case KEY_SPEC_USER_SESSION_KEYRING:
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100630 if (!READ_ONCE(ctx.cred->user->session_keyring)) {
David Howells8bbf49762008-11-14 10:39:14 +1100631 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700632 if (ret < 0)
633 goto error;
634 }
635
David Howells4bdf0bc2013-09-24 10:35:15 +0100636 key = ctx.cred->user->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100637 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100638 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640
641 case KEY_SPEC_GROUP_KEYRING:
642 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100643 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto error;
645
David Howellsb5f545c2006-01-08 01:02:47 -0800646 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100647 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800648 if (!key)
649 goto error;
650
David Howellsccc3e6d2013-09-24 10:35:16 +0100651 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800652 key_ref = make_key_ref(key, 1);
653 break;
654
David Howells8bbf49762008-11-14 10:39:14 +1100655 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100656 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100657 goto error;
658
David Howells4bdf0bc2013-09-24 10:35:15 +0100659 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000660 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100661 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100662 key_ref = ERR_PTR(-EKEYREVOKED);
663 key = NULL;
664 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100665 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100666 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100667 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100668 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100669 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100670 if (!key)
671 goto error;
672 key_ref = make_key_ref(key, 1);
673 break;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 default:
David Howells664cceb2005-09-28 17:03:15 +0100676 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (id < 1)
678 goto error;
679
680 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100681 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800682 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100684 }
685
686 key_ref = make_key_ref(key, 0);
687
688 /* check to see if we possess the key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100689 ctx.index_key.type = key->type;
690 ctx.index_key.description = key->description;
691 ctx.index_key.desc_len = strlen(key->description);
David Howells46291952014-09-16 17:36:02 +0100692 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100693 kdebug("check possessed");
694 skey_ref = search_process_keyrings(&ctx);
695 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100696
697 if (!IS_ERR(skey_ref)) {
698 key_put(key);
699 key_ref = skey_ref;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 }
704
David Howells55931222009-09-02 09:13:45 +0100705 /* unlink does not use the nominated key in any way, so can skip all
706 * the permission checks as it is only concerned with the keyring */
707 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
708 ret = 0;
709 goto error;
710 }
711
712 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700713 ret = wait_for_key_construction(key, true);
714 switch (ret) {
715 case -ERESTARTSYS:
716 goto invalid_key;
717 default:
718 if (perm)
719 goto invalid_key;
720 case 0:
721 break;
722 }
723 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ret = key_validate(key);
725 if (ret < 0)
726 goto invalid_key;
727 }
728
729 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100730 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
David Howells363b02d2017-10-04 16:43:25 +0100731 key_read_state(key) == KEY_IS_UNINSTANTIATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto invalid_key;
733
David Howells3e301482005-06-23 22:00:56 -0700734 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100735 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800736 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto invalid_key;
738
Baolin Wang074d5892017-11-15 16:38:45 +0000739 key->last_used_at = ktime_get_real_seconds();
David Howells31d5a792012-05-11 10:56:56 +0100740
David Howells664cceb2005-09-28 17:03:15 +0100741error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100742 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100743 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David Howells664cceb2005-09-28 17:03:15 +0100745invalid_key:
746 key_ref_put(key_ref);
747 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto error;
749
David Howellsbb952bb2008-11-14 10:39:20 +1100750 /* if we attempted to install a keyring, then it may have caused new
751 * creds to be installed */
752reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100753 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100754 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000755}
Dave Jiang76ef5e12018-12-04 10:31:27 -0800756EXPORT_SYMBOL(lookup_user_key);
David Howellsbb952bb2008-11-14 10:39:20 +1100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
David Howells973c9f42011-01-20 16:38:33 +0000759 * Join the named keyring as the session keyring if possible else attempt to
760 * create a new one of that name and join that.
761 *
762 * If the name is NULL, an empty anonymous keyring will be installed as the
763 * session keyring.
764 *
765 * Named session keyrings are joined with a semaphore held to prevent the
766 * keyrings from going away whilst the attempt is made to going them and also
767 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769long join_session_keyring(const char *name)
770{
David Howellsd84f4f92008-11-14 10:39:23 +1100771 const struct cred *old;
772 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100774 long ret, serial;
775
David Howellsd84f4f92008-11-14 10:39:23 +1100776 new = prepare_creds();
777 if (!new)
778 return -ENOMEM;
779 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* if no name is provided, install an anonymous keyring */
782 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100783 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (ret < 0)
785 goto error;
786
David Howells3a505972012-10-02 19:24:29 +0100787 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100788 ret = commit_creds(new);
789 if (ret == 0)
790 ret = serial;
791 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800795 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700798 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (PTR_ERR(keyring) == -ENOKEY) {
800 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100801 keyring = keyring_alloc(
802 name, old->uid, old->gid, old,
803 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100804 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (IS_ERR(keyring)) {
806 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700807 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
David Howellsd84f4f92008-11-14 10:39:23 +1100809 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ret = PTR_ERR(keyring);
811 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100812 } else if (keyring == new->session_keyring) {
813 ret = 0;
Eric Biggersd636bd92017-06-08 14:48:03 +0100814 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816
817 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100818 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (ret < 0)
Eric Biggersd636bd92017-06-08 14:48:03 +0100820 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
David Howellsd84f4f92008-11-14 10:39:23 +1100822 commit_creds(new);
823 mutex_unlock(&key_session_mutex);
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 ret = keyring->serial;
826 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100827okay:
828 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Eric Biggersd636bd92017-06-08 14:48:03 +0100830error3:
831 key_put(keyring);
David Howells664cceb2005-09-28 17:03:15 +0100832error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800833 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100834error:
David Howellsd84f4f92008-11-14 10:39:23 +1100835 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100837}
David Howellsee18d642009-09-02 09:14:21 +0100838
839/*
David Howells973c9f42011-01-20 16:38:33 +0000840 * Replace a process's session keyring on behalf of one of its children when
841 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100842 */
Al Viro67d12142012-06-27 11:07:19 +0400843void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100844{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000845 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400846 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100847
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000848 if (unlikely(current->flags & PF_EXITING)) {
849 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100850 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000851 }
David Howellsee18d642009-09-02 09:14:21 +0100852
David Howellsee18d642009-09-02 09:14:21 +0100853 new-> uid = old-> uid;
854 new-> euid = old-> euid;
855 new-> suid = old-> suid;
856 new->fsuid = old->fsuid;
857 new-> gid = old-> gid;
858 new-> egid = old-> egid;
859 new-> sgid = old-> sgid;
860 new->fsgid = old->fsgid;
861 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800862 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100863 new->group_info = get_group_info(old->group_info);
864
865 new->securebits = old->securebits;
866 new->cap_inheritable = old->cap_inheritable;
867 new->cap_permitted = old->cap_permitted;
868 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700869 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100870 new->cap_bset = old->cap_bset;
871
872 new->jit_keyring = old->jit_keyring;
873 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100874 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100875
876 security_transfer_creds(new, old);
877
878 commit_creds(new);
879}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100880
881/*
882 * Make sure that root's user and user-session keyrings exist.
883 */
884static int __init init_root_keyring(void)
885{
886 return install_user_keyrings();
887}
888
889late_initcall(init_root_keyring);