blob: aa3bfcadbc6600c0a2a1eba14704aa0bcd726c19 [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
David Howells2e122562019-06-27 23:03:07 +010035static struct key_acl user_reg_keyring_acl = {
36 .usage = REFCOUNT_INIT(1),
37 .possessor_viewable = true,
38 .nr_ace = 2,
39 .aces = {
40 KEY_POSSESSOR_ACE(KEY_ACE_WRITE | KEY_ACE_SEARCH),
41 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
42 }
43};
44
45static struct key_acl user_keyring_acl = {
46 .usage = REFCOUNT_INIT(1),
47 .possessor_viewable = true,
48 .nr_ace = 2,
49 .aces = {
50 KEY_POSSESSOR_ACE(KEY_ACE_VIEW | KEY_ACE_READ | KEY_ACE_WRITE |
51 KEY_ACE_SEARCH | KEY_ACE_LINK),
52 KEY_OWNER_ACE(KEY_ACE__PERMS & ~(KEY_ACE_JOIN | KEY_ACE_SET_SECURITY)),
53 }
54};
55
56static struct key_acl session_keyring_acl = {
57 .usage = REFCOUNT_INIT(1),
58 .possessor_viewable = true,
59 .nr_ace = 2,
60 .aces = {
61 KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~KEY_ACE_JOIN),
62 KEY_OWNER_ACE(KEY_ACE_VIEW | KEY_ACE_READ),
63 }
64};
65
66static struct key_acl thread_and_process_keyring_acl = {
67 .usage = REFCOUNT_INIT(1),
68 .possessor_viewable = true,
69 .nr_ace = 2,
70 .aces = {
71 KEY_POSSESSOR_ACE(KEY_ACE__PERMS & ~(KEY_ACE_JOIN | KEY_ACE_SET_SECURITY)),
72 KEY_OWNER_ACE(KEY_ACE_VIEW),
73 }
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/*
David Howells0f44e4d2019-06-26 21:02:32 +010077 * Get or create a user register keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
David Howells0f44e4d2019-06-26 21:02:32 +010079static struct key *get_user_register(struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
David Howells0f44e4d2019-06-26 21:02:32 +010081 struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register);
82
83 if (reg_keyring)
84 return reg_keyring;
85
86 down_write(&user_ns->keyring_sem);
87
88 /* Make sure there's a register keyring. It gets owned by the
89 * user_namespace's owner.
90 */
91 reg_keyring = user_ns->user_keyring_register;
92 if (!reg_keyring) {
93 reg_keyring = keyring_alloc(".user_reg",
94 user_ns->owner, INVALID_GID,
David Howells2e122562019-06-27 23:03:07 +010095 &init_cred, &user_reg_keyring_acl,
96 0, NULL, NULL);
David Howells0f44e4d2019-06-26 21:02:32 +010097 if (!IS_ERR(reg_keyring))
98 smp_store_release(&user_ns->user_keyring_register,
99 reg_keyring);
100 }
101
102 up_write(&user_ns->keyring_sem);
103
104 /* We don't return a ref since the keyring is pinned by the user_ns */
105 return reg_keyring;
106}
107
108/*
109 * Look up the user and user session keyrings for the current process's UID,
110 * creating them if they don't exist.
111 */
112int look_up_user_keyrings(struct key **_user_keyring,
113 struct key **_user_session_keyring)
114{
115 const struct cred *cred = current_cred();
116 struct user_namespace *user_ns = current_user_ns();
117 struct key *reg_keyring, *uid_keyring, *session_keyring;
David Howells0f44e4d2019-06-26 21:02:32 +0100118 key_ref_t uid_keyring_r, session_keyring_r;
119 uid_t uid = from_kuid(user_ns, cred->user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 char buf[20];
121 int ret;
122
David Howells0f44e4d2019-06-26 21:02:32 +0100123 kenter("%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
David Howells0f44e4d2019-06-26 21:02:32 +0100125 reg_keyring = get_user_register(user_ns);
126 if (IS_ERR(reg_keyring))
127 return PTR_ERR(reg_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Howells0f44e4d2019-06-26 21:02:32 +0100129 down_write(&user_ns->keyring_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 ret = 0;
131
David Howells0f44e4d2019-06-26 21:02:32 +0100132 /* Get the user keyring. Note that there may be one in existence
133 * already as it may have been pinned by a session, but the user_struct
134 * pointing to it may have been destroyed by setuid.
135 */
136 snprintf(buf, sizeof(buf), "_uid.%u", uid);
137 uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
138 &key_type_keyring, buf, false);
139 kdebug("_uid %p", uid_keyring_r);
140 if (uid_keyring_r == ERR_PTR(-EAGAIN)) {
141 uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
David Howells2e122562019-06-27 23:03:07 +0100142 cred, &user_keyring_acl,
David Howells0f44e4d2019-06-26 21:02:32 +0100143 KEY_ALLOC_UID_KEYRING |
144 KEY_ALLOC_IN_QUOTA,
145 NULL, reg_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700146 if (IS_ERR(uid_keyring)) {
David Howells0f44e4d2019-06-26 21:02:32 +0100147 ret = PTR_ERR(uid_keyring);
148 goto error;
David Howells69664cf2008-04-29 01:01:31 -0700149 }
David Howells0f44e4d2019-06-26 21:02:32 +0100150 } else if (IS_ERR(uid_keyring_r)) {
151 ret = PTR_ERR(uid_keyring_r);
152 goto error;
153 } else {
154 uid_keyring = key_ref_to_ptr(uid_keyring_r);
David Howells69664cf2008-04-29 01:01:31 -0700155 }
156
David Howells0f44e4d2019-06-26 21:02:32 +0100157 /* Get a default session keyring (which might also exist already) */
158 snprintf(buf, sizeof(buf), "_uid_ses.%u", uid);
159 session_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
160 &key_type_keyring, buf, false);
161 kdebug("_uid_ses %p", session_keyring_r);
162 if (session_keyring_r == ERR_PTR(-EAGAIN)) {
163 session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
David Howells2e122562019-06-27 23:03:07 +0100164 cred, &user_keyring_acl,
David Howells0f44e4d2019-06-26 21:02:32 +0100165 KEY_ALLOC_UID_KEYRING |
166 KEY_ALLOC_IN_QUOTA,
167 NULL, NULL);
168 if (IS_ERR(session_keyring)) {
169 ret = PTR_ERR(session_keyring);
170 goto error_release;
171 }
172
173 /* We install a link from the user session keyring to
174 * the user keyring.
175 */
176 ret = key_link(session_keyring, uid_keyring);
177 if (ret < 0)
178 goto error_release_session;
179
180 /* And only then link the user-session keyring to the
181 * register.
182 */
183 ret = key_link(reg_keyring, session_keyring);
184 if (ret < 0)
185 goto error_release_session;
186 } else if (IS_ERR(session_keyring_r)) {
187 ret = PTR_ERR(session_keyring_r);
188 goto error_release;
189 } else {
190 session_keyring = key_ref_to_ptr(session_keyring_r);
191 }
192
193 up_write(&user_ns->keyring_sem);
194
195 if (_user_session_keyring)
196 *_user_session_keyring = session_keyring;
197 else
198 key_put(session_keyring);
199 if (_user_keyring)
200 *_user_keyring = uid_keyring;
201 else
202 key_put(uid_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700203 kleave(" = 0");
204 return 0;
205
David Howells0f44e4d2019-06-26 21:02:32 +0100206error_release_session:
David Howells69664cf2008-04-29 01:01:31 -0700207 key_put(session_keyring);
208error_release:
209 key_put(uid_keyring);
210error:
David Howells0f44e4d2019-06-26 21:02:32 +0100211 up_write(&user_ns->keyring_sem);
David Howells69664cf2008-04-29 01:01:31 -0700212 kleave(" = %d", ret);
213 return ret;
214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howells0f44e4d2019-06-26 21:02:32 +0100217 * Get the user session keyring if it exists, but don't create it if it
218 * doesn't.
219 */
220struct key *get_user_session_keyring_rcu(const struct cred *cred)
221{
222 struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register);
223 key_ref_t session_keyring_r;
224 char buf[20];
225
226 struct keyring_search_context ctx = {
227 .index_key.type = &key_type_keyring,
228 .index_key.description = buf,
229 .cred = cred,
230 .match_data.cmp = key_default_cmp,
231 .match_data.raw_data = buf,
232 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
233 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
234 };
235
236 if (!reg_keyring)
237 return NULL;
238
239 ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u",
240 from_kuid(cred->user_ns,
241 cred->user->uid));
242
243 session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true),
244 &ctx);
245 if (IS_ERR(session_keyring_r))
246 return NULL;
247 return key_ref_to_ptr(session_keyring_r);
248}
249
250/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100251 * Install a thread keyring to the given credentials struct if it didn't have
252 * one already. This is allowed to overrun the quota.
253 *
254 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
David Howellsd84f4f92008-11-14 10:39:23 +1100256int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
David Howellsd84f4f92008-11-14 10:39:23 +1100258 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Eric Biggersc9f838d2017-04-18 15:31:09 +0100260 if (new->thread_keyring)
261 return 0;
262
David Howellsd84f4f92008-11-14 10:39:23 +1100263 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells2e122562019-06-27 23:03:07 +0100264 &thread_and_process_keyring_acl,
David Howells5ac7eac2016-04-06 16:14:24 +0100265 KEY_ALLOC_QUOTA_OVERRUN,
266 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100267 if (IS_ERR(keyring))
268 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
David Howellsd84f4f92008-11-14 10:39:23 +1100270 new->thread_keyring = keyring;
271 return 0;
272}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100275 * Install a thread keyring to the current task if it didn't have one already.
276 *
277 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
David Howellsd84f4f92008-11-14 10:39:23 +1100279static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
David Howellsd84f4f92008-11-14 10:39:23 +1100281 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 int ret;
283
David Howellsd84f4f92008-11-14 10:39:23 +1100284 new = prepare_creds();
285 if (!new)
286 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
David Howellsd84f4f92008-11-14 10:39:23 +1100288 ret = install_thread_keyring_to_cred(new);
289 if (ret < 0) {
290 abort_creds(new);
291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
David Howellsd84f4f92008-11-14 10:39:23 +1100294 return commit_creds(new);
295}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
David Howellsd84f4f92008-11-14 10:39:23 +1100297/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100298 * Install a process keyring to the given credentials struct if it didn't have
299 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000300 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100301 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100302 */
303int install_process_keyring_to_cred(struct cred *new)
304{
305 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Howells3a505972012-10-02 19:24:29 +0100307 if (new->process_keyring)
Eric Biggersc9f838d2017-04-18 15:31:09 +0100308 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100309
David Howells96b5c8f2012-10-02 19:24:56 +0100310 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
David Howells2e122562019-06-27 23:03:07 +0100311 &thread_and_process_keyring_acl,
David Howells5ac7eac2016-04-06 16:14:24 +0100312 KEY_ALLOC_QUOTA_OVERRUN,
313 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100314 if (IS_ERR(keyring))
315 return PTR_ERR(keyring);
316
David Howells3a505972012-10-02 19:24:29 +0100317 new->process_keyring = keyring;
318 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100322 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000323 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100324 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
David Howellsd84f4f92008-11-14 10:39:23 +1100326static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
David Howellsd84f4f92008-11-14 10:39:23 +1100328 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 int ret;
330
David Howellsd84f4f92008-11-14 10:39:23 +1100331 new = prepare_creds();
332 if (!new)
333 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700334
David Howellsd84f4f92008-11-14 10:39:23 +1100335 ret = install_process_keyring_to_cred(new);
336 if (ret < 0) {
337 abort_creds(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +0100338 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
David Howellsd84f4f92008-11-14 10:39:23 +1100341 return commit_creds(new);
342}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100345 * Install the given keyring as the session keyring of the given credentials
346 * struct, replacing the existing one if any. If the given keyring is NULL,
347 * then install a new anonymous session keyring.
Jann Horn5c7e3722019-03-27 16:39:38 +0100348 * @cred can not be in use by any task yet.
Eric Biggersc9f838d2017-04-18 15:31:09 +0100349 *
350 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700352int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
David Howells7e047ef2006-06-26 00:24:50 -0700354 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700356
357 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* create an empty session keyring */
360 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700361 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100362 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700363 flags = KEY_ALLOC_IN_QUOTA;
364
David Howells96b5c8f2012-10-02 19:24:56 +0100365 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
David Howells2e122562019-06-27 23:03:07 +0100366 &session_keyring_acl, flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700367 if (IS_ERR(keyring))
368 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100369 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100370 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
373 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100374 old = cred->session_keyring;
Jann Horn5c7e3722019-03-27 16:39:38 +0100375 cred->session_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
David Howells3a505972012-10-02 19:24:29 +0100377 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700378 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
David Howells1a26feb2006-04-10 22:54:26 -0700380 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100384 * Install the given keyring as the session keyring of the current task,
385 * replacing the existing one if any. If the given keyring is NULL, then
386 * install a new anonymous session keyring.
387 *
388 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
David Howellsd84f4f92008-11-14 10:39:23 +1100390static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
David Howellsd84f4f92008-11-14 10:39:23 +1100392 struct cred *new;
393 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Howellsd84f4f92008-11-14 10:39:23 +1100395 new = prepare_creds();
396 if (!new)
397 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800398
David Howells995995372011-08-22 14:08:33 +0100399 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100400 if (ret < 0) {
401 abort_creds(new);
402 return ret;
403 }
David Howellsb5f545c2006-01-08 01:02:47 -0800404
David Howellsd84f4f92008-11-14 10:39:23 +1100405 return commit_creds(new);
406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
David Howells973c9f42011-01-20 16:38:33 +0000409 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
David Howells2e218652019-05-22 14:06:51 +0100411void key_fsuid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100414 if (new_cred->thread_keyring) {
415 down_write(&new_cred->thread_keyring->sem);
416 new_cred->thread_keyring->uid = new_cred->fsuid;
417 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000419}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
David Howells973c9f42011-01-20 16:38:33 +0000422 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
David Howells2e218652019-05-22 14:06:51 +0100424void key_fsgid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100427 if (new_cred->thread_keyring) {
428 down_write(&new_cred->thread_keyring->sem);
429 new_cred->thread_keyring->gid = new_cred->fsgid;
430 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000432}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
David Howells973c9f42011-01-20 16:38:33 +0000435 * Search the process keyrings attached to the supplied cred for the first
David Howellse59428f2019-06-19 16:10:15 +0100436 * matching key under RCU conditions (the caller must be holding the RCU read
437 * lock).
David Howells973c9f42011-01-20 16:38:33 +0000438 *
439 * The search criteria are the type and the match function. The description is
440 * given to the match function as a parameter, but doesn't otherwise influence
441 * the search. Typically the match function will compare the description
442 * parameter to the key's description.
443 *
444 * This can only search keyrings that grant Search permission to the supplied
445 * credentials. Keyrings linked to searched keyrings will also be searched if
446 * they grant Search permission too. Keys can only be found if they grant
447 * Search permission to the credentials.
448 *
449 * Returns a pointer to the key with the key usage count incremented if
450 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
451 * matched negative keys.
452 *
453 * In the case of a successful return, the possession attribute is set on the
454 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
David Howellse59428f2019-06-19 16:10:15 +0100456key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
David Howells0f44e4d2019-06-26 21:02:32 +0100458 struct key *user_session;
David Howellsb5f545c2006-01-08 01:02:47 -0800459 key_ref_t key_ref, ret, err;
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100460 const struct cred *cred = ctx->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
463 * searchable, but we failed to find a key or we found a negative key;
464 * otherwise we want to return a sample error (probably -EACCES) if
465 * none of the keyrings were searchable
466 *
467 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
468 */
David Howells664cceb2005-09-28 17:03:15 +0100469 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ret = NULL;
471 err = ERR_PTR(-EAGAIN);
472
473 /* search the thread keyring first */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100474 if (cred->thread_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100475 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100476 make_key_ref(cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100477 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto found;
479
David Howells664cceb2005-09-28 17:03:15 +0100480 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100483 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 default:
David Howells664cceb2005-09-28 17:03:15 +0100486 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 break;
488 }
489 }
490
491 /* search the process keyring second */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100492 if (cred->process_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100493 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100494 make_key_ref(cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100495 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 goto found;
497
David Howells664cceb2005-09-28 17:03:15 +0100498 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000500 if (ret)
501 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100502 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100504 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 default:
David Howells664cceb2005-09-28 17:03:15 +0100507 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 break;
509 }
510 }
511
David Howells3e301482005-06-23 22:00:56 -0700512 /* search the session keyring */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100513 if (cred->session_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100514 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100515 make_key_ref(cred->session_keyring, 1), ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
David Howells664cceb2005-09-28 17:03:15 +0100517 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700518 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
David Howells664cceb2005-09-28 17:03:15 +0100520 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700521 case -EAGAIN: /* no key */
522 if (ret)
523 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100524 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700525 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100526 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 break;
David Howells3e301482005-06-23 22:00:56 -0700528 default:
David Howells664cceb2005-09-28 17:03:15 +0100529 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700530 break;
531 }
David Howells3e301482005-06-23 22:00:56 -0700532 }
533 /* or search the user-session keyring */
David Howells0f44e4d2019-06-26 21:02:32 +0100534 else if ((user_session = get_user_session_keyring_rcu(cred))) {
535 key_ref = keyring_search_rcu(make_key_ref(user_session, 1),
536 ctx);
537 key_put(user_session);
538
David Howells664cceb2005-09-28 17:03:15 +0100539 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700540 goto found;
541
David Howells664cceb2005-09-28 17:03:15 +0100542 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700543 case -EAGAIN: /* no key */
544 if (ret)
545 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100546 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700547 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100548 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700549 break;
550 default:
David Howells664cceb2005-09-28 17:03:15 +0100551 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700552 break;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
David Howells927942a2010-06-11 17:31:10 +0100556 /* no key - decide on the error we're going to go for */
557 key_ref = ret ? ret : err;
558
559found:
560 return key_ref;
561}
562
David Howells927942a2010-06-11 17:31:10 +0100563/*
David Howells973c9f42011-01-20 16:38:33 +0000564 * Search the process keyrings attached to the supplied cred for the first
565 * matching key in the manner of search_my_process_keyrings(), but also search
566 * the keys attached to the assumed authorisation key using its credentials if
567 * one is available.
568 *
David Howellse59428f2019-06-19 16:10:15 +0100569 * The caller must be holding the RCU read lock.
570 *
571 * Return same as search_cred_keyrings_rcu().
David Howells927942a2010-06-11 17:31:10 +0100572 */
David Howellse59428f2019-06-19 16:10:15 +0100573key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100574{
575 struct request_key_auth *rka;
576 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
577
David Howellse59428f2019-06-19 16:10:15 +0100578 key_ref = search_cred_keyrings_rcu(ctx);
David Howells927942a2010-06-11 17:31:10 +0100579 if (!IS_ERR(key_ref))
580 goto found;
581 err = key_ref;
582
David Howellsb5f545c2006-01-08 01:02:47 -0800583 /* if this process has an instantiation authorisation key, then we also
584 * search the keyrings of the process mentioned there
585 * - we don't permit access to request_key auth keys via this method
586 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100587 if (ctx->cred->request_key_auth &&
588 ctx->cred == current_cred() &&
589 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800590 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100591 const struct cred *cred = ctx->cred;
592
David Howellse59428f2019-06-19 16:10:15 +0100593 if (key_validate(cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100594 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800595
David Howellse59428f2019-06-19 16:10:15 +0100596 //// was search_process_keyrings() [ie. recursive]
David Howells4bdf0bc2013-09-24 10:35:15 +0100597 ctx->cred = rka->cred;
David Howellse59428f2019-06-19 16:10:15 +0100598 key_ref = search_cred_keyrings_rcu(ctx);
David Howells4bdf0bc2013-09-24 10:35:15 +0100599 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800600
David Howells04c567d2006-06-22 14:47:18 -0700601 if (!IS_ERR(key_ref))
602 goto found;
David Howells927942a2010-06-11 17:31:10 +0100603 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800604 }
605 }
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100608 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
609 key_ref = ERR_PTR(-ENOKEY);
610 else if (err == ERR_PTR(-EACCES))
611 key_ref = ret;
612 else
613 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
David Howells3e301482005-06-23 22:00:56 -0700615found:
David Howells664cceb2005-09-28 17:03:15 +0100616 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/*
David Howells973c9f42011-01-20 16:38:33 +0000619 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100620 */
David Howells0c903ab2014-09-16 17:36:08 +0100621bool lookup_user_key_possessed(const struct key *key,
622 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100623{
David Howells46291952014-09-16 17:36:02 +0100624 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000625}
David Howells664cceb2005-09-28 17:03:15 +0100626
David Howells664cceb2005-09-28 17:03:15 +0100627/*
David Howells973c9f42011-01-20 16:38:33 +0000628 * Look up a key ID given us by userspace with a given permissions mask to get
629 * the key it refers to.
630 *
631 * Flags can be passed to request that special keyrings be created if referred
632 * to directly, to permit partially constructed keys to be found and to skip
633 * validity and permission checks on the found key.
634 *
635 * Returns a pointer to the key with an incremented usage count if successful;
636 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
637 * to a key or the best found key was a negative key; -EKEYREVOKED or
638 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
639 * found key doesn't grant the requested permit or the LSM denied access to it;
640 * or -ENOMEM if a special keyring couldn't be created.
641 *
642 * In the case of a successful return, the possession attribute is set on the
643 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
David Howells55931222009-09-02 09:13:45 +0100645key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells2e122562019-06-27 23:03:07 +0100646 unsigned int desired_perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
David Howells4bdf0bc2013-09-24 10:35:15 +0100648 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100649 .match_data.cmp = lookup_user_key_possessed,
650 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100651 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
652 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100653 };
David Howells8bbf49762008-11-14 10:39:14 +1100654 struct request_key_auth *rka;
David Howells0f44e4d2019-06-26 21:02:32 +0100655 struct key *key, *user_session;
David Howellsb6dff3e2008-11-14 10:39:16 +1100656 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 int ret;
658
David Howellsbb952bb2008-11-14 10:39:20 +1100659try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100660 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100661 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 switch (id) {
664 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100665 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100666 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto error;
668
David Howells8bbf49762008-11-14 10:39:14 +1100669 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100671 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto error;
673 }
David Howellsbb952bb2008-11-14 10:39:20 +1100674 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
David Howells4bdf0bc2013-09-24 10:35:15 +0100677 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100678 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100679 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 break;
681
682 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100683 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100684 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto error;
686
David Howells8bbf49762008-11-14 10:39:14 +1100687 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100689 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 goto error;
691 }
David Howellsbb952bb2008-11-14 10:39:20 +1100692 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
David Howells4bdf0bc2013-09-24 10:35:15 +0100695 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100696 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100697 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 break;
699
700 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100701 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 /* always install a session keyring upon access if one
703 * doesn't exist yet */
David Howells0f44e4d2019-06-26 21:02:32 +0100704 ret = look_up_user_keyrings(NULL, &user_session);
David Howells69664cf2008-04-29 01:01:31 -0700705 if (ret < 0)
706 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100707 if (lflags & KEY_LOOKUP_CREATE)
708 ret = join_session_keyring(NULL);
709 else
David Howells0f44e4d2019-06-26 21:02:32 +0100710 ret = install_session_keyring(user_session);
David Howellsd84f4f92008-11-14 10:39:23 +1100711
David Howells0f44e4d2019-06-26 21:02:32 +0100712 key_put(user_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (ret < 0)
714 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100715 goto reget_creds;
David Howells0f44e4d2019-06-26 21:02:32 +0100716 } else if (test_bit(KEY_FLAG_UID_KEYRING,
717 &ctx.cred->session_keyring->flags) &&
David Howells3ecf1b42011-08-22 14:08:43 +0100718 lflags & KEY_LOOKUP_CREATE) {
719 ret = join_session_keyring(NULL);
720 if (ret < 0)
721 goto error;
722 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Jann Horn5c7e3722019-03-27 16:39:38 +0100725 key = ctx.cred->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100726 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100727 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729
730 case KEY_SPEC_USER_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100731 ret = look_up_user_keyrings(&key, NULL);
732 if (ret < 0)
733 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100734 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736
737 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100738 ret = look_up_user_keyrings(NULL, &key);
739 if (ret < 0)
740 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100741 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743
744 case KEY_SPEC_GROUP_KEYRING:
745 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100746 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto error;
748
David Howellsb5f545c2006-01-08 01:02:47 -0800749 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100750 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800751 if (!key)
752 goto error;
753
David Howellsccc3e6d2013-09-24 10:35:16 +0100754 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800755 key_ref = make_key_ref(key, 1);
756 break;
757
David Howells8bbf49762008-11-14 10:39:14 +1100758 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100759 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100760 goto error;
761
David Howells4bdf0bc2013-09-24 10:35:15 +0100762 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000763 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100764 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100765 key_ref = ERR_PTR(-EKEYREVOKED);
766 key = NULL;
767 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100768 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100769 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100770 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100771 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100772 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100773 if (!key)
774 goto error;
775 key_ref = make_key_ref(key, 1);
776 break;
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 default:
David Howells664cceb2005-09-28 17:03:15 +0100779 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (id < 1)
781 goto error;
782
783 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100784 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800785 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100787 }
788
789 key_ref = make_key_ref(key, 0);
790
791 /* check to see if we possess the key */
Eric Biggers47546202019-05-29 14:01:52 -0700792 ctx.index_key = key->index_key;
David Howells46291952014-09-16 17:36:02 +0100793 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100794 kdebug("check possessed");
David Howellse59428f2019-06-19 16:10:15 +0100795 rcu_read_lock();
796 skey_ref = search_process_keyrings_rcu(&ctx);
797 rcu_read_unlock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100798 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100799
800 if (!IS_ERR(skey_ref)) {
801 key_put(key);
802 key_ref = skey_ref;
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 break;
806 }
807
David Howells55931222009-09-02 09:13:45 +0100808 /* unlink does not use the nominated key in any way, so can skip all
809 * the permission checks as it is only concerned with the keyring */
810 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
811 ret = 0;
812 goto error;
813 }
814
815 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700816 ret = wait_for_key_construction(key, true);
817 switch (ret) {
818 case -ERESTARTSYS:
819 goto invalid_key;
820 default:
David Howells2e122562019-06-27 23:03:07 +0100821 if (desired_perm)
David Howells76181c12007-10-16 23:29:46 -0700822 goto invalid_key;
823 case 0:
824 break;
825 }
David Howells2e122562019-06-27 23:03:07 +0100826 } else if (desired_perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 ret = key_validate(key);
828 if (ret < 0)
829 goto invalid_key;
830 }
831
832 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100833 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
David Howells363b02d2017-10-04 16:43:25 +0100834 key_read_state(key) == KEY_IS_UNINSTANTIATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto invalid_key;
836
David Howells3e301482005-06-23 22:00:56 -0700837 /* check the permissions */
David Howells2e122562019-06-27 23:03:07 +0100838 if (desired_perm) {
839 ret = key_task_permission(key_ref, ctx.cred, desired_perm);
840 if (ret < 0)
841 goto invalid_key;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Baolin Wang074d5892017-11-15 16:38:45 +0000844 key->last_used_at = ktime_get_real_seconds();
David Howells31d5a792012-05-11 10:56:56 +0100845
David Howells664cceb2005-09-28 17:03:15 +0100846error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100847 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100848 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
David Howells664cceb2005-09-28 17:03:15 +0100850invalid_key:
851 key_ref_put(key_ref);
852 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 goto error;
854
David Howellsbb952bb2008-11-14 10:39:20 +1100855 /* if we attempted to install a keyring, then it may have caused new
856 * creds to be installed */
857reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100858 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100859 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000860}
Dave Jiang76ef5e12018-12-04 10:31:27 -0800861EXPORT_SYMBOL(lookup_user_key);
David Howellsbb952bb2008-11-14 10:39:20 +1100862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
David Howells973c9f42011-01-20 16:38:33 +0000864 * Join the named keyring as the session keyring if possible else attempt to
865 * create a new one of that name and join that.
866 *
867 * If the name is NULL, an empty anonymous keyring will be installed as the
868 * session keyring.
869 *
870 * Named session keyrings are joined with a semaphore held to prevent the
871 * keyrings from going away whilst the attempt is made to going them and also
872 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 */
874long join_session_keyring(const char *name)
875{
David Howellsd84f4f92008-11-14 10:39:23 +1100876 const struct cred *old;
877 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100879 long ret, serial;
880
David Howellsd84f4f92008-11-14 10:39:23 +1100881 new = prepare_creds();
882 if (!new)
883 return -ENOMEM;
884 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* if no name is provided, install an anonymous keyring */
887 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100888 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ret < 0)
890 goto error;
891
David Howells3a505972012-10-02 19:24:29 +0100892 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100893 ret = commit_creds(new);
894 if (ret == 0)
895 ret = serial;
896 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800900 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700903 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (PTR_ERR(keyring) == -ENOKEY) {
905 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100906 keyring = keyring_alloc(
David Howells2e122562019-06-27 23:03:07 +0100907 name, old->uid, old->gid, old, &joinable_keyring_acl,
David Howells5ac7eac2016-04-06 16:14:24 +0100908 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (IS_ERR(keyring)) {
910 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700911 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
David Howells2e122562019-06-27 23:03:07 +0100913 goto no_perm_test;
David Howellsd84f4f92008-11-14 10:39:23 +1100914 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 ret = PTR_ERR(keyring);
916 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100917 } else if (keyring == new->session_keyring) {
918 ret = 0;
Eric Biggersd636bd92017-06-08 14:48:03 +0100919 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
David Howells2e122562019-06-27 23:03:07 +0100922 ret = key_task_permission(make_key_ref(keyring, false), old,
923 KEY_NEED_JOIN);
924 if (ret < 0)
925 goto error3;
926
927no_perm_test:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100929 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (ret < 0)
Eric Biggersd636bd92017-06-08 14:48:03 +0100931 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
David Howellsd84f4f92008-11-14 10:39:23 +1100933 commit_creds(new);
934 mutex_unlock(&key_session_mutex);
935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ret = keyring->serial;
937 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100938okay:
939 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Eric Biggersd636bd92017-06-08 14:48:03 +0100941error3:
942 key_put(keyring);
David Howells664cceb2005-09-28 17:03:15 +0100943error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800944 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100945error:
David Howellsd84f4f92008-11-14 10:39:23 +1100946 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100948}
David Howellsee18d642009-09-02 09:14:21 +0100949
950/*
David Howells973c9f42011-01-20 16:38:33 +0000951 * Replace a process's session keyring on behalf of one of its children when
952 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100953 */
Al Viro67d12142012-06-27 11:07:19 +0400954void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100955{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000956 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400957 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100958
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000959 if (unlikely(current->flags & PF_EXITING)) {
960 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100961 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000962 }
David Howellsee18d642009-09-02 09:14:21 +0100963
David Howellsee18d642009-09-02 09:14:21 +0100964 new-> uid = old-> uid;
965 new-> euid = old-> euid;
966 new-> suid = old-> suid;
967 new->fsuid = old->fsuid;
968 new-> gid = old-> gid;
969 new-> egid = old-> egid;
970 new-> sgid = old-> sgid;
971 new->fsgid = old->fsgid;
972 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800973 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100974 new->group_info = get_group_info(old->group_info);
975
976 new->securebits = old->securebits;
977 new->cap_inheritable = old->cap_inheritable;
978 new->cap_permitted = old->cap_permitted;
979 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700980 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100981 new->cap_bset = old->cap_bset;
982
983 new->jit_keyring = old->jit_keyring;
984 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100985 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100986
987 security_transfer_creds(new, old);
988
989 commit_creds(new);
990}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100991
992/*
993 * Make sure that root's user and user-session keyrings exist.
994 */
995static int __init init_root_keyring(void)
996{
David Howells0f44e4d2019-06-26 21:02:32 +0100997 return look_up_user_keyrings(NULL, NULL);
Mimi Zoharc124bde2013-09-04 13:26:22 +0100998}
999
1000late_initcall(init_root_keyring);