blob: 7e0232db1707e5bed552094d03228d60a5e8658d [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 Howells0f44e4d2019-06-26 21:02:32 +010018#include <linux/init_task.h>
David Howells822ad642019-02-14 16:20:25 +000019#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howells973c9f42011-01-20 16:38:33 +000022/* Session keyring create vs join semaphore */
Ingo Molnarbb0030792006-03-22 00:09:14 -080023static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells973c9f42011-01-20 16:38:33 +000025/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026struct key_user root_key_user = {
Elena Reshetovaddb99e12017-03-31 15:20:49 +030027 .usage = REFCOUNT_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070028 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080029 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .nkeys = ATOMIC_INIT(2),
31 .nikeys = ATOMIC_INIT(2),
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080032 .uid = GLOBAL_ROOT_UID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070033};
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
David Howells0f44e4d2019-06-26 21:02:32 +010036 * Get or create a user register keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
David Howells0f44e4d2019-06-26 21:02:32 +010038static struct key *get_user_register(struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
David Howells0f44e4d2019-06-26 21:02:32 +010040 struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register);
41
42 if (reg_keyring)
43 return reg_keyring;
44
45 down_write(&user_ns->keyring_sem);
46
47 /* Make sure there's a register keyring. It gets owned by the
48 * user_namespace's owner.
49 */
50 reg_keyring = user_ns->user_keyring_register;
51 if (!reg_keyring) {
52 reg_keyring = keyring_alloc(".user_reg",
53 user_ns->owner, INVALID_GID,
Linus Torvalds028db3e2019-07-10 18:43:43 -070054 &init_cred,
55 KEY_POS_WRITE | KEY_POS_SEARCH |
56 KEY_USR_VIEW | KEY_USR_READ,
57 0,
58 NULL, NULL);
David Howells0f44e4d2019-06-26 21:02:32 +010059 if (!IS_ERR(reg_keyring))
60 smp_store_release(&user_ns->user_keyring_register,
61 reg_keyring);
62 }
63
64 up_write(&user_ns->keyring_sem);
65
66 /* We don't return a ref since the keyring is pinned by the user_ns */
67 return reg_keyring;
68}
69
70/*
71 * Look up the user and user session keyrings for the current process's UID,
72 * creating them if they don't exist.
73 */
74int look_up_user_keyrings(struct key **_user_keyring,
75 struct key **_user_session_keyring)
76{
77 const struct cred *cred = current_cred();
78 struct user_namespace *user_ns = current_user_ns();
79 struct key *reg_keyring, *uid_keyring, *session_keyring;
Linus Torvalds028db3e2019-07-10 18:43:43 -070080 key_perm_t user_keyring_perm;
David Howells0f44e4d2019-06-26 21:02:32 +010081 key_ref_t uid_keyring_r, session_keyring_r;
82 uid_t uid = from_kuid(user_ns, cred->user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 char buf[20];
84 int ret;
85
Linus Torvalds028db3e2019-07-10 18:43:43 -070086 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
87
David Howells0f44e4d2019-06-26 21:02:32 +010088 kenter("%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
David Howells0f44e4d2019-06-26 21:02:32 +010090 reg_keyring = get_user_register(user_ns);
91 if (IS_ERR(reg_keyring))
92 return PTR_ERR(reg_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David Howells0f44e4d2019-06-26 21:02:32 +010094 down_write(&user_ns->keyring_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 ret = 0;
96
David Howells0f44e4d2019-06-26 21:02:32 +010097 /* Get the user keyring. Note that there may be one in existence
98 * already as it may have been pinned by a session, but the user_struct
99 * pointing to it may have been destroyed by setuid.
100 */
101 snprintf(buf, sizeof(buf), "_uid.%u", uid);
102 uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
103 &key_type_keyring, buf, false);
104 kdebug("_uid %p", uid_keyring_r);
105 if (uid_keyring_r == ERR_PTR(-EAGAIN)) {
106 uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700107 cred, user_keyring_perm,
David Howells0f44e4d2019-06-26 21:02:32 +0100108 KEY_ALLOC_UID_KEYRING |
109 KEY_ALLOC_IN_QUOTA,
110 NULL, reg_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700111 if (IS_ERR(uid_keyring)) {
David Howells0f44e4d2019-06-26 21:02:32 +0100112 ret = PTR_ERR(uid_keyring);
113 goto error;
David Howells69664cf2008-04-29 01:01:31 -0700114 }
David Howells0f44e4d2019-06-26 21:02:32 +0100115 } else if (IS_ERR(uid_keyring_r)) {
116 ret = PTR_ERR(uid_keyring_r);
117 goto error;
118 } else {
119 uid_keyring = key_ref_to_ptr(uid_keyring_r);
David Howells69664cf2008-04-29 01:01:31 -0700120 }
121
David Howells0f44e4d2019-06-26 21:02:32 +0100122 /* Get a default session keyring (which might also exist already) */
123 snprintf(buf, sizeof(buf), "_uid_ses.%u", uid);
124 session_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
125 &key_type_keyring, buf, false);
126 kdebug("_uid_ses %p", session_keyring_r);
127 if (session_keyring_r == ERR_PTR(-EAGAIN)) {
128 session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700129 cred, user_keyring_perm,
David Howells0f44e4d2019-06-26 21:02:32 +0100130 KEY_ALLOC_UID_KEYRING |
131 KEY_ALLOC_IN_QUOTA,
132 NULL, NULL);
133 if (IS_ERR(session_keyring)) {
134 ret = PTR_ERR(session_keyring);
135 goto error_release;
136 }
137
138 /* We install a link from the user session keyring to
139 * the user keyring.
140 */
141 ret = key_link(session_keyring, uid_keyring);
142 if (ret < 0)
143 goto error_release_session;
144
145 /* And only then link the user-session keyring to the
146 * register.
147 */
148 ret = key_link(reg_keyring, session_keyring);
149 if (ret < 0)
150 goto error_release_session;
151 } else if (IS_ERR(session_keyring_r)) {
152 ret = PTR_ERR(session_keyring_r);
153 goto error_release;
154 } else {
155 session_keyring = key_ref_to_ptr(session_keyring_r);
156 }
157
158 up_write(&user_ns->keyring_sem);
159
160 if (_user_session_keyring)
161 *_user_session_keyring = session_keyring;
162 else
163 key_put(session_keyring);
164 if (_user_keyring)
165 *_user_keyring = uid_keyring;
166 else
167 key_put(uid_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700168 kleave(" = 0");
169 return 0;
170
David Howells0f44e4d2019-06-26 21:02:32 +0100171error_release_session:
David Howells69664cf2008-04-29 01:01:31 -0700172 key_put(session_keyring);
173error_release:
174 key_put(uid_keyring);
175error:
David Howells0f44e4d2019-06-26 21:02:32 +0100176 up_write(&user_ns->keyring_sem);
David Howells69664cf2008-04-29 01:01:31 -0700177 kleave(" = %d", ret);
178 return ret;
179}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
David Howells0f44e4d2019-06-26 21:02:32 +0100182 * Get the user session keyring if it exists, but don't create it if it
183 * doesn't.
184 */
185struct key *get_user_session_keyring_rcu(const struct cred *cred)
186{
187 struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register);
188 key_ref_t session_keyring_r;
189 char buf[20];
190
191 struct keyring_search_context ctx = {
192 .index_key.type = &key_type_keyring,
193 .index_key.description = buf,
194 .cred = cred,
195 .match_data.cmp = key_default_cmp,
196 .match_data.raw_data = buf,
197 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
198 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
199 };
200
201 if (!reg_keyring)
202 return NULL;
203
204 ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u",
205 from_kuid(cred->user_ns,
206 cred->user->uid));
207
208 session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true),
209 &ctx);
210 if (IS_ERR(session_keyring_r))
211 return NULL;
212 return key_ref_to_ptr(session_keyring_r);
213}
214
215/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100216 * Install a thread keyring to the given credentials struct if it didn't have
217 * one already. This is allowed to overrun the quota.
218 *
219 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
David Howellsd84f4f92008-11-14 10:39:23 +1100221int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
David Howellsd84f4f92008-11-14 10:39:23 +1100223 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Eric Biggersc9f838d2017-04-18 15:31:09 +0100225 if (new->thread_keyring)
226 return 0;
227
David Howellsd84f4f92008-11-14 10:39:23 +1100228 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700229 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100230 KEY_ALLOC_QUOTA_OVERRUN,
231 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100232 if (IS_ERR(keyring))
233 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
David Howellsd84f4f92008-11-14 10:39:23 +1100235 new->thread_keyring = keyring;
236 return 0;
237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100240 * Install a thread keyring to the current task if it didn't have one already.
241 *
242 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
David Howellsd84f4f92008-11-14 10:39:23 +1100244static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
David Howellsd84f4f92008-11-14 10:39:23 +1100246 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int ret;
248
David Howellsd84f4f92008-11-14 10:39:23 +1100249 new = prepare_creds();
250 if (!new)
251 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Howellsd84f4f92008-11-14 10:39:23 +1100253 ret = install_thread_keyring_to_cred(new);
254 if (ret < 0) {
255 abort_creds(new);
256 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
David Howellsd84f4f92008-11-14 10:39:23 +1100259 return commit_creds(new);
260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David Howellsd84f4f92008-11-14 10:39:23 +1100262/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100263 * Install a process keyring to the given credentials struct if it didn't have
264 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000265 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100266 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100267 */
268int install_process_keyring_to_cred(struct cred *new)
269{
270 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
David Howells3a505972012-10-02 19:24:29 +0100272 if (new->process_keyring)
Eric Biggersc9f838d2017-04-18 15:31:09 +0100273 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100274
David Howells96b5c8f2012-10-02 19:24:56 +0100275 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700276 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100277 KEY_ALLOC_QUOTA_OVERRUN,
278 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100279 if (IS_ERR(keyring))
280 return PTR_ERR(keyring);
281
David Howells3a505972012-10-02 19:24:29 +0100282 new->process_keyring = keyring;
283 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100287 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000288 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100289 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
David Howellsd84f4f92008-11-14 10:39:23 +1100291static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
David Howellsd84f4f92008-11-14 10:39:23 +1100293 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int ret;
295
David Howellsd84f4f92008-11-14 10:39:23 +1100296 new = prepare_creds();
297 if (!new)
298 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700299
David Howellsd84f4f92008-11-14 10:39:23 +1100300 ret = install_process_keyring_to_cred(new);
301 if (ret < 0) {
302 abort_creds(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +0100303 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
David Howellsd84f4f92008-11-14 10:39:23 +1100306 return commit_creds(new);
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100310 * Install the given keyring as the session keyring of the given credentials
311 * struct, replacing the existing one if any. If the given keyring is NULL,
312 * then install a new anonymous session keyring.
Jann Horn5c7e3722019-03-27 16:39:38 +0100313 * @cred can not be in use by any task yet.
Eric Biggersc9f838d2017-04-18 15:31:09 +0100314 *
315 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700317int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
David Howells7e047ef2006-06-26 00:24:50 -0700319 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700321
322 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* create an empty session keyring */
325 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700326 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100327 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700328 flags = KEY_ALLOC_IN_QUOTA;
329
David Howells96b5c8f2012-10-02 19:24:56 +0100330 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700331 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
332 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700333 if (IS_ERR(keyring))
334 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100335 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100336 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
339 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100340 old = cred->session_keyring;
Jann Horn5c7e3722019-03-27 16:39:38 +0100341 cred->session_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
David Howells3a505972012-10-02 19:24:29 +0100343 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700344 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David Howells1a26feb2006-04-10 22:54:26 -0700346 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100350 * Install the given keyring as the session keyring of the current task,
351 * replacing the existing one if any. If the given keyring is NULL, then
352 * install a new anonymous session keyring.
353 *
354 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
David Howellsd84f4f92008-11-14 10:39:23 +1100356static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
David Howellsd84f4f92008-11-14 10:39:23 +1100358 struct cred *new;
359 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
David Howellsd84f4f92008-11-14 10:39:23 +1100361 new = prepare_creds();
362 if (!new)
363 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800364
David Howells995995372011-08-22 14:08:33 +0100365 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100366 if (ret < 0) {
367 abort_creds(new);
368 return ret;
369 }
David Howellsb5f545c2006-01-08 01:02:47 -0800370
David Howellsd84f4f92008-11-14 10:39:23 +1100371 return commit_creds(new);
372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
David Howells973c9f42011-01-20 16:38:33 +0000375 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
David Howells2e218652019-05-22 14:06:51 +0100377void key_fsuid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
379 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100380 if (new_cred->thread_keyring) {
381 down_write(&new_cred->thread_keyring->sem);
382 new_cred->thread_keyring->uid = new_cred->fsuid;
383 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
David Howells973c9f42011-01-20 16:38:33 +0000388 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
David Howells2e218652019-05-22 14:06:51 +0100390void key_fsgid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100393 if (new_cred->thread_keyring) {
394 down_write(&new_cred->thread_keyring->sem);
395 new_cred->thread_keyring->gid = new_cred->fsgid;
396 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*
David Howells973c9f42011-01-20 16:38:33 +0000401 * Search the process keyrings attached to the supplied cred for the first
David Howellse59428f2019-06-19 16:10:15 +0100402 * matching key under RCU conditions (the caller must be holding the RCU read
403 * lock).
David Howells973c9f42011-01-20 16:38:33 +0000404 *
405 * The search criteria are the type and the match function. The description is
406 * given to the match function as a parameter, but doesn't otherwise influence
407 * the search. Typically the match function will compare the description
408 * parameter to the key's description.
409 *
410 * This can only search keyrings that grant Search permission to the supplied
411 * credentials. Keyrings linked to searched keyrings will also be searched if
412 * they grant Search permission too. Keys can only be found if they grant
413 * Search permission to the credentials.
414 *
415 * Returns a pointer to the key with the key usage count incremented if
416 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
417 * matched negative keys.
418 *
419 * In the case of a successful return, the possession attribute is set on the
420 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 */
David Howellse59428f2019-06-19 16:10:15 +0100422key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
David Howells0f44e4d2019-06-26 21:02:32 +0100424 struct key *user_session;
David Howellsb5f545c2006-01-08 01:02:47 -0800425 key_ref_t key_ref, ret, err;
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100426 const struct cred *cred = ctx->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
429 * searchable, but we failed to find a key or we found a negative key;
430 * otherwise we want to return a sample error (probably -EACCES) if
431 * none of the keyrings were searchable
432 *
433 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
434 */
David Howells664cceb2005-09-28 17:03:15 +0100435 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 ret = NULL;
437 err = ERR_PTR(-EAGAIN);
438
439 /* search the thread keyring first */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100440 if (cred->thread_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100441 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100442 make_key_ref(cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100443 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto found;
445
David Howells664cceb2005-09-28 17:03:15 +0100446 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100449 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
451 default:
David Howells664cceb2005-09-28 17:03:15 +0100452 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 }
455 }
456
457 /* search the process keyring second */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100458 if (cred->process_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100459 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100460 make_key_ref(cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100461 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto found;
463
David Howells664cceb2005-09-28 17:03:15 +0100464 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000466 if (ret)
467 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100468 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100470 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 break;
472 default:
David Howells664cceb2005-09-28 17:03:15 +0100473 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 }
476 }
477
David Howells3e301482005-06-23 22:00:56 -0700478 /* search the session keyring */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100479 if (cred->session_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100480 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100481 make_key_ref(cred->session_keyring, 1), ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
David Howells664cceb2005-09-28 17:03:15 +0100483 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700484 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
David Howells664cceb2005-09-28 17:03:15 +0100486 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700487 case -EAGAIN: /* no key */
488 if (ret)
489 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100490 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700491 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100492 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
David Howells3e301482005-06-23 22:00:56 -0700494 default:
David Howells664cceb2005-09-28 17:03:15 +0100495 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700496 break;
497 }
David Howells3e301482005-06-23 22:00:56 -0700498 }
499 /* or search the user-session keyring */
David Howells0f44e4d2019-06-26 21:02:32 +0100500 else if ((user_session = get_user_session_keyring_rcu(cred))) {
501 key_ref = keyring_search_rcu(make_key_ref(user_session, 1),
502 ctx);
503 key_put(user_session);
504
David Howells664cceb2005-09-28 17:03:15 +0100505 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700506 goto found;
507
David Howells664cceb2005-09-28 17:03:15 +0100508 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700509 case -EAGAIN: /* no key */
510 if (ret)
511 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100512 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700513 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100514 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700515 break;
516 default:
David Howells664cceb2005-09-28 17:03:15 +0100517 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700518 break;
519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521
David Howells927942a2010-06-11 17:31:10 +0100522 /* no key - decide on the error we're going to go for */
523 key_ref = ret ? ret : err;
524
525found:
526 return key_ref;
527}
528
David Howells927942a2010-06-11 17:31:10 +0100529/*
David Howells973c9f42011-01-20 16:38:33 +0000530 * Search the process keyrings attached to the supplied cred for the first
531 * matching key in the manner of search_my_process_keyrings(), but also search
532 * the keys attached to the assumed authorisation key using its credentials if
533 * one is available.
534 *
David Howellse59428f2019-06-19 16:10:15 +0100535 * The caller must be holding the RCU read lock.
536 *
537 * Return same as search_cred_keyrings_rcu().
David Howells927942a2010-06-11 17:31:10 +0100538 */
David Howellse59428f2019-06-19 16:10:15 +0100539key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100540{
541 struct request_key_auth *rka;
542 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
543
David Howellse59428f2019-06-19 16:10:15 +0100544 key_ref = search_cred_keyrings_rcu(ctx);
David Howells927942a2010-06-11 17:31:10 +0100545 if (!IS_ERR(key_ref))
546 goto found;
547 err = key_ref;
548
David Howellsb5f545c2006-01-08 01:02:47 -0800549 /* if this process has an instantiation authorisation key, then we also
550 * search the keyrings of the process mentioned there
551 * - we don't permit access to request_key auth keys via this method
552 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100553 if (ctx->cred->request_key_auth &&
554 ctx->cred == current_cred() &&
555 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800556 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100557 const struct cred *cred = ctx->cred;
558
David Howellse59428f2019-06-19 16:10:15 +0100559 if (key_validate(cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100560 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800561
David Howellse59428f2019-06-19 16:10:15 +0100562 //// was search_process_keyrings() [ie. recursive]
David Howells4bdf0bc2013-09-24 10:35:15 +0100563 ctx->cred = rka->cred;
David Howellse59428f2019-06-19 16:10:15 +0100564 key_ref = search_cred_keyrings_rcu(ctx);
David Howells4bdf0bc2013-09-24 10:35:15 +0100565 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800566
David Howells04c567d2006-06-22 14:47:18 -0700567 if (!IS_ERR(key_ref))
568 goto found;
David Howells927942a2010-06-11 17:31:10 +0100569 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800570 }
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100574 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
575 key_ref = ERR_PTR(-ENOKEY);
576 else if (err == ERR_PTR(-EACCES))
577 key_ref = ret;
578 else
579 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
David Howells3e301482005-06-23 22:00:56 -0700581found:
David Howells664cceb2005-09-28 17:03:15 +0100582 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/*
David Howells973c9f42011-01-20 16:38:33 +0000585 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100586 */
David Howells0c903ab2014-09-16 17:36:08 +0100587bool lookup_user_key_possessed(const struct key *key,
588 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100589{
David Howells46291952014-09-16 17:36:02 +0100590 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000591}
David Howells664cceb2005-09-28 17:03:15 +0100592
David Howells664cceb2005-09-28 17:03:15 +0100593/*
David Howells973c9f42011-01-20 16:38:33 +0000594 * Look up a key ID given us by userspace with a given permissions mask to get
595 * the key it refers to.
596 *
597 * Flags can be passed to request that special keyrings be created if referred
598 * to directly, to permit partially constructed keys to be found and to skip
599 * validity and permission checks on the found key.
600 *
601 * Returns a pointer to the key with an incremented usage count if successful;
602 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
603 * to a key or the best found key was a negative key; -EKEYREVOKED or
604 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
605 * found key doesn't grant the requested permit or the LSM denied access to it;
606 * or -ENOMEM if a special keyring couldn't be created.
607 *
608 * In the case of a successful return, the possession attribute is set on the
609 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
David Howells55931222009-09-02 09:13:45 +0100611key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8c0637e2020-05-12 15:16:29 +0100612 enum key_need_perm need_perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
David Howells4bdf0bc2013-09-24 10:35:15 +0100614 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100615 .match_data.cmp = lookup_user_key_possessed,
616 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100617 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
618 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100619 };
David Howells8bbf49762008-11-14 10:39:14 +1100620 struct request_key_auth *rka;
David Howells0f44e4d2019-06-26 21:02:32 +0100621 struct key *key, *user_session;
David Howellsb6dff3e2008-11-14 10:39:16 +1100622 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int ret;
624
David Howellsbb952bb2008-11-14 10:39:20 +1100625try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100626 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100627 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 switch (id) {
630 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100631 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100632 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 goto error;
634
David Howells8bbf49762008-11-14 10:39:14 +1100635 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100637 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 goto error;
639 }
David Howellsbb952bb2008-11-14 10:39:20 +1100640 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
David Howells4bdf0bc2013-09-24 10:35:15 +0100643 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100644 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100645 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 break;
647
648 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100649 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100650 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto error;
652
David Howells8bbf49762008-11-14 10:39:14 +1100653 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100655 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto error;
657 }
David Howellsbb952bb2008-11-14 10:39:20 +1100658 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
David Howells4bdf0bc2013-09-24 10:35:15 +0100661 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100662 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100663 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665
666 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100667 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /* always install a session keyring upon access if one
669 * doesn't exist yet */
David Howells0f44e4d2019-06-26 21:02:32 +0100670 ret = look_up_user_keyrings(NULL, &user_session);
David Howells69664cf2008-04-29 01:01:31 -0700671 if (ret < 0)
672 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100673 if (lflags & KEY_LOOKUP_CREATE)
674 ret = join_session_keyring(NULL);
675 else
David Howells0f44e4d2019-06-26 21:02:32 +0100676 ret = install_session_keyring(user_session);
David Howellsd84f4f92008-11-14 10:39:23 +1100677
David Howells0f44e4d2019-06-26 21:02:32 +0100678 key_put(user_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ret < 0)
680 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100681 goto reget_creds;
David Howells0f44e4d2019-06-26 21:02:32 +0100682 } else if (test_bit(KEY_FLAG_UID_KEYRING,
683 &ctx.cred->session_keyring->flags) &&
David Howells3ecf1b42011-08-22 14:08:43 +0100684 lflags & KEY_LOOKUP_CREATE) {
685 ret = join_session_keyring(NULL);
686 if (ret < 0)
687 goto error;
688 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
Jann Horn5c7e3722019-03-27 16:39:38 +0100691 key = ctx.cred->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100692 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100693 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 break;
695
696 case KEY_SPEC_USER_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100697 ret = look_up_user_keyrings(&key, NULL);
698 if (ret < 0)
699 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100700 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702
703 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100704 ret = look_up_user_keyrings(NULL, &key);
705 if (ret < 0)
706 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100707 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 break;
709
710 case KEY_SPEC_GROUP_KEYRING:
711 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100712 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto error;
714
David Howellsb5f545c2006-01-08 01:02:47 -0800715 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100716 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800717 if (!key)
718 goto error;
719
David Howellsccc3e6d2013-09-24 10:35:16 +0100720 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800721 key_ref = make_key_ref(key, 1);
722 break;
723
David Howells8bbf49762008-11-14 10:39:14 +1100724 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100725 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100726 goto error;
727
David Howells4bdf0bc2013-09-24 10:35:15 +0100728 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000729 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100730 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100731 key_ref = ERR_PTR(-EKEYREVOKED);
732 key = NULL;
733 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100734 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100735 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100736 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100737 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100738 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100739 if (!key)
740 goto error;
741 key_ref = make_key_ref(key, 1);
742 break;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 default:
David Howells664cceb2005-09-28 17:03:15 +0100745 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (id < 1)
747 goto error;
748
749 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100750 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800751 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100753 }
754
755 key_ref = make_key_ref(key, 0);
756
757 /* check to see if we possess the key */
Eric Biggers47546202019-05-29 14:01:52 -0700758 ctx.index_key = key->index_key;
David Howells46291952014-09-16 17:36:02 +0100759 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100760 kdebug("check possessed");
David Howellse59428f2019-06-19 16:10:15 +0100761 rcu_read_lock();
762 skey_ref = search_process_keyrings_rcu(&ctx);
763 rcu_read_unlock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100764 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100765
766 if (!IS_ERR(skey_ref)) {
767 key_put(key);
768 key_ref = skey_ref;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 break;
772 }
773
David Howells55931222009-09-02 09:13:45 +0100774 /* unlink does not use the nominated key in any way, so can skip all
775 * the permission checks as it is only concerned with the keyring */
David Howells8c0637e2020-05-12 15:16:29 +0100776 if (need_perm != KEY_NEED_UNLINK) {
777 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
778 ret = wait_for_key_construction(key, true);
779 switch (ret) {
780 case -ERESTARTSYS:
David Howells76181c12007-10-16 23:29:46 -0700781 goto invalid_key;
David Howells8c0637e2020-05-12 15:16:29 +0100782 default:
783 if (need_perm != KEY_AUTHTOKEN_OVERRIDE &&
784 need_perm != KEY_DEFER_PERM_CHECK)
785 goto invalid_key;
786 case 0:
787 break;
788 }
789 } else if (need_perm != KEY_DEFER_PERM_CHECK) {
790 ret = key_validate(key);
791 if (ret < 0)
792 goto invalid_key;
David Howells76181c12007-10-16 23:29:46 -0700793 }
David Howells8c0637e2020-05-12 15:16:29 +0100794
795 ret = -EIO;
796 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
797 key_read_state(key) == KEY_IS_UNINSTANTIATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto invalid_key;
799 }
800
David Howells3e301482005-06-23 22:00:56 -0700801 /* check the permissions */
David Howells8c0637e2020-05-12 15:16:29 +0100802 ret = key_task_permission(key_ref, ctx.cred, need_perm);
Linus Torvalds028db3e2019-07-10 18:43:43 -0700803 if (ret < 0)
804 goto invalid_key;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Baolin Wang074d5892017-11-15 16:38:45 +0000806 key->last_used_at = ktime_get_real_seconds();
David Howells31d5a792012-05-11 10:56:56 +0100807
David Howells664cceb2005-09-28 17:03:15 +0100808error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100809 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100810 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
David Howells664cceb2005-09-28 17:03:15 +0100812invalid_key:
813 key_ref_put(key_ref);
814 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto error;
816
David Howellsbb952bb2008-11-14 10:39:20 +1100817 /* if we attempted to install a keyring, then it may have caused new
818 * creds to be installed */
819reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100820 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100821 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000822}
Dave Jiang76ef5e12018-12-04 10:31:27 -0800823EXPORT_SYMBOL(lookup_user_key);
David Howellsbb952bb2008-11-14 10:39:20 +1100824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
David Howells973c9f42011-01-20 16:38:33 +0000826 * Join the named keyring as the session keyring if possible else attempt to
827 * create a new one of that name and join that.
828 *
829 * If the name is NULL, an empty anonymous keyring will be installed as the
830 * session keyring.
831 *
832 * Named session keyrings are joined with a semaphore held to prevent the
833 * keyrings from going away whilst the attempt is made to going them and also
834 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
836long join_session_keyring(const char *name)
837{
David Howellsd84f4f92008-11-14 10:39:23 +1100838 const struct cred *old;
839 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100841 long ret, serial;
842
David Howellsd84f4f92008-11-14 10:39:23 +1100843 new = prepare_creds();
844 if (!new)
845 return -ENOMEM;
846 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 /* if no name is provided, install an anonymous keyring */
849 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100850 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ret < 0)
852 goto error;
853
David Howells3a505972012-10-02 19:24:29 +0100854 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100855 ret = commit_creds(new);
856 if (ret == 0)
857 ret = serial;
858 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800862 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700865 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (PTR_ERR(keyring) == -ENOKEY) {
867 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100868 keyring = keyring_alloc(
Linus Torvalds028db3e2019-07-10 18:43:43 -0700869 name, old->uid, old->gid, old,
870 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100871 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (IS_ERR(keyring)) {
873 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700874 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
David Howellsd84f4f92008-11-14 10:39:23 +1100876 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ret = PTR_ERR(keyring);
878 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100879 } else if (keyring == new->session_keyring) {
880 ret = 0;
Eric Biggersd636bd92017-06-08 14:48:03 +0100881 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
884 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100885 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (ret < 0)
Eric Biggersd636bd92017-06-08 14:48:03 +0100887 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
David Howellsd84f4f92008-11-14 10:39:23 +1100889 commit_creds(new);
890 mutex_unlock(&key_session_mutex);
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 ret = keyring->serial;
893 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100894okay:
895 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Eric Biggersd636bd92017-06-08 14:48:03 +0100897error3:
898 key_put(keyring);
David Howells664cceb2005-09-28 17:03:15 +0100899error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800900 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100901error:
David Howellsd84f4f92008-11-14 10:39:23 +1100902 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100904}
David Howellsee18d642009-09-02 09:14:21 +0100905
906/*
David Howells973c9f42011-01-20 16:38:33 +0000907 * Replace a process's session keyring on behalf of one of its children when
908 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100909 */
Al Viro67d12142012-06-27 11:07:19 +0400910void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100911{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000912 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400913 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100914
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000915 if (unlikely(current->flags & PF_EXITING)) {
916 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100917 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000918 }
David Howellsee18d642009-09-02 09:14:21 +0100919
David Howellsee18d642009-09-02 09:14:21 +0100920 new-> uid = old-> uid;
921 new-> euid = old-> euid;
922 new-> suid = old-> suid;
923 new->fsuid = old->fsuid;
924 new-> gid = old-> gid;
925 new-> egid = old-> egid;
926 new-> sgid = old-> sgid;
927 new->fsgid = old->fsgid;
928 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800929 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100930 new->group_info = get_group_info(old->group_info);
931
932 new->securebits = old->securebits;
933 new->cap_inheritable = old->cap_inheritable;
934 new->cap_permitted = old->cap_permitted;
935 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700936 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100937 new->cap_bset = old->cap_bset;
938
939 new->jit_keyring = old->jit_keyring;
940 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100941 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100942
943 security_transfer_creds(new, old);
944
945 commit_creds(new);
946}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100947
948/*
949 * Make sure that root's user and user-session keyrings exist.
950 */
951static int __init init_root_keyring(void)
952{
David Howells0f44e4d2019-06-26 21:02:32 +0100953 return look_up_user_keyrings(NULL, NULL);
Mimi Zoharc124bde2013-09-04 13:26:22 +0100954}
955
956late_initcall(init_root_keyring);