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 #define kdebug(FMT, ...) \
22 no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
24 static struct kmem_cache
*cred_jar
;
27 * The common credentials for the initial task's thread group
30 static struct thread_group_cred init_tgcred
= {
31 .usage
= ATOMIC_INIT(2),
33 .lock
= SPIN_LOCK_UNLOCKED
,
38 * The initial credentials for the initial task
40 struct cred init_cred
= {
41 .usage
= ATOMIC_INIT(4),
42 #ifdef CONFIG_DEBUG_CREDENTIALS
43 .subscribers
= ATOMIC_INIT(2),
46 .securebits
= SECUREBITS_DEFAULT
,
47 .cap_inheritable
= CAP_INIT_INH_SET
,
48 .cap_permitted
= CAP_FULL_SET
,
49 .cap_effective
= CAP_INIT_EFF_SET
,
50 .cap_bset
= CAP_INIT_BSET
,
52 .group_info
= &init_groups
,
54 .tgcred
= &init_tgcred
,
58 static inline void set_cred_subscribers(struct cred
*cred
, int n
)
60 #ifdef CONFIG_DEBUG_CREDENTIALS
61 atomic_set(&cred
->subscribers
, n
);
65 static inline int read_cred_subscribers(const struct cred
*cred
)
67 #ifdef CONFIG_DEBUG_CREDENTIALS
68 return atomic_read(&cred
->subscribers
);
74 static inline void alter_cred_subscribers(const struct cred
*_cred
, int n
)
76 #ifdef CONFIG_DEBUG_CREDENTIALS
77 struct cred
*cred
= (struct cred
*) _cred
;
79 atomic_add(n
, &cred
->subscribers
);
84 * Dispose of the shared task group credentials
87 static void release_tgcred_rcu(struct rcu_head
*rcu
)
89 struct thread_group_cred
*tgcred
=
90 container_of(rcu
, struct thread_group_cred
, rcu
);
92 BUG_ON(atomic_read(&tgcred
->usage
) != 0);
94 key_put(tgcred
->session_keyring
);
95 key_put(tgcred
->process_keyring
);
101 * Release a set of thread group credentials.
103 static void release_tgcred(struct cred
*cred
)
106 struct thread_group_cred
*tgcred
= cred
->tgcred
;
108 if (atomic_dec_and_test(&tgcred
->usage
))
109 call_rcu(&tgcred
->rcu
, release_tgcred_rcu
);
114 * The RCU callback to actually dispose of a set of credentials
116 static void put_cred_rcu(struct rcu_head
*rcu
)
118 struct cred
*cred
= container_of(rcu
, struct cred
, rcu
);
120 kdebug("put_cred_rcu(%p)", cred
);
122 #ifdef CONFIG_DEBUG_CREDENTIALS
123 if (cred
->magic
!= CRED_MAGIC_DEAD
||
124 atomic_read(&cred
->usage
) != 0 ||
125 read_cred_subscribers(cred
) != 0)
126 panic("CRED: put_cred_rcu() sees %p with"
127 " mag %x, put %p, usage %d, subscr %d\n",
128 cred
, cred
->magic
, cred
->put_addr
,
129 atomic_read(&cred
->usage
),
130 read_cred_subscribers(cred
));
132 if (atomic_read(&cred
->usage
) != 0)
133 panic("CRED: put_cred_rcu() sees %p with usage %d\n",
134 cred
, atomic_read(&cred
->usage
));
137 security_cred_free(cred
);
138 key_put(cred
->thread_keyring
);
139 key_put(cred
->request_key_auth
);
140 release_tgcred(cred
);
141 if (cred
->group_info
)
142 put_group_info(cred
->group_info
);
143 free_uid(cred
->user
);
144 kmem_cache_free(cred_jar
, cred
);
148 * __put_cred - Destroy a set of credentials
149 * @cred: The record to release
151 * Destroy a set of credentials on which no references remain.
153 void __put_cred(struct cred
*cred
)
155 kdebug("__put_cred(%p{%d,%d})", cred
,
156 atomic_read(&cred
->usage
),
157 read_cred_subscribers(cred
));
159 BUG_ON(atomic_read(&cred
->usage
) != 0);
160 #ifdef CONFIG_DEBUG_CREDENTIALS
161 BUG_ON(read_cred_subscribers(cred
) != 0);
162 cred
->magic
= CRED_MAGIC_DEAD
;
163 cred
->put_addr
= __builtin_return_address(0);
165 BUG_ON(cred
== current
->cred
);
166 BUG_ON(cred
== current
->real_cred
);
168 call_rcu(&cred
->rcu
, put_cred_rcu
);
170 EXPORT_SYMBOL(__put_cred
);
173 * Clean up a task's credentials when it exits
175 void exit_creds(struct task_struct
*tsk
)
179 kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk
->pid
, tsk
->real_cred
, tsk
->cred
,
180 atomic_read(&tsk
->cred
->usage
),
181 read_cred_subscribers(tsk
->cred
));
183 cred
= (struct cred
*) tsk
->real_cred
;
184 tsk
->real_cred
= NULL
;
185 validate_creds(cred
);
186 alter_cred_subscribers(cred
, -1);
189 cred
= (struct cred
*) tsk
->cred
;
191 validate_creds(cred
);
192 alter_cred_subscribers(cred
, -1);
195 cred
= (struct cred
*) tsk
->replacement_session_keyring
;
197 tsk
->replacement_session_keyring
= NULL
;
198 validate_creds(cred
);
204 * get_task_cred - Get another task's objective credentials
205 * @task: The task to query
207 * Get the objective credentials of a task, pinning them so that they can't go
208 * away. Accessing a task's credentials directly is not permitted.
210 * The caller must also make sure task doesn't get deleted, either by holding a
211 * ref on task or by holding tasklist_lock to prevent it from being unlinked.
213 const struct cred
*get_task_cred(struct task_struct
*task
)
215 const struct cred
*cred
;
220 cred
= __task_cred((task
));
222 } while (!atomic_inc_not_zero(&((struct cred
*)cred
)->usage
));
229 * Allocate blank credentials, such that the credentials can be filled in at a
230 * later date without risk of ENOMEM.
232 struct cred
*cred_alloc_blank(void)
236 new = kmem_cache_zalloc(cred_jar
, GFP_KERNEL
);
241 new->tgcred
= kzalloc(sizeof(*new->tgcred
), GFP_KERNEL
);
243 kmem_cache_free(cred_jar
, new);
246 atomic_set(&new->tgcred
->usage
, 1);
249 atomic_set(&new->usage
, 1);
251 if (security_cred_alloc_blank(new, GFP_KERNEL
) < 0)
254 #ifdef CONFIG_DEBUG_CREDENTIALS
255 new->magic
= CRED_MAGIC
;
265 * prepare_creds - Prepare a new set of credentials for modification
267 * Prepare a new set of task credentials for modification. A task's creds
268 * shouldn't generally be modified directly, therefore this function is used to
269 * prepare a new copy, which the caller then modifies and then commits by
270 * calling commit_creds().
272 * Preparation involves making a copy of the objective creds for modification.
274 * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
276 * Call commit_creds() or abort_creds() to clean up.
278 struct cred
*prepare_creds(void)
280 struct task_struct
*task
= current
;
281 const struct cred
*old
;
284 validate_process_creds();
286 new = kmem_cache_alloc(cred_jar
, GFP_KERNEL
);
290 kdebug("prepare_creds() alloc %p", new);
293 memcpy(new, old
, sizeof(struct cred
));
295 atomic_set(&new->usage
, 1);
296 set_cred_subscribers(new, 0);
297 get_group_info(new->group_info
);
301 key_get(new->thread_keyring
);
302 key_get(new->request_key_auth
);
303 atomic_inc(&new->tgcred
->usage
);
306 #ifdef CONFIG_SECURITY
307 new->security
= NULL
;
310 if (security_prepare_creds(new, old
, GFP_KERNEL
) < 0)
319 EXPORT_SYMBOL(prepare_creds
);
322 * Prepare credentials for current to perform an execve()
323 * - The caller must hold current->cred_guard_mutex
325 struct cred
*prepare_exec_creds(void)
327 struct thread_group_cred
*tgcred
= NULL
;
331 tgcred
= kmalloc(sizeof(*tgcred
), GFP_KERNEL
);
336 new = prepare_creds();
343 /* newly exec'd tasks don't get a thread keyring */
344 key_put(new->thread_keyring
);
345 new->thread_keyring
= NULL
;
347 /* create a new per-thread-group creds for all this set of threads to
349 memcpy(tgcred
, new->tgcred
, sizeof(struct thread_group_cred
));
351 atomic_set(&tgcred
->usage
, 1);
352 spin_lock_init(&tgcred
->lock
);
354 /* inherit the session keyring; new process keyring */
355 key_get(tgcred
->session_keyring
);
356 tgcred
->process_keyring
= NULL
;
359 new->tgcred
= tgcred
;
366 * Copy credentials for the new process created by fork()
368 * We share if we can, but under some circumstances we have to generate a new
371 * The new process gets the current process's subjective credentials as its
372 * objective and subjective credentials
374 int copy_creds(struct task_struct
*p
, unsigned long clone_flags
)
377 struct thread_group_cred
*tgcred
;
382 mutex_init(&p
->cred_guard_mutex
);
386 !p
->cred
->thread_keyring
&&
388 clone_flags
& CLONE_THREAD
390 p
->real_cred
= get_cred(p
->cred
);
392 alter_cred_subscribers(p
->cred
, 2);
393 kdebug("share_creds(%p{%d,%d})",
394 p
->cred
, atomic_read(&p
->cred
->usage
),
395 read_cred_subscribers(p
->cred
));
396 atomic_inc(&p
->cred
->user
->processes
);
400 new = prepare_creds();
404 if (clone_flags
& CLONE_NEWUSER
) {
405 ret
= create_user_ns(new);
411 /* new threads get their own thread keyrings if their parent already
413 if (new->thread_keyring
) {
414 key_put(new->thread_keyring
);
415 new->thread_keyring
= NULL
;
416 if (clone_flags
& CLONE_THREAD
)
417 install_thread_keyring_to_cred(new);
420 /* we share the process and session keyrings between all the threads in
421 * a process - this is slightly icky as we violate COW credentials a
423 if (!(clone_flags
& CLONE_THREAD
)) {
424 tgcred
= kmalloc(sizeof(*tgcred
), GFP_KERNEL
);
429 atomic_set(&tgcred
->usage
, 1);
430 spin_lock_init(&tgcred
->lock
);
431 tgcred
->process_keyring
= NULL
;
432 tgcred
->session_keyring
= key_get(new->tgcred
->session_keyring
);
435 new->tgcred
= tgcred
;
439 atomic_inc(&new->user
->processes
);
440 p
->cred
= p
->real_cred
= get_cred(new);
441 alter_cred_subscribers(new, 2);
451 * commit_creds - Install new credentials upon the current task
452 * @new: The credentials to be assigned
454 * Install a new set of credentials to the current task, using RCU to replace
455 * the old set. Both the objective and the subjective credentials pointers are
456 * updated. This function may not be called if the subjective credentials are
457 * in an overridden state.
459 * This function eats the caller's reference to the new credentials.
461 * Always returns 0 thus allowing this function to be tail-called at the end
462 * of, say, sys_setgid().
464 int commit_creds(struct cred
*new)
466 struct task_struct
*task
= current
;
467 const struct cred
*old
= task
->real_cred
;
469 kdebug("commit_creds(%p{%d,%d})", new,
470 atomic_read(&new->usage
),
471 read_cred_subscribers(new));
473 BUG_ON(task
->cred
!= old
);
474 #ifdef CONFIG_DEBUG_CREDENTIALS
475 BUG_ON(read_cred_subscribers(old
) < 2);
479 BUG_ON(atomic_read(&new->usage
) < 1);
481 get_cred(new); /* we will require a ref for the subj creds too */
483 /* dumpability changes */
484 if (old
->euid
!= new->euid
||
485 old
->egid
!= new->egid
||
486 old
->fsuid
!= new->fsuid
||
487 old
->fsgid
!= new->fsgid
||
488 !cap_issubset(new->cap_permitted
, old
->cap_permitted
)) {
490 set_dumpable(task
->mm
, suid_dumpable
);
491 task
->pdeath_signal
= 0;
495 /* alter the thread keyring */
496 if (new->fsuid
!= old
->fsuid
)
497 key_fsuid_changed(task
);
498 if (new->fsgid
!= old
->fsgid
)
499 key_fsgid_changed(task
);
502 * - What if a process setreuid()'s and this brings the
503 * new uid over his NPROC rlimit? We can check this now
504 * cheaply with the new uid cache, so if it matters
505 * we should be checking for it. -DaveM
507 alter_cred_subscribers(new, 2);
508 if (new->user
!= old
->user
)
509 atomic_inc(&new->user
->processes
);
510 rcu_assign_pointer(task
->real_cred
, new);
511 rcu_assign_pointer(task
->cred
, new);
512 if (new->user
!= old
->user
)
513 atomic_dec(&old
->user
->processes
);
514 alter_cred_subscribers(old
, -2);
516 /* send notifications */
517 if (new->uid
!= old
->uid
||
518 new->euid
!= old
->euid
||
519 new->suid
!= old
->suid
||
520 new->fsuid
!= old
->fsuid
)
521 proc_id_connector(task
, PROC_EVENT_UID
);
523 if (new->gid
!= old
->gid
||
524 new->egid
!= old
->egid
||
525 new->sgid
!= old
->sgid
||
526 new->fsgid
!= old
->fsgid
)
527 proc_id_connector(task
, PROC_EVENT_GID
);
529 /* release the old obj and subj refs both */
534 EXPORT_SYMBOL(commit_creds
);
537 * abort_creds - Discard a set of credentials and unlock the current task
538 * @new: The credentials that were going to be applied
540 * Discard a set of credentials that were under construction and unlock the
543 void abort_creds(struct cred
*new)
545 kdebug("abort_creds(%p{%d,%d})", new,
546 atomic_read(&new->usage
),
547 read_cred_subscribers(new));
549 #ifdef CONFIG_DEBUG_CREDENTIALS
550 BUG_ON(read_cred_subscribers(new) != 0);
552 BUG_ON(atomic_read(&new->usage
) < 1);
555 EXPORT_SYMBOL(abort_creds
);
558 * override_creds - Override the current process's subjective credentials
559 * @new: The credentials to be assigned
561 * Install a set of temporary override subjective credentials on the current
562 * process, returning the old set for later reversion.
564 const struct cred
*override_creds(const struct cred
*new)
566 const struct cred
*old
= current
->cred
;
568 kdebug("override_creds(%p{%d,%d})", new,
569 atomic_read(&new->usage
),
570 read_cred_subscribers(new));
575 alter_cred_subscribers(new, 1);
576 rcu_assign_pointer(current
->cred
, new);
577 alter_cred_subscribers(old
, -1);
579 kdebug("override_creds() = %p{%d,%d}", old
,
580 atomic_read(&old
->usage
),
581 read_cred_subscribers(old
));
584 EXPORT_SYMBOL(override_creds
);
587 * revert_creds - Revert a temporary subjective credentials override
588 * @old: The credentials to be restored
590 * Revert a temporary set of override subjective credentials to an old set,
591 * discarding the override set.
593 void revert_creds(const struct cred
*old
)
595 const struct cred
*override
= current
->cred
;
597 kdebug("revert_creds(%p{%d,%d})", old
,
598 atomic_read(&old
->usage
),
599 read_cred_subscribers(old
));
602 validate_creds(override
);
603 alter_cred_subscribers(old
, 1);
604 rcu_assign_pointer(current
->cred
, old
);
605 alter_cred_subscribers(override
, -1);
608 EXPORT_SYMBOL(revert_creds
);
611 * initialise the credentials stuff
613 void __init
cred_init(void)
615 /* allocate a slab in which we can store credentials */
616 cred_jar
= kmem_cache_create("cred_jar", sizeof(struct cred
),
617 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
621 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
622 * @daemon: A userspace daemon to be used as a reference
624 * Prepare a set of credentials for a kernel service. This can then be used to
625 * override a task's own credentials so that work can be done on behalf of that
626 * task that requires a different subjective context.
628 * @daemon is used to provide a base for the security record, but can be NULL.
629 * If @daemon is supplied, then the security data will be derived from that;
630 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
632 * The caller may change these controls afterwards if desired.
634 * Returns the new credentials or NULL if out of memory.
636 * Does not take, and does not return holding current->cred_replace_mutex.
638 struct cred
*prepare_kernel_cred(struct task_struct
*daemon
)
640 const struct cred
*old
;
643 new = kmem_cache_alloc(cred_jar
, GFP_KERNEL
);
647 kdebug("prepare_kernel_cred() alloc %p", new);
650 old
= get_task_cred(daemon
);
652 old
= get_cred(&init_cred
);
658 get_group_info(new->group_info
);
661 atomic_inc(&init_tgcred
.usage
);
662 new->tgcred
= &init_tgcred
;
663 new->request_key_auth
= NULL
;
664 new->thread_keyring
= NULL
;
665 new->jit_keyring
= KEY_REQKEY_DEFL_THREAD_KEYRING
;
668 #ifdef CONFIG_SECURITY
669 new->security
= NULL
;
671 if (security_prepare_creds(new, old
, GFP_KERNEL
) < 0)
674 atomic_set(&new->usage
, 1);
675 set_cred_subscribers(new, 0);
685 EXPORT_SYMBOL(prepare_kernel_cred
);
688 * set_security_override - Set the security ID in a set of credentials
689 * @new: The credentials to alter
690 * @secid: The LSM security ID to set
692 * Set the LSM security ID in a set of credentials so that the subjective
693 * security is overridden when an alternative set of credentials is used.
695 int set_security_override(struct cred
*new, u32 secid
)
697 return security_kernel_act_as(new, secid
);
699 EXPORT_SYMBOL(set_security_override
);
702 * set_security_override_from_ctx - Set the security ID in a set of credentials
703 * @new: The credentials to alter
704 * @secctx: The LSM security context to generate the security ID from.
706 * Set the LSM security ID in a set of credentials so that the subjective
707 * security is overridden when an alternative set of credentials is used. The
708 * security ID is specified in string form as a security context to be
709 * interpreted by the LSM.
711 int set_security_override_from_ctx(struct cred
*new, const char *secctx
)
716 ret
= security_secctx_to_secid(secctx
, strlen(secctx
), &secid
);
720 return set_security_override(new, secid
);
722 EXPORT_SYMBOL(set_security_override_from_ctx
);
725 * set_create_files_as - Set the LSM file create context in a set of credentials
726 * @new: The credentials to alter
727 * @inode: The inode to take the context from
729 * Change the LSM file creation context in a set of credentials to be the same
730 * as the object context of the specified inode, so that the new inodes have
731 * the same MAC context as that inode.
733 int set_create_files_as(struct cred
*new, struct inode
*inode
)
735 new->fsuid
= inode
->i_uid
;
736 new->fsgid
= inode
->i_gid
;
737 return security_kernel_create_files_as(new, inode
);
739 EXPORT_SYMBOL(set_create_files_as
);
741 #ifdef CONFIG_DEBUG_CREDENTIALS
743 bool creds_are_invalid(const struct cred
*cred
)
745 if (cred
->magic
!= CRED_MAGIC
)
747 #ifdef CONFIG_SECURITY_SELINUX
748 if (selinux_is_enabled()) {
749 if ((unsigned long) cred
->security
< PAGE_SIZE
)
751 if ((*(u32
*)cred
->security
& 0xffffff00) ==
752 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8))
758 EXPORT_SYMBOL(creds_are_invalid
);
761 * dump invalid credentials
763 static void dump_invalid_creds(const struct cred
*cred
, const char *label
,
764 const struct task_struct
*tsk
)
766 printk(KERN_ERR
"CRED: %s credentials: %p %s%s%s\n",
768 cred
== &init_cred
? "[init]" : "",
769 cred
== tsk
->real_cred
? "[real]" : "",
770 cred
== tsk
->cred
? "[eff]" : "");
771 printk(KERN_ERR
"CRED: ->magic=%x, put_addr=%p\n",
772 cred
->magic
, cred
->put_addr
);
773 printk(KERN_ERR
"CRED: ->usage=%d, subscr=%d\n",
774 atomic_read(&cred
->usage
),
775 read_cred_subscribers(cred
));
776 printk(KERN_ERR
"CRED: ->*uid = { %d,%d,%d,%d }\n",
777 cred
->uid
, cred
->euid
, cred
->suid
, cred
->fsuid
);
778 printk(KERN_ERR
"CRED: ->*gid = { %d,%d,%d,%d }\n",
779 cred
->gid
, cred
->egid
, cred
->sgid
, cred
->fsgid
);
780 #ifdef CONFIG_SECURITY
781 printk(KERN_ERR
"CRED: ->security is %p\n", cred
->security
);
782 if ((unsigned long) cred
->security
>= PAGE_SIZE
&&
783 (((unsigned long) cred
->security
& 0xffffff00) !=
784 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8)))
785 printk(KERN_ERR
"CRED: ->security {%x, %x}\n",
786 ((u32
*)cred
->security
)[0],
787 ((u32
*)cred
->security
)[1]);
792 * report use of invalid credentials
794 void __invalid_creds(const struct cred
*cred
, const char *file
, unsigned line
)
796 printk(KERN_ERR
"CRED: Invalid credentials\n");
797 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
798 dump_invalid_creds(cred
, "Specified", current
);
801 EXPORT_SYMBOL(__invalid_creds
);
804 * check the credentials on a process
806 void __validate_process_creds(struct task_struct
*tsk
,
807 const char *file
, unsigned line
)
809 if (tsk
->cred
== tsk
->real_cred
) {
810 if (unlikely(read_cred_subscribers(tsk
->cred
) < 2 ||
811 creds_are_invalid(tsk
->cred
)))
814 if (unlikely(read_cred_subscribers(tsk
->real_cred
) < 1 ||
815 read_cred_subscribers(tsk
->cred
) < 1 ||
816 creds_are_invalid(tsk
->real_cred
) ||
817 creds_are_invalid(tsk
->cred
)))
823 printk(KERN_ERR
"CRED: Invalid process credentials\n");
824 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
826 dump_invalid_creds(tsk
->real_cred
, "Real", tsk
);
827 if (tsk
->cred
!= tsk
->real_cred
)
828 dump_invalid_creds(tsk
->cred
, "Effective", tsk
);
830 printk(KERN_ERR
"CRED: Effective creds == Real creds\n");
833 EXPORT_SYMBOL(__validate_process_creds
);
836 * check creds for do_exit()
838 void validate_creds_for_do_exit(struct task_struct
*tsk
)
840 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
841 tsk
->real_cred
, tsk
->cred
,
842 atomic_read(&tsk
->cred
->usage
),
843 read_cred_subscribers(tsk
->cred
));
845 __validate_process_creds(tsk
, __FILE__
, __LINE__
);
848 #endif /* CONFIG_DEBUG_CREDENTIALS */