blob: b6f86808475a0aedfb28e9d677fda7db553f006b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* process_keys.c: management of a process's keyrings
2 *
David Howells8589b4e2005-06-23 22:00:53 -07003 * Copyright (C) 2004-5 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
26/* the root user's tracking struct */
27struct key_user root_key_user = {
28 .usage = ATOMIC_INIT(3),
29 .consq = LIST_HEAD_INIT(root_key_user.consq),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080030 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 .nkeys = ATOMIC_INIT(2),
32 .nikeys = ATOMIC_INIT(2),
33 .uid = 0,
34};
35
36/* the root user's UID keyring */
37struct key root_user_keyring = {
38 .usage = ATOMIC_INIT(1),
39 .serial = 2,
40 .type = &key_type_keyring,
41 .user = &root_key_user,
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 .sem = __RWSEM_INITIALIZER(root_user_keyring.sem),
David Howells29db9192005-10-30 15:02:44 -080043 .perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
David Howells76d8aea2005-06-23 22:00:49 -070044 .flags = 1 << KEY_FLAG_INSTANTIATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 .description = "_uid.0",
46#ifdef KEY_DEBUGGING
47 .magic = KEY_DEBUG_MAGIC,
48#endif
49};
50
51/* the root user's default session keyring */
52struct key root_session_keyring = {
53 .usage = ATOMIC_INIT(1),
54 .serial = 1,
55 .type = &key_type_keyring,
56 .user = &root_key_user,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 .sem = __RWSEM_INITIALIZER(root_session_keyring.sem),
David Howells29db9192005-10-30 15:02:44 -080058 .perm = (KEY_POS_ALL & ~KEY_POS_SETATTR) | KEY_USR_ALL,
David Howells76d8aea2005-06-23 22:00:49 -070059 .flags = 1 << KEY_FLAG_INSTANTIATED,
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 .description = "_uid_ses.0",
61#ifdef KEY_DEBUGGING
62 .magic = KEY_DEBUG_MAGIC,
63#endif
64};
65
66/*****************************************************************************/
67/*
68 * allocate the keyrings to be associated with a UID
69 */
Michael LeMayd7200242006-06-22 14:47:17 -070070int alloc_uid_keyring(struct user_struct *user,
71 struct task_struct *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 struct key *uid_keyring, *session_keyring;
74 char buf[20];
75 int ret;
76
77 /* concoct a default session keyring */
78 sprintf(buf, "_uid_ses.%u", user->uid);
79
David Howells7e047ef2006-06-26 00:24:50 -070080 session_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx,
81 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (IS_ERR(session_keyring)) {
83 ret = PTR_ERR(session_keyring);
84 goto error;
85 }
86
87 /* and a UID specific keyring, pointed to by the default session
88 * keyring */
89 sprintf(buf, "_uid.%u", user->uid);
90
David Howells7e047ef2006-06-26 00:24:50 -070091 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1, ctx,
92 KEY_ALLOC_IN_QUOTA, session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (IS_ERR(uid_keyring)) {
94 key_put(session_keyring);
95 ret = PTR_ERR(uid_keyring);
96 goto error;
97 }
98
99 /* install the keyrings */
100 user->uid_keyring = uid_keyring;
101 user->session_keyring = session_keyring;
102 ret = 0;
103
David Howells664cceb2005-09-28 17:03:15 +0100104error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return ret;
106
107} /* end alloc_uid_keyring() */
108
109/*****************************************************************************/
110/*
111 * deal with the UID changing
112 */
113void switch_uid_keyring(struct user_struct *new_user)
114{
115#if 0 /* do nothing for now */
116 struct key *old;
117
118 /* switch to the new user's session keyring if we were running under
119 * root's default session keyring */
120 if (new_user->uid != 0 &&
121 current->session_keyring == &root_session_keyring
122 ) {
123 atomic_inc(&new_user->session_keyring->usage);
124
125 task_lock(current);
126 old = current->session_keyring;
127 current->session_keyring = new_user->session_keyring;
128 task_unlock(current);
129
130 key_put(old);
131 }
132#endif
133
134} /* end switch_uid_keyring() */
135
136/*****************************************************************************/
137/*
138 * install a fresh thread keyring, discarding the old one
139 */
140int install_thread_keyring(struct task_struct *tsk)
141{
142 struct key *keyring, *old;
143 char buf[20];
144 int ret;
145
146 sprintf(buf, "_tid.%u", tsk->pid);
147
David Howells7e047ef2006-06-26 00:24:50 -0700148 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
149 KEY_ALLOC_QUOTA_OVERRUN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (IS_ERR(keyring)) {
151 ret = PTR_ERR(keyring);
152 goto error;
153 }
154
155 task_lock(tsk);
156 old = tsk->thread_keyring;
157 tsk->thread_keyring = keyring;
158 task_unlock(tsk);
159
160 ret = 0;
161
162 key_put(old);
David Howells664cceb2005-09-28 17:03:15 +0100163error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return ret;
165
166} /* end install_thread_keyring() */
167
168/*****************************************************************************/
169/*
170 * make sure a process keyring is installed
171 */
David Howells3e301482005-06-23 22:00:56 -0700172int install_process_keyring(struct task_struct *tsk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct key *keyring;
175 char buf[20];
176 int ret;
177
David Howells1a26feb2006-04-10 22:54:26 -0700178 might_sleep();
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (!tsk->signal->process_keyring) {
181 sprintf(buf, "_pid.%u", tsk->tgid);
182
David Howells7e047ef2006-06-26 00:24:50 -0700183 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
184 KEY_ALLOC_QUOTA_OVERRUN, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (IS_ERR(keyring)) {
186 ret = PTR_ERR(keyring);
187 goto error;
188 }
189
David Howells8589b4e2005-06-23 22:00:53 -0700190 /* attach keyring */
David Howells1a26feb2006-04-10 22:54:26 -0700191 spin_lock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!tsk->signal->process_keyring) {
193 tsk->signal->process_keyring = keyring;
194 keyring = NULL;
195 }
David Howells1a26feb2006-04-10 22:54:26 -0700196 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 key_put(keyring);
199 }
200
201 ret = 0;
David Howells664cceb2005-09-28 17:03:15 +0100202error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return ret;
204
205} /* end install_process_keyring() */
206
207/*****************************************************************************/
208/*
209 * install a session keyring, discarding the old one
210 * - if a keyring is not supplied, an empty one is invented
211 */
212static int install_session_keyring(struct task_struct *tsk,
213 struct key *keyring)
214{
David Howells7e047ef2006-06-26 00:24:50 -0700215 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct key *old;
217 char buf[20];
David Howells1a26feb2006-04-10 22:54:26 -0700218
219 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* create an empty session keyring */
222 if (!keyring) {
223 sprintf(buf, "_ses.%u", tsk->tgid);
224
David Howells7e047ef2006-06-26 00:24:50 -0700225 flags = KEY_ALLOC_QUOTA_OVERRUN;
226 if (tsk->signal->session_keyring)
227 flags = KEY_ALLOC_IN_QUOTA;
228
229 keyring = keyring_alloc(buf, tsk->uid, tsk->gid, tsk,
230 flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700231 if (IS_ERR(keyring))
232 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 else {
235 atomic_inc(&keyring->usage);
236 }
237
238 /* install the keyring */
David Howells1a26feb2006-04-10 22:54:26 -0700239 spin_lock_irq(&tsk->sighand->siglock);
240 old = tsk->signal->session_keyring;
David Howells8589b4e2005-06-23 22:00:53 -0700241 rcu_assign_pointer(tsk->signal->session_keyring, keyring);
David Howells1a26feb2006-04-10 22:54:26 -0700242 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
David Howells1a26feb2006-04-10 22:54:26 -0700244 /* we're using RCU on the pointer, but there's no point synchronising
245 * on it if it didn't previously point to anything */
246 if (old) {
247 synchronize_rcu();
248 key_put(old);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
David Howells1a26feb2006-04-10 22:54:26 -0700251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253} /* end install_session_keyring() */
254
255/*****************************************************************************/
256/*
257 * copy the keys in a thread group for fork without CLONE_THREAD
258 */
259int copy_thread_group_keys(struct task_struct *tsk)
260{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 key_check(current->thread_group->session_keyring);
262 key_check(current->thread_group->process_keyring);
263
264 /* no process keyring yet */
265 tsk->signal->process_keyring = NULL;
266
267 /* same session keyring */
David Howells8589b4e2005-06-23 22:00:53 -0700268 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 tsk->signal->session_keyring =
David Howells8589b4e2005-06-23 22:00:53 -0700270 key_get(rcu_dereference(current->signal->session_keyring));
271 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 return 0;
274
275} /* end copy_thread_group_keys() */
276
277/*****************************************************************************/
278/*
279 * copy the keys for fork
280 */
281int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
282{
283 key_check(tsk->thread_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800284 key_check(tsk->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 /* no thread keyring yet */
287 tsk->thread_keyring = NULL;
David Howellsb5f545c2006-01-08 01:02:47 -0800288
289 /* copy the request_key() authorisation for this thread */
290 key_get(tsk->request_key_auth);
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return 0;
293
294} /* end copy_keys() */
295
296/*****************************************************************************/
297/*
298 * dispose of thread group keys upon thread group destruction
299 */
300void exit_thread_group_keys(struct signal_struct *tg)
301{
302 key_put(tg->session_keyring);
303 key_put(tg->process_keyring);
304
305} /* end exit_thread_group_keys() */
306
307/*****************************************************************************/
308/*
David Howellsb5f545c2006-01-08 01:02:47 -0800309 * dispose of per-thread keys upon thread exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 */
311void exit_keys(struct task_struct *tsk)
312{
313 key_put(tsk->thread_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800314 key_put(tsk->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316} /* end exit_keys() */
317
318/*****************************************************************************/
319/*
320 * deal with execve()
321 */
322int exec_keys(struct task_struct *tsk)
323{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct key *old;
325
326 /* newly exec'd tasks don't get a thread keyring */
327 task_lock(tsk);
328 old = tsk->thread_keyring;
329 tsk->thread_keyring = NULL;
330 task_unlock(tsk);
331
332 key_put(old);
333
334 /* discard the process keyring from a newly exec'd task */
David Howells1a26feb2006-04-10 22:54:26 -0700335 spin_lock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 old = tsk->signal->process_keyring;
337 tsk->signal->process_keyring = NULL;
David Howells1a26feb2006-04-10 22:54:26 -0700338 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 key_put(old);
341
342 return 0;
343
344} /* end exec_keys() */
345
346/*****************************************************************************/
347/*
348 * deal with SUID programs
349 * - we might want to make this invent a new session keyring
350 */
351int suid_keys(struct task_struct *tsk)
352{
353 return 0;
354
355} /* end suid_keys() */
356
357/*****************************************************************************/
358/*
359 * the filesystem user ID changed
360 */
361void key_fsuid_changed(struct task_struct *tsk)
362{
363 /* update the ownership of the thread keyring */
364 if (tsk->thread_keyring) {
365 down_write(&tsk->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 tsk->thread_keyring->uid = tsk->fsuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 up_write(&tsk->thread_keyring->sem);
368 }
369
370} /* end key_fsuid_changed() */
371
372/*****************************************************************************/
373/*
374 * the filesystem group ID changed
375 */
376void key_fsgid_changed(struct task_struct *tsk)
377{
378 /* update the ownership of the thread keyring */
379 if (tsk->thread_keyring) {
380 down_write(&tsk->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 tsk->thread_keyring->gid = tsk->fsgid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 up_write(&tsk->thread_keyring->sem);
383 }
384
385} /* end key_fsgid_changed() */
386
387/*****************************************************************************/
388/*
389 * search the process keyrings for the first matching key
390 * - we use the supplied match function to see if the description (or other
391 * feature of interest) matches
392 * - we return -EAGAIN if we didn't find any matching key
393 * - we return -ENOKEY if we found only negative matching keys
394 */
David Howells664cceb2005-09-28 17:03:15 +0100395key_ref_t search_process_keyrings(struct key_type *type,
396 const void *description,
397 key_match_func_t match,
398 struct task_struct *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
David Howells3e301482005-06-23 22:00:56 -0700400 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800401 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
David Howells04c567d2006-06-22 14:47:18 -0700403 might_sleep();
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
406 * searchable, but we failed to find a key or we found a negative key;
407 * otherwise we want to return a sample error (probably -EACCES) if
408 * none of the keyrings were searchable
409 *
410 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
411 */
David Howells664cceb2005-09-28 17:03:15 +0100412 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 ret = NULL;
414 err = ERR_PTR(-EAGAIN);
415
416 /* search the thread keyring first */
David Howells3e301482005-06-23 22:00:56 -0700417 if (context->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100418 key_ref = keyring_search_aux(
419 make_key_ref(context->thread_keyring, 1),
420 context, type, description, match);
421 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 goto found;
423
David Howells664cceb2005-09-28 17:03:15 +0100424 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 case -EAGAIN: /* no key */
426 if (ret)
427 break;
428 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100429 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 default:
David Howells664cceb2005-09-28 17:03:15 +0100432 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 break;
434 }
435 }
436
437 /* search the process keyring second */
David Howells3e301482005-06-23 22:00:56 -0700438 if (context->signal->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100439 key_ref = keyring_search_aux(
440 make_key_ref(context->signal->process_keyring, 1),
441 context, type, description, match);
442 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto found;
444
David Howells664cceb2005-09-28 17:03:15 +0100445 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 case -EAGAIN: /* no key */
447 if (ret)
448 break;
449 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100450 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 break;
452 default:
David Howells664cceb2005-09-28 17:03:15 +0100453 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 break;
455 }
456 }
457
David Howells3e301482005-06-23 22:00:56 -0700458 /* search the session keyring */
459 if (context->signal->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700460 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100461 key_ref = keyring_search_aux(
462 make_key_ref(rcu_dereference(
463 context->signal->session_keyring),
464 1),
David Howells3e301482005-06-23 22:00:56 -0700465 context, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Howells664cceb2005-09-28 17:03:15 +0100468 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700469 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
David Howells664cceb2005-09-28 17:03:15 +0100471 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700472 case -EAGAIN: /* no key */
473 if (ret)
474 break;
475 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100476 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
David Howells3e301482005-06-23 22:00:56 -0700478 default:
David Howells664cceb2005-09-28 17:03:15 +0100479 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700480 break;
481 }
David Howells3e301482005-06-23 22:00:56 -0700482 }
483 /* or search the user-session keyring */
484 else {
David Howells664cceb2005-09-28 17:03:15 +0100485 key_ref = keyring_search_aux(
486 make_key_ref(context->user->session_keyring, 1),
487 context, type, description, match);
488 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700489 goto found;
490
David Howells664cceb2005-09-28 17:03:15 +0100491 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700492 case -EAGAIN: /* no key */
493 if (ret)
494 break;
495 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100496 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700497 break;
498 default:
David Howells664cceb2005-09-28 17:03:15 +0100499 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700500 break;
501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
David Howellsb5f545c2006-01-08 01:02:47 -0800504 /* if this process has an instantiation authorisation key, then we also
505 * search the keyrings of the process mentioned there
506 * - we don't permit access to request_key auth keys via this method
507 */
508 if (context->request_key_auth &&
509 context == current &&
David Howells04c567d2006-06-22 14:47:18 -0700510 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800511 ) {
David Howells04c567d2006-06-22 14:47:18 -0700512 /* defend against the auth key being revoked */
513 down_read(&context->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700514
David Howells04c567d2006-06-22 14:47:18 -0700515 if (key_validate(context->request_key_auth) == 0) {
516 rka = context->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800517
David Howells04c567d2006-06-22 14:47:18 -0700518 key_ref = search_process_keyrings(type, description,
519 match, rka->context);
David Howellsb5f545c2006-01-08 01:02:47 -0800520
David Howells04c567d2006-06-22 14:47:18 -0700521 up_read(&context->request_key_auth->sem);
522
523 if (!IS_ERR(key_ref))
524 goto found;
525
526 switch (PTR_ERR(key_ref)) {
527 case -EAGAIN: /* no key */
528 if (ret)
529 break;
530 case -ENOKEY: /* negative key */
531 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800532 break;
David Howells04c567d2006-06-22 14:47:18 -0700533 default:
534 err = key_ref;
535 break;
536 }
537 } else {
538 up_read(&context->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800539 }
540 }
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100543 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
David Howells3e301482005-06-23 22:00:56 -0700545found:
David Howells664cceb2005-09-28 17:03:15 +0100546 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548} /* end search_process_keyrings() */
549
550/*****************************************************************************/
551/*
David Howells664cceb2005-09-28 17:03:15 +0100552 * see if the key we're looking at is the target key
553 */
554static int lookup_user_key_possessed(const struct key *key, const void *target)
555{
556 return key == target;
557
558} /* end lookup_user_key_possessed() */
559
560/*****************************************************************************/
561/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * lookup a key given a key ID from userspace with a given permissions mask
563 * - don't create special keyrings unless so requested
564 * - partially constructed keys aren't found unless requested
565 */
David Howells664cceb2005-09-28 17:03:15 +0100566key_ref_t lookup_user_key(struct task_struct *context, key_serial_t id,
567 int create, int partial, key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
David Howells664cceb2005-09-28 17:03:15 +0100569 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct key *key;
571 int ret;
572
David Howells3e301482005-06-23 22:00:56 -0700573 if (!context)
574 context = current;
575
David Howells664cceb2005-09-28 17:03:15 +0100576 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 switch (id) {
579 case KEY_SPEC_THREAD_KEYRING:
David Howells3e301482005-06-23 22:00:56 -0700580 if (!context->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (!create)
582 goto error;
583
David Howells3e301482005-06-23 22:00:56 -0700584 ret = install_thread_keyring(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (ret < 0) {
586 key = ERR_PTR(ret);
587 goto error;
588 }
589 }
590
David Howells3e301482005-06-23 22:00:56 -0700591 key = context->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100593 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
595
596 case KEY_SPEC_PROCESS_KEYRING:
David Howells3e301482005-06-23 22:00:56 -0700597 if (!context->signal->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (!create)
599 goto error;
600
David Howells3e301482005-06-23 22:00:56 -0700601 ret = install_process_keyring(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (ret < 0) {
603 key = ERR_PTR(ret);
604 goto error;
605 }
606 }
607
David Howells3e301482005-06-23 22:00:56 -0700608 key = context->signal->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100610 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 break;
612
613 case KEY_SPEC_SESSION_KEYRING:
David Howells3e301482005-06-23 22:00:56 -0700614 if (!context->signal->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /* always install a session keyring upon access if one
616 * doesn't exist yet */
617 ret = install_session_keyring(
David Howells664cceb2005-09-28 17:03:15 +0100618 context, context->user->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (ret < 0)
620 goto error;
621 }
622
David Howells3e301482005-06-23 22:00:56 -0700623 rcu_read_lock();
624 key = rcu_dereference(context->signal->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700626 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100627 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 break;
629
630 case KEY_SPEC_USER_KEYRING:
David Howells3e301482005-06-23 22:00:56 -0700631 key = context->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100633 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635
636 case KEY_SPEC_USER_SESSION_KEYRING:
David Howells3e301482005-06-23 22:00:56 -0700637 key = context->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100639 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641
642 case KEY_SPEC_GROUP_KEYRING:
643 /* group keyrings are not yet supported */
644 key = ERR_PTR(-EINVAL);
645 goto error;
646
David Howellsb5f545c2006-01-08 01:02:47 -0800647 case KEY_SPEC_REQKEY_AUTH_KEY:
648 key = context->request_key_auth;
649 if (!key)
650 goto error;
651
652 atomic_inc(&key->usage);
653 key_ref = make_key_ref(key, 1);
654 break;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 default:
David Howells664cceb2005-09-28 17:03:15 +0100657 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (id < 1)
659 goto error;
660
661 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100662 if (IS_ERR(key)) {
663 key_ref = ERR_PTR(PTR_ERR(key));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100665 }
666
667 key_ref = make_key_ref(key, 0);
668
669 /* check to see if we possess the key */
670 skey_ref = search_process_keyrings(key->type, key,
671 lookup_user_key_possessed,
672 current);
673
674 if (!IS_ERR(skey_ref)) {
675 key_put(key);
676 key_ref = skey_ref;
677 }
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 break;
680 }
681
David Howells3e301482005-06-23 22:00:56 -0700682 /* check the status */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (perm) {
684 ret = key_validate(key);
685 if (ret < 0)
686 goto invalid_key;
687 }
688
689 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700690 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 goto invalid_key;
692
David Howells3e301482005-06-23 22:00:56 -0700693 /* check the permissions */
David Howells29db9192005-10-30 15:02:44 -0800694 ret = key_task_permission(key_ref, context, perm);
695 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 goto invalid_key;
697
David Howells664cceb2005-09-28 17:03:15 +0100698error:
699 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
David Howells664cceb2005-09-28 17:03:15 +0100701invalid_key:
702 key_ref_put(key_ref);
703 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto error;
705
706} /* end lookup_user_key() */
707
708/*****************************************************************************/
709/*
710 * join the named keyring as the session keyring if possible, or attempt to
711 * create a new one of that name if not
712 * - if the name is NULL, an empty anonymous keyring is installed instead
713 * - named session keyring joining is done with a semaphore held
714 */
715long join_session_keyring(const char *name)
716{
717 struct task_struct *tsk = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct key *keyring;
719 long ret;
720
721 /* if no name is provided, install an anonymous keyring */
722 if (!name) {
723 ret = install_session_keyring(tsk, NULL);
724 if (ret < 0)
725 goto error;
726
David Howells3e301482005-06-23 22:00:56 -0700727 rcu_read_lock();
728 ret = rcu_dereference(tsk->signal->session_keyring)->serial;
729 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto error;
731 }
732
733 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800734 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* look for an existing keyring of this name */
737 keyring = find_keyring_by_name(name, 0);
738 if (PTR_ERR(keyring) == -ENOKEY) {
739 /* not found - try and create a new one */
David Howells7e047ef2006-06-26 00:24:50 -0700740 keyring = keyring_alloc(name, tsk->uid, tsk->gid, tsk,
741 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (IS_ERR(keyring)) {
743 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700744 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746 }
747 else if (IS_ERR(keyring)) {
748 ret = PTR_ERR(keyring);
749 goto error2;
750 }
751
752 /* we've got a keyring - now to install it */
753 ret = install_session_keyring(tsk, keyring);
754 if (ret < 0)
755 goto error2;
756
757 ret = keyring->serial;
758 key_put(keyring);
759
David Howells664cceb2005-09-28 17:03:15 +0100760error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800761 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100762error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return ret;
764
765} /* end join_session_keyring() */