blob: 2f5d89e92b853a3a4e0ea910aa2e023fc749690a [file] [log] [blame]
David Howells69664cf2008-04-29 01:01:31 -07001/* Management of a process's keyrings
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells69664cf2008-04-29 01:01:31 -07003 * Copyright (C) 2004-2005, 2008 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/sched.h>
15#include <linux/slab.h>
16#include <linux/keyctl.h>
17#include <linux/fs.h>
18#include <linux/err.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
21#include "internal.h"
22
23/* session keyring create vs join semaphore */
Ingo Molnarbb003072006-03-22 00:09:14 -080024static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
David Howells69664cf2008-04-29 01:01:31 -070026/* user keyring creation semaphore */
27static DEFINE_MUTEX(key_user_keyring_mutex);
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/* the root user's tracking struct */
30struct key_user root_key_user = {
31 .usage = ATOMIC_INIT(3),
David Howells76181c12007-10-16 23:29:46 -070032 .cons_lock = __MUTEX_INITIALIZER(root_key_user.cons_lock),
Peter Zijlstra6cfd76a2006-12-06 20:37:22 -080033 .lock = __SPIN_LOCK_UNLOCKED(root_key_user.lock),
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 .nkeys = ATOMIC_INIT(2),
35 .nikeys = ATOMIC_INIT(2),
36 .uid = 0,
37};
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/*****************************************************************************/
40/*
David Howells69664cf2008-04-29 01:01:31 -070041 * install user and user session keyrings for a particular UID
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
David Howells8bbf49762008-11-14 10:39:14 +110043int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
David Howellsd84f4f92008-11-14 10:39:23 +110045 struct user_struct *user;
46 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct key *uid_keyring, *session_keyring;
48 char buf[20];
49 int ret;
50
David Howellsd84f4f92008-11-14 10:39:23 +110051 cred = current_cred();
52 user = cred->user;
53
David Howells69664cf2008-04-29 01:01:31 -070054 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
David Howells69664cf2008-04-29 01:01:31 -070056 if (user->uid_keyring) {
57 kleave(" = 0 [exist]");
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
David Howells69664cf2008-04-29 01:01:31 -070061 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 ret = 0;
63
David Howells69664cf2008-04-29 01:01:31 -070064 if (!user->uid_keyring) {
65 /* get the UID-specific keyring
66 * - there may be one in existence already as it may have been
67 * pinned by a session, but the user_struct pointing to it
68 * may have been destroyed by setuid */
69 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
David Howells69664cf2008-04-29 01:01:31 -070071 uid_keyring = find_keyring_by_name(buf, true);
72 if (IS_ERR(uid_keyring)) {
73 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110074 cred, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070075 NULL);
76 if (IS_ERR(uid_keyring)) {
77 ret = PTR_ERR(uid_keyring);
78 goto error;
79 }
80 }
81
82 /* get a default session keyring (which might also exist
83 * already) */
84 sprintf(buf, "_uid_ses.%u", user->uid);
85
86 session_keyring = find_keyring_by_name(buf, true);
87 if (IS_ERR(session_keyring)) {
88 session_keyring =
89 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110090 cred, KEY_ALLOC_IN_QUOTA, NULL);
David Howells69664cf2008-04-29 01:01:31 -070091 if (IS_ERR(session_keyring)) {
92 ret = PTR_ERR(session_keyring);
93 goto error_release;
94 }
95
96 /* we install a link from the user session keyring to
97 * the user keyring */
98 ret = key_link(session_keyring, uid_keyring);
99 if (ret < 0)
100 goto error_release_both;
101 }
102
103 /* install the keyrings */
104 user->uid_keyring = uid_keyring;
105 user->session_keyring = session_keyring;
106 }
107
108 mutex_unlock(&key_user_keyring_mutex);
109 kleave(" = 0");
110 return 0;
111
112error_release_both:
113 key_put(session_keyring);
114error_release:
115 key_put(uid_keyring);
116error:
117 mutex_unlock(&key_user_keyring_mutex);
118 kleave(" = %d", ret);
119 return ret;
120}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/*
David Howellsd84f4f92008-11-14 10:39:23 +1100123 * install a fresh thread keyring directly to new credentials
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 */
David Howellsd84f4f92008-11-14 10:39:23 +1100125int install_thread_keyring_to_cred(struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
David Howellsd84f4f92008-11-14 10:39:23 +1100127 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
David Howellsd84f4f92008-11-14 10:39:23 +1100129 keyring = keyring_alloc("_tid", new->uid, new->gid, new,
130 KEY_ALLOC_QUOTA_OVERRUN, NULL);
131 if (IS_ERR(keyring))
132 return PTR_ERR(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
David Howellsd84f4f92008-11-14 10:39:23 +1100134 new->thread_keyring = keyring;
135 return 0;
136}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/*
139 * install a fresh thread keyring, discarding the old one
140 */
David Howellsd84f4f92008-11-14 10:39:23 +1100141static int install_thread_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
David Howellsd84f4f92008-11-14 10:39:23 +1100143 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int ret;
145
David Howellsd84f4f92008-11-14 10:39:23 +1100146 new = prepare_creds();
147 if (!new)
148 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
David Howellsd84f4f92008-11-14 10:39:23 +1100150 BUG_ON(new->thread_keyring);
151
152 ret = install_thread_keyring_to_cred(new);
153 if (ret < 0) {
154 abort_creds(new);
155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157
David Howellsd84f4f92008-11-14 10:39:23 +1100158 return commit_creds(new);
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howellsd84f4f92008-11-14 10:39:23 +1100161/*
162 * install a process keyring directly to a credentials struct
163 * - returns -EEXIST if there was already a process keyring, 0 if one installed,
164 * and other -ve on any other error
165 */
166int install_process_keyring_to_cred(struct cred *new)
167{
168 struct key *keyring;
169 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
David Howellsd84f4f92008-11-14 10:39:23 +1100171 if (new->tgcred->process_keyring)
172 return -EEXIST;
173
174 keyring = keyring_alloc("_pid", new->uid, new->gid,
175 new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
176 if (IS_ERR(keyring))
177 return PTR_ERR(keyring);
178
179 spin_lock_irq(&new->tgcred->lock);
180 if (!new->tgcred->process_keyring) {
181 new->tgcred->process_keyring = keyring;
182 keyring = NULL;
183 ret = 0;
184 } else {
185 ret = -EEXIST;
186 }
187 spin_unlock_irq(&new->tgcred->lock);
188 key_put(keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
193 * make sure a process keyring is installed
David Howellsd84f4f92008-11-14 10:39:23 +1100194 * - we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
David Howellsd84f4f92008-11-14 10:39:23 +1100196static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howellsd84f4f92008-11-14 10:39:23 +1100198 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int ret;
200
David Howellsd84f4f92008-11-14 10:39:23 +1100201 new = prepare_creds();
202 if (!new)
203 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700204
David Howellsd84f4f92008-11-14 10:39:23 +1100205 ret = install_process_keyring_to_cred(new);
206 if (ret < 0) {
207 abort_creds(new);
208 return ret != -EEXIST ?: 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211 return commit_creds(new);
212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214/*
David Howellsd84f4f92008-11-14 10:39:23 +1100215 * install a session keyring directly to a credentials struct
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
David Howellsd84f4f92008-11-14 10:39:23 +1100217static int install_session_keyring_to_cred(struct cred *cred,
218 struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
David Howells7e047ef2006-06-26 00:24:50 -0700220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700222
223 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* create an empty session keyring */
226 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700227 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howellsd84f4f92008-11-14 10:39:23 +1100228 if (cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700229 flags = KEY_ALLOC_IN_QUOTA;
230
David Howellsd84f4f92008-11-14 10:39:23 +1100231 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
232 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700233 if (IS_ERR(keyring))
234 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100235 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 atomic_inc(&keyring->usage);
237 }
238
239 /* install the keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100240 spin_lock_irq(&cred->tgcred->lock);
241 old = cred->tgcred->session_keyring;
242 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
243 spin_unlock_irq(&cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
David Howells1a26feb2006-04-10 22:54:26 -0700245 /* we're using RCU on the pointer, but there's no point synchronising
246 * on it if it didn't previously point to anything */
247 if (old) {
248 synchronize_rcu();
249 key_put(old);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
David Howells1a26feb2006-04-10 22:54:26 -0700252 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100253}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*
David Howellsd84f4f92008-11-14 10:39:23 +1100256 * install a session keyring, discarding the old one
257 * - if a keyring is not supplied, an empty one is invented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
David Howellsd84f4f92008-11-14 10:39:23 +1100259static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
David Howellsd84f4f92008-11-14 10:39:23 +1100261 struct cred *new;
262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
David Howellsd84f4f92008-11-14 10:39:23 +1100264 new = prepare_creds();
265 if (!new)
266 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800267
David Howellsd84f4f92008-11-14 10:39:23 +1100268 ret = install_session_keyring_to_cred(new, NULL);
269 if (ret < 0) {
270 abort_creds(new);
271 return ret;
272 }
David Howellsb5f545c2006-01-08 01:02:47 -0800273
David Howellsd84f4f92008-11-14 10:39:23 +1100274 return commit_creds(new);
275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/*****************************************************************************/
278/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * the filesystem user ID changed
280 */
281void key_fsuid_changed(struct task_struct *tsk)
282{
283 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100284 BUG_ON(!tsk->cred);
285 if (tsk->cred->thread_keyring) {
286 down_write(&tsk->cred->thread_keyring->sem);
287 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
288 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291} /* end key_fsuid_changed() */
292
293/*****************************************************************************/
294/*
295 * the filesystem group ID changed
296 */
297void key_fsgid_changed(struct task_struct *tsk)
298{
299 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100300 BUG_ON(!tsk->cred);
301 if (tsk->cred->thread_keyring) {
302 down_write(&tsk->cred->thread_keyring->sem);
303 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
304 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
306
307} /* end key_fsgid_changed() */
308
309/*****************************************************************************/
310/*
311 * search the process keyrings for the first matching key
312 * - we use the supplied match function to see if the description (or other
313 * feature of interest) matches
314 * - we return -EAGAIN if we didn't find any matching key
315 * - we return -ENOKEY if we found only negative matching keys
316 */
David Howells664cceb2005-09-28 17:03:15 +0100317key_ref_t search_process_keyrings(struct key_type *type,
318 const void *description,
319 key_match_func_t match,
David Howellsd84f4f92008-11-14 10:39:23 +1100320 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
David Howells3e301482005-06-23 22:00:56 -0700322 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800323 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David Howells04c567d2006-06-22 14:47:18 -0700325 might_sleep();
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
328 * searchable, but we failed to find a key or we found a negative key;
329 * otherwise we want to return a sample error (probably -EACCES) if
330 * none of the keyrings were searchable
331 *
332 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
333 */
David Howells664cceb2005-09-28 17:03:15 +0100334 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 ret = NULL;
336 err = ERR_PTR(-EAGAIN);
337
338 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100339 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100340 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100341 make_key_ref(cred->thread_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100342 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100343 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 goto found;
345
David Howells664cceb2005-09-28 17:03:15 +0100346 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 case -EAGAIN: /* no key */
348 if (ret)
349 break;
350 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100351 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 default:
David Howells664cceb2005-09-28 17:03:15 +0100354 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 break;
356 }
357 }
358
359 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100360 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100361 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100362 make_key_ref(cred->tgcred->process_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100363 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100364 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto found;
366
David Howells664cceb2005-09-28 17:03:15 +0100367 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 case -EAGAIN: /* no key */
369 if (ret)
370 break;
371 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100372 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 default:
David Howells664cceb2005-09-28 17:03:15 +0100375 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 }
378 }
379
David Howells3e301482005-06-23 22:00:56 -0700380 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100381 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700382 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100383 key_ref = keyring_search_aux(
384 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100385 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100386 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100387 cred, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700388 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
David Howells664cceb2005-09-28 17:03:15 +0100390 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700391 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
David Howells664cceb2005-09-28 17:03:15 +0100393 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700394 case -EAGAIN: /* no key */
395 if (ret)
396 break;
397 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100398 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 break;
David Howells3e301482005-06-23 22:00:56 -0700400 default:
David Howells664cceb2005-09-28 17:03:15 +0100401 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700402 break;
403 }
David Howells3e301482005-06-23 22:00:56 -0700404 }
405 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100406 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100407 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100408 make_key_ref(cred->user->session_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100409 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100410 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700411 goto found;
412
David Howells664cceb2005-09-28 17:03:15 +0100413 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700414 case -EAGAIN: /* no key */
415 if (ret)
416 break;
417 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100418 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700419 break;
420 default:
David Howells664cceb2005-09-28 17:03:15 +0100421 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700422 break;
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
David Howellsb5f545c2006-01-08 01:02:47 -0800426 /* if this process has an instantiation authorisation key, then we also
427 * search the keyrings of the process mentioned there
428 * - we don't permit access to request_key auth keys via this method
429 */
David Howellsc69e8d92008-11-14 10:39:19 +1100430 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100431 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700432 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800433 ) {
David Howells04c567d2006-06-22 14:47:18 -0700434 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100435 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700436
David Howellsc69e8d92008-11-14 10:39:19 +1100437 if (key_validate(cred->request_key_auth) == 0) {
438 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800439
David Howells04c567d2006-06-22 14:47:18 -0700440 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100441 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800442
David Howellsc69e8d92008-11-14 10:39:19 +1100443 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700444
445 if (!IS_ERR(key_ref))
446 goto found;
447
448 switch (PTR_ERR(key_ref)) {
449 case -EAGAIN: /* no key */
450 if (ret)
451 break;
452 case -ENOKEY: /* negative key */
453 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800454 break;
David Howells04c567d2006-06-22 14:47:18 -0700455 default:
456 err = key_ref;
457 break;
458 }
459 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100460 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800461 }
462 }
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100465 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
David Howells3e301482005-06-23 22:00:56 -0700467found:
David Howells664cceb2005-09-28 17:03:15 +0100468 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470} /* end search_process_keyrings() */
471
472/*****************************************************************************/
473/*
David Howells664cceb2005-09-28 17:03:15 +0100474 * see if the key we're looking at is the target key
475 */
476static int lookup_user_key_possessed(const struct key *key, const void *target)
477{
478 return key == target;
479
480} /* end lookup_user_key_possessed() */
481
482/*****************************************************************************/
483/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * lookup a key given a key ID from userspace with a given permissions mask
485 * - don't create special keyrings unless so requested
486 * - partially constructed keys aren't found unless requested
487 */
David Howells8bbf49762008-11-14 10:39:14 +1100488key_ref_t lookup_user_key(key_serial_t id, int create, int partial,
489 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
David Howells8bbf49762008-11-14 10:39:14 +1100491 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100492 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100494 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 int ret;
496
David Howellsbb952bb2008-11-14 10:39:20 +1100497try_again:
498 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100499 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 switch (id) {
502 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100503 if (!cred->thread_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!create)
505 goto error;
506
David Howells8bbf49762008-11-14 10:39:14 +1100507 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (ret < 0) {
509 key = ERR_PTR(ret);
510 goto error;
511 }
David Howellsbb952bb2008-11-14 10:39:20 +1100512 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
David Howellsb6dff3e2008-11-14 10:39:16 +1100515 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100517 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519
520 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100521 if (!cred->tgcred->process_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!create)
523 goto error;
524
David Howells8bbf49762008-11-14 10:39:14 +1100525 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (ret < 0) {
527 key = ERR_PTR(ret);
528 goto error;
529 }
David Howellsbb952bb2008-11-14 10:39:20 +1100530 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
David Howellsbb952bb2008-11-14 10:39:20 +1100533 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100535 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 break;
537
538 case KEY_SPEC_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100539 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* always install a session keyring upon access if one
541 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100542 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700543 if (ret < 0)
544 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100545 ret = install_session_keyring(
546 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (ret < 0)
549 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100550 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
David Howells3e301482005-06-23 22:00:56 -0700553 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100554 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700556 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100557 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
559
560 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100561 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100562 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700563 if (ret < 0)
564 goto error;
565 }
566
David Howellsb6dff3e2008-11-14 10:39:16 +1100567 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100569 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 break;
571
572 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100573 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100574 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700575 if (ret < 0)
576 goto error;
577 }
578
David Howellsb6dff3e2008-11-14 10:39:16 +1100579 key = cred->user->session_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_GROUP_KEYRING:
585 /* group keyrings are not yet supported */
586 key = ERR_PTR(-EINVAL);
587 goto error;
588
David Howellsb5f545c2006-01-08 01:02:47 -0800589 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100590 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800591 if (!key)
592 goto error;
593
594 atomic_inc(&key->usage);
595 key_ref = make_key_ref(key, 1);
596 break;
597
David Howells8bbf49762008-11-14 10:39:14 +1100598 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100599 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100600 goto error;
601
David Howellsb6dff3e2008-11-14 10:39:16 +1100602 down_read(&cred->request_key_auth->sem);
603 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100604 key_ref = ERR_PTR(-EKEYREVOKED);
605 key = NULL;
606 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100607 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100608 key = rka->dest_keyring;
609 atomic_inc(&key->usage);
610 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100611 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100612 if (!key)
613 goto error;
614 key_ref = make_key_ref(key, 1);
615 break;
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 default:
David Howells664cceb2005-09-28 17:03:15 +0100618 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (id < 1)
620 goto error;
621
622 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100623 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800624 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100626 }
627
628 key_ref = make_key_ref(key, 0);
629
630 /* check to see if we possess the key */
631 skey_ref = search_process_keyrings(key->type, key,
632 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100633 cred);
David Howells664cceb2005-09-28 17:03:15 +0100634
635 if (!IS_ERR(skey_ref)) {
636 key_put(key);
637 key_ref = skey_ref;
638 }
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 break;
641 }
642
David Howells76181c12007-10-16 23:29:46 -0700643 if (!partial) {
644 ret = wait_for_key_construction(key, true);
645 switch (ret) {
646 case -ERESTARTSYS:
647 goto invalid_key;
648 default:
649 if (perm)
650 goto invalid_key;
651 case 0:
652 break;
653 }
654 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ret = key_validate(key);
656 if (ret < 0)
657 goto invalid_key;
658 }
659
660 ret = -EIO;
David Howells76d8aea2005-06-23 22:00:49 -0700661 if (!partial && !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto invalid_key;
663
David Howells3e301482005-06-23 22:00:56 -0700664 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100665 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800666 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto invalid_key;
668
David Howells664cceb2005-09-28 17:03:15 +0100669error:
David Howellsbb952bb2008-11-14 10:39:20 +1100670 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100671 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
David Howells664cceb2005-09-28 17:03:15 +0100673invalid_key:
674 key_ref_put(key_ref);
675 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto error;
677
David Howellsbb952bb2008-11-14 10:39:20 +1100678 /* if we attempted to install a keyring, then it may have caused new
679 * creds to be installed */
680reget_creds:
681 put_cred(cred);
682 goto try_again;
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684} /* end lookup_user_key() */
685
686/*****************************************************************************/
687/*
688 * join the named keyring as the session keyring if possible, or attempt to
689 * create a new one of that name if not
690 * - if the name is NULL, an empty anonymous keyring is installed instead
691 * - named session keyring joining is done with a semaphore held
692 */
693long join_session_keyring(const char *name)
694{
David Howellsd84f4f92008-11-14 10:39:23 +1100695 const struct cred *old;
696 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100698 long ret, serial;
699
700 /* only permit this if there's a single thread in the thread group -
701 * this avoids us having to adjust the creds on all threads and risking
702 * ENOMEM */
703 if (!is_single_threaded(current))
704 return -EMLINK;
705
706 new = prepare_creds();
707 if (!new)
708 return -ENOMEM;
709 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 /* if no name is provided, install an anonymous keyring */
712 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100713 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (ret < 0)
715 goto error;
716
David Howellsd84f4f92008-11-14 10:39:23 +1100717 serial = new->tgcred->session_keyring->serial;
718 ret = commit_creds(new);
719 if (ret == 0)
720 ret = serial;
721 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723
724 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800725 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700728 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (PTR_ERR(keyring) == -ENOKEY) {
730 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100731 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700732 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (IS_ERR(keyring)) {
734 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700735 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 }
David Howellsd84f4f92008-11-14 10:39:23 +1100737 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ret = PTR_ERR(keyring);
739 goto error2;
740 }
741
742 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100743 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (ret < 0)
745 goto error2;
746
David Howellsd84f4f92008-11-14 10:39:23 +1100747 commit_creds(new);
748 mutex_unlock(&key_session_mutex);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ret = keyring->serial;
751 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100752okay:
753 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
David Howells664cceb2005-09-28 17:03:15 +0100755error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800756 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100757error:
David Howellsd84f4f92008-11-14 10:39:23 +1100758 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100760}