]> bbs.cooldavid.org Git - net-next-2.6.git/blame - kernel/cred.c
Add a dummy printk function for the maintenance of unused printks
[net-next-2.6.git] / kernel / cred.c
CommitLineData
98870ab0 1/* Task credentials management - see Documentation/credentials.txt
f1752eec
DH
2 *
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>
5a0e3ad6 13#include <linux/slab.h>
f1752eec
DH
14#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>
d84f4f99 19#include <linux/cn_proc.h>
d84f4f99 20
e0e81739
DH
21#if 0
22#define kdebug(FMT, ...) \
23 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
24#else
e0e81739
DH
25#define kdebug(FMT, ...) \
26 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
27#endif
28
d84f4f99 29static struct kmem_cache *cred_jar;
f1752eec 30
bb952bb9
DH
31/*
32 * 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
f1752eec
DH
42/*
43 * The initial credentials for the initial task
44 */
45struct cred init_cred = {
3b11a1de 46 .usage = ATOMIC_INIT(4),
e0e81739
DH
47#ifdef CONFIG_DEBUG_CREDENTIALS
48 .subscribers = ATOMIC_INIT(2),
49 .magic = CRED_MAGIC,
50#endif
f1752eec
DH
51 .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,
bb952bb9
DH
58#ifdef CONFIG_KEYS
59 .tgcred = &init_tgcred,
60#endif
f1752eec
DH
61};
62
e0e81739
DH
63static 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
bb952bb9
DH
88/*
89 * 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 */
a6f76f23 108static void release_tgcred(struct cred *cred)
bb952bb9
DH
109{
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
f1752eec
DH
118/*
119 * 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
e0e81739
DH
125 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
d84f4f99
DH
137 if (atomic_read(&cred->usage) != 0)
138 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
139 cred, atomic_read(&cred->usage));
e0e81739 140#endif
f1752eec 141
d84f4f99 142 security_cred_free(cred);
f1752eec
DH
143 key_put(cred->thread_keyring);
144 key_put(cred->request_key_auth);
bb952bb9 145 release_tgcred(cred);
4a5d6ba1
DH
146 if (cred->group_info)
147 put_group_info(cred->group_info);
f1752eec 148 free_uid(cred->user);
d84f4f99 149 kmem_cache_free(cred_jar, cred);
f1752eec
DH
150}
151
152/**
153 * __put_cred - Destroy a set of credentials
d84f4f99 154 * @cred: The record to release
f1752eec
DH
155 *
156 * Destroy a set of credentials on which no references remain.
157 */
158void __put_cred(struct cred *cred)
159{
e0e81739
DH
160 kdebug("__put_cred(%p{%d,%d})", cred,
161 atomic_read(&cred->usage),
162 read_cred_subscribers(cred));
163
d84f4f99 164 BUG_ON(atomic_read(&cred->usage) != 0);
e0e81739
DH
165#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);
d84f4f99 172
f1752eec
DH
173 call_rcu(&cred->rcu, put_cred_rcu);
174}
175EXPORT_SYMBOL(__put_cred);
176
e0e81739
DH
177/*
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);
ee18d64c
DH
199
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
de09a977
DH
208/**
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
ee18d64c
DH
233/*
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) {
b8a1d37c 248 kmem_cache_free(cred_jar, new);
ee18d64c
DH
249 return NULL;
250 }
251 atomic_set(&new->tgcred->usage, 1);
252#endif
253
254 atomic_set(&new->usage, 1);
255
256 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
257 goto error;
258
259#ifdef CONFIG_DEBUG_CREDENTIALS
260 new->magic = CRED_MAGIC;
261#endif
262 return new;
263
264error:
265 abort_creds(new);
266 return NULL;
e0e81739
DH
267}
268
d84f4f99
DH
269/**
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 *
3b11a1de
DH
277 * Preparation involves making a copy of the objective creds for modification.
278 *
d84f4f99
DH
279 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
280 *
281 * Call commit_creds() or abort_creds() to clean up.
282 */
283struct cred *prepare_creds(void)
284{
285 struct task_struct *task = current;
286 const struct cred *old;
287 struct cred *new;
288
e0e81739 289 validate_process_creds();
d84f4f99
DH
290
291 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
292 if (!new)
293 return NULL;
294
e0e81739
DH
295 kdebug("prepare_creds() alloc %p", new);
296
d84f4f99
DH
297 old = task->cred;
298 memcpy(new, old, sizeof(struct cred));
299
300 atomic_set(&new->usage, 1);
e0e81739 301 set_cred_subscribers(new, 0);
d84f4f99
DH
302 get_group_info(new->group_info);
303 get_uid(new->user);
304
305#ifdef CONFIG_KEYS
306 key_get(new->thread_keyring);
307 key_get(new->request_key_auth);
308 atomic_inc(&new->tgcred->usage);
309#endif
310
311#ifdef CONFIG_SECURITY
312 new->security = NULL;
313#endif
314
315 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
316 goto error;
e0e81739 317 validate_creds(new);
d84f4f99
DH
318 return new;
319
320error:
321 abort_creds(new);
322 return NULL;
323}
324EXPORT_SYMBOL(prepare_creds);
325
a6f76f23
DH
326/*
327 * Prepare credentials for current to perform an execve()
5e751e99 328 * - The caller must hold current->cred_guard_mutex
a6f76f23
DH
329 */
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
f1752eec
DH
370/*
371 * Copy credentials for the new process created by fork()
d84f4f99
DH
372 *
373 * We share if we can, but under some circumstances we have to generate a new
374 * set.
3b11a1de
DH
375 *
376 * The new process gets the current process's subjective credentials as its
377 * objective and subjective credentials
f1752eec
DH
378 */
379int copy_creds(struct task_struct *p, unsigned long clone_flags)
380{
d84f4f99
DH
381#ifdef CONFIG_KEYS
382 struct thread_group_cred *tgcred;
383#endif
384 struct cred *new;
18b6e041 385 int ret;
d84f4f99 386
5e751e99 387 mutex_init(&p->cred_guard_mutex);
f1752eec 388
d84f4f99
DH
389 if (
390#ifdef CONFIG_KEYS
391 !p->cred->thread_keyring &&
392#endif
393 clone_flags & CLONE_THREAD
394 ) {
3b11a1de 395 p->real_cred = get_cred(p->cred);
d84f4f99 396 get_cred(p->cred);
e0e81739
DH
397 alter_cred_subscribers(p->cred, 2);
398 kdebug("share_creds(%p{%d,%d})",
399 p->cred, atomic_read(&p->cred->usage),
400 read_cred_subscribers(p->cred));
d84f4f99
DH
401 atomic_inc(&p->cred->user->processes);
402 return 0;
403 }
404
405 new = prepare_creds();
406 if (!new)
f1752eec
DH
407 return -ENOMEM;
408
18b6e041
SH
409 if (clone_flags & CLONE_NEWUSER) {
410 ret = create_user_ns(new);
411 if (ret < 0)
412 goto error_put;
413 }
414
bb952bb9 415#ifdef CONFIG_KEYS
d84f4f99
DH
416 /* new threads get their own thread keyrings if their parent already
417 * had one */
418 if (new->thread_keyring) {
419 key_put(new->thread_keyring);
420 new->thread_keyring = NULL;
421 if (clone_flags & CLONE_THREAD)
422 install_thread_keyring_to_cred(new);
423 }
424
425 /* we share the process and session keyrings between all the threads in
426 * a process - this is slightly icky as we violate COW credentials a
427 * bit */
428 if (!(clone_flags & CLONE_THREAD)) {
429 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
430 if (!tgcred) {
18b6e041
SH
431 ret = -ENOMEM;
432 goto error_put;
bb952bb9 433 }
d84f4f99
DH
434 atomic_set(&tgcred->usage, 1);
435 spin_lock_init(&tgcred->lock);
436 tgcred->process_keyring = NULL;
437 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
438
439 release_tgcred(new);
440 new->tgcred = tgcred;
bb952bb9
DH
441 }
442#endif
443
d84f4f99 444 atomic_inc(&new->user->processes);
3b11a1de 445 p->cred = p->real_cred = get_cred(new);
e0e81739
DH
446 alter_cred_subscribers(new, 2);
447 validate_creds(new);
d84f4f99 448 return 0;
18b6e041
SH
449
450error_put:
451 put_cred(new);
452 return ret;
d84f4f99 453}
f1752eec 454
d84f4f99
DH
455/**
456 * commit_creds - Install new credentials upon the current task
457 * @new: The credentials to be assigned
458 *
459 * Install a new set of credentials to the current task, using RCU to replace
3b11a1de
DH
460 * the old set. Both the objective and the subjective credentials pointers are
461 * updated. This function may not be called if the subjective credentials are
462 * in an overridden state.
d84f4f99
DH
463 *
464 * This function eats the caller's reference to the new credentials.
465 *
466 * Always returns 0 thus allowing this function to be tail-called at the end
467 * of, say, sys_setgid().
468 */
469int commit_creds(struct cred *new)
470{
471 struct task_struct *task = current;
e0e81739 472 const struct cred *old = task->real_cred;
d84f4f99 473
e0e81739
DH
474 kdebug("commit_creds(%p{%d,%d})", new,
475 atomic_read(&new->usage),
476 read_cred_subscribers(new));
477
478 BUG_ON(task->cred != old);
479#ifdef CONFIG_DEBUG_CREDENTIALS
480 BUG_ON(read_cred_subscribers(old) < 2);
481 validate_creds(old);
482 validate_creds(new);
483#endif
d84f4f99 484 BUG_ON(atomic_read(&new->usage) < 1);
d84f4f99 485
3b11a1de
DH
486 get_cred(new); /* we will require a ref for the subj creds too */
487
d84f4f99
DH
488 /* dumpability changes */
489 if (old->euid != new->euid ||
490 old->egid != new->egid ||
491 old->fsuid != new->fsuid ||
492 old->fsgid != new->fsgid ||
493 !cap_issubset(new->cap_permitted, old->cap_permitted)) {
b9456371
DH
494 if (task->mm)
495 set_dumpable(task->mm, suid_dumpable);
d84f4f99
DH
496 task->pdeath_signal = 0;
497 smp_wmb();
f1752eec
DH
498 }
499
d84f4f99
DH
500 /* alter the thread keyring */
501 if (new->fsuid != old->fsuid)
502 key_fsuid_changed(task);
503 if (new->fsgid != old->fsgid)
504 key_fsgid_changed(task);
505
506 /* do it
507 * - What if a process setreuid()'s and this brings the
508 * new uid over his NPROC rlimit? We can check this now
509 * cheaply with the new uid cache, so if it matters
510 * we should be checking for it. -DaveM
511 */
e0e81739 512 alter_cred_subscribers(new, 2);
d84f4f99
DH
513 if (new->user != old->user)
514 atomic_inc(&new->user->processes);
3b11a1de 515 rcu_assign_pointer(task->real_cred, new);
d84f4f99
DH
516 rcu_assign_pointer(task->cred, new);
517 if (new->user != old->user)
518 atomic_dec(&old->user->processes);
e0e81739 519 alter_cred_subscribers(old, -2);
d84f4f99 520
d84f4f99
DH
521 /* send notifications */
522 if (new->uid != old->uid ||
523 new->euid != old->euid ||
524 new->suid != old->suid ||
525 new->fsuid != old->fsuid)
526 proc_id_connector(task, PROC_EVENT_UID);
f1752eec 527
d84f4f99
DH
528 if (new->gid != old->gid ||
529 new->egid != old->egid ||
530 new->sgid != old->sgid ||
531 new->fsgid != old->fsgid)
532 proc_id_connector(task, PROC_EVENT_GID);
f1752eec 533
3b11a1de
DH
534 /* release the old obj and subj refs both */
535 put_cred(old);
d84f4f99 536 put_cred(old);
f1752eec
DH
537 return 0;
538}
d84f4f99
DH
539EXPORT_SYMBOL(commit_creds);
540
541/**
542 * abort_creds - Discard a set of credentials and unlock the current task
543 * @new: The credentials that were going to be applied
544 *
545 * Discard a set of credentials that were under construction and unlock the
546 * current task.
547 */
548void abort_creds(struct cred *new)
549{
e0e81739
DH
550 kdebug("abort_creds(%p{%d,%d})", new,
551 atomic_read(&new->usage),
552 read_cred_subscribers(new));
553
554#ifdef CONFIG_DEBUG_CREDENTIALS
555 BUG_ON(read_cred_subscribers(new) != 0);
556#endif
d84f4f99
DH
557 BUG_ON(atomic_read(&new->usage) < 1);
558 put_cred(new);
559}
560EXPORT_SYMBOL(abort_creds);
561
562/**
3b11a1de 563 * override_creds - Override the current process's subjective credentials
d84f4f99
DH
564 * @new: The credentials to be assigned
565 *
3b11a1de
DH
566 * Install a set of temporary override subjective credentials on the current
567 * process, returning the old set for later reversion.
d84f4f99
DH
568 */
569const struct cred *override_creds(const struct cred *new)
570{
571 const struct cred *old = current->cred;
572
e0e81739
DH
573 kdebug("override_creds(%p{%d,%d})", new,
574 atomic_read(&new->usage),
575 read_cred_subscribers(new));
576
577 validate_creds(old);
578 validate_creds(new);
579 get_cred(new);
580 alter_cred_subscribers(new, 1);
581 rcu_assign_pointer(current->cred, new);
582 alter_cred_subscribers(old, -1);
583
584 kdebug("override_creds() = %p{%d,%d}", old,
585 atomic_read(&old->usage),
586 read_cred_subscribers(old));
d84f4f99
DH
587 return old;
588}
589EXPORT_SYMBOL(override_creds);
590
591/**
3b11a1de 592 * revert_creds - Revert a temporary subjective credentials override
d84f4f99
DH
593 * @old: The credentials to be restored
594 *
3b11a1de
DH
595 * Revert a temporary set of override subjective credentials to an old set,
596 * discarding the override set.
d84f4f99
DH
597 */
598void revert_creds(const struct cred *old)
599{
600 const struct cred *override = current->cred;
601
e0e81739
DH
602 kdebug("revert_creds(%p{%d,%d})", old,
603 atomic_read(&old->usage),
604 read_cred_subscribers(old));
605
606 validate_creds(old);
607 validate_creds(override);
608 alter_cred_subscribers(old, 1);
d84f4f99 609 rcu_assign_pointer(current->cred, old);
e0e81739 610 alter_cred_subscribers(override, -1);
d84f4f99
DH
611 put_cred(override);
612}
613EXPORT_SYMBOL(revert_creds);
614
615/*
616 * initialise the credentials stuff
617 */
618void __init cred_init(void)
619{
620 /* allocate a slab in which we can store credentials */
621 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
622 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
623}
3a3b7ce9
DH
624
625/**
626 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
627 * @daemon: A userspace daemon to be used as a reference
628 *
629 * Prepare a set of credentials for a kernel service. This can then be used to
630 * override a task's own credentials so that work can be done on behalf of that
631 * task that requires a different subjective context.
632 *
633 * @daemon is used to provide a base for the security record, but can be NULL.
634 * If @daemon is supplied, then the security data will be derived from that;
635 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
636 *
637 * The caller may change these controls afterwards if desired.
638 *
639 * Returns the new credentials or NULL if out of memory.
640 *
641 * Does not take, and does not return holding current->cred_replace_mutex.
642 */
643struct cred *prepare_kernel_cred(struct task_struct *daemon)
644{
645 const struct cred *old;
646 struct cred *new;
647
648 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
649 if (!new)
650 return NULL;
651
e0e81739
DH
652 kdebug("prepare_kernel_cred() alloc %p", new);
653
3a3b7ce9
DH
654 if (daemon)
655 old = get_task_cred(daemon);
656 else
657 old = get_cred(&init_cred);
658
e0e81739
DH
659 validate_creds(old);
660
43529c97 661 *new = *old;
3a3b7ce9
DH
662 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
679 atomic_set(&new->usage, 1);
e0e81739 680 set_cred_subscribers(new, 0);
3a3b7ce9 681 put_cred(old);
e0e81739 682 validate_creds(new);
3a3b7ce9
DH
683 return new;
684
685error:
686 put_cred(new);
0de33681 687 put_cred(old);
3a3b7ce9
DH
688 return NULL;
689}
690EXPORT_SYMBOL(prepare_kernel_cred);
691
692/**
693 * set_security_override - Set the security ID in a set of credentials
694 * @new: The credentials to alter
695 * @secid: The LSM security ID to set
696 *
697 * Set the LSM security ID in a set of credentials so that the subjective
698 * security is overridden when an alternative set of credentials is used.
699 */
700int set_security_override(struct cred *new, u32 secid)
701{
702 return security_kernel_act_as(new, secid);
703}
704EXPORT_SYMBOL(set_security_override);
705
706/**
707 * set_security_override_from_ctx - Set the security ID in a set of credentials
708 * @new: The credentials to alter
709 * @secctx: The LSM security context to generate the security ID from.
710 *
711 * Set the LSM security ID in a set of credentials so that the subjective
712 * security is overridden when an alternative set of credentials is used. The
713 * security ID is specified in string form as a security context to be
714 * interpreted by the LSM.
715 */
716int set_security_override_from_ctx(struct cred *new, const char *secctx)
717{
718 u32 secid;
719 int ret;
720
721 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
722 if (ret < 0)
723 return ret;
724
725 return set_security_override(new, secid);
726}
727EXPORT_SYMBOL(set_security_override_from_ctx);
728
729/**
730 * set_create_files_as - Set the LSM file create context in a set of credentials
731 * @new: The credentials to alter
732 * @inode: The inode to take the context from
733 *
734 * Change the LSM file creation context in a set of credentials to be the same
735 * as the object context of the specified inode, so that the new inodes have
736 * the same MAC context as that inode.
737 */
738int set_create_files_as(struct cred *new, struct inode *inode)
739{
740 new->fsuid = inode->i_uid;
741 new->fsgid = inode->i_gid;
742 return security_kernel_create_files_as(new, inode);
743}
744EXPORT_SYMBOL(set_create_files_as);
e0e81739
DH
745
746#ifdef CONFIG_DEBUG_CREDENTIALS
747
74908a00
AM
748bool creds_are_invalid(const struct cred *cred)
749{
750 if (cred->magic != CRED_MAGIC)
751 return true;
74908a00
AM
752#ifdef CONFIG_SECURITY_SELINUX
753 if (selinux_is_enabled()) {
754 if ((unsigned long) cred->security < PAGE_SIZE)
755 return true;
756 if ((*(u32 *)cred->security & 0xffffff00) ==
757 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
758 return true;
759 }
760#endif
761 return false;
762}
764db03f 763EXPORT_SYMBOL(creds_are_invalid);
74908a00 764
e0e81739
DH
765/*
766 * dump invalid credentials
767 */
768static void dump_invalid_creds(const struct cred *cred, const char *label,
769 const struct task_struct *tsk)
770{
771 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
772 label, cred,
773 cred == &init_cred ? "[init]" : "",
774 cred == tsk->real_cred ? "[real]" : "",
775 cred == tsk->cred ? "[eff]" : "");
776 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
777 cred->magic, cred->put_addr);
778 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
779 atomic_read(&cred->usage),
780 read_cred_subscribers(cred));
781 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
782 cred->uid, cred->euid, cred->suid, cred->fsuid);
783 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
784 cred->gid, cred->egid, cred->sgid, cred->fsgid);
785#ifdef CONFIG_SECURITY
786 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
787 if ((unsigned long) cred->security >= PAGE_SIZE &&
788 (((unsigned long) cred->security & 0xffffff00) !=
789 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
790 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
791 ((u32*)cred->security)[0],
792 ((u32*)cred->security)[1]);
793#endif
794}
795
796/*
797 * report use of invalid credentials
798 */
799void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
800{
801 printk(KERN_ERR "CRED: Invalid credentials\n");
802 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
803 dump_invalid_creds(cred, "Specified", current);
804 BUG();
805}
806EXPORT_SYMBOL(__invalid_creds);
807
808/*
809 * check the credentials on a process
810 */
811void __validate_process_creds(struct task_struct *tsk,
812 const char *file, unsigned line)
813{
814 if (tsk->cred == tsk->real_cred) {
815 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
816 creds_are_invalid(tsk->cred)))
817 goto invalid_creds;
818 } else {
819 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
820 read_cred_subscribers(tsk->cred) < 1 ||
821 creds_are_invalid(tsk->real_cred) ||
822 creds_are_invalid(tsk->cred)))
823 goto invalid_creds;
824 }
825 return;
826
827invalid_creds:
828 printk(KERN_ERR "CRED: Invalid process credentials\n");
829 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
830
831 dump_invalid_creds(tsk->real_cred, "Real", tsk);
832 if (tsk->cred != tsk->real_cred)
833 dump_invalid_creds(tsk->cred, "Effective", tsk);
834 else
835 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
836 BUG();
837}
838EXPORT_SYMBOL(__validate_process_creds);
839
840/*
841 * check creds for do_exit()
842 */
843void validate_creds_for_do_exit(struct task_struct *tsk)
844{
845 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
846 tsk->real_cred, tsk->cred,
847 atomic_read(&tsk->cred->usage),
848 read_cred_subscribers(tsk->cred));
849
850 __validate_process_creds(tsk, __FILE__, __LINE__);
851}
852
853#endif /* CONFIG_DEBUG_CREDENTIALS */