From patchwork Tue May 16 00:08:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Zimmerman X-Patchwork-Id: 762762 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 3wRd9R1y68z9ryZ for ; Tue, 16 May 2017 10:09:31 +1000 (AEST) 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 1dAQ3J-00018P-8C; Tue, 16 May 2017 00:09:25 +0000 Received: from sog-mx-1.v43.ch3.sourceforge.com ([172.29.43.191] helo=mx.sourceforge.net) by sfs-ml-3.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1dAQ3I-00018K-7T for tpmdd-devel@lists.sourceforge.net; Tue, 16 May 2017 00:09:24 +0000 Received-SPF: pass (sog-mx-1.v43.ch3.sourceforge.com: domain of google.com designates 74.125.83.54 as permitted sender) client-ip=74.125.83.54; envelope-from=joshz@google.com; helo=mail-pg0-f54.google.com; Received: from mail-pg0-f54.google.com ([74.125.83.54]) by sog-mx-1.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1dAQ3G-0003Rm-2p for tpmdd-devel@lists.sourceforge.net; Tue, 16 May 2017 00:09:24 +0000 Received: by mail-pg0-f54.google.com with SMTP id u187so67396308pgb.0 for ; Mon, 15 May 2017 17:09:21 -0700 (PDT) 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=4Boj1LyBDuy1CvygDE5YFuhNO48GdfCKjaSkgjvcEn0=; b=M3aU/KRIPI49bZdzgvBY3V/7aSwtqPq4dU+VHohotGCCRY8hBEVdSVS8tN+1telgk/ dfMr6qVVsm7B3w/sYmVMklALsJa6dyMVcDGfxpaMXeItGrpZYpNDMowmphzClcNMgkX4 Z5GF/wFFqxUJxAOtk5p7cR/lEvyGKYavqdcBqIvTVSAftcR/Sfweqd6rByUrdzDIhElG uQYVkJdCA+zG6s6lC0KGUd26W868cRPYKjJRFOX0rDTAHrod7b9hDTCmAfRcYuTHQKqf np0sN0TMbLB4aW04M5JZ6iBiCM7dC42+At5/EeVr1cVXWAgQolDX5JdbZrINHlVMK6gF /mFg== X-Gm-Message-State: AODbwcDaQeJNckewZeD5N9/8HjW9s/l5yI+yqclsVMoVlji1gAS7g4G/ 8VQeYRbBDgLdd3wp X-Received: by 10.99.116.82 with SMTP id e18mr8760609pgn.1.1494893356169; Mon, 15 May 2017 17:09:16 -0700 (PDT) Received: from angband.kir.corp.google.com ([100.119.30.20]) by smtp.googlemail.com with ESMTPSA id k192sm22673828pgc.31.2017.05.15.17.09.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 15 May 2017 17:09:15 -0700 (PDT) From: Josh Zimmerman To: Peter Huewe , Marcel Selhorst , Jarkko Sakkinen , Jason Gunthorpe , tpmdd-devel@lists.sourceforge.net Date: Mon, 15 May 2017 17:08:52 -0700 Message-Id: <20170516000852.28400-1-joshz@google.com> X-Mailer: git-send-email 2.13.0.303.g4ebf302169-goog 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 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [74.125.83.54 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RP_MATCHES_RCVD Envelope sender domain matches handover relay domain -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 X-Headers-End: 1dAQ3G-0003Rm-2p Cc: Greg Kroah-Hartman Subject: [tpmdd-devel] [PATCH v3] tpm: Issue a TPM2_Shutdown for TPM2 devices. X-BeenThere: tpmdd-devel@lists.sourceforge.net X-Mailman-Version: 2.1.9 Precedence: list List-Id: Tpm Device Driver maintainance List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , 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 (eventually, this will cause the TPM to 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. This patch is dependent on '[PATCH] Add "shutdown" to "struct class".' http://marc.info/?l=linux-kernel&m=149463235025420&w=2 Signed-off-by: Josh Zimmerman ---- v2: - Properly split changes between this and another commit - Use proper locking primitive. - Fix commenting style v3: - Re-fix commenting style Reviewed-by: Jarko Sakkinen --- drivers/char/tpm/tpm-chip.c | 20 ++++++++++++++++++++ drivers/char/tpm/tpm-sysfs.c | 3 +++ 2 files changed, 23 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 9dec9f551b83..272a42e77574 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -142,6 +142,25 @@ static void tpm_devs_release(struct device *dev) put_device(&chip->dev); } +static void tpm_shutdown(struct device *dev) +{ + struct tpm_chip *chip = container_of(dev, struct tpm_chip, dev); + /* TPM 2.0 requires that the TPM2_Shutdown() command be issued prior to + * loss of power. If it is not, the DA counter will be incremented and, + * eventually, the user will be locked out of their TPM. + * XXX: This codepath relies on the fact that sysfs is not enabled for + * TPM2: sysfs uses an implicit lock on chip->ops, so this use could + * race if TPM2 has sysfs support enabled before TPM sysfs's implicit + * locking is fixed. + */ + 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); + } +} + /** * tpm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: device to which the chip is associated @@ -181,6 +200,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..5e5ff7eb6f7e 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: Before this restriction is removed, tpm_sysfs must be updated + * to explicitly lock chip->ops. + */ if (chip->flags & TPM_CHIP_FLAG_TPM2) return;