blob: 2d35d71d7d9a2d90e0168b459c862034835b80f2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/keyctl.h>
16#include <linux/fs.h>
17#include <linux/err.h>
Ingo Molnarbb003072006-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 Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22#include "internal.h"
23
David Howells973c9f42011-01-20 16:38:33 +000024/* Session keyring create vs join semaphore */
Ingo Molnarbb003072006-03-22 00:09:14 -080025static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells973c9f42011-01-20 16:38:33 +000027/* User keyring creation semaphore */
David Howells69664cf2008-04-29 01:01:31 -070028static DEFINE_MUTEX(key_user_keyring_mutex);
29
David Howells973c9f42011-01-20 16:38:33 +000030/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct key_user root_key_user = {
32 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070033 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080034 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .nkeys = ATOMIC_INIT(2),
36 .nikeys = ATOMIC_INIT(2),
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080037 .uid = GLOBAL_ROOT_UID,
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
David Howells973c9f42011-01-20 16:38:33 +000041 * Install the user and user session keyrings for the current process's UID.
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
David Howells8bbf49762008-11-14 10:39:14 +110043int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
David Howellsd84f4f92008-11-14 10:39:23 +110045 struct user_struct *user;
46 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct key *uid_keyring, *session_keyring;
David Howells96b5c8f2012-10-02 19:24:56 +010048 key_perm_t user_keyring_perm;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 char buf[20];
50 int ret;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080051 uid_t uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
David Howells96b5c8f2012-10-02 19:24:56 +010053 user_keyring_perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL;
David Howellsd84f4f92008-11-14 10:39:23 +110054 cred = current_cred();
55 user = cred->user;
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080056 uid = from_kuid(cred->user_ns, user->uid);
David Howellsd84f4f92008-11-14 10:39:23 +110057
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080058 kenter("%p{%u}", user, uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
David Howells0da9dfd2013-03-12 16:44:31 +110060 if (user->uid_keyring && user->session_keyring) {
David Howells69664cf2008-04-29 01:01:31 -070061 kleave(" = 0 [exist]");
62 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
David Howells69664cf2008-04-29 01:01:31 -070065 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 ret = 0;
67
David Howells69664cf2008-04-29 01:01:31 -070068 if (!user->uid_keyring) {
69 /* get the UID-specific keyring
70 * - there may be one in existence already as it may have been
71 * pinned by a session, but the user_struct pointing to it
72 * may have been destroyed by setuid */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080073 sprintf(buf, "_uid.%u", uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
David Howells69664cf2008-04-29 01:01:31 -070075 uid_keyring = find_keyring_by_name(buf, true);
76 if (IS_ERR(uid_keyring)) {
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080077 uid_keyring = keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010078 cred, user_keyring_perm,
Eric Biggersbfe9d7b2017-09-18 11:37:03 -070079 KEY_ALLOC_UID_KEYRING |
80 KEY_ALLOC_IN_QUOTA,
David Howells5ac7eac2016-04-06 16:14:24 +010081 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,
Eric Biggersbfe9d7b2017-09-18 11:37:03 -070097 KEY_ALLOC_UID_KEYRING |
98 KEY_ALLOC_IN_QUOTA,
David Howells5ac7eac2016-04-06 16:14:24 +010099 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -0700100 if (IS_ERR(session_keyring)) {
101 ret = PTR_ERR(session_keyring);
102 goto error_release;
103 }
104
105 /* we install a link from the user session keyring to
106 * the user keyring */
107 ret = key_link(session_keyring, uid_keyring);
108 if (ret < 0)
109 goto error_release_both;
110 }
111
112 /* install the keyrings */
113 user->uid_keyring = uid_keyring;
114 user->session_keyring = session_keyring;
115 }
116
117 mutex_unlock(&key_user_keyring_mutex);
118 kleave(" = 0");
119 return 0;
120
121error_release_both:
122 key_put(session_keyring);
123error_release:
124 key_put(uid_keyring);
125error:
126 mutex_unlock(&key_user_keyring_mutex);
127 kleave(" = %d", ret);
128 return ret;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100132 * Install a thread keyring to the given credentials struct if it didn't have
133 * one already. This is allowed to overrun the quota.
134 *
135 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
David Howellsd84f4f92008-11-14 10:39:23 +1100137int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
David Howellsd84f4f92008-11-14 10:39:23 +1100139 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Eric Biggers174a74d2017-04-18 15:31:09 +0100141 if (new->thread_keyring)
142 return 0;
143
David Howellsd84f4f92008-11-14 10:39:23 +1100144 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100145 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100146 KEY_ALLOC_QUOTA_OVERRUN,
147 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100148 if (IS_ERR(keyring))
149 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
David Howellsd84f4f92008-11-14 10:39:23 +1100151 new->thread_keyring = keyring;
152 return 0;
153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100156 * Install a thread keyring to the current task if it didn't have one already.
157 *
158 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
David Howellsd84f4f92008-11-14 10:39:23 +1100160static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Howellsd84f4f92008-11-14 10:39:23 +1100162 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int ret;
164
David Howellsd84f4f92008-11-14 10:39:23 +1100165 new = prepare_creds();
166 if (!new)
167 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
David Howellsd84f4f92008-11-14 10:39:23 +1100169 ret = install_thread_keyring_to_cred(new);
170 if (ret < 0) {
171 abort_creds(new);
172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
David Howellsd84f4f92008-11-14 10:39:23 +1100175 return commit_creds(new);
176}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
David Howellsd84f4f92008-11-14 10:39:23 +1100178/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100179 * Install a process keyring to the given credentials struct if it didn't have
180 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000181 *
Eric Biggers174a74d2017-04-18 15:31:09 +0100182 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100183 */
184int install_process_keyring_to_cred(struct cred *new)
185{
186 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Howells3a505972012-10-02 19:24:29 +0100188 if (new->process_keyring)
Eric Biggers174a74d2017-04-18 15:31:09 +0100189 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100190
David Howells96b5c8f2012-10-02 19:24:56 +0100191 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
192 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100193 KEY_ALLOC_QUOTA_OVERRUN,
194 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100195 if (IS_ERR(keyring))
196 return PTR_ERR(keyring);
197
David Howells3a505972012-10-02 19:24:29 +0100198 new->process_keyring = keyring;
199 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100200}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100203 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000204 *
Eric Biggers174a74d2017-04-18 15:31:09 +0100205 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 */
David Howellsd84f4f92008-11-14 10:39:23 +1100207static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David Howellsd84f4f92008-11-14 10:39:23 +1100209 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int ret;
211
David Howellsd84f4f92008-11-14 10:39:23 +1100212 new = prepare_creds();
213 if (!new)
214 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700215
David Howellsd84f4f92008-11-14 10:39:23 +1100216 ret = install_process_keyring_to_cred(new);
217 if (ret < 0) {
218 abort_creds(new);
Eric Biggers174a74d2017-04-18 15:31:09 +0100219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
221
David Howellsd84f4f92008-11-14 10:39:23 +1100222 return commit_creds(new);
223}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100226 * Install the given keyring as the session keyring of the given credentials
227 * struct, replacing the existing one if any. If the given keyring is NULL,
228 * then install a new anonymous session keyring.
229 *
230 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700232int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David Howells7e047ef2006-06-26 00:24:50 -0700234 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700236
237 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* create an empty session keyring */
240 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700241 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100242 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700243 flags = KEY_ALLOC_IN_QUOTA;
244
David Howells96b5c8f2012-10-02 19:24:56 +0100245 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
246 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100247 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700248 if (IS_ERR(keyring))
249 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100250 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100251 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100255 old = cred->session_keyring;
256 rcu_assign_pointer(cred->session_keyring, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
David Howells3a505972012-10-02 19:24:29 +0100258 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700259 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
David Howells1a26feb2006-04-10 22:54:26 -0700261 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100265 * Install the given keyring as the session keyring of the current task,
266 * replacing the existing one if any. If the given keyring is NULL, then
267 * install a new anonymous session keyring.
268 *
269 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 */
David Howellsd84f4f92008-11-14 10:39:23 +1100271static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
David Howellsd84f4f92008-11-14 10:39:23 +1100273 struct cred *new;
274 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
David Howellsd84f4f92008-11-14 10:39:23 +1100276 new = prepare_creds();
277 if (!new)
278 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800279
David Howells995995372011-08-22 14:08:33 +0100280 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100281 if (ret < 0) {
282 abort_creds(new);
283 return ret;
284 }
David Howellsb5f545c2006-01-08 01:02:47 -0800285
David Howellsd84f4f92008-11-14 10:39:23 +1100286 return commit_creds(new);
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289/*
David Howells973c9f42011-01-20 16:38:33 +0000290 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292void key_fsuid_changed(struct task_struct *tsk)
293{
294 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100295 BUG_ON(!tsk->cred);
296 if (tsk->cred->thread_keyring) {
297 down_write(&tsk->cred->thread_keyring->sem);
298 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
299 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/*
David Howells973c9f42011-01-20 16:38:33 +0000304 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306void key_fsgid_changed(struct task_struct *tsk)
307{
308 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100309 BUG_ON(!tsk->cred);
310 if (tsk->cred->thread_keyring) {
311 down_write(&tsk->cred->thread_keyring->sem);
312 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
313 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
David Howells973c9f42011-01-20 16:38:33 +0000318 * Search the process keyrings attached to the supplied cred for the first
319 * matching key.
320 *
321 * The search criteria are the type and the match function. The description is
322 * given to the match function as a parameter, but doesn't otherwise influence
323 * the search. Typically the match function will compare the description
324 * parameter to the key's description.
325 *
326 * This can only search keyrings that grant Search permission to the supplied
327 * credentials. Keyrings linked to searched keyrings will also be searched if
328 * they grant Search permission too. Keys can only be found if they grant
329 * Search permission to the credentials.
330 *
331 * Returns a pointer to the key with the key usage count incremented if
332 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
333 * matched negative keys.
334 *
335 * In the case of a successful return, the possession attribute is set on the
336 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100338key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
David Howellsb5f545c2006-01-08 01:02:47 -0800340 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
343 * searchable, but we failed to find a key or we found a negative key;
344 * otherwise we want to return a sample error (probably -EACCES) if
345 * none of the keyrings were searchable
346 *
347 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
348 */
David Howells664cceb2005-09-28 17:03:15 +0100349 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ret = NULL;
351 err = ERR_PTR(-EAGAIN);
352
353 /* search the thread keyring first */
David Howells4bdf0bc2013-09-24 10:35:15 +0100354 if (ctx->cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100355 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100356 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100357 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto found;
359
David Howells664cceb2005-09-28 17:03:15 +0100360 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100363 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 default:
David Howells664cceb2005-09-28 17:03:15 +0100366 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 }
369 }
370
371 /* search the process keyring second */
David Howells4bdf0bc2013-09-24 10:35:15 +0100372 if (ctx->cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100373 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100374 make_key_ref(ctx->cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100375 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 goto found;
377
David Howells664cceb2005-09-28 17:03:15 +0100378 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000380 if (ret)
381 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100383 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 default:
David Howells664cceb2005-09-28 17:03:15 +0100386 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 break;
388 }
389 }
390
David Howells3e301482005-06-23 22:00:56 -0700391 /* search the session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100392 if (ctx->cred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700393 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100394 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100395 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
396 ctx);
David Howells8589b4e2005-06-23 22:00:53 -0700397 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
David Howells664cceb2005-09-28 17:03:15 +0100399 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700400 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David Howells664cceb2005-09-28 17:03:15 +0100402 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700403 case -EAGAIN: /* no key */
404 if (ret)
405 break;
406 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100407 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 break;
David Howells3e301482005-06-23 22:00:56 -0700409 default:
David Howells664cceb2005-09-28 17:03:15 +0100410 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700411 break;
412 }
David Howells3e301482005-06-23 22:00:56 -0700413 }
414 /* or search the user-session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100415 else if (ctx->cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100416 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100417 make_key_ref(ctx->cred->user->session_keyring, 1),
418 ctx);
David Howells664cceb2005-09-28 17:03:15 +0100419 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700420 goto found;
421
David Howells664cceb2005-09-28 17:03:15 +0100422 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700423 case -EAGAIN: /* no key */
424 if (ret)
425 break;
426 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100427 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700428 break;
429 default:
David Howells664cceb2005-09-28 17:03:15 +0100430 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700431 break;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
David Howells927942a2010-06-11 17:31:10 +0100435 /* no key - decide on the error we're going to go for */
436 key_ref = ret ? ret : err;
437
438found:
439 return key_ref;
440}
441
David Howells927942a2010-06-11 17:31:10 +0100442/*
David Howells973c9f42011-01-20 16:38:33 +0000443 * Search the process keyrings attached to the supplied cred for the first
444 * matching key in the manner of search_my_process_keyrings(), but also search
445 * the keys attached to the assumed authorisation key using its credentials if
446 * one is available.
447 *
448 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100449 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100450key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100451{
452 struct request_key_auth *rka;
453 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
454
455 might_sleep();
456
David Howells4bdf0bc2013-09-24 10:35:15 +0100457 key_ref = search_my_process_keyrings(ctx);
David Howells927942a2010-06-11 17:31:10 +0100458 if (!IS_ERR(key_ref))
459 goto found;
460 err = key_ref;
461
David Howellsb5f545c2006-01-08 01:02:47 -0800462 /* if this process has an instantiation authorisation key, then we also
463 * search the keyrings of the process mentioned there
464 * - we don't permit access to request_key auth keys via this method
465 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100466 if (ctx->cred->request_key_auth &&
467 ctx->cred == current_cred() &&
468 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800469 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100470 const struct cred *cred = ctx->cred;
471
David Howells04c567d2006-06-22 14:47:18 -0700472 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100473 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700474
David Howells4bdf0bc2013-09-24 10:35:15 +0100475 if (key_validate(ctx->cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100476 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800477
David Howells4bdf0bc2013-09-24 10:35:15 +0100478 ctx->cred = rka->cred;
479 key_ref = search_process_keyrings(ctx);
480 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800481
David Howellsc69e8d92008-11-14 10:39:19 +1100482 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700483
484 if (!IS_ERR(key_ref))
485 goto found;
486
David Howells927942a2010-06-11 17:31:10 +0100487 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700488 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100489 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800490 }
491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100494 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
495 key_ref = ERR_PTR(-ENOKEY);
496 else if (err == ERR_PTR(-EACCES))
497 key_ref = ret;
498 else
499 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Howells3e301482005-06-23 22:00:56 -0700501found:
David Howells664cceb2005-09-28 17:03:15 +0100502 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000503}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
David Howells973c9f42011-01-20 16:38:33 +0000506 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100507 */
David Howells0c903ab2014-09-16 17:36:08 +0100508bool lookup_user_key_possessed(const struct key *key,
509 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100510{
David Howells46291952014-09-16 17:36:02 +0100511 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000512}
David Howells664cceb2005-09-28 17:03:15 +0100513
David Howells664cceb2005-09-28 17:03:15 +0100514/*
David Howells973c9f42011-01-20 16:38:33 +0000515 * Look up a key ID given us by userspace with a given permissions mask to get
516 * the key it refers to.
517 *
518 * Flags can be passed to request that special keyrings be created if referred
519 * to directly, to permit partially constructed keys to be found and to skip
520 * validity and permission checks on the found key.
521 *
522 * Returns a pointer to the key with an incremented usage count if successful;
523 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
524 * to a key or the best found key was a negative key; -EKEYREVOKED or
525 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
526 * found key doesn't grant the requested permit or the LSM denied access to it;
527 * or -ENOMEM if a special keyring couldn't be created.
528 *
529 * In the case of a successful return, the possession attribute is set on the
530 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 */
David Howells55931222009-09-02 09:13:45 +0100532key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100533 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
David Howells4bdf0bc2013-09-24 10:35:15 +0100535 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100536 .match_data.cmp = lookup_user_key_possessed,
537 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
538 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100539 };
David Howells8bbf49762008-11-14 10:39:14 +1100540 struct request_key_auth *rka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100542 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 int ret;
544
David Howellsbb952bb2008-11-14 10:39:20 +1100545try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100546 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100547 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 switch (id) {
550 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100551 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100552 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto error;
554
David Howells8bbf49762008-11-14 10:39:14 +1100555 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100557 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto error;
559 }
David Howellsbb952bb2008-11-14 10:39:20 +1100560 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
David Howells4bdf0bc2013-09-24 10:35:15 +0100563 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100564 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100565 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 break;
567
568 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100569 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100570 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto error;
572
David Howells8bbf49762008-11-14 10:39:14 +1100573 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100575 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 goto error;
577 }
David Howellsbb952bb2008-11-14 10:39:20 +1100578 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
David Howells4bdf0bc2013-09-24 10:35:15 +0100581 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100582 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100583 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585
586 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100587 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* always install a session keyring upon access if one
589 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100590 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700591 if (ret < 0)
592 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100593 if (lflags & KEY_LOOKUP_CREATE)
594 ret = join_session_keyring(NULL);
595 else
596 ret = install_session_keyring(
David Howells4bdf0bc2013-09-24 10:35:15 +0100597 ctx.cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (ret < 0)
600 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100601 goto reget_creds;
David Howells4bdf0bc2013-09-24 10:35:15 +0100602 } else if (ctx.cred->session_keyring ==
603 ctx.cred->user->session_keyring &&
David Howells3ecf1b42011-08-22 14:08:43 +0100604 lflags & KEY_LOOKUP_CREATE) {
605 ret = join_session_keyring(NULL);
606 if (ret < 0)
607 goto error;
608 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
610
David Howells3e301482005-06-23 22:00:56 -0700611 rcu_read_lock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100612 key = rcu_dereference(ctx.cred->session_keyring);
David Howellsccc3e6d2013-09-24 10:35:16 +0100613 __key_get(key);
David Howells3e301482005-06-23 22:00:56 -0700614 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100615 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617
618 case KEY_SPEC_USER_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100619 if (!ctx.cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100620 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700621 if (ret < 0)
622 goto error;
623 }
624
David Howells4bdf0bc2013-09-24 10:35:15 +0100625 key = ctx.cred->user->uid_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100626 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100627 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629
630 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100631 if (!ctx.cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100632 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700633 if (ret < 0)
634 goto error;
635 }
636
David Howells4bdf0bc2013-09-24 10:35:15 +0100637 key = ctx.cred->user->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100638 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100639 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641
642 case KEY_SPEC_GROUP_KEYRING:
643 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100644 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 goto error;
646
David Howellsb5f545c2006-01-08 01:02:47 -0800647 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100648 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800649 if (!key)
650 goto error;
651
David Howellsccc3e6d2013-09-24 10:35:16 +0100652 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800653 key_ref = make_key_ref(key, 1);
654 break;
655
David Howells8bbf49762008-11-14 10:39:14 +1100656 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100657 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100658 goto error;
659
David Howells4bdf0bc2013-09-24 10:35:15 +0100660 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000661 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100662 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100663 key_ref = ERR_PTR(-EKEYREVOKED);
664 key = NULL;
665 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100666 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100667 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100668 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100669 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100670 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100671 if (!key)
672 goto error;
673 key_ref = make_key_ref(key, 1);
674 break;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 default:
David Howells664cceb2005-09-28 17:03:15 +0100677 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (id < 1)
679 goto error;
680
681 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100682 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800683 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100685 }
686
687 key_ref = make_key_ref(key, 0);
688
689 /* check to see if we possess the key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100690 ctx.index_key.type = key->type;
691 ctx.index_key.description = key->description;
692 ctx.index_key.desc_len = strlen(key->description);
David Howells46291952014-09-16 17:36:02 +0100693 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100694 kdebug("check possessed");
695 skey_ref = search_process_keyrings(&ctx);
696 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100697
698 if (!IS_ERR(skey_ref)) {
699 key_put(key);
700 key_ref = skey_ref;
701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 }
705
David Howells55931222009-09-02 09:13:45 +0100706 /* unlink does not use the nominated key in any way, so can skip all
707 * the permission checks as it is only concerned with the keyring */
708 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
709 ret = 0;
710 goto error;
711 }
712
713 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700714 ret = wait_for_key_construction(key, true);
715 switch (ret) {
716 case -ERESTARTSYS:
717 goto invalid_key;
718 default:
719 if (perm)
720 goto invalid_key;
721 case 0:
722 break;
723 }
724 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ret = key_validate(key);
726 if (ret < 0)
727 goto invalid_key;
728 }
729
730 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100731 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
David Howells63c8e452017-10-04 16:43:25 +0100732 key_read_state(key) == KEY_IS_UNINSTANTIATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 goto invalid_key;
734
David Howells3e301482005-06-23 22:00:56 -0700735 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100736 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800737 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 goto invalid_key;
739
David Howells31d5a792012-05-11 10:56:56 +0100740 key->last_used_at = current_kernel_time().tv_sec;
741
David Howells664cceb2005-09-28 17:03:15 +0100742error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100743 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100744 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
David Howells664cceb2005-09-28 17:03:15 +0100746invalid_key:
747 key_ref_put(key_ref);
748 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto error;
750
David Howellsbb952bb2008-11-14 10:39:20 +1100751 /* if we attempted to install a keyring, then it may have caused new
752 * creds to be installed */
753reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100754 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100755 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000756}
David Howellsbb952bb2008-11-14 10:39:20 +1100757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
David Howells973c9f42011-01-20 16:38:33 +0000759 * Join the named keyring as the session keyring if possible else attempt to
760 * create a new one of that name and join that.
761 *
762 * If the name is NULL, an empty anonymous keyring will be installed as the
763 * session keyring.
764 *
765 * Named session keyrings are joined with a semaphore held to prevent the
766 * keyrings from going away whilst the attempt is made to going them and also
767 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
769long join_session_keyring(const char *name)
770{
David Howellsd84f4f92008-11-14 10:39:23 +1100771 const struct cred *old;
772 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100774 long ret, serial;
775
David Howellsd84f4f92008-11-14 10:39:23 +1100776 new = prepare_creds();
777 if (!new)
778 return -ENOMEM;
779 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /* if no name is provided, install an anonymous keyring */
782 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100783 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (ret < 0)
785 goto error;
786
David Howells3a505972012-10-02 19:24:29 +0100787 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100788 ret = commit_creds(new);
789 if (ret == 0)
790 ret = serial;
791 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800795 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700798 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (PTR_ERR(keyring) == -ENOKEY) {
800 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100801 keyring = keyring_alloc(
802 name, old->uid, old->gid, old,
803 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100804 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 if (IS_ERR(keyring)) {
806 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700807 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
David Howellsd84f4f92008-11-14 10:39:23 +1100809 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ret = PTR_ERR(keyring);
811 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100812 } else if (keyring == new->session_keyring) {
Yevgeny Pats23567fd2016-01-19 22:09:04 +0000813 key_put(keyring);
David Howells3a505972012-10-02 19:24:29 +0100814 ret = 0;
815 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
818 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100819 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (ret < 0)
821 goto error2;
822
David Howellsd84f4f92008-11-14 10:39:23 +1100823 commit_creds(new);
824 mutex_unlock(&key_session_mutex);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ret = keyring->serial;
827 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100828okay:
829 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
David Howells664cceb2005-09-28 17:03:15 +0100831error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800832 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100833error:
David Howellsd84f4f92008-11-14 10:39:23 +1100834 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100836}
David Howellsee18d642009-09-02 09:14:21 +0100837
838/*
David Howells973c9f42011-01-20 16:38:33 +0000839 * Replace a process's session keyring on behalf of one of its children when
840 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100841 */
Al Viro67d12142012-06-27 11:07:19 +0400842void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100843{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000844 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400845 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100846
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000847 if (unlikely(current->flags & PF_EXITING)) {
848 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100849 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000850 }
David Howellsee18d642009-09-02 09:14:21 +0100851
David Howellsee18d642009-09-02 09:14:21 +0100852 new-> uid = old-> uid;
853 new-> euid = old-> euid;
854 new-> suid = old-> suid;
855 new->fsuid = old->fsuid;
856 new-> gid = old-> gid;
857 new-> egid = old-> egid;
858 new-> sgid = old-> sgid;
859 new->fsgid = old->fsgid;
860 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800861 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100862 new->group_info = get_group_info(old->group_info);
863
864 new->securebits = old->securebits;
865 new->cap_inheritable = old->cap_inheritable;
866 new->cap_permitted = old->cap_permitted;
867 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700868 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100869 new->cap_bset = old->cap_bset;
870
871 new->jit_keyring = old->jit_keyring;
872 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100873 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100874
875 security_transfer_creds(new, old);
876
877 commit_creds(new);
878}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100879
880/*
881 * Make sure that root's user and user-session keyrings exist.
882 */
883static int __init init_root_keyring(void)
884{
885 return install_user_keyrings();
886}
887
888late_initcall(init_root_keyring);