[PATCH 3.18 09/69] Dont leak a key reference if request_key() tries to use a revoked keyring

From: Greg Kroah-Hartman
Date: Mon Dec 18 2017 - 13:33:31 EST


3.18-stable review patch. If anyone has any objections, please let me know.

------------------

From: David Jeffery <djeffery@xxxxxxxxxx>

commit d0709f1e66e8066c4ac6a54620ec116aa41937c0 upstream.

If a request_key() call to allocate and fill out a key attempts to insert the
key structure into a revoked keyring, the key will leak, using memory and part
of the user's key quota until the system reboots. This is from a failure of
construct_alloc_key() to decrement the key's reference count after the attempt
to insert into the requested keyring is rejected.

key_put() needs to be called in the link_prealloc_failed callpath to ensure
the unused key is released.

Signed-off-by: David Jeffery <djeffery@xxxxxxxxxx>
Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Signed-off-by: James Morris <james.l.morris@xxxxxxxxxx>
Signed-off-by: Eric Biggers <ebiggers@xxxxxxxxxx>
Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
---
security/keys/request_key.c | 1 +
1 file changed, 1 insertion(+)

--- a/security/keys/request_key.c
+++ b/security/keys/request_key.c
@@ -437,6 +437,7 @@ link_check_failed:

link_prealloc_failed:
mutex_unlock(&user->cons_lock);
+ key_put(key);
kleave(" = %d [prelink]", ret);
return ret;