From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753859AbdDCPmk (ORCPT ); Mon, 3 Apr 2017 11:42:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43248 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751716AbdDCPmI (ORCPT ); Mon, 3 Apr 2017 11:42:08 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A342E8046F Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dhowells@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A342E8046F Organization: Red Hat UK Ltd. Registered Address: Red Hat UK Ltd, Amberley Place, 107-111 Peascod Street, Windsor, Berkshire, SI4 1TE, United Kingdom. Registered in England and Wales under Company Registration No. 3798903 From: David Howells In-Reply-To: <20170401185416.10225-1-ebiggers3@gmail.com> References: <20170401185416.10225-1-ebiggers3@gmail.com> To: Eric Biggers Cc: dhowells@redhat.com, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, Eric Biggers Subject: Re: [PATCH] KEYS: put keyring if install_session_keyring_to_cred() fails MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2342.1491234125.1@warthog.procyon.org.uk> Date: Mon, 03 Apr 2017 16:42:05 +0100 Message-ID: <2347.1491234125@warthog.procyon.org.uk> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Mon, 03 Apr 2017 15:42:07 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Eric Biggers wrote: > +error3: > + key_put(keyring); > error2: > mutex_unlock(&key_session_mutex); I would prefer the put to be outside of the locked section. How about the attached instead? David --- commit 5f9e6fc4fdbbf9ada9bd03640b1c3ca50a3e5415 Author: Eric Biggers Date: Sat Apr 1 11:54:16 2017 -0700 KEYS: put keyring if install_session_keyring_to_cred() fails In join_session_keyring(), if install_session_keyring_to_cred() were to fail, we would leak the keyring reference, just like in the bug fixed by commit 23567fd052a9 ("KEYS: Fix keyring ref leak in join_session_keyring()"). Fortunately this cannot happen currently, but we really should be more careful. Do this by adding and using a new error label at which the keyring reference is dropped. Signed-off-by: Eric Biggers Signed-off-by: David Howells diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index 44451af828c0..8e98e5d1b5fb 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -793,21 +793,20 @@ long join_session_keyring(const char *name) KEY_ALLOC_IN_QUOTA, NULL, NULL); if (IS_ERR(keyring)) { ret = PTR_ERR(keyring); - goto error2; + goto error_unlock_no_keyring; } } else if (IS_ERR(keyring)) { ret = PTR_ERR(keyring); - goto error2; + goto error_unlock_no_keyring; } else if (keyring == new->session_keyring) { - key_put(keyring); ret = 0; - goto error2; + goto error_unlock; } /* we've got a keyring - now to install it */ ret = install_session_keyring_to_cred(new, keyring); if (ret < 0) - goto error2; + goto error_unlock; commit_creds(new); mutex_unlock(&key_session_mutex); @@ -817,8 +816,11 @@ long join_session_keyring(const char *name) okay: return ret; -error2: +error_unlock_no_keyring: + keyring = NULL; +error_unlock: mutex_unlock(&key_session_mutex); + key_put(keyring); error: abort_creds(new); return ret;