Permalink
Browse files

KEYS: fix keyctl_set_reqkey_keyring() to not leak thread keyrings

commit c9f838d104fed6f2f61d68164712e3204bf5271b upstream.

This fixes CVE-2017-7472.

Running the following program as an unprivileged user exhausts kernel
memory by leaking thread keyrings:

	#include <keyutils.h>

	int main()
	{
		for (;;)
			keyctl_set_reqkey_keyring(KEY_REQKEY_DEFL_THREAD_KEYRING);
	}

Fix it by only creating a new thread keyring if there wasn't one before.
To make things more consistent, make install_thread_keyring_to_cred()
and install_process_keyring_to_cred() both return 0 if the corresponding
keyring is already present.

Fixes: d84f4f9 ("CRED: Inaugurate COW credentials")
Change-Id: Idff7f480c41a3b4c57c80e15cd6f1d21040581d5
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
  • Loading branch information...
ebiggers authored and andi34 committed Apr 18, 2017
1 parent 6802c51 commit 0a176f650b020b2ce10a84d7f850ed2448431839
Showing with 31 additions and 24 deletions.
  1. +4 −7 security/keys/keyctl.c
  2. +27 −17 security/keys/process_keys.c
View
@@ -1179,8 +1179,8 @@ long keyctl_reject_key(key_serial_t id, unsigned timeout, unsigned error,
* Read or set the default keyring in which request_key() will cache keys and
* return the old setting.
*
* If a process keyring is specified then this will be created if it doesn't
* yet exist. The old setting will be returned if successful.
* If a thread or process keyring is specified then it will be created if it
* doesn't yet exist. The old setting will be returned if successful.
*/
long keyctl_set_reqkey_keyring(int reqkey_defl)
{
@@ -1205,11 +1205,8 @@ long keyctl_set_reqkey_keyring(int reqkey_defl)
case KEY_REQKEY_DEFL_PROCESS_KEYRING:
ret = install_process_keyring_to_cred(new);
if (ret < 0) {
if (ret != -EEXIST)
goto error;
ret = 0;
}
if (ret < 0)
goto error;
goto set;
case KEY_REQKEY_DEFL_DEFAULT:
@@ -121,13 +121,18 @@ int install_user_keyrings(void)
}
/*
* Install a fresh thread keyring directly to new credentials. This keyring is
* allowed to overrun the quota.
* Install a thread keyring to the given credentials struct if it didn't have
* one already. This is allowed to overrun the quota.
*
* Return: 0 if a thread keyring is now present; -errno on failure.
*/
int install_thread_keyring_to_cred(struct cred *new)
{
struct key *keyring;
if (new->thread_keyring)
return 0;
keyring = keyring_alloc("_tid", new->uid, new->gid, new,
KEY_ALLOC_QUOTA_OVERRUN, NULL);
if (IS_ERR(keyring))
@@ -138,7 +143,9 @@ int install_thread_keyring_to_cred(struct cred *new)
}
/*
* Install a fresh thread keyring, discarding the old one.
* Install a thread keyring to the current task if it didn't have one already.
*
* Return: 0 if a thread keyring is now present; -errno on failure.
*/
static int install_thread_keyring(void)
{
@@ -149,8 +156,6 @@ static int install_thread_keyring(void)
if (!new)
return -ENOMEM;
BUG_ON(new->thread_keyring);
ret = install_thread_keyring_to_cred(new);
if (ret < 0) {
abort_creds(new);
@@ -161,18 +166,18 @@ static int install_thread_keyring(void)
}
/*
* Install a process keyring directly to a credentials struct.
* Install a process keyring to the given credentials struct if it didn't have
* one already. This is allowed to overrun the quota.
*
* Returns -EEXIST if there was already a process keyring, 0 if one installed,
* and other value on any other error
* Return: 0 if a process keyring is now present; -errno on failure.
*/
int install_process_keyring_to_cred(struct cred *new)
{
struct key *keyring;
int ret;
if (new->tgcred->process_keyring)
return -EEXIST;
return 0;
keyring = keyring_alloc("_pid", new->uid, new->gid,
new, KEY_ALLOC_QUOTA_OVERRUN, NULL);
@@ -193,11 +198,9 @@ int install_process_keyring_to_cred(struct cred *new)
}
/*
* Make sure a process keyring is installed for the current process. The
* existing process keyring is not replaced.
* Install a process keyring to the current task if it didn't have one already.
*
* Returns 0 if there is a process keyring by the end of this function, some
* error otherwise.
* Return: 0 if a process keyring is now present; -errno on failure.
*/
static int install_process_keyring(void)
{
@@ -211,14 +214,18 @@ static int install_process_keyring(void)
ret = install_process_keyring_to_cred(new);
if (ret < 0) {
abort_creds(new);
return ret != -EEXIST ? ret : 0;
return ret;
}
return commit_creds(new);
}
/*
* Install a session keyring directly to a credentials struct.
* Install the given keyring as the session keyring of the given credentials
* struct, replacing the existing one if any. If the given keyring is NULL,
* then install a new anonymous session keyring.
*
* Return: 0 on success; -errno on failure.
*/
int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
{
@@ -258,8 +265,11 @@ int install_session_keyring_to_cred(struct cred *cred, struct key *keyring)
}
/*
* Install a session keyring, discarding the old one. If a keyring is not
* supplied, an empty one is invented.
* Install the given keyring as the session keyring of the current task,
* replacing the existing one if any. If the given keyring is NULL, then
* install a new anonymous session keyring.
*
* Return: 0 on success; -errno on failure.
*/
static int install_session_keyring(struct key *keyring)
{

0 comments on commit 0a176f6

Please sign in to comment.