blob: 45536c677b05a04aec23a2ba6912ba5a01422390 [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,
David Howells5ac7eac2016-04-06 16:14:24 +010079 KEY_ALLOC_IN_QUOTA,
80 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070081 if (IS_ERR(uid_keyring)) {
82 ret = PTR_ERR(uid_keyring);
83 goto error;
84 }
85 }
86
87 /* get a default session keyring (which might also exist
88 * already) */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080089 sprintf(buf, "_uid_ses.%u", uid);
David Howells69664cf2008-04-29 01:01:31 -070090
91 session_keyring = find_keyring_by_name(buf, true);
92 if (IS_ERR(session_keyring)) {
93 session_keyring =
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -080094 keyring_alloc(buf, user->uid, INVALID_GID,
David Howells96b5c8f2012-10-02 19:24:56 +010095 cred, user_keyring_perm,
David Howells5ac7eac2016-04-06 16:14:24 +010096 KEY_ALLOC_IN_QUOTA,
97 NULL, NULL);
David Howells69664cf2008-04-29 01:01:31 -070098 if (IS_ERR(session_keyring)) {
99 ret = PTR_ERR(session_keyring);
100 goto error_release;
101 }
102
103 /* we install a link from the user session keyring to
104 * the user keyring */
105 ret = key_link(session_keyring, uid_keyring);
106 if (ret < 0)
107 goto error_release_both;
108 }
109
110 /* install the keyrings */
111 user->uid_keyring = uid_keyring;
112 user->session_keyring = session_keyring;
113 }
114
115 mutex_unlock(&key_user_keyring_mutex);
116 kleave(" = 0");
117 return 0;
118
119error_release_both:
120 key_put(session_keyring);
121error_release:
122 key_put(uid_keyring);
123error:
124 mutex_unlock(&key_user_keyring_mutex);
125 kleave(" = %d", ret);
126 return ret;
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100130 * Install a thread keyring to the given credentials struct if it didn't have
131 * one already. This is allowed to overrun the quota.
132 *
133 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
David Howellsd84f4f92008-11-14 10:39:23 +1100135int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
David Howellsd84f4f92008-11-14 10:39:23 +1100137 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Eric Biggers174a74d2017-04-18 15:31:09 +0100139 if (new->thread_keyring)
140 return 0;
141
David Howellsd84f4f92008-11-14 10:39:23 +1100142 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
David Howells96b5c8f2012-10-02 19:24:56 +0100143 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100144 KEY_ALLOC_QUOTA_OVERRUN,
145 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100146 if (IS_ERR(keyring))
147 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
David Howellsd84f4f92008-11-14 10:39:23 +1100149 new->thread_keyring = keyring;
150 return 0;
151}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100154 * Install a thread keyring to the current task if it didn't have one already.
155 *
156 * Return: 0 if a thread keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
David Howellsd84f4f92008-11-14 10:39:23 +1100158static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
David Howellsd84f4f92008-11-14 10:39:23 +1100160 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int ret;
162
David Howellsd84f4f92008-11-14 10:39:23 +1100163 new = prepare_creds();
164 if (!new)
165 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David Howellsd84f4f92008-11-14 10:39:23 +1100167 ret = install_thread_keyring_to_cred(new);
168 if (ret < 0) {
169 abort_creds(new);
170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
David Howellsd84f4f92008-11-14 10:39:23 +1100173 return commit_creds(new);
174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Howellsd84f4f92008-11-14 10:39:23 +1100176/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100177 * Install a process keyring to the given credentials struct if it didn't have
178 * one already. This is allowed to overrun the quota.
David Howells973c9f42011-01-20 16:38:33 +0000179 *
Eric Biggers174a74d2017-04-18 15:31:09 +0100180 * Return: 0 if a process keyring is now present; -errno on failure.
David Howellsd84f4f92008-11-14 10:39:23 +1100181 */
182int install_process_keyring_to_cred(struct cred *new)
183{
184 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howells3a505972012-10-02 19:24:29 +0100186 if (new->process_keyring)
Eric Biggers174a74d2017-04-18 15:31:09 +0100187 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100188
David Howells96b5c8f2012-10-02 19:24:56 +0100189 keyring = keyring_alloc("_pid", new->uid, new->gid, new,
190 KEY_POS_ALL | KEY_USR_VIEW,
David Howells5ac7eac2016-04-06 16:14:24 +0100191 KEY_ALLOC_QUOTA_OVERRUN,
192 NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100193 if (IS_ERR(keyring))
194 return PTR_ERR(keyring);
195
David Howells3a505972012-10-02 19:24:29 +0100196 new->process_keyring = keyring;
197 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100198}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100201 * Install a process keyring to the current task if it didn't have one already.
David Howells973c9f42011-01-20 16:38:33 +0000202 *
Eric Biggers174a74d2017-04-18 15:31:09 +0100203 * Return: 0 if a process keyring is now present; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
David Howellsd84f4f92008-11-14 10:39:23 +1100205static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
David Howellsd84f4f92008-11-14 10:39:23 +1100207 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 int ret;
209
David Howellsd84f4f92008-11-14 10:39:23 +1100210 new = prepare_creds();
211 if (!new)
212 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700213
David Howellsd84f4f92008-11-14 10:39:23 +1100214 ret = install_process_keyring_to_cred(new);
215 if (ret < 0) {
216 abort_creds(new);
Eric Biggers174a74d2017-04-18 15:31:09 +0100217 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
David Howellsd84f4f92008-11-14 10:39:23 +1100220 return commit_creds(new);
221}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100224 * Install the given keyring as the session keyring of the given credentials
225 * struct, replacing the existing one if any. If the given keyring is NULL,
226 * then install a new anonymous session keyring.
227 *
228 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700230int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
David Howells7e047ef2006-06-26 00:24:50 -0700232 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700234
235 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* create an empty session keyring */
238 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700239 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100240 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700241 flags = KEY_ALLOC_IN_QUOTA;
242
David Howells96b5c8f2012-10-02 19:24:56 +0100243 keyring = keyring_alloc("_ses", cred->uid, cred->gid, cred,
244 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100245 flags, NULL, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700246 if (IS_ERR(keyring))
247 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100248 } else {
David Howellsccc3e6d2013-09-24 10:35:16 +0100249 __key_get(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100253 old = cred->session_keyring;
254 rcu_assign_pointer(cred->session_keyring, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Howells3a505972012-10-02 19:24:29 +0100256 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700257 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
David Howells1a26feb2006-04-10 22:54:26 -0700259 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
Eric Biggers174a74d2017-04-18 15:31:09 +0100263 * Install the given keyring as the session keyring of the current task,
264 * replacing the existing one if any. If the given keyring is NULL, then
265 * install a new anonymous session keyring.
266 *
267 * Return: 0 on success; -errno on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
David Howellsd84f4f92008-11-14 10:39:23 +1100269static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
David Howellsd84f4f92008-11-14 10:39:23 +1100271 struct cred *new;
272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
David Howellsd84f4f92008-11-14 10:39:23 +1100274 new = prepare_creds();
275 if (!new)
276 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800277
David Howells995995372011-08-22 14:08:33 +0100278 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100279 if (ret < 0) {
280 abort_creds(new);
281 return ret;
282 }
David Howellsb5f545c2006-01-08 01:02:47 -0800283
David Howellsd84f4f92008-11-14 10:39:23 +1100284 return commit_creds(new);
285}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*
David Howells973c9f42011-01-20 16:38:33 +0000288 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290void key_fsuid_changed(struct task_struct *tsk)
291{
292 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100293 BUG_ON(!tsk->cred);
294 if (tsk->cred->thread_keyring) {
295 down_write(&tsk->cred->thread_keyring->sem);
296 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
297 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000299}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301/*
David Howells973c9f42011-01-20 16:38:33 +0000302 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 */
304void key_fsgid_changed(struct task_struct *tsk)
305{
306 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100307 BUG_ON(!tsk->cred);
308 if (tsk->cred->thread_keyring) {
309 down_write(&tsk->cred->thread_keyring->sem);
310 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
311 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*
David Howells973c9f42011-01-20 16:38:33 +0000316 * Search the process keyrings attached to the supplied cred for the first
317 * matching key.
318 *
319 * The search criteria are the type and the match function. The description is
320 * given to the match function as a parameter, but doesn't otherwise influence
321 * the search. Typically the match function will compare the description
322 * parameter to the key's description.
323 *
324 * This can only search keyrings that grant Search permission to the supplied
325 * credentials. Keyrings linked to searched keyrings will also be searched if
326 * they grant Search permission too. Keys can only be found if they grant
327 * Search permission to the credentials.
328 *
329 * Returns a pointer to the key with the key usage count incremented if
330 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
331 * matched negative keys.
332 *
333 * In the case of a successful return, the possession attribute is set on the
334 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100336key_ref_t search_my_process_keyrings(struct keyring_search_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
David Howellsb5f545c2006-01-08 01:02:47 -0800338 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
341 * searchable, but we failed to find a key or we found a negative key;
342 * otherwise we want to return a sample error (probably -EACCES) if
343 * none of the keyrings were searchable
344 *
345 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
346 */
David Howells664cceb2005-09-28 17:03:15 +0100347 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 ret = NULL;
349 err = ERR_PTR(-EAGAIN);
350
351 /* search the thread keyring first */
David Howells4bdf0bc2013-09-24 10:35:15 +0100352 if (ctx->cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100353 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100354 make_key_ref(ctx->cred->thread_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100355 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 goto found;
357
David Howells664cceb2005-09-28 17:03:15 +0100358 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100361 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 default:
David Howells664cceb2005-09-28 17:03:15 +0100364 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 break;
366 }
367 }
368
369 /* search the process keyring second */
David Howells4bdf0bc2013-09-24 10:35:15 +0100370 if (ctx->cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100371 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100372 make_key_ref(ctx->cred->process_keyring, 1), ctx);
David Howells664cceb2005-09-28 17:03:15 +0100373 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto found;
375
David Howells664cceb2005-09-28 17:03:15 +0100376 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 case -EAGAIN: /* no key */
David Howellsfe9453a2013-02-21 12:00:25 +0000378 if (ret)
379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100381 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383 default:
David Howells664cceb2005-09-28 17:03:15 +0100384 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 break;
386 }
387 }
388
David Howells3e301482005-06-23 22:00:56 -0700389 /* search the session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100390 if (ctx->cred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700391 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100392 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100393 make_key_ref(rcu_dereference(ctx->cred->session_keyring), 1),
394 ctx);
David Howells8589b4e2005-06-23 22:00:53 -0700395 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
David Howells664cceb2005-09-28 17:03:15 +0100397 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700398 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
David Howells664cceb2005-09-28 17:03:15 +0100400 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700401 case -EAGAIN: /* no key */
402 if (ret)
403 break;
404 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100405 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 break;
David Howells3e301482005-06-23 22:00:56 -0700407 default:
David Howells664cceb2005-09-28 17:03:15 +0100408 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700409 break;
410 }
David Howells3e301482005-06-23 22:00:56 -0700411 }
412 /* or search the user-session keyring */
David Howells4bdf0bc2013-09-24 10:35:15 +0100413 else if (ctx->cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100414 key_ref = keyring_search_aux(
David Howells4bdf0bc2013-09-24 10:35:15 +0100415 make_key_ref(ctx->cred->user->session_keyring, 1),
416 ctx);
David Howells664cceb2005-09-28 17:03:15 +0100417 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700418 goto found;
419
David Howells664cceb2005-09-28 17:03:15 +0100420 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700421 case -EAGAIN: /* no key */
422 if (ret)
423 break;
424 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100425 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700426 break;
427 default:
David Howells664cceb2005-09-28 17:03:15 +0100428 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700429 break;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
David Howells927942a2010-06-11 17:31:10 +0100433 /* no key - decide on the error we're going to go for */
434 key_ref = ret ? ret : err;
435
436found:
437 return key_ref;
438}
439
David Howells927942a2010-06-11 17:31:10 +0100440/*
David Howells973c9f42011-01-20 16:38:33 +0000441 * Search the process keyrings attached to the supplied cred for the first
442 * matching key in the manner of search_my_process_keyrings(), but also search
443 * the keys attached to the assumed authorisation key using its credentials if
444 * one is available.
445 *
446 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100447 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100448key_ref_t search_process_keyrings(struct keyring_search_context *ctx)
David Howells927942a2010-06-11 17:31:10 +0100449{
450 struct request_key_auth *rka;
451 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
452
453 might_sleep();
454
David Howells4bdf0bc2013-09-24 10:35:15 +0100455 key_ref = search_my_process_keyrings(ctx);
David Howells927942a2010-06-11 17:31:10 +0100456 if (!IS_ERR(key_ref))
457 goto found;
458 err = key_ref;
459
David Howellsb5f545c2006-01-08 01:02:47 -0800460 /* if this process has an instantiation authorisation key, then we also
461 * search the keyrings of the process mentioned there
462 * - we don't permit access to request_key auth keys via this method
463 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100464 if (ctx->cred->request_key_auth &&
465 ctx->cred == current_cred() &&
466 ctx->index_key.type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800467 ) {
David Howells4bdf0bc2013-09-24 10:35:15 +0100468 const struct cred *cred = ctx->cred;
469
David Howells04c567d2006-06-22 14:47:18 -0700470 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100471 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700472
David Howells4bdf0bc2013-09-24 10:35:15 +0100473 if (key_validate(ctx->cred->request_key_auth) == 0) {
David Howells146aa8b2015-10-21 14:04:48 +0100474 rka = ctx->cred->request_key_auth->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -0800475
David Howells4bdf0bc2013-09-24 10:35:15 +0100476 ctx->cred = rka->cred;
477 key_ref = search_process_keyrings(ctx);
478 ctx->cred = cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800479
David Howellsc69e8d92008-11-14 10:39:19 +1100480 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700481
482 if (!IS_ERR(key_ref))
483 goto found;
484
David Howells927942a2010-06-11 17:31:10 +0100485 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700486 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100487 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800488 }
489 }
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100492 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
493 key_ref = ERR_PTR(-ENOKEY);
494 else if (err == ERR_PTR(-EACCES))
495 key_ref = ret;
496 else
497 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howells3e301482005-06-23 22:00:56 -0700499found:
David Howells664cceb2005-09-28 17:03:15 +0100500 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*
David Howells973c9f42011-01-20 16:38:33 +0000504 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100505 */
David Howells0c903ab2014-09-16 17:36:08 +0100506bool lookup_user_key_possessed(const struct key *key,
507 const struct key_match_data *match_data)
David Howells664cceb2005-09-28 17:03:15 +0100508{
David Howells46291952014-09-16 17:36:02 +0100509 return key == match_data->raw_data;
David Howellsa8b17ed2011-01-20 16:38:27 +0000510}
David Howells664cceb2005-09-28 17:03:15 +0100511
David Howells664cceb2005-09-28 17:03:15 +0100512/*
David Howells973c9f42011-01-20 16:38:33 +0000513 * Look up a key ID given us by userspace with a given permissions mask to get
514 * the key it refers to.
515 *
516 * Flags can be passed to request that special keyrings be created if referred
517 * to directly, to permit partially constructed keys to be found and to skip
518 * validity and permission checks on the found key.
519 *
520 * Returns a pointer to the key with an incremented usage count if successful;
521 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
522 * to a key or the best found key was a negative key; -EKEYREVOKED or
523 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
524 * found key doesn't grant the requested permit or the LSM denied access to it;
525 * or -ENOMEM if a special keyring couldn't be created.
526 *
527 * In the case of a successful return, the possession attribute is set on the
528 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
David Howells55931222009-09-02 09:13:45 +0100530key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100531 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Howells4bdf0bc2013-09-24 10:35:15 +0100533 struct keyring_search_context ctx = {
David Howells46291952014-09-16 17:36:02 +0100534 .match_data.cmp = lookup_user_key_possessed,
535 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
536 .flags = KEYRING_SEARCH_NO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100537 };
David Howells8bbf49762008-11-14 10:39:14 +1100538 struct request_key_auth *rka;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100540 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int ret;
542
David Howellsbb952bb2008-11-14 10:39:20 +1100543try_again:
David Howells4bdf0bc2013-09-24 10:35:15 +0100544 ctx.cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100545 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 switch (id) {
548 case KEY_SPEC_THREAD_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100549 if (!ctx.cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100550 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto error;
552
David Howells8bbf49762008-11-14 10:39:14 +1100553 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100555 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto error;
557 }
David Howellsbb952bb2008-11-14 10:39:20 +1100558 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
560
David Howells4bdf0bc2013-09-24 10:35:15 +0100561 key = ctx.cred->thread_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100562 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100563 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 break;
565
566 case KEY_SPEC_PROCESS_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100567 if (!ctx.cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100568 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 goto error;
570
David Howells8bbf49762008-11-14 10:39:14 +1100571 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100573 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 goto error;
575 }
David Howellsbb952bb2008-11-14 10:39:20 +1100576 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
David Howells4bdf0bc2013-09-24 10:35:15 +0100579 key = ctx.cred->process_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100580 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100581 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583
584 case KEY_SPEC_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100585 if (!ctx.cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* always install a session keyring upon access if one
587 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100588 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700589 if (ret < 0)
590 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100591 if (lflags & KEY_LOOKUP_CREATE)
592 ret = join_session_keyring(NULL);
593 else
594 ret = install_session_keyring(
David Howells4bdf0bc2013-09-24 10:35:15 +0100595 ctx.cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (ret < 0)
598 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100599 goto reget_creds;
David Howells4bdf0bc2013-09-24 10:35:15 +0100600 } else if (ctx.cred->session_keyring ==
601 ctx.cred->user->session_keyring &&
David Howells3ecf1b42011-08-22 14:08:43 +0100602 lflags & KEY_LOOKUP_CREATE) {
603 ret = join_session_keyring(NULL);
604 if (ret < 0)
605 goto error;
606 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
David Howells3e301482005-06-23 22:00:56 -0700609 rcu_read_lock();
David Howells4bdf0bc2013-09-24 10:35:15 +0100610 key = rcu_dereference(ctx.cred->session_keyring);
David Howellsccc3e6d2013-09-24 10:35:16 +0100611 __key_get(key);
David Howells3e301482005-06-23 22:00:56 -0700612 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100613 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 break;
615
616 case KEY_SPEC_USER_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100617 if (!ctx.cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100618 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700619 if (ret < 0)
620 goto error;
621 }
622
David Howells4bdf0bc2013-09-24 10:35:15 +0100623 key = ctx.cred->user->uid_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100624 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100625 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 break;
627
628 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100629 if (!ctx.cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100630 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700631 if (ret < 0)
632 goto error;
633 }
634
David Howells4bdf0bc2013-09-24 10:35:15 +0100635 key = ctx.cred->user->session_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100636 __key_get(key);
David Howells664cceb2005-09-28 17:03:15 +0100637 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639
640 case KEY_SPEC_GROUP_KEYRING:
641 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100642 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto error;
644
David Howellsb5f545c2006-01-08 01:02:47 -0800645 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howells4bdf0bc2013-09-24 10:35:15 +0100646 key = ctx.cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800647 if (!key)
648 goto error;
649
David Howellsccc3e6d2013-09-24 10:35:16 +0100650 __key_get(key);
David Howellsb5f545c2006-01-08 01:02:47 -0800651 key_ref = make_key_ref(key, 1);
652 break;
653
David Howells8bbf49762008-11-14 10:39:14 +1100654 case KEY_SPEC_REQUESTOR_KEYRING:
David Howells4bdf0bc2013-09-24 10:35:15 +0100655 if (!ctx.cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100656 goto error;
657
David Howells4bdf0bc2013-09-24 10:35:15 +0100658 down_read(&ctx.cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000659 if (test_bit(KEY_FLAG_REVOKED,
David Howells4bdf0bc2013-09-24 10:35:15 +0100660 &ctx.cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100661 key_ref = ERR_PTR(-EKEYREVOKED);
662 key = NULL;
663 } else {
David Howells146aa8b2015-10-21 14:04:48 +0100664 rka = ctx.cred->request_key_auth->payload.data[0];
David Howells8bbf49762008-11-14 10:39:14 +1100665 key = rka->dest_keyring;
David Howellsccc3e6d2013-09-24 10:35:16 +0100666 __key_get(key);
David Howells8bbf49762008-11-14 10:39:14 +1100667 }
David Howells4bdf0bc2013-09-24 10:35:15 +0100668 up_read(&ctx.cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100669 if (!key)
670 goto error;
671 key_ref = make_key_ref(key, 1);
672 break;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 default:
David Howells664cceb2005-09-28 17:03:15 +0100675 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (id < 1)
677 goto error;
678
679 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100680 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800681 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100683 }
684
685 key_ref = make_key_ref(key, 0);
686
687 /* check to see if we possess the key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100688 ctx.index_key.type = key->type;
689 ctx.index_key.description = key->description;
690 ctx.index_key.desc_len = strlen(key->description);
David Howells46291952014-09-16 17:36:02 +0100691 ctx.match_data.raw_data = key;
David Howells4bdf0bc2013-09-24 10:35:15 +0100692 kdebug("check possessed");
693 skey_ref = search_process_keyrings(&ctx);
694 kdebug("possessed=%p", skey_ref);
David Howells664cceb2005-09-28 17:03:15 +0100695
696 if (!IS_ERR(skey_ref)) {
697 key_put(key);
698 key_ref = skey_ref;
699 }
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
703
David Howells55931222009-09-02 09:13:45 +0100704 /* unlink does not use the nominated key in any way, so can skip all
705 * the permission checks as it is only concerned with the keyring */
706 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
707 ret = 0;
708 goto error;
709 }
710
711 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700712 ret = wait_for_key_construction(key, true);
713 switch (ret) {
714 case -ERESTARTSYS:
715 goto invalid_key;
716 default:
717 if (perm)
718 goto invalid_key;
719 case 0:
720 break;
721 }
722 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 ret = key_validate(key);
724 if (ret < 0)
725 goto invalid_key;
726 }
727
728 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100729 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
730 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto invalid_key;
732
David Howells3e301482005-06-23 22:00:56 -0700733 /* check the permissions */
David Howells4bdf0bc2013-09-24 10:35:15 +0100734 ret = key_task_permission(key_ref, ctx.cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800735 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 goto invalid_key;
737
David Howells31d5a792012-05-11 10:56:56 +0100738 key->last_used_at = current_kernel_time().tv_sec;
739
David Howells664cceb2005-09-28 17:03:15 +0100740error:
David Howells4bdf0bc2013-09-24 10:35:15 +0100741 put_cred(ctx.cred);
David Howells664cceb2005-09-28 17:03:15 +0100742 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
David Howells664cceb2005-09-28 17:03:15 +0100744invalid_key:
745 key_ref_put(key_ref);
746 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto error;
748
David Howellsbb952bb2008-11-14 10:39:20 +1100749 /* if we attempted to install a keyring, then it may have caused new
750 * creds to be installed */
751reget_creds:
David Howells4bdf0bc2013-09-24 10:35:15 +0100752 put_cred(ctx.cred);
David Howellsbb952bb2008-11-14 10:39:20 +1100753 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000754}
David Howellsbb952bb2008-11-14 10:39:20 +1100755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756/*
David Howells973c9f42011-01-20 16:38:33 +0000757 * Join the named keyring as the session keyring if possible else attempt to
758 * create a new one of that name and join that.
759 *
760 * If the name is NULL, an empty anonymous keyring will be installed as the
761 * session keyring.
762 *
763 * Named session keyrings are joined with a semaphore held to prevent the
764 * keyrings from going away whilst the attempt is made to going them and also
765 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767long join_session_keyring(const char *name)
768{
David Howellsd84f4f92008-11-14 10:39:23 +1100769 const struct cred *old;
770 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100772 long ret, serial;
773
David Howellsd84f4f92008-11-14 10:39:23 +1100774 new = prepare_creds();
775 if (!new)
776 return -ENOMEM;
777 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 /* if no name is provided, install an anonymous keyring */
780 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100781 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (ret < 0)
783 goto error;
784
David Howells3a505972012-10-02 19:24:29 +0100785 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100786 ret = commit_creds(new);
787 if (ret == 0)
788 ret = serial;
789 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800793 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700796 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (PTR_ERR(keyring) == -ENOKEY) {
798 /* not found - try and create a new one */
David Howells96b5c8f2012-10-02 19:24:56 +0100799 keyring = keyring_alloc(
800 name, old->uid, old->gid, old,
801 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ | KEY_USR_LINK,
David Howells5ac7eac2016-04-06 16:14:24 +0100802 KEY_ALLOC_IN_QUOTA, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (IS_ERR(keyring)) {
804 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700805 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
David Howellsd84f4f92008-11-14 10:39:23 +1100807 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ret = PTR_ERR(keyring);
809 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100810 } else if (keyring == new->session_keyring) {
Yevgeny Pats23567fd2016-01-19 22:09:04 +0000811 key_put(keyring);
David Howells3a505972012-10-02 19:24:29 +0100812 ret = 0;
813 goto error2;
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)
819 goto error2;
820
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
David Howells664cceb2005-09-28 17:03:15 +0100829error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800830 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100831error:
David Howellsd84f4f92008-11-14 10:39:23 +1100832 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100834}
David Howellsee18d642009-09-02 09:14:21 +0100835
836/*
David Howells973c9f42011-01-20 16:38:33 +0000837 * Replace a process's session keyring on behalf of one of its children when
838 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100839 */
Al Viro67d12142012-06-27 11:07:19 +0400840void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100841{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000842 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400843 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100844
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000845 if (unlikely(current->flags & PF_EXITING)) {
846 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100847 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000848 }
David Howellsee18d642009-09-02 09:14:21 +0100849
David Howellsee18d642009-09-02 09:14:21 +0100850 new-> uid = old-> uid;
851 new-> euid = old-> euid;
852 new-> suid = old-> suid;
853 new->fsuid = old->fsuid;
854 new-> gid = old-> gid;
855 new-> egid = old-> egid;
856 new-> sgid = old-> sgid;
857 new->fsgid = old->fsgid;
858 new->user = get_uid(old->user);
Eric W. Biedermanba0e3422013-03-02 19:14:03 -0800859 new->user_ns = get_user_ns(old->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100860 new->group_info = get_group_info(old->group_info);
861
862 new->securebits = old->securebits;
863 new->cap_inheritable = old->cap_inheritable;
864 new->cap_permitted = old->cap_permitted;
865 new->cap_effective = old->cap_effective;
Andy Lutomirski58319052015-09-04 15:42:45 -0700866 new->cap_ambient = old->cap_ambient;
David Howellsee18d642009-09-02 09:14:21 +0100867 new->cap_bset = old->cap_bset;
868
869 new->jit_keyring = old->jit_keyring;
870 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100871 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100872
873 security_transfer_creds(new, old);
874
875 commit_creds(new);
876}
Mimi Zoharc124bde2013-09-04 13:26:22 +0100877
878/*
879 * Make sure that root's user and user-session keyrings exist.
880 */
881static int __init init_root_keyring(void)
882{
883 return install_user_keyrings();
884}
885
886late_initcall(init_root_keyring);