1 /* Task credentials management - see Documentation/security/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/export.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/binfmts.h>
20 #include <linux/cn_proc.h>
23 #define kdebug(FMT, ...) \
24 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
26 #define kdebug(FMT, ...) \
27 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
30 static struct kmem_cache *cred_jar;
32 /* init to 2 - one for init_task, one to ensure it is never freed */
33 struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
36 * The initial credentials for the initial task
38 struct cred init_cred = {
39 .usage = ATOMIC_INIT(4),
40 #ifdef CONFIG_DEBUG_CREDENTIALS
41 .subscribers = ATOMIC_INIT(2),
44 .uid = GLOBAL_ROOT_UID,
45 .gid = GLOBAL_ROOT_GID,
46 .suid = GLOBAL_ROOT_UID,
47 .sgid = GLOBAL_ROOT_GID,
48 .euid = GLOBAL_ROOT_UID,
49 .egid = GLOBAL_ROOT_GID,
50 .fsuid = GLOBAL_ROOT_UID,
51 .fsgid = GLOBAL_ROOT_GID,
52 .securebits = SECUREBITS_DEFAULT,
53 .cap_inheritable = CAP_EMPTY_SET,
54 .cap_permitted = CAP_FULL_SET,
55 .cap_effective = CAP_FULL_SET,
56 .cap_bset = CAP_FULL_SET,
58 .user_ns = &init_user_ns,
59 .group_info = &init_groups,
62 static inline void set_cred_subscribers(struct cred *cred, int n)
64 #ifdef CONFIG_DEBUG_CREDENTIALS
65 atomic_set(&cred->subscribers, n);
69 static inline int read_cred_subscribers(const struct cred *cred)
71 #ifdef CONFIG_DEBUG_CREDENTIALS
72 return atomic_read(&cred->subscribers);
78 static inline void alter_cred_subscribers(const struct cred *_cred, int n)
80 #ifdef CONFIG_DEBUG_CREDENTIALS
81 struct cred *cred = (struct cred *) _cred;
83 atomic_add(n, &cred->subscribers);
88 * The RCU callback to actually dispose of a set of credentials
90 static void put_cred_rcu(struct rcu_head *rcu)
92 struct cred *cred = container_of(rcu, struct cred, rcu);
94 kdebug("put_cred_rcu(%p)", cred);
96 #ifdef CONFIG_DEBUG_CREDENTIALS
97 if (cred->magic != CRED_MAGIC_DEAD ||
98 atomic_read(&cred->usage) != 0 ||
99 read_cred_subscribers(cred) != 0)
100 panic("CRED: put_cred_rcu() sees %p with"
101 " mag %x, put %p, usage %d, subscr %d\n",
102 cred, cred->magic, cred->put_addr,
103 atomic_read(&cred->usage),
104 read_cred_subscribers(cred));
106 if (atomic_read(&cred->usage) != 0)
107 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
108 cred, atomic_read(&cred->usage));
111 security_cred_free(cred);
112 key_put(cred->session_keyring);
113 key_put(cred->process_keyring);
114 key_put(cred->thread_keyring);
115 key_put(cred->request_key_auth);
116 if (cred->group_info)
117 put_group_info(cred->group_info);
118 free_uid(cred->user);
119 put_user_ns(cred->user_ns);
120 kmem_cache_free(cred_jar, cred);
124 * __put_cred - Destroy a set of credentials
125 * @cred: The record to release
127 * Destroy a set of credentials on which no references remain.
129 void __put_cred(struct cred *cred)
131 kdebug("__put_cred(%p{%d,%d})", cred,
132 atomic_read(&cred->usage),
133 read_cred_subscribers(cred));
135 BUG_ON(atomic_read(&cred->usage) != 0);
136 #ifdef CONFIG_DEBUG_CREDENTIALS
137 BUG_ON(read_cred_subscribers(cred) != 0);
138 cred->magic = CRED_MAGIC_DEAD;
139 cred->put_addr = __builtin_return_address(0);
141 BUG_ON(cred == current->cred);
142 BUG_ON(cred == current->real_cred);
144 call_rcu(&cred->rcu, put_cred_rcu);
146 EXPORT_SYMBOL(__put_cred);
149 * Clean up a task's credentials when it exits
151 void exit_creds(struct task_struct *tsk)
155 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
156 atomic_read(&tsk->cred->usage),
157 read_cred_subscribers(tsk->cred));
159 cred = (struct cred *) tsk->real_cred;
160 tsk->real_cred = NULL;
161 validate_creds(cred);
162 alter_cred_subscribers(cred, -1);
165 cred = (struct cred *) tsk->cred;
167 validate_creds(cred);
168 alter_cred_subscribers(cred, -1);
173 * get_task_cred - Get another task's objective credentials
174 * @task: The task to query
176 * Get the objective credentials of a task, pinning them so that they can't go
177 * away. Accessing a task's credentials directly is not permitted.
179 * The caller must also make sure task doesn't get deleted, either by holding a
180 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
182 const struct cred *get_task_cred(struct task_struct *task)
184 const struct cred *cred;
189 cred = __task_cred((task));
191 } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
198 * Allocate blank credentials, such that the credentials can be filled in at a
199 * later date without risk of ENOMEM.
201 struct cred *cred_alloc_blank(void)
205 new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
209 atomic_set(&new->usage, 1);
210 #ifdef CONFIG_DEBUG_CREDENTIALS
211 new->magic = CRED_MAGIC;
214 if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
225 * prepare_creds - Prepare a new set of credentials for modification
227 * Prepare a new set of task credentials for modification. A task's creds
228 * shouldn't generally be modified directly, therefore this function is used to
229 * prepare a new copy, which the caller then modifies and then commits by
230 * calling commit_creds().
232 * Preparation involves making a copy of the objective creds for modification.
234 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
236 * Call commit_creds() or abort_creds() to clean up.
238 struct cred *prepare_creds(void)
240 struct task_struct *task = current;
241 const struct cred *old;
244 validate_process_creds();
246 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
250 kdebug("prepare_creds() alloc %p", new);
253 memcpy(new, old, sizeof(struct cred));
255 atomic_set(&new->usage, 1);
256 set_cred_subscribers(new, 0);
257 get_group_info(new->group_info);
259 get_user_ns(new->user_ns);
262 key_get(new->session_keyring);
263 key_get(new->process_keyring);
264 key_get(new->thread_keyring);
265 key_get(new->request_key_auth);
268 #ifdef CONFIG_SECURITY
269 new->security = NULL;
272 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
281 EXPORT_SYMBOL(prepare_creds);
284 * Prepare credentials for current to perform an execve()
285 * - The caller must hold ->cred_guard_mutex
287 struct cred *prepare_exec_creds(void)
291 new = prepare_creds();
296 /* newly exec'd tasks don't get a thread keyring */
297 key_put(new->thread_keyring);
298 new->thread_keyring = NULL;
300 /* inherit the session keyring; new process keyring */
301 key_put(new->process_keyring);
302 new->process_keyring = NULL;
309 * Copy credentials for the new process created by fork()
311 * We share if we can, but under some circumstances we have to generate a new
314 * The new process gets the current process's subjective credentials as its
315 * objective and subjective credentials
317 int copy_creds(struct task_struct *p, unsigned long clone_flags)
324 !p->cred->thread_keyring &&
326 clone_flags & CLONE_THREAD
328 p->real_cred = get_cred(p->cred);
330 alter_cred_subscribers(p->cred, 2);
331 kdebug("share_creds(%p{%d,%d})",
332 p->cred, atomic_read(&p->cred->usage),
333 read_cred_subscribers(p->cred));
334 atomic_inc(&p->cred->user->processes);
338 new = prepare_creds();
342 if (clone_flags & CLONE_NEWUSER) {
343 ret = create_user_ns(new);
349 /* new threads get their own thread keyrings if their parent already
351 if (new->thread_keyring) {
352 key_put(new->thread_keyring);
353 new->thread_keyring = NULL;
354 if (clone_flags & CLONE_THREAD)
355 install_thread_keyring_to_cred(new);
358 /* The process keyring is only shared between the threads in a process;
359 * anything outside of those threads doesn't inherit.
361 if (!(clone_flags & CLONE_THREAD)) {
362 key_put(new->process_keyring);
363 new->process_keyring = NULL;
367 atomic_inc(&new->user->processes);
368 p->cred = p->real_cred = get_cred(new);
369 alter_cred_subscribers(new, 2);
378 static bool cred_cap_issubset(const struct cred *set, const struct cred *subset)
380 const struct user_namespace *set_ns = set->user_ns;
381 const struct user_namespace *subset_ns = subset->user_ns;
383 /* If the two credentials are in the same user namespace see if
384 * the capabilities of subset are a subset of set.
386 if (set_ns == subset_ns)
387 return cap_issubset(subset->cap_permitted, set->cap_permitted);
389 /* The credentials are in a different user namespaces
390 * therefore one is a subset of the other only if a set is an
391 * ancestor of subset and set->euid is owner of subset or one
392 * of subsets ancestors.
394 for (;subset_ns != &init_user_ns; subset_ns = subset_ns->parent) {
395 if ((set_ns == subset_ns->parent) &&
396 uid_eq(subset_ns->owner, set->euid))
404 * commit_creds - Install new credentials upon the current task
405 * @new: The credentials to be assigned
407 * Install a new set of credentials to the current task, using RCU to replace
408 * the old set. Both the objective and the subjective credentials pointers are
409 * updated. This function may not be called if the subjective credentials are
410 * in an overridden state.
412 * This function eats the caller's reference to the new credentials.
414 * Always returns 0 thus allowing this function to be tail-called at the end
415 * of, say, sys_setgid().
417 int commit_creds(struct cred *new)
419 struct task_struct *task = current;
420 const struct cred *old = task->real_cred;
422 kdebug("commit_creds(%p{%d,%d})", new,
423 atomic_read(&new->usage),
424 read_cred_subscribers(new));
426 BUG_ON(task->cred != old);
427 #ifdef CONFIG_DEBUG_CREDENTIALS
428 BUG_ON(read_cred_subscribers(old) < 2);
432 BUG_ON(atomic_read(&new->usage) < 1);
434 get_cred(new); /* we will require a ref for the subj creds too */
436 /* dumpability changes */
437 if (!uid_eq(old->euid, new->euid) ||
438 !gid_eq(old->egid, new->egid) ||
439 !uid_eq(old->fsuid, new->fsuid) ||
440 !gid_eq(old->fsgid, new->fsgid) ||
441 !cred_cap_issubset(old, new)) {
443 set_dumpable(task->mm, suid_dumpable);
444 task->pdeath_signal = 0;
448 /* alter the thread keyring */
449 if (!uid_eq(new->fsuid, old->fsuid))
450 key_fsuid_changed(task);
451 if (!gid_eq(new->fsgid, old->fsgid))
452 key_fsgid_changed(task);
455 * RLIMIT_NPROC limits on user->processes have already been checked
458 alter_cred_subscribers(new, 2);
459 if (new->user != old->user)
460 atomic_inc(&new->user->processes);
461 rcu_assign_pointer(task->real_cred, new);
462 rcu_assign_pointer(task->cred, new);
463 if (new->user != old->user)
464 atomic_dec(&old->user->processes);
465 alter_cred_subscribers(old, -2);
467 /* send notifications */
468 if (!uid_eq(new->uid, old->uid) ||
469 !uid_eq(new->euid, old->euid) ||
470 !uid_eq(new->suid, old->suid) ||
471 !uid_eq(new->fsuid, old->fsuid))
472 proc_id_connector(task, PROC_EVENT_UID);
474 if (!gid_eq(new->gid, old->gid) ||
475 !gid_eq(new->egid, old->egid) ||
476 !gid_eq(new->sgid, old->sgid) ||
477 !gid_eq(new->fsgid, old->fsgid))
478 proc_id_connector(task, PROC_EVENT_GID);
480 /* release the old obj and subj refs both */
485 EXPORT_SYMBOL(commit_creds);
488 * abort_creds - Discard a set of credentials and unlock the current task
489 * @new: The credentials that were going to be applied
491 * Discard a set of credentials that were under construction and unlock the
494 void abort_creds(struct cred *new)
496 kdebug("abort_creds(%p{%d,%d})", new,
497 atomic_read(&new->usage),
498 read_cred_subscribers(new));
500 #ifdef CONFIG_DEBUG_CREDENTIALS
501 BUG_ON(read_cred_subscribers(new) != 0);
503 BUG_ON(atomic_read(&new->usage) < 1);
506 EXPORT_SYMBOL(abort_creds);
509 * override_creds - Override the current process's subjective credentials
510 * @new: The credentials to be assigned
512 * Install a set of temporary override subjective credentials on the current
513 * process, returning the old set for later reversion.
515 const struct cred *override_creds(const struct cred *new)
517 const struct cred *old = current->cred;
519 kdebug("override_creds(%p{%d,%d})", new,
520 atomic_read(&new->usage),
521 read_cred_subscribers(new));
526 alter_cred_subscribers(new, 1);
527 rcu_assign_pointer(current->cred, new);
528 alter_cred_subscribers(old, -1);
530 kdebug("override_creds() = %p{%d,%d}", old,
531 atomic_read(&old->usage),
532 read_cred_subscribers(old));
535 EXPORT_SYMBOL(override_creds);
538 * revert_creds - Revert a temporary subjective credentials override
539 * @old: The credentials to be restored
541 * Revert a temporary set of override subjective credentials to an old set,
542 * discarding the override set.
544 void revert_creds(const struct cred *old)
546 const struct cred *override = current->cred;
548 kdebug("revert_creds(%p{%d,%d})", old,
549 atomic_read(&old->usage),
550 read_cred_subscribers(old));
553 validate_creds(override);
554 alter_cred_subscribers(old, 1);
555 rcu_assign_pointer(current->cred, old);
556 alter_cred_subscribers(override, -1);
559 EXPORT_SYMBOL(revert_creds);
562 * initialise the credentials stuff
564 void __init cred_init(void)
566 /* allocate a slab in which we can store credentials */
567 cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
568 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
572 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
573 * @daemon: A userspace daemon to be used as a reference
575 * Prepare a set of credentials for a kernel service. This can then be used to
576 * override a task's own credentials so that work can be done on behalf of that
577 * task that requires a different subjective context.
579 * @daemon is used to provide a base for the security record, but can be NULL.
580 * If @daemon is supplied, then the security data will be derived from that;
581 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
583 * The caller may change these controls afterwards if desired.
585 * Returns the new credentials or NULL if out of memory.
587 * Does not take, and does not return holding current->cred_replace_mutex.
589 struct cred *prepare_kernel_cred(struct task_struct *daemon)
591 const struct cred *old;
594 new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
598 kdebug("prepare_kernel_cred() alloc %p", new);
601 old = get_task_cred(daemon);
603 old = get_cred(&init_cred);
608 atomic_set(&new->usage, 1);
609 set_cred_subscribers(new, 0);
611 get_user_ns(new->user_ns);
612 get_group_info(new->group_info);
615 new->session_keyring = NULL;
616 new->process_keyring = NULL;
617 new->thread_keyring = NULL;
618 new->request_key_auth = NULL;
619 new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
622 #ifdef CONFIG_SECURITY
623 new->security = NULL;
625 if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
637 EXPORT_SYMBOL(prepare_kernel_cred);
640 * set_security_override - Set the security ID in a set of credentials
641 * @new: The credentials to alter
642 * @secid: The LSM security ID to set
644 * Set the LSM security ID in a set of credentials so that the subjective
645 * security is overridden when an alternative set of credentials is used.
647 int set_security_override(struct cred *new, u32 secid)
649 return security_kernel_act_as(new, secid);
651 EXPORT_SYMBOL(set_security_override);
654 * set_security_override_from_ctx - Set the security ID in a set of credentials
655 * @new: The credentials to alter
656 * @secctx: The LSM security context to generate the security ID from.
658 * Set the LSM security ID in a set of credentials so that the subjective
659 * security is overridden when an alternative set of credentials is used. The
660 * security ID is specified in string form as a security context to be
661 * interpreted by the LSM.
663 int set_security_override_from_ctx(struct cred *new, const char *secctx)
668 ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
672 return set_security_override(new, secid);
674 EXPORT_SYMBOL(set_security_override_from_ctx);
677 * set_create_files_as - Set the LSM file create context in a set of credentials
678 * @new: The credentials to alter
679 * @inode: The inode to take the context from
681 * Change the LSM file creation context in a set of credentials to be the same
682 * as the object context of the specified inode, so that the new inodes have
683 * the same MAC context as that inode.
685 int set_create_files_as(struct cred *new, struct inode *inode)
687 new->fsuid = inode->i_uid;
688 new->fsgid = inode->i_gid;
689 return security_kernel_create_files_as(new, inode);
691 EXPORT_SYMBOL(set_create_files_as);
693 #ifdef CONFIG_DEBUG_CREDENTIALS
695 bool creds_are_invalid(const struct cred *cred)
697 if (cred->magic != CRED_MAGIC)
699 #ifdef CONFIG_SECURITY_SELINUX
701 * cred->security == NULL if security_cred_alloc_blank() or
702 * security_prepare_creds() returned an error.
704 if (selinux_is_enabled() && cred->security) {
705 if ((unsigned long) cred->security < PAGE_SIZE)
707 if ((*(u32 *)cred->security & 0xffffff00) ==
708 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
714 EXPORT_SYMBOL(creds_are_invalid);
717 * dump invalid credentials
719 static void dump_invalid_creds(const struct cred *cred, const char *label,
720 const struct task_struct *tsk)
722 printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
724 cred == &init_cred ? "[init]" : "",
725 cred == tsk->real_cred ? "[real]" : "",
726 cred == tsk->cred ? "[eff]" : "");
727 printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
728 cred->magic, cred->put_addr);
729 printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
730 atomic_read(&cred->usage),
731 read_cred_subscribers(cred));
732 printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
733 from_kuid_munged(&init_user_ns, cred->uid),
734 from_kuid_munged(&init_user_ns, cred->euid),
735 from_kuid_munged(&init_user_ns, cred->suid),
736 from_kuid_munged(&init_user_ns, cred->fsuid));
737 printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
738 from_kgid_munged(&init_user_ns, cred->gid),
739 from_kgid_munged(&init_user_ns, cred->egid),
740 from_kgid_munged(&init_user_ns, cred->sgid),
741 from_kgid_munged(&init_user_ns, cred->fsgid));
742 #ifdef CONFIG_SECURITY
743 printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
744 if ((unsigned long) cred->security >= PAGE_SIZE &&
745 (((unsigned long) cred->security & 0xffffff00) !=
746 (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
747 printk(KERN_ERR "CRED: ->security {%x, %x}\n",
748 ((u32*)cred->security)[0],
749 ((u32*)cred->security)[1]);
754 * report use of invalid credentials
756 void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
758 printk(KERN_ERR "CRED: Invalid credentials\n");
759 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
760 dump_invalid_creds(cred, "Specified", current);
763 EXPORT_SYMBOL(__invalid_creds);
766 * check the credentials on a process
768 void __validate_process_creds(struct task_struct *tsk,
769 const char *file, unsigned line)
771 if (tsk->cred == tsk->real_cred) {
772 if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
773 creds_are_invalid(tsk->cred)))
776 if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
777 read_cred_subscribers(tsk->cred) < 1 ||
778 creds_are_invalid(tsk->real_cred) ||
779 creds_are_invalid(tsk->cred)))
785 printk(KERN_ERR "CRED: Invalid process credentials\n");
786 printk(KERN_ERR "CRED: At %s:%u\n", file, line);
788 dump_invalid_creds(tsk->real_cred, "Real", tsk);
789 if (tsk->cred != tsk->real_cred)
790 dump_invalid_creds(tsk->cred, "Effective", tsk);
792 printk(KERN_ERR "CRED: Effective creds == Real creds\n");
795 EXPORT_SYMBOL(__validate_process_creds);
798 * check creds for do_exit()
800 void validate_creds_for_do_exit(struct task_struct *tsk)
802 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
803 tsk->real_cred, tsk->cred,
804 atomic_read(&tsk->cred->usage),
805 read_cred_subscribers(tsk->cred));
807 __validate_process_creds(tsk, __FILE__, __LINE__);
810 #endif /* CONFIG_DEBUG_CREDENTIALS */