blob: 70ee93406f309cdd0ae2338651f520267229c0fb [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 Molnarbb0030792006-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 Molnarbb0030792006-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
David Howellsbb952bb2008-11-14 10:39:20 +1100192 if (!tsk->cred->tgcred->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 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 Howellsbb952bb2008-11-14 10:39:20 +1100203 spin_lock_irq(&tsk->cred->tgcred->lock);
204 if (!tsk->cred->tgcred->process_keyring) {
205 tsk->cred->tgcred->process_keyring = keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 keyring = NULL;
207 }
David Howellsbb952bb2008-11-14 10:39:20 +1100208 spin_unlock_irq(&tsk->cred->tgcred->lock);
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;
David Howellsbb952bb2008-11-14 10:39:20 +1100238 if (tsk->cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700239 flags = KEY_ALLOC_IN_QUOTA;
240
David Howellsbb952bb2008-11-14 10:39:20 +1100241 keyring = keyring_alloc(buf, tsk->cred->uid, tsk->cred->gid,
242 tsk, 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 Howellsbb952bb2008-11-14 10:39:20 +1100251 spin_lock_irq(&tsk->cred->tgcred->lock);
252 old = tsk->cred->tgcred->session_keyring;
253 rcu_assign_pointer(tsk->cred->tgcred->session_keyring, keyring);
254 spin_unlock_irq(&tsk->cred->tgcred->lock);
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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * copy the keys for fork
270 */
271int copy_keys(unsigned long clone_flags, struct task_struct *tsk)
272{
David Howellsb6dff3e2008-11-14 10:39:16 +1100273 key_check(tsk->cred->thread_keyring);
274 key_check(tsk->cred->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* no thread keyring yet */
David Howellsb6dff3e2008-11-14 10:39:16 +1100277 tsk->cred->thread_keyring = NULL;
David Howellsb5f545c2006-01-08 01:02:47 -0800278
279 /* copy the request_key() authorisation for this thread */
David Howellsb6dff3e2008-11-14 10:39:16 +1100280 key_get(tsk->cred->request_key_auth);
David Howellsb5f545c2006-01-08 01:02:47 -0800281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283
284} /* end copy_keys() */
285
286/*****************************************************************************/
287/*
David Howellsb5f545c2006-01-08 01:02:47 -0800288 * dispose of per-thread keys upon thread exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290void exit_keys(struct task_struct *tsk)
291{
David Howellsb6dff3e2008-11-14 10:39:16 +1100292 key_put(tsk->cred->thread_keyring);
293 key_put(tsk->cred->request_key_auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295} /* end exit_keys() */
296
297/*****************************************************************************/
298/*
299 * deal with execve()
300 */
301int exec_keys(struct task_struct *tsk)
302{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct key *old;
304
305 /* newly exec'd tasks don't get a thread keyring */
306 task_lock(tsk);
David Howellsb6dff3e2008-11-14 10:39:16 +1100307 old = tsk->cred->thread_keyring;
308 tsk->cred->thread_keyring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 task_unlock(tsk);
310
311 key_put(old);
312
313 /* discard the process keyring from a newly exec'd task */
David Howellsbb952bb2008-11-14 10:39:20 +1100314 spin_lock_irq(&tsk->cred->tgcred->lock);
315 old = tsk->cred->tgcred->process_keyring;
316 tsk->cred->tgcred->process_keyring = NULL;
317 spin_unlock_irq(&tsk->cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 key_put(old);
320
321 return 0;
322
323} /* end exec_keys() */
324
325/*****************************************************************************/
326/*
327 * deal with SUID programs
328 * - we might want to make this invent a new session keyring
329 */
330int suid_keys(struct task_struct *tsk)
331{
332 return 0;
333
334} /* end suid_keys() */
335
336/*****************************************************************************/
337/*
338 * the filesystem user ID changed
339 */
340void key_fsuid_changed(struct task_struct *tsk)
341{
342 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100343 BUG_ON(!tsk->cred);
344 if (tsk->cred->thread_keyring) {
345 down_write(&tsk->cred->thread_keyring->sem);
346 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
347 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350} /* end key_fsuid_changed() */
351
352/*****************************************************************************/
353/*
354 * the filesystem group ID changed
355 */
356void key_fsgid_changed(struct task_struct *tsk)
357{
358 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100359 BUG_ON(!tsk->cred);
360 if (tsk->cred->thread_keyring) {
361 down_write(&tsk->cred->thread_keyring->sem);
362 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
363 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366} /* end key_fsgid_changed() */
367
368/*****************************************************************************/
369/*
370 * search the process keyrings for the first matching key
371 * - we use the supplied match function to see if the description (or other
372 * feature of interest) matches
373 * - we return -EAGAIN if we didn't find any matching key
374 * - we return -ENOKEY if we found only negative matching keys
375 */
David Howells664cceb2005-09-28 17:03:15 +0100376key_ref_t search_process_keyrings(struct key_type *type,
377 const void *description,
378 key_match_func_t match,
379 struct task_struct *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
David Howells3e301482005-06-23 22:00:56 -0700381 struct request_key_auth *rka;
David Howellsc69e8d92008-11-14 10:39:19 +1100382 struct cred *cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800383 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Howells04c567d2006-06-22 14:47:18 -0700385 might_sleep();
386
David Howellsc69e8d92008-11-14 10:39:19 +1100387 cred = get_task_cred(context);
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
390 * searchable, but we failed to find a key or we found a negative key;
391 * otherwise we want to return a sample error (probably -EACCES) if
392 * none of the keyrings were searchable
393 *
394 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
395 */
David Howells664cceb2005-09-28 17:03:15 +0100396 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 ret = NULL;
398 err = ERR_PTR(-EAGAIN);
399
400 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100401 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100402 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100403 make_key_ref(cred->thread_keyring, 1),
David Howells664cceb2005-09-28 17:03:15 +0100404 context, type, description, match);
405 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto found;
407
David Howells664cceb2005-09-28 17:03:15 +0100408 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 case -EAGAIN: /* no key */
410 if (ret)
411 break;
412 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100413 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 break;
415 default:
David Howells664cceb2005-09-28 17:03:15 +0100416 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 break;
418 }
419 }
420
421 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100422 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100423 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100424 make_key_ref(cred->tgcred->process_keyring, 1),
David Howells664cceb2005-09-28 17:03:15 +0100425 context, type, description, match);
426 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto found;
428
David Howells664cceb2005-09-28 17:03:15 +0100429 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 case -EAGAIN: /* no key */
431 if (ret)
432 break;
433 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100434 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
436 default:
David Howells664cceb2005-09-28 17:03:15 +0100437 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
439 }
440 }
441
David Howells3e301482005-06-23 22:00:56 -0700442 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100443 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700444 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100445 key_ref = keyring_search_aux(
446 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100447 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100448 1),
David Howells3e301482005-06-23 22:00:56 -0700449 context, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700450 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
David Howells664cceb2005-09-28 17:03:15 +0100452 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700453 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
David Howells3e301482005-06-23 22:00:56 -0700462 default:
David Howells664cceb2005-09-28 17:03:15 +0100463 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700464 break;
465 }
David Howells3e301482005-06-23 22:00:56 -0700466 }
467 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100468 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100469 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100470 make_key_ref(cred->user->session_keyring, 1),
David Howells664cceb2005-09-28 17:03:15 +0100471 context, type, description, match);
472 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700473 goto found;
474
David Howells664cceb2005-09-28 17:03:15 +0100475 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700476 case -EAGAIN: /* no key */
477 if (ret)
478 break;
479 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100480 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700481 break;
482 default:
David Howells664cceb2005-09-28 17:03:15 +0100483 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700484 break;
485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487
David Howellsb5f545c2006-01-08 01:02:47 -0800488 /* if this process has an instantiation authorisation key, then we also
489 * search the keyrings of the process mentioned there
490 * - we don't permit access to request_key auth keys via this method
491 */
David Howellsc69e8d92008-11-14 10:39:19 +1100492 if (cred->request_key_auth &&
David Howellsb5f545c2006-01-08 01:02:47 -0800493 context == current &&
David Howells04c567d2006-06-22 14:47:18 -0700494 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800495 ) {
David Howells04c567d2006-06-22 14:47:18 -0700496 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100497 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700498
David Howellsc69e8d92008-11-14 10:39:19 +1100499 if (key_validate(cred->request_key_auth) == 0) {
500 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800501
David Howells04c567d2006-06-22 14:47:18 -0700502 key_ref = search_process_keyrings(type, description,
503 match, rka->context);
David Howellsb5f545c2006-01-08 01:02:47 -0800504
David Howellsc69e8d92008-11-14 10:39:19 +1100505 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700506
507 if (!IS_ERR(key_ref))
508 goto found;
509
510 switch (PTR_ERR(key_ref)) {
511 case -EAGAIN: /* no key */
512 if (ret)
513 break;
514 case -ENOKEY: /* negative key */
515 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800516 break;
David Howells04c567d2006-06-22 14:47:18 -0700517 default:
518 err = key_ref;
519 break;
520 }
521 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100522 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800523 }
524 }
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100527 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
David Howells3e301482005-06-23 22:00:56 -0700529found:
David Howellsc69e8d92008-11-14 10:39:19 +1100530 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100531 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533} /* end search_process_keyrings() */
534
535/*****************************************************************************/
536/*
David Howells664cceb2005-09-28 17:03:15 +0100537 * see if the key we're looking at is the target key
538 */
539static int lookup_user_key_possessed(const struct key *key, const void *target)
540{
541 return key == target;
542
543} /* end lookup_user_key_possessed() */
544
545/*****************************************************************************/
546/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 * lookup a key given a key ID from userspace with a given permissions mask
548 * - don't create special keyrings unless so requested
549 * - partially constructed keys aren't found unless requested
550 */
David Howells8bbf49762008-11-14 10:39:14 +1100551key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
552 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
David Howells8bbf49762008-11-14 10:39:14 +1100554 struct request_key_auth *rka;
555 struct task_struct *t = current;
David Howellsbb952bb2008-11-14 10:39:20 +1100556 struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100558 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int ret;
560
David Howellsbb952bb2008-11-14 10:39:20 +1100561try_again:
562 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100563 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 switch (id) {
566 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100567 if (!cred->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!create)
569 goto error;
570
David Howells8bbf49762008-11-14 10:39:14 +1100571 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (ret < 0) {
573 key = ERR_PTR(ret);
574 goto error;
575 }
David Howellsbb952bb2008-11-14 10:39:20 +1100576 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578
David Howellsb6dff3e2008-11-14 10:39:16 +1100579 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100581 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 break;
583
584 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100585 if (!cred->tgcred->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (!create)
587 goto error;
588
David Howells8bbf49762008-11-14 10:39:14 +1100589 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (ret < 0) {
591 key = ERR_PTR(ret);
592 goto error;
593 }
David Howellsbb952bb2008-11-14 10:39:20 +1100594 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596
David Howellsbb952bb2008-11-14 10:39:20 +1100597 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 atomic_inc(&key->usage);
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_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100603 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* always install a session keyring upon access if one
605 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100606 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700607 if (ret < 0)
608 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100609 ret = install_session_keyring(
610 cred->user->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (ret < 0)
612 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100613 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
615
David Howells3e301482005-06-23 22:00:56 -0700616 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100617 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700619 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100620 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 break;
622
623 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100624 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100625 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700626 if (ret < 0)
627 goto error;
628 }
629
David Howellsb6dff3e2008-11-14 10:39:16 +1100630 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100632 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
634
635 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100636 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100637 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700638 if (ret < 0)
639 goto error;
640 }
641
David Howellsb6dff3e2008-11-14 10:39:16 +1100642 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 atomic_inc(&key->usage);
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_GROUP_KEYRING:
648 /* group keyrings are not yet supported */
649 key = ERR_PTR(-EINVAL);
650 goto error;
651
David Howellsb5f545c2006-01-08 01:02:47 -0800652 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100653 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800654 if (!key)
655 goto error;
656
657 atomic_inc(&key->usage);
658 key_ref = make_key_ref(key, 1);
659 break;
660
David Howells8bbf49762008-11-14 10:39:14 +1100661 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100662 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100663 goto error;
664
David Howellsb6dff3e2008-11-14 10:39:16 +1100665 down_read(&cred->request_key_auth->sem);
666 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100667 key_ref = ERR_PTR(-EKEYREVOKED);
668 key = NULL;
669 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100670 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100671 key = rka->dest_keyring;
672 atomic_inc(&key->usage);
673 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100674 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100675 if (!key)
676 goto error;
677 key_ref = make_key_ref(key, 1);
678 break;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 default:
David Howells664cceb2005-09-28 17:03:15 +0100681 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (id < 1)
683 goto error;
684
685 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100686 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800687 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100689 }
690
691 key_ref = make_key_ref(key, 0);
692
693 /* check to see if we possess the key */
694 skey_ref = search_process_keyrings(key->type, key,
695 lookup_user_key_possessed,
696 current);
697
698 if (!IS_ERR(skey_ref)) {
699 key_put(key);
700 key_ref = skey_ref;
701 }
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 break;
704 }
705
David Howells76181c12007-10-16 23:29:46 -0700706 if (!partial) {
707 ret = wait_for_key_construction(key, true);
708 switch (ret) {
709 case -ERESTARTSYS:
710 goto invalid_key;
711 default:
712 if (perm)
713 goto invalid_key;
714 case 0:
715 break;
716 }
717 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 ret = key_validate(key);
719 if (ret < 0)
720 goto invalid_key;
721 }
722
723 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700724 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto invalid_key;
726
David Howells3e301482005-06-23 22:00:56 -0700727 /* check the permissions */
David Howells8bbf49762008-11-14 10:39:14 +1100728 ret = key_task_permission(key_ref, t, perm);
David Howells29db9192005-10-30 15:02:44 -0800729 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 goto invalid_key;
731
David Howells664cceb2005-09-28 17:03:15 +0100732error:
David Howellsbb952bb2008-11-14 10:39:20 +1100733 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100734 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
David Howells664cceb2005-09-28 17:03:15 +0100736invalid_key:
737 key_ref_put(key_ref);
738 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto error;
740
David Howellsbb952bb2008-11-14 10:39:20 +1100741 /* if we attempted to install a keyring, then it may have caused new
742 * creds to be installed */
743reget_creds:
744 put_cred(cred);
745 goto try_again;
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747} /* end lookup_user_key() */
748
749/*****************************************************************************/
750/*
751 * join the named keyring as the session keyring if possible, or attempt to
752 * create a new one of that name if not
753 * - if the name is NULL, an empty anonymous keyring is installed instead
754 * - named session keyring joining is done with a semaphore held
755 */
756long join_session_keyring(const char *name)
757{
758 struct task_struct *tsk = current;
David Howellsbb952bb2008-11-14 10:39:20 +1100759 struct cred *cred = current->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct key *keyring;
761 long ret;
762
763 /* if no name is provided, install an anonymous keyring */
764 if (!name) {
David Howells8bbf49762008-11-14 10:39:14 +1100765 ret = install_session_keyring(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (ret < 0)
767 goto error;
768
David Howells3e301482005-06-23 22:00:56 -0700769 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100770 ret = rcu_dereference(cred->tgcred->session_keyring)->serial;
David Howells3e301482005-06-23 22:00:56 -0700771 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto error;
773 }
774
775 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800776 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700779 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (PTR_ERR(keyring) == -ENOKEY) {
781 /* not found - try and create a new one */
David Howellsbb952bb2008-11-14 10:39:20 +1100782 keyring = keyring_alloc(name, cred->uid, cred->gid, tsk,
David Howells7e047ef2006-06-26 00:24:50 -0700783 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (IS_ERR(keyring)) {
785 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700786 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
788 }
789 else if (IS_ERR(keyring)) {
790 ret = PTR_ERR(keyring);
791 goto error2;
792 }
793
794 /* we've got a keyring - now to install it */
David Howells8bbf49762008-11-14 10:39:14 +1100795 ret = install_session_keyring(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (ret < 0)
797 goto error2;
798
799 ret = keyring->serial;
800 key_put(keyring);
801
David Howells664cceb2005-09-28 17:03:15 +0100802error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800803 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100804error:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return ret;
806
807} /* end join_session_keyring() */