Import all changes from 2.6.32.60 commit 9f875a0d53411994c357a361352535370369b996
[linux-2.6.34.14-moxart.git] / kernel / cred.c
blobaa66d6f69f21db44a73076fa75f5ee44c6642d74
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/slab.h>
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>
19 #include <linux/cn_proc.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 goto free_tgcred;
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;
427 free_tgcred:
428 #ifdef CONFIG_KEYS
429 kfree(tgcred);
430 #endif
431 return NULL;
435 * Copy credentials for the new process created by fork()
437 * We share if we can, but under some circumstances we have to generate a new
438 * set.
440 * The new process gets the current process's subjective credentials as its
441 * objective and subjective credentials
443 int copy_creds(struct task_struct *p, unsigned long clone_flags)
445 #ifdef CONFIG_KEYS
446 struct thread_group_cred *tgcred;
447 #endif
448 struct cred *new;
449 int ret;
451 mutex_init(&p->cred_guard_mutex);
453 if (
454 #ifdef CONFIG_KEYS
455 !p->cred->thread_keyring &&
456 #endif
457 clone_flags & CLONE_THREAD
459 p->real_cred = get_cred(p->cred);
460 get_cred(p->cred);
461 alter_cred_subscribers(p->cred, 2);
462 kdebug("share_creds(%p{%d,%d})",
463 p->cred, atomic_read(&p->cred->usage),
464 read_cred_subscribers(p->cred));
465 atomic_inc(&p->cred->user->processes);
466 return 0;
469 new = prepare_creds();
470 if (!new)
471 return -ENOMEM;
473 if (clone_flags & CLONE_NEWUSER) {
474 ret = create_user_ns(new);
475 if (ret < 0)
476 goto error_put;
479 #ifdef CONFIG_KEYS
480 /* new threads get their own thread keyrings if their parent already
481 * had one */
482 if (new->thread_keyring) {
483 key_put(new->thread_keyring);
484 new->thread_keyring = NULL;
485 if (clone_flags & CLONE_THREAD)
486 install_thread_keyring_to_cred(new);
489 /* we share the process and session keyrings between all the threads in
490 * a process - this is slightly icky as we violate COW credentials a
491 * bit */
492 if (!(clone_flags & CLONE_THREAD)) {
493 tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
494 if (!tgcred) {
495 ret = -ENOMEM;
496 goto error_put;
498 atomic_set(&tgcred->usage, 1);
499 spin_lock_init(&tgcred->lock);
500 tgcred->process_keyring = NULL;
501 tgcred->session_keyring = key_get(new->tgcred->session_keyring);
503 release_tgcred(new);
504 new->tgcred = tgcred;
506 #endif
508 atomic_inc(&new->user->processes);
509 p->cred = p->real_cred = get_cred(new);
510 alter_cred_subscribers(new, 2);
511 validate_creds(new);
512 return 0;
514 error_put:
515 put_cred(new);
516 return ret;
520 * commit_creds - Install new credentials upon the current task
521 * @new: The credentials to be assigned
523 * Install a new set of credentials to the current task, using RCU to replace
524 * the old set. Both the objective and the subjective credentials pointers are
525 * updated. This function may not be called if the subjective credentials are
526 * in an overridden state.
528 * This function eats the caller's reference to the new credentials.
530 * Always returns 0 thus allowing this function to be tail-called at the end
531 * of, say, sys_setgid().
533 int commit_creds(struct cred *new)
535 struct task_struct *task = current;
536 const struct cred *old = task->real_cred;
538 kdebug("commit_creds(%p{%d,%d})", new,
539 atomic_read(&new->usage),
540 read_cred_subscribers(new));
542 BUG_ON(task->cred != old);
543 #ifdef CONFIG_DEBUG_CREDENTIALS
544 BUG_ON(read_cred_subscribers(old) < 2);
545 validate_creds(old);
546 validate_creds(new);
547 #endif
548 BUG_ON(atomic_read(&new->usage) < 1);
550 security_commit_creds(new, old);
552 get_cred(new); /* we will require a ref for the subj creds too */
554 /* dumpability changes */
555 if (old->euid != new->euid ||
556 old->egid != new->egid ||
557 old->fsuid != new->fsuid ||
558 old->fsgid != new->fsgid ||
559 !cap_issubset(new->cap_permitted, old->cap_permitted)) {
560 if (task->mm)
561 set_dumpable(task->mm, suid_dumpable);
562 task->pdeath_signal = 0;
563 smp_wmb();
566 /* alter the thread keyring */
567 if (new->fsuid != old->fsuid)
568 key_fsuid_changed(task);
569 if (new->fsgid != old->fsgid)
570 key_fsgid_changed(task);
572 /* do it
573 * - What if a process setreuid()'s and this brings the
574 * new uid over his NPROC rlimit? We can check this now
575 * cheaply with the new uid cache, so if it matters
576 * we should be checking for it. -DaveM
578 alter_cred_subscribers(new, 2);
579 if (new->user != old->user)
580 atomic_inc(&new->user->processes);
581 rcu_assign_pointer(task->real_cred, new);
582 rcu_assign_pointer(task->cred, new);
583 if (new->user != old->user)
584 atomic_dec(&old->user->processes);
585 alter_cred_subscribers(old, -2);
587 /* send notifications */
588 if (new->uid != old->uid ||
589 new->euid != old->euid ||
590 new->suid != old->suid ||
591 new->fsuid != old->fsuid)
592 proc_id_connector(task, PROC_EVENT_UID);
594 if (new->gid != old->gid ||
595 new->egid != old->egid ||
596 new->sgid != old->sgid ||
597 new->fsgid != old->fsgid)
598 proc_id_connector(task, PROC_EVENT_GID);
600 /* release the old obj and subj refs both */
601 put_cred(old);
602 put_cred(old);
603 return 0;
605 EXPORT_SYMBOL(commit_creds);
608 * abort_creds - Discard a set of credentials and unlock the current task
609 * @new: The credentials that were going to be applied
611 * Discard a set of credentials that were under construction and unlock the
612 * current task.
614 void abort_creds(struct cred *new)
616 kdebug("abort_creds(%p{%d,%d})", new,
617 atomic_read(&new->usage),
618 read_cred_subscribers(new));
620 #ifdef CONFIG_DEBUG_CREDENTIALS
621 BUG_ON(read_cred_subscribers(new) != 0);
622 #endif
623 BUG_ON(atomic_read(&new->usage) < 1);
624 put_cred(new);
626 EXPORT_SYMBOL(abort_creds);
629 * override_creds - Override the current process's subjective credentials
630 * @new: The credentials to be assigned
632 * Install a set of temporary override subjective credentials on the current
633 * process, returning the old set for later reversion.
635 const struct cred *override_creds(const struct cred *new)
637 const struct cred *old = current->cred;
639 kdebug("override_creds(%p{%d,%d})", new,
640 atomic_read(&new->usage),
641 read_cred_subscribers(new));
643 validate_creds(old);
644 validate_creds(new);
645 get_cred(new);
646 alter_cred_subscribers(new, 1);
647 rcu_assign_pointer(current->cred, new);
648 alter_cred_subscribers(old, -1);
650 kdebug("override_creds() = %p{%d,%d}", old,
651 atomic_read(&old->usage),
652 read_cred_subscribers(old));
653 return old;
655 EXPORT_SYMBOL(override_creds);
658 * revert_creds - Revert a temporary subjective credentials override
659 * @old: The credentials to be restored
661 * Revert a temporary set of override subjective credentials to an old set,
662 * discarding the override set.
664 void revert_creds(const struct cred *old)
666 const struct cred *override = current->cred;
668 kdebug("revert_creds(%p{%d,%d})", old,
669 atomic_read(&old->usage),
670 read_cred_subscribers(old));
672 validate_creds(old);
673 validate_creds(override);
674 alter_cred_subscribers(old, 1);
675 rcu_assign_pointer(current->cred, old);
676 alter_cred_subscribers(override, -1);
677 put_cred(override);
679 EXPORT_SYMBOL(revert_creds);
682 * initialise the credentials stuff
684 void __init cred_init(void)
686 /* allocate a slab in which we can store credentials */
687 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
688 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
692 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
693 * @daemon: A userspace daemon to be used as a reference
695 * Prepare a set of credentials for a kernel service. This can then be used to
696 * override a task's own credentials so that work can be done on behalf of that
697 * task that requires a different subjective context.
699 * @daemon is used to provide a base for the security record, but can be NULL.
700 * If @daemon is supplied, then the security data will be derived from that;
701 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
703 * The caller may change these controls afterwards if desired.
705 * Returns the new credentials or NULL if out of memory.
707 * Does not take, and does not return holding current->cred_replace_mutex.
709 struct cred *prepare_kernel_cred(struct task_struct *daemon)
711 const struct cred *old;
712 struct cred *new;
714 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
715 if (!new)
716 return NULL;
718 kdebug("prepare_kernel_cred() alloc %p", new);
720 if (daemon)
721 old = get_task_cred(daemon);
722 else
723 old = get_cred(&init_cred);
725 validate_creds(old);
727 *new = *old;
728 atomic_set(&new->usage, 1);
729 set_cred_subscribers(new, 0);
730 get_uid(new->user);
731 get_group_info(new->group_info);
733 #ifdef CONFIG_KEYS
734 atomic_inc(&init_tgcred.usage);
735 new->tgcred = &init_tgcred;
736 new->request_key_auth = NULL;
737 new->thread_keyring = NULL;
738 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
739 #endif
741 #ifdef CONFIG_SECURITY
742 new->security = NULL;
743 #endif
744 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
745 goto error;
747 put_cred(old);
748 validate_creds(new);
749 return new;
751 error:
752 put_cred(new);
753 put_cred(old);
754 return NULL;
756 EXPORT_SYMBOL(prepare_kernel_cred);
759 * set_security_override - Set the security ID in a set of credentials
760 * @new: The credentials to alter
761 * @secid: The LSM security ID to set
763 * Set the LSM security ID in a set of credentials so that the subjective
764 * security is overridden when an alternative set of credentials is used.
766 int set_security_override(struct cred *new, u32 secid)
768 return security_kernel_act_as(new, secid);
770 EXPORT_SYMBOL(set_security_override);
773 * set_security_override_from_ctx - Set the security ID in a set of credentials
774 * @new: The credentials to alter
775 * @secctx: The LSM security context to generate the security ID from.
777 * Set the LSM security ID in a set of credentials so that the subjective
778 * security is overridden when an alternative set of credentials is used. The
779 * security ID is specified in string form as a security context to be
780 * interpreted by the LSM.
782 int set_security_override_from_ctx(struct cred *new, const char *secctx)
784 u32 secid;
785 int ret;
787 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
788 if (ret < 0)
789 return ret;
791 return set_security_override(new, secid);
793 EXPORT_SYMBOL(set_security_override_from_ctx);
796 * set_create_files_as - Set the LSM file create context in a set of credentials
797 * @new: The credentials to alter
798 * @inode: The inode to take the context from
800 * Change the LSM file creation context in a set of credentials to be the same
801 * as the object context of the specified inode, so that the new inodes have
802 * the same MAC context as that inode.
804 int set_create_files_as(struct cred *new, struct inode *inode)
806 new->fsuid = inode->i_uid;
807 new->fsgid = inode->i_gid;
808 return security_kernel_create_files_as(new, inode);
810 EXPORT_SYMBOL(set_create_files_as);
812 #ifdef CONFIG_DEBUG_CREDENTIALS
814 bool creds_are_invalid(const struct cred *cred)
816 if (cred->magic != CRED_MAGIC)
817 return true;
818 #ifdef CONFIG_SECURITY_SELINUX
820 * cred->security == NULL if security_cred_alloc_blank() or
821 * security_prepare_creds() returned an error.
823 if (selinux_is_enabled() && cred->security) {
824 if ((unsigned long) cred->security < PAGE_SIZE)
825 return true;
826 if ((*(u32 *)cred->security & 0xffffff00) ==
827 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
828 return true;
830 #endif
831 return false;
833 EXPORT_SYMBOL(creds_are_invalid);
836 * dump invalid credentials
838 static void dump_invalid_creds(const struct cred *cred, const char *label,
839 const struct task_struct *tsk)
841 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
842 label, cred,
843 cred == &init_cred ? "[init]" : "",
844 cred == tsk->real_cred ? "[real]" : "",
845 cred == tsk->cred ? "[eff]" : "");
846 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
847 cred->magic, cred->put_addr);
848 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
849 atomic_read(&cred->usage),
850 read_cred_subscribers(cred));
851 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
852 cred->uid, cred->euid, cred->suid, cred->fsuid);
853 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
854 cred->gid, cred->egid, cred->sgid, cred->fsgid);
855 #ifdef CONFIG_SECURITY
856 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
857 if ((unsigned long) cred->security >= PAGE_SIZE &&
858 (((unsigned long) cred->security & 0xffffff00) !=
859 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
860 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
861 ((u32*)cred->security)[0],
862 ((u32*)cred->security)[1]);
863 #endif
867 * report use of invalid credentials
869 void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
871 printk(KERN_ERR "CRED: Invalid credentials\n");
872 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
873 dump_invalid_creds(cred, "Specified", current);
874 BUG();
876 EXPORT_SYMBOL(__invalid_creds);
879 * check the credentials on a process
881 void __validate_process_creds(struct task_struct *tsk,
882 const char *file, unsigned line)
884 if (tsk->cred == tsk->real_cred) {
885 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
886 creds_are_invalid(tsk->cred)))
887 goto invalid_creds;
888 } else {
889 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
890 read_cred_subscribers(tsk->cred) < 1 ||
891 creds_are_invalid(tsk->real_cred) ||
892 creds_are_invalid(tsk->cred)))
893 goto invalid_creds;
895 return;
897 invalid_creds:
898 printk(KERN_ERR "CRED: Invalid process credentials\n");
899 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
901 dump_invalid_creds(tsk->real_cred, "Real", tsk);
902 if (tsk->cred != tsk->real_cred)
903 dump_invalid_creds(tsk->cred, "Effective", tsk);
904 else
905 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
906 BUG();
908 EXPORT_SYMBOL(__validate_process_creds);
911 * check creds for do_exit()
913 void validate_creds_for_do_exit(struct task_struct *tsk)
915 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
916 tsk->real_cred, tsk->cred,
917 atomic_read(&tsk->cred->usage),
918 read_cred_subscribers(tsk->cred));
920 __validate_process_creds(tsk, __FILE__, __LINE__);
923 #endif /* CONFIG_DEBUG_CREDENTIALS */