From patchwork Sun Jun 25 21:53:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Zimmerman via tpmdd-devel X-Patchwork-Id: 780538 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.34.181.88]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wwmD33h69z9s65 for ; Mon, 26 Jun 2017 07:53:55 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=lists.sourceforge.net header.i=@lists.sourceforge.net header.b="Sx/W+dO9"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sourceforge.net header.i=@sourceforge.net header.b="LxYhWm23"; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sf.net header.i=@sf.net header.b="NROVyM/P"; dkim=neutral (0-bit key) header.d=google.com header.i=@google.com header.b="fKH5Q+FE"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Reply-To:From:List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Subject:References:In-Reply-To:Message-Id:Date:To; bh=1I2tj3KaWbbRn18Ml/F1vJjSVV7YBOL6P96VcHaqB1o=; b=Sx/W+dO9aMnDa5jvsTnIOvm9tDW7V20xfUcqWTW40/hfeauUX8vC+G8qRN9rW/KSm3FcziFhQV6ah/yh1SQJdRz+7NmDqd3fgmv1z0PqgzEo7yvqZ8XFYaSSEkjLy+lx2Ufbn9dQytTKez2GbQpFj85gAi+RbivTsM9lSO//jQ4=; Received: from localhost ([127.0.0.1] helo=sfs-ml-3.v29.ch3.sourceforge.com) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dPFTZ-00089X-Ny; Sun, 25 Jun 2017 21:53:49 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dPFTY-00089N-R2 for tpmdd-devel@lists.sourceforge.net; Sun, 25 Jun 2017 21:53:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=hNzlOJYtxAWtJmOeO0S/qpD/HHfMvZ0MOdsP0xmsI00=; b=LxYhWm23suaH6CntL8OJSa5WHwzUA3LgsOY4eOyJr4+T/aaBnQhNBeRHo/nZHQDKFBHK2VRR3If3HrInv316QW/FVR96j+46zX48pbT7UhVMEi7BtUZKjeHIbgo40criaBiBPni+1pa/DSDEs3Nwkd5VzLPi3h6CTQDs+R4AYNw=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=hNzlOJYtxAWtJmOeO0S/qpD/HHfMvZ0MOdsP0xmsI00=; b=NROVyM/PJTO9eLiducwaQgSYvmYuc101fBqkicT2MJ/jeA7d2i40uSIK1U5f6MmfAETbV7vNbRzDYkyQWq/hdyK3Rgbaxusha6ZXVmTIfGRDXrjhXmXme66HssMZYFuTcrIjj5WxlfxJ3U22c+cHgYgdNIOPSiQ3QBC/A8vnPNY=; Received-SPF: pass (sog-mx-3.v43.ch3.sourceforge.com: domain of google.com designates 74.125.83.53 as permitted sender) client-ip=74.125.83.53; envelope-from=joshz@google.com; helo=mail-pg0-f53.google.com; Received: from mail-pg0-f53.google.com ([74.125.83.53]) by sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1dPFTY-0005qt-0c for tpmdd-devel@lists.sourceforge.net; Sun, 25 Jun 2017 21:53:48 +0000 Received: by mail-pg0-f53.google.com with SMTP id t186so708721pgb.1 for ; Sun, 25 Jun 2017 14:53:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=hNzlOJYtxAWtJmOeO0S/qpD/HHfMvZ0MOdsP0xmsI00=; b=fKH5Q+FEYctkYSuvOVPC2zGLCxgLAyiYwDKwExw4kF/RgFpWkiJctPNFHl70Wee8DN veXDThMpJvo/G378iaC4xpRn/8QXmBUv0UtlnrH4kDFRCC5XMa3EGsZSE/F1K4u2MCAC zzfEUaAd/HBzRYDG/6CN2MAIY4VKvGHtE4BW7FshxYm1/4eIQlMl1mHsUEddNHZkhwCv ZvErrG1RyDql+pkjyPTykh0/RcoO5uE5txpxce4uV6yUs5CbN53qNYnxl2idBoMkVtCS 0qekPGImeoU41ZuUNr9pT6OJFALkYg55QIiYgx+rNrd13i4u/xI84NEk9vXTEaMyIsHA D4rg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=hNzlOJYtxAWtJmOeO0S/qpD/HHfMvZ0MOdsP0xmsI00=; b=qVpNJWpmJv09yYGq9J3eloaFqllS0Z3pp1GpGAUGguSLOiVqTiuTaizwgUPNagQ37S VWEbbyMDP+UT653WRXT1g5lH38mSj1L8aJbvsn+YEGM1ZJWLi45o4V3U71HibmyuBVZc 35Naia4cp+NsOMbFezHLTUSN++m7WlO/IKkR+J5pIwx+y8Zj9lHDzNSPnT4Gp8PFXT4y Gwa+SOGrpePltqLGWxr/iGwbTfMrOzZLj4Z903SvWy3bX81uocjtQps8/U9R2LP7QfLw yxTu8X2GtOF/Q0G1CUX6138PhGfZVfre7szHxOAmd9Km9GlClhHB3Kx65rCowoo65zgd BA7g== X-Gm-Message-State: AKS2vOyagu4yutv8jmQdJa8C6RDKuvSHWDepZDx0G08LIXW65UvXMNTh ElLuGXwg/arOSEPP X-Received: by 10.99.149.83 with SMTP id t19mr17823231pgn.247.1498427622175; Sun, 25 Jun 2017 14:53:42 -0700 (PDT) Received: from angband.kir.corp.google.com ([100.66.174.26]) by smtp.googlemail.com with ESMTPSA id s64sm25692458pfd.77.2017.06.25.14.53.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 25 Jun 2017 14:53:41 -0700 (PDT) To: Peter Huewe , Marcel Selhorst , Jarkko Sakkinen , Jason Gunthorpe , tpmdd-devel@lists.sourceforge.net, jmorris@namei.org Date: Sun, 25 Jun 2017 14:53:24 -0700 Message-Id: <20170625215324.17467-3-joshz@google.com> X-Mailer: git-send-email 2.13.1.611.g7e3b11ae1-goog In-Reply-To: <20170625215324.17467-1-joshz@google.com> References: <20170625215324.17467-1-joshz@google.com> X-Spam-Score: -1.6 (-) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [74.125.83.53 listed in list.dnswl.org] -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.0 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1dPFTY-0005qt-0c Subject: [tpmdd-devel] [PATCH v7 2/2] tpm: Issue a TPM2_Shutdown for TPM2 devices. X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Josh Zimmerman via tpmdd-devel From: Josh Zimmerman via tpmdd-devel Reply-To: Josh Zimmerman MIME-Version: 1.0 Errors-To: tpmdd-devel-bounces@lists.sourceforge.net If a TPM2 loses power without a TPM2_Shutdown command being issued (a "disorderly reboot"), it may lose some state that has yet to be persisted to NVRam, and will increment the DA counter. After the DA counter gets sufficiently large, the TPM will lock the user out. NOTE: This only changes behavior on TPM2 devices. Since TPM1 uses sysfs, and sysfs relies on implicit locking on chip->ops, it is not safe to allow this code to run in TPM1, or to add sysfs support to TPM2, until that locking is made explicit. Signed-off-by: Josh Zimmerman Cc: stable@vger.kernel.org Fixes: 74d6b3ceaa17 ("tpm: fix suspend/resume paths for TPM 2.0") --- v2: - Properly split changes between this and another commit - Use proper locking primitive. - Fix commenting style v3: - Re-fix commenting style v4: - Update description and tags (Reviewed-by, Cc). v5: - Update documentation. v6: - Call device and/or bus shutdown from tpm_shutdown. v7: - Fix build - Rename function - Fix missing NULL check drivers/char/tpm/tpm-chip.c | 34 ++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm-sysfs.c | 3 +++ 2 files changed, 37 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 322b8a51ffc6..67ec9d3d04f5 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -142,6 +142,39 @@ static void tpm_devs_release(struct device *dev) put_device(&chip->dev); } +/** + * tpm_class_shutdown() - prepare the TPM device for loss of power. + * @dev: device to which the chip is associated. + * + * Issues a TPM2_Shutdown command prior to loss of power, as required by the + * TPM 2.0 spec. + * Then, calls bus- and device- specific shutdown code. + * + * XXX: This codepath relies on the fact that sysfs is not enabled for + * TPM2: sysfs uses an implicit lock on chip->ops, so this could race if TPM2 + * has sysfs support enabled before TPM sysfs's implicit locking is fixed. + */ +static int tpm_class_shutdown(struct device *dev) +{ + struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); + + if (chip->flags & TPM_CHIP_FLAG_TPM2) { + down_write(&chip->ops_sem); + tpm2_shutdown(chip, TPM2_SU_CLEAR); + chip->ops = NULL; + up_write(&chip->ops_sem); + } + /* Allow bus- and device-specific code to run. Note: since chip->ops + * is NULL, more-specific shutdown code will not be able to issue TPM + * commands. + */ + if (dev->bus && dev->bus->shutdown) + dev->bus->shutdown(dev); + else if (dev->driver && dev->driver->shutdown) + dev->driver->shutdown(dev); + return 0; +} + /** * tpm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: device to which the chip is associated @@ -181,6 +214,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, device_initialize(&chip->devs); chip->dev.class = tpm_class; + chip->dev.class->shutdown = tpm_class_shutdown; chip->dev.release = tpm_dev_release; chip->dev.parent = pdev; chip->dev.groups = chip->groups; diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c index eedb8e47bde2..86f38d239476 100644 --- a/drivers/char/tpm/tpm-sysfs.c +++ b/drivers/char/tpm/tpm-sysfs.c @@ -295,6 +295,9 @@ static const struct attribute_group tpm_dev_group = { void tpm_sysfs_add_device(struct tpm_chip *chip) { + /* XXX: If you wish to remove this restriction, you must first update + * tpm_sysfs to explicitly lock chip->ops. + */ if (chip->flags & TPM_CHIP_FLAG_TPM2) return;