blob: b6fdd22205b169b663cdb00aecd5d214c7a376dd [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
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010015#include <linux/sched/user.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/keyctl.h>
17#include <linux/fs.h>
18#include <linux/err.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080019#include <linux/mutex.h>
David Howellsee18d642009-09-02 09:14:21 +010020#include <linux/security.h>
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060021#include <linux/user_namespace.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080022#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "internal.h"
24
David Howells973c9f42011-01-20 16:38:33 +000025/* Session keyring create vs join semaphore */
Ingo Molnarbb0030792006-03-22 00:09:14 -080026static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells973c9f42011-01-20 16:38:33 +000028/* User keyring creation semaphore */
David Howells69664cf2008-04-29 01:01:31 -070029static DEFINE_MUTEX(key_user_keyring_mutex);
30
David Howells973c9f42011-01-20 16:38:33 +000031/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct key_user root_key_user = {
33 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070034 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080035 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .nkeys = ATOMIC_INIT(2),
37 .nikeys = ATOMIC_INIT(2),
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080038 .uid = GLOBAL_ROOT_UID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*
David Howells973c9f42011-01-20 16:38:33 +000042 * Install the user and user session keyrings for the current process's UID.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
David Howells8bbf49762008-11-14 10:39:14 +110044int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
David Howellsd84f4f92008-11-14 10:39:23 +110046 struct user_struct *user;
47 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct key *uid_keyring, *session_keyring;
David Howells96b5c8f2012-10-02 19:24:56 +010049 key_perm_t user_keyring_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 char buf[20];
51 int ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080052 uid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
David Howells96b5c8f2012-10-02 19:24:56 +010054 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
David Howellsd84f4f92008-11-14 10:39:23 +110055 cred = current_cred();
56 user = cred->user;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080057 uid = from_kuid(cred->user_ns, user->uid);
David Howellsd84f4f92008-11-14 10:39:23 +110058
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080059 kenter("%p{%u}", user, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
David Howells0da9dfd2013-03-12 16:44:31 +110061 if (user->uid_keyring && user->session_keyring) {
David Howells69664cf2008-04-29 01:01:31 -070062 kleave(" = 0 [exist]");
63 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 }
65
David Howells69664cf2008-04-29 01:01:31 -070066 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 ret = 0;
68
David Howells69664cf2008-04-29 01:01:31 -070069 if (!user->uid_keyring) {
70 /* get the UID-specific keyring
71 * - there may be one in existence already as it may have been
72 * pinned by a session, but the user_struct pointing to it
73 * may have been destroyed by setuid */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080074 sprintf(buf, "_uid.%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David Howells69664cf2008-04-29 01:01:31 -070076 uid_keyring = find_keyring_by_name(buf, true);
77 if (IS_ERR(uid_keyring)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080078 uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010079 cred, user_keyring_perm,
David Howells5ac7eac2016-04-06 16:14:24 +010080 KEY_ALLOC_IN_QUOTA,
81 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070082 if (IS_ERR(uid_keyring)) {
83 ret = PTR_ERR(uid_keyring);
84 goto error;
85 }
86 }
87
88 /* get a default session keyring (which might also exist
89 * already) */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080090 sprintf(buf, "_uid_ses.%u", uid);
David Howells69664cf2008-04-29 01:01:31 -070091
92 session_keyring = find_keyring_by_name(buf, true);
93 if (IS_ERR(session_keyring)) {
94 session_keyring =
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080095 keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010096 cred, user_keyring_perm,
David Howells5ac7eac2016-04-06 16:14:24 +010097 KEY_ALLOC_IN_QUOTA,
98 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070099 if (IS_ERR(session_keyring)) {
100 ret = PTR_ERR(session_keyring);
101 goto error_release;
102 }
103
104 /* we install a link from the user session keyring to
105 * the user keyring */
106 ret = key_link(session_keyring, uid_keyring);
107 if (ret < 0)
108 goto error_release_both;
109 }
110
111 /* install the keyrings */
112 user->uid_keyring = uid_keyring;
113 user->session_keyring = session_keyring;
114 }
115
116 mutex_unlock(&key_user_keyring_mutex);
117 kleave(" = 0");
118 return 0;
119
120error_release_both:
121 key_put(session_keyring);
122error_release:
123 key_put(uid_keyring);
124error:
125 mutex_unlock(&key_user_keyring_mutex);
126 kleave(" = %d", ret);
127 return ret;
128}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
David Howells973c9f42011-01-20 16:38:33 +0000131 * Install a fresh thread keyring directly to new credentials. This keyring is
132 * allowed to overrun the quota.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
David Howellsd84f4f92008-11-14 10:39:23 +1100134int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
David Howellsd84f4f92008-11-14 10:39:23 +1100136 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
David Howellsd84f4f92008-11-14 10:39:23 +1100138 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100139 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100140 KEY_ALLOC_QUOTA_OVERRUN,
141 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100142 if (IS_ERR(keyring))
143 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Howellsd84f4f92008-11-14 10:39:23 +1100145 new->thread_keyring = keyring;
146 return 0;
147}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
David Howells973c9f42011-01-20 16:38:33 +0000150 * Install a fresh thread keyring, discarding the old one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 */
David Howellsd84f4f92008-11-14 10:39:23 +1100152static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
David Howellsd84f4f92008-11-14 10:39:23 +1100154 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 int ret;
156
David Howellsd84f4f92008-11-14 10:39:23 +1100157 new = prepare_creds();
158 if (!new)
159 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161 BUG_ON(new->thread_keyring);
162
163 ret = install_thread_keyring_to_cred(new);
164 if (ret < 0) {
165 abort_creds(new);
166 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
David Howellsd84f4f92008-11-14 10:39:23 +1100169 return commit_creds(new);
170}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
David Howellsd84f4f92008-11-14 10:39:23 +1100172/*
David Howells973c9f42011-01-20 16:38:33 +0000173 * Install a process keyring directly to a credentials struct.
174 *
175 * Returns -EEXIST if there was already a process keyring, 0 if one installed,
176 * and other value on any other error
David Howellsd84f4f92008-11-14 10:39:23 +1100177 */
178int install_process_keyring_to_cred(struct cred *new)
179{
180 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David Howells3a505972012-10-02 19:24:29 +0100182 if (new->process_keyring)
David Howellsd84f4f92008-11-14 10:39:23 +1100183 return -EEXIST;
184
David Howells96b5c8f2012-10-02 19:24:56 +0100185 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
186 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100187 KEY_ALLOC_QUOTA_OVERRUN,
188 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100189 if (IS_ERR(keyring))
190 return PTR_ERR(keyring);
191
David Howells3a505972012-10-02 19:24:29 +0100192 new->process_keyring = keyring;
193 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
David Howells973c9f42011-01-20 16:38:33 +0000197 * Make sure a process keyring is installed for the current process. The
198 * existing process keyring is not replaced.
199 *
200 * Returns 0 if there is a process keyring by the end of this function, some
201 * error otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
David Howellsd84f4f92008-11-14 10:39:23 +1100203static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David Howellsd84f4f92008-11-14 10:39:23 +1100205 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 int ret;
207
David Howellsd84f4f92008-11-14 10:39:23 +1100208 new = prepare_creds();
209 if (!new)
210 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700211
David Howellsd84f4f92008-11-14 10:39:23 +1100212 ret = install_process_keyring_to_cred(new);
213 if (ret < 0) {
214 abort_creds(new);
Andi Kleen27d63792010-10-28 13:16:13 +0100215 return ret != -EEXIST ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
David Howellsd84f4f92008-11-14 10:39:23 +1100218 return commit_creds(new);
219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
David Howells973c9f42011-01-20 16:38:33 +0000222 * Install a session keyring directly to a credentials struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700224int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
David Howells7e047ef2006-06-26 00:24:50 -0700226 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700228
229 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* create an empty session keyring */
232 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700233 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100234 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700235 flags = KEY_ALLOC_IN_QUOTA;
236
David Howells96b5c8f2012-10-02 19:24:56 +0100237 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
238 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100239 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700240 if (IS_ERR(keyring))
241 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100242 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100243 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100247 old = cred->session_keyring;
248 rcu_assign_pointer(cred->session_keyring, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
David Howells3a505972012-10-02 19:24:29 +0100250 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700251 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Howells1a26feb2006-04-10 22:54:26 -0700253 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
David Howells973c9f42011-01-20 16:38:33 +0000257 * Install a session keyring, discarding the old one. If a keyring is not
258 * supplied, an empty one is invented.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
David Howellsd84f4f92008-11-14 10:39:23 +1100260static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howellsd84f4f92008-11-14 10:39:23 +1100262 struct cred *new;
263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
David Howellsd84f4f92008-11-14 10:39:23 +1100265 new = prepare_creds();
266 if (!new)
267 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800268
David Howells995995372011-08-22 14:08:33 +0100269 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100270 if (ret < 0) {
271 abort_creds(new);
272 return ret;
273 }
David Howellsb5f545c2006-01-08 01:02:47 -0800274
David Howellsd84f4f92008-11-14 10:39:23 +1100275 return commit_creds(new);
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
David Howells973c9f42011-01-20 16:38:33 +0000279 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281void key_fsuid_changed(struct task_struct *tsk)
282{
283 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100284 BUG_ON(!tsk->cred);
285 if (tsk->cred->thread_keyring) {
286 down_write(&tsk->cred->thread_keyring->sem);
287 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
288 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292/*
David Howells973c9f42011-01-20 16:38:33 +0000293 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 */
295void key_fsgid_changed(struct task_struct *tsk)
296{
297 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100298 BUG_ON(!tsk->cred);
299 if (tsk->cred->thread_keyring) {
300 down_write(&tsk->cred->thread_keyring->sem);
301 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
302 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
David Howells973c9f42011-01-20 16:38:33 +0000307 * Search the process keyrings attached to the supplied cred for the first
308 * matching key.
309 *
310 * The search criteria are the type and the match function. The description is
311 * given to the match function as a parameter, but doesn't otherwise influence
312 * the search. Typically the match function will compare the description
313 * parameter to the key's description.
314 *
315 * This can only search keyrings that grant Search permission to the supplied
316 * credentials. Keyrings linked to searched keyrings will also be searched if
317 * they grant Search permission too. Keys can only be found if they grant
318 * Search permission to the credentials.
319 *
320 * Returns a pointer to the key with the key usage count incremented if
321 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
322 * matched negative keys.
323 *
324 * In the case of a successful return, the possession attribute is set on the
325 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100327key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
David Howellsb5f545c2006-01-08 01:02:47 -0800329 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
332 * searchable, but we failed to find a key or we found a negative key;
333 * otherwise we want to return a sample error (probably -EACCES) if
334 * none of the keyrings were searchable
335 *
336 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
337 */
David Howells664cceb2005-09-28 17:03:15 +0100338 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 ret = NULL;
340 err = ERR_PTR(-EAGAIN);
341
342 /* search the thread keyring first */
David Howells4bdf0bc2013-09-24 10:35:15 +0100343 if (ctx->cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100344 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100345 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100346 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto found;
348
David Howells664cceb2005-09-28 17:03:15 +0100349 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100352 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 default:
David Howells664cceb2005-09-28 17:03:15 +0100355 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358 }
359
360 /* search the process keyring second */
David Howells4bdf0bc2013-09-24 10:35:15 +0100361 if (ctx->cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100362 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100363 make_key_ref(ctx->cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100364 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto found;
366
David Howells664cceb2005-09-28 17:03:15 +0100367 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000369 if (ret)
370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100372 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 default:
David Howells664cceb2005-09-28 17:03:15 +0100375 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 }
378 }
379
David Howells3e301482005-06-23 22:00:56 -0700380 /* search the session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100381 if (ctx->cred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700382 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100383 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100384 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
385 ctx);
David Howells8589b4e2005-06-23 22:00:53 -0700386 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David Howells664cceb2005-09-28 17:03:15 +0100388 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700389 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David Howells664cceb2005-09-28 17:03:15 +0100391 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700392 case -EAGAIN: /* no key */
393 if (ret)
394 break;
395 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100396 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
David Howells3e301482005-06-23 22:00:56 -0700398 default:
David Howells664cceb2005-09-28 17:03:15 +0100399 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700400 break;
401 }
David Howells3e301482005-06-23 22:00:56 -0700402 }
403 /* or search the user-session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100404 else if (ctx->cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100405 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100406 make_key_ref(ctx->cred->user->session_keyring, 1),
407 ctx);
David Howells664cceb2005-09-28 17:03:15 +0100408 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700409 goto found;
410
David Howells664cceb2005-09-28 17:03:15 +0100411 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700412 case -EAGAIN: /* no key */
413 if (ret)
414 break;
415 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100416 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700417 break;
418 default:
David Howells664cceb2005-09-28 17:03:15 +0100419 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700420 break;
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
David Howells927942a2010-06-11 17:31:10 +0100424 /* no key - decide on the error we're going to go for */
425 key_ref = ret ? ret : err;
426
427found:
428 return key_ref;
429}
430
David Howells927942a2010-06-11 17:31:10 +0100431/*
David Howells973c9f42011-01-20 16:38:33 +0000432 * Search the process keyrings attached to the supplied cred for the first
433 * matching key in the manner of search_my_process_keyrings(), but also search
434 * the keys attached to the assumed authorisation key using its credentials if
435 * one is available.
436 *
437 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100438 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100439key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100440{
441 struct request_key_auth *rka;
442 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
443
444 might_sleep();
445
David Howells4bdf0bc2013-09-24 10:35:15 +0100446 key_ref = search_my_process_keyrings(ctx);
David Howells927942a2010-06-11 17:31:10 +0100447 if (!IS_ERR(key_ref))
448 goto found;
449 err = key_ref;
450
David Howellsb5f545c2006-01-08 01:02:47 -0800451 /* if this process has an instantiation authorisation key, then we also
452 * search the keyrings of the process mentioned there
453 * - we don't permit access to request_key auth keys via this method
454 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100455 if (ctx->cred->request_key_auth &&
456 ctx->cred == current_cred() &&
457 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800458 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100459 const struct cred *cred = ctx->cred;
460
David Howells04c567d2006-06-22 14:47:18 -0700461 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100462 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700463
David Howells4bdf0bc2013-09-24 10:35:15 +0100464 if (key_validate(ctx->cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100465 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800466
David Howells4bdf0bc2013-09-24 10:35:15 +0100467 ctx->cred = rka->cred;
468 key_ref = search_process_keyrings(ctx);
469 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800470
David Howellsc69e8d92008-11-14 10:39:19 +1100471 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700472
473 if (!IS_ERR(key_ref))
474 goto found;
475
David Howells927942a2010-06-11 17:31:10 +0100476 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700477 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100478 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800479 }
480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100483 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
484 key_ref = ERR_PTR(-ENOKEY);
485 else if (err == ERR_PTR(-EACCES))
486 key_ref = ret;
487 else
488 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
David Howells3e301482005-06-23 22:00:56 -0700490found:
David Howells664cceb2005-09-28 17:03:15 +0100491 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000492}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494/*
David Howells973c9f42011-01-20 16:38:33 +0000495 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100496 */
David Howells0c903ab2014-09-16 17:36:08 +0100497bool lookup_user_key_possessed(const struct key *key,
498 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100499{
David Howells46291952014-09-16 17:36:02 +0100500 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000501}
David Howells664cceb2005-09-28 17:03:15 +0100502
David Howells664cceb2005-09-28 17:03:15 +0100503/*
David Howells973c9f42011-01-20 16:38:33 +0000504 * Look up a key ID given us by userspace with a given permissions mask to get
505 * the key it refers to.
506 *
507 * Flags can be passed to request that special keyrings be created if referred
508 * to directly, to permit partially constructed keys to be found and to skip
509 * validity and permission checks on the found key.
510 *
511 * Returns a pointer to the key with an incremented usage count if successful;
512 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
513 * to a key or the best found key was a negative key; -EKEYREVOKED or
514 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
515 * found key doesn't grant the requested permit or the LSM denied access to it;
516 * or -ENOMEM if a special keyring couldn't be created.
517 *
518 * In the case of a successful return, the possession attribute is set on the
519 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
David Howells55931222009-09-02 09:13:45 +0100521key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100522 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
David Howells4bdf0bc2013-09-24 10:35:15 +0100524 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100525 .match_data.cmp = lookup_user_key_possessed,
526 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
527 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100528 };
David Howells8bbf49762008-11-14 10:39:14 +1100529 struct request_key_auth *rka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100531 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 int ret;
533
David Howellsbb952bb2008-11-14 10:39:20 +1100534try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100535 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100536 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 switch (id) {
539 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100540 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100541 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto error;
543
David Howells8bbf49762008-11-14 10:39:14 +1100544 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100546 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto error;
548 }
David Howellsbb952bb2008-11-14 10:39:20 +1100549 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
David Howells4bdf0bc2013-09-24 10:35:15 +0100552 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100553 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100554 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
556
557 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100558 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100559 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto error;
561
David Howells8bbf49762008-11-14 10:39:14 +1100562 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100564 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto error;
566 }
David Howellsbb952bb2008-11-14 10:39:20 +1100567 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
David Howells4bdf0bc2013-09-24 10:35:15 +0100570 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100571 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100572 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574
575 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100576 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 /* always install a session keyring upon access if one
578 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100579 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700580 if (ret < 0)
581 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100582 if (lflags & KEY_LOOKUP_CREATE)
583 ret = join_session_keyring(NULL);
584 else
585 ret = install_session_keyring(
David Howells4bdf0bc2013-09-24 10:35:15 +0100586 ctx.cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (ret < 0)
589 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100590 goto reget_creds;
David Howells4bdf0bc2013-09-24 10:35:15 +0100591 } else if (ctx.cred->session_keyring ==
592 ctx.cred->user->session_keyring &&
David Howells3ecf1b42011-08-22 14:08:43 +0100593 lflags & KEY_LOOKUP_CREATE) {
594 ret = join_session_keyring(NULL);
595 if (ret < 0)
596 goto error;
597 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
David Howells3e301482005-06-23 22:00:56 -0700600 rcu_read_lock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100601 key = rcu_dereference(ctx.cred->session_keyring);
David Howellsccc3e6d2013-09-24 10:35:16 +0100602 __key_get(key);
David Howells3e301482005-06-23 22:00:56 -0700603 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100604 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 break;
606
607 case KEY_SPEC_USER_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100608 if (!ctx.cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100609 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700610 if (ret < 0)
611 goto error;
612 }
613
David Howells4bdf0bc2013-09-24 10:35:15 +0100614 key = ctx.cred->user->uid_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100615 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100616 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
618
619 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100620 if (!ctx.cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100621 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700622 if (ret < 0)
623 goto error;
624 }
625
David Howells4bdf0bc2013-09-24 10:35:15 +0100626 key = ctx.cred->user->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100627 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100628 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 break;
630
631 case KEY_SPEC_GROUP_KEYRING:
632 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100633 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto error;
635
David Howellsb5f545c2006-01-08 01:02:47 -0800636 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100637 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800638 if (!key)
639 goto error;
640
David Howellsccc3e6d2013-09-24 10:35:16 +0100641 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800642 key_ref = make_key_ref(key, 1);
643 break;
644
David Howells8bbf49762008-11-14 10:39:14 +1100645 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100646 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100647 goto error;
648
David Howells4bdf0bc2013-09-24 10:35:15 +0100649 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000650 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100651 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100652 key_ref = ERR_PTR(-EKEYREVOKED);
653 key = NULL;
654 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100655 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100656 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100657 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100658 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100659 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100660 if (!key)
661 goto error;
662 key_ref = make_key_ref(key, 1);
663 break;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 default:
David Howells664cceb2005-09-28 17:03:15 +0100666 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (id < 1)
668 goto error;
669
670 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100671 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800672 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100674 }
675
676 key_ref = make_key_ref(key, 0);
677
678 /* check to see if we possess the key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100679 ctx.index_key.type = key->type;
680 ctx.index_key.description = key->description;
681 ctx.index_key.desc_len = strlen(key->description);
David Howells46291952014-09-16 17:36:02 +0100682 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100683 kdebug("check possessed");
684 skey_ref = search_process_keyrings(&ctx);
685 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100686
687 if (!IS_ERR(skey_ref)) {
688 key_put(key);
689 key_ref = skey_ref;
690 }
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 }
694
David Howells55931222009-09-02 09:13:45 +0100695 /* unlink does not use the nominated key in any way, so can skip all
696 * the permission checks as it is only concerned with the keyring */
697 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
698 ret = 0;
699 goto error;
700 }
701
702 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700703 ret = wait_for_key_construction(key, true);
704 switch (ret) {
705 case -ERESTARTSYS:
706 goto invalid_key;
707 default:
708 if (perm)
709 goto invalid_key;
710 case 0:
711 break;
712 }
713 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret = key_validate(key);
715 if (ret < 0)
716 goto invalid_key;
717 }
718
719 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100720 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
721 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto invalid_key;
723
David Howells3e301482005-06-23 22:00:56 -0700724 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100725 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800726 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 goto invalid_key;
728
David Howells31d5a792012-05-11 10:56:56 +0100729 key->last_used_at = current_kernel_time().tv_sec;
730
David Howells664cceb2005-09-28 17:03:15 +0100731error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100732 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100733 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
David Howells664cceb2005-09-28 17:03:15 +0100735invalid_key:
736 key_ref_put(key_ref);
737 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto error;
739
David Howellsbb952bb2008-11-14 10:39:20 +1100740 /* if we attempted to install a keyring, then it may have caused new
741 * creds to be installed */
742reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100743 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100744 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000745}
David Howellsbb952bb2008-11-14 10:39:20 +1100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
David Howells973c9f42011-01-20 16:38:33 +0000748 * Join the named keyring as the session keyring if possible else attempt to
749 * create a new one of that name and join that.
750 *
751 * If the name is NULL, an empty anonymous keyring will be installed as the
752 * session keyring.
753 *
754 * Named session keyrings are joined with a semaphore held to prevent the
755 * keyrings from going away whilst the attempt is made to going them and also
756 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 */
758long join_session_keyring(const char *name)
759{
David Howellsd84f4f92008-11-14 10:39:23 +1100760 const struct cred *old;
761 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100763 long ret, serial;
764
David Howellsd84f4f92008-11-14 10:39:23 +1100765 new = prepare_creds();
766 if (!new)
767 return -ENOMEM;
768 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* if no name is provided, install an anonymous keyring */
771 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100772 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (ret < 0)
774 goto error;
775
David Howells3a505972012-10-02 19:24:29 +0100776 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100777 ret = commit_creds(new);
778 if (ret == 0)
779 ret = serial;
780 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
782
783 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800784 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700787 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (PTR_ERR(keyring) == -ENOKEY) {
789 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100790 keyring = keyring_alloc(
791 name, old->uid, old->gid, old,
792 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100793 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (IS_ERR(keyring)) {
795 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700796 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
David Howellsd84f4f92008-11-14 10:39:23 +1100798 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 ret = PTR_ERR(keyring);
800 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100801 } else if (keyring == new->session_keyring) {
Yevgeny Pats23567fd2016-01-19 22:09:04 +0000802 key_put(keyring);
David Howells3a505972012-10-02 19:24:29 +0100803 ret = 0;
804 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
807 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100808 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (ret < 0)
810 goto error2;
811
David Howellsd84f4f92008-11-14 10:39:23 +1100812 commit_creds(new);
813 mutex_unlock(&key_session_mutex);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 ret = keyring->serial;
816 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100817okay:
818 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
David Howells664cceb2005-09-28 17:03:15 +0100820error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800821 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100822error:
David Howellsd84f4f92008-11-14 10:39:23 +1100823 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100825}
David Howellsee18d642009-09-02 09:14:21 +0100826
827/*
David Howells973c9f42011-01-20 16:38:33 +0000828 * Replace a process's session keyring on behalf of one of its children when
829 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100830 */
Al Viro67d12142012-06-27 11:07:19 +0400831void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100832{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000833 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400834 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100835
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000836 if (unlikely(current->flags & PF_EXITING)) {
837 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100838 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000839 }
David Howellsee18d642009-09-02 09:14:21 +0100840
David Howellsee18d642009-09-02 09:14:21 +0100841 new-> uid = old-> uid;
842 new-> euid = old-> euid;
843 new-> suid = old-> suid;
844 new->fsuid = old->fsuid;
845 new-> gid = old-> gid;
846 new-> egid = old-> egid;
847 new-> sgid = old-> sgid;
848 new->fsgid = old->fsgid;
849 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800850 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100851 new->group_info = get_group_info(old->group_info);
852
853 new->securebits = old->securebits;
854 new->cap_inheritable = old->cap_inheritable;
855 new->cap_permitted = old->cap_permitted;
856 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700857 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100858 new->cap_bset = old->cap_bset;
859
860 new->jit_keyring = old->jit_keyring;
861 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100862 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100863
864 security_transfer_creds(new, old);
865
866 commit_creds(new);
867}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100868
869/*
870 * Make sure that root's user and user-session keyrings exist.
871 */
872static int __init init_root_keyring(void)
873{
874 return install_user_keyrings();
875}
876
877late_initcall(init_root_keyring);