From patchwork Fri Jul 14 19:58:03 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: 9841593 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 439BB60212 for ; Fri, 14 Jul 2017 19:58:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 33952287B2 for ; Fri, 14 Jul 2017 19:58:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 280A8287C3; Fri, 14 Jul 2017 19:58:54 +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, HK_RANDOM_FROM, 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 277CA287C1 for ; Fri, 14 Jul 2017 19:58:53 +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:References:In-Reply-To:Message-Id:Date:To; bh=hZL9CMdMTRRtP3vdZsjcAiyoUpz73Ws9Ku1G3lCj4os=; b=UGmin8mDZ9KX/rdizQjVc/JKSx9M1pB1Fy3D6TX81K4nLDCysumZfzJlrUi4l3ecJh3EHF1xea7AASCQ4LhxnRg6jal6Vlx6wh0IZqfCdtyw0UAQMlTeb+aUS0e84cMoM+BNAZ4SqKH/MVcx4oloQnK9eShw4cQOD3nGMvJxUcY=; Received: from localhost ([127.0.0.1] helo=sfs-ml-1.v29.ch3.sourceforge.com) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dW6jk-00021e-Bg; Fri, 14 Jul 2017 19:58:52 +0000 Received: from sog-mx-4.v43.ch3.sourceforge.com ([172.29.43.194] helo=mx.sourceforge.net) by sfs-ml-1.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dW6jj-00021Z-AH for tpmdd-devel@lists.sourceforge.net; Fri, 14 Jul 2017 19:58:51 +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=BfpU/OThSueZ57uElYPDvmh6zYJpzAF6ptPCAun17Hk=; b=esTu3ysXwugqapoPe2LcV6ABIY2DdO8CsAReNMgO88PiOXFYpErHUTGPZ/X38tdeOFp+Vg/+adjH1JrAbQJXQS4sUgn3VWIa3YajTCAqfpQF7uPoFlVV02pqNXcoFUfQU2WqvEdXWdR1l3zuSqadGHa+U+3gfdIA4G/C3fyHAOw=; 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=BfpU/OThSueZ57uElYPDvmh6zYJpzAF6ptPCAun17Hk=; b=cB6GbH/TElf8WSj2cnMFdjcb8fkjyRQufdzjzRH/sUbv2cqsMiRaAQaoy7OxUYn8tJpbIE1+BIQD1+g4FmkKXV34cnMBHoXKLmWepg1bEiXo815CsTBLCTWLdn7XC5ZWXTtKW7g7knHAjYvmOYFfxOqDvCpCP4pHaFl6JLkjiVI=; Received-SPF: pass (sog-mx-4.v43.ch3.sourceforge.com: domain of google.com designates 209.85.192.170 as permitted sender) client-ip=209.85.192.170; envelope-from=joshz@google.com; helo=mail-pf0-f170.google.com; Received: from mail-pf0-f170.google.com ([209.85.192.170]) by sog-mx-4.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1dW6ji-0000zj-CR for tpmdd-devel@lists.sourceforge.net; Fri, 14 Jul 2017 19:58:51 +0000 Received: by mail-pf0-f170.google.com with SMTP id c73so50068117pfk.2 for ; Fri, 14 Jul 2017 12:58:50 -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=BfpU/OThSueZ57uElYPDvmh6zYJpzAF6ptPCAun17Hk=; b=py0msnL6BsVov33Okouu9NlAaVuCYk6WG6cSaMu4GQaEFFSj9f8UJSeyCfHnSAaqv4 ZXMbO20MfaYM1VHVZwBYip5+yd7eXStgr7mkXFznQQsiUOhR0bbwIhEYDpmrNwLCQhIf YRsSb14T1XaXxWch9M5QZwsu4amSKxqoqS4yzTnFtq4fXfcEpvx5URoNlxYwed/brDo6 IXs4w57nWBaGc6FUWgAZxbcvGsGtcWnyIA17QjzEQHXtm61d7rzN+L1Ytw1pFIXz8KX2 +VlcdMjrb6l8dv+LZHzbnxTFKSoATkOKYnxuJ2cwAvFQpWc1m2qkSEX5BZpVyE5Lobdy aqSg== 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=BfpU/OThSueZ57uElYPDvmh6zYJpzAF6ptPCAun17Hk=; b=AbjEdDtne1rNhEefzRT3t5WTnA7bcCqV8JmT97Ok4z7M2HE2ZOOPgcPDanylOsdzYT /njaEGKbl3LO/7sQjFEzRePxW2VpuNNvjP0ec6X7+avwDt5gqXUPwQg0q9Qx691a64Bz LCD7drXllxKCYoYETwAVjTYpgNBFG/c0kf3/n/xBmcFoWJwqri/jMBkcDGEURHw3Fqxs k973sFIAq/y1NHpaBEazceNJzDqLFHByrBLL4TaueBQfCTX8z4HnKk4a6IkNeidtkRIt Kj1/o2twYb+kUIeKb/3nvJgL5x9qmpHL58woq6UtCjWmJyDVbEVNBJ6YchVxv8lJB1+/ bKug== X-Gm-Message-State: AIVw112pC1C6sy8z32TRymqt7xU7VAPVRHr6Iho/CqhDOvanRpqoqMzq meionKbSHxLQlxL7 X-Received: by 10.84.133.11 with SMTP id 11mr17837834plf.77.1500062324510; Fri, 14 Jul 2017 12:58:44 -0700 (PDT) Received: from angband.kir.corp.google.com ([100.66.174.26]) by smtp.googlemail.com with ESMTPSA id t26sm22043500pfe.88.2017.07.14.12.58.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 14 Jul 2017 12:58:43 -0700 (PDT) To: Jarkko Sakkinen , Jason Gunthorpe , tpmdd-devel@lists.sourceforge.net, gregkh@linuxfoundation.org, stable@vger.kernel.org Date: Fri, 14 Jul 2017 12:58:03 -0700 Message-Id: <20170714195803.7035-5-joshz@google.com> X-Mailer: git-send-email 2.13.2.932.g7449e964c-goog In-Reply-To: <20170714195803.7035-1-joshz@google.com> References: <20170714195803.7035-1-joshz@google.com> X-Headers-End: 1dW6ji-0000zj-CR Subject: [tpmdd-devel] [PATCH v1 4/4] 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 Backport of d1bd4a792d3961a04e6154118816b00167aad91a upstream. 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. --- drivers/char/tpm/tpm-chip.c | 36 ++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm-sysfs.c | 7 +++++++ 2 files changed, 43 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index f3a887e4f692..6d56877b2e0a 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -124,6 +124,41 @@ static void tpm_dev_release(struct device *dev) kfree(chip); } + +/** + * 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; +} + + /** * tpmm_chip_alloc() - allocate a new struct tpm_chip instance * @dev: device to which the chip is associated @@ -166,6 +201,7 @@ struct tpm_chip *tpmm_chip_alloc(struct device *dev, dev_set_drvdata(dev, chip); chip->dev.class = tpm_class; + chip->dev.class->shutdown = tpm_class_shutdown; chip->dev.release = tpm_dev_release; chip->dev.parent = dev; #ifdef CONFIG_ACPI diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c index 8af4145d10c7..6a4056a3f7ee 100644 --- a/drivers/char/tpm/tpm-sysfs.c +++ b/drivers/char/tpm/tpm-sysfs.c @@ -284,6 +284,13 @@ static const struct attribute_group tpm_dev_group = { int tpm_sysfs_add_device(struct tpm_chip *chip) { int err; + + /* 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 0; + err = sysfs_create_group(&chip->dev.parent->kobj, &tpm_dev_group);