From patchwork Tue May 30 19:00:54 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Zimmerman X-Patchwork-Id: 9755157 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 BC38D601D2 for ; Tue, 30 May 2017 19:01:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AD8AB2849A for ; Tue, 30 May 2017 19:01:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A24982847F; Tue, 30 May 2017 19:01:26 +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=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 2B20528294 for ; Tue, 30 May 2017 19:01:26 +0000 (UTC) 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 1dFmOS-0002AT-Rp; Tue, 30 May 2017 19:01:24 +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 1dFmOR-0002AL-0C for tpmdd-devel@lists.sourceforge.net; Tue, 30 May 2017 19:01:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Type:To:Subject:Message-ID:Date:From:MIME-Version; bh=8/l2OZN9UEyHIH7bjm95EKb51KJ+yqOI3zKNSGB4LDY=; b=eAdkztcsrDhNo4/Xtqjnb5OiN0uHrVZpQ2b3uv1HQ4XpF3+DWbe/8foYxFk8dr1zVf/lUoqdUojqKZfEh58ZaxFgg6I4/8jybzZGhdlmR8wQc8rXRpcGOAAAbBEpNp5a7ibg8vkL7nB6eCBZlnvwxikiQlbW9wT2gCun1zFjKpQ=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x; h=Content-Type:To:Subject:Message-ID:Date:From:MIME-Version; bh=8/l2OZN9UEyHIH7bjm95EKb51KJ+yqOI3zKNSGB4LDY=; b=GBjugoGXXJB36Qg/PECGi9PU7rHXD6DXt4EzdKNEKMIy8TOG5qaJdbgwOATnzEwtXoICiogOa97/z7L9DPCZHlJI1BbC+fAyUrXsZiSJmrAfAlhGScfb/O1kTQUc5SDzofOupFLYBYWv1dgyVXXEDAubMhYqHpD6yBNoCfQfXJM=; Received-SPF: pass (sog-mx-4.v43.ch3.sourceforge.com: domain of google.com designates 209.85.220.173 as permitted sender) client-ip=209.85.220.173; envelope-from=joshz@google.com; helo=mail-qk0-f173.google.com; Received: from mail-qk0-f173.google.com ([209.85.220.173]) by sog-mx-4.v43.ch3.sourceforge.com with esmtps (TLSv1:AES128-SHA:128) (Exim 4.76) id 1dFmOQ-0000fR-5N for tpmdd-devel@lists.sourceforge.net; Tue, 30 May 2017 19:01:22 +0000 Received: by mail-qk0-f173.google.com with SMTP id 19so10984530qke.2 for ; Tue, 30 May 2017 12:01:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=8/l2OZN9UEyHIH7bjm95EKb51KJ+yqOI3zKNSGB4LDY=; b=QuzzMlIa/ymrnj4UE6CPiVpjL+9sWD+lxb6NRdwqvMslZgzDph2zV684pXSXYPMCMo V3pTuSZcWhP++zEwUsjdEhqhGSBmGqqCNBDXNNo5AKF6x8m0Wk0+5xS2kgCtWim+fCaW CufEAkOipcCrVElg09Tw+9Do684BSh+oyx/8LN1ZBOtGjXEBbnHEIdeQKSDxHkyifKOI 0mEK4g23Trrku7wX3EzuNQ5TRKgzReJrJPHNE7ktu7qQ35E91A86PQGNvTCW4Y7fIhXx 2oMU+uSivIfd9nuh8aV+0D/vqPVy5WWu0bNoMfepMgQD5K5/jCh7CNCeDV5+q31At2vH u73A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=8/l2OZN9UEyHIH7bjm95EKb51KJ+yqOI3zKNSGB4LDY=; b=HrEj3kvthR+3U6GVWTSk+P2aluRhaBltopKhoxmCLVMz45wRxkBxL6G6N2+iVdHYyp wrOPeGagtsl8YtIP9+tNcvqxvfOBNX74mZnWcLfVnkQF+HAqkiKqpYJd/05Xo0zYM4Ci fbhsYA9YVzK1wlYt7SVOpCsoMZAVLgPtoXjZM0z0V48FOi0yzHAoYkA5qdKUnGgtthz4 iRZOXKTbQPWfuGZt83g0+5mNbfPAp0+3W/xZc5tHYIyFa388U8KVd3pTYo34dpgFxxA2 nXLAuTiRKI8eHLjTTDdnOAaT8ccRFXCcVDs0vXI2Pt2LJQlkJsiU9Xc+2DJgcvVeW+JV Qg5A== X-Gm-Message-State: AODbwcDnB/WjCIuzK271yvhxbmmMVCwy6rtcCyqDPRuCz9il6H1qGlKR SIeKvA9u6BA/zJjD0Otm5py1psz8Ks50xw3DIA== X-Received: by 10.55.166.137 with SMTP id p131mr23057092qke.132.1496170875371; Tue, 30 May 2017 12:01:15 -0700 (PDT) MIME-Version: 1.0 Received: by 10.140.82.180 with HTTP; Tue, 30 May 2017 12:00:54 -0700 (PDT) From: Josh Zimmerman Date: Tue, 30 May 2017 12:00:54 -0700 Message-ID: To: Peter Huewe , Marcel Selhorst , Jarkko Sakkinen , Jason Gunthorpe , tpmdd-devel@lists.sourceforge.net X-Headers-End: 1dFmOQ-0000fR-5N Subject: [tpmdd-devel] [PATCH v5] 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: , 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. --- drivers/char/tpm/tpm-chip.c | 23 +++++++++++++++++++++++ drivers/char/tpm/tpm-sysfs.c | 3 +++ 2 files changed, 26 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 9dec9f551b83..75a734c5cac5 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -143,6 +143,28 @@ static void tpm_devs_release(struct device *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); + } +} + +/** * tpm_chip_alloc() - allocate a new struct tpm_chip instance * @pdev: device to which the chip is associated * At this point pdev mst be initialized, but does not have to @@ -181,6 +203,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;