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>
22 #define kdebug(FMT, ...) \
23 printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
25 #define kdebug(FMT, ...) \
26 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
29 static struct kmem_cache
*cred_jar
;
32 * The common credentials for the initial task's thread group
35 static struct thread_group_cred init_tgcred
= {
36 .usage
= ATOMIC_INIT(2),
38 .lock
= __SPIN_LOCK_UNLOCKED(init_cred
.tgcred
.lock
),
43 * The initial credentials for the initial task
45 struct cred init_cred
= {
46 .usage
= ATOMIC_INIT(4),
47 #ifdef CONFIG_DEBUG_CREDENTIALS
48 .subscribers
= ATOMIC_INIT(2),
51 .securebits
= SECUREBITS_DEFAULT
,
52 .cap_inheritable
= CAP_EMPTY_SET
,
53 .cap_permitted
= CAP_FULL_SET
,
54 .cap_effective
= CAP_FULL_SET
,
55 .cap_bset
= CAP_FULL_SET
,
57 .group_info
= &init_groups
,
59 .tgcred
= &init_tgcred
,
63 static inline void set_cred_subscribers(struct cred
*cred
, int n
)
65 #ifdef CONFIG_DEBUG_CREDENTIALS
66 atomic_set(&cred
->subscribers
, n
);
70 static inline int read_cred_subscribers(const struct cred
*cred
)
72 #ifdef CONFIG_DEBUG_CREDENTIALS
73 return atomic_read(&cred
->subscribers
);
79 static inline void alter_cred_subscribers(const struct cred
*_cred
, int n
)
81 #ifdef CONFIG_DEBUG_CREDENTIALS
82 struct cred
*cred
= (struct cred
*) _cred
;
84 atomic_add(n
, &cred
->subscribers
);
89 * Dispose of the shared task group credentials
92 static void release_tgcred_rcu(struct rcu_head
*rcu
)
94 struct thread_group_cred
*tgcred
=
95 container_of(rcu
, struct thread_group_cred
, rcu
);
97 BUG_ON(atomic_read(&tgcred
->usage
) != 0);
99 key_put(tgcred
->session_keyring
);
100 key_put(tgcred
->process_keyring
);
106 * Release a set of thread group credentials.
108 static void release_tgcred(struct cred
*cred
)
111 struct thread_group_cred
*tgcred
= cred
->tgcred
;
113 if (atomic_dec_and_test(&tgcred
->usage
))
114 call_rcu(&tgcred
->rcu
, release_tgcred_rcu
);
119 * The RCU callback to actually dispose of a set of credentials
121 static void put_cred_rcu(struct rcu_head
*rcu
)
123 struct cred
*cred
= container_of(rcu
, struct cred
, rcu
);
125 kdebug("put_cred_rcu(%p)", cred
);
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
));
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
));
142 security_cred_free(cred
);
143 key_put(cred
->thread_keyring
);
144 key_put(cred
->request_key_auth
);
145 release_tgcred(cred
);
146 if (cred
->group_info
)
147 put_group_info(cred
->group_info
);
148 free_uid(cred
->user
);
149 kmem_cache_free(cred_jar
, cred
);
153 * __put_cred - Destroy a set of credentials
154 * @cred: The record to release
156 * Destroy a set of credentials on which no references remain.
158 void __put_cred(struct cred
*cred
)
160 kdebug("__put_cred(%p{%d,%d})", cred
,
161 atomic_read(&cred
->usage
),
162 read_cred_subscribers(cred
));
164 BUG_ON(atomic_read(&cred
->usage
) != 0);
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);
170 BUG_ON(cred
== current
->cred
);
171 BUG_ON(cred
== current
->real_cred
);
173 call_rcu(&cred
->rcu
, put_cred_rcu
);
175 EXPORT_SYMBOL(__put_cred
);
178 * Clean up a task's credentials when it exits
180 void exit_creds(struct task_struct
*tsk
)
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
));
188 cred
= (struct cred
*) tsk
->real_cred
;
189 tsk
->real_cred
= NULL
;
190 validate_creds(cred
);
191 alter_cred_subscribers(cred
, -1);
194 cred
= (struct cred
*) tsk
->cred
;
196 validate_creds(cred
);
197 alter_cred_subscribers(cred
, -1);
200 cred
= (struct cred
*) tsk
->replacement_session_keyring
;
202 tsk
->replacement_session_keyring
= NULL
;
203 validate_creds(cred
);
209 * get_task_cred - Get another task's objective credentials
210 * @task: The task to query
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.
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.
218 const struct cred
*get_task_cred(struct task_struct
*task
)
220 const struct cred
*cred
;
225 cred
= __task_cred((task
));
227 } while (!atomic_inc_not_zero(&((struct cred
*)cred
)->usage
));
234 * Allocate blank credentials, such that the credentials can be filled in at a
235 * later date without risk of ENOMEM.
237 struct cred
*cred_alloc_blank(void)
241 new = kmem_cache_zalloc(cred_jar
, GFP_KERNEL
);
246 new->tgcred
= kzalloc(sizeof(*new->tgcred
), GFP_KERNEL
);
248 kmem_cache_free(cred_jar
, new);
251 atomic_set(&new->tgcred
->usage
, 1);
254 atomic_set(&new->usage
, 1);
255 #ifdef CONFIG_DEBUG_CREDENTIALS
256 new->magic
= CRED_MAGIC
;
259 if (security_cred_alloc_blank(new, GFP_KERNEL
) < 0)
270 * prepare_creds - Prepare a new set of credentials for modification
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().
277 * Preparation involves making a copy of the objective creds for modification.
279 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
281 * Call commit_creds() or abort_creds() to clean up.
283 struct cred
*prepare_creds(void)
285 struct task_struct
*task
= current
;
286 const struct cred
*old
;
289 validate_process_creds();
291 new = kmem_cache_alloc(cred_jar
, GFP_KERNEL
);
295 kdebug("prepare_creds() alloc %p", new);
298 memcpy(new, old
, sizeof(struct cred
));
300 atomic_set(&new->usage
, 1);
301 set_cred_subscribers(new, 0);
302 get_group_info(new->group_info
);
306 key_get(new->thread_keyring
);
307 key_get(new->request_key_auth
);
308 atomic_inc(&new->tgcred
->usage
);
311 #ifdef CONFIG_SECURITY
312 new->security
= NULL
;
315 if (security_prepare_creds(new, old
, GFP_KERNEL
) < 0)
324 EXPORT_SYMBOL(prepare_creds
);
327 * Prepare credentials for current to perform an execve()
328 * - The caller must hold ->cred_guard_mutex
330 struct cred
*prepare_exec_creds(void)
332 struct thread_group_cred
*tgcred
= NULL
;
336 tgcred
= kmalloc(sizeof(*tgcred
), GFP_KERNEL
);
341 new = prepare_creds();
348 /* newly exec'd tasks don't get a thread keyring */
349 key_put(new->thread_keyring
);
350 new->thread_keyring
= NULL
;
352 /* create a new per-thread-group creds for all this set of threads to
354 memcpy(tgcred
, new->tgcred
, sizeof(struct thread_group_cred
));
356 atomic_set(&tgcred
->usage
, 1);
357 spin_lock_init(&tgcred
->lock
);
359 /* inherit the session keyring; new process keyring */
360 key_get(tgcred
->session_keyring
);
361 tgcred
->process_keyring
= NULL
;
364 new->tgcred
= tgcred
;
371 * Copy credentials for the new process created by fork()
373 * We share if we can, but under some circumstances we have to generate a new
376 * The new process gets the current process's subjective credentials as its
377 * objective and subjective credentials
379 int copy_creds(struct task_struct
*p
, unsigned long clone_flags
)
382 struct thread_group_cred
*tgcred
;
389 !p
->cred
->thread_keyring
&&
391 clone_flags
& CLONE_THREAD
393 p
->real_cred
= get_cred(p
->cred
);
395 alter_cred_subscribers(p
->cred
, 2);
396 kdebug("share_creds(%p{%d,%d})",
397 p
->cred
, atomic_read(&p
->cred
->usage
),
398 read_cred_subscribers(p
->cred
));
399 atomic_inc(&p
->cred
->user
->processes
);
403 new = prepare_creds();
407 if (clone_flags
& CLONE_NEWUSER
) {
408 ret
= create_user_ns(new);
414 /* new threads get their own thread keyrings if their parent already
416 if (new->thread_keyring
) {
417 key_put(new->thread_keyring
);
418 new->thread_keyring
= NULL
;
419 if (clone_flags
& CLONE_THREAD
)
420 install_thread_keyring_to_cred(new);
423 /* we share the process and session keyrings between all the threads in
424 * a process - this is slightly icky as we violate COW credentials a
426 if (!(clone_flags
& CLONE_THREAD
)) {
427 tgcred
= kmalloc(sizeof(*tgcred
), GFP_KERNEL
);
432 atomic_set(&tgcred
->usage
, 1);
433 spin_lock_init(&tgcred
->lock
);
434 tgcred
->process_keyring
= NULL
;
435 tgcred
->session_keyring
= key_get(new->tgcred
->session_keyring
);
438 new->tgcred
= tgcred
;
442 atomic_inc(&new->user
->processes
);
443 p
->cred
= p
->real_cred
= get_cred(new);
444 alter_cred_subscribers(new, 2);
454 * commit_creds - Install new credentials upon the current task
455 * @new: The credentials to be assigned
457 * Install a new set of credentials to the current task, using RCU to replace
458 * the old set. Both the objective and the subjective credentials pointers are
459 * updated. This function may not be called if the subjective credentials are
460 * in an overridden state.
462 * This function eats the caller's reference to the new credentials.
464 * Always returns 0 thus allowing this function to be tail-called at the end
465 * of, say, sys_setgid().
467 int commit_creds(struct cred
*new)
469 struct task_struct
*task
= current
;
470 const struct cred
*old
= task
->real_cred
;
472 kdebug("commit_creds(%p{%d,%d})", new,
473 atomic_read(&new->usage
),
474 read_cred_subscribers(new));
476 BUG_ON(task
->cred
!= old
);
477 #ifdef CONFIG_DEBUG_CREDENTIALS
478 BUG_ON(read_cred_subscribers(old
) < 2);
482 BUG_ON(atomic_read(&new->usage
) < 1);
484 get_cred(new); /* we will require a ref for the subj creds too */
486 /* dumpability changes */
487 if (old
->euid
!= new->euid
||
488 old
->egid
!= new->egid
||
489 old
->fsuid
!= new->fsuid
||
490 old
->fsgid
!= new->fsgid
||
491 !cap_issubset(new->cap_permitted
, old
->cap_permitted
)) {
493 set_dumpable(task
->mm
, suid_dumpable
);
494 task
->pdeath_signal
= 0;
498 /* alter the thread keyring */
499 if (new->fsuid
!= old
->fsuid
)
500 key_fsuid_changed(task
);
501 if (new->fsgid
!= old
->fsgid
)
502 key_fsgid_changed(task
);
505 * - What if a process setreuid()'s and this brings the
506 * new uid over his NPROC rlimit? We can check this now
507 * cheaply with the new uid cache, so if it matters
508 * we should be checking for it. -DaveM
510 alter_cred_subscribers(new, 2);
511 if (new->user
!= old
->user
)
512 atomic_inc(&new->user
->processes
);
513 rcu_assign_pointer(task
->real_cred
, new);
514 rcu_assign_pointer(task
->cred
, new);
515 if (new->user
!= old
->user
)
516 atomic_dec(&old
->user
->processes
);
517 alter_cred_subscribers(old
, -2);
519 /* send notifications */
520 if (new->uid
!= old
->uid
||
521 new->euid
!= old
->euid
||
522 new->suid
!= old
->suid
||
523 new->fsuid
!= old
->fsuid
)
524 proc_id_connector(task
, PROC_EVENT_UID
);
526 if (new->gid
!= old
->gid
||
527 new->egid
!= old
->egid
||
528 new->sgid
!= old
->sgid
||
529 new->fsgid
!= old
->fsgid
)
530 proc_id_connector(task
, PROC_EVENT_GID
);
532 /* release the old obj and subj refs both */
537 EXPORT_SYMBOL(commit_creds
);
540 * abort_creds - Discard a set of credentials and unlock the current task
541 * @new: The credentials that were going to be applied
543 * Discard a set of credentials that were under construction and unlock the
546 void abort_creds(struct cred
*new)
548 kdebug("abort_creds(%p{%d,%d})", new,
549 atomic_read(&new->usage
),
550 read_cred_subscribers(new));
552 #ifdef CONFIG_DEBUG_CREDENTIALS
553 BUG_ON(read_cred_subscribers(new) != 0);
555 BUG_ON(atomic_read(&new->usage
) < 1);
558 EXPORT_SYMBOL(abort_creds
);
561 * override_creds - Override the current process's subjective credentials
562 * @new: The credentials to be assigned
564 * Install a set of temporary override subjective credentials on the current
565 * process, returning the old set for later reversion.
567 const struct cred
*override_creds(const struct cred
*new)
569 const struct cred
*old
= current
->cred
;
571 kdebug("override_creds(%p{%d,%d})", new,
572 atomic_read(&new->usage
),
573 read_cred_subscribers(new));
578 alter_cred_subscribers(new, 1);
579 rcu_assign_pointer(current
->cred
, new);
580 alter_cred_subscribers(old
, -1);
582 kdebug("override_creds() = %p{%d,%d}", old
,
583 atomic_read(&old
->usage
),
584 read_cred_subscribers(old
));
587 EXPORT_SYMBOL(override_creds
);
590 * revert_creds - Revert a temporary subjective credentials override
591 * @old: The credentials to be restored
593 * Revert a temporary set of override subjective credentials to an old set,
594 * discarding the override set.
596 void revert_creds(const struct cred
*old
)
598 const struct cred
*override
= current
->cred
;
600 kdebug("revert_creds(%p{%d,%d})", old
,
601 atomic_read(&old
->usage
),
602 read_cred_subscribers(old
));
605 validate_creds(override
);
606 alter_cred_subscribers(old
, 1);
607 rcu_assign_pointer(current
->cred
, old
);
608 alter_cred_subscribers(override
, -1);
611 EXPORT_SYMBOL(revert_creds
);
614 * initialise the credentials stuff
616 void __init
cred_init(void)
618 /* allocate a slab in which we can store credentials */
619 cred_jar
= kmem_cache_create("cred_jar", sizeof(struct cred
),
620 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
624 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
625 * @daemon: A userspace daemon to be used as a reference
627 * Prepare a set of credentials for a kernel service. This can then be used to
628 * override a task's own credentials so that work can be done on behalf of that
629 * task that requires a different subjective context.
631 * @daemon is used to provide a base for the security record, but can be NULL.
632 * If @daemon is supplied, then the security data will be derived from that;
633 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
635 * The caller may change these controls afterwards if desired.
637 * Returns the new credentials or NULL if out of memory.
639 * Does not take, and does not return holding current->cred_replace_mutex.
641 struct cred
*prepare_kernel_cred(struct task_struct
*daemon
)
643 const struct cred
*old
;
646 new = kmem_cache_alloc(cred_jar
, GFP_KERNEL
);
650 kdebug("prepare_kernel_cred() alloc %p", new);
653 old
= get_task_cred(daemon
);
655 old
= get_cred(&init_cred
);
660 atomic_set(&new->usage
, 1);
661 set_cred_subscribers(new, 0);
663 get_group_info(new->group_info
);
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
;
673 #ifdef CONFIG_SECURITY
674 new->security
= NULL
;
676 if (security_prepare_creds(new, old
, GFP_KERNEL
) < 0)
688 EXPORT_SYMBOL(prepare_kernel_cred
);
691 * set_security_override - Set the security ID in a set of credentials
692 * @new: The credentials to alter
693 * @secid: The LSM security ID to set
695 * Set the LSM security ID in a set of credentials so that the subjective
696 * security is overridden when an alternative set of credentials is used.
698 int set_security_override(struct cred
*new, u32 secid
)
700 return security_kernel_act_as(new, secid
);
702 EXPORT_SYMBOL(set_security_override
);
705 * set_security_override_from_ctx - Set the security ID in a set of credentials
706 * @new: The credentials to alter
707 * @secctx: The LSM security context to generate the security ID from.
709 * Set the LSM security ID in a set of credentials so that the subjective
710 * security is overridden when an alternative set of credentials is used. The
711 * security ID is specified in string form as a security context to be
712 * interpreted by the LSM.
714 int set_security_override_from_ctx(struct cred
*new, const char *secctx
)
719 ret
= security_secctx_to_secid(secctx
, strlen(secctx
), &secid
);
723 return set_security_override(new, secid
);
725 EXPORT_SYMBOL(set_security_override_from_ctx
);
728 * set_create_files_as - Set the LSM file create context in a set of credentials
729 * @new: The credentials to alter
730 * @inode: The inode to take the context from
732 * Change the LSM file creation context in a set of credentials to be the same
733 * as the object context of the specified inode, so that the new inodes have
734 * the same MAC context as that inode.
736 int set_create_files_as(struct cred
*new, struct inode
*inode
)
738 new->fsuid
= inode
->i_uid
;
739 new->fsgid
= inode
->i_gid
;
740 return security_kernel_create_files_as(new, inode
);
742 EXPORT_SYMBOL(set_create_files_as
);
744 struct user_namespace
*current_user_ns(void)
746 return _current_user_ns();
748 EXPORT_SYMBOL(current_user_ns
);
750 #ifdef CONFIG_DEBUG_CREDENTIALS
752 bool creds_are_invalid(const struct cred
*cred
)
754 if (cred
->magic
!= CRED_MAGIC
)
756 #ifdef CONFIG_SECURITY_SELINUX
758 * cred->security == NULL if security_cred_alloc_blank() or
759 * security_prepare_creds() returned an error.
761 if (selinux_is_enabled() && cred
->security
) {
762 if ((unsigned long) cred
->security
< PAGE_SIZE
)
764 if ((*(u32
*)cred
->security
& 0xffffff00) ==
765 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8))
771 EXPORT_SYMBOL(creds_are_invalid
);
774 * dump invalid credentials
776 static void dump_invalid_creds(const struct cred
*cred
, const char *label
,
777 const struct task_struct
*tsk
)
779 printk(KERN_ERR
"CRED: %s credentials: %p %s%s%s\n",
781 cred
== &init_cred
? "[init]" : "",
782 cred
== tsk
->real_cred
? "[real]" : "",
783 cred
== tsk
->cred
? "[eff]" : "");
784 printk(KERN_ERR
"CRED: ->magic=%x, put_addr=%p\n",
785 cred
->magic
, cred
->put_addr
);
786 printk(KERN_ERR
"CRED: ->usage=%d, subscr=%d\n",
787 atomic_read(&cred
->usage
),
788 read_cred_subscribers(cred
));
789 printk(KERN_ERR
"CRED: ->*uid = { %d,%d,%d,%d }\n",
790 cred
->uid
, cred
->euid
, cred
->suid
, cred
->fsuid
);
791 printk(KERN_ERR
"CRED: ->*gid = { %d,%d,%d,%d }\n",
792 cred
->gid
, cred
->egid
, cred
->sgid
, cred
->fsgid
);
793 #ifdef CONFIG_SECURITY
794 printk(KERN_ERR
"CRED: ->security is %p\n", cred
->security
);
795 if ((unsigned long) cred
->security
>= PAGE_SIZE
&&
796 (((unsigned long) cred
->security
& 0xffffff00) !=
797 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8)))
798 printk(KERN_ERR
"CRED: ->security {%x, %x}\n",
799 ((u32
*)cred
->security
)[0],
800 ((u32
*)cred
->security
)[1]);
805 * report use of invalid credentials
807 void __invalid_creds(const struct cred
*cred
, const char *file
, unsigned line
)
809 printk(KERN_ERR
"CRED: Invalid credentials\n");
810 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
811 dump_invalid_creds(cred
, "Specified", current
);
814 EXPORT_SYMBOL(__invalid_creds
);
817 * check the credentials on a process
819 void __validate_process_creds(struct task_struct
*tsk
,
820 const char *file
, unsigned line
)
822 if (tsk
->cred
== tsk
->real_cred
) {
823 if (unlikely(read_cred_subscribers(tsk
->cred
) < 2 ||
824 creds_are_invalid(tsk
->cred
)))
827 if (unlikely(read_cred_subscribers(tsk
->real_cred
) < 1 ||
828 read_cred_subscribers(tsk
->cred
) < 1 ||
829 creds_are_invalid(tsk
->real_cred
) ||
830 creds_are_invalid(tsk
->cred
)))
836 printk(KERN_ERR
"CRED: Invalid process credentials\n");
837 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
839 dump_invalid_creds(tsk
->real_cred
, "Real", tsk
);
840 if (tsk
->cred
!= tsk
->real_cred
)
841 dump_invalid_creds(tsk
->cred
, "Effective", tsk
);
843 printk(KERN_ERR
"CRED: Effective creds == Real creds\n");
846 EXPORT_SYMBOL(__validate_process_creds
);
849 * check creds for do_exit()
851 void validate_creds_for_do_exit(struct task_struct
*tsk
)
853 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
854 tsk
->real_cred
, tsk
->cred
,
855 atomic_read(&tsk
->cred
->usage
),
856 read_cred_subscribers(tsk
->cred
));
858 __validate_process_creds(tsk
, __FILE__
, __LINE__
);
861 #endif /* CONFIG_DEBUG_CREDENTIALS */