Return-Path: Received: from mail-co1nam03on0103.outbound.protection.outlook.com ([104.47.40.103]:45124 "EHLO NAM03-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933149AbeDIAjg (ORCPT ); Sun, 8 Apr 2018 20:39:36 -0400 From: Sasha Levin To: "stable@vger.kernel.org" , "linux-kernel@vger.kernel.org" CC: Eric Biggers , David Howells , James Morris , Sasha Levin Subject: [PATCH AUTOSEL for 3.18 009/101] KEYS: put keyring if install_session_keyring_to_cred() fails Date: Mon, 9 Apr 2018 00:35:16 +0000 Message-ID: <20180409003505.164715-9-alexander.levin@microsoft.com> References: <20180409003505.164715-1-alexander.levin@microsoft.com> In-Reply-To: <20180409003505.164715-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: Eric Biggers [ Upstream commit d636bd9f12a66ea3775c9fabbf3f8e118253467a ] 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 Signed-off-by: James Morris Signed-off-by: Sasha Levin --- security/keys/process_keys.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/security/keys/process_keys.c b/security/keys/process_keys.c index 85b61a3ac981..6f32ca53456a 100644 --- a/security/keys/process_keys.c +++ b/security/keys/process_keys.c @@ -808,15 +808,14 @@ long join_session_keyring(const char *name) ret =3D PTR_ERR(keyring); goto error2; } else if (keyring =3D=3D new->session_keyring) { - key_put(keyring); ret =3D 0; - goto error2; + goto error3; } =20 /* we've got a keyring - now to install it */ ret =3D install_session_keyring_to_cred(new, keyring); if (ret < 0) - goto error2; + goto error3; =20 commit_creds(new); mutex_unlock(&key_session_mutex); @@ -826,6 +825,8 @@ long join_session_keyring(const char *name) okay: return ret; =20 +error3: + key_put(keyring); error2: mutex_unlock(&key_session_mutex); error: --=20 2.15.1