Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1046101rwr; Thu, 27 Apr 2023 11:30:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6Sc8HQkrbLRz/sZYinCDWctkxaBIvFUwKD1Jq+Ql5m+o104eUNghXWtTan9NuyxURsstCC X-Received: by 2002:a17:903:1c9:b0:1a6:f5d5:b7f6 with SMTP id e9-20020a17090301c900b001a6f5d5b7f6mr8599674plh.10.1682620216510; Thu, 27 Apr 2023 11:30:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682620216; cv=none; d=google.com; s=arc-20160816; b=EDaSKd1BbEC1xK3pUzr4qnShZlRDrbbvDgy5qGGY3kH9oyJVig5HM7WXR+MD2UNRky h9gSGkLgN3ZIDh399sFeib3qMrQu4IKQccocrAcfWYtgFhZnBEJ8QgGHw7QZ/sDvA3X/ RInckH41PJRW+i130S/5zkBjN8rVKkAE8IyaITClGNhW4PpNtLiOFLnvsBS9egv/LNsg j5bFt6A685/HWJrFqAJc3PX83nMeFEw25g6xSsgLqo7fO2spRXgulBIN8+swS/qN3yzT ns+4bqBh/awpkt5JDLthFAoR32yxfz6eLHGEooDJVAsbdfZ265R8bYoJna/YBWageWmZ UF3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=T+CX4GL4gGpxnXf/hJ5GWtmKz7MYbRhiiF9QJ1pFsSM=; b=Xi6078gRwbGWqShC2+pw6jay1XxmXf/yMm4XhizlWY9Ous2R+Ss1lWIFiWNq+iiDPz x9XgBjL675jd40a+JJKLleEMz0F3QpInA0ccodNpMtgK3PAPZRQtbnoZcxDbq1W5WV5+ 5uEY3UT9kMK7ddz5MF9w7wznaG0l2mJ2z5+mYQz+ISXBFOtVXwD4UZHMY9ecwnlAvttf IZypXJ0FVfRR4bKNfdm/vN+J2asLl/8zV6XZNtjWoYPzzA+V2o9T2ZqZgoXMW504vUEF 98CcF0xz0MEbKgLnkLsOYzKXmq7g3lkFdH1eBqkZbWJmNMvd7+AFLFk1yrYma1L/Fb56 eC9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=N88+kLHA; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id kb8-20020a17090ae7c800b002470488de14si24180095pjb.182.2023.04.27.11.30.01; Thu, 27 Apr 2023 11:30:16 -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=@redhat.com header.s=mimecast20190719 header.b=N88+kLHA; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244474AbjD0S2o (ORCPT + 99 others); Thu, 27 Apr 2023 14:28:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47286 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244039AbjD0S2n (ORCPT ); Thu, 27 Apr 2023 14:28:43 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0DFDB44BF for ; Thu, 27 Apr 2023 11:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682620077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=T+CX4GL4gGpxnXf/hJ5GWtmKz7MYbRhiiF9QJ1pFsSM=; b=N88+kLHAIM79eS+q/xb+Hwgxt5IWlQ1T7qeA0qgzBeRjx8HT3w5bJo9GtYY3kpHvQkXVJQ 3LlSh9/sVCncsokccSB61oVXOx1qx4MQOnorriaFv9kKEdKT9U4IMtrIX9uLc1vEPt3sOC YdKVGtV+qGk+3z3OlMlPzR63n6Vt850= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-562-6Md7NY57MYOOg3tgNAX_OA-1; Thu, 27 Apr 2023 14:27:56 -0400 X-MC-Unique: 6Md7NY57MYOOg3tgNAX_OA-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-751319bd93cso492554485a.0 for ; Thu, 27 Apr 2023 11:27:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682620075; x=1685212075; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=T+CX4GL4gGpxnXf/hJ5GWtmKz7MYbRhiiF9QJ1pFsSM=; b=F/MAtvQWOhqHOYCGWXlWu8bsRQSZWEA4Y5HVDrzvwgcTG0zBm1cUIct+Uwanl0UyCj yVXJR28TcwGTzDBYmmP20TZCS0jfhE3wd9cHNACISrXrTQzBq4UA9Xk86rcMWB7owzF4 5zxnabKoH30fhZNkOWi/pu0O7NRbOufflsHG8fA7OCbeM/m2R1xcUYwvb4JrcWMqyWFO ddsDmdSbMs0rcoP4ohwbQ1MbMaR5dtkg/vFqkzJggEMv81Zil5zy6EdMorORJboVG2RC yiHpd4SQubjerqavuYxaWxXy7ENwVVdkpB7rqmzaGyGyg2P879qcCkd6T6leVRosOD0I lQYA== X-Gm-Message-State: AC+VfDyecCpgMUeziKFFRlKyF1TMGLjftZYt6V1HWoBA+0DZyxJzhg3L 2MsDVSZWro3rRKIP79y9JvcuZqa0jbKRi2CHbmLS/zg6XR9deyAy+pP4HzAhphBbU+OYEs9zRuw GpOdKquIqGZSN95nT1117ruY1 X-Received: by 2002:a05:6214:2aa3:b0:5ef:6103:1475 with SMTP id js3-20020a0562142aa300b005ef61031475mr11387352qvb.20.1682620075762; Thu, 27 Apr 2023 11:27:55 -0700 (PDT) X-Received: by 2002:a05:6214:2aa3:b0:5ef:6103:1475 with SMTP id js3-20020a0562142aa300b005ef61031475mr11387327qvb.20.1682620075431; Thu, 27 Apr 2023 11:27:55 -0700 (PDT) Received: from localhost (ip98-179-76-75.ph.ph.cox.net. [98.179.76.75]) by smtp.gmail.com with ESMTPSA id k25-20020a0c9719000000b005ef529dc39esm4016014qvd.108.2023.04.27.11.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Apr 2023 11:27:55 -0700 (PDT) Date: Thu, 27 Apr 2023 11:27:53 -0700 From: Jerry Snitselaar To: Jarkko Sakkinen Cc: linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Huewe , Jason Gunthorpe , stable@vger.kernel.org Subject: Re: [PATCH 2/2] tpm: Prevent hwrng from activating during resume Message-ID: <3r4dorqzh7nrcsqmsedmt47iimqrh52dfzgrmkjduqglhwufkt@3svji6cfdsug> References: <20230426172928.3963287-1-jarkko@kernel.org> <20230426172928.3963287-3-jarkko@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230426172928.3963287-3-jarkko@kernel.org> X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Wed, Apr 26, 2023 at 08:29:28PM +0300, Jarkko Sakkinen wrote: > 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 --- > 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 >