Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1163340rwr; Wed, 26 Apr 2023 10:52:34 -0700 (PDT) X-Google-Smtp-Source: AKy350aDrxY9ud5ceofNg8akliXSSqbHOkBDaCsCB5IlesZc6SDmIUmRnH9gRnyUx9Nmpqawe77s X-Received: by 2002:a17:902:be10:b0:1a6:7fac:1b7d with SMTP id r16-20020a170902be1000b001a67fac1b7dmr20703206pls.59.1682531553738; Wed, 26 Apr 2023 10:52:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682531553; cv=none; d=google.com; s=arc-20160816; b=bxmQ8bmca9GKgLT5nNZzUcg0ws2cKP6BXcTRzOPH+muT+bFPHJaCaQyx1o8QXpjwDM GW+VCIWnlh6/0SmXbmK+IQaSL2WBqzu+T2Bv0c9WqOBnGqA0uLSyK6QZMPcH7rpI5L1s Plsax4JnwmzJmw12W5XNNhCkvVozC558I8T1ZrGpP/d/oAgheulcSAeLFUKbTo/2P7Pv hB+eIahNb5q5wldGCL8NOZ8en3/3IwrkvxjzXEBIO7CgUw3QI6Rjkfpucc8Is060hkeJ W/W6bPPz1DrwWvmKRQVrT8uDXnHppzE6tq2ZshPeKTZ+1fdURat+BotQJnliO/2eT48w /BsA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JfWTo5tD9jd9L7WhVc137I2G0dlBck7/rBZH+K4KKi0=; b=0R6rw2M9TYPcyJGPg4ck7SnMZlj5yFxoXpusAaMfJ6Bn48iLF8/50JgpxDp/HajPAR gxrxagVO6tXZu/4QVYdI6+UMyHp9brvxTq1jAMLUKFQWpziIS/OnZMLRGxZpiYUFGfdA F8YcyBqROVkvD2M5ARKqf3MjZtVKG+LepQIG+6tKfcvF6g8pYb72GSCLr6cqpPFKEro1 bbqvA/S5Oxwfe4uisVmXO09zDrLHX01hKSoizM2PzMoO4xntl/Eu4hZIE8EToN+kdo/R oxlwuyqAVQ+eagYtReuyoddDezN0OjU2Rsa0MwDP2/B7DcRVoTPhdh/ghbH+Mip3z5Z2 6sZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=qiE6OuWA; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jj13-20020a170903048d00b001a04202f782si14332472plb.122.2023.04.26.10.52.22; Wed, 26 Apr 2023 10:52:33 -0700 (PDT) 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=@kernel.org header.s=k20201202 header.b=qiE6OuWA; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233678AbjDZR3n (ORCPT + 99 others); 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,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 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 --- drivers/char/tpm/tpm-chip.c | 4 ++++ drivers/char/tpm/tpm-interface.c | 10 ++++++++++ include/linux/tpm.h | 13 +++++++------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 6fdfa65a00c3..6f5ee27aeda1 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -572,6 +572,10 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) { struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); + /* Give back zero bytes, as TPM chip has not yet fully resumed: */ + if (chip->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) -- 2.39.2