blob: df329f684a6545cd49f9e47618eaf9725a87afd9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21#include "internal.h"
22
23/* session keyring create vs join semaphore */
Ingo Molnarbb003072006-03-22 00:09:14 -080024static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Howells69664cf2008-04-29 01:01:31 -070026/* user keyring creation semaphore */
27static DEFINE_MUTEX(key_user_keyring_mutex);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* the root user's tracking struct */
30struct key_user root_key_user = {
31 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070032 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080033 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .nkeys = ATOMIC_INIT(2),
35 .nikeys = ATOMIC_INIT(2),
36 .uid = 0,
37};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*****************************************************************************/
40/*
David Howells69664cf2008-04-29 01:01:31 -070041 * install user and user session keyrings for a particular 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;
48 char buf[20];
49 int ret;
50
David Howellsd84f4f92008-11-14 10:39:23 +110051 cred = current_cred();
52 user = cred->user;
53
David Howells69664cf2008-04-29 01:01:31 -070054 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Howells69664cf2008-04-29 01:01:31 -070056 if (user->uid_keyring) {
57 kleave(" = 0 [exist]");
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
David Howells69664cf2008-04-29 01:01:31 -070061 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 ret = 0;
63
David Howells69664cf2008-04-29 01:01:31 -070064 if (!user->uid_keyring) {
65 /* get the UID-specific keyring
66 * - there may be one in existence already as it may have been
67 * pinned by a session, but the user_struct pointing to it
68 * may have been destroyed by setuid */
69 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Howells69664cf2008-04-29 01:01:31 -070071 uid_keyring = find_keyring_by_name(buf, true);
72 if (IS_ERR(uid_keyring)) {
73 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110074 cred, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070075 NULL);
76 if (IS_ERR(uid_keyring)) {
77 ret = PTR_ERR(uid_keyring);
78 goto error;
79 }
80 }
81
82 /* get a default session keyring (which might also exist
83 * already) */
84 sprintf(buf, "_uid_ses.%u", user->uid);
85
86 session_keyring = find_keyring_by_name(buf, true);
87 if (IS_ERR(session_keyring)) {
88 session_keyring =
89 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110090 cred, KEY_ALLOC_IN_QUOTA, NULL);
David Howells69664cf2008-04-29 01:01:31 -070091 if (IS_ERR(session_keyring)) {
92 ret = PTR_ERR(session_keyring);
93 goto error_release;
94 }
95
96 /* we install a link from the user session keyring to
97 * the user keyring */
98 ret = key_link(session_keyring, uid_keyring);
99 if (ret < 0)
100 goto error_release_both;
101 }
102
103 /* install the keyrings */
104 user->uid_keyring = uid_keyring;
105 user->session_keyring = session_keyring;
106 }
107
108 mutex_unlock(&key_user_keyring_mutex);
109 kleave(" = 0");
110 return 0;
111
112error_release_both:
113 key_put(session_keyring);
114error_release:
115 key_put(uid_keyring);
116error:
117 mutex_unlock(&key_user_keyring_mutex);
118 kleave(" = %d", ret);
119 return ret;
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
David Howellsd84f4f92008-11-14 10:39:23 +1100123 * install a fresh thread keyring directly to new credentials
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
David Howellsd84f4f92008-11-14 10:39:23 +1100125int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
David Howellsd84f4f92008-11-14 10:39:23 +1100127 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Howellsd84f4f92008-11-14 10:39:23 +1100129 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
130 KEY_ALLOC_QUOTA_OVERRUN, NULL);
131 if (IS_ERR(keyring))
132 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David Howellsd84f4f92008-11-14 10:39:23 +1100134 new->thread_keyring = keyring;
135 return 0;
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * install a fresh thread keyring, discarding the old one
140 */
David Howellsd84f4f92008-11-14 10:39:23 +1100141static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David Howellsd84f4f92008-11-14 10:39:23 +1100143 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret;
145
David Howellsd84f4f92008-11-14 10:39:23 +1100146 new = prepare_creds();
147 if (!new)
148 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David Howellsd84f4f92008-11-14 10:39:23 +1100150 BUG_ON(new->thread_keyring);
151
152 ret = install_thread_keyring_to_cred(new);
153 if (ret < 0) {
154 abort_creds(new);
155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
David Howellsd84f4f92008-11-14 10:39:23 +1100158 return commit_creds(new);
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161/*
162 * install a process keyring directly to a credentials struct
163 * - returns -EEXIST if there was already a process keyring, 0 if one installed,
164 * and other -ve on any other error
165 */
166int install_process_keyring_to_cred(struct cred *new)
167{
168 struct key *keyring;
169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Howellsd84f4f92008-11-14 10:39:23 +1100171 if (new->tgcred->process_keyring)
172 return -EEXIST;
173
174 keyring = keyring_alloc("_pid", new->uid, new->gid,
175 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
176 if (IS_ERR(keyring))
177 return PTR_ERR(keyring);
178
179 spin_lock_irq(&new->tgcred->lock);
180 if (!new->tgcred->process_keyring) {
181 new->tgcred->process_keyring = keyring;
182 keyring = NULL;
183 ret = 0;
184 } else {
185 ret = -EEXIST;
186 }
187 spin_unlock_irq(&new->tgcred->lock);
188 key_put(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * make sure a process keyring is installed
David Howellsd84f4f92008-11-14 10:39:23 +1100194 * - we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
David Howellsd84f4f92008-11-14 10:39:23 +1100196static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howellsd84f4f92008-11-14 10:39:23 +1100198 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int ret;
200
David Howellsd84f4f92008-11-14 10:39:23 +1100201 new = prepare_creds();
202 if (!new)
203 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700204
David Howellsd84f4f92008-11-14 10:39:23 +1100205 ret = install_process_keyring_to_cred(new);
206 if (ret < 0) {
207 abort_creds(new);
208 return ret != -EEXIST ?: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211 return commit_creds(new);
212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
David Howellsd84f4f92008-11-14 10:39:23 +1100215 * install a session keyring directly to a credentials struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
David Howellsd84f4f92008-11-14 10:39:23 +1100217static int install_session_keyring_to_cred(struct cred *cred,
218 struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
David Howells7e047ef2006-06-26 00:24:50 -0700220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700222
223 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* create an empty session keyring */
226 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700227 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howellsd84f4f92008-11-14 10:39:23 +1100228 if (cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700229 flags = KEY_ALLOC_IN_QUOTA;
230
David Howellsd84f4f92008-11-14 10:39:23 +1100231 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
232 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700233 if (IS_ERR(keyring))
234 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100235 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 atomic_inc(&keyring->usage);
237 }
238
239 /* install the keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100240 spin_lock_irq(&cred->tgcred->lock);
241 old = cred->tgcred->session_keyring;
242 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
243 spin_unlock_irq(&cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
David Howells1a26feb2006-04-10 22:54:26 -0700245 /* we're using RCU on the pointer, but there's no point synchronising
246 * on it if it didn't previously point to anything */
247 if (old) {
248 synchronize_rcu();
249 key_put(old);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Howells1a26feb2006-04-10 22:54:26 -0700252 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
David Howellsd84f4f92008-11-14 10:39:23 +1100256 * install a session keyring, discarding the old one
257 * - if a keyring is not supplied, an empty one is invented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
David Howellsd84f4f92008-11-14 10:39:23 +1100259static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
David Howellsd84f4f92008-11-14 10:39:23 +1100261 struct cred *new;
262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Howellsd84f4f92008-11-14 10:39:23 +1100264 new = prepare_creds();
265 if (!new)
266 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800267
David Howellsd84f4f92008-11-14 10:39:23 +1100268 ret = install_session_keyring_to_cred(new, NULL);
269 if (ret < 0) {
270 abort_creds(new);
271 return ret;
272 }
David Howellsb5f545c2006-01-08 01:02:47 -0800273
David Howellsd84f4f92008-11-14 10:39:23 +1100274 return commit_creds(new);
275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/*****************************************************************************/
278/*
279 * deal with execve()
280 */
281int exec_keys(struct task_struct *tsk)
282{
David Howellsd84f4f92008-11-14 10:39:23 +1100283 struct thread_group_cred *tgcred = NULL;
284 struct cred *new;
285
286#ifdef CONFIG_KEYS
287 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
288 if (!tgcred)
289 return -ENOMEM;
290#endif
291
292 new = prepare_creds();
293 if (new < 0)
294 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* newly exec'd tasks don't get a thread keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100297 key_put(new->thread_keyring);
298 new->thread_keyring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
David Howellsd84f4f92008-11-14 10:39:23 +1100300 /* create a new per-thread-group creds for all this set of threads to
301 * share */
302 memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
David Howellsd84f4f92008-11-14 10:39:23 +1100304 atomic_set(&tgcred->usage, 1);
305 spin_lock_init(&tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
David Howellsd84f4f92008-11-14 10:39:23 +1100307 /* inherit the session keyring; new process keyring */
308 key_get(tgcred->session_keyring);
309 tgcred->process_keyring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
David Howellsd84f4f92008-11-14 10:39:23 +1100311 release_tgcred(new);
312 new->tgcred = tgcred;
313
314 commit_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316
317} /* end exec_keys() */
318
319/*****************************************************************************/
320/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * the filesystem user ID changed
322 */
323void key_fsuid_changed(struct task_struct *tsk)
324{
325 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100326 BUG_ON(!tsk->cred);
327 if (tsk->cred->thread_keyring) {
328 down_write(&tsk->cred->thread_keyring->sem);
329 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
330 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333} /* end key_fsuid_changed() */
334
335/*****************************************************************************/
336/*
337 * the filesystem group ID changed
338 */
339void key_fsgid_changed(struct task_struct *tsk)
340{
341 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100342 BUG_ON(!tsk->cred);
343 if (tsk->cred->thread_keyring) {
344 down_write(&tsk->cred->thread_keyring->sem);
345 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
346 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
349} /* end key_fsgid_changed() */
350
351/*****************************************************************************/
352/*
353 * search the process keyrings for the first matching key
354 * - we use the supplied match function to see if the description (or other
355 * feature of interest) matches
356 * - we return -EAGAIN if we didn't find any matching key
357 * - we return -ENOKEY if we found only negative matching keys
358 */
David Howells664cceb2005-09-28 17:03:15 +0100359key_ref_t search_process_keyrings(struct key_type *type,
360 const void *description,
361 key_match_func_t match,
David Howellsd84f4f92008-11-14 10:39:23 +1100362 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
David Howells3e301482005-06-23 22:00:56 -0700364 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800365 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
David Howells04c567d2006-06-22 14:47:18 -0700367 might_sleep();
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
370 * searchable, but we failed to find a key or we found a negative key;
371 * otherwise we want to return a sample error (probably -EACCES) if
372 * none of the keyrings were searchable
373 *
374 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
375 */
David Howells664cceb2005-09-28 17:03:15 +0100376 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 ret = NULL;
378 err = ERR_PTR(-EAGAIN);
379
380 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100381 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100382 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100383 make_key_ref(cred->thread_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100384 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100385 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto found;
387
David Howells664cceb2005-09-28 17:03:15 +0100388 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 case -EAGAIN: /* no key */
390 if (ret)
391 break;
392 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100393 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
395 default:
David Howells664cceb2005-09-28 17:03:15 +0100396 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 }
399 }
400
401 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100402 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100403 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100404 make_key_ref(cred->tgcred->process_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100405 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100406 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto found;
408
David Howells664cceb2005-09-28 17:03:15 +0100409 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 case -EAGAIN: /* no key */
411 if (ret)
412 break;
413 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100414 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 break;
416 default:
David Howells664cceb2005-09-28 17:03:15 +0100417 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
420 }
421
David Howells3e301482005-06-23 22:00:56 -0700422 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100423 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700424 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100425 key_ref = keyring_search_aux(
426 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100427 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100428 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100429 cred, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
David Howells664cceb2005-09-28 17:03:15 +0100432 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700433 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
David Howells664cceb2005-09-28 17:03:15 +0100435 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700436 case -EAGAIN: /* no key */
437 if (ret)
438 break;
439 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100440 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
David Howells3e301482005-06-23 22:00:56 -0700442 default:
David Howells664cceb2005-09-28 17:03:15 +0100443 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700444 break;
445 }
David Howells3e301482005-06-23 22:00:56 -0700446 }
447 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100448 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100449 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100450 make_key_ref(cred->user->session_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100451 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100452 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700453 goto found;
454
David Howells664cceb2005-09-28 17:03:15 +0100455 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700456 case -EAGAIN: /* no key */
457 if (ret)
458 break;
459 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100460 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700461 break;
462 default:
David Howells664cceb2005-09-28 17:03:15 +0100463 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700464 break;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
David Howellsb5f545c2006-01-08 01:02:47 -0800468 /* if this process has an instantiation authorisation key, then we also
469 * search the keyrings of the process mentioned there
470 * - we don't permit access to request_key auth keys via this method
471 */
David Howellsc69e8d92008-11-14 10:39:19 +1100472 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100473 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700474 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800475 ) {
David Howells04c567d2006-06-22 14:47:18 -0700476 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100477 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700478
David Howellsc69e8d92008-11-14 10:39:19 +1100479 if (key_validate(cred->request_key_auth) == 0) {
480 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800481
David Howells04c567d2006-06-22 14:47:18 -0700482 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100483 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800484
David Howellsc69e8d92008-11-14 10:39:19 +1100485 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700486
487 if (!IS_ERR(key_ref))
488 goto found;
489
490 switch (PTR_ERR(key_ref)) {
491 case -EAGAIN: /* no key */
492 if (ret)
493 break;
494 case -ENOKEY: /* negative key */
495 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800496 break;
David Howells04c567d2006-06-22 14:47:18 -0700497 default:
498 err = key_ref;
499 break;
500 }
501 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100502 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800503 }
504 }
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100507 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
David Howells3e301482005-06-23 22:00:56 -0700509found:
David Howells664cceb2005-09-28 17:03:15 +0100510 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512} /* end search_process_keyrings() */
513
514/*****************************************************************************/
515/*
David Howells664cceb2005-09-28 17:03:15 +0100516 * see if the key we're looking at is the target key
517 */
518static int lookup_user_key_possessed(const struct key *key, const void *target)
519{
520 return key == target;
521
522} /* end lookup_user_key_possessed() */
523
524/*****************************************************************************/
525/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 * lookup a key given a key ID from userspace with a given permissions mask
527 * - don't create special keyrings unless so requested
528 * - partially constructed keys aren't found unless requested
529 */
David Howells8bbf49762008-11-14 10:39:14 +1100530key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
531 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
David Howells8bbf49762008-11-14 10:39:14 +1100533 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100534 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100536 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int ret;
538
David Howellsbb952bb2008-11-14 10:39:20 +1100539try_again:
540 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100541 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 switch (id) {
544 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100545 if (!cred->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (!create)
547 goto error;
548
David Howells8bbf49762008-11-14 10:39:14 +1100549 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ret < 0) {
551 key = ERR_PTR(ret);
552 goto error;
553 }
David Howellsbb952bb2008-11-14 10:39:20 +1100554 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556
David Howellsb6dff3e2008-11-14 10:39:16 +1100557 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100559 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561
562 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100563 if (!cred->tgcred->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (!create)
565 goto error;
566
David Howells8bbf49762008-11-14 10:39:14 +1100567 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (ret < 0) {
569 key = ERR_PTR(ret);
570 goto error;
571 }
David Howellsbb952bb2008-11-14 10:39:20 +1100572 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
David Howellsbb952bb2008-11-14 10:39:20 +1100575 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100577 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579
580 case KEY_SPEC_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100581 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 /* always install a session keyring upon access if one
583 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100584 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700585 if (ret < 0)
586 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100587 ret = install_session_keyring(
588 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (ret < 0)
591 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100592 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
David Howells3e301482005-06-23 22:00:56 -0700595 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100596 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700598 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100599 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 break;
601
602 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100603 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100604 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700605 if (ret < 0)
606 goto error;
607 }
608
David Howellsb6dff3e2008-11-14 10:39:16 +1100609 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100611 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 break;
613
614 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100615 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100616 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700617 if (ret < 0)
618 goto error;
619 }
620
David Howellsb6dff3e2008-11-14 10:39:16 +1100621 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100623 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
625
626 case KEY_SPEC_GROUP_KEYRING:
627 /* group keyrings are not yet supported */
628 key = ERR_PTR(-EINVAL);
629 goto error;
630
David Howellsb5f545c2006-01-08 01:02:47 -0800631 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100632 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800633 if (!key)
634 goto error;
635
636 atomic_inc(&key->usage);
637 key_ref = make_key_ref(key, 1);
638 break;
639
David Howells8bbf49762008-11-14 10:39:14 +1100640 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100641 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100642 goto error;
643
David Howellsb6dff3e2008-11-14 10:39:16 +1100644 down_read(&cred->request_key_auth->sem);
645 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100646 key_ref = ERR_PTR(-EKEYREVOKED);
647 key = NULL;
648 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100649 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100650 key = rka->dest_keyring;
651 atomic_inc(&key->usage);
652 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100653 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100654 if (!key)
655 goto error;
656 key_ref = make_key_ref(key, 1);
657 break;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 default:
David Howells664cceb2005-09-28 17:03:15 +0100660 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (id < 1)
662 goto error;
663
664 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100665 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800666 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100668 }
669
670 key_ref = make_key_ref(key, 0);
671
672 /* check to see if we possess the key */
673 skey_ref = search_process_keyrings(key->type, key,
674 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100675 cred);
David Howells664cceb2005-09-28 17:03:15 +0100676
677 if (!IS_ERR(skey_ref)) {
678 key_put(key);
679 key_ref = skey_ref;
680 }
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 break;
683 }
684
David Howells76181c12007-10-16 23:29:46 -0700685 if (!partial) {
686 ret = wait_for_key_construction(key, true);
687 switch (ret) {
688 case -ERESTARTSYS:
689 goto invalid_key;
690 default:
691 if (perm)
692 goto invalid_key;
693 case 0:
694 break;
695 }
696 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 ret = key_validate(key);
698 if (ret < 0)
699 goto invalid_key;
700 }
701
702 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700703 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto invalid_key;
705
David Howells3e301482005-06-23 22:00:56 -0700706 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100707 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800708 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto invalid_key;
710
David Howells664cceb2005-09-28 17:03:15 +0100711error:
David Howellsbb952bb2008-11-14 10:39:20 +1100712 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100713 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
David Howells664cceb2005-09-28 17:03:15 +0100715invalid_key:
716 key_ref_put(key_ref);
717 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto error;
719
David Howellsbb952bb2008-11-14 10:39:20 +1100720 /* if we attempted to install a keyring, then it may have caused new
721 * creds to be installed */
722reget_creds:
723 put_cred(cred);
724 goto try_again;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726} /* end lookup_user_key() */
727
728/*****************************************************************************/
729/*
730 * join the named keyring as the session keyring if possible, or attempt to
731 * create a new one of that name if not
732 * - if the name is NULL, an empty anonymous keyring is installed instead
733 * - named session keyring joining is done with a semaphore held
734 */
735long join_session_keyring(const char *name)
736{
David Howellsd84f4f92008-11-14 10:39:23 +1100737 const struct cred *old;
738 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100740 long ret, serial;
741
742 /* only permit this if there's a single thread in the thread group -
743 * this avoids us having to adjust the creds on all threads and risking
744 * ENOMEM */
745 if (!is_single_threaded(current))
746 return -EMLINK;
747
748 new = prepare_creds();
749 if (!new)
750 return -ENOMEM;
751 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 /* if no name is provided, install an anonymous keyring */
754 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100755 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (ret < 0)
757 goto error;
758
David Howellsd84f4f92008-11-14 10:39:23 +1100759 serial = new->tgcred->session_keyring->serial;
760 ret = commit_creds(new);
761 if (ret == 0)
762 ret = serial;
763 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800767 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700770 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (PTR_ERR(keyring) == -ENOKEY) {
772 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100773 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700774 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (IS_ERR(keyring)) {
776 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700777 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
David Howellsd84f4f92008-11-14 10:39:23 +1100779 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 ret = PTR_ERR(keyring);
781 goto error2;
782 }
783
784 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100785 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (ret < 0)
787 goto error2;
788
David Howellsd84f4f92008-11-14 10:39:23 +1100789 commit_creds(new);
790 mutex_unlock(&key_session_mutex);
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 ret = keyring->serial;
793 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100794okay:
795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
David Howells664cceb2005-09-28 17:03:15 +0100797error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800798 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100799error:
David Howellsd84f4f92008-11-14 10:39:23 +1100800 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100802}