Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp171578rwb; Wed, 10 Aug 2022 22:18:01 -0700 (PDT) X-Google-Smtp-Source: AA6agR7vyY14qheu3TW3EUQGhUn+lHgLiGi4altqLYmiHew1rJRM846FOcI6ZcUJMc9bTJhgtMJy X-Received: by 2002:aa7:8896:0:b0:52e:7cba:ffd8 with SMTP id z22-20020aa78896000000b0052e7cbaffd8mr30694416pfe.82.1660195081113; Wed, 10 Aug 2022 22:18:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660195081; cv=none; d=google.com; s=arc-20160816; b=TY9Bio+aOm7Sx3Pey5q3fwvetvfEL3rA1lUo+Xa01RjKPcxYJaf8kKbLXaElVntuDj 8ZcrumDOI/fft01is1r4bzjT+lqdGqnJtyxu8hIBeqgdXBZ+DLid7E8EbMXDTrzOArm9 uSUKIILbl2V4yqYTFyyuWnf7JmjNoAOVEL8RA+aA2fHiC+wnt1ukmi/zcNLMk9VdFjPG hV9hleHraNz6nV8+1YKGp425+Hkg/nbcmlI/oXlOLnw5PQi+0+tplJCsSIrF6lm2Paaq +SMDjWKH7o0VFZP7/YAwryFTRYmcksTses50Hx++KaIRWa69N3z9taQpsoyniw1eXlPH mRGw== 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=6Wag5mTkI+Sb/QkXyWSgr/hejSXB+e+TInk3ZqpoNZk=; b=zynbOVlZ/afyZ0wvhiBaY9+6Zr/Xi8flR/zL70/IkmhkKYsV45JUF8cQYgfuw+vbWA jbjSD5KloSkgX3Z2yjE+HE/tG7eBupXij/3T/xRflo1jDsX/UX34qB63i7K/d33XxdQW /TG0SYeXIHVFL8M+jAhvF1OedyboseQ2NKSA9BOycG5ZTenwqoIoDHsRuAPIY/OflRa8 g6rDESZa1kB75wkYE1a+VGFuxZHOprpGwVAV5iCRkL/h6FdtrVQzFQjH/++9QeECwsN6 xBfyQZ8WRS5thgobOgIHHMjY6Cwbukhd4TiJXMPTICE8z2u4NwQGavvZs2W+jldKpqO+ wyfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=NIeyuwHh; 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 a26-20020a056a001d1a00b0050df0383302si4053572pfx.255.2022.08.10.22.17.47; Wed, 10 Aug 2022 22:18:01 -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=NIeyuwHh; 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 S233939AbiHKFCN (ORCPT + 99 others); Thu, 11 Aug 2022 01:02:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233459AbiHKFCM (ORCPT ); Thu, 11 Aug 2022 01:02:12 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D65E67CB58; Wed, 10 Aug 2022 22:02:10 -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 ams.source.kernel.org (Postfix) with ESMTPS id 9950DB81EF2; Thu, 11 Aug 2022 05:02:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0779AC433D6; Thu, 11 Aug 2022 05:02:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1660194128; bh=sbr+S4YokhI1iJOsvmS4oDGxcnii+dV1h5gl6Iw/AfM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=NIeyuwHhlTo61IVf8eg9iytmLr2lKh3mKMzrSG5fMwp+fI9MZKC76CbHxxDyKzlYB BTsI72wfs9ugBB1cJ1phGLPFLsUSwAIpGcBk/k60FFZxJ8jzBgxvWAToQV5C6R45m1 GC3lqoIg3kQmaxa+ghwiQ+DKLaAdYkpmIkvGcHLMBBeIyTbS12vgiCcejm0Uh/u/lG Ay7ZROaP8ZQHpc9108+U2VArdjDpLFEAdqs1jB59+FpWGGbCmgJvlkJtRa+Yk189c9 5GqKRKo608Y4i4W1qDAtcGI4dMA1oCJ/K3p0i4spuzbtFtWlQ0AC3ERJtb7nz7EacH J7njQnuooYIPg== Date: Thu, 11 Aug 2022 08:02:02 +0300 From: Jarkko Sakkinen To: Sven van Ashbrook Cc: Peter Huewe , Jason Gunthorpe , Hao Wu , Yi Chou , Andrey Pronin , James Morris , stable@vger.kernel.org, linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] tpm: fix potential race condition in suspend/resume Message-ID: References: <20220809193921.544546-1-svenva@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220809193921.544546-1-svenva@chromium.org> X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 On Tue, Aug 09, 2022 at 07:39:18PM +0000, Sven van Ashbrook wrote: > Concurrent accesses to the tpm chip are prevented by allowing only a > single thread at a time to obtain a tpm chip reference through > tpm_try_get_ops(). However, the tpm's suspend function does not use > this mechanism, so when the tpm api is called by a kthread which > does not get frozen on suspend (such as the hw_random kthread) > it's possible that the tpm is used when already in suspend, or > in use while in the process of suspending. > > This is seen on certain ChromeOS platforms - low-probability warnings > are generated during suspend. In this case, the tpm attempted to read data > from a tpm chip on an already-suspended bus. > > i2c_designware i2c_designware.1: Transfer while suspended > > Fix: > 1. prevent concurrent execution of tpm accesses and suspend/ > resume, by letting suspend/resume grab the tpm_mutex. > 2. before commencing a tpm access, check if the tpm chip is already > suspended. Fail with -EAGAIN if so. > > Tested by running 6000 suspend/resume cycles back-to-back on a > ChromeOS "brya" device. The intermittent warnings reliably > disappear after applying this patch. No system issues were observed. > > Cc: > Fixes: e891db1a18bf ("tpm: turn on TPM on suspend for TPM 1.x") > Signed-off-by: Sven van Ashbrook > --- > drivers/char/tpm/tpm-interface.c | 16 ++++++++++++++++ > include/linux/tpm.h | 2 ++ > 2 files changed, 18 insertions(+) > > diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c > index 1621ce818705..16ca490fd483 100644 > --- a/drivers/char/tpm/tpm-interface.c > +++ b/drivers/char/tpm/tpm-interface.c > @@ -82,6 +82,11 @@ static ssize_t tpm_try_transmit(struct tpm_chip *chip, void *buf, size_t bufsiz) > return -E2BIG; > } > > + if (chip->is_suspended) { > + dev_info(&chip->dev, "blocking transmit while suspended\n"); > + return -EAGAIN; > + } > + > rc = chip->ops->send(chip, buf, count); > if (rc < 0) { > if (rc != -EPIPE) > @@ -394,6 +399,8 @@ int tpm_pm_suspend(struct device *dev) > if (!chip) > return -ENODEV; > > + mutex_lock(&chip->tpm_mutex); > + > if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED) > goto suspended; > > @@ -411,6 +418,11 @@ int tpm_pm_suspend(struct device *dev) > } > > suspended: > + if (!rc) > + chip->is_suspended = true; > + > + mutex_unlock(&chip->tpm_mutex); > + > return rc; > } > EXPORT_SYMBOL_GPL(tpm_pm_suspend); > @@ -426,6 +438,10 @@ int tpm_pm_resume(struct device *dev) > if (chip == NULL) > return -ENODEV; > > + mutex_lock(&chip->tpm_mutex); > + chip->is_suspended = false; > + mutex_unlock(&chip->tpm_mutex); > + > return 0; > } > EXPORT_SYMBOL_GPL(tpm_pm_resume); > diff --git a/include/linux/tpm.h b/include/linux/tpm.h > index d7c67581929f..0fbc1a43ae80 100644 > --- a/include/linux/tpm.h > +++ b/include/linux/tpm.h > @@ -131,6 +131,8 @@ struct tpm_chip { > int dev_num; /* /dev/tpm# */ > unsigned long is_open; /* only one allowed */ > > + bool is_suspended; > + > char hwrng_name[64]; > struct hwrng hwrng; > > -- > 2.37.1.559.g78731f0fdb-goog > What about adding TPM_CHIP_FLAG_SUSPENDED instead? BR, Jarkko