blob: f74d6421594229a71511a316f1d18ae5c6cb5159 [file] [log] [blame]
David Howells973c9f42011-01-20 16:38:33 +00001/* Manage a process's keyrings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells69664cf2008-04-29 01:01:31 -07003 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
13#include <linux/sched.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010014#include <linux/sched/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/keyctl.h>
16#include <linux/fs.h>
17#include <linux/err.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080018#include <linux/mutex.h>
David Howellsee18d642009-09-02 09:14:21 +010019#include <linux/security.h>
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060020#include <linux/user_namespace.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080021#include <linux/uaccess.h>
David Howells0f44e4d2019-06-26 21:02:32 +010022#include <linux/init_task.h>
David Howells822ad642019-02-14 16:20:25 +000023#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
David Howells973c9f42011-01-20 16:38:33 +000026/* Session keyring create vs join semaphore */
Ingo Molnarbb0030792006-03-22 00:09:14 -080027static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
David Howells973c9f42011-01-20 16:38:33 +000029/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030struct key_user root_key_user = {
Elena Reshetovaddb99e12017-03-31 15:20:49 +030031 .usage = REFCOUNT_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070032 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080033 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .nkeys = ATOMIC_INIT(2),
35 .nikeys = ATOMIC_INIT(2),
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080036 .uid = GLOBAL_ROOT_UID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*
David Howells0f44e4d2019-06-26 21:02:32 +010040 * Get or create a user register keyring.
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
David Howells0f44e4d2019-06-26 21:02:32 +010042static struct key *get_user_register(struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
David Howells0f44e4d2019-06-26 21:02:32 +010044 struct key *reg_keyring = READ_ONCE(user_ns->user_keyring_register);
45
46 if (reg_keyring)
47 return reg_keyring;
48
49 down_write(&user_ns->keyring_sem);
50
51 /* Make sure there's a register keyring. It gets owned by the
52 * user_namespace's owner.
53 */
54 reg_keyring = user_ns->user_keyring_register;
55 if (!reg_keyring) {
56 reg_keyring = keyring_alloc(".user_reg",
57 user_ns->owner, INVALID_GID,
58 &init_cred,
59 KEY_POS_WRITE | KEY_POS_SEARCH |
60 KEY_USR_VIEW | KEY_USR_READ,
61 0,
62 NULL, NULL);
63 if (!IS_ERR(reg_keyring))
64 smp_store_release(&user_ns->user_keyring_register,
65 reg_keyring);
66 }
67
68 up_write(&user_ns->keyring_sem);
69
70 /* We don't return a ref since the keyring is pinned by the user_ns */
71 return reg_keyring;
72}
73
74/*
75 * Look up the user and user session keyrings for the current process's UID,
76 * creating them if they don't exist.
77 */
78int look_up_user_keyrings(struct key **_user_keyring,
79 struct key **_user_session_keyring)
80{
81 const struct cred *cred = current_cred();
82 struct user_namespace *user_ns = current_user_ns();
83 struct key *reg_keyring, *uid_keyring, *session_keyring;
David Howells96b5c8f2012-10-02 19:24:56 +010084 key_perm_t user_keyring_perm;
David Howells0f44e4d2019-06-26 21:02:32 +010085 key_ref_t uid_keyring_r, session_keyring_r;
86 uid_t uid = from_kuid(user_ns, cred->user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 char buf[20];
88 int ret;
89
David Howells96b5c8f2012-10-02 19:24:56 +010090 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
David Howellsd84f4f92008-11-14 10:39:23 +110091
David Howells0f44e4d2019-06-26 21:02:32 +010092 kenter("%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David Howells0f44e4d2019-06-26 21:02:32 +010094 reg_keyring = get_user_register(user_ns);
95 if (IS_ERR(reg_keyring))
96 return PTR_ERR(reg_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
David Howells0f44e4d2019-06-26 21:02:32 +010098 down_write(&user_ns->keyring_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 ret = 0;
100
David Howells0f44e4d2019-06-26 21:02:32 +0100101 /* Get the user keyring. Note that there may be one in existence
102 * already as it may have been pinned by a session, but the user_struct
103 * pointing to it may have been destroyed by setuid.
104 */
105 snprintf(buf, sizeof(buf), "_uid.%u", uid);
106 uid_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
107 &key_type_keyring, buf, false);
108 kdebug("_uid %p", uid_keyring_r);
109 if (uid_keyring_r == ERR_PTR(-EAGAIN)) {
110 uid_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
111 cred, user_keyring_perm,
112 KEY_ALLOC_UID_KEYRING |
113 KEY_ALLOC_IN_QUOTA,
114 NULL, reg_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700115 if (IS_ERR(uid_keyring)) {
David Howells0f44e4d2019-06-26 21:02:32 +0100116 ret = PTR_ERR(uid_keyring);
117 goto error;
David Howells69664cf2008-04-29 01:01:31 -0700118 }
David Howells0f44e4d2019-06-26 21:02:32 +0100119 } else if (IS_ERR(uid_keyring_r)) {
120 ret = PTR_ERR(uid_keyring_r);
121 goto error;
122 } else {
123 uid_keyring = key_ref_to_ptr(uid_keyring_r);
David Howells69664cf2008-04-29 01:01:31 -0700124 }
125
David Howells0f44e4d2019-06-26 21:02:32 +0100126 /* Get a default session keyring (which might also exist already) */
127 snprintf(buf, sizeof(buf), "_uid_ses.%u", uid);
128 session_keyring_r = keyring_search(make_key_ref(reg_keyring, true),
129 &key_type_keyring, buf, false);
130 kdebug("_uid_ses %p", session_keyring_r);
131 if (session_keyring_r == ERR_PTR(-EAGAIN)) {
132 session_keyring = keyring_alloc(buf, cred->user->uid, INVALID_GID,
133 cred, user_keyring_perm,
134 KEY_ALLOC_UID_KEYRING |
135 KEY_ALLOC_IN_QUOTA,
136 NULL, NULL);
137 if (IS_ERR(session_keyring)) {
138 ret = PTR_ERR(session_keyring);
139 goto error_release;
140 }
141
142 /* We install a link from the user session keyring to
143 * the user keyring.
144 */
145 ret = key_link(session_keyring, uid_keyring);
146 if (ret < 0)
147 goto error_release_session;
148
149 /* And only then link the user-session keyring to the
150 * register.
151 */
152 ret = key_link(reg_keyring, session_keyring);
153 if (ret < 0)
154 goto error_release_session;
155 } else if (IS_ERR(session_keyring_r)) {
156 ret = PTR_ERR(session_keyring_r);
157 goto error_release;
158 } else {
159 session_keyring = key_ref_to_ptr(session_keyring_r);
160 }
161
162 up_write(&user_ns->keyring_sem);
163
164 if (_user_session_keyring)
165 *_user_session_keyring = session_keyring;
166 else
167 key_put(session_keyring);
168 if (_user_keyring)
169 *_user_keyring = uid_keyring;
170 else
171 key_put(uid_keyring);
David Howells69664cf2008-04-29 01:01:31 -0700172 kleave(" = 0");
173 return 0;
174
David Howells0f44e4d2019-06-26 21:02:32 +0100175error_release_session:
David Howells69664cf2008-04-29 01:01:31 -0700176 key_put(session_keyring);
177error_release:
178 key_put(uid_keyring);
179error:
David Howells0f44e4d2019-06-26 21:02:32 +0100180 up_write(&user_ns->keyring_sem);
David Howells69664cf2008-04-29 01:01:31 -0700181 kleave(" = %d", ret);
182 return ret;
183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
David Howells0f44e4d2019-06-26 21:02:32 +0100186 * Get the user session keyring if it exists, but don't create it if it
187 * doesn't.
188 */
189struct key *get_user_session_keyring_rcu(const struct cred *cred)
190{
191 struct key *reg_keyring = READ_ONCE(cred->user_ns->user_keyring_register);
192 key_ref_t session_keyring_r;
193 char buf[20];
194
195 struct keyring_search_context ctx = {
196 .index_key.type = &key_type_keyring,
197 .index_key.description = buf,
198 .cred = cred,
199 .match_data.cmp = key_default_cmp,
200 .match_data.raw_data = buf,
201 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
202 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
203 };
204
205 if (!reg_keyring)
206 return NULL;
207
208 ctx.index_key.desc_len = snprintf(buf, sizeof(buf), "_uid_ses.%u",
209 from_kuid(cred->user_ns,
210 cred->user->uid));
211
212 session_keyring_r = keyring_search_rcu(make_key_ref(reg_keyring, true),
213 &ctx);
214 if (IS_ERR(session_keyring_r))
215 return NULL;
216 return key_ref_to_ptr(session_keyring_r);
217}
218
219/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100220 * Install a thread keyring to the given credentials struct if it didn't have
221 * one already. This is allowed to overrun the quota.
222 *
223 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 */
David Howellsd84f4f92008-11-14 10:39:23 +1100225int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
David Howellsd84f4f92008-11-14 10:39:23 +1100227 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Eric Biggersc9f838d2017-04-18 15:31:09 +0100229 if (new->thread_keyring)
230 return 0;
231
David Howellsd84f4f92008-11-14 10:39:23 +1100232 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100233 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100234 KEY_ALLOC_QUOTA_OVERRUN,
235 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100236 if (IS_ERR(keyring))
237 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
David Howellsd84f4f92008-11-14 10:39:23 +1100239 new->thread_keyring = keyring;
240 return 0;
241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100244 * Install a thread keyring to the current task if it didn't have one already.
245 *
246 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
David Howellsd84f4f92008-11-14 10:39:23 +1100248static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
David Howellsd84f4f92008-11-14 10:39:23 +1100250 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int ret;
252
David Howellsd84f4f92008-11-14 10:39:23 +1100253 new = prepare_creds();
254 if (!new)
255 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David Howellsd84f4f92008-11-14 10:39:23 +1100257 ret = install_thread_keyring_to_cred(new);
258 if (ret < 0) {
259 abort_creds(new);
260 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
262
David Howellsd84f4f92008-11-14 10:39:23 +1100263 return commit_creds(new);
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Howellsd84f4f92008-11-14 10:39:23 +1100266/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100267 * Install a process keyring to the given credentials struct if it didn't have
268 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000269 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100270 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100271 */
272int install_process_keyring_to_cred(struct cred *new)
273{
274 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Howells3a505972012-10-02 19:24:29 +0100276 if (new->process_keyring)
Eric Biggersc9f838d2017-04-18 15:31:09 +0100277 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100278
David Howells96b5c8f2012-10-02 19:24:56 +0100279 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
280 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100281 KEY_ALLOC_QUOTA_OVERRUN,
282 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100283 if (IS_ERR(keyring))
284 return PTR_ERR(keyring);
285
David Howells3a505972012-10-02 19:24:29 +0100286 new->process_keyring = keyring;
287 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100291 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000292 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100293 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
David Howellsd84f4f92008-11-14 10:39:23 +1100295static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
David Howellsd84f4f92008-11-14 10:39:23 +1100297 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 int ret;
299
David Howellsd84f4f92008-11-14 10:39:23 +1100300 new = prepare_creds();
301 if (!new)
302 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700303
David Howellsd84f4f92008-11-14 10:39:23 +1100304 ret = install_process_keyring_to_cred(new);
305 if (ret < 0) {
306 abort_creds(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +0100307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
David Howellsd84f4f92008-11-14 10:39:23 +1100310 return commit_creds(new);
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100314 * Install the given keyring as the session keyring of the given credentials
315 * struct, replacing the existing one if any. If the given keyring is NULL,
316 * then install a new anonymous session keyring.
Jann Horn5c7e3722019-03-27 16:39:38 +0100317 * @cred can not be in use by any task yet.
Eric Biggersc9f838d2017-04-18 15:31:09 +0100318 *
319 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700321int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Howells7e047ef2006-06-26 00:24:50 -0700323 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700325
326 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* create an empty session keyring */
329 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700330 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100331 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700332 flags = KEY_ALLOC_IN_QUOTA;
333
David Howells96b5c8f2012-10-02 19:24:56 +0100334 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
335 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100336 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700337 if (IS_ERR(keyring))
338 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100339 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100340 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100344 old = cred->session_keyring;
Jann Horn5c7e3722019-03-27 16:39:38 +0100345 cred->session_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
David Howells3a505972012-10-02 19:24:29 +0100347 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700348 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
David Howells1a26feb2006-04-10 22:54:26 -0700350 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100354 * Install the given keyring as the session keyring of the current task,
355 * replacing the existing one if any. If the given keyring is NULL, then
356 * install a new anonymous session keyring.
357 *
358 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
David Howellsd84f4f92008-11-14 10:39:23 +1100360static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
David Howellsd84f4f92008-11-14 10:39:23 +1100362 struct cred *new;
363 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David Howellsd84f4f92008-11-14 10:39:23 +1100365 new = prepare_creds();
366 if (!new)
367 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800368
David Howells995995372011-08-22 14:08:33 +0100369 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100370 if (ret < 0) {
371 abort_creds(new);
372 return ret;
373 }
David Howellsb5f545c2006-01-08 01:02:47 -0800374
David Howellsd84f4f92008-11-14 10:39:23 +1100375 return commit_creds(new);
376}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/*
David Howells973c9f42011-01-20 16:38:33 +0000379 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
David Howells2e218652019-05-22 14:06:51 +0100381void key_fsuid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100384 if (new_cred->thread_keyring) {
385 down_write(&new_cred->thread_keyring->sem);
386 new_cred->thread_keyring->uid = new_cred->fsuid;
387 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/*
David Howells973c9f42011-01-20 16:38:33 +0000392 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 */
David Howells2e218652019-05-22 14:06:51 +0100394void key_fsgid_changed(struct cred *new_cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 /* update the ownership of the thread keyring */
David Howells2e218652019-05-22 14:06:51 +0100397 if (new_cred->thread_keyring) {
398 down_write(&new_cred->thread_keyring->sem);
399 new_cred->thread_keyring->gid = new_cred->fsgid;
400 up_write(&new_cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
David Howells973c9f42011-01-20 16:38:33 +0000405 * Search the process keyrings attached to the supplied cred for the first
David Howellse59428f2019-06-19 16:10:15 +0100406 * matching key under RCU conditions (the caller must be holding the RCU read
407 * lock).
David Howells973c9f42011-01-20 16:38:33 +0000408 *
409 * The search criteria are the type and the match function. The description is
410 * given to the match function as a parameter, but doesn't otherwise influence
411 * the search. Typically the match function will compare the description
412 * parameter to the key's description.
413 *
414 * This can only search keyrings that grant Search permission to the supplied
415 * credentials. Keyrings linked to searched keyrings will also be searched if
416 * they grant Search permission too. Keys can only be found if they grant
417 * Search permission to the credentials.
418 *
419 * Returns a pointer to the key with the key usage count incremented if
420 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
421 * matched negative keys.
422 *
423 * In the case of a successful return, the possession attribute is set on the
424 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 */
David Howellse59428f2019-06-19 16:10:15 +0100426key_ref_t search_cred_keyrings_rcu(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
David Howells0f44e4d2019-06-26 21:02:32 +0100428 struct key *user_session;
David Howellsb5f545c2006-01-08 01:02:47 -0800429 key_ref_t key_ref, ret, err;
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100430 const struct cred *cred = ctx->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
433 * searchable, but we failed to find a key or we found a negative key;
434 * otherwise we want to return a sample error (probably -EACCES) if
435 * none of the keyrings were searchable
436 *
437 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
438 */
David Howells664cceb2005-09-28 17:03:15 +0100439 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ret = NULL;
441 err = ERR_PTR(-EAGAIN);
442
443 /* search the thread keyring first */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100444 if (cred->thread_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100445 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100446 make_key_ref(cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100447 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 goto found;
449
David Howells664cceb2005-09-28 17:03:15 +0100450 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100453 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 default:
David Howells664cceb2005-09-28 17:03:15 +0100456 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 }
459 }
460
461 /* search the process keyring second */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100462 if (cred->process_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100463 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100464 make_key_ref(cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100465 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 goto found;
467
David Howells664cceb2005-09-28 17:03:15 +0100468 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000470 if (ret)
471 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100472 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100474 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 default:
David Howells664cceb2005-09-28 17:03:15 +0100477 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 }
480 }
481
David Howells3e301482005-06-23 22:00:56 -0700482 /* search the session keyring */
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100483 if (cred->session_keyring) {
David Howellse59428f2019-06-19 16:10:15 +0100484 key_ref = keyring_search_rcu(
Jann Horn0b9dc6c2019-03-27 16:55:08 +0100485 make_key_ref(cred->session_keyring, 1), ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
David Howells664cceb2005-09-28 17:03:15 +0100487 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700488 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David Howells664cceb2005-09-28 17:03:15 +0100490 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700491 case -EAGAIN: /* no key */
492 if (ret)
493 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100494 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700495 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100496 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
David Howells3e301482005-06-23 22:00:56 -0700498 default:
David Howells664cceb2005-09-28 17:03:15 +0100499 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700500 break;
501 }
David Howells3e301482005-06-23 22:00:56 -0700502 }
503 /* or search the user-session keyring */
David Howells0f44e4d2019-06-26 21:02:32 +0100504 else if ((user_session = get_user_session_keyring_rcu(cred))) {
505 key_ref = keyring_search_rcu(make_key_ref(user_session, 1),
506 ctx);
507 key_put(user_session);
508
David Howells664cceb2005-09-28 17:03:15 +0100509 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700510 goto found;
511
David Howells664cceb2005-09-28 17:03:15 +0100512 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700513 case -EAGAIN: /* no key */
514 if (ret)
515 break;
Mathieu Malaterre0f949bc2019-01-14 21:17:24 +0100516 /* fall through */
David Howells3e301482005-06-23 22:00:56 -0700517 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100518 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700519 break;
520 default:
David Howells664cceb2005-09-28 17:03:15 +0100521 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700522 break;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
David Howells927942a2010-06-11 17:31:10 +0100526 /* no key - decide on the error we're going to go for */
527 key_ref = ret ? ret : err;
528
529found:
530 return key_ref;
531}
532
David Howells927942a2010-06-11 17:31:10 +0100533/*
David Howells973c9f42011-01-20 16:38:33 +0000534 * Search the process keyrings attached to the supplied cred for the first
535 * matching key in the manner of search_my_process_keyrings(), but also search
536 * the keys attached to the assumed authorisation key using its credentials if
537 * one is available.
538 *
David Howellse59428f2019-06-19 16:10:15 +0100539 * The caller must be holding the RCU read lock.
540 *
541 * Return same as search_cred_keyrings_rcu().
David Howells927942a2010-06-11 17:31:10 +0100542 */
David Howellse59428f2019-06-19 16:10:15 +0100543key_ref_t search_process_keyrings_rcu(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100544{
545 struct request_key_auth *rka;
546 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
547
David Howellse59428f2019-06-19 16:10:15 +0100548 key_ref = search_cred_keyrings_rcu(ctx);
David Howells927942a2010-06-11 17:31:10 +0100549 if (!IS_ERR(key_ref))
550 goto found;
551 err = key_ref;
552
David Howellsb5f545c2006-01-08 01:02:47 -0800553 /* if this process has an instantiation authorisation key, then we also
554 * search the keyrings of the process mentioned there
555 * - we don't permit access to request_key auth keys via this method
556 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100557 if (ctx->cred->request_key_auth &&
558 ctx->cred == current_cred() &&
559 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800560 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100561 const struct cred *cred = ctx->cred;
562
David Howellse59428f2019-06-19 16:10:15 +0100563 if (key_validate(cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100564 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800565
David Howellse59428f2019-06-19 16:10:15 +0100566 //// was search_process_keyrings() [ie. recursive]
David Howells4bdf0bc2013-09-24 10:35:15 +0100567 ctx->cred = rka->cred;
David Howellse59428f2019-06-19 16:10:15 +0100568 key_ref = search_cred_keyrings_rcu(ctx);
David Howells4bdf0bc2013-09-24 10:35:15 +0100569 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800570
David Howells04c567d2006-06-22 14:47:18 -0700571 if (!IS_ERR(key_ref))
572 goto found;
David Howells927942a2010-06-11 17:31:10 +0100573 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800574 }
575 }
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100578 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
579 key_ref = ERR_PTR(-ENOKEY);
580 else if (err == ERR_PTR(-EACCES))
581 key_ref = ret;
582 else
583 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
David Howells3e301482005-06-23 22:00:56 -0700585found:
David Howells664cceb2005-09-28 17:03:15 +0100586 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000587}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/*
David Howells973c9f42011-01-20 16:38:33 +0000589 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100590 */
David Howells0c903ab2014-09-16 17:36:08 +0100591bool lookup_user_key_possessed(const struct key *key,
592 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100593{
David Howells46291952014-09-16 17:36:02 +0100594 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000595}
David Howells664cceb2005-09-28 17:03:15 +0100596
David Howells664cceb2005-09-28 17:03:15 +0100597/*
David Howells973c9f42011-01-20 16:38:33 +0000598 * Look up a key ID given us by userspace with a given permissions mask to get
599 * the key it refers to.
600 *
601 * Flags can be passed to request that special keyrings be created if referred
602 * to directly, to permit partially constructed keys to be found and to skip
603 * validity and permission checks on the found key.
604 *
605 * Returns a pointer to the key with an incremented usage count if successful;
606 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
607 * to a key or the best found key was a negative key; -EKEYREVOKED or
608 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
609 * found key doesn't grant the requested permit or the LSM denied access to it;
610 * or -ENOMEM if a special keyring couldn't be created.
611 *
612 * In the case of a successful return, the possession attribute is set on the
613 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 */
David Howells55931222009-09-02 09:13:45 +0100615key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100616 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
David Howells4bdf0bc2013-09-24 10:35:15 +0100618 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100619 .match_data.cmp = lookup_user_key_possessed,
620 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100621 .flags = (KEYRING_SEARCH_NO_STATE_CHECK |
622 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100623 };
David Howells8bbf49762008-11-14 10:39:14 +1100624 struct request_key_auth *rka;
David Howells0f44e4d2019-06-26 21:02:32 +0100625 struct key *key, *user_session;
David Howellsb6dff3e2008-11-14 10:39:16 +1100626 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int ret;
628
David Howellsbb952bb2008-11-14 10:39:20 +1100629try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100630 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100631 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 switch (id) {
634 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100635 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100636 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto error;
638
David Howells8bbf49762008-11-14 10:39:14 +1100639 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100641 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto error;
643 }
David Howellsbb952bb2008-11-14 10:39:20 +1100644 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
David Howells4bdf0bc2013-09-24 10:35:15 +0100647 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100648 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100649 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 break;
651
652 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100653 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100654 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 goto error;
656
David Howells8bbf49762008-11-14 10:39:14 +1100657 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100659 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto error;
661 }
David Howellsbb952bb2008-11-14 10:39:20 +1100662 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
David Howells4bdf0bc2013-09-24 10:35:15 +0100665 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100666 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100667 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 break;
669
670 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100671 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* always install a session keyring upon access if one
673 * doesn't exist yet */
David Howells0f44e4d2019-06-26 21:02:32 +0100674 ret = look_up_user_keyrings(NULL, &user_session);
David Howells69664cf2008-04-29 01:01:31 -0700675 if (ret < 0)
676 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100677 if (lflags & KEY_LOOKUP_CREATE)
678 ret = join_session_keyring(NULL);
679 else
David Howells0f44e4d2019-06-26 21:02:32 +0100680 ret = install_session_keyring(user_session);
David Howellsd84f4f92008-11-14 10:39:23 +1100681
David Howells0f44e4d2019-06-26 21:02:32 +0100682 key_put(user_session);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ret < 0)
684 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100685 goto reget_creds;
David Howells0f44e4d2019-06-26 21:02:32 +0100686 } else if (test_bit(KEY_FLAG_UID_KEYRING,
687 &ctx.cred->session_keyring->flags) &&
David Howells3ecf1b42011-08-22 14:08:43 +0100688 lflags & KEY_LOOKUP_CREATE) {
689 ret = join_session_keyring(NULL);
690 if (ret < 0)
691 goto error;
692 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
Jann Horn5c7e3722019-03-27 16:39:38 +0100695 key = ctx.cred->session_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_USER_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100701 ret = look_up_user_keyrings(&key, NULL);
702 if (ret < 0)
703 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100704 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706
707 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells0f44e4d2019-06-26 21:02:32 +0100708 ret = look_up_user_keyrings(NULL, &key);
709 if (ret < 0)
710 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100711 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 break;
713
714 case KEY_SPEC_GROUP_KEYRING:
715 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100716 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 goto error;
718
David Howellsb5f545c2006-01-08 01:02:47 -0800719 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100720 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800721 if (!key)
722 goto error;
723
David Howellsccc3e6d2013-09-24 10:35:16 +0100724 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800725 key_ref = make_key_ref(key, 1);
726 break;
727
David Howells8bbf49762008-11-14 10:39:14 +1100728 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100729 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100730 goto error;
731
David Howells4bdf0bc2013-09-24 10:35:15 +0100732 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000733 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100734 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100735 key_ref = ERR_PTR(-EKEYREVOKED);
736 key = NULL;
737 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100738 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100739 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100740 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100741 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100742 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100743 if (!key)
744 goto error;
745 key_ref = make_key_ref(key, 1);
746 break;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 default:
David Howells664cceb2005-09-28 17:03:15 +0100749 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (id < 1)
751 goto error;
752
753 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100754 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800755 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100757 }
758
759 key_ref = make_key_ref(key, 0);
760
761 /* check to see if we possess the key */
Eric Biggers47546202019-05-29 14:01:52 -0700762 ctx.index_key = key->index_key;
David Howells46291952014-09-16 17:36:02 +0100763 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100764 kdebug("check possessed");
David Howellse59428f2019-06-19 16:10:15 +0100765 rcu_read_lock();
766 skey_ref = search_process_keyrings_rcu(&ctx);
767 rcu_read_unlock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100768 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100769
770 if (!IS_ERR(skey_ref)) {
771 key_put(key);
772 key_ref = skey_ref;
773 }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 }
777
David Howells55931222009-09-02 09:13:45 +0100778 /* unlink does not use the nominated key in any way, so can skip all
779 * the permission checks as it is only concerned with the keyring */
780 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
781 ret = 0;
782 goto error;
783 }
784
785 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700786 ret = wait_for_key_construction(key, true);
787 switch (ret) {
788 case -ERESTARTSYS:
789 goto invalid_key;
790 default:
791 if (perm)
792 goto invalid_key;
793 case 0:
794 break;
795 }
796 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 ret = key_validate(key);
798 if (ret < 0)
799 goto invalid_key;
800 }
801
802 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100803 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
David Howells363b02d2017-10-04 16:43:25 +0100804 key_read_state(key) == KEY_IS_UNINSTANTIATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto invalid_key;
806
David Howells3e301482005-06-23 22:00:56 -0700807 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100808 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800809 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 goto invalid_key;
811
Baolin Wang074d5892017-11-15 16:38:45 +0000812 key->last_used_at = ktime_get_real_seconds();
David Howells31d5a792012-05-11 10:56:56 +0100813
David Howells664cceb2005-09-28 17:03:15 +0100814error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100815 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100816 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
David Howells664cceb2005-09-28 17:03:15 +0100818invalid_key:
819 key_ref_put(key_ref);
820 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto error;
822
David Howellsbb952bb2008-11-14 10:39:20 +1100823 /* if we attempted to install a keyring, then it may have caused new
824 * creds to be installed */
825reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100826 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100827 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000828}
Dave Jiang76ef5e12018-12-04 10:31:27 -0800829EXPORT_SYMBOL(lookup_user_key);
David Howellsbb952bb2008-11-14 10:39:20 +1100830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831/*
David Howells973c9f42011-01-20 16:38:33 +0000832 * Join the named keyring as the session keyring if possible else attempt to
833 * create a new one of that name and join that.
834 *
835 * If the name is NULL, an empty anonymous keyring will be installed as the
836 * session keyring.
837 *
838 * Named session keyrings are joined with a semaphore held to prevent the
839 * keyrings from going away whilst the attempt is made to going them and also
840 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 */
842long join_session_keyring(const char *name)
843{
David Howellsd84f4f92008-11-14 10:39:23 +1100844 const struct cred *old;
845 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100847 long ret, serial;
848
David Howellsd84f4f92008-11-14 10:39:23 +1100849 new = prepare_creds();
850 if (!new)
851 return -ENOMEM;
852 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* if no name is provided, install an anonymous keyring */
855 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100856 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (ret < 0)
858 goto error;
859
David Howells3a505972012-10-02 19:24:29 +0100860 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100861 ret = commit_creds(new);
862 if (ret == 0)
863 ret = serial;
864 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866
867 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800868 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700871 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (PTR_ERR(keyring) == -ENOKEY) {
873 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100874 keyring = keyring_alloc(
875 name, old->uid, old->gid, old,
876 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100877 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (IS_ERR(keyring)) {
879 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700880 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
David Howellsd84f4f92008-11-14 10:39:23 +1100882 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 ret = PTR_ERR(keyring);
884 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100885 } else if (keyring == new->session_keyring) {
886 ret = 0;
Eric Biggersd636bd92017-06-08 14:48:03 +0100887 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889
890 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100891 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ret < 0)
Eric Biggersd636bd92017-06-08 14:48:03 +0100893 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
David Howellsd84f4f92008-11-14 10:39:23 +1100895 commit_creds(new);
896 mutex_unlock(&key_session_mutex);
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 ret = keyring->serial;
899 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100900okay:
901 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Eric Biggersd636bd92017-06-08 14:48:03 +0100903error3:
904 key_put(keyring);
David Howells664cceb2005-09-28 17:03:15 +0100905error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800906 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100907error:
David Howellsd84f4f92008-11-14 10:39:23 +1100908 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100910}
David Howellsee18d642009-09-02 09:14:21 +0100911
912/*
David Howells973c9f42011-01-20 16:38:33 +0000913 * Replace a process's session keyring on behalf of one of its children when
914 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100915 */
Al Viro67d12142012-06-27 11:07:19 +0400916void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100917{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000918 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400919 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100920
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000921 if (unlikely(current->flags & PF_EXITING)) {
922 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100923 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000924 }
David Howellsee18d642009-09-02 09:14:21 +0100925
David Howellsee18d642009-09-02 09:14:21 +0100926 new-> uid = old-> uid;
927 new-> euid = old-> euid;
928 new-> suid = old-> suid;
929 new->fsuid = old->fsuid;
930 new-> gid = old-> gid;
931 new-> egid = old-> egid;
932 new-> sgid = old-> sgid;
933 new->fsgid = old->fsgid;
934 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800935 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100936 new->group_info = get_group_info(old->group_info);
937
938 new->securebits = old->securebits;
939 new->cap_inheritable = old->cap_inheritable;
940 new->cap_permitted = old->cap_permitted;
941 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700942 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100943 new->cap_bset = old->cap_bset;
944
945 new->jit_keyring = old->jit_keyring;
946 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100947 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100948
949 security_transfer_creds(new, old);
950
951 commit_creds(new);
952}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100953
954/*
955 * Make sure that root's user and user-session keyrings exist.
956 */
957static int __init init_root_keyring(void)
958{
David Howells0f44e4d2019-06-26 21:02:32 +0100959 return look_up_user_keyrings(NULL, NULL);
Mimi Zoharc124bde2013-09-04 13:26:22 +0100960}
961
962late_initcall(init_root_keyring);