Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp2164277pxp; Mon, 7 Mar 2022 09:34:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJz2wV4YgIzjRGDpWsfOsEw5+1fbZO6uwZTuWXwL/envphPayiG8/7dkf2V/808+OXUdXzm7 X-Received: by 2002:a05:6402:b42:b0:415:e6e7:65aa with SMTP id bx2-20020a0564020b4200b00415e6e765aamr11797718edb.105.1646674448902; Mon, 07 Mar 2022 09:34:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646674448; cv=none; d=google.com; s=arc-20160816; b=O6cmVHKZX2eVyV0KCvwmJOE02NBKuKqitRc4r+ebFdmRFlGhlM7ZhuPHntssiUImUa jXXftXO3rtSTYTmAoS13V4GyQnBlEYnyPZNRnQfM2vYdd8RCliSxXDygWWJoGi7Ctp4u EgeiSadvH2yxYXzi+Tam8i7VvUnHN6EbP2CHbgix1kMy6UVI2Mn6vWRMniB2C42R90hk dhQZoY4cmwzOsFw3qRsyVpGYp9JaKzejcGSqUD4LdAUbNGazHUyF6F/acBjlhQ1A8rhW RdLMiwKwdtqzhJ1hPqDxuzOzuBISNxCp6VsJwhFQlOc+1LAbbA6pmpOjrCQIc3DP/+/B klrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id:dkim-signature:dkim-signature; bh=MNP/NRlKlAH+4lQNCgxKx9sriuYjbyzguFWzMZtIYSw=; b=CXaqHpGdJgS/14htqU7DLv03ot2AMZv55FC4N1AgWYh9/OVAmR+KCs75z/cN7XDiG2 AkNj49HKcH/22GOGuzl+sauGkQrqr2DCj+bQICgLHhS7O5ywHfmbKC3dKDGTmGtflXPb OAY2JSviFsfsG7FWNaRBEVzWxkpK1HSe+Q+x0QnqVOWYGlLZ9EozFWDiwPz4qQk2crze 6cxKtXp6vcb6CvDecmcZ1TJE77GGhddKzUszOysPsieRhRLdSBWik3M2xUmM/Uz0bLVA kD/XlHk93ZK4RsRRGq2dHfgoFMtjYl+iNyUHS2+JOHVSzordD6cPHgkTTr1Iq6UcIi6G WBiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@hansenpartnership.com header.s=20151216 header.b="JseSE/lS"; dkim=pass header.i=@hansenpartnership.com header.s=20151216 header.b="JseSE/lS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=hansenpartnership.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a13-20020a50e70d000000b004132afe1c81si9157275edn.143.2022.03.07.09.33.24; Mon, 07 Mar 2022 09:34:08 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@hansenpartnership.com header.s=20151216 header.b="JseSE/lS"; dkim=pass header.i=@hansenpartnership.com header.s=20151216 header.b="JseSE/lS"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=hansenpartnership.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244130AbiCGQXc (ORCPT + 99 others); Mon, 7 Mar 2022 11:23:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241657AbiCGQX3 (ORCPT ); Mon, 7 Mar 2022 11:23:29 -0500 Received: from bedivere.hansenpartnership.com (bedivere.hansenpartnership.com [IPv6:2607:fcd0:100:8a00::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39AD4BF66; Mon, 7 Mar 2022 08:22:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=hansenpartnership.com; s=20151216; t=1646670149; bh=ptRoAb8mg6/U7TkOzs/YecjHNLhRU4WkHJrUaIumxVs=; h=Message-ID:Subject:From:To:Date:In-Reply-To:References:From; b=JseSE/lSZFukslI2nJr8C5heEK+iyAPk2P4gAKHROrZlmr34i7jdH4qwPHAFQf+wj xkXDESYVEe+/EigfyuGtyfp4YD8Aug7+h1Yi3+M5Wx/5cEuoa7/GbZPYj1hvUlLs5h iTWneSMRjINEh1yNluaUymo1DhN/AjEH/7pQCFSw= Received: from localhost (localhost [127.0.0.1]) by bedivere.hansenpartnership.com (Postfix) with ESMTP id D74111280BC9; Mon, 7 Mar 2022 11:22:29 -0500 (EST) Received: from bedivere.hansenpartnership.com ([127.0.0.1]) by localhost (bedivere.hansenpartnership.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id KMOW5Aw_jOo3; Mon, 7 Mar 2022 11:22:29 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=hansenpartnership.com; s=20151216; t=1646670149; bh=ptRoAb8mg6/U7TkOzs/YecjHNLhRU4WkHJrUaIumxVs=; h=Message-ID:Subject:From:To:Date:In-Reply-To:References:From; b=JseSE/lSZFukslI2nJr8C5heEK+iyAPk2P4gAKHROrZlmr34i7jdH4qwPHAFQf+wj xkXDESYVEe+/EigfyuGtyfp4YD8Aug7+h1Yi3+M5Wx/5cEuoa7/GbZPYj1hvUlLs5h iTWneSMRjINEh1yNluaUymo1DhN/AjEH/7pQCFSw= Received: from jarvis.int.hansenpartnership.com (unknown [IPv6:2601:5c4:4300:c551::527]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bedivere.hansenpartnership.com (Postfix) with ESMTPSA id 13CFC1280BC6; Mon, 7 Mar 2022 11:22:29 -0500 (EST) Message-ID: <120839d2925750e278810b2ad211f5465638f7b0.camel@HansenPartnership.com> Subject: Re: [PATCH v2] tpm: Rework open/close/shutdown to avoid races From: James Bottomley To: Jarkko Sakkinen Cc: Sergey Temerkhanov , Peter Huewe , Jason Gunthorpe , Jerry Snitselaar , linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org Date: Mon, 07 Mar 2022 11:22:27 -0500 In-Reply-To: References: <20201215133801.546207-1-s.temerkhanov@gmail.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.34.4 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2022-03-07 at 17:41 +0200, Jarkko Sakkinen wrote: [...] > James, would it possible for you to construct a proper patch from > this and send it so we could include it to the next PR (use my tree > as baseline)? Sure, There's not really any identifiable patch for a fixes tag, since the race really always existed. How about the below. James -------------------8>8>8><8<8<8------------------ From 3e0e640e47e5728b68693c5d45bf3cdd0bff48ca Mon Sep 17 00:00:00 2001 From: James Bottomley Date: Mon, 7 Mar 2022 11:16:55 -0500 Subject: [PATCH] tpm: use try_get_ops() in tpm-space.c As part of the series conversion to remove nested TPM operations: https://lore.kernel.org/all/20190205224723.19671-1-jarkko.sakkinen@linux.intel.com/ exposure of the chip->tpm_mutex was removed from much of the upper level code. In this conversion, tpm2_del_space() was missed. This didn't matter much because it's usually called closely after a converted operation, so there's only a very tiny race window where the chip can be removed before the space flushing is done which causes a NULL deref on the mutex. However, there are reports of this window being hit in practice, so fix this by converting tpm2_del_space() to use tpm_try_get_ops(), which performs all the teardown checks before acquring the mutex. Signed-off-by: James Bottomley --- drivers/char/tpm/tpm2-space.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c index 265ec72b1d81..ffb35f0154c1 100644 --- a/drivers/char/tpm/tpm2-space.c +++ b/drivers/char/tpm/tpm2-space.c @@ -58,12 +58,12 @@ int tpm2_init_space(struct tpm_space *space, unsigned int buf_size) void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space) { - mutex_lock(&chip->tpm_mutex); - if (!tpm_chip_start(chip)) { + + if (tpm_try_get_ops(chip) == 0) { tpm2_flush_sessions(chip, space); - tpm_chip_stop(chip); + tpm_put_ops(chip); } - mutex_unlock(&chip->tpm_mutex); + kfree(space->context_buf); kfree(space->session_buf); } -- 2.34.1