blob: 5c23afb31ece464ad0165e1a3fb052a8969244c5 [file] [log] [blame]
David Howells69664cf2008-04-29 01:01:31 -07001/* Management of 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>
15#include <linux/slab.h>
16#include <linux/keyctl.h>
17#include <linux/fs.h>
18#include <linux/err.h>
Ingo Molnarbb003072006-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 Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/uaccess.h>
23#include "internal.h"
24
25/* session keyring create vs join semaphore */
Ingo Molnarbb003072006-03-22 00:09:14 -080026static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
David Howells69664cf2008-04-29 01:01:31 -070028/* user keyring creation semaphore */
29static DEFINE_MUTEX(key_user_keyring_mutex);
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/* the root user's tracking struct */
32struct key_user root_key_user = {
33 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070034 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080035 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 .nkeys = ATOMIC_INIT(2),
37 .nikeys = ATOMIC_INIT(2),
38 .uid = 0,
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060039 .user_ns = &init_user_ns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070040};
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/*****************************************************************************/
43/*
David Howells69664cf2008-04-29 01:01:31 -070044 * install user and user session keyrings for a particular UID
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
David Howells8bbf49762008-11-14 10:39:14 +110046int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
David Howellsd84f4f92008-11-14 10:39:23 +110048 struct user_struct *user;
49 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct key *uid_keyring, *session_keyring;
51 char buf[20];
52 int ret;
53
David Howellsd84f4f92008-11-14 10:39:23 +110054 cred = current_cred();
55 user = cred->user;
56
David Howells69664cf2008-04-29 01:01:31 -070057 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
David Howells69664cf2008-04-29 01:01:31 -070059 if (user->uid_keyring) {
60 kleave(" = 0 [exist]");
61 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
63
David Howells69664cf2008-04-29 01:01:31 -070064 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 ret = 0;
66
David Howells69664cf2008-04-29 01:01:31 -070067 if (!user->uid_keyring) {
68 /* get the UID-specific keyring
69 * - there may be one in existence already as it may have been
70 * pinned by a session, but the user_struct pointing to it
71 * may have been destroyed by setuid */
72 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
David Howells69664cf2008-04-29 01:01:31 -070074 uid_keyring = find_keyring_by_name(buf, true);
75 if (IS_ERR(uid_keyring)) {
76 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110077 cred, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070078 NULL);
79 if (IS_ERR(uid_keyring)) {
80 ret = PTR_ERR(uid_keyring);
81 goto error;
82 }
83 }
84
85 /* get a default session keyring (which might also exist
86 * already) */
87 sprintf(buf, "_uid_ses.%u", user->uid);
88
89 session_keyring = find_keyring_by_name(buf, true);
90 if (IS_ERR(session_keyring)) {
91 session_keyring =
92 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110093 cred, KEY_ALLOC_IN_QUOTA, NULL);
David Howells69664cf2008-04-29 01:01:31 -070094 if (IS_ERR(session_keyring)) {
95 ret = PTR_ERR(session_keyring);
96 goto error_release;
97 }
98
99 /* we install a link from the user session keyring to
100 * the user keyring */
101 ret = key_link(session_keyring, uid_keyring);
102 if (ret < 0)
103 goto error_release_both;
104 }
105
106 /* install the keyrings */
107 user->uid_keyring = uid_keyring;
108 user->session_keyring = session_keyring;
109 }
110
111 mutex_unlock(&key_user_keyring_mutex);
112 kleave(" = 0");
113 return 0;
114
115error_release_both:
116 key_put(session_keyring);
117error_release:
118 key_put(uid_keyring);
119error:
120 mutex_unlock(&key_user_keyring_mutex);
121 kleave(" = %d", ret);
122 return ret;
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
David Howellsd84f4f92008-11-14 10:39:23 +1100126 * install a fresh thread keyring directly to new credentials
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
David Howellsd84f4f92008-11-14 10:39:23 +1100128int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
David Howellsd84f4f92008-11-14 10:39:23 +1100130 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
David Howellsd84f4f92008-11-14 10:39:23 +1100132 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
133 KEY_ALLOC_QUOTA_OVERRUN, NULL);
134 if (IS_ERR(keyring))
135 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
David Howellsd84f4f92008-11-14 10:39:23 +1100137 new->thread_keyring = keyring;
138 return 0;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
142 * install a fresh thread keyring, discarding the old one
143 */
David Howellsd84f4f92008-11-14 10:39:23 +1100144static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
David Howellsd84f4f92008-11-14 10:39:23 +1100146 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int ret;
148
David Howellsd84f4f92008-11-14 10:39:23 +1100149 new = prepare_creds();
150 if (!new)
151 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
David Howellsd84f4f92008-11-14 10:39:23 +1100153 BUG_ON(new->thread_keyring);
154
155 ret = install_thread_keyring_to_cred(new);
156 if (ret < 0) {
157 abort_creds(new);
158 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
David Howellsd84f4f92008-11-14 10:39:23 +1100161 return commit_creds(new);
162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David Howellsd84f4f92008-11-14 10:39:23 +1100164/*
165 * install a process keyring directly to a credentials struct
166 * - returns -EEXIST if there was already a process keyring, 0 if one installed,
167 * and other -ve on any other error
168 */
169int install_process_keyring_to_cred(struct cred *new)
170{
171 struct key *keyring;
172 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David Howellsd84f4f92008-11-14 10:39:23 +1100174 if (new->tgcred->process_keyring)
175 return -EEXIST;
176
177 keyring = keyring_alloc("_pid", new->uid, new->gid,
178 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
179 if (IS_ERR(keyring))
180 return PTR_ERR(keyring);
181
182 spin_lock_irq(&new->tgcred->lock);
183 if (!new->tgcred->process_keyring) {
184 new->tgcred->process_keyring = keyring;
185 keyring = NULL;
186 ret = 0;
187 } else {
188 ret = -EEXIST;
189 }
190 spin_unlock_irq(&new->tgcred->lock);
191 key_put(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100193}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195/*
196 * make sure a process keyring is installed
David Howellsd84f4f92008-11-14 10:39:23 +1100197 * - we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 */
David Howellsd84f4f92008-11-14 10:39:23 +1100199static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
David Howellsd84f4f92008-11-14 10:39:23 +1100201 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 int ret;
203
David Howellsd84f4f92008-11-14 10:39:23 +1100204 new = prepare_creds();
205 if (!new)
206 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700207
David Howellsd84f4f92008-11-14 10:39:23 +1100208 ret = install_process_keyring_to_cred(new);
209 if (ret < 0) {
210 abort_creds(new);
211 return ret != -EEXIST ?: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
David Howellsd84f4f92008-11-14 10:39:23 +1100214 return commit_creds(new);
215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
David Howellsd84f4f92008-11-14 10:39:23 +1100218 * install a session keyring directly to a credentials struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
David Howellsd84f4f92008-11-14 10:39:23 +1100220static int install_session_keyring_to_cred(struct cred *cred,
221 struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
David Howells7e047ef2006-06-26 00:24:50 -0700223 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700225
226 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* create an empty session keyring */
229 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700230 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howellsd84f4f92008-11-14 10:39:23 +1100231 if (cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700232 flags = KEY_ALLOC_IN_QUOTA;
233
David Howellsd84f4f92008-11-14 10:39:23 +1100234 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
235 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700236 if (IS_ERR(keyring))
237 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100238 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 atomic_inc(&keyring->usage);
240 }
241
242 /* install the keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100243 spin_lock_irq(&cred->tgcred->lock);
244 old = cred->tgcred->session_keyring;
245 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
246 spin_unlock_irq(&cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
David Howells1a26feb2006-04-10 22:54:26 -0700248 /* we're using RCU on the pointer, but there's no point synchronising
249 * on it if it didn't previously point to anything */
250 if (old) {
251 synchronize_rcu();
252 key_put(old);
253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David Howells1a26feb2006-04-10 22:54:26 -0700255 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100256}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
David Howellsd84f4f92008-11-14 10:39:23 +1100259 * install a session keyring, discarding the old one
260 * - if a keyring is not supplied, an empty one is invented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
David Howellsd84f4f92008-11-14 10:39:23 +1100262static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
David Howellsd84f4f92008-11-14 10:39:23 +1100264 struct cred *new;
265 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
David Howellsd84f4f92008-11-14 10:39:23 +1100267 new = prepare_creds();
268 if (!new)
269 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800270
David Howellsd84f4f92008-11-14 10:39:23 +1100271 ret = install_session_keyring_to_cred(new, NULL);
272 if (ret < 0) {
273 abort_creds(new);
274 return ret;
275 }
David Howellsb5f545c2006-01-08 01:02:47 -0800276
David Howellsd84f4f92008-11-14 10:39:23 +1100277 return commit_creds(new);
278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280/*****************************************************************************/
281/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * the filesystem user ID changed
283 */
284void key_fsuid_changed(struct task_struct *tsk)
285{
286 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100287 BUG_ON(!tsk->cred);
288 if (tsk->cred->thread_keyring) {
289 down_write(&tsk->cred->thread_keyring->sem);
290 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
291 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
294} /* end key_fsuid_changed() */
295
296/*****************************************************************************/
297/*
298 * the filesystem group ID changed
299 */
300void key_fsgid_changed(struct task_struct *tsk)
301{
302 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100303 BUG_ON(!tsk->cred);
304 if (tsk->cred->thread_keyring) {
305 down_write(&tsk->cred->thread_keyring->sem);
306 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
307 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309
310} /* end key_fsgid_changed() */
311
312/*****************************************************************************/
313/*
314 * search the process keyrings for the first matching key
315 * - we use the supplied match function to see if the description (or other
316 * feature of interest) matches
317 * - we return -EAGAIN if we didn't find any matching key
318 * - we return -ENOKEY if we found only negative matching keys
319 */
David Howells664cceb2005-09-28 17:03:15 +0100320key_ref_t search_process_keyrings(struct key_type *type,
321 const void *description,
322 key_match_func_t match,
David Howellsd84f4f92008-11-14 10:39:23 +1100323 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
David Howells3e301482005-06-23 22:00:56 -0700325 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800326 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
David Howells04c567d2006-06-22 14:47:18 -0700328 might_sleep();
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
331 * searchable, but we failed to find a key or we found a negative key;
332 * otherwise we want to return a sample error (probably -EACCES) if
333 * none of the keyrings were searchable
334 *
335 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
336 */
David Howells664cceb2005-09-28 17:03:15 +0100337 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ret = NULL;
339 err = ERR_PTR(-EAGAIN);
340
341 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100342 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100343 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100344 make_key_ref(cred->thread_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100345 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100346 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 goto found;
348
David Howells664cceb2005-09-28 17:03:15 +0100349 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 case -EAGAIN: /* no key */
351 if (ret)
352 break;
353 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100354 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 default:
David Howells664cceb2005-09-28 17:03:15 +0100357 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 }
360 }
361
362 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100363 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100364 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100365 make_key_ref(cred->tgcred->process_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100366 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100367 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 goto found;
369
David Howells664cceb2005-09-28 17:03:15 +0100370 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 case -EAGAIN: /* no key */
372 if (ret)
373 break;
374 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100375 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 default:
David Howells664cceb2005-09-28 17:03:15 +0100378 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 }
381 }
382
David Howells3e301482005-06-23 22:00:56 -0700383 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100384 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700385 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100386 key_ref = keyring_search_aux(
387 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100388 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100389 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100390 cred, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howells664cceb2005-09-28 17:03:15 +0100393 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700394 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
David Howells664cceb2005-09-28 17:03:15 +0100396 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700397 case -EAGAIN: /* no key */
398 if (ret)
399 break;
400 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100401 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
David Howells3e301482005-06-23 22:00:56 -0700403 default:
David Howells664cceb2005-09-28 17:03:15 +0100404 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700405 break;
406 }
David Howells3e301482005-06-23 22:00:56 -0700407 }
408 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100409 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100410 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100411 make_key_ref(cred->user->session_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100412 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100413 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700414 goto found;
415
David Howells664cceb2005-09-28 17:03:15 +0100416 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700417 case -EAGAIN: /* no key */
418 if (ret)
419 break;
420 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100421 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700422 break;
423 default:
David Howells664cceb2005-09-28 17:03:15 +0100424 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700425 break;
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
David Howellsb5f545c2006-01-08 01:02:47 -0800429 /* if this process has an instantiation authorisation key, then we also
430 * search the keyrings of the process mentioned there
431 * - we don't permit access to request_key auth keys via this method
432 */
David Howellsc69e8d92008-11-14 10:39:19 +1100433 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100434 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700435 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800436 ) {
David Howells04c567d2006-06-22 14:47:18 -0700437 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100438 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700439
David Howellsc69e8d92008-11-14 10:39:19 +1100440 if (key_validate(cred->request_key_auth) == 0) {
441 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800442
David Howells04c567d2006-06-22 14:47:18 -0700443 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100444 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800445
David Howellsc69e8d92008-11-14 10:39:19 +1100446 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700447
448 if (!IS_ERR(key_ref))
449 goto found;
450
451 switch (PTR_ERR(key_ref)) {
452 case -EAGAIN: /* no key */
453 if (ret)
454 break;
455 case -ENOKEY: /* negative key */
456 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800457 break;
David Howells04c567d2006-06-22 14:47:18 -0700458 default:
459 err = key_ref;
460 break;
461 }
462 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100463 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800464 }
465 }
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100468 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
David Howells3e301482005-06-23 22:00:56 -0700470found:
David Howells664cceb2005-09-28 17:03:15 +0100471 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473} /* end search_process_keyrings() */
474
475/*****************************************************************************/
476/*
David Howells664cceb2005-09-28 17:03:15 +0100477 * see if the key we're looking at is the target key
478 */
479static int lookup_user_key_possessed(const struct key *key, const void *target)
480{
481 return key == target;
482
483} /* end lookup_user_key_possessed() */
484
485/*****************************************************************************/
486/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 * lookup a key given a key ID from userspace with a given permissions mask
488 * - don't create special keyrings unless so requested
489 * - partially constructed keys aren't found unless requested
490 */
David Howells55931222009-09-02 09:13:45 +0100491key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100492 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Howells8bbf49762008-11-14 10:39:14 +1100494 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100495 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100497 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 int ret;
499
David Howellsbb952bb2008-11-14 10:39:20 +1100500try_again:
501 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100502 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 switch (id) {
505 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100506 if (!cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100507 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto error;
509
David Howells8bbf49762008-11-14 10:39:14 +1100510 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (ret < 0) {
512 key = ERR_PTR(ret);
513 goto error;
514 }
David Howellsbb952bb2008-11-14 10:39:20 +1100515 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
David Howellsb6dff3e2008-11-14 10:39:16 +1100518 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100520 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 break;
522
523 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100524 if (!cred->tgcred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100525 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 goto error;
527
David Howells8bbf49762008-11-14 10:39:14 +1100528 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (ret < 0) {
530 key = ERR_PTR(ret);
531 goto error;
532 }
David Howellsbb952bb2008-11-14 10:39:20 +1100533 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
David Howellsbb952bb2008-11-14 10:39:20 +1100536 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100538 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 break;
540
541 case KEY_SPEC_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100542 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /* always install a session keyring upon access if one
544 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100545 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700546 if (ret < 0)
547 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100548 ret = install_session_keyring(
549 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (ret < 0)
552 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100553 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
555
David Howells3e301482005-06-23 22:00:56 -0700556 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100557 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700559 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100560 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562
563 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100564 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100565 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700566 if (ret < 0)
567 goto error;
568 }
569
David Howellsb6dff3e2008-11-14 10:39:16 +1100570 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100572 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 break;
574
575 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100576 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100577 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700578 if (ret < 0)
579 goto error;
580 }
581
David Howellsb6dff3e2008-11-14 10:39:16 +1100582 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100584 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 break;
586
587 case KEY_SPEC_GROUP_KEYRING:
588 /* group keyrings are not yet supported */
589 key = ERR_PTR(-EINVAL);
590 goto error;
591
David Howellsb5f545c2006-01-08 01:02:47 -0800592 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100593 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800594 if (!key)
595 goto error;
596
597 atomic_inc(&key->usage);
598 key_ref = make_key_ref(key, 1);
599 break;
600
David Howells8bbf49762008-11-14 10:39:14 +1100601 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100602 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100603 goto error;
604
David Howellsb6dff3e2008-11-14 10:39:16 +1100605 down_read(&cred->request_key_auth->sem);
606 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100607 key_ref = ERR_PTR(-EKEYREVOKED);
608 key = NULL;
609 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100610 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100611 key = rka->dest_keyring;
612 atomic_inc(&key->usage);
613 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100614 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100615 if (!key)
616 goto error;
617 key_ref = make_key_ref(key, 1);
618 break;
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 default:
David Howells664cceb2005-09-28 17:03:15 +0100621 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (id < 1)
623 goto error;
624
625 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100626 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800627 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100629 }
630
631 key_ref = make_key_ref(key, 0);
632
633 /* check to see if we possess the key */
634 skey_ref = search_process_keyrings(key->type, key,
635 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100636 cred);
David Howells664cceb2005-09-28 17:03:15 +0100637
638 if (!IS_ERR(skey_ref)) {
639 key_put(key);
640 key_ref = skey_ref;
641 }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 break;
644 }
645
David Howells55931222009-09-02 09:13:45 +0100646 /* unlink does not use the nominated key in any way, so can skip all
647 * the permission checks as it is only concerned with the keyring */
648 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
649 ret = 0;
650 goto error;
651 }
652
653 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700654 ret = wait_for_key_construction(key, true);
655 switch (ret) {
656 case -ERESTARTSYS:
657 goto invalid_key;
658 default:
659 if (perm)
660 goto invalid_key;
661 case 0:
662 break;
663 }
664 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ret = key_validate(key);
666 if (ret < 0)
667 goto invalid_key;
668 }
669
670 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100671 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
672 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto invalid_key;
674
David Howells3e301482005-06-23 22:00:56 -0700675 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100676 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800677 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto invalid_key;
679
David Howells664cceb2005-09-28 17:03:15 +0100680error:
David Howellsbb952bb2008-11-14 10:39:20 +1100681 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100682 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
David Howells664cceb2005-09-28 17:03:15 +0100684invalid_key:
685 key_ref_put(key_ref);
686 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 goto error;
688
David Howellsbb952bb2008-11-14 10:39:20 +1100689 /* if we attempted to install a keyring, then it may have caused new
690 * creds to be installed */
691reget_creds:
692 put_cred(cred);
693 goto try_again;
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695} /* end lookup_user_key() */
696
697/*****************************************************************************/
698/*
699 * join the named keyring as the session keyring if possible, or attempt to
700 * create a new one of that name if not
701 * - if the name is NULL, an empty anonymous keyring is installed instead
702 * - named session keyring joining is done with a semaphore held
703 */
704long join_session_keyring(const char *name)
705{
David Howellsd84f4f92008-11-14 10:39:23 +1100706 const struct cred *old;
707 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100709 long ret, serial;
710
711 /* only permit this if there's a single thread in the thread group -
712 * this avoids us having to adjust the creds on all threads and risking
713 * ENOMEM */
Oleg Nesterov5bb459b2009-07-10 03:48:23 +0200714 if (!current_is_single_threaded())
David Howellsd84f4f92008-11-14 10:39:23 +1100715 return -EMLINK;
716
717 new = prepare_creds();
718 if (!new)
719 return -ENOMEM;
720 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* if no name is provided, install an anonymous keyring */
723 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100724 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (ret < 0)
726 goto error;
727
David Howellsd84f4f92008-11-14 10:39:23 +1100728 serial = new->tgcred->session_keyring->serial;
729 ret = commit_creds(new);
730 if (ret == 0)
731 ret = serial;
732 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734
735 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800736 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700739 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (PTR_ERR(keyring) == -ENOKEY) {
741 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100742 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700743 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (IS_ERR(keyring)) {
745 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700746 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
David Howellsd84f4f92008-11-14 10:39:23 +1100748 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 ret = PTR_ERR(keyring);
750 goto error2;
751 }
752
753 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100754 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (ret < 0)
756 goto error2;
757
David Howellsd84f4f92008-11-14 10:39:23 +1100758 commit_creds(new);
759 mutex_unlock(&key_session_mutex);
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ret = keyring->serial;
762 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100763okay:
764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
David Howells664cceb2005-09-28 17:03:15 +0100766error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800767 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100768error:
David Howellsd84f4f92008-11-14 10:39:23 +1100769 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100771}
David Howellsee18d642009-09-02 09:14:21 +0100772
773/*
774 * Replace a process's session keyring when that process resumes userspace on
775 * behalf of one of its children
776 */
777void key_replace_session_keyring(void)
778{
779 const struct cred *old;
780 struct cred *new;
781
782 if (!current->replacement_session_keyring)
783 return;
784
785 write_lock_irq(&tasklist_lock);
786 new = current->replacement_session_keyring;
787 current->replacement_session_keyring = NULL;
788 write_unlock_irq(&tasklist_lock);
789
790 if (!new)
791 return;
792
793 old = current_cred();
794 new-> uid = old-> uid;
795 new-> euid = old-> euid;
796 new-> suid = old-> suid;
797 new->fsuid = old->fsuid;
798 new-> gid = old-> gid;
799 new-> egid = old-> egid;
800 new-> sgid = old-> sgid;
801 new->fsgid = old->fsgid;
802 new->user = get_uid(old->user);
803 new->group_info = get_group_info(old->group_info);
804
805 new->securebits = old->securebits;
806 new->cap_inheritable = old->cap_inheritable;
807 new->cap_permitted = old->cap_permitted;
808 new->cap_effective = old->cap_effective;
809 new->cap_bset = old->cap_bset;
810
811 new->jit_keyring = old->jit_keyring;
812 new->thread_keyring = key_get(old->thread_keyring);
813 new->tgcred->tgid = old->tgcred->tgid;
814 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
815
816 security_transfer_creds(new, old);
817
818 commit_creds(new);
819}