blob: ec1c07667ec1d5daec2820d6b30cda2bf99818c4 [file] [log] [blame]
Randy Dunlapd410fa42011-05-19 15:59:38 -07001/* Task credentials management - see Documentation/security/credentials.txt
David Howellsf1752ee2008-11-14 10:39:17 +11002 *
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * 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 Licence
8 * as published by the Free Software Foundation; either version
9 * 2 of the Licence, or (at your option) any later version.
10 */
Paul Gortmaker9984de12011-05-23 14:51:41 -040011#include <linux/export.h>
David Howellsf1752ee2008-11-14 10:39:17 +110012#include <linux/cred.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Howellsf1752ee2008-11-14 10:39:17 +110014#include <linux/sched.h>
15#include <linux/key.h>
16#include <linux/keyctl.h>
17#include <linux/init_task.h>
18#include <linux/security.h>
Al Viro40401532012-02-13 03:58:52 +000019#include <linux/binfmts.h>
David Howellsd84f4f92008-11-14 10:39:23 +110020#include <linux/cn_proc.h>
David Howellsd84f4f92008-11-14 10:39:23 +110021
David Howellse0e81732009-09-02 09:13:40 +010022#if 0
23#define kdebug(FMT, ...) \
24 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
25#else
David Howellse0e81732009-09-02 09:13:40 +010026#define kdebug(FMT, ...) \
27 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
28#endif
29
David Howellsd84f4f92008-11-14 10:39:23 +110030static struct kmem_cache *cred_jar;
David Howellsf1752ee2008-11-14 10:39:17 +110031
Iulia Manda28138932015-04-15 16:16:41 -070032/* init to 2 - one for init_task, one to ensure it is never freed */
33struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
34
David Howellsf1752ee2008-11-14 10:39:17 +110035/*
36 * The initial credentials for the initial task
37 */
38struct cred init_cred = {
David Howells3b11a1d2008-11-14 10:39:26 +110039 .usage = ATOMIC_INIT(4),
David Howellse0e81732009-09-02 09:13:40 +010040#ifdef CONFIG_DEBUG_CREDENTIALS
41 .subscribers = ATOMIC_INIT(2),
42 .magic = CRED_MAGIC,
43#endif
Eric W. Biederman078de5f2012-02-08 07:00:08 -080044 .uid = GLOBAL_ROOT_UID,
45 .gid = GLOBAL_ROOT_GID,
46 .suid = GLOBAL_ROOT_UID,
47 .sgid = GLOBAL_ROOT_GID,
48 .euid = GLOBAL_ROOT_UID,
49 .egid = GLOBAL_ROOT_GID,
50 .fsuid = GLOBAL_ROOT_UID,
51 .fsgid = GLOBAL_ROOT_GID,
David Howellsf1752ee2008-11-14 10:39:17 +110052 .securebits = SECUREBITS_DEFAULT,
Eric Parisa3232d22011-04-01 17:08:45 -040053 .cap_inheritable = CAP_EMPTY_SET,
David Howellsf1752ee2008-11-14 10:39:17 +110054 .cap_permitted = CAP_FULL_SET,
Eric Parisa3232d22011-04-01 17:08:45 -040055 .cap_effective = CAP_FULL_SET,
56 .cap_bset = CAP_FULL_SET,
David Howellsf1752ee2008-11-14 10:39:17 +110057 .user = INIT_USER,
Serge E. Hallyn47a150e2011-05-13 04:27:54 +010058 .user_ns = &init_user_ns,
David Howellsf1752ee2008-11-14 10:39:17 +110059 .group_info = &init_groups,
60};
61
David Howellse0e81732009-09-02 09:13:40 +010062static inline void set_cred_subscribers(struct cred *cred, int n)
63{
64#ifdef CONFIG_DEBUG_CREDENTIALS
65 atomic_set(&cred->subscribers, n);
66#endif
67}
68
69static inline int read_cred_subscribers(const struct cred *cred)
70{
71#ifdef CONFIG_DEBUG_CREDENTIALS
72 return atomic_read(&cred->subscribers);
73#else
74 return 0;
75#endif
76}
77
78static inline void alter_cred_subscribers(const struct cred *_cred, int n)
79{
80#ifdef CONFIG_DEBUG_CREDENTIALS
81 struct cred *cred = (struct cred *) _cred;
82
83 atomic_add(n, &cred->subscribers);
84#endif
85}
86
David Howellsf1752ee2008-11-14 10:39:17 +110087/*
88 * The RCU callback to actually dispose of a set of credentials
89 */
90static void put_cred_rcu(struct rcu_head *rcu)
91{
92 struct cred *cred = container_of(rcu, struct cred, rcu);
93
David Howellse0e81732009-09-02 09:13:40 +010094 kdebug("put_cred_rcu(%p)", cred);
95
96#ifdef CONFIG_DEBUG_CREDENTIALS
97 if (cred->magic != CRED_MAGIC_DEAD ||
98 atomic_read(&cred->usage) != 0 ||
99 read_cred_subscribers(cred) != 0)
100 panic("CRED: put_cred_rcu() sees %p with"
101 " mag %x, put %p, usage %d, subscr %d\n",
102 cred, cred->magic, cred->put_addr,
103 atomic_read(&cred->usage),
104 read_cred_subscribers(cred));
105#else
David Howellsd84f4f92008-11-14 10:39:23 +1100106 if (atomic_read(&cred->usage) != 0)
107 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
108 cred, atomic_read(&cred->usage));
David Howellse0e81732009-09-02 09:13:40 +0100109#endif
David Howellsf1752ee2008-11-14 10:39:17 +1100110
David Howellsd84f4f92008-11-14 10:39:23 +1100111 security_cred_free(cred);
David Howells3a505972012-10-02 19:24:29 +0100112 key_put(cred->session_keyring);
113 key_put(cred->process_keyring);
David Howellsf1752ee2008-11-14 10:39:17 +1100114 key_put(cred->thread_keyring);
115 key_put(cred->request_key_auth);
David Howells4a5d6ba2009-09-14 12:45:39 +0100116 if (cred->group_info)
117 put_group_info(cred->group_info);
David Howellsf1752ee2008-11-14 10:39:17 +1100118 free_uid(cred->user);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -0800119 put_user_ns(cred->user_ns);
David Howellsd84f4f92008-11-14 10:39:23 +1100120 kmem_cache_free(cred_jar, cred);
David Howellsf1752ee2008-11-14 10:39:17 +1100121}
122
123/**
124 * __put_cred - Destroy a set of credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100125 * @cred: The record to release
David Howellsf1752ee2008-11-14 10:39:17 +1100126 *
127 * Destroy a set of credentials on which no references remain.
128 */
129void __put_cred(struct cred *cred)
130{
David Howellse0e81732009-09-02 09:13:40 +0100131 kdebug("__put_cred(%p{%d,%d})", cred,
132 atomic_read(&cred->usage),
133 read_cred_subscribers(cred));
134
David Howellsd84f4f92008-11-14 10:39:23 +1100135 BUG_ON(atomic_read(&cred->usage) != 0);
David Howellse0e81732009-09-02 09:13:40 +0100136#ifdef CONFIG_DEBUG_CREDENTIALS
137 BUG_ON(read_cred_subscribers(cred) != 0);
138 cred->magic = CRED_MAGIC_DEAD;
139 cred->put_addr = __builtin_return_address(0);
140#endif
141 BUG_ON(cred == current->cred);
142 BUG_ON(cred == current->real_cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100143
David Howellsf1752ee2008-11-14 10:39:17 +1100144 call_rcu(&cred->rcu, put_cred_rcu);
145}
146EXPORT_SYMBOL(__put_cred);
147
David Howellse0e81732009-09-02 09:13:40 +0100148/*
149 * Clean up a task's credentials when it exits
150 */
151void exit_creds(struct task_struct *tsk)
152{
153 struct cred *cred;
154
155 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
156 atomic_read(&tsk->cred->usage),
157 read_cred_subscribers(tsk->cred));
158
159 cred = (struct cred *) tsk->real_cred;
160 tsk->real_cred = NULL;
161 validate_creds(cred);
162 alter_cred_subscribers(cred, -1);
163 put_cred(cred);
164
165 cred = (struct cred *) tsk->cred;
166 tsk->cred = NULL;
167 validate_creds(cred);
168 alter_cred_subscribers(cred, -1);
169 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +0100170}
171
David Howellsde09a972010-07-29 12:45:49 +0100172/**
173 * get_task_cred - Get another task's objective credentials
174 * @task: The task to query
175 *
176 * Get the objective credentials of a task, pinning them so that they can't go
177 * away. Accessing a task's credentials directly is not permitted.
178 *
179 * The caller must also make sure task doesn't get deleted, either by holding a
180 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
181 */
182const struct cred *get_task_cred(struct task_struct *task)
183{
184 const struct cred *cred;
185
186 rcu_read_lock();
187
188 do {
189 cred = __task_cred((task));
190 BUG_ON(!cred);
191 } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
192
193 rcu_read_unlock();
194 return cred;
195}
196
David Howellsee18d642009-09-02 09:14:21 +0100197/*
198 * Allocate blank credentials, such that the credentials can be filled in at a
199 * later date without risk of ENOMEM.
200 */
201struct cred *cred_alloc_blank(void)
202{
203 struct cred *new;
204
205 new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
206 if (!new)
207 return NULL;
208
David Howellsee18d642009-09-02 09:14:21 +0100209 atomic_set(&new->usage, 1);
Tetsuo Handa2edeaa32011-02-07 13:36:10 +0000210#ifdef CONFIG_DEBUG_CREDENTIALS
211 new->magic = CRED_MAGIC;
212#endif
David Howellsee18d642009-09-02 09:14:21 +0100213
214 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
215 goto error;
216
David Howellsee18d642009-09-02 09:14:21 +0100217 return new;
218
219error:
220 abort_creds(new);
221 return NULL;
David Howellse0e81732009-09-02 09:13:40 +0100222}
223
David Howellsd84f4f92008-11-14 10:39:23 +1100224/**
225 * prepare_creds - Prepare a new set of credentials for modification
226 *
227 * Prepare a new set of task credentials for modification. A task's creds
228 * shouldn't generally be modified directly, therefore this function is used to
229 * prepare a new copy, which the caller then modifies and then commits by
230 * calling commit_creds().
231 *
David Howells3b11a1d2008-11-14 10:39:26 +1100232 * Preparation involves making a copy of the objective creds for modification.
233 *
David Howellsd84f4f92008-11-14 10:39:23 +1100234 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
235 *
236 * Call commit_creds() or abort_creds() to clean up.
David Howellsf1752ee2008-11-14 10:39:17 +1100237 */
David Howellsd84f4f92008-11-14 10:39:23 +1100238struct cred *prepare_creds(void)
David Howellsf1752ee2008-11-14 10:39:17 +1100239{
David Howellsd84f4f92008-11-14 10:39:23 +1100240 struct task_struct *task = current;
241 const struct cred *old;
242 struct cred *new;
David Howellsf1752ee2008-11-14 10:39:17 +1100243
David Howellse0e81732009-09-02 09:13:40 +0100244 validate_process_creds();
David Howellsd84f4f92008-11-14 10:39:23 +1100245
246 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
247 if (!new)
248 return NULL;
249
David Howellse0e81732009-09-02 09:13:40 +0100250 kdebug("prepare_creds() alloc %p", new);
251
David Howellsd84f4f92008-11-14 10:39:23 +1100252 old = task->cred;
253 memcpy(new, old, sizeof(struct cred));
254
255 atomic_set(&new->usage, 1);
David Howellse0e81732009-09-02 09:13:40 +0100256 set_cred_subscribers(new, 0);
David Howellsd84f4f92008-11-14 10:39:23 +1100257 get_group_info(new->group_info);
258 get_uid(new->user);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -0800259 get_user_ns(new->user_ns);
David Howellsf1752ee2008-11-14 10:39:17 +1100260
David Howellsbb952bb2008-11-14 10:39:20 +1100261#ifdef CONFIG_KEYS
David Howells3a505972012-10-02 19:24:29 +0100262 key_get(new->session_keyring);
263 key_get(new->process_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100264 key_get(new->thread_keyring);
265 key_get(new->request_key_auth);
David Howellsbb952bb2008-11-14 10:39:20 +1100266#endif
267
David Howellsf1752ee2008-11-14 10:39:17 +1100268#ifdef CONFIG_SECURITY
David Howellsd84f4f92008-11-14 10:39:23 +1100269 new->security = NULL;
David Howellsf1752ee2008-11-14 10:39:17 +1100270#endif
271
David Howellsd84f4f92008-11-14 10:39:23 +1100272 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
273 goto error;
David Howellse0e81732009-09-02 09:13:40 +0100274 validate_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100275 return new;
276
277error:
278 abort_creds(new);
279 return NULL;
280}
281EXPORT_SYMBOL(prepare_creds);
282
283/*
David Howellsa6f76f22008-11-14 10:39:24 +1100284 * Prepare credentials for current to perform an execve()
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700285 * - The caller must hold ->cred_guard_mutex
David Howellsa6f76f22008-11-14 10:39:24 +1100286 */
287struct cred *prepare_exec_creds(void)
288{
David Howellsa6f76f22008-11-14 10:39:24 +1100289 struct cred *new;
290
David Howellsa6f76f22008-11-14 10:39:24 +1100291 new = prepare_creds();
David Howells3a505972012-10-02 19:24:29 +0100292 if (!new)
David Howellsa6f76f22008-11-14 10:39:24 +1100293 return new;
David Howellsa6f76f22008-11-14 10:39:24 +1100294
295#ifdef CONFIG_KEYS
296 /* newly exec'd tasks don't get a thread keyring */
297 key_put(new->thread_keyring);
298 new->thread_keyring = NULL;
299
David Howellsa6f76f22008-11-14 10:39:24 +1100300 /* inherit the session keyring; new process keyring */
David Howells3a505972012-10-02 19:24:29 +0100301 key_put(new->process_keyring);
302 new->process_keyring = NULL;
David Howellsa6f76f22008-11-14 10:39:24 +1100303#endif
304
305 return new;
306}
307
308/*
David Howellsd84f4f92008-11-14 10:39:23 +1100309 * Copy credentials for the new process created by fork()
310 *
311 * We share if we can, but under some circumstances we have to generate a new
312 * set.
David Howells3b11a1d2008-11-14 10:39:26 +1100313 *
314 * The new process gets the current process's subjective credentials as its
315 * objective and subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100316 */
317int copy_creds(struct task_struct *p, unsigned long clone_flags)
318{
David Howellsd84f4f92008-11-14 10:39:23 +1100319 struct cred *new;
Serge Hallyn18b6e042008-10-15 16:38:45 -0500320 int ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100321
David Howellsd84f4f92008-11-14 10:39:23 +1100322 if (
323#ifdef CONFIG_KEYS
324 !p->cred->thread_keyring &&
325#endif
326 clone_flags & CLONE_THREAD
327 ) {
David Howells3b11a1d2008-11-14 10:39:26 +1100328 p->real_cred = get_cred(p->cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100329 get_cred(p->cred);
David Howellse0e81732009-09-02 09:13:40 +0100330 alter_cred_subscribers(p->cred, 2);
331 kdebug("share_creds(%p{%d,%d})",
332 p->cred, atomic_read(&p->cred->usage),
333 read_cred_subscribers(p->cred));
David Howellsd84f4f92008-11-14 10:39:23 +1100334 atomic_inc(&p->cred->user->processes);
335 return 0;
David Howellsf1752ee2008-11-14 10:39:17 +1100336 }
337
David Howellsd84f4f92008-11-14 10:39:23 +1100338 new = prepare_creds();
339 if (!new)
340 return -ENOMEM;
David Howellsf1752ee2008-11-14 10:39:17 +1100341
Serge Hallyn18b6e042008-10-15 16:38:45 -0500342 if (clone_flags & CLONE_NEWUSER) {
343 ret = create_user_ns(new);
344 if (ret < 0)
345 goto error_put;
346 }
347
David Howellsd84f4f92008-11-14 10:39:23 +1100348#ifdef CONFIG_KEYS
349 /* new threads get their own thread keyrings if their parent already
350 * had one */
351 if (new->thread_keyring) {
352 key_put(new->thread_keyring);
353 new->thread_keyring = NULL;
354 if (clone_flags & CLONE_THREAD)
355 install_thread_keyring_to_cred(new);
356 }
David Howellsf1752ee2008-11-14 10:39:17 +1100357
David Howells3a505972012-10-02 19:24:29 +0100358 /* The process keyring is only shared between the threads in a process;
359 * anything outside of those threads doesn't inherit.
360 */
David Howellsd84f4f92008-11-14 10:39:23 +1100361 if (!(clone_flags & CLONE_THREAD)) {
David Howells3a505972012-10-02 19:24:29 +0100362 key_put(new->process_keyring);
363 new->process_keyring = NULL;
David Howellsd84f4f92008-11-14 10:39:23 +1100364 }
365#endif
366
367 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100368 p->cred = p->real_cred = get_cred(new);
David Howellse0e81732009-09-02 09:13:40 +0100369 alter_cred_subscribers(new, 2);
370 validate_creds(new);
David Howellsf1752ee2008-11-14 10:39:17 +1100371 return 0;
Serge Hallyn18b6e042008-10-15 16:38:45 -0500372
373error_put:
374 put_cred(new);
375 return ret;
David Howellsf1752ee2008-11-14 10:39:17 +1100376}
David Howellsd84f4f92008-11-14 10:39:23 +1100377
Eric W. Biedermanaa6d0542012-12-14 08:50:54 -0800378static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
379{
380 const struct user_namespace *set_ns = set->user_ns;
381 const struct user_namespace *subset_ns = subset->user_ns;
382
383 /* If the two credentials are in the same user namespace see if
384 * the capabilities of subset are a subset of set.
385 */
386 if (set_ns == subset_ns)
387 return cap_issubset(subset->cap_permitted, set->cap_permitted);
388
389 /* The credentials are in a different user namespaces
390 * therefore one is a subset of the other only if a set is an
391 * ancestor of subset and set->euid is owner of subset or one
392 * of subsets ancestors.
393 */
394 for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
395 if ((set_ns == subset_ns->parent) &&
396 uid_eq(subset_ns->owner, set->euid))
397 return true;
398 }
399
400 return false;
401}
402
David Howellsd84f4f92008-11-14 10:39:23 +1100403/**
404 * commit_creds - Install new credentials upon the current task
405 * @new: The credentials to be assigned
406 *
407 * Install a new set of credentials to the current task, using RCU to replace
David Howells3b11a1d2008-11-14 10:39:26 +1100408 * the old set. Both the objective and the subjective credentials pointers are
409 * updated. This function may not be called if the subjective credentials are
410 * in an overridden state.
David Howellsd84f4f92008-11-14 10:39:23 +1100411 *
412 * This function eats the caller's reference to the new credentials.
413 *
414 * Always returns 0 thus allowing this function to be tail-called at the end
415 * of, say, sys_setgid().
416 */
417int commit_creds(struct cred *new)
418{
419 struct task_struct *task = current;
David Howellse0e81732009-09-02 09:13:40 +0100420 const struct cred *old = task->real_cred;
David Howellsd84f4f92008-11-14 10:39:23 +1100421
David Howellse0e81732009-09-02 09:13:40 +0100422 kdebug("commit_creds(%p{%d,%d})", new,
423 atomic_read(&new->usage),
424 read_cred_subscribers(new));
425
426 BUG_ON(task->cred != old);
427#ifdef CONFIG_DEBUG_CREDENTIALS
428 BUG_ON(read_cred_subscribers(old) < 2);
429 validate_creds(old);
430 validate_creds(new);
431#endif
David Howellsd84f4f92008-11-14 10:39:23 +1100432 BUG_ON(atomic_read(&new->usage) < 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100433
David Howells3b11a1d2008-11-14 10:39:26 +1100434 get_cred(new); /* we will require a ref for the subj creds too */
435
David Howellsd84f4f92008-11-14 10:39:23 +1100436 /* dumpability changes */
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800437 if (!uid_eq(old->euid, new->euid) ||
438 !gid_eq(old->egid, new->egid) ||
439 !uid_eq(old->fsuid, new->fsuid) ||
440 !gid_eq(old->fsgid, new->fsgid) ||
Eric W. Biedermanaa6d0542012-12-14 08:50:54 -0800441 !cred_cap_issubset(old, new)) {
David Howellsb9456372009-01-08 11:18:31 +0000442 if (task->mm)
443 set_dumpable(task->mm, suid_dumpable);
David Howellsd84f4f92008-11-14 10:39:23 +1100444 task->pdeath_signal = 0;
445 smp_wmb();
446 }
447
448 /* alter the thread keyring */
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800449 if (!uid_eq(new->fsuid, old->fsuid))
David Howellsd84f4f92008-11-14 10:39:23 +1100450 key_fsuid_changed(task);
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800451 if (!gid_eq(new->fsgid, old->fsgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100452 key_fsgid_changed(task);
453
454 /* do it
Vasiliy Kulikov72fa5992011-08-08 19:02:04 +0400455 * RLIMIT_NPROC limits on user->processes have already been checked
456 * in set_user().
David Howellsd84f4f92008-11-14 10:39:23 +1100457 */
David Howellse0e81732009-09-02 09:13:40 +0100458 alter_cred_subscribers(new, 2);
David Howellsd84f4f92008-11-14 10:39:23 +1100459 if (new->user != old->user)
460 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100461 rcu_assign_pointer(task->real_cred, new);
David Howellsd84f4f92008-11-14 10:39:23 +1100462 rcu_assign_pointer(task->cred, new);
463 if (new->user != old->user)
464 atomic_dec(&old->user->processes);
David Howellse0e81732009-09-02 09:13:40 +0100465 alter_cred_subscribers(old, -2);
David Howellsd84f4f92008-11-14 10:39:23 +1100466
David Howellsd84f4f92008-11-14 10:39:23 +1100467 /* send notifications */
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800468 if (!uid_eq(new->uid, old->uid) ||
469 !uid_eq(new->euid, old->euid) ||
470 !uid_eq(new->suid, old->suid) ||
471 !uid_eq(new->fsuid, old->fsuid))
David Howellsd84f4f92008-11-14 10:39:23 +1100472 proc_id_connector(task, PROC_EVENT_UID);
473
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800474 if (!gid_eq(new->gid, old->gid) ||
475 !gid_eq(new->egid, old->egid) ||
476 !gid_eq(new->sgid, old->sgid) ||
477 !gid_eq(new->fsgid, old->fsgid))
David Howellsd84f4f92008-11-14 10:39:23 +1100478 proc_id_connector(task, PROC_EVENT_GID);
479
David Howells3b11a1d2008-11-14 10:39:26 +1100480 /* release the old obj and subj refs both */
481 put_cred(old);
David Howellsd84f4f92008-11-14 10:39:23 +1100482 put_cred(old);
483 return 0;
484}
485EXPORT_SYMBOL(commit_creds);
486
487/**
488 * abort_creds - Discard a set of credentials and unlock the current task
489 * @new: The credentials that were going to be applied
490 *
491 * Discard a set of credentials that were under construction and unlock the
492 * current task.
493 */
494void abort_creds(struct cred *new)
495{
David Howellse0e81732009-09-02 09:13:40 +0100496 kdebug("abort_creds(%p{%d,%d})", new,
497 atomic_read(&new->usage),
498 read_cred_subscribers(new));
499
500#ifdef CONFIG_DEBUG_CREDENTIALS
501 BUG_ON(read_cred_subscribers(new) != 0);
502#endif
David Howellsd84f4f92008-11-14 10:39:23 +1100503 BUG_ON(atomic_read(&new->usage) < 1);
504 put_cred(new);
505}
506EXPORT_SYMBOL(abort_creds);
507
508/**
David Howells3b11a1d2008-11-14 10:39:26 +1100509 * override_creds - Override the current process's subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100510 * @new: The credentials to be assigned
511 *
David Howells3b11a1d2008-11-14 10:39:26 +1100512 * Install a set of temporary override subjective credentials on the current
513 * process, returning the old set for later reversion.
David Howellsd84f4f92008-11-14 10:39:23 +1100514 */
515const struct cred *override_creds(const struct cred *new)
516{
517 const struct cred *old = current->cred;
518
David Howellse0e81732009-09-02 09:13:40 +0100519 kdebug("override_creds(%p{%d,%d})", new,
520 atomic_read(&new->usage),
521 read_cred_subscribers(new));
522
523 validate_creds(old);
524 validate_creds(new);
525 get_cred(new);
526 alter_cred_subscribers(new, 1);
527 rcu_assign_pointer(current->cred, new);
528 alter_cred_subscribers(old, -1);
529
530 kdebug("override_creds() = %p{%d,%d}", old,
531 atomic_read(&old->usage),
532 read_cred_subscribers(old));
David Howellsd84f4f92008-11-14 10:39:23 +1100533 return old;
534}
535EXPORT_SYMBOL(override_creds);
536
537/**
David Howells3b11a1d2008-11-14 10:39:26 +1100538 * revert_creds - Revert a temporary subjective credentials override
David Howellsd84f4f92008-11-14 10:39:23 +1100539 * @old: The credentials to be restored
540 *
David Howells3b11a1d2008-11-14 10:39:26 +1100541 * Revert a temporary set of override subjective credentials to an old set,
542 * discarding the override set.
David Howellsd84f4f92008-11-14 10:39:23 +1100543 */
544void revert_creds(const struct cred *old)
545{
546 const struct cred *override = current->cred;
547
David Howellse0e81732009-09-02 09:13:40 +0100548 kdebug("revert_creds(%p{%d,%d})", old,
549 atomic_read(&old->usage),
550 read_cred_subscribers(old));
551
552 validate_creds(old);
553 validate_creds(override);
554 alter_cred_subscribers(old, 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100555 rcu_assign_pointer(current->cred, old);
David Howellse0e81732009-09-02 09:13:40 +0100556 alter_cred_subscribers(override, -1);
David Howellsd84f4f92008-11-14 10:39:23 +1100557 put_cred(override);
558}
559EXPORT_SYMBOL(revert_creds);
560
561/*
562 * initialise the credentials stuff
563 */
564void __init cred_init(void)
565{
566 /* allocate a slab in which we can store credentials */
567 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
568 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
569}
David Howells3a3b7ce2008-11-14 10:39:28 +1100570
571/**
572 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
573 * @daemon: A userspace daemon to be used as a reference
574 *
575 * Prepare a set of credentials for a kernel service. This can then be used to
576 * override a task's own credentials so that work can be done on behalf of that
577 * task that requires a different subjective context.
578 *
579 * @daemon is used to provide a base for the security record, but can be NULL.
580 * If @daemon is supplied, then the security data will be derived from that;
581 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
582 *
583 * The caller may change these controls afterwards if desired.
584 *
585 * Returns the new credentials or NULL if out of memory.
586 *
587 * Does not take, and does not return holding current->cred_replace_mutex.
588 */
589struct cred *prepare_kernel_cred(struct task_struct *daemon)
590{
591 const struct cred *old;
592 struct cred *new;
593
594 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
595 if (!new)
596 return NULL;
597
David Howellse0e81732009-09-02 09:13:40 +0100598 kdebug("prepare_kernel_cred() alloc %p", new);
599
David Howells3a3b7ce2008-11-14 10:39:28 +1100600 if (daemon)
601 old = get_task_cred(daemon);
602 else
603 old = get_cred(&init_cred);
604
David Howellse0e81732009-09-02 09:13:40 +0100605 validate_creds(old);
606
David Howells43529c92009-01-09 16:13:46 +0000607 *new = *old;
Tetsuo Handafb2b2a12011-02-07 13:36:16 +0000608 atomic_set(&new->usage, 1);
609 set_cred_subscribers(new, 0);
David Howells3a3b7ce2008-11-14 10:39:28 +1100610 get_uid(new->user);
Eric W. Biederman0093ccb2011-11-16 21:52:53 -0800611 get_user_ns(new->user_ns);
David Howells3a3b7ce2008-11-14 10:39:28 +1100612 get_group_info(new->group_info);
613
614#ifdef CONFIG_KEYS
David Howells3a505972012-10-02 19:24:29 +0100615 new->session_keyring = NULL;
616 new->process_keyring = NULL;
David Howells3a3b7ce2008-11-14 10:39:28 +1100617 new->thread_keyring = NULL;
David Howells3a505972012-10-02 19:24:29 +0100618 new->request_key_auth = NULL;
David Howells3a3b7ce2008-11-14 10:39:28 +1100619 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
620#endif
621
622#ifdef CONFIG_SECURITY
623 new->security = NULL;
624#endif
625 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
626 goto error;
627
David Howells3a3b7ce2008-11-14 10:39:28 +1100628 put_cred(old);
David Howellse0e81732009-09-02 09:13:40 +0100629 validate_creds(new);
David Howells3a3b7ce2008-11-14 10:39:28 +1100630 return new;
631
632error:
633 put_cred(new);
David Howells0de33682009-01-09 16:13:41 +0000634 put_cred(old);
David Howells3a3b7ce2008-11-14 10:39:28 +1100635 return NULL;
636}
637EXPORT_SYMBOL(prepare_kernel_cred);
638
639/**
640 * set_security_override - Set the security ID in a set of credentials
641 * @new: The credentials to alter
642 * @secid: The LSM security ID to set
643 *
644 * Set the LSM security ID in a set of credentials so that the subjective
645 * security is overridden when an alternative set of credentials is used.
646 */
647int set_security_override(struct cred *new, u32 secid)
648{
649 return security_kernel_act_as(new, secid);
650}
651EXPORT_SYMBOL(set_security_override);
652
653/**
654 * set_security_override_from_ctx - Set the security ID in a set of credentials
655 * @new: The credentials to alter
656 * @secctx: The LSM security context to generate the security ID from.
657 *
658 * Set the LSM security ID in a set of credentials so that the subjective
659 * security is overridden when an alternative set of credentials is used. The
660 * security ID is specified in string form as a security context to be
661 * interpreted by the LSM.
662 */
663int set_security_override_from_ctx(struct cred *new, const char *secctx)
664{
665 u32 secid;
666 int ret;
667
668 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
669 if (ret < 0)
670 return ret;
671
672 return set_security_override(new, secid);
673}
674EXPORT_SYMBOL(set_security_override_from_ctx);
675
676/**
677 * set_create_files_as - Set the LSM file create context in a set of credentials
678 * @new: The credentials to alter
679 * @inode: The inode to take the context from
680 *
681 * Change the LSM file creation context in a set of credentials to be the same
682 * as the object context of the specified inode, so that the new inodes have
683 * the same MAC context as that inode.
684 */
685int set_create_files_as(struct cred *new, struct inode *inode)
686{
687 new->fsuid = inode->i_uid;
688 new->fsgid = inode->i_gid;
689 return security_kernel_create_files_as(new, inode);
690}
691EXPORT_SYMBOL(set_create_files_as);
David Howellse0e81732009-09-02 09:13:40 +0100692
693#ifdef CONFIG_DEBUG_CREDENTIALS
694
Andrew Morton74908a02009-09-17 17:47:12 -0700695bool creds_are_invalid(const struct cred *cred)
696{
697 if (cred->magic != CRED_MAGIC)
698 return true;
Andrew Morton74908a02009-09-17 17:47:12 -0700699#ifdef CONFIG_SECURITY_SELINUX
Tetsuo Handa2edeaa32011-02-07 13:36:10 +0000700 /*
701 * cred->security == NULL if security_cred_alloc_blank() or
702 * security_prepare_creds() returned an error.
703 */
704 if (selinux_is_enabled() && cred->security) {
Andrew Morton74908a02009-09-17 17:47:12 -0700705 if ((unsigned long) cred->security < PAGE_SIZE)
706 return true;
707 if ((*(u32 *)cred->security & 0xffffff00) ==
708 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
709 return true;
710 }
711#endif
712 return false;
713}
Randy Dunlap764db032009-09-18 11:06:47 -0700714EXPORT_SYMBOL(creds_are_invalid);
Andrew Morton74908a02009-09-17 17:47:12 -0700715
David Howellse0e81732009-09-02 09:13:40 +0100716/*
717 * dump invalid credentials
718 */
719static void dump_invalid_creds(const struct cred *cred, const char *label,
720 const struct task_struct *tsk)
721{
722 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
723 label, cred,
724 cred == &init_cred ? "[init]" : "",
725 cred == tsk->real_cred ? "[real]" : "",
726 cred == tsk->cred ? "[eff]" : "");
727 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
728 cred->magic, cred->put_addr);
729 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
730 atomic_read(&cred->usage),
731 read_cred_subscribers(cred));
732 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
Eric W. Biedermanc9235f42012-04-23 17:06:34 -0700733 from_kuid_munged(&init_user_ns, cred->uid),
734 from_kuid_munged(&init_user_ns, cred->euid),
735 from_kuid_munged(&init_user_ns, cred->suid),
736 from_kuid_munged(&init_user_ns, cred->fsuid));
David Howellse0e81732009-09-02 09:13:40 +0100737 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
Eric W. Biedermanc9235f42012-04-23 17:06:34 -0700738 from_kgid_munged(&init_user_ns, cred->gid),
739 from_kgid_munged(&init_user_ns, cred->egid),
740 from_kgid_munged(&init_user_ns, cred->sgid),
741 from_kgid_munged(&init_user_ns, cred->fsgid));
David Howellse0e81732009-09-02 09:13:40 +0100742#ifdef CONFIG_SECURITY
743 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
744 if ((unsigned long) cred->security >= PAGE_SIZE &&
745 (((unsigned long) cred->security & 0xffffff00) !=
746 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
747 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
748 ((u32*)cred->security)[0],
749 ((u32*)cred->security)[1]);
750#endif
751}
752
753/*
754 * report use of invalid credentials
755 */
756void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
757{
758 printk(KERN_ERR "CRED: Invalid credentials\n");
759 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
760 dump_invalid_creds(cred, "Specified", current);
761 BUG();
762}
763EXPORT_SYMBOL(__invalid_creds);
764
765/*
766 * check the credentials on a process
767 */
768void __validate_process_creds(struct task_struct *tsk,
769 const char *file, unsigned line)
770{
771 if (tsk->cred == tsk->real_cred) {
772 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
773 creds_are_invalid(tsk->cred)))
774 goto invalid_creds;
775 } else {
776 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
777 read_cred_subscribers(tsk->cred) < 1 ||
778 creds_are_invalid(tsk->real_cred) ||
779 creds_are_invalid(tsk->cred)))
780 goto invalid_creds;
781 }
782 return;
783
784invalid_creds:
785 printk(KERN_ERR "CRED: Invalid process credentials\n");
786 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
787
788 dump_invalid_creds(tsk->real_cred, "Real", tsk);
789 if (tsk->cred != tsk->real_cred)
790 dump_invalid_creds(tsk->cred, "Effective", tsk);
791 else
792 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
793 BUG();
794}
795EXPORT_SYMBOL(__validate_process_creds);
796
797/*
798 * check creds for do_exit()
799 */
800void validate_creds_for_do_exit(struct task_struct *tsk)
801{
802 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
803 tsk->real_cred, tsk->cred,
804 atomic_read(&tsk->cred->usage),
805 read_cred_subscribers(tsk->cred));
806
807 __validate_process_creds(tsk, __FILE__, __LINE__);
808}
809
810#endif /* CONFIG_DEBUG_CREDENTIALS */