From patchwork Wed Apr 26 17:29:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13224816 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A54CC7618E for ; Wed, 26 Apr 2023 17:29:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238432AbjDZR3j (ORCPT ); Wed, 26 Apr 2023 13:29:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54418 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233678AbjDZR3i (ORCPT ); Wed, 26 Apr 2023 13:29:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4425EBE; Wed, 26 Apr 2023 10:29:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D76856232F; Wed, 26 Apr 2023 17:29:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EAEE3C433EF; Wed, 26 Apr 2023 17:29:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682530176; bh=dcW/YGcfbxh1/KUv8M6Wk49tlitkkdIukS6SS0m9pEQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=T0d38LH2IrCAUzZ3Ljnkpq7lWYQ10iQ7lD0UNtxp3dtB1QhtTGKYwRMaVwe+Q383d r32hskaz+qbLvPgwaj5D6xRl9HRWAGOGcyBjsinyo4WyEeBSF7Lthir2RCTXtnR57m XFRTFKUUB2Uob7VCrAlDqRFlBSyO9Nq+S+LDnHZWnOKfU5KtwfeQf6Ug2EDa9AHU0e KVCEFaVuck9yig1f7nyW2026ZzDYJlwZ5zoAxTja2kR53ExIRld5CRf995ZyChBKTV Ac0jLjllKJqr+Pm85FC/34lFX67KmHeZ+Hy0MOnDS/AdWa48QuixooT1BKCLE6+6qi LotndTosOc+FQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Huewe , Jarkko Sakkinen , Jason Gunthorpe , Jerry Snitselaar , Stefan Berger , James Bottomley Cc: stable@vger.kernel.org, "Jason A . Donenfeld" Subject: [PATCH 1/2] tpm_tis: Use tpm_chip_{start,stop} decoration inside tpm_tis_resume Date: Wed, 26 Apr 2023 20:29:27 +0300 Message-Id: <20230426172928.3963287-2-jarkko@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230426172928.3963287-1-jarkko@kernel.org> References: <20230426172928.3963287-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Before sending a TPM command, CLKRUN protocol must be disabled. This is not done in the case of tpm1_do_selftest() call site inside tpm_tis_resume(). Address this by decorating the calls with tpm_chip_{start,stop}, which arm and disarm the TPM chip for transmission, and take care of disabling and re-enabling CLKRUN, among other things. Cc: stable@vger.kernel.org Reported-by: Jason A. Donenfeld Link: https://lore.kernel.org/linux-integrity/CS68AWILHXS4.3M36M1EKZLUMS@suppilovahvero/ Fixes: a3fbfae82b4c ("tpm: take TPM chip power gating out of tpm_transmit()") Signed-off-by: Jarkko Sakkinen Reviewed-by: Jerry Snitselaar ops->clk_enable != NULL) - chip->ops->clk_enable(chip, true); - - /* reenable interrupts that device may have lost or - * BIOS/firmware may have disabled + /* + * Re-enable interrupts that device may have lost or BIOS/firmware may + * have disabled. */ rc = tpm_tis_write8(priv, TPM_INT_VECTOR(priv->locality), priv->irq); - if (rc < 0) - goto out; + if (rc < 0) { + dev_err(&chip->dev, "Setting IRQ failed.\n"); + return; + } intmask = priv->int_mask | TPM_GLOBAL_INT_ENABLE; - - tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); - -out: - if (chip->ops->clk_enable != NULL) - chip->ops->clk_enable(chip, false); - - return; + rc = tpm_tis_write32(priv, TPM_INT_ENABLE(priv->locality), intmask); + if (rc < 0) + dev_err(&chip->dev, "Enabling interrupts failed.\n"); } int tpm_tis_resume(struct device *dev) @@ -1235,27 +1230,27 @@ int tpm_tis_resume(struct device *dev) struct tpm_chip *chip = dev_get_drvdata(dev); int ret; - ret = tpm_tis_request_locality(chip, 0); - if (ret < 0) + ret = tpm_chip_start(chip); + if (ret) return ret; if (chip->flags & TPM_CHIP_FLAG_IRQ) tpm_tis_reenable_interrupts(chip); - ret = tpm_pm_resume(dev); - if (ret) - goto out; - /* * TPM 1.2 requires self-test on resume. This function actually returns * an error code but for unknown reason it isn't handled. */ if (!(chip->flags & TPM_CHIP_FLAG_TPM2)) tpm1_do_selftest(chip); -out: - tpm_tis_relinquish_locality(chip, 0); - return ret; + tpm_chip_stop(chip); + + ret = tpm_pm_resume(dev); + if (ret) + return ret; + + return 0; } EXPORT_SYMBOL_GPL(tpm_tis_resume); #endif From patchwork Wed Apr 26 17:29:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13224817 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 32D89C7618E for ; Wed, 26 Apr 2023 17:29:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238599AbjDZR3n (ORCPT ); Wed, 26 Apr 2023 13:29:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238587AbjDZR3l (ORCPT ); Wed, 26 Apr 2023 13:29:41 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 617CF83FA; Wed, 26 Apr 2023 10:29:40 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id F34BA63441; Wed, 26 Apr 2023 17:29:39 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 11EE7C433D2; Wed, 26 Apr 2023 17:29:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1682530179; bh=4K/gOer+QGjqHMQJATlyIp1MBELParfSmv57SyLuFJA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qiE6OuWAUVo/BJELaCYkXpzLI5fIwkIjUNsI5JWP4YkDXwaw0vbheoPORQe/LagTY mydsXKJAo4YBMNqPHYuvAcytRwL9+/BxLg2x010umHTF3xIHtpcE+CnvpXPRYAKbMU FVlIRmrDyocQ+p6V22dj5XLnyNu+TtflM3d30Q/SYDcVPMxBn1sTwXu4eQn9AgpDM+ m69nDO+ZqpzZ+2XNQmktq5wS2lG4pRsfOha7chFVSkrCeT7xh4kpUIdUGDiro7mnC5 HDvThWf9vb5nzdODkNeBxAEWKDro4WC+4Y+d2v+pD7rnJS0DFLlu+AAiyjrL20LtGa CNmahOEFV4ISQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Huewe , Jarkko Sakkinen , Jason Gunthorpe Cc: stable@vger.kernel.org Subject: [PATCH 2/2] tpm: Prevent hwrng from activating during resume Date: Wed, 26 Apr 2023 20:29:28 +0300 Message-Id: <20230426172928.3963287-3-jarkko@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230426172928.3963287-1-jarkko@kernel.org> References: <20230426172928.3963287-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-integrity@vger.kernel.org Set TPM_CHIP_FLAG_SUSPENDED in tpm_pm_suspend() and reset in tpm_pm_resume(). While the flag is set, tpm_hwrng() gives back zero bytes. This prevents hwrng from racing during resume. Cc: stable@vger.kernel.org Fixes: 6e592a065d51 ("tpm: Move Linux RNG connection to hwrng") Signed-off-by: Jarkko Sakkinen Reviewed-by: Jerry Snitselaar flags & TPM_CHIP_FLAG_SUSPENDED) + return 0; + return tpm_get_random(chip, data, max); } diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index 7e513b771832..0f941cb32eb1 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -412,6 +412,8 @@ int tpm_pm_suspend(struct device *dev) } suspended: + chip->flags |= TPM_CHIP_FLAG_SUSPENDED; + if (rc) dev_err(dev, "Ignoring error %d while suspending\n", rc); return 0; @@ -429,6 +431,14 @@ int tpm_pm_resume(struct device *dev) if (chip == NULL) return -ENODEV; + chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED; + + /* + * Guarantee that SUSPENDED is written last, so that hwrng does not + * activate before the chip has been fully resumed. + */ + wmb(); + return 0; } EXPORT_SYMBOL_GPL(tpm_pm_resume); diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 4dc97b9f65fb..d7073dc45444 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -274,13 +274,14 @@ enum tpm2_cc_attrs { #define TPM_VID_ATML 0x1114 enum tpm_chip_flags { - TPM_CHIP_FLAG_TPM2 = BIT(1), - TPM_CHIP_FLAG_IRQ = BIT(2), - TPM_CHIP_FLAG_VIRTUAL = BIT(3), - TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4), - TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5), + TPM_CHIP_FLAG_SUSPENDED = BIT(0), + TPM_CHIP_FLAG_TPM2 = BIT(1), + TPM_CHIP_FLAG_IRQ = BIT(2), + TPM_CHIP_FLAG_VIRTUAL = BIT(3), + TPM_CHIP_FLAG_HAVE_TIMEOUTS = BIT(4), + TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5), TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6), - TPM_CHIP_FLAG_FIRMWARE_UPGRADE = BIT(7), + TPM_CHIP_FLAG_FIRMWARE_UPGRADE = BIT(7), }; #define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)