CVE-2017-7472 (C, published 2017-05-11T19:29Z)
Description: value: The KEYS subsystem in the Linux kernel before 4.10.13 allows local users to cause a denial of service (memory consumption) via a series of KEY_REQKEY_DEFL_THREAD_KEYRING keyctl_set_reqkey_keyring calls.
Severity: MEDIUM (CVSS v3 base score: 5.5)
Weakness: CWE-404 \u2014 Improper Resource Shutdown or Release
The product does not release or incorrectly releases a resource before it is made available for re-use.
Repository: https://github.com/torvalds/linux
Files changed:
security/keys/keyctl.c
security/keys/process_keys.c
Commit message: KEYS: fix keyctl_set_reqkey_keyring() to not leak thread keyrings

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: d84f4f992cbd ("CRED: Inaugurate COW credentials")
Cc: stable@vger.kernel.org # 2.6.29+
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>


Vulnerable code:
 * 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 (ret < 0) {
			if (ret != -EEXIST)
				goto error;
			ret = 0;
		}
 * Install a fresh thread keyring directly to new credentials.  This keyring is
 * allowed to overrun the quota.
 * Install a fresh thread keyring, discarding the old one.
	BUG_ON(new->thread_keyring);

 * Install a process keyring directly to a credentials struct.
 * Returns -EEXIST if there was already a process keyring, 0 if one installed,
 * and other value on any other error
		return -EEXIST;
 * Make sure a process keyring is installed for the current process.  The
 * existing process keyring is not replaced.
 * Returns 0 if there is a process keyring by the end of this function, some
 * error otherwise.
		return ret != -EEXIST ? ret : 0;
 * Install a session keyring directly to a credentials struct.
 * Install a session keyring, discarding the old one.  If a keyring is not
 * supplied, an empty one is invented.

Fixed code:
 * 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.
		if (ret < 0)
			goto error;
 * 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.
	if (new->thread_keyring)
		return 0;

 * 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.
 * Install a process keyring to the given credentials struct if it didn't have
 * one already.  This is allowed to overrun the quota.
 * Return: 0 if a process keyring is now present; -errno on failure.
		return 0;
 * Install a process keyring to the current task if it didn't have one already.
 * Return: 0 if a process keyring is now present; -errno on failure.
		return ret;
 * 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.
 * 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.