blob: 3a9d6dd53a6cd79696fe88784d27fe3c07c27aaf [file] [log] [blame]
David Howells98870ab2008-11-14 10:39:26 +11001/* Task credentials management - see Documentation/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 */
11#include <linux/module.h>
12#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>
David Howellsd84f4f92008-11-14 10:39:23 +110019#include <linux/cn_proc.h>
David Howellsd84f4f92008-11-14 10:39:23 +110020
David Howellse0e81732009-09-02 09:13:40 +010021#if 0
22#define kdebug(FMT, ...) \
23 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
24#else
David Howellse0e81732009-09-02 09:13:40 +010025#define kdebug(FMT, ...) \
26 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
27#endif
28
David Howellsd84f4f92008-11-14 10:39:23 +110029static struct kmem_cache *cred_jar;
David Howellsf1752ee2008-11-14 10:39:17 +110030
31/*
David Howellsbb952bb2008-11-14 10:39:20 +110032 * The common credentials for the initial task's thread group
33 */
34#ifdef CONFIG_KEYS
35static struct thread_group_cred init_tgcred = {
36 .usage = ATOMIC_INIT(2),
37 .tgid = 0,
38 .lock = SPIN_LOCK_UNLOCKED,
39};
40#endif
41
42/*
David Howellsf1752ee2008-11-14 10:39:17 +110043 * The initial credentials for the initial task
44 */
45struct cred init_cred = {
David Howells3b11a1d2008-11-14 10:39:26 +110046 .usage = ATOMIC_INIT(4),
David Howellse0e81732009-09-02 09:13:40 +010047#ifdef CONFIG_DEBUG_CREDENTIALS
48 .subscribers = ATOMIC_INIT(2),
49 .magic = CRED_MAGIC,
50#endif
David Howellsf1752ee2008-11-14 10:39:17 +110051 .securebits = SECUREBITS_DEFAULT,
52 .cap_inheritable = CAP_INIT_INH_SET,
53 .cap_permitted = CAP_FULL_SET,
54 .cap_effective = CAP_INIT_EFF_SET,
55 .cap_bset = CAP_INIT_BSET,
56 .user = INIT_USER,
57 .group_info = &init_groups,
David Howellsbb952bb2008-11-14 10:39:20 +110058#ifdef CONFIG_KEYS
59 .tgcred = &init_tgcred,
60#endif
David Howellsf1752ee2008-11-14 10:39:17 +110061};
62
David Howellse0e81732009-09-02 09:13:40 +010063static inline void set_cred_subscribers(struct cred *cred, int n)
64{
65#ifdef CONFIG_DEBUG_CREDENTIALS
66 atomic_set(&cred->subscribers, n);
67#endif
68}
69
70static inline int read_cred_subscribers(const struct cred *cred)
71{
72#ifdef CONFIG_DEBUG_CREDENTIALS
73 return atomic_read(&cred->subscribers);
74#else
75 return 0;
76#endif
77}
78
79static inline void alter_cred_subscribers(const struct cred *_cred, int n)
80{
81#ifdef CONFIG_DEBUG_CREDENTIALS
82 struct cred *cred = (struct cred *) _cred;
83
84 atomic_add(n, &cred->subscribers);
85#endif
86}
87
David Howellsf1752ee2008-11-14 10:39:17 +110088/*
David Howellsbb952bb2008-11-14 10:39:20 +110089 * Dispose of the shared task group credentials
90 */
91#ifdef CONFIG_KEYS
92static void release_tgcred_rcu(struct rcu_head *rcu)
93{
94 struct thread_group_cred *tgcred =
95 container_of(rcu, struct thread_group_cred, rcu);
96
97 BUG_ON(atomic_read(&tgcred->usage) != 0);
98
99 key_put(tgcred->session_keyring);
100 key_put(tgcred->process_keyring);
101 kfree(tgcred);
102}
103#endif
104
105/*
106 * Release a set of thread group credentials.
107 */
David Howellsa6f76f22008-11-14 10:39:24 +1100108static void release_tgcred(struct cred *cred)
David Howellsbb952bb2008-11-14 10:39:20 +1100109{
110#ifdef CONFIG_KEYS
111 struct thread_group_cred *tgcred = cred->tgcred;
112
113 if (atomic_dec_and_test(&tgcred->usage))
114 call_rcu(&tgcred->rcu, release_tgcred_rcu);
115#endif
116}
117
118/*
David Howellsf1752ee2008-11-14 10:39:17 +1100119 * The RCU callback to actually dispose of a set of credentials
120 */
121static void put_cred_rcu(struct rcu_head *rcu)
122{
123 struct cred *cred = container_of(rcu, struct cred, rcu);
124
David Howellse0e81732009-09-02 09:13:40 +0100125 kdebug("put_cred_rcu(%p)", cred);
126
127#ifdef CONFIG_DEBUG_CREDENTIALS
128 if (cred->magic != CRED_MAGIC_DEAD ||
129 atomic_read(&cred->usage) != 0 ||
130 read_cred_subscribers(cred) != 0)
131 panic("CRED: put_cred_rcu() sees %p with"
132 " mag %x, put %p, usage %d, subscr %d\n",
133 cred, cred->magic, cred->put_addr,
134 atomic_read(&cred->usage),
135 read_cred_subscribers(cred));
136#else
David Howellsd84f4f92008-11-14 10:39:23 +1100137 if (atomic_read(&cred->usage) != 0)
138 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
139 cred, atomic_read(&cred->usage));
David Howellse0e81732009-09-02 09:13:40 +0100140#endif
David Howellsf1752ee2008-11-14 10:39:17 +1100141
David Howellsd84f4f92008-11-14 10:39:23 +1100142 security_cred_free(cred);
David Howellsf1752ee2008-11-14 10:39:17 +1100143 key_put(cred->thread_keyring);
144 key_put(cred->request_key_auth);
David Howellsbb952bb2008-11-14 10:39:20 +1100145 release_tgcred(cred);
David Howells4a5d6ba2009-09-14 12:45:39 +0100146 if (cred->group_info)
147 put_group_info(cred->group_info);
David Howellsf1752ee2008-11-14 10:39:17 +1100148 free_uid(cred->user);
David Howellsd84f4f92008-11-14 10:39:23 +1100149 kmem_cache_free(cred_jar, cred);
David Howellsf1752ee2008-11-14 10:39:17 +1100150}
151
152/**
153 * __put_cred - Destroy a set of credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100154 * @cred: The record to release
David Howellsf1752ee2008-11-14 10:39:17 +1100155 *
156 * Destroy a set of credentials on which no references remain.
157 */
158void __put_cred(struct cred *cred)
159{
David Howellse0e81732009-09-02 09:13:40 +0100160 kdebug("__put_cred(%p{%d,%d})", cred,
161 atomic_read(&cred->usage),
162 read_cred_subscribers(cred));
163
David Howellsd84f4f92008-11-14 10:39:23 +1100164 BUG_ON(atomic_read(&cred->usage) != 0);
David Howellse0e81732009-09-02 09:13:40 +0100165#ifdef CONFIG_DEBUG_CREDENTIALS
166 BUG_ON(read_cred_subscribers(cred) != 0);
167 cred->magic = CRED_MAGIC_DEAD;
168 cred->put_addr = __builtin_return_address(0);
169#endif
170 BUG_ON(cred == current->cred);
171 BUG_ON(cred == current->real_cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100172
David Howellsf1752ee2008-11-14 10:39:17 +1100173 call_rcu(&cred->rcu, put_cred_rcu);
174}
175EXPORT_SYMBOL(__put_cred);
176
David Howellse0e81732009-09-02 09:13:40 +0100177/*
178 * Clean up a task's credentials when it exits
179 */
180void exit_creds(struct task_struct *tsk)
181{
182 struct cred *cred;
183
184 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
185 atomic_read(&tsk->cred->usage),
186 read_cred_subscribers(tsk->cred));
187
188 cred = (struct cred *) tsk->real_cred;
189 tsk->real_cred = NULL;
190 validate_creds(cred);
191 alter_cred_subscribers(cred, -1);
192 put_cred(cred);
193
194 cred = (struct cred *) tsk->cred;
195 tsk->cred = NULL;
196 validate_creds(cred);
197 alter_cred_subscribers(cred, -1);
198 put_cred(cred);
David Howellsee18d642009-09-02 09:14:21 +0100199
200 cred = (struct cred *) tsk->replacement_session_keyring;
201 if (cred) {
202 tsk->replacement_session_keyring = NULL;
203 validate_creds(cred);
204 put_cred(cred);
205 }
206}
207
David Howellsde09a972010-07-29 12:45:49 +0100208/**
209 * get_task_cred - Get another task's objective credentials
210 * @task: The task to query
211 *
212 * Get the objective credentials of a task, pinning them so that they can't go
213 * away. Accessing a task's credentials directly is not permitted.
214 *
215 * The caller must also make sure task doesn't get deleted, either by holding a
216 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
217 */
218const struct cred *get_task_cred(struct task_struct *task)
219{
220 const struct cred *cred;
221
222 rcu_read_lock();
223
224 do {
225 cred = __task_cred((task));
226 BUG_ON(!cred);
227 } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
228
229 rcu_read_unlock();
230 return cred;
231}
232
David Howellsee18d642009-09-02 09:14:21 +0100233/*
234 * Allocate blank credentials, such that the credentials can be filled in at a
235 * later date without risk of ENOMEM.
236 */
237struct cred *cred_alloc_blank(void)
238{
239 struct cred *new;
240
241 new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
242 if (!new)
243 return NULL;
244
245#ifdef CONFIG_KEYS
246 new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);
247 if (!new->tgcred) {
Julia Lawallb8a1d372010-02-03 09:31:36 +1100248 kmem_cache_free(cred_jar, new);
David Howellsee18d642009-09-02 09:14:21 +0100249 return NULL;
250 }
251 atomic_set(&new->tgcred->usage, 1);
252#endif
253
254 atomic_set(&new->usage, 1);
Tetsuo Handa2edeaa32011-02-07 13:36:10 +0000255#ifdef CONFIG_DEBUG_CREDENTIALS
256 new->magic = CRED_MAGIC;
257#endif
David Howellsee18d642009-09-02 09:14:21 +0100258
259 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
260 goto error;
261
David Howellsee18d642009-09-02 09:14:21 +0100262 return new;
263
264error:
265 abort_creds(new);
266 return NULL;
David Howellse0e81732009-09-02 09:13:40 +0100267}
268
David Howellsd84f4f92008-11-14 10:39:23 +1100269/**
270 * prepare_creds - Prepare a new set of credentials for modification
271 *
272 * Prepare a new set of task credentials for modification. A task's creds
273 * shouldn't generally be modified directly, therefore this function is used to
274 * prepare a new copy, which the caller then modifies and then commits by
275 * calling commit_creds().
276 *
David Howells3b11a1d2008-11-14 10:39:26 +1100277 * Preparation involves making a copy of the objective creds for modification.
278 *
David Howellsd84f4f92008-11-14 10:39:23 +1100279 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
280 *
281 * Call commit_creds() or abort_creds() to clean up.
David Howellsf1752ee2008-11-14 10:39:17 +1100282 */
David Howellsd84f4f92008-11-14 10:39:23 +1100283struct cred *prepare_creds(void)
David Howellsf1752ee2008-11-14 10:39:17 +1100284{
David Howellsd84f4f92008-11-14 10:39:23 +1100285 struct task_struct *task = current;
286 const struct cred *old;
287 struct cred *new;
David Howellsf1752ee2008-11-14 10:39:17 +1100288
David Howellse0e81732009-09-02 09:13:40 +0100289 validate_process_creds();
David Howellsd84f4f92008-11-14 10:39:23 +1100290
291 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
292 if (!new)
293 return NULL;
294
David Howellse0e81732009-09-02 09:13:40 +0100295 kdebug("prepare_creds() alloc %p", new);
296
David Howellsd84f4f92008-11-14 10:39:23 +1100297 old = task->cred;
298 memcpy(new, old, sizeof(struct cred));
299
300 atomic_set(&new->usage, 1);
David Howellse0e81732009-09-02 09:13:40 +0100301 set_cred_subscribers(new, 0);
David Howellsd84f4f92008-11-14 10:39:23 +1100302 get_group_info(new->group_info);
303 get_uid(new->user);
David Howellsf1752ee2008-11-14 10:39:17 +1100304
David Howellsbb952bb2008-11-14 10:39:20 +1100305#ifdef CONFIG_KEYS
David Howellsd84f4f92008-11-14 10:39:23 +1100306 key_get(new->thread_keyring);
307 key_get(new->request_key_auth);
308 atomic_inc(&new->tgcred->usage);
David Howellsbb952bb2008-11-14 10:39:20 +1100309#endif
310
David Howellsf1752ee2008-11-14 10:39:17 +1100311#ifdef CONFIG_SECURITY
David Howellsd84f4f92008-11-14 10:39:23 +1100312 new->security = NULL;
David Howellsf1752ee2008-11-14 10:39:17 +1100313#endif
314
David Howellsd84f4f92008-11-14 10:39:23 +1100315 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
316 goto error;
David Howellse0e81732009-09-02 09:13:40 +0100317 validate_creds(new);
David Howellsd84f4f92008-11-14 10:39:23 +1100318 return new;
319
320error:
321 abort_creds(new);
322 return NULL;
323}
324EXPORT_SYMBOL(prepare_creds);
325
326/*
David Howellsa6f76f22008-11-14 10:39:24 +1100327 * Prepare credentials for current to perform an execve()
KOSAKI Motohiro9b1bf122010-10-27 15:34:08 -0700328 * - The caller must hold ->cred_guard_mutex
David Howellsa6f76f22008-11-14 10:39:24 +1100329 */
330struct cred *prepare_exec_creds(void)
331{
332 struct thread_group_cred *tgcred = NULL;
333 struct cred *new;
334
335#ifdef CONFIG_KEYS
336 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
337 if (!tgcred)
338 return NULL;
339#endif
340
341 new = prepare_creds();
342 if (!new) {
343 kfree(tgcred);
344 return new;
345 }
346
347#ifdef CONFIG_KEYS
348 /* newly exec'd tasks don't get a thread keyring */
349 key_put(new->thread_keyring);
350 new->thread_keyring = NULL;
351
352 /* create a new per-thread-group creds for all this set of threads to
353 * share */
354 memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
355
356 atomic_set(&tgcred->usage, 1);
357 spin_lock_init(&tgcred->lock);
358
359 /* inherit the session keyring; new process keyring */
360 key_get(tgcred->session_keyring);
361 tgcred->process_keyring = NULL;
362
363 release_tgcred(new);
364 new->tgcred = tgcred;
365#endif
366
367 return new;
368}
369
370/*
David Howellsd84f4f92008-11-14 10:39:23 +1100371 * Copy credentials for the new process created by fork()
372 *
373 * We share if we can, but under some circumstances we have to generate a new
374 * set.
David Howells3b11a1d2008-11-14 10:39:26 +1100375 *
376 * The new process gets the current process's subjective credentials as its
377 * objective and subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100378 */
379int copy_creds(struct task_struct *p, unsigned long clone_flags)
380{
381#ifdef CONFIG_KEYS
382 struct thread_group_cred *tgcred;
383#endif
384 struct cred *new;
Serge Hallyn18b6e042008-10-15 16:38:45 -0500385 int ret;
David Howellsd84f4f92008-11-14 10:39:23 +1100386
David Howellsd84f4f92008-11-14 10:39:23 +1100387 if (
388#ifdef CONFIG_KEYS
389 !p->cred->thread_keyring &&
390#endif
391 clone_flags & CLONE_THREAD
392 ) {
David Howells3b11a1d2008-11-14 10:39:26 +1100393 p->real_cred = get_cred(p->cred);
David Howellsd84f4f92008-11-14 10:39:23 +1100394 get_cred(p->cred);
David Howellse0e81732009-09-02 09:13:40 +0100395 alter_cred_subscribers(p->cred, 2);
396 kdebug("share_creds(%p{%d,%d})",
397 p->cred, atomic_read(&p->cred->usage),
398 read_cred_subscribers(p->cred));
David Howellsd84f4f92008-11-14 10:39:23 +1100399 atomic_inc(&p->cred->user->processes);
400 return 0;
David Howellsf1752ee2008-11-14 10:39:17 +1100401 }
402
David Howellsd84f4f92008-11-14 10:39:23 +1100403 new = prepare_creds();
404 if (!new)
405 return -ENOMEM;
David Howellsf1752ee2008-11-14 10:39:17 +1100406
Serge Hallyn18b6e042008-10-15 16:38:45 -0500407 if (clone_flags & CLONE_NEWUSER) {
408 ret = create_user_ns(new);
409 if (ret < 0)
410 goto error_put;
411 }
412
David Howellsd84f4f92008-11-14 10:39:23 +1100413#ifdef CONFIG_KEYS
414 /* new threads get their own thread keyrings if their parent already
415 * had one */
416 if (new->thread_keyring) {
417 key_put(new->thread_keyring);
418 new->thread_keyring = NULL;
419 if (clone_flags & CLONE_THREAD)
420 install_thread_keyring_to_cred(new);
421 }
David Howellsf1752ee2008-11-14 10:39:17 +1100422
David Howellsd84f4f92008-11-14 10:39:23 +1100423 /* we share the process and session keyrings between all the threads in
424 * a process - this is slightly icky as we violate COW credentials a
425 * bit */
426 if (!(clone_flags & CLONE_THREAD)) {
427 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
428 if (!tgcred) {
Serge Hallyn18b6e042008-10-15 16:38:45 -0500429 ret = -ENOMEM;
430 goto error_put;
David Howellsd84f4f92008-11-14 10:39:23 +1100431 }
432 atomic_set(&tgcred->usage, 1);
433 spin_lock_init(&tgcred->lock);
434 tgcred->process_keyring = NULL;
435 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
436
437 release_tgcred(new);
438 new->tgcred = tgcred;
439 }
440#endif
441
442 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100443 p->cred = p->real_cred = get_cred(new);
David Howellse0e81732009-09-02 09:13:40 +0100444 alter_cred_subscribers(new, 2);
445 validate_creds(new);
David Howellsf1752ee2008-11-14 10:39:17 +1100446 return 0;
Serge Hallyn18b6e042008-10-15 16:38:45 -0500447
448error_put:
449 put_cred(new);
450 return ret;
David Howellsf1752ee2008-11-14 10:39:17 +1100451}
David Howellsd84f4f92008-11-14 10:39:23 +1100452
453/**
454 * commit_creds - Install new credentials upon the current task
455 * @new: The credentials to be assigned
456 *
457 * Install a new set of credentials to the current task, using RCU to replace
David Howells3b11a1d2008-11-14 10:39:26 +1100458 * the old set. Both the objective and the subjective credentials pointers are
459 * updated. This function may not be called if the subjective credentials are
460 * in an overridden state.
David Howellsd84f4f92008-11-14 10:39:23 +1100461 *
462 * This function eats the caller's reference to the new credentials.
463 *
464 * Always returns 0 thus allowing this function to be tail-called at the end
465 * of, say, sys_setgid().
466 */
467int commit_creds(struct cred *new)
468{
469 struct task_struct *task = current;
David Howellse0e81732009-09-02 09:13:40 +0100470 const struct cred *old = task->real_cred;
David Howellsd84f4f92008-11-14 10:39:23 +1100471
David Howellse0e81732009-09-02 09:13:40 +0100472 kdebug("commit_creds(%p{%d,%d})", new,
473 atomic_read(&new->usage),
474 read_cred_subscribers(new));
475
476 BUG_ON(task->cred != old);
477#ifdef CONFIG_DEBUG_CREDENTIALS
478 BUG_ON(read_cred_subscribers(old) < 2);
479 validate_creds(old);
480 validate_creds(new);
481#endif
David Howellsd84f4f92008-11-14 10:39:23 +1100482 BUG_ON(atomic_read(&new->usage) < 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100483
David Howells3b11a1d2008-11-14 10:39:26 +1100484 get_cred(new); /* we will require a ref for the subj creds too */
485
David Howellsd84f4f92008-11-14 10:39:23 +1100486 /* dumpability changes */
487 if (old->euid != new->euid ||
488 old->egid != new->egid ||
489 old->fsuid != new->fsuid ||
490 old->fsgid != new->fsgid ||
491 !cap_issubset(new->cap_permitted, old->cap_permitted)) {
David Howellsb9456372009-01-08 11:18:31 +0000492 if (task->mm)
493 set_dumpable(task->mm, suid_dumpable);
David Howellsd84f4f92008-11-14 10:39:23 +1100494 task->pdeath_signal = 0;
495 smp_wmb();
496 }
497
498 /* alter the thread keyring */
499 if (new->fsuid != old->fsuid)
500 key_fsuid_changed(task);
501 if (new->fsgid != old->fsgid)
502 key_fsgid_changed(task);
503
504 /* do it
505 * - What if a process setreuid()'s and this brings the
506 * new uid over his NPROC rlimit? We can check this now
507 * cheaply with the new uid cache, so if it matters
508 * we should be checking for it. -DaveM
509 */
David Howellse0e81732009-09-02 09:13:40 +0100510 alter_cred_subscribers(new, 2);
David Howellsd84f4f92008-11-14 10:39:23 +1100511 if (new->user != old->user)
512 atomic_inc(&new->user->processes);
David Howells3b11a1d2008-11-14 10:39:26 +1100513 rcu_assign_pointer(task->real_cred, new);
David Howellsd84f4f92008-11-14 10:39:23 +1100514 rcu_assign_pointer(task->cred, new);
515 if (new->user != old->user)
516 atomic_dec(&old->user->processes);
David Howellse0e81732009-09-02 09:13:40 +0100517 alter_cred_subscribers(old, -2);
David Howellsd84f4f92008-11-14 10:39:23 +1100518
David Howellsd84f4f92008-11-14 10:39:23 +1100519 /* send notifications */
520 if (new->uid != old->uid ||
521 new->euid != old->euid ||
522 new->suid != old->suid ||
523 new->fsuid != old->fsuid)
524 proc_id_connector(task, PROC_EVENT_UID);
525
526 if (new->gid != old->gid ||
527 new->egid != old->egid ||
528 new->sgid != old->sgid ||
529 new->fsgid != old->fsgid)
530 proc_id_connector(task, PROC_EVENT_GID);
531
David Howells3b11a1d2008-11-14 10:39:26 +1100532 /* release the old obj and subj refs both */
533 put_cred(old);
David Howellsd84f4f92008-11-14 10:39:23 +1100534 put_cred(old);
535 return 0;
536}
537EXPORT_SYMBOL(commit_creds);
538
539/**
540 * abort_creds - Discard a set of credentials and unlock the current task
541 * @new: The credentials that were going to be applied
542 *
543 * Discard a set of credentials that were under construction and unlock the
544 * current task.
545 */
546void abort_creds(struct cred *new)
547{
David Howellse0e81732009-09-02 09:13:40 +0100548 kdebug("abort_creds(%p{%d,%d})", new,
549 atomic_read(&new->usage),
550 read_cred_subscribers(new));
551
552#ifdef CONFIG_DEBUG_CREDENTIALS
553 BUG_ON(read_cred_subscribers(new) != 0);
554#endif
David Howellsd84f4f92008-11-14 10:39:23 +1100555 BUG_ON(atomic_read(&new->usage) < 1);
556 put_cred(new);
557}
558EXPORT_SYMBOL(abort_creds);
559
560/**
David Howells3b11a1d2008-11-14 10:39:26 +1100561 * override_creds - Override the current process's subjective credentials
David Howellsd84f4f92008-11-14 10:39:23 +1100562 * @new: The credentials to be assigned
563 *
David Howells3b11a1d2008-11-14 10:39:26 +1100564 * Install a set of temporary override subjective credentials on the current
565 * process, returning the old set for later reversion.
David Howellsd84f4f92008-11-14 10:39:23 +1100566 */
567const struct cred *override_creds(const struct cred *new)
568{
569 const struct cred *old = current->cred;
570
David Howellse0e81732009-09-02 09:13:40 +0100571 kdebug("override_creds(%p{%d,%d})", new,
572 atomic_read(&new->usage),
573 read_cred_subscribers(new));
574
575 validate_creds(old);
576 validate_creds(new);
577 get_cred(new);
578 alter_cred_subscribers(new, 1);
579 rcu_assign_pointer(current->cred, new);
580 alter_cred_subscribers(old, -1);
581
582 kdebug("override_creds() = %p{%d,%d}", old,
583 atomic_read(&old->usage),
584 read_cred_subscribers(old));
David Howellsd84f4f92008-11-14 10:39:23 +1100585 return old;
586}
587EXPORT_SYMBOL(override_creds);
588
589/**
David Howells3b11a1d2008-11-14 10:39:26 +1100590 * revert_creds - Revert a temporary subjective credentials override
David Howellsd84f4f92008-11-14 10:39:23 +1100591 * @old: The credentials to be restored
592 *
David Howells3b11a1d2008-11-14 10:39:26 +1100593 * Revert a temporary set of override subjective credentials to an old set,
594 * discarding the override set.
David Howellsd84f4f92008-11-14 10:39:23 +1100595 */
596void revert_creds(const struct cred *old)
597{
598 const struct cred *override = current->cred;
599
David Howellse0e81732009-09-02 09:13:40 +0100600 kdebug("revert_creds(%p{%d,%d})", old,
601 atomic_read(&old->usage),
602 read_cred_subscribers(old));
603
604 validate_creds(old);
605 validate_creds(override);
606 alter_cred_subscribers(old, 1);
David Howellsd84f4f92008-11-14 10:39:23 +1100607 rcu_assign_pointer(current->cred, old);
David Howellse0e81732009-09-02 09:13:40 +0100608 alter_cred_subscribers(override, -1);
David Howellsd84f4f92008-11-14 10:39:23 +1100609 put_cred(override);
610}
611EXPORT_SYMBOL(revert_creds);
612
613/*
614 * initialise the credentials stuff
615 */
616void __init cred_init(void)
617{
618 /* allocate a slab in which we can store credentials */
619 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
620 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
621}
David Howells3a3b7ce2008-11-14 10:39:28 +1100622
623/**
624 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
625 * @daemon: A userspace daemon to be used as a reference
626 *
627 * Prepare a set of credentials for a kernel service. This can then be used to
628 * override a task's own credentials so that work can be done on behalf of that
629 * task that requires a different subjective context.
630 *
631 * @daemon is used to provide a base for the security record, but can be NULL.
632 * If @daemon is supplied, then the security data will be derived from that;
633 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
634 *
635 * The caller may change these controls afterwards if desired.
636 *
637 * Returns the new credentials or NULL if out of memory.
638 *
639 * Does not take, and does not return holding current->cred_replace_mutex.
640 */
641struct cred *prepare_kernel_cred(struct task_struct *daemon)
642{
643 const struct cred *old;
644 struct cred *new;
645
646 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
647 if (!new)
648 return NULL;
649
David Howellse0e81732009-09-02 09:13:40 +0100650 kdebug("prepare_kernel_cred() alloc %p", new);
651
David Howells3a3b7ce2008-11-14 10:39:28 +1100652 if (daemon)
653 old = get_task_cred(daemon);
654 else
655 old = get_cred(&init_cred);
656
David Howellse0e81732009-09-02 09:13:40 +0100657 validate_creds(old);
658
David Howells43529c92009-01-09 16:13:46 +0000659 *new = *old;
Tetsuo Handafb2b2a12011-02-07 13:36:16 +0000660 atomic_set(&new->usage, 1);
661 set_cred_subscribers(new, 0);
David Howells3a3b7ce2008-11-14 10:39:28 +1100662 get_uid(new->user);
663 get_group_info(new->group_info);
664
665#ifdef CONFIG_KEYS
666 atomic_inc(&init_tgcred.usage);
667 new->tgcred = &init_tgcred;
668 new->request_key_auth = NULL;
669 new->thread_keyring = NULL;
670 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
671#endif
672
673#ifdef CONFIG_SECURITY
674 new->security = NULL;
675#endif
676 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
677 goto error;
678
David Howells3a3b7ce2008-11-14 10:39:28 +1100679 put_cred(old);
David Howellse0e81732009-09-02 09:13:40 +0100680 validate_creds(new);
David Howells3a3b7ce2008-11-14 10:39:28 +1100681 return new;
682
683error:
684 put_cred(new);
David Howells0de33682009-01-09 16:13:41 +0000685 put_cred(old);
David Howells3a3b7ce2008-11-14 10:39:28 +1100686 return NULL;
687}
688EXPORT_SYMBOL(prepare_kernel_cred);
689
690/**
691 * set_security_override - Set the security ID in a set of credentials
692 * @new: The credentials to alter
693 * @secid: The LSM security ID to set
694 *
695 * Set the LSM security ID in a set of credentials so that the subjective
696 * security is overridden when an alternative set of credentials is used.
697 */
698int set_security_override(struct cred *new, u32 secid)
699{
700 return security_kernel_act_as(new, secid);
701}
702EXPORT_SYMBOL(set_security_override);
703
704/**
705 * set_security_override_from_ctx - Set the security ID in a set of credentials
706 * @new: The credentials to alter
707 * @secctx: The LSM security context to generate the security ID from.
708 *
709 * Set the LSM security ID in a set of credentials so that the subjective
710 * security is overridden when an alternative set of credentials is used. The
711 * security ID is specified in string form as a security context to be
712 * interpreted by the LSM.
713 */
714int set_security_override_from_ctx(struct cred *new, const char *secctx)
715{
716 u32 secid;
717 int ret;
718
719 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
720 if (ret < 0)
721 return ret;
722
723 return set_security_override(new, secid);
724}
725EXPORT_SYMBOL(set_security_override_from_ctx);
726
727/**
728 * set_create_files_as - Set the LSM file create context in a set of credentials
729 * @new: The credentials to alter
730 * @inode: The inode to take the context from
731 *
732 * Change the LSM file creation context in a set of credentials to be the same
733 * as the object context of the specified inode, so that the new inodes have
734 * the same MAC context as that inode.
735 */
736int set_create_files_as(struct cred *new, struct inode *inode)
737{
738 new->fsuid = inode->i_uid;
739 new->fsgid = inode->i_gid;
740 return security_kernel_create_files_as(new, inode);
741}
742EXPORT_SYMBOL(set_create_files_as);
David Howellse0e81732009-09-02 09:13:40 +0100743
744#ifdef CONFIG_DEBUG_CREDENTIALS
745
Andrew Morton74908a02009-09-17 17:47:12 -0700746bool creds_are_invalid(const struct cred *cred)
747{
748 if (cred->magic != CRED_MAGIC)
749 return true;
Andrew Morton74908a02009-09-17 17:47:12 -0700750#ifdef CONFIG_SECURITY_SELINUX
Tetsuo Handa2edeaa32011-02-07 13:36:10 +0000751 /*
752 * cred->security == NULL if security_cred_alloc_blank() or
753 * security_prepare_creds() returned an error.
754 */
755 if (selinux_is_enabled() && cred->security) {
Andrew Morton74908a02009-09-17 17:47:12 -0700756 if ((unsigned long) cred->security < PAGE_SIZE)
757 return true;
758 if ((*(u32 *)cred->security & 0xffffff00) ==
759 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
760 return true;
761 }
762#endif
763 return false;
764}
Randy Dunlap764db032009-09-18 11:06:47 -0700765EXPORT_SYMBOL(creds_are_invalid);
Andrew Morton74908a02009-09-17 17:47:12 -0700766
David Howellse0e81732009-09-02 09:13:40 +0100767/*
768 * dump invalid credentials
769 */
770static void dump_invalid_creds(const struct cred *cred, const char *label,
771 const struct task_struct *tsk)
772{
773 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
774 label, cred,
775 cred == &init_cred ? "[init]" : "",
776 cred == tsk->real_cred ? "[real]" : "",
777 cred == tsk->cred ? "[eff]" : "");
778 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
779 cred->magic, cred->put_addr);
780 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
781 atomic_read(&cred->usage),
782 read_cred_subscribers(cred));
783 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
784 cred->uid, cred->euid, cred->suid, cred->fsuid);
785 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
786 cred->gid, cred->egid, cred->sgid, cred->fsgid);
787#ifdef CONFIG_SECURITY
788 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
789 if ((unsigned long) cred->security >= PAGE_SIZE &&
790 (((unsigned long) cred->security & 0xffffff00) !=
791 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
792 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
793 ((u32*)cred->security)[0],
794 ((u32*)cred->security)[1]);
795#endif
796}
797
798/*
799 * report use of invalid credentials
800 */
801void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
802{
803 printk(KERN_ERR "CRED: Invalid credentials\n");
804 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
805 dump_invalid_creds(cred, "Specified", current);
806 BUG();
807}
808EXPORT_SYMBOL(__invalid_creds);
809
810/*
811 * check the credentials on a process
812 */
813void __validate_process_creds(struct task_struct *tsk,
814 const char *file, unsigned line)
815{
816 if (tsk->cred == tsk->real_cred) {
817 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
818 creds_are_invalid(tsk->cred)))
819 goto invalid_creds;
820 } else {
821 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
822 read_cred_subscribers(tsk->cred) < 1 ||
823 creds_are_invalid(tsk->real_cred) ||
824 creds_are_invalid(tsk->cred)))
825 goto invalid_creds;
826 }
827 return;
828
829invalid_creds:
830 printk(KERN_ERR "CRED: Invalid process credentials\n");
831 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
832
833 dump_invalid_creds(tsk->real_cred, "Real", tsk);
834 if (tsk->cred != tsk->real_cred)
835 dump_invalid_creds(tsk->cred, "Effective", tsk);
836 else
837 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
838 BUG();
839}
840EXPORT_SYMBOL(__validate_process_creds);
841
842/*
843 * check creds for do_exit()
844 */
845void validate_creds_for_do_exit(struct task_struct *tsk)
846{
847 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
848 tsk->real_cred, tsk->cred,
849 atomic_read(&tsk->cred->usage),
850 read_cred_subscribers(tsk->cred));
851
852 __validate_process_creds(tsk, __FILE__, __LINE__);
853}
854
855#endif /* CONFIG_DEBUG_CREDENTIALS */