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
,
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_INIT_INH_SET
,
53 .cap_permitted
= CAP_FULL_SET
,
54 .cap_effective
= CAP_INIT_EFF_SET
,
55 .cap_bset
= CAP_INIT_BSET
,
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);
256 if (security_cred_alloc_blank(new, GFP_KERNEL
) < 0)
259 #ifdef CONFIG_DEBUG_CREDENTIALS
260 new->magic
= CRED_MAGIC
;
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 current->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
;
387 mutex_init(&p
->cred_guard_mutex
);
391 !p
->cred
->thread_keyring
&&
393 clone_flags
& CLONE_THREAD
395 p
->real_cred
= get_cred(p
->cred
);
397 alter_cred_subscribers(p
->cred
, 2);
398 kdebug("share_creds(%p{%d,%d})",
399 p
->cred
, atomic_read(&p
->cred
->usage
),
400 read_cred_subscribers(p
->cred
));
401 atomic_inc(&p
->cred
->user
->processes
);
405 new = prepare_creds();
409 if (clone_flags
& CLONE_NEWUSER
) {
410 ret
= create_user_ns(new);
416 /* new threads get their own thread keyrings if their parent already
418 if (new->thread_keyring
) {
419 key_put(new->thread_keyring
);
420 new->thread_keyring
= NULL
;
421 if (clone_flags
& CLONE_THREAD
)
422 install_thread_keyring_to_cred(new);
425 /* we share the process and session keyrings between all the threads in
426 * a process - this is slightly icky as we violate COW credentials a
428 if (!(clone_flags
& CLONE_THREAD
)) {
429 tgcred
= kmalloc(sizeof(*tgcred
), GFP_KERNEL
);
434 atomic_set(&tgcred
->usage
, 1);
435 spin_lock_init(&tgcred
->lock
);
436 tgcred
->process_keyring
= NULL
;
437 tgcred
->session_keyring
= key_get(new->tgcred
->session_keyring
);
440 new->tgcred
= tgcred
;
444 atomic_inc(&new->user
->processes
);
445 p
->cred
= p
->real_cred
= get_cred(new);
446 alter_cred_subscribers(new, 2);
456 * commit_creds - Install new credentials upon the current task
457 * @new: The credentials to be assigned
459 * Install a new set of credentials to the current task, using RCU to replace
460 * the old set. Both the objective and the subjective credentials pointers are
461 * updated. This function may not be called if the subjective credentials are
462 * in an overridden state.
464 * This function eats the caller's reference to the new credentials.
466 * Always returns 0 thus allowing this function to be tail-called at the end
467 * of, say, sys_setgid().
469 int commit_creds(struct cred
*new)
471 struct task_struct
*task
= current
;
472 const struct cred
*old
= task
->real_cred
;
474 kdebug("commit_creds(%p{%d,%d})", new,
475 atomic_read(&new->usage
),
476 read_cred_subscribers(new));
478 BUG_ON(task
->cred
!= old
);
479 #ifdef CONFIG_DEBUG_CREDENTIALS
480 BUG_ON(read_cred_subscribers(old
) < 2);
484 BUG_ON(atomic_read(&new->usage
) < 1);
486 get_cred(new); /* we will require a ref for the subj creds too */
488 /* dumpability changes */
489 if (old
->euid
!= new->euid
||
490 old
->egid
!= new->egid
||
491 old
->fsuid
!= new->fsuid
||
492 old
->fsgid
!= new->fsgid
||
493 !cap_issubset(new->cap_permitted
, old
->cap_permitted
)) {
495 set_dumpable(task
->mm
, suid_dumpable
);
496 task
->pdeath_signal
= 0;
500 /* alter the thread keyring */
501 if (new->fsuid
!= old
->fsuid
)
502 key_fsuid_changed(task
);
503 if (new->fsgid
!= old
->fsgid
)
504 key_fsgid_changed(task
);
507 * - What if a process setreuid()'s and this brings the
508 * new uid over his NPROC rlimit? We can check this now
509 * cheaply with the new uid cache, so if it matters
510 * we should be checking for it. -DaveM
512 alter_cred_subscribers(new, 2);
513 if (new->user
!= old
->user
)
514 atomic_inc(&new->user
->processes
);
515 rcu_assign_pointer(task
->real_cred
, new);
516 rcu_assign_pointer(task
->cred
, new);
517 if (new->user
!= old
->user
)
518 atomic_dec(&old
->user
->processes
);
519 alter_cred_subscribers(old
, -2);
521 /* send notifications */
522 if (new->uid
!= old
->uid
||
523 new->euid
!= old
->euid
||
524 new->suid
!= old
->suid
||
525 new->fsuid
!= old
->fsuid
)
526 proc_id_connector(task
, PROC_EVENT_UID
);
528 if (new->gid
!= old
->gid
||
529 new->egid
!= old
->egid
||
530 new->sgid
!= old
->sgid
||
531 new->fsgid
!= old
->fsgid
)
532 proc_id_connector(task
, PROC_EVENT_GID
);
534 /* release the old obj and subj refs both */
539 EXPORT_SYMBOL(commit_creds
);
542 * abort_creds - Discard a set of credentials and unlock the current task
543 * @new: The credentials that were going to be applied
545 * Discard a set of credentials that were under construction and unlock the
548 void abort_creds(struct cred
*new)
550 kdebug("abort_creds(%p{%d,%d})", new,
551 atomic_read(&new->usage
),
552 read_cred_subscribers(new));
554 #ifdef CONFIG_DEBUG_CREDENTIALS
555 BUG_ON(read_cred_subscribers(new) != 0);
557 BUG_ON(atomic_read(&new->usage
) < 1);
560 EXPORT_SYMBOL(abort_creds
);
563 * override_creds - Override the current process's subjective credentials
564 * @new: The credentials to be assigned
566 * Install a set of temporary override subjective credentials on the current
567 * process, returning the old set for later reversion.
569 const struct cred
*override_creds(const struct cred
*new)
571 const struct cred
*old
= current
->cred
;
573 kdebug("override_creds(%p{%d,%d})", new,
574 atomic_read(&new->usage
),
575 read_cred_subscribers(new));
580 alter_cred_subscribers(new, 1);
581 rcu_assign_pointer(current
->cred
, new);
582 alter_cred_subscribers(old
, -1);
584 kdebug("override_creds() = %p{%d,%d}", old
,
585 atomic_read(&old
->usage
),
586 read_cred_subscribers(old
));
589 EXPORT_SYMBOL(override_creds
);
592 * revert_creds - Revert a temporary subjective credentials override
593 * @old: The credentials to be restored
595 * Revert a temporary set of override subjective credentials to an old set,
596 * discarding the override set.
598 void revert_creds(const struct cred
*old
)
600 const struct cred
*override
= current
->cred
;
602 kdebug("revert_creds(%p{%d,%d})", old
,
603 atomic_read(&old
->usage
),
604 read_cred_subscribers(old
));
607 validate_creds(override
);
608 alter_cred_subscribers(old
, 1);
609 rcu_assign_pointer(current
->cred
, old
);
610 alter_cred_subscribers(override
, -1);
613 EXPORT_SYMBOL(revert_creds
);
616 * initialise the credentials stuff
618 void __init
cred_init(void)
620 /* allocate a slab in which we can store credentials */
621 cred_jar
= kmem_cache_create("cred_jar", sizeof(struct cred
),
622 0, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
626 * prepare_kernel_cred - Prepare a set of credentials for a kernel service
627 * @daemon: A userspace daemon to be used as a reference
629 * Prepare a set of credentials for a kernel service. This can then be used to
630 * override a task's own credentials so that work can be done on behalf of that
631 * task that requires a different subjective context.
633 * @daemon is used to provide a base for the security record, but can be NULL.
634 * If @daemon is supplied, then the security data will be derived from that;
635 * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
637 * The caller may change these controls afterwards if desired.
639 * Returns the new credentials or NULL if out of memory.
641 * Does not take, and does not return holding current->cred_replace_mutex.
643 struct cred
*prepare_kernel_cred(struct task_struct
*daemon
)
645 const struct cred
*old
;
648 new = kmem_cache_alloc(cred_jar
, GFP_KERNEL
);
652 kdebug("prepare_kernel_cred() alloc %p", new);
655 old
= get_task_cred(daemon
);
657 old
= get_cred(&init_cred
);
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)
679 atomic_set(&new->usage
, 1);
680 set_cred_subscribers(new, 0);
690 EXPORT_SYMBOL(prepare_kernel_cred
);
693 * set_security_override - Set the security ID in a set of credentials
694 * @new: The credentials to alter
695 * @secid: The LSM security ID to set
697 * Set the LSM security ID in a set of credentials so that the subjective
698 * security is overridden when an alternative set of credentials is used.
700 int set_security_override(struct cred
*new, u32 secid
)
702 return security_kernel_act_as(new, secid
);
704 EXPORT_SYMBOL(set_security_override
);
707 * set_security_override_from_ctx - Set the security ID in a set of credentials
708 * @new: The credentials to alter
709 * @secctx: The LSM security context to generate the security ID from.
711 * Set the LSM security ID in a set of credentials so that the subjective
712 * security is overridden when an alternative set of credentials is used. The
713 * security ID is specified in string form as a security context to be
714 * interpreted by the LSM.
716 int set_security_override_from_ctx(struct cred
*new, const char *secctx
)
721 ret
= security_secctx_to_secid(secctx
, strlen(secctx
), &secid
);
725 return set_security_override(new, secid
);
727 EXPORT_SYMBOL(set_security_override_from_ctx
);
730 * set_create_files_as - Set the LSM file create context in a set of credentials
731 * @new: The credentials to alter
732 * @inode: The inode to take the context from
734 * Change the LSM file creation context in a set of credentials to be the same
735 * as the object context of the specified inode, so that the new inodes have
736 * the same MAC context as that inode.
738 int set_create_files_as(struct cred
*new, struct inode
*inode
)
740 new->fsuid
= inode
->i_uid
;
741 new->fsgid
= inode
->i_gid
;
742 return security_kernel_create_files_as(new, inode
);
744 EXPORT_SYMBOL(set_create_files_as
);
746 #ifdef CONFIG_DEBUG_CREDENTIALS
748 bool creds_are_invalid(const struct cred
*cred
)
750 if (cred
->magic
!= CRED_MAGIC
)
752 #ifdef CONFIG_SECURITY_SELINUX
753 if (selinux_is_enabled()) {
754 if ((unsigned long) cred
->security
< PAGE_SIZE
)
756 if ((*(u32
*)cred
->security
& 0xffffff00) ==
757 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8))
763 EXPORT_SYMBOL(creds_are_invalid
);
766 * dump invalid credentials
768 static void dump_invalid_creds(const struct cred
*cred
, const char *label
,
769 const struct task_struct
*tsk
)
771 printk(KERN_ERR
"CRED: %s credentials: %p %s%s%s\n",
773 cred
== &init_cred
? "[init]" : "",
774 cred
== tsk
->real_cred
? "[real]" : "",
775 cred
== tsk
->cred
? "[eff]" : "");
776 printk(KERN_ERR
"CRED: ->magic=%x, put_addr=%p\n",
777 cred
->magic
, cred
->put_addr
);
778 printk(KERN_ERR
"CRED: ->usage=%d, subscr=%d\n",
779 atomic_read(&cred
->usage
),
780 read_cred_subscribers(cred
));
781 printk(KERN_ERR
"CRED: ->*uid = { %d,%d,%d,%d }\n",
782 cred
->uid
, cred
->euid
, cred
->suid
, cred
->fsuid
);
783 printk(KERN_ERR
"CRED: ->*gid = { %d,%d,%d,%d }\n",
784 cred
->gid
, cred
->egid
, cred
->sgid
, cred
->fsgid
);
785 #ifdef CONFIG_SECURITY
786 printk(KERN_ERR
"CRED: ->security is %p\n", cred
->security
);
787 if ((unsigned long) cred
->security
>= PAGE_SIZE
&&
788 (((unsigned long) cred
->security
& 0xffffff00) !=
789 (POISON_FREE
<< 24 | POISON_FREE
<< 16 | POISON_FREE
<< 8)))
790 printk(KERN_ERR
"CRED: ->security {%x, %x}\n",
791 ((u32
*)cred
->security
)[0],
792 ((u32
*)cred
->security
)[1]);
797 * report use of invalid credentials
799 void __invalid_creds(const struct cred
*cred
, const char *file
, unsigned line
)
801 printk(KERN_ERR
"CRED: Invalid credentials\n");
802 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
803 dump_invalid_creds(cred
, "Specified", current
);
806 EXPORT_SYMBOL(__invalid_creds
);
809 * check the credentials on a process
811 void __validate_process_creds(struct task_struct
*tsk
,
812 const char *file
, unsigned line
)
814 if (tsk
->cred
== tsk
->real_cred
) {
815 if (unlikely(read_cred_subscribers(tsk
->cred
) < 2 ||
816 creds_are_invalid(tsk
->cred
)))
819 if (unlikely(read_cred_subscribers(tsk
->real_cred
) < 1 ||
820 read_cred_subscribers(tsk
->cred
) < 1 ||
821 creds_are_invalid(tsk
->real_cred
) ||
822 creds_are_invalid(tsk
->cred
)))
828 printk(KERN_ERR
"CRED: Invalid process credentials\n");
829 printk(KERN_ERR
"CRED: At %s:%u\n", file
, line
);
831 dump_invalid_creds(tsk
->real_cred
, "Real", tsk
);
832 if (tsk
->cred
!= tsk
->real_cred
)
833 dump_invalid_creds(tsk
->cred
, "Effective", tsk
);
835 printk(KERN_ERR
"CRED: Effective creds == Real creds\n");
838 EXPORT_SYMBOL(__validate_process_creds
);
841 * check creds for do_exit()
843 void validate_creds_for_do_exit(struct task_struct
*tsk
)
845 kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
846 tsk
->real_cred
, tsk
->cred
,
847 atomic_read(&tsk
->cred
->usage
),
848 read_cred_subscribers(tsk
->cred
));
850 __validate_process_creds(tsk
, __FILE__
, __LINE__
);
853 #endif /* CONFIG_DEBUG_CREDENTIALS */