blob: 276d27882ce84394d7d6ce5b0c12d82e05f89987 [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>
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060020#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/uaccess.h>
22#include "internal.h"
23
24/* session keyring create vs join semaphore */
Ingo Molnarbb003072006-03-22 00:09:14 -080025static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells69664cf2008-04-29 01:01:31 -070027/* user keyring creation semaphore */
28static DEFINE_MUTEX(key_user_keyring_mutex);
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/* the root user's tracking struct */
31struct key_user root_key_user = {
32 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070033 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080034 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 .nkeys = ATOMIC_INIT(2),
36 .nikeys = ATOMIC_INIT(2),
37 .uid = 0,
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -060038 .user_ns = &init_user_ns,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/*****************************************************************************/
42/*
David Howells69664cf2008-04-29 01:01:31 -070043 * install user and user session keyrings for a particular UID
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 */
David Howells8bbf49762008-11-14 10:39:14 +110045int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
David Howellsd84f4f92008-11-14 10:39:23 +110047 struct user_struct *user;
48 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct key *uid_keyring, *session_keyring;
50 char buf[20];
51 int ret;
52
David Howellsd84f4f92008-11-14 10:39:23 +110053 cred = current_cred();
54 user = cred->user;
55
David Howells69664cf2008-04-29 01:01:31 -070056 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
David Howells69664cf2008-04-29 01:01:31 -070058 if (user->uid_keyring) {
59 kleave(" = 0 [exist]");
60 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
62
David Howells69664cf2008-04-29 01:01:31 -070063 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 ret = 0;
65
David Howells69664cf2008-04-29 01:01:31 -070066 if (!user->uid_keyring) {
67 /* get the UID-specific keyring
68 * - there may be one in existence already as it may have been
69 * pinned by a session, but the user_struct pointing to it
70 * may have been destroyed by setuid */
71 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
David Howells69664cf2008-04-29 01:01:31 -070073 uid_keyring = find_keyring_by_name(buf, true);
74 if (IS_ERR(uid_keyring)) {
75 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110076 cred, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070077 NULL);
78 if (IS_ERR(uid_keyring)) {
79 ret = PTR_ERR(uid_keyring);
80 goto error;
81 }
82 }
83
84 /* get a default session keyring (which might also exist
85 * already) */
86 sprintf(buf, "_uid_ses.%u", user->uid);
87
88 session_keyring = find_keyring_by_name(buf, true);
89 if (IS_ERR(session_keyring)) {
90 session_keyring =
91 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110092 cred, KEY_ALLOC_IN_QUOTA, NULL);
David Howells69664cf2008-04-29 01:01:31 -070093 if (IS_ERR(session_keyring)) {
94 ret = PTR_ERR(session_keyring);
95 goto error_release;
96 }
97
98 /* we install a link from the user session keyring to
99 * the user keyring */
100 ret = key_link(session_keyring, uid_keyring);
101 if (ret < 0)
102 goto error_release_both;
103 }
104
105 /* install the keyrings */
106 user->uid_keyring = uid_keyring;
107 user->session_keyring = session_keyring;
108 }
109
110 mutex_unlock(&key_user_keyring_mutex);
111 kleave(" = 0");
112 return 0;
113
114error_release_both:
115 key_put(session_keyring);
116error_release:
117 key_put(uid_keyring);
118error:
119 mutex_unlock(&key_user_keyring_mutex);
120 kleave(" = %d", ret);
121 return ret;
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
David Howellsd84f4f92008-11-14 10:39:23 +1100125 * install a fresh thread keyring directly to new credentials
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
David Howellsd84f4f92008-11-14 10:39:23 +1100127int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
David Howellsd84f4f92008-11-14 10:39:23 +1100129 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
David Howellsd84f4f92008-11-14 10:39:23 +1100131 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
132 KEY_ALLOC_QUOTA_OVERRUN, NULL);
133 if (IS_ERR(keyring))
134 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
David Howellsd84f4f92008-11-14 10:39:23 +1100136 new->thread_keyring = keyring;
137 return 0;
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
141 * install a fresh thread keyring, discarding the old one
142 */
David Howellsd84f4f92008-11-14 10:39:23 +1100143static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
David Howellsd84f4f92008-11-14 10:39:23 +1100145 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int ret;
147
David Howellsd84f4f92008-11-14 10:39:23 +1100148 new = prepare_creds();
149 if (!new)
150 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
David Howellsd84f4f92008-11-14 10:39:23 +1100152 BUG_ON(new->thread_keyring);
153
154 ret = install_thread_keyring_to_cred(new);
155 if (ret < 0) {
156 abort_creds(new);
157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159
David Howellsd84f4f92008-11-14 10:39:23 +1100160 return commit_creds(new);
161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
David Howellsd84f4f92008-11-14 10:39:23 +1100163/*
164 * install a process keyring directly to a credentials struct
165 * - returns -EEXIST if there was already a process keyring, 0 if one installed,
166 * and other -ve on any other error
167 */
168int install_process_keyring_to_cred(struct cred *new)
169{
170 struct key *keyring;
171 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
David Howellsd84f4f92008-11-14 10:39:23 +1100173 if (new->tgcred->process_keyring)
174 return -EEXIST;
175
176 keyring = keyring_alloc("_pid", new->uid, new->gid,
177 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
178 if (IS_ERR(keyring))
179 return PTR_ERR(keyring);
180
181 spin_lock_irq(&new->tgcred->lock);
182 if (!new->tgcred->process_keyring) {
183 new->tgcred->process_keyring = keyring;
184 keyring = NULL;
185 ret = 0;
186 } else {
187 ret = -EEXIST;
188 }
189 spin_unlock_irq(&new->tgcred->lock);
190 key_put(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100192}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * make sure a process keyring is installed
David Howellsd84f4f92008-11-14 10:39:23 +1100196 * - we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
David Howellsd84f4f92008-11-14 10:39:23 +1100198static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
David Howellsd84f4f92008-11-14 10:39:23 +1100200 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 int ret;
202
David Howellsd84f4f92008-11-14 10:39:23 +1100203 new = prepare_creds();
204 if (!new)
205 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700206
David Howellsd84f4f92008-11-14 10:39:23 +1100207 ret = install_process_keyring_to_cred(new);
208 if (ret < 0) {
209 abort_creds(new);
210 return ret != -EEXIST ?: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
David Howellsd84f4f92008-11-14 10:39:23 +1100213 return commit_creds(new);
214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howellsd84f4f92008-11-14 10:39:23 +1100217 * install a session keyring directly to a credentials struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 */
David Howellsd84f4f92008-11-14 10:39:23 +1100219static int install_session_keyring_to_cred(struct cred *cred,
220 struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David Howells7e047ef2006-06-26 00:24:50 -0700222 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700224
225 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* create an empty session keyring */
228 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700229 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howellsd84f4f92008-11-14 10:39:23 +1100230 if (cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700231 flags = KEY_ALLOC_IN_QUOTA;
232
David Howellsd84f4f92008-11-14 10:39:23 +1100233 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
234 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700235 if (IS_ERR(keyring))
236 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100237 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 atomic_inc(&keyring->usage);
239 }
240
241 /* install the keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100242 spin_lock_irq(&cred->tgcred->lock);
243 old = cred->tgcred->session_keyring;
244 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
245 spin_unlock_irq(&cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David Howells1a26feb2006-04-10 22:54:26 -0700247 /* we're using RCU on the pointer, but there's no point synchronising
248 * on it if it didn't previously point to anything */
249 if (old) {
250 synchronize_rcu();
251 key_put(old);
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
David Howells1a26feb2006-04-10 22:54:26 -0700254 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/*
David Howellsd84f4f92008-11-14 10:39:23 +1100258 * install a session keyring, discarding the old one
259 * - if a keyring is not supplied, an empty one is invented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
David Howellsd84f4f92008-11-14 10:39:23 +1100261static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
David Howellsd84f4f92008-11-14 10:39:23 +1100263 struct cred *new;
264 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
David Howellsd84f4f92008-11-14 10:39:23 +1100266 new = prepare_creds();
267 if (!new)
268 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800269
David Howellsd84f4f92008-11-14 10:39:23 +1100270 ret = install_session_keyring_to_cred(new, NULL);
271 if (ret < 0) {
272 abort_creds(new);
273 return ret;
274 }
David Howellsb5f545c2006-01-08 01:02:47 -0800275
David Howellsd84f4f92008-11-14 10:39:23 +1100276 return commit_creds(new);
277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279/*****************************************************************************/
280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * the filesystem user ID changed
282 */
283void key_fsuid_changed(struct task_struct *tsk)
284{
285 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100286 BUG_ON(!tsk->cred);
287 if (tsk->cred->thread_keyring) {
288 down_write(&tsk->cred->thread_keyring->sem);
289 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
290 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
293} /* end key_fsuid_changed() */
294
295/*****************************************************************************/
296/*
297 * the filesystem group ID changed
298 */
299void key_fsgid_changed(struct task_struct *tsk)
300{
301 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100302 BUG_ON(!tsk->cred);
303 if (tsk->cred->thread_keyring) {
304 down_write(&tsk->cred->thread_keyring->sem);
305 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
306 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
309} /* end key_fsgid_changed() */
310
311/*****************************************************************************/
312/*
313 * search the process keyrings for the first matching key
314 * - we use the supplied match function to see if the description (or other
315 * feature of interest) matches
316 * - we return -EAGAIN if we didn't find any matching key
317 * - we return -ENOKEY if we found only negative matching keys
318 */
David Howells664cceb2005-09-28 17:03:15 +0100319key_ref_t search_process_keyrings(struct key_type *type,
320 const void *description,
321 key_match_func_t match,
David Howellsd84f4f92008-11-14 10:39:23 +1100322 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
David Howells3e301482005-06-23 22:00:56 -0700324 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800325 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
David Howells04c567d2006-06-22 14:47:18 -0700327 might_sleep();
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
330 * searchable, but we failed to find a key or we found a negative key;
331 * otherwise we want to return a sample error (probably -EACCES) if
332 * none of the keyrings were searchable
333 *
334 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
335 */
David Howells664cceb2005-09-28 17:03:15 +0100336 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ret = NULL;
338 err = ERR_PTR(-EAGAIN);
339
340 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100341 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100342 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100343 make_key_ref(cred->thread_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100344 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100345 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 goto found;
347
David Howells664cceb2005-09-28 17:03:15 +0100348 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 case -EAGAIN: /* no key */
350 if (ret)
351 break;
352 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100353 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355 default:
David Howells664cceb2005-09-28 17:03:15 +0100356 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
359 }
360
361 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100362 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100363 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100364 make_key_ref(cred->tgcred->process_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100365 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100366 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto found;
368
David Howells664cceb2005-09-28 17:03:15 +0100369 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case -EAGAIN: /* no key */
371 if (ret)
372 break;
373 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100374 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 break;
376 default:
David Howells664cceb2005-09-28 17:03:15 +0100377 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
379 }
380 }
381
David Howells3e301482005-06-23 22:00:56 -0700382 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100383 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700384 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100385 key_ref = keyring_search_aux(
386 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100387 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100388 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100389 cred, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700390 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
David Howells664cceb2005-09-28 17:03:15 +0100392 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700393 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
David Howells664cceb2005-09-28 17:03:15 +0100395 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700396 case -EAGAIN: /* no key */
397 if (ret)
398 break;
399 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100400 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
David Howells3e301482005-06-23 22:00:56 -0700402 default:
David Howells664cceb2005-09-28 17:03:15 +0100403 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700404 break;
405 }
David Howells3e301482005-06-23 22:00:56 -0700406 }
407 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100408 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100409 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100410 make_key_ref(cred->user->session_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100411 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100412 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700413 goto found;
414
David Howells664cceb2005-09-28 17:03:15 +0100415 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700416 case -EAGAIN: /* no key */
417 if (ret)
418 break;
419 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100420 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700421 break;
422 default:
David Howells664cceb2005-09-28 17:03:15 +0100423 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700424 break;
425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
David Howellsb5f545c2006-01-08 01:02:47 -0800428 /* if this process has an instantiation authorisation key, then we also
429 * search the keyrings of the process mentioned there
430 * - we don't permit access to request_key auth keys via this method
431 */
David Howellsc69e8d92008-11-14 10:39:19 +1100432 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100433 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700434 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800435 ) {
David Howells04c567d2006-06-22 14:47:18 -0700436 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100437 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700438
David Howellsc69e8d92008-11-14 10:39:19 +1100439 if (key_validate(cred->request_key_auth) == 0) {
440 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800441
David Howells04c567d2006-06-22 14:47:18 -0700442 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100443 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800444
David Howellsc69e8d92008-11-14 10:39:19 +1100445 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700446
447 if (!IS_ERR(key_ref))
448 goto found;
449
450 switch (PTR_ERR(key_ref)) {
451 case -EAGAIN: /* no key */
452 if (ret)
453 break;
454 case -ENOKEY: /* negative key */
455 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800456 break;
David Howells04c567d2006-06-22 14:47:18 -0700457 default:
458 err = key_ref;
459 break;
460 }
461 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100462 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800463 }
464 }
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100467 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
David Howells3e301482005-06-23 22:00:56 -0700469found:
David Howells664cceb2005-09-28 17:03:15 +0100470 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472} /* end search_process_keyrings() */
473
474/*****************************************************************************/
475/*
David Howells664cceb2005-09-28 17:03:15 +0100476 * see if the key we're looking at is the target key
477 */
478static int lookup_user_key_possessed(const struct key *key, const void *target)
479{
480 return key == target;
481
482} /* end lookup_user_key_possessed() */
483
484/*****************************************************************************/
485/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 * lookup a key given a key ID from userspace with a given permissions mask
487 * - don't create special keyrings unless so requested
488 * - partially constructed keys aren't found unless requested
489 */
David Howells8bbf49762008-11-14 10:39:14 +1100490key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
491 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
David Howells8bbf49762008-11-14 10:39:14 +1100493 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100494 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100496 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int ret;
498
David Howellsbb952bb2008-11-14 10:39:20 +1100499try_again:
500 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100501 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 switch (id) {
504 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100505 if (!cred->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (!create)
507 goto error;
508
David Howells8bbf49762008-11-14 10:39:14 +1100509 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (ret < 0) {
511 key = ERR_PTR(ret);
512 goto error;
513 }
David Howellsbb952bb2008-11-14 10:39:20 +1100514 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
David Howellsb6dff3e2008-11-14 10:39:16 +1100517 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100519 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 break;
521
522 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100523 if (!cred->tgcred->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!create)
525 goto error;
526
David Howells8bbf49762008-11-14 10:39:14 +1100527 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (ret < 0) {
529 key = ERR_PTR(ret);
530 goto error;
531 }
David Howellsbb952bb2008-11-14 10:39:20 +1100532 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
David Howellsbb952bb2008-11-14 10:39:20 +1100535 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100537 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 break;
539
540 case KEY_SPEC_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100541 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /* always install a session keyring upon access if one
543 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100544 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700545 if (ret < 0)
546 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100547 ret = install_session_keyring(
548 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ret < 0)
551 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100552 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554
David Howells3e301482005-06-23 22:00:56 -0700555 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100556 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700558 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100559 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 break;
561
562 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100563 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100564 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700565 if (ret < 0)
566 goto error;
567 }
568
David Howellsb6dff3e2008-11-14 10:39:16 +1100569 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100571 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573
574 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100575 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100576 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700577 if (ret < 0)
578 goto error;
579 }
580
David Howellsb6dff3e2008-11-14 10:39:16 +1100581 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100583 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 break;
585
586 case KEY_SPEC_GROUP_KEYRING:
587 /* group keyrings are not yet supported */
588 key = ERR_PTR(-EINVAL);
589 goto error;
590
David Howellsb5f545c2006-01-08 01:02:47 -0800591 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100592 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800593 if (!key)
594 goto error;
595
596 atomic_inc(&key->usage);
597 key_ref = make_key_ref(key, 1);
598 break;
599
David Howells8bbf49762008-11-14 10:39:14 +1100600 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100601 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100602 goto error;
603
David Howellsb6dff3e2008-11-14 10:39:16 +1100604 down_read(&cred->request_key_auth->sem);
605 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100606 key_ref = ERR_PTR(-EKEYREVOKED);
607 key = NULL;
608 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100609 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100610 key = rka->dest_keyring;
611 atomic_inc(&key->usage);
612 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100613 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100614 if (!key)
615 goto error;
616 key_ref = make_key_ref(key, 1);
617 break;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 default:
David Howells664cceb2005-09-28 17:03:15 +0100620 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (id < 1)
622 goto error;
623
624 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100625 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800626 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100628 }
629
630 key_ref = make_key_ref(key, 0);
631
632 /* check to see if we possess the key */
633 skey_ref = search_process_keyrings(key->type, key,
634 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100635 cred);
David Howells664cceb2005-09-28 17:03:15 +0100636
637 if (!IS_ERR(skey_ref)) {
638 key_put(key);
639 key_ref = skey_ref;
640 }
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643 }
644
David Howells76181c12007-10-16 23:29:46 -0700645 if (!partial) {
646 ret = wait_for_key_construction(key, true);
647 switch (ret) {
648 case -ERESTARTSYS:
649 goto invalid_key;
650 default:
651 if (perm)
652 goto invalid_key;
653 case 0:
654 break;
655 }
656 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ret = key_validate(key);
658 if (ret < 0)
659 goto invalid_key;
660 }
661
662 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700663 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto invalid_key;
665
David Howells3e301482005-06-23 22:00:56 -0700666 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100667 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800668 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto invalid_key;
670
David Howells664cceb2005-09-28 17:03:15 +0100671error:
David Howellsbb952bb2008-11-14 10:39:20 +1100672 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100673 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
David Howells664cceb2005-09-28 17:03:15 +0100675invalid_key:
676 key_ref_put(key_ref);
677 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto error;
679
David Howellsbb952bb2008-11-14 10:39:20 +1100680 /* if we attempted to install a keyring, then it may have caused new
681 * creds to be installed */
682reget_creds:
683 put_cred(cred);
684 goto try_again;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686} /* end lookup_user_key() */
687
688/*****************************************************************************/
689/*
690 * join the named keyring as the session keyring if possible, or attempt to
691 * create a new one of that name if not
692 * - if the name is NULL, an empty anonymous keyring is installed instead
693 * - named session keyring joining is done with a semaphore held
694 */
695long join_session_keyring(const char *name)
696{
David Howellsd84f4f92008-11-14 10:39:23 +1100697 const struct cred *old;
698 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100700 long ret, serial;
701
702 /* only permit this if there's a single thread in the thread group -
703 * this avoids us having to adjust the creds on all threads and risking
704 * ENOMEM */
705 if (!is_single_threaded(current))
706 return -EMLINK;
707
708 new = prepare_creds();
709 if (!new)
710 return -ENOMEM;
711 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713 /* if no name is provided, install an anonymous keyring */
714 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100715 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (ret < 0)
717 goto error;
718
David Howellsd84f4f92008-11-14 10:39:23 +1100719 serial = new->tgcred->session_keyring->serial;
720 ret = commit_creds(new);
721 if (ret == 0)
722 ret = serial;
723 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800727 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700730 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (PTR_ERR(keyring) == -ENOKEY) {
732 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100733 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700734 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (IS_ERR(keyring)) {
736 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700737 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
David Howellsd84f4f92008-11-14 10:39:23 +1100739 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ret = PTR_ERR(keyring);
741 goto error2;
742 }
743
744 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100745 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (ret < 0)
747 goto error2;
748
David Howellsd84f4f92008-11-14 10:39:23 +1100749 commit_creds(new);
750 mutex_unlock(&key_session_mutex);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ret = keyring->serial;
753 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100754okay:
755 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
David Howells664cceb2005-09-28 17:03:15 +0100757error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800758 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100759error:
David Howellsd84f4f92008-11-14 10:39:23 +1100760 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100762}