From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965016AbcATWCE (ORCPT ); Wed, 20 Jan 2016 17:02:04 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:40438 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S964978AbcATWB4 (ORCPT ); Wed, 20 Jan 2016 17:01:56 -0500 From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Petr Matousek , David Howells Subject: [PATCH 3.14 44/47] KEYS: Fix race between key destruction and finding a keyring by name Date: Wed, 20 Jan 2016 14:01:16 -0800 Message-Id: <20160120215514.224518169@linuxfoundation.org> X-Mailer: git-send-email 2.7.0 In-Reply-To: <20160120215507.575738941@linuxfoundation.org> References: <20160120215507.575738941@linuxfoundation.org> User-Agent: quilt/0.64 MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: David Howells commit 94c4554ba07adbdde396748ee7ae01e86cf2d8d7 upstream. There appears to be a race between: (1) key_gc_unused_keys() which frees key->security and then calls keyring_destroy() to unlink the name from the name list (2) find_keyring_by_name() which calls key_permission(), thus accessing key->security, on a key before checking to see whether the key usage is 0 (ie. the key is dead and might be cleaned up). Fix this by calling ->destroy() before cleaning up the core key data - including key->security. Reported-by: Petr Matousek Signed-off-by: David Howells Signed-off-by: Greg Kroah-Hartman --- security/keys/gc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/security/keys/gc.c +++ b/security/keys/gc.c @@ -143,6 +143,10 @@ static noinline void key_gc_unused_keys( kdebug("- %u", key->serial); key_check(key); + /* Throw away the key data */ + if (key->type->destroy) + key->type->destroy(key); + security_key_free(key); /* deal with the user's key tracking and quota */ @@ -157,10 +161,6 @@ static noinline void key_gc_unused_keys( if (test_bit(KEY_FLAG_INSTANTIATED, &key->flags)) atomic_dec(&key->user->nikeys); - /* now throw away the key memory */ - if (key->type->destroy) - key->type->destroy(key); - key_user_put(key->user); kfree(key->description);