From patchwork Fri Jun 16 17:25:30 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: 9792335 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 267D760326 for ; Fri, 16 Jun 2017 17:25:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0902928456 for ; Fri, 16 Jun 2017 17:25:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F1B162866B; Fri, 16 Jun 2017 17:25:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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 mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1AC8D28456 for ; Fri, 16 Jun 2017 17:25:47 +0000 (UTC) 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:Message-Id:Date:To; bh=1HqSAUlDs0zq9rl4fLYHlQvwMpUxWQG47roqbJKq1u8=; b=lti+fxzZlaJ4o6huLQ2w68CXjlfdb/xXZIYN1csxWU2Mgt6BCLM7gmbUhMkFFomaQEggcdlfc/4VUaOPSgmf/DugjfG2c9+hrFYM+8ab/irQGiS1JnuyyEckN1/RB8N6JNeWFPNnluUWiTxlrhk/otzXNzpZlxDHDnk/wp3lqnE=; Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dLv0E-00041i-2d; Fri, 16 Jun 2017 17:25:46 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dLv0D-00041c-1e for tpmdd-devel@lists.sourceforge.net; Fri, 16 Jun 2017 17:25:45 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Message-Id:Date:Subject:Cc:To:From; bh=+CnUnSglUq2usrQMuN5Rnpu29m/Hl4xLWT6bO+iQU+I=; b=fyBuY0/DKvJMrCVKvnBDGpY9DszctjZuNNV8AstkmeMxWn/PMM0BwBkbzo6RXjnViOkQWoc1c2kiOLNAeyqlJ+MMRzUVJJ6jISfK16HmS9ZEkw/3tjNuqEO+xJMsGF6ZY4ULeHClBF15loZ2PsBwwKBYt4TdUZ9JUpr4cxWT2ac=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x; h=Message-Id:Date:Subject:Cc:To:From; bh=+CnUnSglUq2usrQMuN5Rnpu29m/Hl4xLWT6bO+iQU+I=; b=AfJtgzghnN6F+0mxwatm/kgIiNQBQyDeuXtQNqZSlWbu7P64extQygjG3WBNP2s89ekIXu+ZIpniZBnzcXwI3UQF/Lw5bzw7oeykOnafxPVKu+ue/BnWL+SLoqkkDs4EFixi/MP0JTve4VFqMpvXPhqUqGaIimFD/miYxSm7VuI=; Received-SPF: pass (sog-mx-1.v43.ch3.sourceforge.com: domain of google.com designates 209.85.192.169 as permitted sender) client-ip=209.85.192.169; envelope-from=joshz@google.com; helo=mail-pf0-f169.google.com; Received: from mail-pf0-f169.google.com ([209.85.192.169]) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1dLv0A-0005bF-TC for tpmdd-devel@lists.sourceforge.net; Fri, 16 Jun 2017 17:25:45 +0000 Received: by mail-pf0-f169.google.com with SMTP id l89so25264125pfi.2 for ; Fri, 16 Jun 2017 10:25:42 -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; bh=+CnUnSglUq2usrQMuN5Rnpu29m/Hl4xLWT6bO+iQU+I=; b=g6G4bWucqEJrGmjMJzLPkJRrBjiw2f83hl0KRqESZoXFIILd5EBKjUDIi5xFS+JWSE ByGUg9hvYTZpynlul/MOoyQNwRBi87CSdOURj3RoNRBPI2jwLZON77QErCdIWeIVvaoR i7hji/7xhBKOFngeENn4vDErrG2qt7QWwxZiEF924nobkjOSfDHoxB3y2X6diR1XO/WU BvSGNgsteNlxUpOVk4naSb4S7cTYkUuK7tM4KT41h0n4ep+Du/NHC9p9RamEK8PsmFE/ wLre01y+fB+AXTsA9W4u8Oa/O+CxEBxfnJz/8CNnaHXq2W+5ymfm5hYzUixFKztlPCW9 +rLA== 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; bh=+CnUnSglUq2usrQMuN5Rnpu29m/Hl4xLWT6bO+iQU+I=; b=MjFL5A5/8jxTTNwlxx5fPrDivN0BIp0NLjJD3P4dYHKx1j+1U/XR3Dl1cNEgpMIKYd QeOWj3tt3RwKMDToJJqJqqH4zxm8/nczLG//5LVvSqNkCAQey+ihhnsd8ifrZx3dT7r/ RjTTaOhVTurRkyEjxFyijnf6ZO3sgtCcmBlNgO+OmtqUJTGELFhja9u5Hoz1wLFFysM6 rartuQB54RZK2aeObspgY82h2p0HuZxtGt+93asRDUiu03JYatL/RbCpnaMC7cCUijDv SHn8GJO/SOb3Pib9v6Mkm9lZhag59O2Yqecbjw08rkf09FQ84jNl7Ee+d8x2VNYFLM7x CFhg== X-Gm-Message-State: AKS2vOyu27l6ura2q7mVTHNpDmtGaUmBKBo4bUhV9ATZSbn9qhlnxQ/u Yj2LtoY1bqeSgRml X-Received: by 10.84.232.73 with SMTP id f9mr13979871pln.28.1497633937119; Fri, 16 Jun 2017 10:25:37 -0700 (PDT) Received: from angband.kir.corp.google.com ([100.66.174.26]) by smtp.googlemail.com with ESMTPSA id l2sm5823733pfg.6.2017.06.16.10.25.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 16 Jun 2017 10:25:36 -0700 (PDT) To: Peter Huewe , Marcel Selhorst , Jarkko Sakkinen , Jason Gunthorpe , tpmdd-devel@lists.sourceforge.net, jmorris@namei.org Date: Fri, 16 Jun 2017 10:25:30 -0700 Message-Id: <20170616172531.28464-1-joshz@google.com> X-Mailer: git-send-email 2.13.1.518.g3df882009-goog X-Headers-End: 1dLv0A-0005bF-TC Subject: [tpmdd-devel] [PATCH v6 1/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 X-Virus-Scanned: ClamAV using ClamSMTP 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 ---- 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. --- drivers/char/tpm/tpm-chip.c | 31 +++++++++++++++++++++++++++++++ drivers/char/tpm/tpm-sysfs.c | 3 +++ 2 files changed, 34 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 9dec9f551b83..62691d266f22 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -142,6 +142,36 @@ static void tpm_devs_release(struct device *dev) put_device(&chip->dev); } +/** + * tpm_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. + * + * 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 void tpm_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, TPM_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->shutdown) + dev->bus->shutdown(dev); + else if (dev->driver && dev->driver->shutdown) + dev->driver->shutdown(dev); +} + /** * tpm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: device to which the chip is associated @@ -181,6 +211,7 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, device_initialize(&chip->devs); chip->dev.class = tpm_class; + chip->dev.class.shutdown = tpm_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 55405dbe43fa..59bd0b7b5959 100644 --- a/drivers/char/tpm/tpm-sysfs.c +++ b/drivers/char/tpm/tpm-sysfs.c @@ -294,6 +294,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;