blob: 9de5dc59827645577eaa9bab06cd1fbeb7bc8bde [file] [log] [blame]
David Howells973c9f42011-01-20 16:38:33 +00001/* Manage 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 Molnarbb0030792006-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
David Howells973c9f42011-01-20 16:38:33 +000024/* Session keyring create vs join semaphore */
Ingo Molnarbb0030792006-03-22 00:09:14 -080025static DEFINE_MUTEX(key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
David Howells973c9f42011-01-20 16:38:33 +000027/* User keyring creation semaphore */
David Howells69664cf2008-04-29 01:01:31 -070028static DEFINE_MUTEX(key_user_keyring_mutex);
29
David Howells973c9f42011-01-20 16:38:33 +000030/* The root user's tracking struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031struct 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/*
David Howells973c9f42011-01-20 16:38:33 +000042 * Install the user and user session keyrings for the current process's UID.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
David Howells8bbf49762008-11-14 10:39:14 +110044int install_user_keyrings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
David Howellsd84f4f92008-11-14 10:39:23 +110046 struct user_struct *user;
47 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct key *uid_keyring, *session_keyring;
49 char buf[20];
50 int ret;
51
David Howellsd84f4f92008-11-14 10:39:23 +110052 cred = current_cred();
53 user = cred->user;
54
David Howells69664cf2008-04-29 01:01:31 -070055 kenter("%p{%u}", user, user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David Howells69664cf2008-04-29 01:01:31 -070057 if (user->uid_keyring) {
58 kleave(" = 0 [exist]");
59 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 }
61
David Howells69664cf2008-04-29 01:01:31 -070062 mutex_lock(&key_user_keyring_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 ret = 0;
64
David Howells69664cf2008-04-29 01:01:31 -070065 if (!user->uid_keyring) {
66 /* get the UID-specific keyring
67 * - there may be one in existence already as it may have been
68 * pinned by a session, but the user_struct pointing to it
69 * may have been destroyed by setuid */
70 sprintf(buf, "_uid.%u", user->uid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
David Howells69664cf2008-04-29 01:01:31 -070072 uid_keyring = find_keyring_by_name(buf, true);
73 if (IS_ERR(uid_keyring)) {
74 uid_keyring = keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110075 cred, KEY_ALLOC_IN_QUOTA,
David Howells69664cf2008-04-29 01:01:31 -070076 NULL);
77 if (IS_ERR(uid_keyring)) {
78 ret = PTR_ERR(uid_keyring);
79 goto error;
80 }
81 }
82
83 /* get a default session keyring (which might also exist
84 * already) */
85 sprintf(buf, "_uid_ses.%u", user->uid);
86
87 session_keyring = find_keyring_by_name(buf, true);
88 if (IS_ERR(session_keyring)) {
89 session_keyring =
90 keyring_alloc(buf, user->uid, (gid_t) -1,
David Howellsd84f4f92008-11-14 10:39:23 +110091 cred, KEY_ALLOC_IN_QUOTA, NULL);
David Howells69664cf2008-04-29 01:01:31 -070092 if (IS_ERR(session_keyring)) {
93 ret = PTR_ERR(session_keyring);
94 goto error_release;
95 }
96
97 /* we install a link from the user session keyring to
98 * the user keyring */
99 ret = key_link(session_keyring, uid_keyring);
100 if (ret < 0)
101 goto error_release_both;
102 }
103
104 /* install the keyrings */
105 user->uid_keyring = uid_keyring;
106 user->session_keyring = session_keyring;
107 }
108
109 mutex_unlock(&key_user_keyring_mutex);
110 kleave(" = 0");
111 return 0;
112
113error_release_both:
114 key_put(session_keyring);
115error_release:
116 key_put(uid_keyring);
117error:
118 mutex_unlock(&key_user_keyring_mutex);
119 kleave(" = %d", ret);
120 return ret;
121}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
David Howells973c9f42011-01-20 16:38:33 +0000124 * Install a fresh thread keyring directly to new credentials. This keyring is
125 * allowed to overrun the quota.
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/*
David Howells973c9f42011-01-20 16:38:33 +0000141 * Install a fresh thread keyring, discarding the old one.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
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/*
David Howells973c9f42011-01-20 16:38:33 +0000164 * Install a process keyring directly to a credentials struct.
165 *
166 * Returns -EEXIST if there was already a process keyring, 0 if one installed,
167 * and other value on any other error
David Howellsd84f4f92008-11-14 10:39:23 +1100168 */
169int install_process_keyring_to_cred(struct cred *new)
170{
171 struct key *keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
David Howells3a505972012-10-02 19:24:29 +0100173 if (new->process_keyring)
David Howellsd84f4f92008-11-14 10:39:23 +1100174 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
David Howells3a505972012-10-02 19:24:29 +0100181 new->process_keyring = keyring;
182 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100183}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
David Howells973c9f42011-01-20 16:38:33 +0000186 * Make sure a process keyring is installed for the current process. The
187 * existing process keyring is not replaced.
188 *
189 * Returns 0 if there is a process keyring by the end of this function, some
190 * error otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
David Howellsd84f4f92008-11-14 10:39:23 +1100192static int install_process_keyring(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
David Howellsd84f4f92008-11-14 10:39:23 +1100194 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int ret;
196
David Howellsd84f4f92008-11-14 10:39:23 +1100197 new = prepare_creds();
198 if (!new)
199 return -ENOMEM;
David Howells1a26feb2006-04-10 22:54:26 -0700200
David Howellsd84f4f92008-11-14 10:39:23 +1100201 ret = install_process_keyring_to_cred(new);
202 if (ret < 0) {
203 abort_creds(new);
Andi Kleen27d63792010-10-28 13:16:13 +0100204 return ret != -EEXIST ? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
David Howellsd84f4f92008-11-14 10:39:23 +1100207 return commit_creds(new);
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
David Howells973c9f42011-01-20 16:38:33 +0000211 * Install a session keyring directly to a credentials struct.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -0700213int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
David Howells7e047ef2006-06-26 00:24:50 -0700215 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 struct key *old;
David Howells1a26feb2006-04-10 22:54:26 -0700217
218 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* create an empty session keyring */
221 if (!keyring) {
David Howells7e047ef2006-06-26 00:24:50 -0700222 flags = KEY_ALLOC_QUOTA_OVERRUN;
David Howells3a505972012-10-02 19:24:29 +0100223 if (cred->session_keyring)
David Howells7e047ef2006-06-26 00:24:50 -0700224 flags = KEY_ALLOC_IN_QUOTA;
225
David Howellsd84f4f92008-11-14 10:39:23 +1100226 keyring = keyring_alloc("_ses", cred->uid, cred->gid,
227 cred, flags, NULL);
David Howells1a26feb2006-04-10 22:54:26 -0700228 if (IS_ERR(keyring))
229 return PTR_ERR(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100230 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 atomic_inc(&keyring->usage);
232 }
233
234 /* install the keyring */
David Howells3a505972012-10-02 19:24:29 +0100235 old = cred->session_keyring;
236 rcu_assign_pointer(cred->session_keyring, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
David Howells3a505972012-10-02 19:24:29 +0100238 if (old)
David Howells1a26feb2006-04-10 22:54:26 -0700239 key_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
David Howells1a26feb2006-04-10 22:54:26 -0700241 return 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
David Howells973c9f42011-01-20 16:38:33 +0000245 * Install a session keyring, discarding the old one. If a keyring is not
246 * supplied, an empty one is invented.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 */
David Howellsd84f4f92008-11-14 10:39:23 +1100248static int install_session_keyring(struct key *keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
David Howellsd84f4f92008-11-14 10:39:23 +1100250 struct cred *new;
251 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
David Howellsd84f4f92008-11-14 10:39:23 +1100253 new = prepare_creds();
254 if (!new)
255 return -ENOMEM;
David Howellsb5f545c2006-01-08 01:02:47 -0800256
David Howells995995372011-08-22 14:08:33 +0100257 ret = install_session_keyring_to_cred(new, keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100258 if (ret < 0) {
259 abort_creds(new);
260 return ret;
261 }
David Howellsb5f545c2006-01-08 01:02:47 -0800262
David Howellsd84f4f92008-11-14 10:39:23 +1100263 return commit_creds(new);
264}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*
David Howells973c9f42011-01-20 16:38:33 +0000267 * Handle the fsuid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
269void key_fsuid_changed(struct task_struct *tsk)
270{
271 /* update the ownership of the thread keyring */
David Howellsb6dff3e2008-11-14 10:39:16 +1100272 BUG_ON(!tsk->cred);
273 if (tsk->cred->thread_keyring) {
274 down_write(&tsk->cred->thread_keyring->sem);
275 tsk->cred->thread_keyring->uid = tsk->cred->fsuid;
276 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000278}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
David Howells973c9f42011-01-20 16:38:33 +0000281 * Handle the fsgid changing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283void key_fsgid_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->gid = tsk->cred->fsgid;
290 up_write(&tsk->cred->thread_keyring->sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
David Howells973c9f42011-01-20 16:38:33 +0000295 * Search the process keyrings attached to the supplied cred for the first
296 * matching key.
297 *
298 * The search criteria are the type and the match function. The description is
299 * given to the match function as a parameter, but doesn't otherwise influence
300 * the search. Typically the match function will compare the description
301 * parameter to the key's description.
302 *
303 * This can only search keyrings that grant Search permission to the supplied
304 * credentials. Keyrings linked to searched keyrings will also be searched if
305 * they grant Search permission too. Keys can only be found if they grant
306 * Search permission to the credentials.
307 *
308 * Returns a pointer to the key with the key usage count incremented if
309 * successful, -EAGAIN if we didn't find any matching key or -ENOKEY if we only
310 * matched negative keys.
311 *
312 * In the case of a successful return, the possession attribute is set on the
313 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 */
David Howells927942a2010-06-11 17:31:10 +0100315key_ref_t search_my_process_keyrings(struct key_type *type,
316 const void *description,
317 key_match_func_t match,
David Howells78b72802011-03-11 17:57:23 +0000318 bool no_state_check,
David Howells927942a2010-06-11 17:31:10 +0100319 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
David Howellsb5f545c2006-01-08 01:02:47 -0800321 key_ref_t key_ref, ret, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* we want to return -EAGAIN or -ENOKEY if any of the keyrings were
324 * searchable, but we failed to find a key or we found a negative key;
325 * otherwise we want to return a sample error (probably -EACCES) if
326 * none of the keyrings were searchable
327 *
328 * in terms of priority: success > -ENOKEY > -EAGAIN > other error
329 */
David Howells664cceb2005-09-28 17:03:15 +0100330 key_ref = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ret = NULL;
332 err = ERR_PTR(-EAGAIN);
333
334 /* search the thread keyring first */
David Howellsc69e8d92008-11-14 10:39:19 +1100335 if (cred->thread_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100336 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100337 make_key_ref(cred->thread_keyring, 1),
David Howells78b72802011-03-11 17:57:23 +0000338 cred, type, description, match, no_state_check);
David Howells664cceb2005-09-28 17:03:15 +0100339 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto found;
341
David Howells664cceb2005-09-28 17:03:15 +0100342 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 case -EAGAIN: /* no key */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100345 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347 default:
David Howells664cceb2005-09-28 17:03:15 +0100348 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 break;
350 }
351 }
352
353 /* search the process keyring second */
David Howells3a505972012-10-02 19:24:29 +0100354 if (cred->process_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100355 key_ref = keyring_search_aux(
David Howells3a505972012-10-02 19:24:29 +0100356 make_key_ref(cred->process_keyring, 1),
David Howells78b72802011-03-11 17:57:23 +0000357 cred, type, description, match, no_state_check);
David Howells664cceb2005-09-28 17:03:15 +0100358 if (!IS_ERR(key_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto found;
360
David Howells664cceb2005-09-28 17:03:15 +0100361 switch (PTR_ERR(key_ref)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 case -EAGAIN: /* no key */
363 if (ret)
364 break;
365 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100366 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368 default:
David Howells664cceb2005-09-28 17:03:15 +0100369 err = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 break;
371 }
372 }
373
David Howells3e301482005-06-23 22:00:56 -0700374 /* search the session keyring */
David Howells3a505972012-10-02 19:24:29 +0100375 if (cred->session_keyring) {
David Howells8589b4e2005-06-23 22:00:53 -0700376 rcu_read_lock();
David Howells664cceb2005-09-28 17:03:15 +0100377 key_ref = keyring_search_aux(
David Howells3a505972012-10-02 19:24:29 +0100378 make_key_ref(rcu_dereference(cred->session_keyring), 1),
David Howells78b72802011-03-11 17:57:23 +0000379 cred, type, description, match, no_state_check);
David Howells8589b4e2005-06-23 22:00:53 -0700380 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David Howells664cceb2005-09-28 17:03:15 +0100382 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700383 goto found;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
David Howells664cceb2005-09-28 17:03:15 +0100385 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700386 case -EAGAIN: /* no key */
387 if (ret)
388 break;
389 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100390 ret = key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
David Howells3e301482005-06-23 22:00:56 -0700392 default:
David Howells664cceb2005-09-28 17:03:15 +0100393 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700394 break;
395 }
David Howells3e301482005-06-23 22:00:56 -0700396 }
397 /* or search the user-session keyring */
David Howellsc69e8d92008-11-14 10:39:19 +1100398 else if (cred->user->session_keyring) {
David Howells664cceb2005-09-28 17:03:15 +0100399 key_ref = keyring_search_aux(
David Howellsc69e8d92008-11-14 10:39:19 +1100400 make_key_ref(cred->user->session_keyring, 1),
David Howells78b72802011-03-11 17:57:23 +0000401 cred, type, description, match, no_state_check);
David Howells664cceb2005-09-28 17:03:15 +0100402 if (!IS_ERR(key_ref))
David Howells3e301482005-06-23 22:00:56 -0700403 goto found;
404
David Howells664cceb2005-09-28 17:03:15 +0100405 switch (PTR_ERR(key_ref)) {
David Howells3e301482005-06-23 22:00:56 -0700406 case -EAGAIN: /* no key */
407 if (ret)
408 break;
409 case -ENOKEY: /* negative key */
David Howells664cceb2005-09-28 17:03:15 +0100410 ret = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700411 break;
412 default:
David Howells664cceb2005-09-28 17:03:15 +0100413 err = key_ref;
David Howells3e301482005-06-23 22:00:56 -0700414 break;
415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
417
David Howells927942a2010-06-11 17:31:10 +0100418 /* no key - decide on the error we're going to go for */
419 key_ref = ret ? ret : err;
420
421found:
422 return key_ref;
423}
424
David Howells927942a2010-06-11 17:31:10 +0100425/*
David Howells973c9f42011-01-20 16:38:33 +0000426 * Search the process keyrings attached to the supplied cred for the first
427 * matching key in the manner of search_my_process_keyrings(), but also search
428 * the keys attached to the assumed authorisation key using its credentials if
429 * one is available.
430 *
431 * Return same as search_my_process_keyrings().
David Howells927942a2010-06-11 17:31:10 +0100432 */
433key_ref_t search_process_keyrings(struct key_type *type,
434 const void *description,
435 key_match_func_t match,
436 const struct cred *cred)
437{
438 struct request_key_auth *rka;
439 key_ref_t key_ref, ret = ERR_PTR(-EACCES), err;
440
441 might_sleep();
442
David Howells78b72802011-03-11 17:57:23 +0000443 key_ref = search_my_process_keyrings(type, description, match,
444 false, cred);
David Howells927942a2010-06-11 17:31:10 +0100445 if (!IS_ERR(key_ref))
446 goto found;
447 err = key_ref;
448
David Howellsb5f545c2006-01-08 01:02:47 -0800449 /* if this process has an instantiation authorisation key, then we also
450 * search the keyrings of the process mentioned there
451 * - we don't permit access to request_key auth keys via this method
452 */
David Howellsc69e8d92008-11-14 10:39:19 +1100453 if (cred->request_key_auth &&
David Howellsd84f4f92008-11-14 10:39:23 +1100454 cred == current_cred() &&
David Howells04c567d2006-06-22 14:47:18 -0700455 type != &key_type_request_key_auth
David Howellsb5f545c2006-01-08 01:02:47 -0800456 ) {
David Howells04c567d2006-06-22 14:47:18 -0700457 /* defend against the auth key being revoked */
David Howellsc69e8d92008-11-14 10:39:19 +1100458 down_read(&cred->request_key_auth->sem);
David Howells3e301482005-06-23 22:00:56 -0700459
David Howellsc69e8d92008-11-14 10:39:19 +1100460 if (key_validate(cred->request_key_auth) == 0) {
461 rka = cred->request_key_auth->payload.data;
David Howellsb5f545c2006-01-08 01:02:47 -0800462
David Howells04c567d2006-06-22 14:47:18 -0700463 key_ref = search_process_keyrings(type, description,
David Howellsd84f4f92008-11-14 10:39:23 +1100464 match, rka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800465
David Howellsc69e8d92008-11-14 10:39:19 +1100466 up_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700467
468 if (!IS_ERR(key_ref))
469 goto found;
470
David Howells927942a2010-06-11 17:31:10 +0100471 ret = key_ref;
David Howells04c567d2006-06-22 14:47:18 -0700472 } else {
David Howellsc69e8d92008-11-14 10:39:19 +1100473 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800474 }
475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* no key - decide on the error we're going to go for */
David Howells927942a2010-06-11 17:31:10 +0100478 if (err == ERR_PTR(-ENOKEY) || ret == ERR_PTR(-ENOKEY))
479 key_ref = ERR_PTR(-ENOKEY);
480 else if (err == ERR_PTR(-EACCES))
481 key_ref = ret;
482 else
483 key_ref = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
David Howells3e301482005-06-23 22:00:56 -0700485found:
David Howells664cceb2005-09-28 17:03:15 +0100486 return key_ref;
David Howellsa8b17ed2011-01-20 16:38:27 +0000487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
David Howells973c9f42011-01-20 16:38:33 +0000490 * See if the key we're looking at is the target key.
David Howells664cceb2005-09-28 17:03:15 +0100491 */
David Howells927942a2010-06-11 17:31:10 +0100492int lookup_user_key_possessed(const struct key *key, const void *target)
David Howells664cceb2005-09-28 17:03:15 +0100493{
494 return key == target;
David Howellsa8b17ed2011-01-20 16:38:27 +0000495}
David Howells664cceb2005-09-28 17:03:15 +0100496
David Howells664cceb2005-09-28 17:03:15 +0100497/*
David Howells973c9f42011-01-20 16:38:33 +0000498 * Look up a key ID given us by userspace with a given permissions mask to get
499 * the key it refers to.
500 *
501 * Flags can be passed to request that special keyrings be created if referred
502 * to directly, to permit partially constructed keys to be found and to skip
503 * validity and permission checks on the found key.
504 *
505 * Returns a pointer to the key with an incremented usage count if successful;
506 * -EINVAL if the key ID is invalid; -ENOKEY if the key ID does not correspond
507 * to a key or the best found key was a negative key; -EKEYREVOKED or
508 * -EKEYEXPIRED if the best found key was revoked or expired; -EACCES if the
509 * found key doesn't grant the requested permit or the LSM denied access to it;
510 * or -ENOMEM if a special keyring couldn't be created.
511 *
512 * In the case of a successful return, the possession attribute is set on the
513 * returned key reference.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
David Howells55931222009-09-02 09:13:45 +0100515key_ref_t lookup_user_key(key_serial_t id, unsigned long lflags,
David Howells8bbf49762008-11-14 10:39:14 +1100516 key_perm_t perm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
David Howells8bbf49762008-11-14 10:39:14 +1100518 struct request_key_auth *rka;
David Howellsd84f4f92008-11-14 10:39:23 +1100519 const struct cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct key *key;
David Howellsb6dff3e2008-11-14 10:39:16 +1100521 key_ref_t key_ref, skey_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int ret;
523
David Howellsbb952bb2008-11-14 10:39:20 +1100524try_again:
525 cred = get_current_cred();
David Howells664cceb2005-09-28 17:03:15 +0100526 key_ref = ERR_PTR(-ENOKEY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 switch (id) {
529 case KEY_SPEC_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100530 if (!cred->thread_keyring) {
David Howells55931222009-09-02 09:13:45 +0100531 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto error;
533
David Howells8bbf49762008-11-14 10:39:14 +1100534 ret = install_thread_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100536 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto error;
538 }
David Howellsbb952bb2008-11-14 10:39:20 +1100539 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541
David Howellsb6dff3e2008-11-14 10:39:16 +1100542 key = cred->thread_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100544 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 break;
546
547 case KEY_SPEC_PROCESS_KEYRING:
David Howells3a505972012-10-02 19:24:29 +0100548 if (!cred->process_keyring) {
David Howells55931222009-09-02 09:13:45 +0100549 if (!(lflags & KEY_LOOKUP_CREATE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto error;
551
David Howells8bbf49762008-11-14 10:39:14 +1100552 ret = install_process_keyring();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (ret < 0) {
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100554 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto error;
556 }
David Howellsbb952bb2008-11-14 10:39:20 +1100557 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559
David Howells3a505972012-10-02 19:24:29 +0100560 key = cred->process_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100562 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564
565 case KEY_SPEC_SESSION_KEYRING:
David Howells3a505972012-10-02 19:24:29 +0100566 if (!cred->session_keyring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* always install a session keyring upon access if one
568 * doesn't exist yet */
David Howells8bbf49762008-11-14 10:39:14 +1100569 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700570 if (ret < 0)
571 goto error;
David Howells3ecf1b42011-08-22 14:08:43 +0100572 if (lflags & KEY_LOOKUP_CREATE)
573 ret = join_session_keyring(NULL);
574 else
575 ret = install_session_keyring(
576 cred->user->session_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (ret < 0)
579 goto error;
David Howellsbb952bb2008-11-14 10:39:20 +1100580 goto reget_creds;
David Howells3a505972012-10-02 19:24:29 +0100581 } else if (cred->session_keyring ==
David Howells3ecf1b42011-08-22 14:08:43 +0100582 cred->user->session_keyring &&
583 lflags & KEY_LOOKUP_CREATE) {
584 ret = join_session_keyring(NULL);
585 if (ret < 0)
586 goto error;
587 goto reget_creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
David Howells3e301482005-06-23 22:00:56 -0700590 rcu_read_lock();
David Howells3a505972012-10-02 19:24:29 +0100591 key = rcu_dereference(cred->session_keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 atomic_inc(&key->usage);
David Howells3e301482005-06-23 22:00:56 -0700593 rcu_read_unlock();
David Howells664cceb2005-09-28 17:03:15 +0100594 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596
597 case KEY_SPEC_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100598 if (!cred->user->uid_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100599 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700600 if (ret < 0)
601 goto error;
602 }
603
David Howellsb6dff3e2008-11-14 10:39:16 +1100604 key = cred->user->uid_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100606 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 break;
608
609 case KEY_SPEC_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100610 if (!cred->user->session_keyring) {
David Howells8bbf49762008-11-14 10:39:14 +1100611 ret = install_user_keyrings();
David Howells69664cf2008-04-29 01:01:31 -0700612 if (ret < 0)
613 goto error;
614 }
615
David Howellsb6dff3e2008-11-14 10:39:16 +1100616 key = cred->user->session_keyring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 atomic_inc(&key->usage);
David Howells664cceb2005-09-28 17:03:15 +0100618 key_ref = make_key_ref(key, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 break;
620
621 case KEY_SPEC_GROUP_KEYRING:
622 /* group keyrings are not yet supported */
Dan Carpenter4d09ec02010-05-17 14:42:35 +0100623 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto error;
625
David Howellsb5f545c2006-01-08 01:02:47 -0800626 case KEY_SPEC_REQKEY_AUTH_KEY:
David Howellsb6dff3e2008-11-14 10:39:16 +1100627 key = cred->request_key_auth;
David Howellsb5f545c2006-01-08 01:02:47 -0800628 if (!key)
629 goto error;
630
631 atomic_inc(&key->usage);
632 key_ref = make_key_ref(key, 1);
633 break;
634
David Howells8bbf49762008-11-14 10:39:14 +1100635 case KEY_SPEC_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100636 if (!cred->request_key_auth)
David Howells8bbf49762008-11-14 10:39:14 +1100637 goto error;
638
David Howellsb6dff3e2008-11-14 10:39:16 +1100639 down_read(&cred->request_key_auth->sem);
Dan Carpenterf67dabb2012-03-06 13:32:16 +0000640 if (test_bit(KEY_FLAG_REVOKED,
641 &cred->request_key_auth->flags)) {
David Howells8bbf49762008-11-14 10:39:14 +1100642 key_ref = ERR_PTR(-EKEYREVOKED);
643 key = NULL;
644 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100645 rka = cred->request_key_auth->payload.data;
David Howells8bbf49762008-11-14 10:39:14 +1100646 key = rka->dest_keyring;
647 atomic_inc(&key->usage);
648 }
David Howellsb6dff3e2008-11-14 10:39:16 +1100649 up_read(&cred->request_key_auth->sem);
David Howells8bbf49762008-11-14 10:39:14 +1100650 if (!key)
651 goto error;
652 key_ref = make_key_ref(key, 1);
653 break;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 default:
David Howells664cceb2005-09-28 17:03:15 +0100656 key_ref = ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (id < 1)
658 goto error;
659
660 key = key_lookup(id);
David Howells664cceb2005-09-28 17:03:15 +0100661 if (IS_ERR(key)) {
David Howellse231c2e2008-02-07 00:15:26 -0800662 key_ref = ERR_CAST(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto error;
David Howells664cceb2005-09-28 17:03:15 +0100664 }
665
666 key_ref = make_key_ref(key, 0);
667
668 /* check to see if we possess the key */
669 skey_ref = search_process_keyrings(key->type, key,
670 lookup_user_key_possessed,
David Howellsd84f4f92008-11-14 10:39:23 +1100671 cred);
David Howells664cceb2005-09-28 17:03:15 +0100672
673 if (!IS_ERR(skey_ref)) {
674 key_put(key);
675 key_ref = skey_ref;
676 }
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
679 }
680
David Howells55931222009-09-02 09:13:45 +0100681 /* unlink does not use the nominated key in any way, so can skip all
682 * the permission checks as it is only concerned with the keyring */
683 if (lflags & KEY_LOOKUP_FOR_UNLINK) {
684 ret = 0;
685 goto error;
686 }
687
688 if (!(lflags & KEY_LOOKUP_PARTIAL)) {
David Howells76181c12007-10-16 23:29:46 -0700689 ret = wait_for_key_construction(key, true);
690 switch (ret) {
691 case -ERESTARTSYS:
692 goto invalid_key;
693 default:
694 if (perm)
695 goto invalid_key;
696 case 0:
697 break;
698 }
699 } else if (perm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ret = key_validate(key);
701 if (ret < 0)
702 goto invalid_key;
703 }
704
705 ret = -EIO;
David Howells55931222009-09-02 09:13:45 +0100706 if (!(lflags & KEY_LOOKUP_PARTIAL) &&
707 !test_bit(KEY_FLAG_INSTANTIATED, &key->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto invalid_key;
709
David Howells3e301482005-06-23 22:00:56 -0700710 /* check the permissions */
David Howellsd84f4f92008-11-14 10:39:23 +1100711 ret = key_task_permission(key_ref, cred, perm);
David Howells29db9192005-10-30 15:02:44 -0800712 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto invalid_key;
714
David Howells31d5a792012-05-11 10:56:56 +0100715 key->last_used_at = current_kernel_time().tv_sec;
716
David Howells664cceb2005-09-28 17:03:15 +0100717error:
David Howellsbb952bb2008-11-14 10:39:20 +1100718 put_cred(cred);
David Howells664cceb2005-09-28 17:03:15 +0100719 return key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
David Howells664cceb2005-09-28 17:03:15 +0100721invalid_key:
722 key_ref_put(key_ref);
723 key_ref = ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto error;
725
David Howellsbb952bb2008-11-14 10:39:20 +1100726 /* if we attempted to install a keyring, then it may have caused new
727 * creds to be installed */
728reget_creds:
729 put_cred(cred);
730 goto try_again;
David Howellsa8b17ed2011-01-20 16:38:27 +0000731}
David Howellsbb952bb2008-11-14 10:39:20 +1100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
David Howells973c9f42011-01-20 16:38:33 +0000734 * Join the named keyring as the session keyring if possible else attempt to
735 * create a new one of that name and join that.
736 *
737 * If the name is NULL, an empty anonymous keyring will be installed as the
738 * session keyring.
739 *
740 * Named session keyrings are joined with a semaphore held to prevent the
741 * keyrings from going away whilst the attempt is made to going them and also
742 * to prevent a race in creating compatible session keyrings.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
744long join_session_keyring(const char *name)
745{
David Howellsd84f4f92008-11-14 10:39:23 +1100746 const struct cred *old;
747 struct cred *new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct key *keyring;
David Howellsd84f4f92008-11-14 10:39:23 +1100749 long ret, serial;
750
David Howellsd84f4f92008-11-14 10:39:23 +1100751 new = prepare_creds();
752 if (!new)
753 return -ENOMEM;
754 old = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* if no name is provided, install an anonymous keyring */
757 if (!name) {
David Howellsd84f4f92008-11-14 10:39:23 +1100758 ret = install_session_keyring_to_cred(new, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (ret < 0)
760 goto error;
761
David Howells3a505972012-10-02 19:24:29 +0100762 serial = new->session_keyring->serial;
David Howellsd84f4f92008-11-14 10:39:23 +1100763 ret = commit_creds(new);
764 if (ret == 0)
765 ret = serial;
766 goto okay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768
769 /* allow the user to join or create a named keyring */
Ingo Molnarbb0030792006-03-22 00:09:14 -0800770 mutex_lock(&key_session_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* look for an existing keyring of this name */
David Howells69664cf2008-04-29 01:01:31 -0700773 keyring = find_keyring_by_name(name, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (PTR_ERR(keyring) == -ENOKEY) {
775 /* not found - try and create a new one */
David Howellsd84f4f92008-11-14 10:39:23 +1100776 keyring = keyring_alloc(name, old->uid, old->gid, old,
David Howells7e047ef2006-06-26 00:24:50 -0700777 KEY_ALLOC_IN_QUOTA, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (IS_ERR(keyring)) {
779 ret = PTR_ERR(keyring);
David Howellsbcf945d2005-08-04 13:07:06 -0700780 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
David Howellsd84f4f92008-11-14 10:39:23 +1100782 } else if (IS_ERR(keyring)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 ret = PTR_ERR(keyring);
784 goto error2;
David Howells3a505972012-10-02 19:24:29 +0100785 } else if (keyring == new->session_keyring) {
786 ret = 0;
787 goto error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 /* we've got a keyring - now to install it */
David Howellsd84f4f92008-11-14 10:39:23 +1100791 ret = install_session_keyring_to_cred(new, keyring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (ret < 0)
793 goto error2;
794
David Howellsd84f4f92008-11-14 10:39:23 +1100795 commit_creds(new);
796 mutex_unlock(&key_session_mutex);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ret = keyring->serial;
799 key_put(keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100800okay:
801 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
David Howells664cceb2005-09-28 17:03:15 +0100803error2:
Ingo Molnarbb0030792006-03-22 00:09:14 -0800804 mutex_unlock(&key_session_mutex);
David Howells664cceb2005-09-28 17:03:15 +0100805error:
David Howellsd84f4f92008-11-14 10:39:23 +1100806 abort_creds(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100808}
David Howellsee18d642009-09-02 09:14:21 +0100809
810/*
David Howells973c9f42011-01-20 16:38:33 +0000811 * Replace a process's session keyring on behalf of one of its children when
812 * the target process is about to resume userspace execution.
David Howellsee18d642009-09-02 09:14:21 +0100813 */
Al Viro67d12142012-06-27 11:07:19 +0400814void key_change_session_keyring(struct callback_head *twork)
David Howellsee18d642009-09-02 09:14:21 +0100815{
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000816 const struct cred *old = current_cred();
Al Viro67d12142012-06-27 11:07:19 +0400817 struct cred *new = container_of(twork, struct cred, rcu);
David Howellsee18d642009-09-02 09:14:21 +0100818
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000819 if (unlikely(current->flags & PF_EXITING)) {
820 put_cred(new);
David Howellsee18d642009-09-02 09:14:21 +0100821 return;
Oleg Nesterov413cd3d2012-05-11 10:59:08 +1000822 }
David Howellsee18d642009-09-02 09:14:21 +0100823
David Howellsee18d642009-09-02 09:14:21 +0100824 new-> uid = old-> uid;
825 new-> euid = old-> euid;
826 new-> suid = old-> suid;
827 new->fsuid = old->fsuid;
828 new-> gid = old-> gid;
829 new-> egid = old-> egid;
830 new-> sgid = old-> sgid;
831 new->fsgid = old->fsgid;
832 new->user = get_uid(old->user);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -0800833 new->user_ns = get_user_ns(new->user_ns);
David Howellsee18d642009-09-02 09:14:21 +0100834 new->group_info = get_group_info(old->group_info);
835
836 new->securebits = old->securebits;
837 new->cap_inheritable = old->cap_inheritable;
838 new->cap_permitted = old->cap_permitted;
839 new->cap_effective = old->cap_effective;
840 new->cap_bset = old->cap_bset;
841
842 new->jit_keyring = old->jit_keyring;
843 new->thread_keyring = key_get(old->thread_keyring);
David Howells3a505972012-10-02 19:24:29 +0100844 new->process_keyring = key_get(old->process_keyring);
David Howellsee18d642009-09-02 09:14:21 +0100845
846 security_transfer_creds(new, old);
847
848 commit_creds(new);
849}