From patchwork Wed Jan 18 15:10:42 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Bottomley X-Patchwork-Id: 716694 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 3v3Vlz6sn6z9stc for ; Thu, 19 Jan 2017 02:10:55 +1100 (AEDT) Received: from localhost ([127.0.0.1] helo=sfs-ml-2.v29.ch3.sourceforge.com) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cTrt0-0001JN-7j; Wed, 18 Jan 2017 15:10:54 +0000 Received: from sog-mx-2.v43.ch3.sourceforge.com ([172.29.43.192] helo=mx.sourceforge.net) by sfs-ml-2.v29.ch3.sourceforge.com with esmtp (Exim 4.76) (envelope-from ) id 1cTrsy-0001JH-Ls for tpmdd-devel@lists.sourceforge.net; Wed, 18 Jan 2017 15:10:52 +0000 Received-SPF: pass (sog-mx-2.v43.ch3.sourceforge.com: domain of HansenPartnership.com designates 66.63.167.143 as permitted sender) client-ip=66.63.167.143; envelope-from=James.Bottomley@HansenPartnership.com; helo=bedivere.hansenpartnership.com; Received: from bedivere.hansenpartnership.com ([66.63.167.143]) by sog-mx-2.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1cTrsx-0005sa-Bg for tpmdd-devel@lists.sourceforge.net; Wed, 18 Jan 2017 15:10:52 +0000 Received: from localhost (localhost [127.0.0.1]) by bedivere.hansenpartnership.com (Postfix) with ESMTP id 9C1A48EE2B2; Wed, 18 Jan 2017 07:10:45 -0800 (PST) Received: from bedivere.hansenpartnership.com ([127.0.0.1]) by localhost (bedivere.hansenpartnership.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id yCk9OYPK_nfL; Wed, 18 Jan 2017 07:10:45 -0800 (PST) Received: from [9.232.160.153] (unknown [129.33.253.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by bedivere.hansenpartnership.com (Postfix) with ESMTPSA id 01EBD8EE07D; Wed, 18 Jan 2017 07:10:44 -0800 (PST) Message-ID: <1484752242.2717.17.camel@HansenPartnership.com> From: James Bottomley To: tpmdd-devel@lists.sourceforge.net Date: Wed, 18 Jan 2017 10:10:42 -0500 In-Reply-To: <1484752097.2717.14.camel@HansenPartnership.com> References: <1484752097.2717.14.camel@HansenPartnership.com> X-Mailer: Evolution 3.16.5 Mime-Version: 1.0 X-Spam-Score: -4.8 (----) 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 SPF_PASS SPF: sender matches SPF record -3.2 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: 1cTrsx-0005sa-Bg Cc: linux-security-module@vger.kernel.org, open list Subject: [tpmdd-devel] [PATCH 2/2] tpm2: context save and restore space managed sessions 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: , Errors-To: tpmdd-devel-bounces@lists.sourceforge.net Now that sessions are isolated, we can introduce a session_buf in the tpm2 space to save and restore them. This allows us to have many more sessions active simultaneously (up to TPM_PT_MAX_SESSIONS). As part of this, we must intercept and manually remove contexts for flushed sessions. Signed-off-by: James Bottomley --- drivers/char/tpm/tpm-chip.c | 6 ++ drivers/char/tpm/tpm.h | 1 + drivers/char/tpm/tpm2-space.c | 223 ++++++++++++++++++++++++++++-------------- drivers/char/tpm/tpms-dev.c | 7 ++ 4 files changed, 164 insertions(+), 73 deletions(-) diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c index 96ea93e..a625884 100644 --- a/drivers/char/tpm/tpm-chip.c +++ b/drivers/char/tpm/tpm-chip.c @@ -130,6 +130,7 @@ static void tpm_dev_release(struct device *dev) kfree(chip->log.bios_event_log); kfree(chip->work_space.context_buf); + kfree(chip->work_space.session_buf); kfree(chip); } @@ -223,6 +224,11 @@ struct tpm_chip *tpm_chip_alloc(struct device *pdev, rc = -ENOMEM; goto out; } + chip->work_space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (!chip->work_space.session_buf) { + rc = -ENOMEM; + goto out; + } return chip; diff --git a/drivers/char/tpm/tpm.h b/drivers/char/tpm/tpm.h index 265b7f5..9923daa 100644 --- a/drivers/char/tpm/tpm.h +++ b/drivers/char/tpm/tpm.h @@ -159,6 +159,7 @@ struct tpm_space { u32 context_tbl[14]; u8 *context_buf; u32 session_tbl[6]; + u8 *session_buf; }; enum tpm_chip_flags { diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c index 49048af..04c9431 100644 --- a/drivers/char/tpm/tpm2-space.c +++ b/drivers/char/tpm/tpm2-space.c @@ -27,6 +27,91 @@ enum tpm2_handle_types { #define TPM2_HT_TAG_FOR_FLUSH 0xF0000000 +struct tpm2_context { + __be64 sequence; + __be32 saved_handle; + __be32 hierarchy; + __be16 blob_size; +} __packed; + +static int tpm2_context_save(struct tpm_chip *chip, u8 *area, + int *offset, u32 handle) +{ + struct tpm_buf buf; + u32 s; + int rc; + + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, + TPM2_CC_CONTEXT_SAVE); + if (rc) + return rc; + + tpm_buf_append_u32(&buf, handle); + + rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, + TPM_HEADER_SIZE, TPM_TRANSMIT_UNLOCKED, + NULL); + if ((rc & TPM2_RC_HANDLE) == TPM2_RC_HANDLE) { + /* no handle to save */ + rc = 1; + goto out; + } else if (rc) { + dev_warn(&chip->dev, "%s: saving failed with %d\n", + __func__, rc); + rc = -EFAULT; + goto out; + } + + s = tpm_buf_length(&buf) - TPM_HEADER_SIZE; + if ((*offset + s) > PAGE_SIZE) { + dev_warn(&chip->dev, "out of context storage\n"); + rc = -ENOMEM; + goto out; + } + + memcpy(&area[*offset], &buf.data[TPM_HEADER_SIZE], s); + *offset += s; + + out: + tpm_buf_destroy(&buf); + return rc; +} + +static int tpm2_context_load(struct tpm_chip *chip, u8 *area, + int *offset, u32 *handle) +{ + struct tpm_buf buf; + struct tpm2_context *ctx; + int rc; + u32 s; + + rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, + TPM2_CC_CONTEXT_LOAD); + if (rc) + return rc; + + ctx = (struct tpm2_context *)&area[*offset]; + s = sizeof(*ctx) + be16_to_cpu(ctx->blob_size); + tpm_buf_append(&buf, (const void *)ctx, s); + + rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, + TPM_HEADER_SIZE + 4, + TPM_TRANSMIT_UNLOCKED, NULL); + if (rc) { + dev_warn(&chip->dev, "context loading failed with %d\n", rc); + rc = -EFAULT; + goto out; + } + *handle = get_unaligned_be32((__be32 *)&buf.data[TPM_HEADER_SIZE]); + + *offset += s; + + out: + tpm_buf_destroy(&buf); + + return rc; +} + static int tpm2_session_find(struct tpm_space *space, u32 handle) { int i; @@ -58,11 +143,35 @@ static int tpm2_session_add(struct tpm_chip *chip, return 0; } +static int tpm2_session_forget(struct tpm_space *space, u32 handle) +{ + int i, j; + struct tpm2_context *ctx; + + for (i = 0, j = 0; i < ARRAY_SIZE(space->session_tbl); i++) { + if (space->session_tbl[i] == 0) + continue; + + ctx = (struct tpm2_context *)&space->session_buf[j]; + j += sizeof(*ctx) + get_unaligned_be16(&ctx->blob_size); + + if (space->session_tbl[i] != handle) + continue; + + /* forget the session context */ + memcpy(ctx, &space->session_buf[j], PAGE_SIZE - j); + space->session_tbl[i] = 0; + break; + } + if (i == ARRAY_SIZE(space->session_tbl)) + return -EINVAL; + return 0; +} + /* if a space is active, emulate some commands */ -static int tpm2_intercept(struct tpm_chip *chip, struct tpm_space *space, - u32 cc, u8 *buf, size_t bufsiz) +static int tpm2_intercept(struct tpm_chip *chip, u32 cc, u8 *buf, size_t bufsiz) { - int j; + struct tpm_space *space = &chip->work_space; u32 handle, handle_type; if (!space) @@ -78,13 +187,7 @@ static int tpm2_intercept(struct tpm_chip *chip, struct tpm_space *space, /* let the TPM figure out and return the error */ return 0; - j = tpm2_session_find(space, handle); - if (j < 0) - return -EINVAL; - - space->session_tbl[j] |= TPM2_HT_TAG_FOR_FLUSH; - - return 0; + return tpm2_session_forget(space, handle); } void tpm2_flush_space(struct tpm_chip *chip, struct tpm_space *space) @@ -104,22 +207,12 @@ void tpm2_flush_space(struct tpm_chip *chip, struct tpm_space *space) } } -struct tpm2_context { - __be64 sequence; - __be32 saved_handle; - __be32 hierarchy; - __be16 blob_size; -} __packed; - static int tpm2_load_space(struct tpm_chip *chip) { struct tpm_space *space = &chip->work_space; - struct tpm2_context *ctx; - struct tpm_buf buf; int i; int j; int rc; - u32 s; for (i = 0, j = 0; i < ARRAY_SIZE(space->context_tbl); i++) { if (!space->context_tbl[i]) @@ -131,37 +224,33 @@ static int tpm2_load_space(struct tpm_chip *chip) return -EFAULT; } - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, - TPM2_CC_CONTEXT_LOAD); + rc = tpm2_context_load(chip, space->context_buf, + &j, &space->context_tbl[i]); if (rc) - return rc; - - ctx = (struct tpm2_context *)&space->context_buf[j]; - s = sizeof(*ctx) + be16_to_cpu(ctx->blob_size); - tpm_buf_append(&buf, &space->context_buf[j], s); - - rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, - TPM_HEADER_SIZE + 4, - TPM_TRANSMIT_UNLOCKED, NULL); - if (rc) { - dev_warn(&chip->dev, "%s: loading failed with %d\n", - __func__, rc); - rc = -EFAULT; goto out_err; - } - space->context_tbl[i] = - be32_to_cpup((__be32 *)&buf.data[TPM_HEADER_SIZE]); + } - j += s; + for (i = 0, j = 0; i < ARRAY_SIZE(space->session_tbl); i++) { + u32 handle; - tpm_buf_destroy(&buf); + if (!space->session_tbl[i]) + continue; + + rc = tpm2_context_load(chip, space->session_buf, + &j, &handle); + if (rc) + goto out_err; + if (handle != (space->session_tbl[i] & ~TPM2_HT_TAG_FOR_FLUSH)) { + dev_warn(&chip->dev, "session restored to wrong handle\n"); + rc = -EFAULT; + goto out_err; + } } return 0; out_err: - tpm_buf_destroy(&buf); tpm2_flush_space(chip, space); return rc; } @@ -297,8 +386,9 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, memcpy(&chip->work_space.session_tbl, &space->session_tbl, sizeof(space->session_tbl)); memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE); + memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE); - rc = tpm2_intercept(chip, space, cc, buf, bufsiz); + rc = tpm2_intercept(chip, cc, buf, bufsiz); if (rc) return rc; @@ -384,59 +474,45 @@ static int tpm2_map_response(struct tpm_chip *chip, u32 cc, u8 *rsp, size_t len) static int tpm2_save_space(struct tpm_chip *chip) { struct tpm_space *space = &chip->work_space; - struct tpm_buf buf; int i; int j; int rc; - u32 s; for (i = 0, j = 0; i < ARRAY_SIZE(space->context_tbl); i++) { if (!(space->context_tbl[i] && ~space->context_tbl[i])) continue; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, - TPM2_CC_CONTEXT_SAVE); - if (rc) - return rc; - - tpm_buf_append_u32(&buf, space->context_tbl[i]); - - rc = tpm_transmit_cmd(chip, NULL, buf.data, PAGE_SIZE, - TPM_HEADER_SIZE, TPM_TRANSMIT_UNLOCKED, - NULL); - if ((rc & TPM2_RC_HANDLE) == TPM2_RC_HANDLE) { + rc = tpm2_context_save(chip, space->context_buf, &j, + space->context_tbl[i]); + if (rc < 0) + goto out_err; + if (rc > 0) { space->context_tbl[i] = 0; continue; - } else if (rc) { - dev_warn(&chip->dev, "%s: saving failed with %d\n", - __func__, rc); - rc = -EFAULT; - goto out_err; } - s = tpm_buf_length(&buf) - TPM_HEADER_SIZE; - if ((j + s) > PAGE_SIZE) { - dev_warn(&chip->dev, "%s: out of backing storage\n", - __func__); - rc = -ENOMEM; - goto out_err; - } - - memcpy(&space->context_buf[j], &buf.data[TPM_HEADER_SIZE], s); - tpm2_flush_context_cmd(chip, space->context_tbl[i], TPM_TRANSMIT_UNLOCKED); space->context_tbl[i] = ~0; + } - j += s; + for (i = 0, j = 0; i < ARRAY_SIZE(space->session_tbl); i++) { + if (!space->session_tbl[i]) + continue; - tpm_buf_destroy(&buf); + rc = tpm2_context_save(chip, space->session_buf, &j, + space->session_tbl[i]); + if (rc < 0) + goto out_err; + if (rc > 0) { + space->context_tbl[i] = 0; + continue; + } } return 0; out_err: - tpm_buf_destroy(&buf); tpm2_flush_space(chip, space); return rc; } @@ -462,6 +538,7 @@ int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space, memcpy(&space->session_tbl, &chip->work_space.session_tbl, sizeof(space->session_tbl)); memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE); + memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE); return 0; } diff --git a/drivers/char/tpm/tpms-dev.c b/drivers/char/tpm/tpms-dev.c index d6e3491..12b6e34 100644 --- a/drivers/char/tpm/tpms-dev.c +++ b/drivers/char/tpm/tpms-dev.c @@ -25,6 +25,12 @@ static int tpms_open(struct inode *inode, struct file *file) kfree(priv); return -ENOMEM; } + priv->space.session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL); + if (priv->space.session_buf == NULL) { + kfree(priv->space.context_buf); + kfree(priv); + return -ENOMEM; + } tpm_common_open(file, chip, &priv->priv); @@ -39,6 +45,7 @@ static int tpms_release(struct inode *inode, struct file *file) tpm2_flush_space(fpriv->chip, &priv->space); tpm_common_release(file, fpriv); kfree(priv->space.context_buf); + kfree(priv->space.session_buf); kfree(priv); return 0;