KEYS: fix keyctl_set_reqkey_keyring() to not leak thread keyrings
authorEric Biggers <ebiggers@google.com>
Tue, 18 Apr 2017 14:31:09 +0000 (15:31 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 27 Apr 2017 07:09:31 +0000 (09:09 +0200)
commitc9460fbceb2f3efa1d20050cdbffa51ec025745a
tree41e9fab9fa6b022aa79533d16a587a1c6202a7a3
parenteb78d987757967749d0b2e82fce0314697937ee5
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: d84f4f992cbd ("CRED: Inaugurate COW credentials")
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
security/keys/keyctl.c
security/keys/process_keys.c