blob: 86bced9fdbdf22eb60170584d87730e1179a2744 [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 = {
Elena Reshetovaddb99e12017-03-31 15:20:49 +030033 .usage = REFCOUNT_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/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100131 * Install a thread keyring to the given credentials struct if it didn't have
132 * one already. This is allowed to overrun the quota.
133 *
134 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
David Howellsd84f4f92008-11-14 10:39:23 +1100136int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
David Howellsd84f4f92008-11-14 10:39:23 +1100138 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Eric Biggersc9f838d2017-04-18 15:31:09 +0100140 if (new->thread_keyring)
141 return 0;
142
David Howellsd84f4f92008-11-14 10:39:23 +1100143 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100144 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100145 KEY_ALLOC_QUOTA_OVERRUN,
146 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100147 if (IS_ERR(keyring))
148 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David Howellsd84f4f92008-11-14 10:39:23 +1100150 new->thread_keyring = keyring;
151 return 0;
152}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100155 * Install a thread keyring to the current task if it didn't have one already.
156 *
157 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
David Howellsd84f4f92008-11-14 10:39:23 +1100159static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
David Howellsd84f4f92008-11-14 10:39:23 +1100161 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int ret;
163
David Howellsd84f4f92008-11-14 10:39:23 +1100164 new = prepare_creds();
165 if (!new)
166 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David Howellsd84f4f92008-11-14 10:39:23 +1100168 ret = install_thread_keyring_to_cred(new);
169 if (ret < 0) {
170 abort_creds(new);
171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
David Howellsd84f4f92008-11-14 10:39:23 +1100174 return commit_creds(new);
175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Howellsd84f4f92008-11-14 10:39:23 +1100177/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100178 * Install a process keyring to the given credentials struct if it didn't have
179 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000180 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100181 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100182 */
183int install_process_keyring_to_cred(struct cred *new)
184{
185 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howells3a505972012-10-02 19:24:29 +0100187 if (new->process_keyring)
Eric Biggersc9f838d2017-04-18 15:31:09 +0100188 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100189
David Howells96b5c8f2012-10-02 19:24:56 +0100190 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
191 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100192 KEY_ALLOC_QUOTA_OVERRUN,
193 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100194 if (IS_ERR(keyring))
195 return PTR_ERR(keyring);
196
David Howells3a505972012-10-02 19:24:29 +0100197 new->process_keyring = keyring;
198 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100199}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100202 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000203 *
Eric Biggersc9f838d2017-04-18 15:31:09 +0100204 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
David Howellsd84f4f92008-11-14 10:39:23 +1100206static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
David Howellsd84f4f92008-11-14 10:39:23 +1100208 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int ret;
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211 new = prepare_creds();
212 if (!new)
213 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700214
David Howellsd84f4f92008-11-14 10:39:23 +1100215 ret = install_process_keyring_to_cred(new);
216 if (ret < 0) {
217 abort_creds(new);
Eric Biggersc9f838d2017-04-18 15:31:09 +0100218 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
David Howellsd84f4f92008-11-14 10:39:23 +1100221 return commit_creds(new);
222}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100225 * Install the given keyring as the session keyring of the given credentials
226 * struct, replacing the existing one if any. If the given keyring is NULL,
227 * then install a new anonymous session keyring.
228 *
229 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700231int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
David Howells7e047ef2006-06-26 00:24:50 -0700233 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700235
236 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 /* create an empty session keyring */
239 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700240 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100241 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700242 flags = KEY_ALLOC_IN_QUOTA;
243
David Howells96b5c8f2012-10-02 19:24:56 +0100244 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
245 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100246 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700247 if (IS_ERR(keyring))
248 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100249 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100250 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
253 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100254 old = cred->session_keyring;
255 rcu_assign_pointer(cred->session_keyring, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David Howells3a505972012-10-02 19:24:29 +0100257 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700258 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
David Howells1a26feb2006-04-10 22:54:26 -0700260 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
Eric Biggersc9f838d2017-04-18 15:31:09 +0100264 * Install the given keyring as the session keyring of the current task,
265 * replacing the existing one if any. If the given keyring is NULL, then
266 * install a new anonymous session keyring.
267 *
268 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 */
David Howellsd84f4f92008-11-14 10:39:23 +1100270static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
David Howellsd84f4f92008-11-14 10:39:23 +1100272 struct cred *new;
273 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
David Howellsd84f4f92008-11-14 10:39:23 +1100275 new = prepare_creds();
276 if (!new)
277 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800278
David Howells995995372011-08-22 14:08:33 +0100279 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100280 if (ret < 0) {
281 abort_creds(new);
282 return ret;
283 }
David Howellsb5f545c2006-01-08 01:02:47 -0800284
David Howellsd84f4f92008-11-14 10:39:23 +1100285 return commit_creds(new);
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
David Howells973c9f42011-01-20 16:38:33 +0000289 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291void key_fsuid_changed(struct task_struct *tsk)
292{
293 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100294 BUG_ON(!tsk->cred);
295 if (tsk->cred->thread_keyring) {
296 down_write(&tsk->cred->thread_keyring->sem);
297 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
298 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
David Howells973c9f42011-01-20 16:38:33 +0000303 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305void key_fsgid_changed(struct task_struct *tsk)
306{
307 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100308 BUG_ON(!tsk->cred);
309 if (tsk->cred->thread_keyring) {
310 down_write(&tsk->cred->thread_keyring->sem);
311 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
312 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
David Howells973c9f42011-01-20 16:38:33 +0000317 * Search the process keyrings attached to the supplied cred for the first
318 * matching key.
319 *
320 * The search criteria are the type and the match function. The description is
321 * given to the match function as a parameter, but doesn't otherwise influence
322 * the search. Typically the match function will compare the description
323 * parameter to the key's description.
324 *
325 * This can only search keyrings that grant Search permission to the supplied
326 * credentials. Keyrings linked to searched keyrings will also be searched if
327 * they grant Search permission too. Keys can only be found if they grant
328 * Search permission to the credentials.
329 *
330 * Returns a pointer to the key with the key usage count incremented if
331 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
332 * matched negative keys.
333 *
334 * In the case of a successful return, the possession attribute is set on the
335 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100337key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
David Howellsb5f545c2006-01-08 01:02:47 -0800339 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
342 * searchable, but we failed to find a key or we found a negative key;
343 * otherwise we want to return a sample error (probably -EACCES) if
344 * none of the keyrings were searchable
345 *
346 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
347 */
David Howells664cceb2005-09-28 17:03:15 +0100348 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ret = NULL;
350 err = ERR_PTR(-EAGAIN);
351
352 /* search the thread keyring first */
David Howells4bdf0bc2013-09-24 10:35:15 +0100353 if (ctx->cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100354 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100355 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100356 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 goto found;
358
David Howells664cceb2005-09-28 17:03:15 +0100359 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100362 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 default:
David Howells664cceb2005-09-28 17:03:15 +0100365 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 break;
367 }
368 }
369
370 /* search the process keyring second */
David Howells4bdf0bc2013-09-24 10:35:15 +0100371 if (ctx->cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100372 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100373 make_key_ref(ctx->cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100374 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto found;
376
David Howells664cceb2005-09-28 17:03:15 +0100377 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000379 if (ret)
380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100382 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384 default:
David Howells664cceb2005-09-28 17:03:15 +0100385 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 }
388 }
389
David Howells3e301482005-06-23 22:00:56 -0700390 /* search the session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100391 if (ctx->cred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700392 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100393 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100394 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
395 ctx);
David Howells8589b4e2005-06-23 22:00:53 -0700396 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
David Howells664cceb2005-09-28 17:03:15 +0100398 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700399 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
David Howells664cceb2005-09-28 17:03:15 +0100401 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700402 case -EAGAIN: /* no key */
403 if (ret)
404 break;
405 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100406 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 break;
David Howells3e301482005-06-23 22:00:56 -0700408 default:
David Howells664cceb2005-09-28 17:03:15 +0100409 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700410 break;
411 }
David Howells3e301482005-06-23 22:00:56 -0700412 }
413 /* or search the user-session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100414 else if (ctx->cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100415 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100416 make_key_ref(ctx->cred->user->session_keyring, 1),
417 ctx);
David Howells664cceb2005-09-28 17:03:15 +0100418 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700419 goto found;
420
David Howells664cceb2005-09-28 17:03:15 +0100421 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700422 case -EAGAIN: /* no key */
423 if (ret)
424 break;
425 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100426 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700427 break;
428 default:
David Howells664cceb2005-09-28 17:03:15 +0100429 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700430 break;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
David Howells927942a2010-06-11 17:31:10 +0100434 /* no key - decide on the error we're going to go for */
435 key_ref = ret ? ret : err;
436
437found:
438 return key_ref;
439}
440
David Howells927942a2010-06-11 17:31:10 +0100441/*
David Howells973c9f42011-01-20 16:38:33 +0000442 * Search the process keyrings attached to the supplied cred for the first
443 * matching key in the manner of search_my_process_keyrings(), but also search
444 * the keys attached to the assumed authorisation key using its credentials if
445 * one is available.
446 *
447 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100448 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100449key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100450{
451 struct request_key_auth *rka;
452 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
453
454 might_sleep();
455
David Howells4bdf0bc2013-09-24 10:35:15 +0100456 key_ref = search_my_process_keyrings(ctx);
David Howells927942a2010-06-11 17:31:10 +0100457 if (!IS_ERR(key_ref))
458 goto found;
459 err = key_ref;
460
David Howellsb5f545c2006-01-08 01:02:47 -0800461 /* if this process has an instantiation authorisation key, then we also
462 * search the keyrings of the process mentioned there
463 * - we don't permit access to request_key auth keys via this method
464 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100465 if (ctx->cred->request_key_auth &&
466 ctx->cred == current_cred() &&
467 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800468 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100469 const struct cred *cred = ctx->cred;
470
David Howells04c567d2006-06-22 14:47:18 -0700471 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100472 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700473
David Howells4bdf0bc2013-09-24 10:35:15 +0100474 if (key_validate(ctx->cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100475 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800476
David Howells4bdf0bc2013-09-24 10:35:15 +0100477 ctx->cred = rka->cred;
478 key_ref = search_process_keyrings(ctx);
479 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800480
David Howellsc69e8d92008-11-14 10:39:19 +1100481 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700482
483 if (!IS_ERR(key_ref))
484 goto found;
485
David Howells927942a2010-06-11 17:31:10 +0100486 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700487 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100488 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800489 }
490 }
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100493 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
494 key_ref = ERR_PTR(-ENOKEY);
495 else if (err == ERR_PTR(-EACCES))
496 key_ref = ret;
497 else
498 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
David Howells3e301482005-06-23 22:00:56 -0700500found:
David Howells664cceb2005-09-28 17:03:15 +0100501 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000502}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504/*
David Howells973c9f42011-01-20 16:38:33 +0000505 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100506 */
David Howells0c903ab2014-09-16 17:36:08 +0100507bool lookup_user_key_possessed(const struct key *key,
508 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100509{
David Howells46291952014-09-16 17:36:02 +0100510 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000511}
David Howells664cceb2005-09-28 17:03:15 +0100512
David Howells664cceb2005-09-28 17:03:15 +0100513/*
David Howells973c9f42011-01-20 16:38:33 +0000514 * Look up a key ID given us by userspace with a given permissions mask to get
515 * the key it refers to.
516 *
517 * Flags can be passed to request that special keyrings be created if referred
518 * to directly, to permit partially constructed keys to be found and to skip
519 * validity and permission checks on the found key.
520 *
521 * Returns a pointer to the key with an incremented usage count if successful;
522 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
523 * to a key or the best found key was a negative key; -EKEYREVOKED or
524 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
525 * found key doesn't grant the requested permit or the LSM denied access to it;
526 * or -ENOMEM if a special keyring couldn't be created.
527 *
528 * In the case of a successful return, the possession attribute is set on the
529 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
David Howells55931222009-09-02 09:13:45 +0100531key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100532 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
David Howells4bdf0bc2013-09-24 10:35:15 +0100534 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100535 .match_data.cmp = lookup_user_key_possessed,
536 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
537 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100538 };
David Howells8bbf49762008-11-14 10:39:14 +1100539 struct request_key_auth *rka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100541 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int ret;
543
David Howellsbb952bb2008-11-14 10:39:20 +1100544try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100545 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100546 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 switch (id) {
549 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100550 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100551 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto error;
553
David Howells8bbf49762008-11-14 10:39:14 +1100554 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100556 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 goto error;
558 }
David Howellsbb952bb2008-11-14 10:39:20 +1100559 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
David Howells4bdf0bc2013-09-24 10:35:15 +0100562 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100563 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100564 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 break;
566
567 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100568 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100569 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto error;
571
David Howells8bbf49762008-11-14 10:39:14 +1100572 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100574 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 goto error;
576 }
David Howellsbb952bb2008-11-14 10:39:20 +1100577 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
David Howells4bdf0bc2013-09-24 10:35:15 +0100580 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100581 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100582 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584
585 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100586 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* always install a session keyring upon access if one
588 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100589 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700590 if (ret < 0)
591 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100592 if (lflags & KEY_LOOKUP_CREATE)
593 ret = join_session_keyring(NULL);
594 else
595 ret = install_session_keyring(
David Howells4bdf0bc2013-09-24 10:35:15 +0100596 ctx.cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (ret < 0)
599 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100600 goto reget_creds;
David Howells4bdf0bc2013-09-24 10:35:15 +0100601 } else if (ctx.cred->session_keyring ==
602 ctx.cred->user->session_keyring &&
David Howells3ecf1b42011-08-22 14:08:43 +0100603 lflags & KEY_LOOKUP_CREATE) {
604 ret = join_session_keyring(NULL);
605 if (ret < 0)
606 goto error;
607 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
David Howells3e301482005-06-23 22:00:56 -0700610 rcu_read_lock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100611 key = rcu_dereference(ctx.cred->session_keyring);
David Howellsccc3e6d2013-09-24 10:35:16 +0100612 __key_get(key);
David Howells3e301482005-06-23 22:00:56 -0700613 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100614 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
616
617 case KEY_SPEC_USER_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100618 if (!ctx.cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100619 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700620 if (ret < 0)
621 goto error;
622 }
623
David Howells4bdf0bc2013-09-24 10:35:15 +0100624 key = ctx.cred->user->uid_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100625 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100626 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 break;
628
629 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100630 if (!ctx.cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100631 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700632 if (ret < 0)
633 goto error;
634 }
635
David Howells4bdf0bc2013-09-24 10:35:15 +0100636 key = ctx.cred->user->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100637 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100638 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640
641 case KEY_SPEC_GROUP_KEYRING:
642 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100643 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 goto error;
645
David Howellsb5f545c2006-01-08 01:02:47 -0800646 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100647 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800648 if (!key)
649 goto error;
650
David Howellsccc3e6d2013-09-24 10:35:16 +0100651 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800652 key_ref = make_key_ref(key, 1);
653 break;
654
David Howells8bbf49762008-11-14 10:39:14 +1100655 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100656 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100657 goto error;
658
David Howells4bdf0bc2013-09-24 10:35:15 +0100659 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000660 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100661 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100662 key_ref = ERR_PTR(-EKEYREVOKED);
663 key = NULL;
664 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100665 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100666 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100667 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100668 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100669 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100670 if (!key)
671 goto error;
672 key_ref = make_key_ref(key, 1);
673 break;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 default:
David Howells664cceb2005-09-28 17:03:15 +0100676 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (id < 1)
678 goto error;
679
680 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100681 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800682 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100684 }
685
686 key_ref = make_key_ref(key, 0);
687
688 /* check to see if we possess the key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100689 ctx.index_key.type = key->type;
690 ctx.index_key.description = key->description;
691 ctx.index_key.desc_len = strlen(key->description);
David Howells46291952014-09-16 17:36:02 +0100692 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100693 kdebug("check possessed");
694 skey_ref = search_process_keyrings(&ctx);
695 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100696
697 if (!IS_ERR(skey_ref)) {
698 key_put(key);
699 key_ref = skey_ref;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 break;
703 }
704
David Howells55931222009-09-02 09:13:45 +0100705 /* unlink does not use the nominated key in any way, so can skip all
706 * the permission checks as it is only concerned with the keyring */
707 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
708 ret = 0;
709 goto error;
710 }
711
712 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700713 ret = wait_for_key_construction(key, true);
714 switch (ret) {
715 case -ERESTARTSYS:
716 goto invalid_key;
717 default:
718 if (perm)
719 goto invalid_key;
720 case 0:
721 break;
722 }
723 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 ret = key_validate(key);
725 if (ret < 0)
726 goto invalid_key;
727 }
728
729 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100730 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
731 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto invalid_key;
733
David Howells3e301482005-06-23 22:00:56 -0700734 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100735 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800736 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto invalid_key;
738
David Howells31d5a792012-05-11 10:56:56 +0100739 key->last_used_at = current_kernel_time().tv_sec;
740
David Howells664cceb2005-09-28 17:03:15 +0100741error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100742 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100743 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
David Howells664cceb2005-09-28 17:03:15 +0100745invalid_key:
746 key_ref_put(key_ref);
747 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto error;
749
David Howellsbb952bb2008-11-14 10:39:20 +1100750 /* if we attempted to install a keyring, then it may have caused new
751 * creds to be installed */
752reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100753 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100754 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000755}
David Howellsbb952bb2008-11-14 10:39:20 +1100756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
David Howells973c9f42011-01-20 16:38:33 +0000758 * Join the named keyring as the session keyring if possible else attempt to
759 * create a new one of that name and join that.
760 *
761 * If the name is NULL, an empty anonymous keyring will be installed as the
762 * session keyring.
763 *
764 * Named session keyrings are joined with a semaphore held to prevent the
765 * keyrings from going away whilst the attempt is made to going them and also
766 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 */
768long join_session_keyring(const char *name)
769{
David Howellsd84f4f92008-11-14 10:39:23 +1100770 const struct cred *old;
771 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100773 long ret, serial;
774
David Howellsd84f4f92008-11-14 10:39:23 +1100775 new = prepare_creds();
776 if (!new)
777 return -ENOMEM;
778 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 /* if no name is provided, install an anonymous keyring */
781 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100782 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (ret < 0)
784 goto error;
785
David Howells3a505972012-10-02 19:24:29 +0100786 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100787 ret = commit_creds(new);
788 if (ret == 0)
789 ret = serial;
790 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792
793 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800794 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700797 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (PTR_ERR(keyring) == -ENOKEY) {
799 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100800 keyring = keyring_alloc(
801 name, old->uid, old->gid, old,
802 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100803 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (IS_ERR(keyring)) {
805 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700806 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
David Howellsd84f4f92008-11-14 10:39:23 +1100808 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 ret = PTR_ERR(keyring);
810 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100811 } else if (keyring == new->session_keyring) {
812 ret = 0;
Eric Biggersd636bd92017-06-08 14:48:03 +0100813 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
815
816 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100817 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (ret < 0)
Eric Biggersd636bd92017-06-08 14:48:03 +0100819 goto error3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
David Howellsd84f4f92008-11-14 10:39:23 +1100821 commit_creds(new);
822 mutex_unlock(&key_session_mutex);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ret = keyring->serial;
825 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100826okay:
827 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Eric Biggersd636bd92017-06-08 14:48:03 +0100829error3:
830 key_put(keyring);
David Howells664cceb2005-09-28 17:03:15 +0100831error2:
Ingo Molnarbb0030792006-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);