861 lines · 21.9 KB · C
RawDownload
/* Task credentials management - see Documentation/security/credentials.txt * * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. * Written by David Howells (dhowells@redhat.com) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public Licence * as published by the Free Software Foundation; either version * 2 of the Licence, or (at your option) any later version. */#include <linux/module.h>#include <linux/cred.h>#include <linux/slab.h>#include <linux/sched.h>#include <linux/key.h>#include <linux/keyctl.h>#include <linux/init_task.h>#include <linux/security.h>#include <linux/cn_proc.h> #if 0#define kdebug(FMT, ...) \	printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)#else#define kdebug(FMT, ...) \	no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)#endif static struct kmem_cache *cred_jar; /* * The common credentials for the initial task's thread group */#ifdef CONFIG_KEYSstatic struct thread_group_cred init_tgcred = {	.usage	= ATOMIC_INIT(2),	.tgid	= 0,	.lock	= __SPIN_LOCK_UNLOCKED(init_cred.tgcred.lock),};#endif /* * The initial credentials for the initial task */struct cred init_cred = {	.usage			= ATOMIC_INIT(4),#ifdef CONFIG_DEBUG_CREDENTIALS	.subscribers		= ATOMIC_INIT(2),	.magic			= CRED_MAGIC,#endif	.securebits		= SECUREBITS_DEFAULT,	.cap_inheritable	= CAP_EMPTY_SET,	.cap_permitted		= CAP_FULL_SET,	.cap_effective		= CAP_FULL_SET,	.cap_bset		= CAP_FULL_SET,	.user			= INIT_USER,	.user_ns		= &init_user_ns,	.group_info		= &init_groups,#ifdef CONFIG_KEYS	.tgcred			= &init_tgcred,#endif}; static inline void set_cred_subscribers(struct cred *cred, int n){#ifdef CONFIG_DEBUG_CREDENTIALS	atomic_set(&cred->subscribers, n);#endif} static inline int read_cred_subscribers(const struct cred *cred){#ifdef CONFIG_DEBUG_CREDENTIALS	return atomic_read(&cred->subscribers);#else	return 0;#endif} static inline void alter_cred_subscribers(const struct cred *_cred, int n){#ifdef CONFIG_DEBUG_CREDENTIALS	struct cred *cred = (struct cred *) _cred; 	atomic_add(n, &cred->subscribers);#endif} /* * Dispose of the shared task group credentials */#ifdef CONFIG_KEYSstatic void release_tgcred_rcu(struct rcu_head *rcu){	struct thread_group_cred *tgcred =		container_of(rcu, struct thread_group_cred, rcu); 	BUG_ON(atomic_read(&tgcred->usage) != 0); 	key_put(tgcred->session_keyring);	key_put(tgcred->process_keyring);	kfree(tgcred);}#endif /* * Release a set of thread group credentials. */static void release_tgcred(struct cred *cred){#ifdef CONFIG_KEYS	struct thread_group_cred *tgcred = cred->tgcred; 	if (atomic_dec_and_test(&tgcred->usage))		call_rcu(&tgcred->rcu, release_tgcred_rcu);#endif} /* * The RCU callback to actually dispose of a set of credentials */static void put_cred_rcu(struct rcu_head *rcu){	struct cred *cred = container_of(rcu, struct cred, rcu); 	kdebug("put_cred_rcu(%p)", cred); #ifdef CONFIG_DEBUG_CREDENTIALS	if (cred->magic != CRED_MAGIC_DEAD ||	    atomic_read(&cred->usage) != 0 ||	    read_cred_subscribers(cred) != 0)		panic("CRED: put_cred_rcu() sees %p with"		      " mag %x, put %p, usage %d, subscr %d\n",		      cred, cred->magic, cred->put_addr,		      atomic_read(&cred->usage),		      read_cred_subscribers(cred));#else	if (atomic_read(&cred->usage) != 0)		panic("CRED: put_cred_rcu() sees %p with usage %d\n",		      cred, atomic_read(&cred->usage));#endif 	security_cred_free(cred);	key_put(cred->thread_keyring);	key_put(cred->request_key_auth);	release_tgcred(cred);	if (cred->group_info)		put_group_info(cred->group_info);	free_uid(cred->user);	kmem_cache_free(cred_jar, cred);} /** * __put_cred - Destroy a set of credentials * @cred: The record to release * * Destroy a set of credentials on which no references remain. */void __put_cred(struct cred *cred){	kdebug("__put_cred(%p{%d,%d})", cred,	       atomic_read(&cred->usage),	       read_cred_subscribers(cred)); 	BUG_ON(atomic_read(&cred->usage) != 0);#ifdef CONFIG_DEBUG_CREDENTIALS	BUG_ON(read_cred_subscribers(cred) != 0);	cred->magic = CRED_MAGIC_DEAD;	cred->put_addr = __builtin_return_address(0);#endif	BUG_ON(cred == current->cred);	BUG_ON(cred == current->real_cred); 	call_rcu(&cred->rcu, put_cred_rcu);}EXPORT_SYMBOL(__put_cred); /* * Clean up a task's credentials when it exits */void exit_creds(struct task_struct *tsk){	struct cred *cred; 	kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,	       atomic_read(&tsk->cred->usage),	       read_cred_subscribers(tsk->cred)); 	cred = (struct cred *) tsk->real_cred;	tsk->real_cred = NULL;	validate_creds(cred);	alter_cred_subscribers(cred, -1);	put_cred(cred); 	cred = (struct cred *) tsk->cred;	tsk->cred = NULL;	validate_creds(cred);	alter_cred_subscribers(cred, -1);	put_cred(cred); 	cred = (struct cred *) tsk->replacement_session_keyring;	if (cred) {		tsk->replacement_session_keyring = NULL;		validate_creds(cred);		put_cred(cred);	}} /** * get_task_cred - Get another task's objective credentials * @task: The task to query * * Get the objective credentials of a task, pinning them so that they can't go * away.  Accessing a task's credentials directly is not permitted. * * The caller must also make sure task doesn't get deleted, either by holding a * ref on task or by holding tasklist_lock to prevent it from being unlinked. */const struct cred *get_task_cred(struct task_struct *task){	const struct cred *cred; 	rcu_read_lock(); 	do {		cred = __task_cred((task));		BUG_ON(!cred);	} while (!atomic_inc_not_zero(&((struct cred *)cred)->usage)); 	rcu_read_unlock();	return cred;} /* * Allocate blank credentials, such that the credentials can be filled in at a * later date without risk of ENOMEM. */struct cred *cred_alloc_blank(void){	struct cred *new; 	new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);	if (!new)		return NULL; #ifdef CONFIG_KEYS	new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);	if (!new->tgcred) {		kmem_cache_free(cred_jar, new);		return NULL;	}	atomic_set(&new->tgcred->usage, 1);#endif 	atomic_set(&new->usage, 1);#ifdef CONFIG_DEBUG_CREDENTIALS	new->magic = CRED_MAGIC;#endif 	if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)		goto error; 	return new; error:	abort_creds(new);	return NULL;} /** * prepare_creds - Prepare a new set of credentials for modification * * Prepare a new set of task credentials for modification.  A task's creds * shouldn't generally be modified directly, therefore this function is used to * prepare a new copy, which the caller then modifies and then commits by * calling commit_creds(). * * Preparation involves making a copy of the objective creds for modification. * * Returns a pointer to the new creds-to-be if successful, NULL otherwise. * * Call commit_creds() or abort_creds() to clean up. */struct cred *prepare_creds(void){	struct task_struct *task = current;	const struct cred *old;	struct cred *new; 	validate_process_creds(); 	new = kmem_cache_alloc(cred_jar, GFP_KERNEL);	if (!new)		return NULL; 	kdebug("prepare_creds() alloc %p", new); 	old = task->cred;	memcpy(new, old, sizeof(struct cred)); 	atomic_set(&new->usage, 1);	set_cred_subscribers(new, 0);	get_group_info(new->group_info);	get_uid(new->user); #ifdef CONFIG_KEYS	key_get(new->thread_keyring);	key_get(new->request_key_auth);	atomic_inc(&new->tgcred->usage);#endif #ifdef CONFIG_SECURITY	new->security = NULL;#endif 	if (security_prepare_creds(new, old, GFP_KERNEL) < 0)		goto error;	validate_creds(new);	return new; error:	abort_creds(new);	return NULL;}EXPORT_SYMBOL(prepare_creds); /* * Prepare credentials for current to perform an execve() * - The caller must hold ->cred_guard_mutex */struct cred *prepare_exec_creds(void){	struct thread_group_cred *tgcred = NULL;	struct cred *new; #ifdef CONFIG_KEYS	tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);	if (!tgcred)		return NULL;#endif 	new = prepare_creds();	if (!new) {		kfree(tgcred);		return new;	} #ifdef CONFIG_KEYS	/* newly exec'd tasks don't get a thread keyring */	key_put(new->thread_keyring);	new->thread_keyring = NULL; 	/* create a new per-thread-group creds for all this set of threads to	 * share */	memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred)); 	atomic_set(&tgcred->usage, 1);	spin_lock_init(&tgcred->lock); 	/* inherit the session keyring; new process keyring */	key_get(tgcred->session_keyring);	tgcred->process_keyring = NULL; 	release_tgcred(new);	new->tgcred = tgcred;#endif 	return new;} /* * Copy credentials for the new process created by fork() * * We share if we can, but under some circumstances we have to generate a new * set. * * The new process gets the current process's subjective credentials as its * objective and subjective credentials */int copy_creds(struct task_struct *p, unsigned long clone_flags){#ifdef CONFIG_KEYS	struct thread_group_cred *tgcred;#endif	struct cred *new;	int ret; 	if (#ifdef CONFIG_KEYS		!p->cred->thread_keyring &&#endif		clone_flags & CLONE_THREAD	    ) {		p->real_cred = get_cred(p->cred);		get_cred(p->cred);		alter_cred_subscribers(p->cred, 2);		kdebug("share_creds(%p{%d,%d})",		       p->cred, atomic_read(&p->cred->usage),		       read_cred_subscribers(p->cred));		atomic_inc(&p->cred->user->processes);		return 0;	} 	new = prepare_creds();	if (!new)		return -ENOMEM; 	if (clone_flags & CLONE_NEWUSER) {		ret = create_user_ns(new);		if (ret < 0)			goto error_put;	} 	/* cache user_ns in cred.  Doesn't need a refcount because it will	 * stay pinned by cred->user	 */	new->user_ns = new->user->user_ns; #ifdef CONFIG_KEYS	/* new threads get their own thread keyrings if their parent already	 * had one */	if (new->thread_keyring) {		key_put(new->thread_keyring);		new->thread_keyring = NULL;		if (clone_flags & CLONE_THREAD)			install_thread_keyring_to_cred(new);	} 	/* we share the process and session keyrings between all the threads in	 * a process - this is slightly icky as we violate COW credentials a	 * bit */	if (!(clone_flags & CLONE_THREAD)) {		tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);		if (!tgcred) {			ret = -ENOMEM;			goto error_put;		}		atomic_set(&tgcred->usage, 1);		spin_lock_init(&tgcred->lock);		tgcred->process_keyring = NULL;		tgcred->session_keyring = key_get(new->tgcred->session_keyring); 		release_tgcred(new);		new->tgcred = tgcred;	}#endif 	atomic_inc(&new->user->processes);	p->cred = p->real_cred = get_cred(new);	alter_cred_subscribers(new, 2);	validate_creds(new);	return 0; error_put:	put_cred(new);	return ret;} /** * commit_creds - Install new credentials upon the current task * @new: The credentials to be assigned * * Install a new set of credentials to the current task, using RCU to replace * the old set.  Both the objective and the subjective credentials pointers are * updated.  This function may not be called if the subjective credentials are * in an overridden state. * * This function eats the caller's reference to the new credentials. * * Always returns 0 thus allowing this function to be tail-called at the end * of, say, sys_setgid(). */int commit_creds(struct cred *new){	struct task_struct *task = current;	const struct cred *old = task->real_cred; 	kdebug("commit_creds(%p{%d,%d})", new,	       atomic_read(&new->usage),	       read_cred_subscribers(new)); 	BUG_ON(task->cred != old);#ifdef CONFIG_DEBUG_CREDENTIALS	BUG_ON(read_cred_subscribers(old) < 2);	validate_creds(old);	validate_creds(new);#endif	BUG_ON(atomic_read(&new->usage) < 1); 	get_cred(new); /* we will require a ref for the subj creds too */ 	/* dumpability changes */	if (old->euid != new->euid ||	    old->egid != new->egid ||	    old->fsuid != new->fsuid ||	    old->fsgid != new->fsgid ||	    !cap_issubset(new->cap_permitted, old->cap_permitted)) {		if (task->mm)			set_dumpable(task->mm, suid_dumpable);		task->pdeath_signal = 0;

Lines 1–500 of 861.