mmc: sdhci: Check mrq->cmd in sdhci_tasklet_finish
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / cred.c
blobddeba366eaffec19830bd5d668a41fa379d92016
1 /* Task credentials management - see Documentation/credentials.txt
3 * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 #include <linux/module.h>
12 #include <linux/cred.h>
13 #include <linux/sched.h>
14 #include <linux/key.h>
15 #include <linux/keyctl.h>
16 #include <linux/init_task.h>
17 #include <linux/security.h>
18 #include <linux/cn_proc.h>
19 #include "cred-internals.h"
21 #if 0
22 #define kdebug(FMT, ...) \
23 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
24 #else
25 static inline __attribute__((format(printf, 1, 2)))
26 void no_printk(const char *fmt, ...)
29 #define kdebug(FMT, ...) \
30 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
31 #endif
33 static struct kmem_cache *cred_jar;
36 * The common credentials for the initial task's thread group
38 #ifdef CONFIG_KEYS
39 static struct thread_group_cred init_tgcred = {
40 .usage = ATOMIC_INIT(2),
41 .tgid = 0,
42 .lock = SPIN_LOCK_UNLOCKED,
44 #endif
47 * The initial credentials for the initial task
49 struct cred init_cred = {
50 .usage = ATOMIC_INIT(4),
51 #ifdef CONFIG_DEBUG_CREDENTIALS
52 .subscribers = ATOMIC_INIT(2),
53 .magic = CRED_MAGIC,
54 #endif
55 .securebits = SECUREBITS_DEFAULT,
56 .cap_inheritable = CAP_INIT_INH_SET,
57 .cap_permitted = CAP_FULL_SET,
58 .cap_effective = CAP_INIT_EFF_SET,
59 .cap_bset = CAP_INIT_BSET,
60 .user = INIT_USER,
61 .group_info = &init_groups,
62 #ifdef CONFIG_KEYS
63 .tgcred = &init_tgcred,
64 #endif
67 static inline void set_cred_subscribers(struct cred *cred, int n)
69 #ifdef CONFIG_DEBUG_CREDENTIALS
70 atomic_set(&cred->subscribers, n);
71 #endif
74 static inline int read_cred_subscribers(const struct cred *cred)
76 #ifdef CONFIG_DEBUG_CREDENTIALS
77 return atomic_read(&cred->subscribers);
78 #else
79 return 0;
80 #endif
83 static inline void alter_cred_subscribers(const struct cred *_cred, int n)
85 #ifdef CONFIG_DEBUG_CREDENTIALS
86 struct cred *cred = (struct cred *) _cred;
88 atomic_add(n, &cred->subscribers);
89 #endif
93 * Dispose of the shared task group credentials
95 #ifdef CONFIG_KEYS
96 static void release_tgcred_rcu(struct rcu_head *rcu)
98 struct thread_group_cred *tgcred =
99 container_of(rcu, struct thread_group_cred, rcu);
101 BUG_ON(atomic_read(&tgcred->usage) != 0);
103 key_put(tgcred->session_keyring);
104 key_put(tgcred->process_keyring);
105 kfree(tgcred);
107 #endif
110 * Release a set of thread group credentials.
112 static void release_tgcred(struct cred *cred)
114 #ifdef CONFIG_KEYS
115 struct thread_group_cred *tgcred = cred->tgcred;
117 if (atomic_dec_and_test(&tgcred->usage))
118 call_rcu(&tgcred->rcu, release_tgcred_rcu);
119 #endif
123 * The RCU callback to actually dispose of a set of credentials
125 static void put_cred_rcu(struct rcu_head *rcu)
127 struct cred *cred = container_of(rcu, struct cred, rcu);
129 kdebug("put_cred_rcu(%p)", cred);
131 #ifdef CONFIG_DEBUG_CREDENTIALS
132 if (cred->magic != CRED_MAGIC_DEAD ||
133 atomic_read(&cred->usage) != 0 ||
134 read_cred_subscribers(cred) != 0)
135 panic("CRED: put_cred_rcu() sees %p with"
136 " mag %x, put %p, usage %d, subscr %d\n",
137 cred, cred->magic, cred->put_addr,
138 atomic_read(&cred->usage),
139 read_cred_subscribers(cred));
140 #else
141 if (atomic_read(&cred->usage) != 0)
142 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
143 cred, atomic_read(&cred->usage));
144 #endif
146 security_cred_free(cred);
147 key_put(cred->thread_keyring);
148 key_put(cred->request_key_auth);
149 release_tgcred(cred);
150 if (cred->group_info)
151 put_group_info(cred->group_info);
152 free_uid(cred->user);
153 kmem_cache_free(cred_jar, cred);
157 * __put_cred - Destroy a set of credentials
158 * @cred: The record to release
160 * Destroy a set of credentials on which no references remain.
162 void __put_cred(struct cred *cred)
164 kdebug("__put_cred(%p{%d,%d})", cred,
165 atomic_read(&cred->usage),
166 read_cred_subscribers(cred));
168 BUG_ON(atomic_read(&cred->usage) != 0);
169 #ifdef CONFIG_DEBUG_CREDENTIALS
170 BUG_ON(read_cred_subscribers(cred) != 0);
171 cred->magic = CRED_MAGIC_DEAD;
172 cred->put_addr = __builtin_return_address(0);
173 #endif
174 BUG_ON(cred == current->cred);
175 BUG_ON(cred == current->real_cred);
177 call_rcu(&cred->rcu, put_cred_rcu);
179 EXPORT_SYMBOL(__put_cred);
182 * Clean up a task's credentials when it exits
184 void exit_creds(struct task_struct *tsk)
186 struct cred *cred;
188 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
189 atomic_read(&tsk->cred->usage),
190 read_cred_subscribers(tsk->cred));
192 cred = (struct cred *) tsk->real_cred;
193 tsk->real_cred = NULL;
194 validate_creds(cred);
195 alter_cred_subscribers(cred, -1);
196 put_cred(cred);
198 cred = (struct cred *) tsk->cred;
199 tsk->cred = NULL;
200 validate_creds(cred);
201 alter_cred_subscribers(cred, -1);
202 put_cred(cred);
204 cred = (struct cred *) tsk->replacement_session_keyring;
205 if (cred) {
206 tsk->replacement_session_keyring = NULL;
207 validate_creds(cred);
208 put_cred(cred);
213 * get_task_cred - Get another task's objective credentials
214 * @task: The task to query
216 * Get the objective credentials of a task, pinning them so that they can't go
217 * away. Accessing a task's credentials directly is not permitted.
219 * The caller must also make sure task doesn't get deleted, either by holding a
220 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
222 const struct cred *get_task_cred(struct task_struct *task)
224 const struct cred *cred;
226 rcu_read_lock();
228 do {
229 cred = __task_cred((task));
230 BUG_ON(!cred);
231 } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
233 rcu_read_unlock();
234 return cred;
238 * Allocate blank credentials, such that the credentials can be filled in at a
239 * later date without risk of ENOMEM.
241 struct cred *cred_alloc_blank(void)
243 struct cred *new;
245 new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
246 if (!new)
247 return NULL;
249 #ifdef CONFIG_KEYS
250 new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);
251 if (!new->tgcred) {
252 kmem_cache_free(cred_jar, new);
253 return NULL;
255 atomic_set(&new->tgcred->usage, 1);
256 #endif
258 atomic_set(&new->usage, 1);
259 #ifdef CONFIG_DEBUG_CREDENTIALS
260 new->magic = CRED_MAGIC;
261 #endif
263 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
264 goto error;
266 return new;
268 error:
269 abort_creds(new);
270 return NULL;
274 * prepare_creds - Prepare a new set of credentials for modification
276 * Prepare a new set of task credentials for modification. A task's creds
277 * shouldn't generally be modified directly, therefore this function is used to
278 * prepare a new copy, which the caller then modifies and then commits by
279 * calling commit_creds().
281 * Preparation involves making a copy of the objective creds for modification.
283 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
285 * Call commit_creds() or abort_creds() to clean up.
287 struct cred *prepare_creds(void)
289 struct task_struct *task = current;
290 const struct cred *old;
291 struct cred *new;
293 validate_process_creds();
295 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
296 if (!new)
297 return NULL;
299 kdebug("prepare_creds() alloc %p", new);
301 old = task->cred;
302 memcpy(new, old, sizeof(struct cred));
304 atomic_set(&new->usage, 1);
305 set_cred_subscribers(new, 0);
306 get_group_info(new->group_info);
307 get_uid(new->user);
309 #ifdef CONFIG_KEYS
310 key_get(new->thread_keyring);
311 key_get(new->request_key_auth);
312 atomic_inc(&new->tgcred->usage);
313 #endif
315 #ifdef CONFIG_SECURITY
316 new->security = NULL;
317 #endif
319 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
320 goto error;
321 validate_creds(new);
322 return new;
324 error:
325 abort_creds(new);
326 return NULL;
328 EXPORT_SYMBOL(prepare_creds);
331 * Prepare credentials for current to perform an execve()
332 * - The caller must hold current->cred_guard_mutex
334 struct cred *prepare_exec_creds(void)
336 struct thread_group_cred *tgcred = NULL;
337 struct cred *new;
339 #ifdef CONFIG_KEYS
340 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
341 if (!tgcred)
342 return NULL;
343 #endif
345 new = prepare_creds();
346 if (!new) {
347 kfree(tgcred);
348 return new;
351 #ifdef CONFIG_KEYS
352 /* newly exec'd tasks don't get a thread keyring */
353 key_put(new->thread_keyring);
354 new->thread_keyring = NULL;
356 /* create a new per-thread-group creds for all this set of threads to
357 * share */
358 memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
360 atomic_set(&tgcred->usage, 1);
361 spin_lock_init(&tgcred->lock);
363 /* inherit the session keyring; new process keyring */
364 key_get(tgcred->session_keyring);
365 tgcred->process_keyring = NULL;
367 release_tgcred(new);
368 new->tgcred = tgcred;
369 #endif
371 return new;
375 * prepare new credentials for the usermode helper dispatcher
377 struct cred *prepare_usermodehelper_creds(void)
379 #ifdef CONFIG_KEYS
380 struct thread_group_cred *tgcred = NULL;
381 #endif
382 struct cred *new;
384 #ifdef CONFIG_KEYS
385 tgcred = kzalloc(sizeof(*new->tgcred), GFP_ATOMIC);
386 if (!tgcred)
387 return NULL;
388 #endif
390 new = kmem_cache_alloc(cred_jar, GFP_ATOMIC);
391 if (!new)
392 return NULL;
394 kdebug("prepare_usermodehelper_creds() alloc %p", new);
396 memcpy(new, &init_cred, sizeof(struct cred));
398 atomic_set(&new->usage, 1);
399 set_cred_subscribers(new, 0);
400 get_group_info(new->group_info);
401 get_uid(new->user);
403 #ifdef CONFIG_KEYS
404 new->thread_keyring = NULL;
405 new->request_key_auth = NULL;
406 new->jit_keyring = KEY_REQKEY_DEFL_DEFAULT;
408 atomic_set(&tgcred->usage, 1);
409 spin_lock_init(&tgcred->lock);
410 new->tgcred = tgcred;
411 #endif
413 #ifdef CONFIG_SECURITY
414 new->security = NULL;
415 #endif
416 if (security_prepare_creds(new, &init_cred, GFP_ATOMIC) < 0)
417 goto error;
418 validate_creds(new);
420 BUG_ON(atomic_read(&new->usage) != 1);
421 return new;
423 error:
424 put_cred(new);
425 return NULL;
429 * Copy credentials for the new process created by fork()
431 * We share if we can, but under some circumstances we have to generate a new
432 * set.
434 * The new process gets the current process's subjective credentials as its
435 * objective and subjective credentials
437 int copy_creds(struct task_struct *p, unsigned long clone_flags)
439 #ifdef CONFIG_KEYS
440 struct thread_group_cred *tgcred;
441 #endif
442 struct cred *new;
443 int ret;
445 mutex_init(&p->cred_guard_mutex);
447 if (
448 #ifdef CONFIG_KEYS
449 !p->cred->thread_keyring &&
450 #endif
451 clone_flags & CLONE_THREAD
453 p->real_cred = get_cred(p->cred);
454 get_cred(p->cred);
455 alter_cred_subscribers(p->cred, 2);
456 kdebug("share_creds(%p{%d,%d})",
457 p->cred, atomic_read(&p->cred->usage),
458 read_cred_subscribers(p->cred));
459 atomic_inc(&p->cred->user->processes);
460 return 0;
463 new = prepare_creds();
464 if (!new)
465 return -ENOMEM;
467 if (clone_flags & CLONE_NEWUSER) {
468 ret = create_user_ns(new);
469 if (ret < 0)
470 goto error_put;
473 #ifdef CONFIG_KEYS
474 /* new threads get their own thread keyrings if their parent already
475 * had one */
476 if (new->thread_keyring) {
477 key_put(new->thread_keyring);
478 new->thread_keyring = NULL;
479 if (clone_flags & CLONE_THREAD)
480 install_thread_keyring_to_cred(new);
483 /* we share the process and session keyrings between all the threads in
484 * a process - this is slightly icky as we violate COW credentials a
485 * bit */
486 if (!(clone_flags & CLONE_THREAD)) {
487 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
488 if (!tgcred) {
489 ret = -ENOMEM;
490 goto error_put;
492 atomic_set(&tgcred->usage, 1);
493 spin_lock_init(&tgcred->lock);
494 tgcred->process_keyring = NULL;
495 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
497 release_tgcred(new);
498 new->tgcred = tgcred;
500 #endif
502 atomic_inc(&new->user->processes);
503 p->cred = p->real_cred = get_cred(new);
504 alter_cred_subscribers(new, 2);
505 validate_creds(new);
506 return 0;
508 error_put:
509 put_cred(new);
510 return ret;
514 * commit_creds - Install new credentials upon the current task
515 * @new: The credentials to be assigned
517 * Install a new set of credentials to the current task, using RCU to replace
518 * the old set. Both the objective and the subjective credentials pointers are
519 * updated. This function may not be called if the subjective credentials are
520 * in an overridden state.
522 * This function eats the caller's reference to the new credentials.
524 * Always returns 0 thus allowing this function to be tail-called at the end
525 * of, say, sys_setgid().
527 int commit_creds(struct cred *new)
529 struct task_struct *task = current;
530 const struct cred *old = task->real_cred;
532 kdebug("commit_creds(%p{%d,%d})", new,
533 atomic_read(&new->usage),
534 read_cred_subscribers(new));
536 BUG_ON(task->cred != old);
537 #ifdef CONFIG_DEBUG_CREDENTIALS
538 BUG_ON(read_cred_subscribers(old) < 2);
539 validate_creds(old);
540 validate_creds(new);
541 #endif
542 BUG_ON(atomic_read(&new->usage) < 1);
544 security_commit_creds(new, old);
546 get_cred(new); /* we will require a ref for the subj creds too */
548 /* dumpability changes */
549 if (old->euid != new->euid ||
550 old->egid != new->egid ||
551 old->fsuid != new->fsuid ||
552 old->fsgid != new->fsgid ||
553 !cap_issubset(new->cap_permitted, old->cap_permitted)) {
554 if (task->mm)
555 set_dumpable(task->mm, suid_dumpable);
556 task->pdeath_signal = 0;
557 smp_wmb();
560 /* alter the thread keyring */
561 if (new->fsuid != old->fsuid)
562 key_fsuid_changed(task);
563 if (new->fsgid != old->fsgid)
564 key_fsgid_changed(task);
566 /* do it
567 * - What if a process setreuid()'s and this brings the
568 * new uid over his NPROC rlimit? We can check this now
569 * cheaply with the new uid cache, so if it matters
570 * we should be checking for it. -DaveM
572 alter_cred_subscribers(new, 2);
573 if (new->user != old->user)
574 atomic_inc(&new->user->processes);
575 rcu_assign_pointer(task->real_cred, new);
576 rcu_assign_pointer(task->cred, new);
577 if (new->user != old->user)
578 atomic_dec(&old->user->processes);
579 alter_cred_subscribers(old, -2);
581 sched_switch_user(task);
583 /* send notifications */
584 if (new->uid != old->uid ||
585 new->euid != old->euid ||
586 new->suid != old->suid ||
587 new->fsuid != old->fsuid)
588 proc_id_connector(task, PROC_EVENT_UID);
590 if (new->gid != old->gid ||
591 new->egid != old->egid ||
592 new->sgid != old->sgid ||
593 new->fsgid != old->fsgid)
594 proc_id_connector(task, PROC_EVENT_GID);
596 /* release the old obj and subj refs both */
597 put_cred(old);
598 put_cred(old);
599 return 0;
601 EXPORT_SYMBOL(commit_creds);
604 * abort_creds - Discard a set of credentials and unlock the current task
605 * @new: The credentials that were going to be applied
607 * Discard a set of credentials that were under construction and unlock the
608 * current task.
610 void abort_creds(struct cred *new)
612 kdebug("abort_creds(%p{%d,%d})", new,
613 atomic_read(&new->usage),
614 read_cred_subscribers(new));
616 #ifdef CONFIG_DEBUG_CREDENTIALS
617 BUG_ON(read_cred_subscribers(new) != 0);
618 #endif
619 BUG_ON(atomic_read(&new->usage) < 1);
620 put_cred(new);
622 EXPORT_SYMBOL(abort_creds);
625 * override_creds - Override the current process's subjective credentials
626 * @new: The credentials to be assigned
628 * Install a set of temporary override subjective credentials on the current
629 * process, returning the old set for later reversion.
631 const struct cred *override_creds(const struct cred *new)
633 const struct cred *old = current->cred;
635 kdebug("override_creds(%p{%d,%d})", new,
636 atomic_read(&new->usage),
637 read_cred_subscribers(new));
639 validate_creds(old);
640 validate_creds(new);
641 get_cred(new);
642 alter_cred_subscribers(new, 1);
643 rcu_assign_pointer(current->cred, new);
644 alter_cred_subscribers(old, -1);
646 kdebug("override_creds() = %p{%d,%d}", old,
647 atomic_read(&old->usage),
648 read_cred_subscribers(old));
649 return old;
651 EXPORT_SYMBOL(override_creds);
654 * revert_creds - Revert a temporary subjective credentials override
655 * @old: The credentials to be restored
657 * Revert a temporary set of override subjective credentials to an old set,
658 * discarding the override set.
660 void revert_creds(const struct cred *old)
662 const struct cred *override = current->cred;
664 kdebug("revert_creds(%p{%d,%d})", old,
665 atomic_read(&old->usage),
666 read_cred_subscribers(old));
668 validate_creds(old);
669 validate_creds(override);
670 alter_cred_subscribers(old, 1);
671 rcu_assign_pointer(current->cred, old);
672 alter_cred_subscribers(override, -1);
673 put_cred(override);
675 EXPORT_SYMBOL(revert_creds);
678 * initialise the credentials stuff
680 void __init cred_init(void)
682 /* allocate a slab in which we can store credentials */
683 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
684 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
688 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
689 * @daemon: A userspace daemon to be used as a reference
691 * Prepare a set of credentials for a kernel service. This can then be used to
692 * override a task's own credentials so that work can be done on behalf of that
693 * task that requires a different subjective context.
695 * @daemon is used to provide a base for the security record, but can be NULL.
696 * If @daemon is supplied, then the security data will be derived from that;
697 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
699 * The caller may change these controls afterwards if desired.
701 * Returns the new credentials or NULL if out of memory.
703 * Does not take, and does not return holding current->cred_replace_mutex.
705 struct cred *prepare_kernel_cred(struct task_struct *daemon)
707 const struct cred *old;
708 struct cred *new;
710 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
711 if (!new)
712 return NULL;
714 kdebug("prepare_kernel_cred() alloc %p", new);
716 if (daemon)
717 old = get_task_cred(daemon);
718 else
719 old = get_cred(&init_cred);
721 validate_creds(old);
723 *new = *old;
724 atomic_set(&new->usage, 1);
725 set_cred_subscribers(new, 0);
726 get_uid(new->user);
727 get_group_info(new->group_info);
729 #ifdef CONFIG_KEYS
730 atomic_inc(&init_tgcred.usage);
731 new->tgcred = &init_tgcred;
732 new->request_key_auth = NULL;
733 new->thread_keyring = NULL;
734 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
735 #endif
737 #ifdef CONFIG_SECURITY
738 new->security = NULL;
739 #endif
740 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
741 goto error;
743 put_cred(old);
744 validate_creds(new);
745 return new;
747 error:
748 put_cred(new);
749 put_cred(old);
750 return NULL;
752 EXPORT_SYMBOL(prepare_kernel_cred);
755 * set_security_override - Set the security ID in a set of credentials
756 * @new: The credentials to alter
757 * @secid: The LSM security ID to set
759 * Set the LSM security ID in a set of credentials so that the subjective
760 * security is overridden when an alternative set of credentials is used.
762 int set_security_override(struct cred *new, u32 secid)
764 return security_kernel_act_as(new, secid);
766 EXPORT_SYMBOL(set_security_override);
769 * set_security_override_from_ctx - Set the security ID in a set of credentials
770 * @new: The credentials to alter
771 * @secctx: The LSM security context to generate the security ID from.
773 * Set the LSM security ID in a set of credentials so that the subjective
774 * security is overridden when an alternative set of credentials is used. The
775 * security ID is specified in string form as a security context to be
776 * interpreted by the LSM.
778 int set_security_override_from_ctx(struct cred *new, const char *secctx)
780 u32 secid;
781 int ret;
783 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
784 if (ret < 0)
785 return ret;
787 return set_security_override(new, secid);
789 EXPORT_SYMBOL(set_security_override_from_ctx);
792 * set_create_files_as - Set the LSM file create context in a set of credentials
793 * @new: The credentials to alter
794 * @inode: The inode to take the context from
796 * Change the LSM file creation context in a set of credentials to be the same
797 * as the object context of the specified inode, so that the new inodes have
798 * the same MAC context as that inode.
800 int set_create_files_as(struct cred *new, struct inode *inode)
802 new->fsuid = inode->i_uid;
803 new->fsgid = inode->i_gid;
804 return security_kernel_create_files_as(new, inode);
806 EXPORT_SYMBOL(set_create_files_as);
808 #ifdef CONFIG_DEBUG_CREDENTIALS
810 bool creds_are_invalid(const struct cred *cred)
812 if (cred->magic != CRED_MAGIC)
813 return true;
814 #ifdef CONFIG_SECURITY_SELINUX
816 * cred->security == NULL if security_cred_alloc_blank() or
817 * security_prepare_creds() returned an error.
819 if (selinux_is_enabled() && cred->security) {
820 if ((unsigned long) cred->security < PAGE_SIZE)
821 return true;
822 if ((*(u32 *)cred->security & 0xffffff00) ==
823 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
824 return true;
826 #endif
827 return false;
829 EXPORT_SYMBOL(creds_are_invalid);
832 * dump invalid credentials
834 static void dump_invalid_creds(const struct cred *cred, const char *label,
835 const struct task_struct *tsk)
837 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
838 label, cred,
839 cred == &init_cred ? "[init]" : "",
840 cred == tsk->real_cred ? "[real]" : "",
841 cred == tsk->cred ? "[eff]" : "");
842 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
843 cred->magic, cred->put_addr);
844 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
845 atomic_read(&cred->usage),
846 read_cred_subscribers(cred));
847 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
848 cred->uid, cred->euid, cred->suid, cred->fsuid);
849 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
850 cred->gid, cred->egid, cred->sgid, cred->fsgid);
851 #ifdef CONFIG_SECURITY
852 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
853 if ((unsigned long) cred->security >= PAGE_SIZE &&
854 (((unsigned long) cred->security & 0xffffff00) !=
855 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
856 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
857 ((u32*)cred->security)[0],
858 ((u32*)cred->security)[1]);
859 #endif
863 * report use of invalid credentials
865 void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
867 printk(KERN_ERR "CRED: Invalid credentials\n");
868 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
869 dump_invalid_creds(cred, "Specified", current);
870 BUG();
872 EXPORT_SYMBOL(__invalid_creds);
875 * check the credentials on a process
877 void __validate_process_creds(struct task_struct *tsk,
878 const char *file, unsigned line)
880 if (tsk->cred == tsk->real_cred) {
881 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
882 creds_are_invalid(tsk->cred)))
883 goto invalid_creds;
884 } else {
885 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
886 read_cred_subscribers(tsk->cred) < 1 ||
887 creds_are_invalid(tsk->real_cred) ||
888 creds_are_invalid(tsk->cred)))
889 goto invalid_creds;
891 return;
893 invalid_creds:
894 printk(KERN_ERR "CRED: Invalid process credentials\n");
895 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
897 dump_invalid_creds(tsk->real_cred, "Real", tsk);
898 if (tsk->cred != tsk->real_cred)
899 dump_invalid_creds(tsk->cred, "Effective", tsk);
900 else
901 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
902 BUG();
904 EXPORT_SYMBOL(__validate_process_creds);
907 * check creds for do_exit()
909 void validate_creds_for_do_exit(struct task_struct *tsk)
911 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
912 tsk->real_cred, tsk->cred,
913 atomic_read(&tsk->cred->usage),
914 read_cred_subscribers(tsk->cred));
916 __validate_process_creds(tsk, __FILE__, __LINE__);
919 #endif /* CONFIG_DEBUG_CREDENTIALS */