blob: 6b8e4ff4cc68cfebe2bb845dda44d8d4b8542635 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/keyctl.h>
16#include <linux/fs.h>
17#include <linux/err.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080018#include <linux/mutex.h>
David Howellsee18d642009-09-02 09:14:21 +010019#include <linux/security.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 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700219int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
David Howells7e047ef2006-06-26 00:24:50 -0700221 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700223
224 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* create an empty session keyring */
227 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700228 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howellsd84f4f92008-11-14 10:39:23 +1100229 if (cred->tgcred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700230 flags = KEY_ALLOC_IN_QUOTA;
231
David Howellsd84f4f92008-11-14 10:39:23 +1100232 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
233 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700234 if (IS_ERR(keyring))
235 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100236 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 atomic_inc(&keyring->usage);
238 }
239
240 /* install the keyring */
David Howellsd84f4f92008-11-14 10:39:23 +1100241 spin_lock_irq(&cred->tgcred->lock);
242 old = cred->tgcred->session_keyring;
243 rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
244 spin_unlock_irq(&cred->tgcred->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
David Howells1a26feb2006-04-10 22:54:26 -0700246 /* we're using RCU on the pointer, but there's no point synchronising
247 * on it if it didn't previously point to anything */
248 if (old) {
249 synchronize_rcu();
250 key_put(old);
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Howells1a26feb2006-04-10 22:54:26 -0700253 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100254}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256/*
David Howellsd84f4f92008-11-14 10:39:23 +1100257 * install a session keyring, discarding the old one
258 * - if a keyring is not supplied, an empty one is invented
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 */
David Howellsd84f4f92008-11-14 10:39:23 +1100260static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howellsd84f4f92008-11-14 10:39:23 +1100262 struct cred *new;
263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
David Howellsd84f4f92008-11-14 10:39:23 +1100265 new = prepare_creds();
266 if (!new)
267 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800268
David Howellsd84f4f92008-11-14 10:39:23 +1100269 ret = install_session_keyring_to_cred(new, NULL);
270 if (ret < 0) {
271 abort_creds(new);
272 return ret;
273 }
David Howellsb5f545c2006-01-08 01:02:47 -0800274
David Howellsd84f4f92008-11-14 10:39:23 +1100275 return commit_creds(new);
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278/*****************************************************************************/
279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * the filesystem user ID changed
281 */
282void key_fsuid_changed(struct task_struct *tsk)
283{
284 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100285 BUG_ON(!tsk->cred);
286 if (tsk->cred->thread_keyring) {
287 down_write(&tsk->cred->thread_keyring->sem);
288 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
289 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
292} /* end key_fsuid_changed() */
293
294/*****************************************************************************/
295/*
296 * the filesystem group ID changed
297 */
298void key_fsgid_changed(struct task_struct *tsk)
299{
300 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100301 BUG_ON(!tsk->cred);
302 if (tsk->cred->thread_keyring) {
303 down_write(&tsk->cred->thread_keyring->sem);
304 tsk->cred->thread_keyring->gid = tsk->cred->fsgid;
305 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308} /* end key_fsgid_changed() */
309
310/*****************************************************************************/
311/*
312 * search the process keyrings for the first matching key
313 * - we use the supplied match function to see if the description (or other
314 * feature of interest) matches
315 * - we return -EAGAIN if we didn't find any matching key
316 * - we return -ENOKEY if we found only negative matching keys
317 */
David Howells664cceb2005-09-28 17:03:15 +0100318key_ref_t search_process_keyrings(struct key_type *type,
319 const void *description,
320 key_match_func_t match,
David Howellsd84f4f92008-11-14 10:39:23 +1100321 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
David Howells3e301482005-06-23 22:00:56 -0700323 struct request_key_auth *rka;
David Howellsb5f545c2006-01-08 01:02:47 -0800324 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
David Howells04c567d2006-06-22 14:47:18 -0700326 might_sleep();
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
329 * searchable, but we failed to find a key or we found a negative key;
330 * otherwise we want to return a sample error (probably -EACCES) if
331 * none of the keyrings were searchable
332 *
333 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
334 */
David Howells664cceb2005-09-28 17:03:15 +0100335 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 ret = NULL;
337 err = ERR_PTR(-EAGAIN);
338
339 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100340 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100341 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100342 make_key_ref(cred->thread_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100343 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100344 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto found;
346
David Howells664cceb2005-09-28 17:03:15 +0100347 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 case -EAGAIN: /* no key */
349 if (ret)
350 break;
351 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100352 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 default:
David Howells664cceb2005-09-28 17:03:15 +0100355 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 break;
357 }
358 }
359
360 /* search the process keyring second */
David Howellsbb952bb2008-11-14 10:39:20 +1100361 if (cred->tgcred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100362 key_ref = keyring_search_aux(
David Howellsbb952bb2008-11-14 10:39:20 +1100363 make_key_ref(cred->tgcred->process_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100364 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100365 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto found;
367
David Howells664cceb2005-09-28 17:03:15 +0100368 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case -EAGAIN: /* no key */
370 if (ret)
371 break;
372 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100373 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 break;
375 default:
David Howells664cceb2005-09-28 17:03:15 +0100376 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 }
379 }
380
David Howells3e301482005-06-23 22:00:56 -0700381 /* search the session keyring */
David Howellsbb952bb2008-11-14 10:39:20 +1100382 if (cred->tgcred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700383 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100384 key_ref = keyring_search_aux(
385 make_key_ref(rcu_dereference(
David Howellsbb952bb2008-11-14 10:39:20 +1100386 cred->tgcred->session_keyring),
David Howells664cceb2005-09-28 17:03:15 +0100387 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100388 cred, type, description, match);
David Howells8589b4e2005-06-23 22:00:53 -0700389 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
David Howells664cceb2005-09-28 17:03:15 +0100391 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700392 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
David Howells664cceb2005-09-28 17:03:15 +0100394 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700395 case -EAGAIN: /* no key */
396 if (ret)
397 break;
398 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100399 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 break;
David Howells3e301482005-06-23 22:00:56 -0700401 default:
David Howells664cceb2005-09-28 17:03:15 +0100402 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700403 break;
404 }
David Howells3e301482005-06-23 22:00:56 -0700405 }
406 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100407 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100408 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100409 make_key_ref(cred->user->session_keyring, 1),
David Howellsd84f4f92008-11-14 10:39:23 +1100410 cred, type, description, match);
David Howells664cceb2005-09-28 17:03:15 +0100411 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700412 goto found;
413
David Howells664cceb2005-09-28 17:03:15 +0100414 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700415 case -EAGAIN: /* no key */
416 if (ret)
417 break;
418 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100419 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700420 break;
421 default:
David Howells664cceb2005-09-28 17:03:15 +0100422 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700423 break;
424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
David Howellsb5f545c2006-01-08 01:02:47 -0800427 /* if this process has an instantiation authorisation key, then we also
428 * search the keyrings of the process mentioned there
429 * - we don't permit access to request_key auth keys via this method
430 */
David Howellsc69e8d92008-11-14 10:39:19 +1100431 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100432 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700433 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800434 ) {
David Howells04c567d2006-06-22 14:47:18 -0700435 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100436 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700437
David Howellsc69e8d92008-11-14 10:39:19 +1100438 if (key_validate(cred->request_key_auth) == 0) {
439 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800440
David Howells04c567d2006-06-22 14:47:18 -0700441 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100442 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800443
David Howellsc69e8d92008-11-14 10:39:19 +1100444 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700445
446 if (!IS_ERR(key_ref))
447 goto found;
448
449 switch (PTR_ERR(key_ref)) {
450 case -EAGAIN: /* no key */
451 if (ret)
452 break;
453 case -ENOKEY: /* negative key */
454 ret = key_ref;
David Howellsb5f545c2006-01-08 01:02:47 -0800455 break;
David Howells04c567d2006-06-22 14:47:18 -0700456 default:
457 err = key_ref;
458 break;
459 }
460 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100461 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800462 }
463 }
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /* no key - decide on the error we're going to go for */
David Howells664cceb2005-09-28 17:03:15 +0100466 key_ref = ret ? ret : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
David Howells3e301482005-06-23 22:00:56 -0700468found:
David Howells664cceb2005-09-28 17:03:15 +0100469 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471} /* end search_process_keyrings() */
472
473/*****************************************************************************/
474/*
David Howells664cceb2005-09-28 17:03:15 +0100475 * see if the key we're looking at is the target key
476 */
477static int lookup_user_key_possessed(const struct key *key, const void *target)
478{
479 return key == target;
480
481} /* end lookup_user_key_possessed() */
482
483/*****************************************************************************/
484/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 * lookup a key given a key ID from userspace with a given permissions mask
486 * - don't create special keyrings unless so requested
487 * - partially constructed keys aren't found unless requested
488 */
David Howells55931222009-09-02 09:13:45 +0100489key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100490 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
David Howells8bbf49762008-11-14 10:39:14 +1100492 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100493 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100495 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int ret;
497
David Howellsbb952bb2008-11-14 10:39:20 +1100498try_again:
499 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100500 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 switch (id) {
503 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100504 if (!cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100505 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto error;
507
David Howells8bbf49762008-11-14 10:39:14 +1100508 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100510 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 goto error;
512 }
David Howellsbb952bb2008-11-14 10:39:20 +1100513 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515
David Howellsb6dff3e2008-11-14 10:39:16 +1100516 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100518 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 break;
520
521 case KEY_SPEC_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100522 if (!cred->tgcred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100523 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto error;
525
David Howells8bbf49762008-11-14 10:39:14 +1100526 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100528 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto error;
530 }
David Howellsbb952bb2008-11-14 10:39:20 +1100531 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
David Howellsbb952bb2008-11-14 10:39:20 +1100534 key = cred->tgcred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100536 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538
539 case KEY_SPEC_SESSION_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100540 if (!cred->tgcred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 /* always install a session keyring upon access if one
542 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100543 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700544 if (ret < 0)
545 goto error;
David Howellsb6dff3e2008-11-14 10:39:16 +1100546 ret = install_session_keyring(
547 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (ret < 0)
550 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100551 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553
David Howells3e301482005-06-23 22:00:56 -0700554 rcu_read_lock();
David Howellsbb952bb2008-11-14 10:39:20 +1100555 key = rcu_dereference(cred->tgcred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700557 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100558 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560
561 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100562 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100563 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700564 if (ret < 0)
565 goto error;
566 }
567
David Howellsb6dff3e2008-11-14 10:39:16 +1100568 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100570 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572
573 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100574 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100575 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700576 if (ret < 0)
577 goto error;
578 }
579
David Howellsb6dff3e2008-11-14 10:39:16 +1100580 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100582 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 break;
584
585 case KEY_SPEC_GROUP_KEYRING:
586 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100587 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 goto error;
589
David Howellsb5f545c2006-01-08 01:02:47 -0800590 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100591 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800592 if (!key)
593 goto error;
594
595 atomic_inc(&key->usage);
596 key_ref = make_key_ref(key, 1);
597 break;
598
David Howells8bbf49762008-11-14 10:39:14 +1100599 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100600 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100601 goto error;
602
David Howellsb6dff3e2008-11-14 10:39:16 +1100603 down_read(&cred->request_key_auth->sem);
604 if (cred->request_key_auth->flags & KEY_FLAG_REVOKED) {
David Howells8bbf49762008-11-14 10:39:14 +1100605 key_ref = ERR_PTR(-EKEYREVOKED);
606 key = NULL;
607 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100608 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100609 key = rka->dest_keyring;
610 atomic_inc(&key->usage);
611 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100612 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100613 if (!key)
614 goto error;
615 key_ref = make_key_ref(key, 1);
616 break;
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 default:
David Howells664cceb2005-09-28 17:03:15 +0100619 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 if (id < 1)
621 goto error;
622
623 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100624 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800625 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100627 }
628
629 key_ref = make_key_ref(key, 0);
630
631 /* check to see if we possess the key */
632 skey_ref = search_process_keyrings(key->type, key,
633 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100634 cred);
David Howells664cceb2005-09-28 17:03:15 +0100635
636 if (!IS_ERR(skey_ref)) {
637 key_put(key);
638 key_ref = skey_ref;
639 }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642 }
643
David Howells55931222009-09-02 09:13:45 +0100644 /* unlink does not use the nominated key in any way, so can skip all
645 * the permission checks as it is only concerned with the keyring */
646 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
647 ret = 0;
648 goto error;
649 }
650
651 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700652 ret = wait_for_key_construction(key, true);
653 switch (ret) {
654 case -ERESTARTSYS:
655 goto invalid_key;
656 default:
657 if (perm)
658 goto invalid_key;
659 case 0:
660 break;
661 }
662 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 ret = key_validate(key);
664 if (ret < 0)
665 goto invalid_key;
666 }
667
668 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100669 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
670 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto invalid_key;
672
David Howells3e301482005-06-23 22:00:56 -0700673 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100674 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800675 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto invalid_key;
677
David Howells664cceb2005-09-28 17:03:15 +0100678error:
David Howellsbb952bb2008-11-14 10:39:20 +1100679 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100680 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
David Howells664cceb2005-09-28 17:03:15 +0100682invalid_key:
683 key_ref_put(key_ref);
684 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto error;
686
David Howellsbb952bb2008-11-14 10:39:20 +1100687 /* if we attempted to install a keyring, then it may have caused new
688 * creds to be installed */
689reget_creds:
690 put_cred(cred);
691 goto try_again;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693} /* end lookup_user_key() */
694
695/*****************************************************************************/
696/*
697 * join the named keyring as the session keyring if possible, or attempt to
698 * create a new one of that name if not
699 * - if the name is NULL, an empty anonymous keyring is installed instead
700 * - named session keyring joining is done with a semaphore held
701 */
702long join_session_keyring(const char *name)
703{
David Howellsd84f4f92008-11-14 10:39:23 +1100704 const struct cred *old;
705 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100707 long ret, serial;
708
709 /* only permit this if there's a single thread in the thread group -
710 * this avoids us having to adjust the creds on all threads and risking
711 * ENOMEM */
Oleg Nesterov5bb459b2009-07-10 03:48:23 +0200712 if (!current_is_single_threaded())
David Howellsd84f4f92008-11-14 10:39:23 +1100713 return -EMLINK;
714
715 new = prepare_creds();
716 if (!new)
717 return -ENOMEM;
718 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* if no name is provided, install an anonymous keyring */
721 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100722 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (ret < 0)
724 goto error;
725
David Howellsd84f4f92008-11-14 10:39:23 +1100726 serial = new->tgcred->session_keyring->serial;
727 ret = commit_creds(new);
728 if (ret == 0)
729 ret = serial;
730 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 /* allow the user to join or create a named keyring */
Ingo Molnarbb003072006-03-22 00:09:14 -0800734 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700737 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (PTR_ERR(keyring) == -ENOKEY) {
739 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100740 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700741 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (IS_ERR(keyring)) {
743 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700744 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
David Howellsd84f4f92008-11-14 10:39:23 +1100746 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ret = PTR_ERR(keyring);
748 goto error2;
749 }
750
751 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100752 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (ret < 0)
754 goto error2;
755
David Howellsd84f4f92008-11-14 10:39:23 +1100756 commit_creds(new);
757 mutex_unlock(&key_session_mutex);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ret = keyring->serial;
760 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100761okay:
762 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
David Howells664cceb2005-09-28 17:03:15 +0100764error2:
Ingo Molnarbb003072006-03-22 00:09:14 -0800765 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100766error:
David Howellsd84f4f92008-11-14 10:39:23 +1100767 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100769}
David Howellsee18d642009-09-02 09:14:21 +0100770
771/*
772 * Replace a process's session keyring when that process resumes userspace on
773 * behalf of one of its children
774 */
775void key_replace_session_keyring(void)
776{
777 const struct cred *old;
778 struct cred *new;
779
780 if (!current->replacement_session_keyring)
781 return;
782
783 write_lock_irq(&tasklist_lock);
784 new = current->replacement_session_keyring;
785 current->replacement_session_keyring = NULL;
786 write_unlock_irq(&tasklist_lock);
787
788 if (!new)
789 return;
790
791 old = current_cred();
792 new-> uid = old-> uid;
793 new-> euid = old-> euid;
794 new-> suid = old-> suid;
795 new->fsuid = old->fsuid;
796 new-> gid = old-> gid;
797 new-> egid = old-> egid;
798 new-> sgid = old-> sgid;
799 new->fsgid = old->fsgid;
800 new->user = get_uid(old->user);
801 new->group_info = get_group_info(old->group_info);
802
803 new->securebits = old->securebits;
804 new->cap_inheritable = old->cap_inheritable;
805 new->cap_permitted = old->cap_permitted;
806 new->cap_effective = old->cap_effective;
807 new->cap_bset = old->cap_bset;
808
809 new->jit_keyring = old->jit_keyring;
810 new->thread_keyring = key_get(old->thread_keyring);
811 new->tgcred->tgid = old->tgcred->tgid;
812 new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
813
814 security_transfer_creds(new, old);
815
816 commit_creds(new);
817}