blob: b0904cdda2e7b03f5e2ec0d0fdb2f2c474b85dcb [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 Howellsb6dff3e2008-11-14 10:39:16 +110045 struct user_struct *user = current->cred->user;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct key *uid_keyring, *session_keyring;
47 char buf[20];
48 int ret;
49
David Howells69664cf2008-04-29 01:01:31 -070050 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Howells69664cf2008-04-29 01:01:31 -070052 if (user->uid_keyring) {
53 kleave(" = 0 [exist]");
54 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 }
56
David Howells69664cf2008-04-29 01:01:31 -070057 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 ret = 0;
59
David Howells69664cf2008-04-29 01:01:31 -070060 if (!user->uid_keyring) {
61 /* get the UID-specific keyring
62 * - there may be one in existence already as it may have been
63 * pinned by a session, but the user_struct pointing to it
64 * may have been destroyed by setuid */
65 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David Howells69664cf2008-04-29 01:01:31 -070067 uid_keyring = find_keyring_by_name(buf, true);
68 if (IS_ERR(uid_keyring)) {
69 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howells8bbf49762008-11-14 10:39:14 +110070 current, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070071 NULL);
72 if (IS_ERR(uid_keyring)) {
73 ret = PTR_ERR(uid_keyring);
74 goto error;
75 }
76 }
77
78 /* get a default session keyring (which might also exist
79 * already) */
80 sprintf(buf, "_uid_ses.%u", user->uid);
81
82 session_keyring = find_keyring_by_name(buf, true);
83 if (IS_ERR(session_keyring)) {
84 session_keyring =
85 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howells8bbf49762008-11-14 10:39:14 +110086 current, KEY_ALLOC_IN_QUOTA,
87 NULL);
David Howells69664cf2008-04-29 01:01:31 -070088 if (IS_ERR(session_keyring)) {
89 ret = PTR_ERR(session_keyring);
90 goto error_release;
91 }
92
93 /* we install a link from the user session keyring to
94 * the user keyring */
95 ret = key_link(session_keyring, uid_keyring);
96 if (ret < 0)
97 goto error_release_both;
98 }
99
100 /* install the keyrings */
101 user->uid_keyring = uid_keyring;
102 user->session_keyring = session_keyring;
103 }
104
105 mutex_unlock(&key_user_keyring_mutex);
106 kleave(" = 0");
107 return 0;
108
109error_release_both:
110 key_put(session_keyring);
111error_release:
112 key_put(uid_keyring);
113error:
114 mutex_unlock(&key_user_keyring_mutex);
115 kleave(" = %d", ret);
116 return ret;
117}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/*****************************************************************************/
120/*
121 * deal with the UID changing
122 */
123void switch_uid_keyring(struct user_struct *new_user)
124{
125#if 0 /* do nothing for now */
126 struct key *old;
127
128 /* switch to the new user's session keyring if we were running under
129 * root's default session keyring */
130 if (new_user->uid != 0 &&
131 current->session_keyring == &root_session_keyring
132 ) {
133 atomic_inc(&new_user->session_keyring->usage);
134
135 task_lock(current);
136 old = current->session_keyring;
137 current->session_keyring = new_user->session_keyring;
138 task_unlock(current);
139
140 key_put(old);
141 }
142#endif
143
144} /* end switch_uid_keyring() */
145
146/*****************************************************************************/
147/*
148 * install a fresh thread keyring, discarding the old one
149 */
David Howells8bbf49762008-11-14 10:39:14 +1100150int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
David Howells8bbf49762008-11-14 10:39:14 +1100152 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct key *keyring, *old;
154 char buf[20];
155 int ret;
156
157 sprintf(buf, "_tid.%u", tsk->pid);
158
David Howellsb6dff3e2008-11-14 10:39:16 +1100159 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
David Howells7e047ef2006-06-26 00:24:50 -0700160 KEY_ALLOC_QUOTA_OVERRUN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (IS_ERR(keyring)) {
162 ret = PTR_ERR(keyring);
163 goto error;
164 }
165
166 task_lock(tsk);
David Howellsb6dff3e2008-11-14 10:39:16 +1100167 old = tsk->cred->thread_keyring;
168 tsk->cred->thread_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 task_unlock(tsk);
170
171 ret = 0;
172
173 key_put(old);
David Howells664cceb2005-09-28 17:03:15 +0100174error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return ret;
176
177} /* end install_thread_keyring() */
178
179/*****************************************************************************/
180/*
181 * make sure a process keyring is installed
182 */
David Howells8bbf49762008-11-14 10:39:14 +1100183int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
David Howells8bbf49762008-11-14 10:39:14 +1100185 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct key *keyring;
187 char buf[20];
188 int ret;
189
David Howells1a26feb2006-04-10 22:54:26 -0700190 might_sleep();
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!tsk->signal->process_keyring) {
193 sprintf(buf, "_pid.%u", tsk->tgid);
194
David Howellsb6dff3e2008-11-14 10:39:16 +1100195 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
David Howells7e047ef2006-06-26 00:24:50 -0700196 KEY_ALLOC_QUOTA_OVERRUN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (IS_ERR(keyring)) {
198 ret = PTR_ERR(keyring);
199 goto error;
200 }
201
David Howells8589b4e2005-06-23 22:00:53 -0700202 /* attach keyring */
David Howells1a26feb2006-04-10 22:54:26 -0700203 spin_lock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (!tsk->signal->process_keyring) {
205 tsk->signal->process_keyring = keyring;
206 keyring = NULL;
207 }
David Howells1a26feb2006-04-10 22:54:26 -0700208 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 key_put(keyring);
211 }
212
213 ret = 0;
David Howells664cceb2005-09-28 17:03:15 +0100214error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return ret;
216
217} /* end install_process_keyring() */
218
219/*****************************************************************************/
220/*
221 * install a session keyring, discarding the old one
222 * - if a keyring is not supplied, an empty one is invented
223 */
David Howells8bbf49762008-11-14 10:39:14 +1100224static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
David Howells8bbf49762008-11-14 10:39:14 +1100226 struct task_struct *tsk = current;
David Howells7e047ef2006-06-26 00:24:50 -0700227 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct key *old;
229 char buf[20];
David Howells1a26feb2006-04-10 22:54:26 -0700230
231 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* create an empty session keyring */
234 if (!keyring) {
235 sprintf(buf, "_ses.%u", tsk->tgid);
236
David Howells7e047ef2006-06-26 00:24:50 -0700237 flags = KEY_ALLOC_QUOTA_OVERRUN;
238 if (tsk->signal->session_keyring)
239 flags = KEY_ALLOC_IN_QUOTA;
240
David Howellsb6dff3e2008-11-14 10:39:16 +1100241 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid, tsk,
David Howells7e047ef2006-06-26 00:24:50 -0700242 flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700243 if (IS_ERR(keyring))
244 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 else {
247 atomic_inc(&keyring->usage);
248 }
249
250 /* install the keyring */
David Howells1a26feb2006-04-10 22:54:26 -0700251 spin_lock_irq(&tsk->sighand->siglock);
252 old = tsk->signal->session_keyring;
David Howells8589b4e2005-06-23 22:00:53 -0700253 rcu_assign_pointer(tsk->signal->session_keyring, keyring);
David Howells1a26feb2006-04-10 22:54:26 -0700254 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
David Howells1a26feb2006-04-10 22:54:26 -0700256 /* we're using RCU on the pointer, but there's no point synchronising
257 * on it if it didn't previously point to anything */
258 if (old) {
259 synchronize_rcu();
260 key_put(old);
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
David Howells1a26feb2006-04-10 22:54:26 -0700263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265} /* end install_session_keyring() */
266
267/*****************************************************************************/
268/*
269 * copy the keys in a thread group for fork without CLONE_THREAD
270 */
271int copy_thread_group_keys(struct task_struct *tsk)
272{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 key_check(current->thread_group->session_keyring);
274 key_check(current->thread_group->process_keyring);
275
276 /* no process keyring yet */
277 tsk->signal->process_keyring = NULL;
278
279 /* same session keyring */
David Howells8589b4e2005-06-23 22:00:53 -0700280 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 tsk->signal->session_keyring =
David Howells8589b4e2005-06-23 22:00:53 -0700282 key_get(rcu_dereference(current->signal->session_keyring));
283 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 return 0;
286
287} /* end copy_thread_group_keys() */
288
289/*****************************************************************************/
290/*
291 * copy the keys for fork
292 */
293int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
294{
David Howellsb6dff3e2008-11-14 10:39:16 +1100295 key_check(tsk->cred->thread_keyring);
296 key_check(tsk->cred->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /* no thread keyring yet */
David Howellsb6dff3e2008-11-14 10:39:16 +1100299 tsk->cred->thread_keyring = NULL;
David Howellsb5f545c2006-01-08 01:02:47 -0800300
301 /* copy the request_key() authorisation for this thread */
David Howellsb6dff3e2008-11-14 10:39:16 +1100302 key_get(tsk->cred->request_key_auth);
David Howellsb5f545c2006-01-08 01:02:47 -0800303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305
306} /* end copy_keys() */
307
308/*****************************************************************************/
309/*
310 * dispose of thread group keys upon thread group destruction
311 */
312void exit_thread_group_keys(struct signal_struct *tg)
313{
314 key_put(tg->session_keyring);
315 key_put(tg->process_keyring);
316
317} /* end exit_thread_group_keys() */
318
319/*****************************************************************************/
320/*
David Howellsb5f545c2006-01-08 01:02:47 -0800321 * dispose of per-thread keys upon thread exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
323void exit_keys(struct task_struct *tsk)
324{
David Howellsb6dff3e2008-11-14 10:39:16 +1100325 key_put(tsk->cred->thread_keyring);
326 key_put(tsk->cred->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328} /* end exit_keys() */
329
330/*****************************************************************************/
331/*
332 * deal with execve()
333 */
334int exec_keys(struct task_struct *tsk)
335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct key *old;
337
338 /* newly exec'd tasks don't get a thread keyring */
339 task_lock(tsk);
David Howellsb6dff3e2008-11-14 10:39:16 +1100340 old = tsk->cred->thread_keyring;
341 tsk->cred->thread_keyring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 task_unlock(tsk);
343
344 key_put(old);
345
346 /* discard the process keyring from a newly exec'd task */
David Howells1a26feb2006-04-10 22:54:26 -0700347 spin_lock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 old = tsk->signal->process_keyring;
349 tsk->signal->process_keyring = NULL;
David Howells1a26feb2006-04-10 22:54:26 -0700350 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 key_put(old);
353
354 return 0;
355
356} /* end exec_keys() */
357
358/*****************************************************************************/
359/*
360 * deal with SUID programs
361 * - we might want to make this invent a new session keyring
362 */
363int suid_keys(struct task_struct *tsk)
364{
365 return 0;
366
367} /* end suid_keys() */
368
369/*****************************************************************************/
370/*
371 * the filesystem user ID changed
372 */
373void key_fsuid_changed(struct task_struct *tsk)
374{
375 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100376 BUG_ON(!tsk->cred);
377 if (tsk->cred->thread_keyring) {
378 down_write(&tsk->cred->thread_keyring->sem);
379 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
380 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 }
382
383} /* end key_fsuid_changed() */
384
385/*****************************************************************************/
386/*
387 * the filesystem group ID changed
388 */
389void key_fsgid_changed(struct task_struct *tsk)
390{
391 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100392 BUG_ON(!tsk->cred);
393 if (tsk->cred->thread_keyring) {
394 down_write(&tsk->cred->thread_keyring->sem);
395 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
396 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399} /* end key_fsgid_changed() */
400
401/*****************************************************************************/
402/*
403 * search the process keyrings for the first matching key
404 * - we use the supplied match function to see if the description (or other
405 * feature of interest) matches
406 * - we return -EAGAIN if we didn't find any matching key
407 * - we return -ENOKEY if we found only negative matching keys
408 */
David Howells664cceb2005-09-28 17:03:15 +0100409key_ref_t search_process_keyrings(struct key_type *type,
410 const void *description,
411 key_match_func_t match,
412 struct task_struct *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
David Howells3e301482005-06-23 22:00:56 -0700414 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800415 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells04c567d2006-06-22 14:47:18 -0700417 might_sleep();
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
420 * searchable, but we failed to find a key or we found a negative key;
421 * otherwise we want to return a sample error (probably -EACCES) if
422 * none of the keyrings were searchable
423 *
424 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
425 */
David Howells664cceb2005-09-28 17:03:15 +0100426 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 ret = NULL;
428 err = ERR_PTR(-EAGAIN);
429
430 /* search the thread keyring first */
David Howellsb6dff3e2008-11-14 10:39:16 +1100431 if (context->cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100432 key_ref = keyring_search_aux(
David Howellsb6dff3e2008-11-14 10:39:16 +1100433 make_key_ref(context->cred->thread_keyring, 1),
David Howells664cceb2005-09-28 17:03:15 +0100434 context, type, description, match);
435 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto found;
437
David Howells664cceb2005-09-28 17:03:15 +0100438 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 case -EAGAIN: /* no key */
440 if (ret)
441 break;
442 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100443 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 break;
445 default:
David Howells664cceb2005-09-28 17:03:15 +0100446 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
448 }
449 }
450
451 /* search the process keyring second */
David Howells3e301482005-06-23 22:00:56 -0700452 if (context->signal->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100453 key_ref = keyring_search_aux(
454 make_key_ref(context->signal->process_keyring, 1),
455 context, type, description, match);
456 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto found;
458
David Howells664cceb2005-09-28 17:03:15 +0100459 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 case -EAGAIN: /* no key */
461 if (ret)
462 break;
463 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100464 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 break;
466 default:
David Howells664cceb2005-09-28 17:03:15 +0100467 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 break;
469 }
470 }
471
David Howells3e301482005-06-23 22:00:56 -0700472 /* search the session keyring */
473 if (context->signal->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700474 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100475 key_ref = keyring_search_aux(
476 make_key_ref(rcu_dereference(
477 context->signal->session_keyring),
478 1),
David Howells3e301482005-06-23 22:00:56 -0700479 context, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700480 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
David Howells664cceb2005-09-28 17:03:15 +0100482 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700483 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David Howells664cceb2005-09-28 17:03:15 +0100485 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700486 case -EAGAIN: /* no key */
487 if (ret)
488 break;
489 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100490 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 break;
David Howells3e301482005-06-23 22:00:56 -0700492 default:
David Howells664cceb2005-09-28 17:03:15 +0100493 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700494 break;
495 }
David Howells3e301482005-06-23 22:00:56 -0700496 }
497 /* or search the user-session keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100498 else if (context->cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100499 key_ref = keyring_search_aux(
David Howellsb6dff3e2008-11-14 10:39:16 +1100500 make_key_ref(context->cred->user->session_keyring, 1),
David Howells664cceb2005-09-28 17:03:15 +0100501 context, type, description, match);
502 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700503 goto found;
504
David Howells664cceb2005-09-28 17:03:15 +0100505 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700506 case -EAGAIN: /* no key */
507 if (ret)
508 break;
509 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100510 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700511 break;
512 default:
David Howells664cceb2005-09-28 17:03:15 +0100513 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700514 break;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
David Howellsb5f545c2006-01-08 01:02:47 -0800518 /* if this process has an instantiation authorisation key, then we also
519 * search the keyrings of the process mentioned there
520 * - we don't permit access to request_key auth keys via this method
521 */
David Howellsb6dff3e2008-11-14 10:39:16 +1100522 if (context->cred->request_key_auth &&
David Howellsb5f545c2006-01-08 01:02:47 -0800523 context == current &&
David Howells04c567d2006-06-22 14:47:18 -0700524 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800525 ) {
David Howells04c567d2006-06-22 14:47:18 -0700526 /* defend against the auth key being revoked */
David Howellsb6dff3e2008-11-14 10:39:16 +1100527 down_read(&context->cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700528
David Howellsb6dff3e2008-11-14 10:39:16 +1100529 if (key_validate(context->cred->request_key_auth) == 0) {
530 rka = context->cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800531
David Howells04c567d2006-06-22 14:47:18 -0700532 key_ref = search_process_keyrings(type, description,
533 match, rka->context);
David Howellsb5f545c2006-01-08 01:02:47 -0800534
David Howellsb6dff3e2008-11-14 10:39:16 +1100535 up_read(&context->cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700536
537 if (!IS_ERR(key_ref))
538 goto found;
539
540 switch (PTR_ERR(key_ref)) {
541 case -EAGAIN: /* no key */
542 if (ret)
543 break;
544 case -ENOKEY: /* negative key */
545 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800546 break;
David Howells04c567d2006-06-22 14:47:18 -0700547 default:
548 err = key_ref;
549 break;
550 }
551 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100552 up_read(&context->cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800553 }
554 }
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100557 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
David Howells3e301482005-06-23 22:00:56 -0700559found:
David Howells664cceb2005-09-28 17:03:15 +0100560 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562} /* end search_process_keyrings() */
563
564/*****************************************************************************/
565/*
David Howells664cceb2005-09-28 17:03:15 +0100566 * see if the key we're looking at is the target key
567 */
568static int lookup_user_key_possessed(const struct key *key, const void *target)
569{
570 return key == target;
571
572} /* end lookup_user_key_possessed() */
573
574/*****************************************************************************/
575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 * lookup a key given a key ID from userspace with a given permissions mask
577 * - don't create special keyrings unless so requested
578 * - partially constructed keys aren't found unless requested
579 */
David Howells8bbf49762008-11-14 10:39:14 +1100580key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
581 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
David Howells8bbf49762008-11-14 10:39:14 +1100583 struct request_key_auth *rka;
584 struct task_struct *t = current;
David Howellsb6dff3e2008-11-14 10:39:16 +1100585 struct cred *cred = t->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100587 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int ret;
589
David Howells664cceb2005-09-28 17:03:15 +0100590 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 switch (id) {
593 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100594 if (!cred->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!create)
596 goto error;
597
David Howells8bbf49762008-11-14 10:39:14 +1100598 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 if (ret < 0) {
600 key = ERR_PTR(ret);
601 goto error;
602 }
603 }
604
David Howellsb6dff3e2008-11-14 10:39:16 +1100605 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100607 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609
610 case KEY_SPEC_PROCESS_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +1100611 if (!t->signal->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (!create)
613 goto error;
614
David Howells8bbf49762008-11-14 10:39:14 +1100615 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 if (ret < 0) {
617 key = ERR_PTR(ret);
618 goto error;
619 }
620 }
621
David Howells8bbf49762008-11-14 10:39:14 +1100622 key = t->signal->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100624 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
626
627 case KEY_SPEC_SESSION_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +1100628 if (!t->signal->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* always install a session keyring upon access if one
630 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100631 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700632 if (ret < 0)
633 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100634 ret = install_session_keyring(
635 cred->user->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (ret < 0)
637 goto error;
638 }
639
David Howells3e301482005-06-23 22:00:56 -0700640 rcu_read_lock();
David Howells8bbf49762008-11-14 10:39:14 +1100641 key = rcu_dereference(t->signal->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700643 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100644 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646
647 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100648 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100649 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700650 if (ret < 0)
651 goto error;
652 }
653
David Howellsb6dff3e2008-11-14 10:39:16 +1100654 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100656 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658
659 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100660 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100661 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700662 if (ret < 0)
663 goto error;
664 }
665
David Howellsb6dff3e2008-11-14 10:39:16 +1100666 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100668 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
670
671 case KEY_SPEC_GROUP_KEYRING:
672 /* group keyrings are not yet supported */
673 key = ERR_PTR(-EINVAL);
674 goto error;
675
David Howellsb5f545c2006-01-08 01:02:47 -0800676 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100677 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800678 if (!key)
679 goto error;
680
681 atomic_inc(&key->usage);
682 key_ref = make_key_ref(key, 1);
683 break;
684
David Howells8bbf49762008-11-14 10:39:14 +1100685 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100686 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100687 goto error;
688
David Howellsb6dff3e2008-11-14 10:39:16 +1100689 down_read(&cred->request_key_auth->sem);
690 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100691 key_ref = ERR_PTR(-EKEYREVOKED);
692 key = NULL;
693 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100694 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100695 key = rka->dest_keyring;
696 atomic_inc(&key->usage);
697 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100698 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100699 if (!key)
700 goto error;
701 key_ref = make_key_ref(key, 1);
702 break;
703
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 default:
David Howells664cceb2005-09-28 17:03:15 +0100705 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (id < 1)
707 goto error;
708
709 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100710 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800711 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100713 }
714
715 key_ref = make_key_ref(key, 0);
716
717 /* check to see if we possess the key */
718 skey_ref = search_process_keyrings(key->type, key,
719 lookup_user_key_possessed,
720 current);
721
722 if (!IS_ERR(skey_ref)) {
723 key_put(key);
724 key_ref = skey_ref;
725 }
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
728 }
729
David Howells76181c12007-10-16 23:29:46 -0700730 if (!partial) {
731 ret = wait_for_key_construction(key, true);
732 switch (ret) {
733 case -ERESTARTSYS:
734 goto invalid_key;
735 default:
736 if (perm)
737 goto invalid_key;
738 case 0:
739 break;
740 }
741 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 ret = key_validate(key);
743 if (ret < 0)
744 goto invalid_key;
745 }
746
747 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700748 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto invalid_key;
750
David Howells3e301482005-06-23 22:00:56 -0700751 /* check the permissions */
David Howells8bbf49762008-11-14 10:39:14 +1100752 ret = key_task_permission(key_ref, t, perm);
David Howells29db9192005-10-30 15:02:44 -0800753 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto invalid_key;
755
David Howells664cceb2005-09-28 17:03:15 +0100756error:
757 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
David Howells664cceb2005-09-28 17:03:15 +0100759invalid_key:
760 key_ref_put(key_ref);
761 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto error;
763
764} /* end lookup_user_key() */
765
766/*****************************************************************************/
767/*
768 * join the named keyring as the session keyring if possible, or attempt to
769 * create a new one of that name if not
770 * - if the name is NULL, an empty anonymous keyring is installed instead
771 * - named session keyring joining is done with a semaphore held
772 */
773long join_session_keyring(const char *name)
774{
775 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 struct key *keyring;
777 long ret;
778
779 /* if no name is provided, install an anonymous keyring */
780 if (!name) {
David Howells8bbf49762008-11-14 10:39:14 +1100781 ret = install_session_keyring(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (ret < 0)
783 goto error;
784
David Howells3e301482005-06-23 22:00:56 -0700785 rcu_read_lock();
786 ret = rcu_dereference(tsk->signal->session_keyring)->serial;
787 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto error;
789 }
790
791 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800792 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700795 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (PTR_ERR(keyring) == -ENOKEY) {
797 /* not found - try and create a new one */
David Howellsb6dff3e2008-11-14 10:39:16 +1100798 keyring = keyring_alloc(name, tsk->cred->uid, tsk->cred->gid, tsk,
David Howells7e047ef2006-06-26 00:24:50 -0700799 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (IS_ERR(keyring)) {
801 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700802 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 }
805 else if (IS_ERR(keyring)) {
806 ret = PTR_ERR(keyring);
807 goto error2;
808 }
809
810 /* we've got a keyring - now to install it */
David Howells8bbf49762008-11-14 10:39:14 +1100811 ret = install_session_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (ret < 0)
813 goto error2;
814
815 ret = keyring->serial;
816 key_put(keyring);
817
David Howells664cceb2005-09-28 17:03:15 +0100818error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800819 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100820error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return ret;
822
823} /* end join_session_keyring() */