KEYS: Make the session and process keyrings per-thread

Make the session keyring per-thread rather than per-process, but still
inherited from the parent thread to solve a problem with PAM and gdm.

The problem is that join_session_keyring() will reject attempts to change the
session keyring of a multithreaded program but gdm is now multithreaded before
it gets to the point of starting PAM and running pam_keyinit to create the
session keyring.  See:

	https://bugs.freedesktop.org/show_bug.cgi?id=49211

The reason that join_session_keyring() will only change the session keyring
under a single-threaded environment is that it's hard to alter the other
thread's credentials to effect the change in a multi-threaded program.  The
problems are such as:

 (1) How to prevent two threads both running join_session_keyring() from
     racing.

 (2) Another thread's credentials may not be modified directly by this process.

 (3) The number of threads is uncertain whilst we're not holding the
     appropriate spinlock, making preallocation slightly tricky.

 (4) We could use TIF_NOTIFY_RESUME and key_replace_session_keyring() to get
     another thread to replace its keyring, but that means preallocating for
     each thread.

A reasonable way around this is to make the session keyring per-thread rather
than per-process and just document that if you want a common session keyring,
you must get it before you spawn any threads - which is the current situation
anyway.

Whilst we're at it, we can the process keyring behave in the same way.  This
means we can clean up some of the ickyness in the creds code.

Basically, after this patch, the session, process and thread keyrings are about
inheritance rules only and not about sharing changes of keyring.

Reported-by: Mantas M. <grawity@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Ray Strode <rstrode@redhat.com>
This commit is contained in:
David Howells 2012-10-02 19:24:29 +01:00
parent a84a921978
commit 3a50597de8
5 changed files with 49 additions and 180 deletions

View File

@ -76,21 +76,6 @@ extern int groups_search(const struct group_info *, kgid_t);
extern int in_group_p(kgid_t); extern int in_group_p(kgid_t);
extern int in_egroup_p(kgid_t); extern int in_egroup_p(kgid_t);
/*
* The common credentials for a thread group
* - shared by CLONE_THREAD
*/
#ifdef CONFIG_KEYS
struct thread_group_cred {
atomic_t usage;
pid_t tgid; /* thread group process ID */
spinlock_t lock;
struct key __rcu *session_keyring; /* keyring inherited over fork */
struct key *process_keyring; /* keyring private to this process */
struct rcu_head rcu; /* RCU deletion hook */
};
#endif
/* /*
* The security context of a task * The security context of a task
* *
@ -139,6 +124,8 @@ struct cred {
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
unsigned char jit_keyring; /* default keyring to attach requested unsigned char jit_keyring; /* default keyring to attach requested
* keys to */ * keys to */
struct key __rcu *session_keyring; /* keyring inherited over fork */
struct key *process_keyring; /* keyring private to this process */
struct key *thread_keyring; /* keyring private to this thread */ struct key *thread_keyring; /* keyring private to this thread */
struct key *request_key_auth; /* assumed request_key authority */ struct key *request_key_auth; /* assumed request_key authority */
struct thread_group_cred *tgcred; /* thread-group shared credentials */ struct thread_group_cred *tgcred; /* thread-group shared credentials */

View File

@ -29,17 +29,6 @@
static struct kmem_cache *cred_jar; static struct kmem_cache *cred_jar;
/*
* The common credentials for the initial task's thread group
*/
#ifdef CONFIG_KEYS
static 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 * The initial credentials for the initial task
*/ */
@ -65,9 +54,6 @@ struct cred init_cred = {
.user = INIT_USER, .user = INIT_USER,
.user_ns = &init_user_ns, .user_ns = &init_user_ns,
.group_info = &init_groups, .group_info = &init_groups,
#ifdef CONFIG_KEYS
.tgcred = &init_tgcred,
#endif
}; };
static inline void set_cred_subscribers(struct cred *cred, int n) static inline void set_cred_subscribers(struct cred *cred, int n)
@ -95,36 +81,6 @@ static inline void alter_cred_subscribers(const struct cred *_cred, int n)
#endif #endif
} }
/*
* Dispose of the shared task group credentials
*/
#ifdef CONFIG_KEYS
static 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 * The RCU callback to actually dispose of a set of credentials
*/ */
@ -150,9 +106,10 @@ static void put_cred_rcu(struct rcu_head *rcu)
#endif #endif
security_cred_free(cred); security_cred_free(cred);
key_put(cred->session_keyring);
key_put(cred->process_keyring);
key_put(cred->thread_keyring); key_put(cred->thread_keyring);
key_put(cred->request_key_auth); key_put(cred->request_key_auth);
release_tgcred(cred);
if (cred->group_info) if (cred->group_info)
put_group_info(cred->group_info); put_group_info(cred->group_info);
free_uid(cred->user); free_uid(cred->user);
@ -246,15 +203,6 @@ struct cred *cred_alloc_blank(void)
if (!new) if (!new)
return NULL; 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); atomic_set(&new->usage, 1);
#ifdef CONFIG_DEBUG_CREDENTIALS #ifdef CONFIG_DEBUG_CREDENTIALS
new->magic = CRED_MAGIC; new->magic = CRED_MAGIC;
@ -308,9 +256,10 @@ struct cred *prepare_creds(void)
get_user_ns(new->user_ns); get_user_ns(new->user_ns);
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
key_get(new->session_keyring);
key_get(new->process_keyring);
key_get(new->thread_keyring); key_get(new->thread_keyring);
key_get(new->request_key_auth); key_get(new->request_key_auth);
atomic_inc(&new->tgcred->usage);
#endif #endif
#ifdef CONFIG_SECURITY #ifdef CONFIG_SECURITY
@ -334,39 +283,20 @@ EXPORT_SYMBOL(prepare_creds);
*/ */
struct cred *prepare_exec_creds(void) struct cred *prepare_exec_creds(void)
{ {
struct thread_group_cred *tgcred = NULL;
struct cred *new; struct cred *new;
#ifdef CONFIG_KEYS
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
if (!tgcred)
return NULL;
#endif
new = prepare_creds(); new = prepare_creds();
if (!new) { if (!new)
kfree(tgcred);
return new; return new;
}
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
/* newly exec'd tasks don't get a thread keyring */ /* newly exec'd tasks don't get a thread keyring */
key_put(new->thread_keyring); key_put(new->thread_keyring);
new->thread_keyring = NULL; 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 */ /* inherit the session keyring; new process keyring */
key_get(tgcred->session_keyring); key_put(new->process_keyring);
tgcred->process_keyring = NULL; new->process_keyring = NULL;
release_tgcred(new);
new->tgcred = tgcred;
#endif #endif
return new; return new;
@ -383,9 +313,6 @@ struct cred *prepare_exec_creds(void)
*/ */
int copy_creds(struct task_struct *p, unsigned long clone_flags) int copy_creds(struct task_struct *p, unsigned long clone_flags)
{ {
#ifdef CONFIG_KEYS
struct thread_group_cred *tgcred;
#endif
struct cred *new; struct cred *new;
int ret; int ret;
@ -425,22 +352,12 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
install_thread_keyring_to_cred(new); install_thread_keyring_to_cred(new);
} }
/* we share the process and session keyrings between all the threads in /* The process keyring is only shared between the threads in a process;
* a process - this is slightly icky as we violate COW credentials a * anything outside of those threads doesn't inherit.
* bit */ */
if (!(clone_flags & CLONE_THREAD)) { if (!(clone_flags & CLONE_THREAD)) {
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL); key_put(new->process_keyring);
if (!tgcred) { new->process_keyring = NULL;
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 #endif
@ -643,9 +560,6 @@ void __init cred_init(void)
*/ */
struct cred *prepare_kernel_cred(struct task_struct *daemon) struct cred *prepare_kernel_cred(struct task_struct *daemon)
{ {
#ifdef CONFIG_KEYS
struct thread_group_cred *tgcred;
#endif
const struct cred *old; const struct cred *old;
struct cred *new; struct cred *new;
@ -653,14 +567,6 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
if (!new) if (!new)
return NULL; return NULL;
#ifdef CONFIG_KEYS
tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
if (!tgcred) {
kmem_cache_free(cred_jar, new);
return NULL;
}
#endif
kdebug("prepare_kernel_cred() alloc %p", new); kdebug("prepare_kernel_cred() alloc %p", new);
if (daemon) if (daemon)
@ -678,13 +584,10 @@ struct cred *prepare_kernel_cred(struct task_struct *daemon)
get_group_info(new->group_info); get_group_info(new->group_info);
#ifdef CONFIG_KEYS #ifdef CONFIG_KEYS
atomic_set(&tgcred->usage, 1); new->session_keyring = NULL;
spin_lock_init(&tgcred->lock); new->process_keyring = NULL;
tgcred->process_keyring = NULL;
tgcred->session_keyring = NULL;
new->tgcred = tgcred;
new->request_key_auth = NULL;
new->thread_keyring = NULL; new->thread_keyring = NULL;
new->request_key_auth = NULL;
new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING; new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
#endif #endif

View File

@ -1475,7 +1475,8 @@ long keyctl_session_to_parent(void)
goto error_keyring; goto error_keyring;
newwork = &cred->rcu; newwork = &cred->rcu;
cred->tgcred->session_keyring = key_ref_to_ptr(keyring_r); cred->session_keyring = key_ref_to_ptr(keyring_r);
keyring_r = NULL;
init_task_work(newwork, key_change_session_keyring); init_task_work(newwork, key_change_session_keyring);
me = current; me = current;
@ -1500,7 +1501,7 @@ long keyctl_session_to_parent(void)
mycred = current_cred(); mycred = current_cred();
pcred = __task_cred(parent); pcred = __task_cred(parent);
if (mycred == pcred || if (mycred == pcred ||
mycred->tgcred->session_keyring == pcred->tgcred->session_keyring) { mycred->session_keyring == pcred->session_keyring) {
ret = 0; ret = 0;
goto unlock; goto unlock;
} }
@ -1516,9 +1517,9 @@ long keyctl_session_to_parent(void)
goto unlock; goto unlock;
/* the keyrings must have the same UID */ /* the keyrings must have the same UID */
if ((pcred->tgcred->session_keyring && if ((pcred->session_keyring &&
pcred->tgcred->session_keyring->uid != mycred->euid) || pcred->session_keyring->uid != mycred->euid) ||
mycred->tgcred->session_keyring->uid != mycred->euid) mycred->session_keyring->uid != mycred->euid)
goto unlock; goto unlock;
/* cancel an already pending keyring replacement */ /* cancel an already pending keyring replacement */

View File

@ -169,9 +169,8 @@ static int install_thread_keyring(void)
int install_process_keyring_to_cred(struct cred *new) int install_process_keyring_to_cred(struct cred *new)
{ {
struct key *keyring; struct key *keyring;
int ret;
if (new->tgcred->process_keyring) if (new->process_keyring)
return -EEXIST; return -EEXIST;
keyring = keyring_alloc("_pid", new->uid, new->gid, keyring = keyring_alloc("_pid", new->uid, new->gid,
@ -179,17 +178,8 @@ int install_process_keyring_to_cred(struct cred *new)
if (IS_ERR(keyring)) if (IS_ERR(keyring))
return PTR_ERR(keyring); return PTR_ERR(keyring);
spin_lock_irq(&new->tgcred->lock); new->process_keyring = keyring;
if (!new->tgcred->process_keyring) { return 0;
new->tgcred->process_keyring = keyring;
keyring = NULL;
ret = 0;
} else {
ret = -EEXIST;
}
spin_unlock_irq(&new->tgcred->lock);
key_put(keyring);
return ret;
} }
/* /*
@ -230,7 +220,7 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
/* create an empty session keyring */ /* create an empty session keyring */
if (!keyring) { if (!keyring) {
flags = KEY_ALLOC_QUOTA_OVERRUN; flags = KEY_ALLOC_QUOTA_OVERRUN;
if (cred->tgcred->session_keyring) if (cred->session_keyring)
flags = KEY_ALLOC_IN_QUOTA; flags = KEY_ALLOC_IN_QUOTA;
keyring = keyring_alloc("_ses", cred->uid, cred->gid, keyring = keyring_alloc("_ses", cred->uid, cred->gid,
@ -242,17 +232,11 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
} }
/* install the keyring */ /* install the keyring */
spin_lock_irq(&cred->tgcred->lock); old = cred->session_keyring;
old = cred->tgcred->session_keyring; rcu_assign_pointer(cred->session_keyring, keyring);
rcu_assign_pointer(cred->tgcred->session_keyring, keyring);
spin_unlock_irq(&cred->tgcred->lock);
/* we're using RCU on the pointer, but there's no point synchronising if (old)
* on it if it didn't previously point to anything */
if (old) {
synchronize_rcu();
key_put(old); key_put(old);
}
return 0; return 0;
} }
@ -367,9 +351,9 @@ key_ref_t search_my_process_keyrings(struct key_type *type,
} }
/* search the process keyring second */ /* search the process keyring second */
if (cred->tgcred->process_keyring) { if (cred->process_keyring) {
key_ref = keyring_search_aux( key_ref = keyring_search_aux(
make_key_ref(cred->tgcred->process_keyring, 1), make_key_ref(cred->process_keyring, 1),
cred, type, description, match, no_state_check); cred, type, description, match, no_state_check);
if (!IS_ERR(key_ref)) if (!IS_ERR(key_ref))
goto found; goto found;
@ -388,12 +372,10 @@ key_ref_t search_my_process_keyrings(struct key_type *type,
} }
/* search the session keyring */ /* search the session keyring */
if (cred->tgcred->session_keyring) { if (cred->session_keyring) {
rcu_read_lock(); rcu_read_lock();
key_ref = keyring_search_aux( key_ref = keyring_search_aux(
make_key_ref(rcu_dereference( make_key_ref(rcu_dereference(cred->session_keyring), 1),
cred->tgcred->session_keyring),
1),
cred, type, description, match, no_state_check); cred, type, description, match, no_state_check);
rcu_read_unlock(); rcu_read_unlock();
@ -563,7 +545,7 @@ try_again:
break; break;
case KEY_SPEC_PROCESS_KEYRING: case KEY_SPEC_PROCESS_KEYRING:
if (!cred->tgcred->process_keyring) { if (!cred->process_keyring) {
if (!(lflags & KEY_LOOKUP_CREATE)) if (!(lflags & KEY_LOOKUP_CREATE))
goto error; goto error;
@ -575,13 +557,13 @@ try_again:
goto reget_creds; goto reget_creds;
} }
key = cred->tgcred->process_keyring; key = cred->process_keyring;
atomic_inc(&key->usage); atomic_inc(&key->usage);
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
break; break;
case KEY_SPEC_SESSION_KEYRING: case KEY_SPEC_SESSION_KEYRING:
if (!cred->tgcred->session_keyring) { if (!cred->session_keyring) {
/* always install a session keyring upon access if one /* always install a session keyring upon access if one
* doesn't exist yet */ * doesn't exist yet */
ret = install_user_keyrings(); ret = install_user_keyrings();
@ -596,7 +578,7 @@ try_again:
if (ret < 0) if (ret < 0)
goto error; goto error;
goto reget_creds; goto reget_creds;
} else if (cred->tgcred->session_keyring == } else if (cred->session_keyring ==
cred->user->session_keyring && cred->user->session_keyring &&
lflags & KEY_LOOKUP_CREATE) { lflags & KEY_LOOKUP_CREATE) {
ret = join_session_keyring(NULL); ret = join_session_keyring(NULL);
@ -606,7 +588,7 @@ try_again:
} }
rcu_read_lock(); rcu_read_lock();
key = rcu_dereference(cred->tgcred->session_keyring); key = rcu_dereference(cred->session_keyring);
atomic_inc(&key->usage); atomic_inc(&key->usage);
rcu_read_unlock(); rcu_read_unlock();
key_ref = make_key_ref(key, 1); key_ref = make_key_ref(key, 1);
@ -766,12 +748,6 @@ long join_session_keyring(const char *name)
struct key *keyring; struct key *keyring;
long ret, serial; long ret, serial;
/* only permit this if there's a single thread in the thread group -
* this avoids us having to adjust the creds on all threads and risking
* ENOMEM */
if (!current_is_single_threaded())
return -EMLINK;
new = prepare_creds(); new = prepare_creds();
if (!new) if (!new)
return -ENOMEM; return -ENOMEM;
@ -783,7 +759,7 @@ long join_session_keyring(const char *name)
if (ret < 0) if (ret < 0)
goto error; goto error;
serial = new->tgcred->session_keyring->serial; serial = new->session_keyring->serial;
ret = commit_creds(new); ret = commit_creds(new);
if (ret == 0) if (ret == 0)
ret = serial; ret = serial;
@ -806,6 +782,9 @@ long join_session_keyring(const char *name)
} else if (IS_ERR(keyring)) { } else if (IS_ERR(keyring)) {
ret = PTR_ERR(keyring); ret = PTR_ERR(keyring);
goto error2; goto error2;
} else if (keyring == new->session_keyring) {
ret = 0;
goto error2;
} }
/* we've got a keyring - now to install it */ /* we've got a keyring - now to install it */
@ -862,8 +841,7 @@ void key_change_session_keyring(struct callback_head *twork)
new->jit_keyring = old->jit_keyring; new->jit_keyring = old->jit_keyring;
new->thread_keyring = key_get(old->thread_keyring); new->thread_keyring = key_get(old->thread_keyring);
new->tgcred->tgid = old->tgcred->tgid; new->process_keyring = key_get(old->process_keyring);
new->tgcred->process_keyring = key_get(old->tgcred->process_keyring);
security_transfer_creds(new, old); security_transfer_creds(new, old);

View File

@ -150,12 +150,12 @@ static int call_sbin_request_key(struct key_construction *cons,
cred->thread_keyring ? cred->thread_keyring->serial : 0); cred->thread_keyring ? cred->thread_keyring->serial : 0);
prkey = 0; prkey = 0;
if (cred->tgcred->process_keyring) if (cred->process_keyring)
prkey = cred->tgcred->process_keyring->serial; prkey = cred->process_keyring->serial;
sprintf(keyring_str[1], "%d", prkey); sprintf(keyring_str[1], "%d", prkey);
rcu_read_lock(); rcu_read_lock();
session = rcu_dereference(cred->tgcred->session_keyring); session = rcu_dereference(cred->session_keyring);
if (!session) if (!session)
session = cred->user->session_keyring; session = cred->user->session_keyring;
sskey = session->serial; sskey = session->serial;
@ -297,14 +297,14 @@ static void construct_get_dest_keyring(struct key **_dest_keyring)
break; break;
case KEY_REQKEY_DEFL_PROCESS_KEYRING: case KEY_REQKEY_DEFL_PROCESS_KEYRING:
dest_keyring = key_get(cred->tgcred->process_keyring); dest_keyring = key_get(cred->process_keyring);
if (dest_keyring) if (dest_keyring)
break; break;
case KEY_REQKEY_DEFL_SESSION_KEYRING: case KEY_REQKEY_DEFL_SESSION_KEYRING:
rcu_read_lock(); rcu_read_lock();
dest_keyring = key_get( dest_keyring = key_get(
rcu_dereference(cred->tgcred->session_keyring)); rcu_dereference(cred->session_keyring));
rcu_read_unlock(); rcu_read_unlock();
if (dest_keyring) if (dest_keyring)