From patchwork Mon Sep 16 11:07:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805281 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40D7F149C57; Mon, 16 Sep 2024 11:07:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484847; cv=none; b=J8v5xE8h1AaOyEP1i71YsuEkOv1NkLQrHe3Xs4AnWaEfsGaNUBSJCdxIzC+p8aG8iTPPMBdUnXwEbM5qYlDiWvCCvttTdVpKODIEwy7cjJOdf1IbhR4wJ+kKLfDP7ogTNO+Hec4IjH2/lBqYi6l71lSjJQzwClv7nQYU6J3rtfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484847; c=relaxed/simple; bh=khBmSXOuLlVYvAwvZ+9x5G0EGtluhzmI2sB1ubUkQgI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=af3pzNJhA1C6Cb4ngYtgEUhH7K2Ol3vbPV7NI3xQ6BZRAM7TrolGgh2dkl0x8T7SaBUZedev5oOu8LlwJ7SI9J2Nl162SIpvI7MUR2+ihYPiIJeYn5sr20/exlmSiorIqY63apLHW75GbAKmdrSBF63KMqUJRWCkjtCGtpR/Klw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Lon6fQqe; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Lon6fQqe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 53FEAC4CEC4; Mon, 16 Sep 2024 11:07:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484846; bh=khBmSXOuLlVYvAwvZ+9x5G0EGtluhzmI2sB1ubUkQgI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Lon6fQqe/GjDLNyi1VC44dqYI+3OlBrmsU8s91QsRqach08cYlVvw9ax3HpK16ZZJ p963NkwcgIK9mYh9AAdbS6CoyRG4KLmb3ZQljDGH9E79w6rtiFtluuDsV62H38QP9D mLS4KEZrDdktiT86mo5zOkkPEAwEWfmNVGwjSw4cihb39xBe4cRPAjE+IdWr2k6KlX SM+UQV114Y5QZ+StH/aFgSmPH9y+kehSgpoGEzhxv6lyo8GqRIUoGAPKht8TZv3qYu QBc9zJG8Sb3HAl6VP52WRv9kZwzxqInUOJUzmuMivvSzoMinH2rxk2Ja/OIDFDvrKo Tv0ybYaGCxixw== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 1/6] tpm: Remove documentation from the header of tpm2-sessions.c Date: Mon, 16 Sep 2024 14:07:06 +0300 Message-ID: <20240916110714.1396407-2-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The documentation in the file header is duplicate documentation to kernel doc comments in the function declarations and tpm-security.rst. Wipe it off as nobody will ever take care of keeping it up to date. Signed-off-by: Jarkko Sakkinen --- v2: - Refine the commit message. --- drivers/char/tpm/tpm2-sessions.c | 65 -------------------------------- 1 file changed, 65 deletions(-) diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index 44f60730cff4..6cc1ea81c57c 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -1,71 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 - /* * Copyright (C) 2018 James.Bottomley@HansenPartnership.com - * - * Cryptographic helper routines for handling TPM2 sessions for - * authorization HMAC and request response encryption. - * - * The idea is to ensure that every TPM command is HMAC protected by a - * session, meaning in-flight tampering would be detected and in - * addition all sensitive inputs and responses should be encrypted. - * - * The basic way this works is to use a TPM feature called salted - * sessions where a random secret used in session construction is - * encrypted to the public part of a known TPM key. The problem is we - * have no known keys, so initially a primary Elliptic Curve key is - * derived from the NULL seed (we use EC because most TPMs generate - * these keys much faster than RSA ones). The curve used is NIST_P256 - * because that's now mandated to be present in 'TCG TPM v2.0 - * Provisioning Guidance' - * - * Threat problems: the initial TPM2_CreatePrimary is not (and cannot - * be) session protected, so a clever Man in the Middle could return a - * public key they control to this command and from there intercept - * and decode all subsequent session based transactions. The kernel - * cannot mitigate this threat but, after boot, userspace can get - * proof this has not happened by asking the TPM to certify the NULL - * key. This certification would chain back to the TPM Endorsement - * Certificate and prove the NULL seed primary had not been tampered - * with and thus all sessions must have been cryptographically secure. - * To assist with this, the initial NULL seed public key name is made - * available in a sysfs file. - * - * Use of these functions: - * - * The design is all the crypto, hash and hmac gunk is confined in this - * file and never needs to be seen even by the kernel internal user. To - * the user there's an init function tpm2_sessions_init() that needs to - * be called once per TPM which generates the NULL seed primary key. - * - * These are the usage functions: - * - * tpm2_start_auth_session() which allocates the opaque auth structure - * and gets a session from the TPM. This must be called before - * any of the following functions. The session is protected by a - * session_key which is derived from a random salt value - * encrypted to the NULL seed. - * tpm2_end_auth_session() kills the session and frees the resources. - * Under normal operation this function is done by - * tpm_buf_check_hmac_response(), so this is only to be used on - * error legs where the latter is not executed. - * tpm_buf_append_name() to add a handle to the buffer. This must be - * used in place of the usual tpm_buf_append_u32() for adding - * handles because handles have to be processed specially when - * calculating the HMAC. In particular, for NV, volatile and - * permanent objects you now need to provide the name. - * tpm_buf_append_hmac_session() which appends the hmac session to the - * buf in the same way tpm_buf_append_auth does(). - * tpm_buf_fill_hmac_session() This calculates the correct hash and - * places it in the buffer. It must be called after the complete - * command buffer is finalized so it can fill in the correct HMAC - * based on the parameters. - * tpm_buf_check_hmac_response() which checks the session response in - * the buffer and calculates what it should be. If there's a - * mismatch it will log a warning and return an error. If - * tpm_buf_append_hmac_session() did not specify - * TPM_SA_CONTINUE_SESSION then the session will be closed (if it - * hasn't been consumed) and the auth structure freed. */ #include "tpm.h" From patchwork Mon Sep 16 11:07:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805282 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29DA714F9FB; Mon, 16 Sep 2024 11:07:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484851; cv=none; b=oIFx1tX4fxHjJx7r7YG561eYUuZALPfVqSCErlhcWwAn/a9RTIVDMXun1DoPCgo8NsrIkXBAhC3xKeC04IeTMXxIl0bRE2ntgWqS/SoFxDKKgWVzcccQKDXXs/iuY5+hB2nmasU/6urc2oVhnsKb4SWgXgUeH75W2xFdE/V0iX8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484851; c=relaxed/simple; bh=WKndujFOl4HJUdZxPiD2QOGHxLXb87Q7q/U3qAy0Wpo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ue63OpltDMk8MGkkWYJGg8xnYP1+GdEk17uCtq0aQlVcWSiz8rpuQWr18DO7jzlDmGoKhJWR8KQfu5dfNUlAeIB1YO4CYkGutov+Q/vJAKrMq7I+LmglkSnkSTb8t2Bu71JRwGzSjnZtk+AoN4dTTMuZaP7Xac+sV1GH24EKl5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Sb+W3zzG; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Sb+W3zzG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F0FBC4CECF; Mon, 16 Sep 2024 11:07:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484850; bh=WKndujFOl4HJUdZxPiD2QOGHxLXb87Q7q/U3qAy0Wpo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sb+W3zzGl63tk4zLCIBX91MR2i7HIYHfO/b+VmSAM1/EFS7UY9RfhUueMNELM0EaU Hbirwy/QSDw2OkLsZigJA1MJNruSpJfhEHEYkj+1O8owi/NdWGwKuUYOy7+AEW81hA BVTfmqAHogvGlNGV7i/vfkiF0LEj+GuLD4iczuP+0QboZVicPVK2VNsqVaQm+RZOnb 5jKyZMst4CWkz17truMx7U3NThJ20mXDYNUHPhCsKA7jAY/ZPLWOGxfuf3RSfT3I/q JTa3OCCnquJ7CAIAqPFRLMGY83GSEcoGRNw+XutJZqqE81QOnw6OKzHrmp0XE+JiRR HBlzqxzUlS6Rw== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , stable@vger.kernel.org, Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 2/6] tpm: Return on tpm2_create_null_primary() failure Date: Mon, 16 Sep 2024 14:07:07 +0300 Message-ID: <20240916110714.1396407-3-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 tpm2_sessions_init() ignores the return value of tpm2_create_null_primary(). Address this by returning on failure. Cc: stable@vger.kernel.org # v6.11+ Fixes: d2add27cf2b8 ("tpm: Add NULL primary creation") Signed-off-by: Jarkko Sakkinen --- v2: - Refined the commit message. --- drivers/char/tpm/tpm2-sessions.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index 6cc1ea81c57c..d63510ad44ab 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -1288,8 +1288,10 @@ int tpm2_sessions_init(struct tpm_chip *chip) int rc; rc = tpm2_create_null_primary(chip); - if (rc) + if (rc) { dev_err(&chip->dev, "TPM: security failed (NULL seed derivation): %d\n", rc); + return rc; + } chip->auth = kmalloc(sizeof(*chip->auth), GFP_KERNEL); if (!chip->auth) From patchwork Mon Sep 16 11:07:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805283 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C206156661; Mon, 16 Sep 2024 11:07:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484855; cv=none; b=iz36XQg23isWknpaAF7Ffq2mG0+gPdSsqKX62GtayfeEKt3unKkWRXh0q9MucxC2j283GNtsfmWBEzIG7UtLpVwcwS4EAV4bxXOJuEppDtgEJbtXz6QXxyaQIKETdgsFD44+yFBkBark7UC/v1nbc1ZdpYPXcNxxlPj4U6xQi/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484855; c=relaxed/simple; bh=Qw+w7KRfHJ0gIw4SApLuVV0TeYVyKK0mF4hCdPm1L68=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qad1YVhzgWxzWKeCSjdDRFtwsua7JEVlgTd3cEm9jsSbNBB69W+IWmkKNKlopUON31OL3EYuFnt2Whpezlt7o7pBXb4UQBozXLxIq6BGQ4hyaNOXbJGTkofShK27wfzWqSEeqvhZyL24Rv20h+aPTfG0jKgLdwkvA5lnDxgJuBQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qFDppsOs; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="qFDppsOs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 72B07C4CEC4; Mon, 16 Sep 2024 11:07:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484854; bh=Qw+w7KRfHJ0gIw4SApLuVV0TeYVyKK0mF4hCdPm1L68=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qFDppsOsqgwfFpQ55MhwYdmVmh4f+d2Q70uvglLj6LBcwQo76j3glQGdPkpwhIvsd DeZqjc26MMeRBVYOe04gdoPOGcWsJ/ZcQmiAgMR0J3LiHOkFffv/eTHA9hR3zHC7kU PCn2974yWcij6vLGE7bvlBQpPHl5BSyVl0q6TaL/wc2kcyswq/rCHw3Oh42deWqj7X U2vgj/G3kYWEEHW5HWzj1+qpdUwrOmA/OaOGmTEDpTBMGC79G0i5EOftDJ2q5R9SFl MTstneelbG6BnHx7XOuzvkZ2TvHoQVxEQhgqUqAJWl0/CL07SuAz1v+5wFlPGTcwTT NEq9XzuMnaDTA== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , stable@vger.kernel.org, Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 3/6] tpm: Return on tpm2_create_primary() failure in tpm2_load_null() Date: Mon, 16 Sep 2024 14:07:08 +0300 Message-ID: <20240916110714.1396407-4-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 tpm2_load_null() ignores the return value of tpm2_create_primary(). Further, it does not heal from the situation when memcmp() returns zero. Address this by returning on failure and saving the null key if there was no detected interference in the bus. Cc: stable@vger.kernel.org # v6.11+ Fixes: eb24c9788cd9 ("tpm: disable the TPM if NULL name changes") Signed-off-by: Jarkko Sakkinen --- v2: - Refined the commit message. - Reverted tpm2_create_primary() changes. They are not required if tmp_null_key is used as the parameter. --- drivers/char/tpm/tpm2-sessions.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index d63510ad44ab..9c0356d7ce5e 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -850,22 +850,32 @@ static int tpm2_parse_start_auth_session(struct tpm2_auth *auth, static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key) { - int rc; unsigned int offset = 0; /* dummy offset for null seed context */ u8 name[SHA256_DIGEST_SIZE + 2]; + u32 tmp_null_key; + int rc; rc = tpm2_load_context(chip, chip->null_key_context, &offset, - null_key); - if (rc != -EINVAL) + &tmp_null_key); + if (rc != -EINVAL) { + if (!rc) + *null_key = tmp_null_key; return rc; + } /* an integrity failure may mean the TPM has been reset */ dev_err(&chip->dev, "NULL key integrity failure!\n"); - /* check the null name against what we know */ - tpm2_create_primary(chip, TPM2_RH_NULL, NULL, name); - if (memcmp(name, chip->null_key_name, sizeof(name)) == 0) - /* name unchanged, assume transient integrity failure */ + + rc = tpm2_create_primary(chip, TPM2_RH_NULL, &tmp_null_key, name); + if (rc) return rc; + + /* Return the null key if the name has not been changed: */ + if (memcmp(name, chip->null_key_name, sizeof(name)) == 0) { + *null_key = tmp_null_key; + return 0; + } + /* * Fatal TPM failure: the NULL seed has actually changed, so * the TPM must have been illegally reset. All in-kernel TPM @@ -874,6 +884,7 @@ static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key) * userspace programmes can't be compromised by it. */ dev_err(&chip->dev, "NULL name has changed, disabling TPM due to interference\n"); + tpm2_flush_context(chip, tmp_null_key); chip->flags |= TPM_CHIP_FLAG_DISABLE; return rc; From patchwork Mon Sep 16 11:07:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805284 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7134D155333; Mon, 16 Sep 2024 11:07:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484860; cv=none; b=d02A8+hUPxATEZ4bJWycXH4Gszu+p6Eio4+9NcRZGzGv500JMJybWnK1aoBorzuccQ1azc4S+VEybAKiEip3xsl/YLVrr+cwD1ozqxaBG14Xe5/BUNlfssku/0SoG1fyoq10hytY4LEI92Bwv5bqC4vDkAWC1ffwWfDkr4C2fCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484860; c=relaxed/simple; bh=LMBOVTLhQra6uPehKBIVfVv9QIeKqZZrvqc1FlP7ctg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pgGy6h3xlMSfSSfOi/540DuwBcCgSM0gChY1saYoSs2qyD0tKZRHP80VM/KAdoXIylWX4ZcS1s9MClvq5+AKfZjzyoE+FhsFQPZxC9fNBZ7Jjz+80zZ1A5UfX+I1TTK6BHmDWHTCdNVEzSJLDSdzoxb4TtCOQoMZey/iwYDzI7w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=chJIxOBU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="chJIxOBU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3208C4CEC4; Mon, 16 Sep 2024 11:07:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484860; bh=LMBOVTLhQra6uPehKBIVfVv9QIeKqZZrvqc1FlP7ctg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=chJIxOBUJyJ0GRVx2dPAHMo/r/TFtL7O7XK7dx13FBMhy1pB1vKnB3B73aUY7Ymic /1bcUaPY4/sDEZpXEtYjIoXmHiI8TrTfETpaTU1wJEZPnvJN+GSRFHsxY72PpEGbR8 0/5wKezpu9viUiDZlmSfRnTBBb+jzwaHYcj0Uv8muaGKHQdvfX1J9BBK7FxacC586e 54/nsdwZAImAL+XMKbM4hd5Klygb2qesftiIZdO4wk2TZs2LpNkACDVbTlZK92pC+5 zK02Wcwmg/DN4XMtJwYA+TE9LNrFKBd+xPlhSzTd2pzDNTzSqmsq2UP5Yepcj+9WcC sVy6Yv0H82lSQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , Stefan Berger , Ard Biesheuvel , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 4/6] tpm: flush the null key only when /dev/tpm0 is accessed Date: Mon, 16 Sep 2024 14:07:09 +0300 Message-ID: <20240916110714.1396407-5-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Instead of flushing and reloading the null key for every single auth session, flush it only when: 1. User space needs to access /dev/tpm{rm}0. 2. When going to sleep. 3. When unregistering the chip. This removes the need to load and swap the null key between TPM and regular memory per transaction, when the user space is not using the chip. Tested-by: Pengyu Ma Signed-off-by: Jarkko Sakkinen --- v2: - Refined the commit message. - Added tested-by from Pengyu Ma . - Removed spurious pr_info() statement. --- drivers/char/tpm/tpm-chip.c | 13 +++++++++++++ drivers/char/tpm/tpm-dev-common.c | 7 +++++++ drivers/char/tpm/tpm-interface.c | 9 +++++++-- drivers/char/tpm/tpm2-cmd.c | 3 +++ drivers/char/tpm/tpm2-sessions.c | 17 ++++++++++++++--- include/linux/tpm.h | 2 ++ 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 854546000c92..0ea00e32f575 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -674,6 +674,19 @@ EXPORT_SYMBOL_GPL(tpm_chip_register); */ void tpm_chip_unregister(struct tpm_chip *chip) { +#ifdef CONFIG_TCG_TPM2_HMAC + int rc; + + rc = tpm_try_get_ops(chip); + if (!rc) { + if (chip->flags & TPM_CHIP_FLAG_TPM2) { + tpm2_flush_context(chip, chip->null_key); + chip->null_key = 0; + } + tpm_put_ops(chip); + } +#endif + tpm_del_legacy_sysfs(chip); if (tpm_is_hwrng_enabled(chip)) hwrng_unregister(&chip->hwrng); diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c index c3fbbf4d3db7..4bc07963e260 100644 --- a/drivers/char/tpm/tpm-dev-common.c +++ b/drivers/char/tpm/tpm-dev-common.c @@ -27,6 +27,13 @@ static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space, struct tpm_header *header = (void *)buf; ssize_t ret, len; +#ifdef CONFIG_TCG_TPM2_HMAC + if (chip->flags & TPM_CHIP_FLAG_TPM2) { + tpm2_flush_context(chip, chip->null_key); + chip->null_key = 0; + } +#endif + ret = tpm2_prepare_space(chip, space, buf, bufsiz); /* If the command is not implemented by the TPM, synthesize a * response with a TPM2_RC_COMMAND_CODE return for user-space. diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index 5da134f12c9a..bfa47d48b0f2 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -379,10 +379,15 @@ int tpm_pm_suspend(struct device *dev) rc = tpm_try_get_ops(chip); if (!rc) { - if (chip->flags & TPM_CHIP_FLAG_TPM2) + if (chip->flags & TPM_CHIP_FLAG_TPM2) { +#ifdef CONFIG_TCG_TPM2_HMAC + tpm2_flush_context(chip, chip->null_key); + chip->null_key = 0; +#endif tpm2_shutdown(chip, TPM2_SU_STATE); - else + } else { rc = tpm1_pm_suspend(chip, tpm_suspend_pcr); + } tpm_put_ops(chip); } diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 1e856259219e..aba024cbe7c5 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -364,6 +364,9 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle) struct tpm_buf buf; int rc; + if (!handle) + return; + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); if (rc) { dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n", diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index 9c0356d7ce5e..fc1f3502e6ee 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -855,11 +855,19 @@ static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key) u32 tmp_null_key; int rc; + /* fast path */ + if (chip->null_key) { + *null_key = chip->null_key; + return 0; + } + rc = tpm2_load_context(chip, chip->null_key_context, &offset, &tmp_null_key); if (rc != -EINVAL) { - if (!rc) + if (!rc) { + chip->null_key = tmp_null_key; *null_key = tmp_null_key; + } return rc; } @@ -872,6 +880,7 @@ static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key) /* Return the null key if the name has not been changed: */ if (memcmp(name, chip->null_key_name, sizeof(name)) == 0) { + chip->null_key = tmp_null_key; *null_key = tmp_null_key; return 0; } @@ -949,7 +958,6 @@ int tpm2_start_auth_session(struct tpm_chip *chip) tpm_buf_append_u16(&buf, TPM_ALG_SHA256); rc = tpm_transmit_cmd(chip, &buf, 0, "start auth session"); - tpm2_flush_context(chip, null_key); if (rc == TPM2_RC_SUCCESS) rc = tpm2_parse_start_auth_session(auth, &buf); @@ -1281,7 +1289,10 @@ static int tpm2_create_null_primary(struct tpm_chip *chip) rc = tpm2_save_context(chip, null_key, chip->null_key_context, sizeof(chip->null_key_context), &offset); - tpm2_flush_context(chip, null_key); + if (rc) + tpm2_flush_context(chip, null_key); + else + chip->null_key = null_key; } return rc; diff --git a/include/linux/tpm.h b/include/linux/tpm.h index e93ee8d936a9..4eb39db80e05 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -205,6 +205,8 @@ struct tpm_chip { #ifdef CONFIG_TCG_TPM2_HMAC /* details for communication security via sessions */ + /* loaded null key */ + u32 null_key; /* saved context for NULL seed */ u8 null_key_context[TPM2_MAX_CONTEXT_SIZE]; /* name of NULL seed */ From patchwork Mon Sep 16 11:07:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805285 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 72B3A13B79F; Mon, 16 Sep 2024 11:07:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484864; cv=none; b=G7aTHTkj+H14/XD1l1TrASXFmT5FFMf69PIhqt/KZ0IgHpDRzMpvf+018oZ3shbqhBJFMAdvWjYPRVtC+KR9nI1jf6ijzgHieR9ErLhQXOP4cS0EkangAOXQIXxYH44ACud4KrhGmJ066DF+NMyUULBUb5GaQ9rETqNb/trdT1U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484864; c=relaxed/simple; bh=PS65qO6IIeNlJU7mQBF4NPFIdNyDq11GyCkWvQYX8Cw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SUNCZEBT/7HnJ3Fs4cTpP/lIrz2ma6qenlDLoQLgz2gd+UOqOC8pJkxpKf3jtBvKJaV9TeG57FseFhjWd1X9Hy59nnVkIXrYZO7FpyWTqjWjpXe6JGPtQDEvDDYzQDZkiwmS1KBKbihZsbkIHn7qKJUJ5avz9VLdGvYELJQLzw0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K2Xn5R34; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="K2Xn5R34" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B0048C4CECC; Mon, 16 Sep 2024 11:07:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484864; bh=PS65qO6IIeNlJU7mQBF4NPFIdNyDq11GyCkWvQYX8Cw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K2Xn5R34+/fSY7hPX/Yd4b/psu925W1xkbyj9GDkCn8iseTqDaU9J3Zv3Ku5yZVFZ A7G850zhSWLzxGyswJxlW9qNMTGsSe+Pl0jTTdww21Mu2zuiNbAzn0bdyb+rjFb/j9 1bJ4mDjM4ar4MPYfarWY4IkA+4uzBcAUc4CfUJpso1CPwEskWRTCgXtKmvkMYVnadW K+JaJQUDMjEIFlvoJ7rmkrkXEeiz4wlBjNuX3IjHRoz9gmVBNyLdv1fBHiIUmnFCwN 9ygNhkxQ2JpD3j9YeIJCU1sBTMvkgeB73Mifl6Ga9Pcby/dCLXPDHJzhHvBfLs5jva DA5bkndbfb0Ww== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 5/6] tpm: Allocate chip->auth in tpm2_start_auth_session() Date: Mon, 16 Sep 2024 14:07:10 +0300 Message-ID: <20240916110714.1396407-6-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Move allocation of chip->auth to tpm2_start_auth_session() so that the field can be used as flag to tell whether auth session is active or not. Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm2-sessions.c | 43 +++++++++++++++++++------------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index fc1f3502e6ee..f7746a165695 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -419,7 +419,8 @@ static void tpm2_KDFe(u8 z[EC_PT_SZ], const char *str, u8 *pt_u, u8 *pt_v, sha256_final(&sctx, out); } -static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip) +static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip, + struct tpm2_auth *auth) { struct crypto_kpp *kpp; struct kpp_request *req; @@ -478,7 +479,7 @@ static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip) sg_set_buf(&s[0], chip->null_ec_key_x, EC_PT_SZ); sg_set_buf(&s[1], chip->null_ec_key_y, EC_PT_SZ); kpp_request_set_input(req, s, EC_PT_SZ*2); - sg_init_one(d, chip->auth->salt, EC_PT_SZ); + sg_init_one(d, auth->salt, EC_PT_SZ); kpp_request_set_output(req, d, EC_PT_SZ); crypto_kpp_compute_shared_secret(req); kpp_request_free(req); @@ -489,8 +490,7 @@ static void tpm_buf_append_salt(struct tpm_buf *buf, struct tpm_chip *chip) * This works because KDFe fully consumes the secret before it * writes the salt */ - tpm2_KDFe(chip->auth->salt, "SECRET", x, chip->null_ec_key_x, - chip->auth->salt); + tpm2_KDFe(auth->salt, "SECRET", x, chip->null_ec_key_x, auth->salt); out: crypto_free_kpp(kpp); @@ -789,6 +789,8 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf, /* manually close the session if it wasn't consumed */ tpm2_flush_context(chip, auth->handle); memzero_explicit(auth, sizeof(*auth)); + kfree(auth); + chip->auth = NULL; } else { /* reset for next use */ auth->session = TPM_HEADER_SIZE; @@ -817,6 +819,8 @@ void tpm2_end_auth_session(struct tpm_chip *chip) tpm2_flush_context(chip, auth->handle); memzero_explicit(auth, sizeof(*auth)); + kfree(auth); + chip->auth = NULL; } EXPORT_SYMBOL(tpm2_end_auth_session); @@ -913,25 +917,29 @@ static int tpm2_load_null(struct tpm_chip *chip, u32 *null_key) */ int tpm2_start_auth_session(struct tpm_chip *chip) { + struct tpm2_auth *auth; struct tpm_buf buf; - struct tpm2_auth *auth = chip->auth; - int rc; u32 null_key; + int rc; - if (!auth) { - dev_warn_once(&chip->dev, "auth session is not active\n"); + if (chip->auth) { + dev_warn_once(&chip->dev, "auth session is active\n"); return 0; } + auth = kzalloc(sizeof(*auth), GFP_KERNEL); + if (!auth) + return -ENOMEM; + rc = tpm2_load_null(chip, &null_key); if (rc) - goto out; + goto err; auth->session = TPM_HEADER_SIZE; rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_START_AUTH_SESS); if (rc) - goto out; + goto err; /* salt key handle */ tpm_buf_append_u32(&buf, null_key); @@ -943,7 +951,7 @@ int tpm2_start_auth_session(struct tpm_chip *chip) tpm_buf_append(&buf, auth->our_nonce, sizeof(auth->our_nonce)); /* append encrypted salt and squirrel away unencrypted in auth */ - tpm_buf_append_salt(&buf, chip); + tpm_buf_append_salt(&buf, chip, auth); /* session type (HMAC, audit or policy) */ tpm_buf_append_u8(&buf, TPM2_SE_HMAC); @@ -964,10 +972,13 @@ int tpm2_start_auth_session(struct tpm_chip *chip) tpm_buf_destroy(&buf); - if (rc) - goto out; + if (rc == TPM2_RC_SUCCESS) { + chip->auth = auth; + return 0; + } - out: +err: + kfree(auth); return rc; } EXPORT_SYMBOL(tpm2_start_auth_session); @@ -1315,10 +1326,6 @@ int tpm2_sessions_init(struct tpm_chip *chip) return rc; } - chip->auth = kmalloc(sizeof(*chip->auth), GFP_KERNEL); - if (!chip->auth) - return -ENOMEM; - return rc; } EXPORT_SYMBOL(tpm2_sessions_init); From patchwork Mon Sep 16 11:07:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 13805286 X-Patchwork-Delegate: paul@paul-moore.com Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3992714E2DA; Mon, 16 Sep 2024 11:07:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484868; cv=none; b=hc2F81Pc0xW8UXNpyawjJzPxGC6b8fG4TatzCC8aF5/OT8WOCByOFcJSXemIvvhP6gZop3HDzp+lvdWYA+rLUh/x6LZHhrvMYB9Yi2INmK1udi63LAhqrg6ACMfB5TMqOWj2ycUxGRw5GtNicGkKnw65bt6prFeptEdDE8N5pZk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726484868; c=relaxed/simple; bh=xD2s5On1ZCWWzafhSszKhanrFM8TIDWSq6PdNHlzYy0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nANs7SNLPQr/lvGKp2aRioyHycv1Qaw3ozC4Ycen5TpIBO7VPZVQ6gqJ8IlGzph+uKbjRyK3DJcTZaT9O0mlnJfTMqopEB47ZYPiCtFxjldh25ACiS6jtI1VpKxtxQm5LO8QCNa0DVK2wGNoH6esmjCYrwWZtfzX8avTC9OrCvw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AY0jUZP5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AY0jUZP5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7251C4CEC4; Mon, 16 Sep 2024 11:07:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1726484868; bh=xD2s5On1ZCWWzafhSszKhanrFM8TIDWSq6PdNHlzYy0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AY0jUZP5uKNbnY5Z8u9a6VGpH+29Fu+8WquuEtw3+rXhKUC7QWKx2b4USwVrhInDH m/EbDH33kcy2drqRApYKAZTe7ccQGnXeWmMAsmYA2sMJ4D6o7Wtqb2/Tum+9+s58iF fhixB1bh2UNfyZ4TaE0F4FJFDBVrWqBkssb0W9jyuMxAskhzveCqLsBT/+tW4qx11E ZoU8MpjO0zqw7/66DE7H0fvQxemxbwsMzbjiYVXvx3OhMc4STH8fkZme24iBdzH7n1 gQRI4Co0YUDx9z26/Ny/DsuJ1RCDoKrcRQ6s5gQUw9667/G+r5r6YxFFR9zeXC6DfC uLzUK1ql7nIlQ== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: James.Bottomley@HansenPartnership.com, roberto.sassu@huawei.com, mapengyu@gmail.com, Jarkko Sakkinen , Peter Huewe , Jason Gunthorpe , Mimi Zohar , David Howells , Paul Moore , James Morris , "Serge E. Hallyn" , linux-kernel@vger.kernel.org (open list), keyrings@vger.kernel.org (open list:KEYS-TRUSTED), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v2 6/6] tpm: flush the auth session only when /dev/tpm0 is open Date: Mon, 16 Sep 2024 14:07:11 +0300 Message-ID: <20240916110714.1396407-7-jarkko@kernel.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240916110714.1396407-1-jarkko@kernel.org> References: <20240916110714.1396407-1-jarkko@kernel.org> Precedence: bulk X-Mailing-List: linux-security-module@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Instead of flushing and reloading the auth session for every single transaction, keep the session open unless /dev/tpm0 is open. In practice this means applying TPM2_SA_CONTINUE_SESSION to the session attributes. Flush the session always when /dev/tpm0 is written. Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-chip.c | 1 + drivers/char/tpm/tpm-dev-common.c | 1 + drivers/char/tpm/tpm-interface.c | 1 + drivers/char/tpm/tpm2-sessions.c | 4 ++++ 4 files changed, 7 insertions(+) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 0ea00e32f575..7a6bb30d1f32 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -680,6 +680,7 @@ void tpm_chip_unregister(struct tpm_chip *chip) rc = tpm_try_get_ops(chip); if (!rc) { if (chip->flags & TPM_CHIP_FLAG_TPM2) { + tpm2_end_auth_session(chip); tpm2_flush_context(chip, chip->null_key); chip->null_key = 0; } diff --git a/drivers/char/tpm/tpm-dev-common.c b/drivers/char/tpm/tpm-dev-common.c index 4bc07963e260..c6fdeb4feaef 100644 --- a/drivers/char/tpm/tpm-dev-common.c +++ b/drivers/char/tpm/tpm-dev-common.c @@ -29,6 +29,7 @@ static ssize_t tpm_dev_transmit(struct tpm_chip *chip, struct tpm_space *space, #ifdef CONFIG_TCG_TPM2_HMAC if (chip->flags & TPM_CHIP_FLAG_TPM2) { + tpm2_end_auth_session(chip); tpm2_flush_context(chip, chip->null_key); chip->null_key = 0; } diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c index bfa47d48b0f2..2363018fa8fb 100644 --- a/drivers/char/tpm/tpm-interface.c +++ b/drivers/char/tpm/tpm-interface.c @@ -381,6 +381,7 @@ int tpm_pm_suspend(struct device *dev) if (!rc) { if (chip->flags & TPM_CHIP_FLAG_TPM2) { #ifdef CONFIG_TCG_TPM2_HMAC + tpm2_end_auth_session(chip); tpm2_flush_context(chip, chip->null_key); chip->null_key = 0; #endif diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c index f7746a165695..efe4b0017a83 100644 --- a/drivers/char/tpm/tpm2-sessions.c +++ b/drivers/char/tpm/tpm2-sessions.c @@ -268,6 +268,10 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf, } #ifdef CONFIG_TCG_TPM2_HMAC + /* The first write to /dev/tpm{rm0} will flush the session. */ + if (!chip->is_open) + attributes |= TPM2_SA_CONTINUE_SESSION; + /* * The Architecture Guide requires us to strip trailing zeros * before computing the HMAC