From patchwork Tue Aug 22 17:52:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 136578 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3791956vqi; Tue, 22 Aug 2023 10:53:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFlOm3FwZIWJp7RpOz4qKfDRYHDlb1dM2wPWsSPK+swfGoYh2r4Wzkw5Qy3Jp8LBwBcIxC1 X-Received: by 2002:a17:90a:df08:b0:26d:3e6a:cd93 with SMTP id gp8-20020a17090adf0800b0026d3e6acd93mr14426162pjb.17.1692726800881; Tue, 22 Aug 2023 10:53:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692726800; cv=none; d=google.com; s=arc-20160816; b=o3AWgVAXhTZGbgyB3791eBkb9Iq1FCiClF5aPcVpvdDWm5QnJT5A7arbEhNK9PSVe+ QrH01AijqQgn1HypZ43F/0rlfhto4HyV7lxKhmbmAO8wkRAOKQ+/Jf1f5jkmkrOZdmD+ aRN03ZoJjbgLbOQZhUdCXUE5DSFMhK9ZkGk8BHkVpfXhQpl9URI17TyzsD1rBB6BPUuw VUEMlBC+y8H/j0kGeuppFdtJR2aGgxKs6EodVyd1yvT3HP7TojAvO2rCPacs8XsbcqM2 dnpY8cL1nVNW+FH+agwF1Q2Q/AztZtPtmErgoWI5AmZ3VRB3L21y16B0ZAz+UYFvExiD eVVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1CzBoB9ZWRANJCdGMmzUNkn1qwLF0WqPCYIo+j9RABE=; fh=u60wR0im9IsWjq8XrMMOI7O/eDFZ8LJpmr5kemyPVlw=; b=aKl2MtAMvR6QJX+NpzNS5ddtGoy19sz5+YwVX2/tVwnsnLkYZRdVNzstLXaksQNFVu VIXgjbARgQnAC/x9gq7eMe8cryViUu5q7VPrF6nMIzEkPRsYnxElyZU/Mn5hIFvV2hL3 CjAUOuv5eYQ9XgiTVOn4D7PkwFVZpngkAmcjaFtbMuypwnGof7fKJwGR4OFBpsGKW+mf AyhFESrOQqrLnjUL7v0vq5Np6rlvxBnCeG4JpwCO82+7VU2e4WjuZ9LbHP8igRfz3cIN wZp7heIWaIKPN+axrYvWIj4FABlqc/urRd+za3XFEdAcotNVjgEqWSsSQ2hDAPiFTmAr MLMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QkYPwwWU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b0056c2892bfb9si1902215pgn.644.2023.08.22.10.53.04; Tue, 22 Aug 2023 10:53:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QkYPwwWU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229620AbjHVRws (ORCPT + 99 others); Tue, 22 Aug 2023 13:52:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229609AbjHVRwr (ORCPT ); Tue, 22 Aug 2023 13:52:47 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71CA41728; Tue, 22 Aug 2023 10:52:43 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C170E65CF6; Tue, 22 Aug 2023 17:52:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE768C433C8; Tue, 22 Aug 2023 17:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692726762; bh=zuJ2/94LtenthFJwiFNqhl0mmdGYejJg0F7VpNtM9aM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=QkYPwwWUdAeORnBMrH84RlLWspJhyFtvr0SVtcyv4FkuLI1eFMUQrEWnpmlxkYs67 A3VvIOKNWkWhBwG9b2j8CBpiIlpQlrZYNWDgjUq+RyNHZlfcU1xPWS/FTR0AUCPbHt k6vA3LkTIxTOecXQt7ELHJtQNgzPNMjscbRZIt5/hsWlD5KHPhVmdGQhSVzjw/irci ijyo5tvBHCKEqKw/e1BM187jrzhxGiQru0cNzv8eX6iInRLsC0Ciht1VZUYr2pIa7T 2gcmdX5OS1yFbmp6OCDj82qMcI2mqYiHSKxu1oIc7X44Xg/k8Nny37XOCyuZXOFx1y CSmXbWr36N7bQ== From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: Jarkko Sakkinen , James Bottomley , William Roberts , Stefan Berger , David Howells , Jason Gunthorpe , Mimi Zohar , Peter Huewe , James Bottomley , Paul Moore , James Morris , "Serge E. Hallyn" , Julien Gomes , Mario Limonciello , Jerry Snitselaar , linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v2 3/6] tpm: Detach tpm_buf_reset() from tpm_buf_init() Date: Tue, 22 Aug 2023 20:52:18 +0300 Message-Id: <20230822175221.2196136-4-jarkko@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230822175221.2196136-1-jarkko@kernel.org> References: <20230822175221.2196136-1-jarkko@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774952697781887893 X-GMAIL-MSGID: 1774952697781887893 In order to use tpm_buf_init() for other buffer types, detach tpm_buf_reset() from tpm_buf_init(). Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-buf.c | 15 ++++++++-- drivers/char/tpm/tpm-sysfs.c | 3 +- drivers/char/tpm/tpm1-cmd.c | 26 +++++++++++----- drivers/char/tpm/tpm2-cmd.c | 36 ++++++++++++++++------- drivers/char/tpm/tpm2-space.c | 7 +++-- drivers/char/tpm/tpm_vtpm_proxy.c | 13 ++++---- include/linux/tpm.h | 2 +- security/keys/trusted-keys/trusted_tpm1.c | 4 +-- security/keys/trusted-keys/trusted_tpm2.c | 9 ++++-- 9 files changed, 80 insertions(+), 35 deletions(-) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index 8dc6b9db006b..fa9a4c51157a 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -6,14 +6,25 @@ #include #include -int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) +/** + * tpm_buf_init() - Initialize from the heap + * @buf: A @tpm_buf + * + * Initialize all structure fields to zero, allocate a page from the heap, and + * zero the bytes that the buffer headers will consume. + * + * Return: 0 or -ENOMEM + */ +int tpm_buf_init(struct tpm_buf *buf) { buf->data = (u8 *)__get_free_page(GFP_KERNEL); if (!buf->data) return -ENOMEM; buf->flags = 0; - tpm_buf_reset(buf, tag, ordinal); + buf->length = TPM_HEADER_SIZE; + memset(buf->data, 0, TPM_HEADER_SIZE); + return 0; } EXPORT_SYMBOL_GPL(tpm_buf_init); diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c index 54c71473aa29..557e7f86d98d 100644 --- a/drivers/char/tpm/tpm-sysfs.c +++ b/drivers/char/tpm/tpm-sysfs.c @@ -44,9 +44,10 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr, if (tpm_try_get_ops(chip)) return 0; - if (tpm_buf_init(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK)) + if (tpm_buf_init(&tpm_buf)) goto out_ops; + tpm_buf_reset(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK); tpm_buf_append(&tpm_buf, anti_replay, sizeof(anti_replay)); if (tpm_transmit_cmd(chip, &tpm_buf, READ_PUBEK_RESULT_MIN_BODY_SIZE, diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c index cf64c7385105..28fea4e2daaf 100644 --- a/drivers/char/tpm/tpm1-cmd.c +++ b/drivers/char/tpm/tpm1-cmd.c @@ -328,10 +328,11 @@ static int tpm1_startup(struct tpm_chip *chip) dev_info(&chip->dev, "starting up the TPM manually\n"); - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP); + rc = tpm_buf_init(&buf); if (rc < 0) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_STARTUP); tpm_buf_append_u16(&buf, TPM_ST_CLEAR); rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM"); @@ -466,10 +467,11 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash, struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCR_EXTEND); tpm_buf_append_u32(&buf, pcr_idx); tpm_buf_append(&buf, hash, TPM_DIGEST_SIZE); @@ -485,10 +487,12 @@ ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_CAP); + if (subcap_id == TPM_CAP_VERSION_1_1 || subcap_id == TPM_CAP_VERSION_1_2) { tpm_buf_append_u32(&buf, subcap_id); @@ -537,10 +541,12 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) u32 recd; int rc; - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM); + do { tpm_buf_append_u32(&buf, num_bytes); @@ -586,10 +592,11 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_PCRREAD); tpm_buf_append_u32(&buf, pcr_idx); rc = tpm_transmit_cmd(chip, &buf, TPM_DIGEST_SIZE, @@ -622,10 +629,12 @@ static int tpm1_continue_selftest(struct tpm_chip *chip) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_CONTINUE_SELFTEST); + rc = tpm_transmit_cmd(chip, &buf, 0, "continue selftest"); tpm_buf_destroy(&buf); return rc; @@ -752,9 +761,12 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr) rc = tpm1_pcr_extend(chip, tpm_suspend_pcr, dummy_hash, "extending dummy pcr before suspend"); - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE); + rc = tpm_buf_init(&buf); if (rc) return rc; + + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SAVESTATE); + /* now do the actual savestate */ for (try = 0; try < TPM_RETRY; try++) { rc = tpm_transmit_cmd(chip, &buf, 0, NULL); diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 93545be190a5..94dacbf74e0d 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -183,10 +183,12 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, expected_digest_size = chip->allocated_banks[i].digest_size; } - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_PCR_READ); + pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7); tpm_buf_append_u32(&buf, 1); @@ -240,10 +242,11 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, int rc; int i; - rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_PCR_EXTEND); tpm_buf_append_u32(&buf, pcr_idx); auth_area.handle = cpu_to_be32(TPM2_RS_PW); @@ -299,7 +302,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) if (!num_bytes || max > TPM_MAX_RNG_DATA) return -EINVAL; - err = tpm_buf_init(&buf, 0, 0); + err = tpm_buf_init(&buf); if (err) return err; @@ -350,13 +353,14 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); + rc = tpm_buf_init(&buf); if (rc) { dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n", handle); return; } + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_FLUSH_CONTEXT); tpm_buf_append_u32(&buf, handle); tpm_transmit_cmd(chip, &buf, 0, "flushing context"); @@ -390,9 +394,11 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value, struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); + rc = tpm_buf_init(&buf); if (rc) return rc; + + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES); tpm_buf_append_u32(&buf, property_id); tpm_buf_append_u32(&buf, 1); @@ -431,9 +437,11 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN); + rc = tpm_buf_init(&buf); if (rc) return; + + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SHUTDOWN); tpm_buf_append_u16(&buf, shutdown_type); tpm_transmit_cmd(chip, &buf, 0, "stopping the TPM"); tpm_buf_destroy(&buf); @@ -459,10 +467,11 @@ static int tpm2_do_selftest(struct tpm_chip *chip) int rc; for (full = 0; full < 2; full++) { - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_SELF_TEST); tpm_buf_append_u8(&buf, full); rc = tpm_transmit_cmd(chip, &buf, 0, "attempting the self test"); @@ -495,9 +504,11 @@ int tpm2_probe(struct tpm_chip *chip) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); + rc = tpm_buf_init(&buf); if (rc) return rc; + + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); tpm_buf_append_u32(&buf, TPM2_CAP_TPM_PROPERTIES); tpm_buf_append_u32(&buf, TPM_PT_TOTAL_COMMANDS); tpm_buf_append_u32(&buf, 1); @@ -560,10 +571,11 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) int rc; int i = 0; - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); tpm_buf_append_u32(&buf, TPM2_CAP_PCRS); tpm_buf_append_u32(&buf, 0); tpm_buf_append_u32(&buf, 1); @@ -649,10 +661,11 @@ int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip) goto out; } - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); + rc = tpm_buf_init(&buf); if (rc) goto out; + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY); tpm_buf_append_u32(&buf, TPM2_CAP_COMMANDS); tpm_buf_append_u32(&buf, TPM2_CC_FIRST); tpm_buf_append_u32(&buf, nr_commands); @@ -711,10 +724,11 @@ static int tpm2_startup(struct tpm_chip *chip) dev_info(&chip->dev, "starting up the TPM manually\n"); - rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP); + rc = tpm_buf_init(&buf); if (rc < 0) return rc; + tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_STARTUP); tpm_buf_append_u16(&buf, TPM2_SU_CLEAR); rc = tpm_transmit_cmd(chip, &buf, 0, "attempting to start the TPM"); tpm_buf_destroy(&buf); diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c index ffb35f0154c1..4538178e398c 100644 --- a/drivers/char/tpm/tpm2-space.c +++ b/drivers/char/tpm/tpm2-space.c @@ -76,10 +76,12 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf, unsigned int body_size; int rc; - rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_LOAD); + rc = tpm_buf_init(&tbuf); if (rc) return rc; + tpm_buf_reset(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_LOAD); + ctx = (struct tpm2_context *)&buf[*offset]; body_size = sizeof(*ctx) + be16_to_cpu(ctx->blob_size); tpm_buf_append(&tbuf, &buf[*offset], body_size); @@ -126,10 +128,11 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf, unsigned int body_size; int rc; - rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_SAVE); + rc = tpm_buf_init(&tbuf); if (rc) return rc; + tpm_buf_reset(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_SAVE); tpm_buf_append_u32(&tbuf, handle); rc = tpm_transmit_cmd(chip, &tbuf, 0, NULL); diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c index 30e953988cab..b2b9a15a4a59 100644 --- a/drivers/char/tpm/tpm_vtpm_proxy.c +++ b/drivers/char/tpm/tpm_vtpm_proxy.c @@ -399,14 +399,15 @@ static int vtpm_proxy_request_locality(struct tpm_chip *chip, int locality) const struct tpm_header *header; struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev); - if (chip->flags & TPM_CHIP_FLAG_TPM2) - rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, - TPM2_CC_SET_LOCALITY); - else - rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, - TPM_ORD_SET_LOCALITY); + rc = tpm_buf_init(&buf); if (rc) return rc; + + if (chip->flags & TPM_CHIP_FLAG_TPM2) + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_SET_LOCALITY); + else + tpm_buf_reset(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_SET_LOCALITY); + tpm_buf_append_u8(&buf, locality); proxy_dev->state |= STATE_DRIVER_COMMAND; diff --git a/include/linux/tpm.h b/include/linux/tpm.h index 3cfe2aeb1d9a..c355597351c6 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -328,7 +328,7 @@ struct tpm2_hash { }; -int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal); +int tpm_buf_init(struct tpm_buf *buf); void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal); void tpm_buf_destroy(struct tpm_buf *buf); u32 tpm_buf_length(struct tpm_buf *buf); diff --git a/security/keys/trusted-keys/trusted_tpm1.c b/security/keys/trusted-keys/trusted_tpm1.c index 7c1515014a5d..fcf0eef79ba0 100644 --- a/security/keys/trusted-keys/trusted_tpm1.c +++ b/security/keys/trusted-keys/trusted_tpm1.c @@ -664,7 +664,7 @@ static int key_seal(struct trusted_key_payload *p, struct tpm_buf tb; int ret; - ret = tpm_buf_init(&tb, 0, 0); + ret = tpm_buf_init(&tb); if (ret) return ret; @@ -690,7 +690,7 @@ static int key_unseal(struct trusted_key_payload *p, struct tpm_buf tb; int ret; - ret = tpm_buf_init(&tb, 0, 0); + ret = tpm_buf_init(&tb); if (ret) return ret; diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index bc700f85f80b..c54659d06dcb 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -252,12 +252,13 @@ int tpm2_seal_trusted(struct tpm_chip *chip, if (rc) return rc; - rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); + rc = tpm_buf_init(&buf); if (rc) { tpm_put_ops(chip); return rc; } + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, NULL /* nonce */, 0, @@ -409,10 +410,11 @@ static int tpm2_load_cmd(struct tpm_chip *chip, if (blob_len > payload->blob_len) return -E2BIG; - rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_LOAD); tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, NULL /* nonce */, 0, @@ -465,10 +467,11 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, u8 *data; int rc; - rc = tpm_buf_init(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); + rc = tpm_buf_init(&buf); if (rc) return rc; + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_UNSEAL); tpm_buf_append_u32(&buf, blob_handle); tpm2_buf_append_auth(&buf, options->policyhandle ? From patchwork Tue Aug 22 17:52:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 136580 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b82d:0:b0:3f2:4152:657d with SMTP id z13csp3793723vqi; Tue, 22 Aug 2023 10:57:00 -0700 (PDT) X-Google-Smtp-Source: AGHT+IErfOUPIhPFaTdY09aHdlv7qY6AbVP7sTTegG6H5F/C4S0uCVJzJ5JNHlb+ZiT9Ey9M65WN X-Received: by 2002:a17:90a:6c24:b0:26b:660e:852c with SMTP id x33-20020a17090a6c2400b0026b660e852cmr7494879pjj.20.1692727020192; Tue, 22 Aug 2023 10:57:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692727020; cv=none; d=google.com; s=arc-20160816; b=TDHarmfdtSPipElp6sXMpUJk1x4sVbMq8vDQtPIJBQzpD9blFeL3reyFDxOHGxRHNm /0XOk8qSTp97qBbCo4x30IQI3AukS6C+WaojdOlNxaJDpzKJLT7nEmFRlFA50XhDaplQ 8+A/ofK0oifWqGt39surNdcNnD8wbOevDYzN6Nv4XX3p535yZV/NPMGfxFISvBPcW/AY dGa/n4YqYyDDcdX1ajHvf7gQxib2w+deztbjAJspS2v8F9UcTW9dY64CRX9FzdJ2H7QJ n+cQIZ02PeVj3z1iyWUEvsBvRO+ojhB4N/tOWickO+xqyyU7NGzRmVMRq779pYZBL2t6 93jg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=K5HO467PLl1MBu9i9sULKZnLyg8H0bQmAMv7H3gzJoM=; fh=comGAmSqx0f6qXrQWJN2HsHS/ERDdK2XbPQxT0TORfs=; b=G34QgLTJwc6mTdPTXCQkn3bskEZmqMvNMXEhZgCK73wq/OPX5U3HeejWrv7FUhDOHF sHr7YOgyA5VrX/zRgJvxC41vw4922SScGfyU3uFCnldyDKPrs0tcsRZId0lxuv8RbcjX e7rJ4kLGs38oa4S/XJEYUyGQKp1nEjyPvBpI3LlB2P1a4A+2FWLIh4WMXBisNkjsmxBI tZv6hq42YjdNnwrnDjvNIhpKrv44cdVfy/r94nQJTXTyg1YPxyNYc1yqCnrCgh8VtfrG RBl8J3PDHDHfyuScHENk+yFZLVRDSqgwIX1ijRuKDjGXoDD7yHEUp5BzouhgZg8USKlI chpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V1boDcjE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nm20-20020a17090b19d400b0026f3e1e9639si5941498pjb.40.2023.08.22.10.56.24; Tue, 22 Aug 2023 10:57:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=V1boDcjE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229630AbjHVRxP (ORCPT + 99 others); Tue, 22 Aug 2023 13:53:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbjHVRxO (ORCPT ); Tue, 22 Aug 2023 13:53:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AB5C330C4; Tue, 22 Aug 2023 10:52:55 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 404B56564C; Tue, 22 Aug 2023 17:52:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21924C433C8; Tue, 22 Aug 2023 17:52:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1692726774; bh=7ZBQ60QAjHKVFVd0j7bD6mifIJEl/fZHiRDkdIVlxpc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=V1boDcjEUEjowJ74wIqHCDivWYXpyGjFaDDuAS9KS90+lQjBnBz51TP6w5Ic6x43H Zz20oIizH/yH2WSkoXhh+vUmhufn+/aPX3wY3Zj9KKrOTGW+zaHMgqjp+oMUW7EspJ 5Ji01WRp74jsgv+RUr3hBbdJdGzR5wpY2UHcb7UVwhm6cZVtN18gFeOJ+9VvzluVWy TCX7ssofokW2yaUtoo3uQls99pOxJRAj3dZThYhCWM1whyqbQeilV9mvzByhZU5D9t zQ7BVuwAg5+JbtMGdlpwfOvgXPn+jjskPrzt60OdUKfVmOu+5g5h77A+8XR79ME0Lm vfbeaVeEzcKkQ== From: Jarkko Sakkinen To: linux-sgx@vger.kernel.org Cc: Jarkko Sakkinen , James Bottomley , William Roberts , Stefan Berger , David Howells , Jason Gunthorpe , Mimi Zohar , James Bottomley , Paul Moore , James Morris , "Serge E. Hallyn" , linux-integrity@vger.kernel.org, keyrings@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 6/6] KEYS: trusted: tpm2: Use struct tpm_buf for sized buffers Date: Tue, 22 Aug 2023 20:52:21 +0300 Message-Id: <20230822175221.2196136-7-jarkko@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230822175221.2196136-1-jarkko@kernel.org> References: <20230822175221.2196136-1-jarkko@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1774952927813192697 X-GMAIL-MSGID: 1774952927813192697 Take advantage of the new sized buffer (TPM2B) mode of struct tpm_buf in tpm2_seal_trusted(). This allows to add robustness to the command construction without requiring to calculate buffer sizes manually. Signed-off-by: Jarkko Sakkinen v2: * Use tpm_buf_read_* --- security/keys/trusted-keys/trusted_tpm2.c | 51 +++++++++++++---------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index c41f30770138..5d262306184c 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -228,8 +228,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip, struct trusted_key_payload *payload, struct trusted_key_options *options) { + off_t offset = TPM_HEADER_SIZE; + struct tpm_buf buf, sized; int blob_len = 0; - struct tpm_buf buf; u32 hash; u32 flags; int i; @@ -258,6 +259,13 @@ int tpm2_seal_trusted(struct tpm_chip *chip, return rc; } + rc = tpm_buf_init(&sized, true, true); + if (rc) { + tpm_buf_destroy(&buf); + tpm_put_ops(chip); + return rc; + } + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE); tpm_buf_append_u32(&buf, options->keyhandle); tpm2_buf_append_auth(&buf, TPM2_RS_PW, @@ -267,36 +275,36 @@ int tpm2_seal_trusted(struct tpm_chip *chip, TPM_DIGEST_SIZE); /* sensitive */ - tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len); + tpm_buf_append_u16(&sized, options->blobauth_len); - tpm_buf_append_u16(&buf, options->blobauth_len); if (options->blobauth_len) - tpm_buf_append(&buf, options->blobauth, options->blobauth_len); + tpm_buf_append(&sized, options->blobauth, options->blobauth_len); - tpm_buf_append_u16(&buf, payload->key_len); - tpm_buf_append(&buf, payload->key, payload->key_len); + tpm_buf_append_u16(&sized, payload->key_len); + tpm_buf_append(&sized, payload->key, payload->key_len); + tpm_buf_append(&buf, sized.data, sized.length); /* public */ - tpm_buf_append_u16(&buf, 14 + options->policydigest_len); - tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH); - tpm_buf_append_u16(&buf, hash); + tpm_buf_init(&sized, false, true); + tpm_buf_append_u16(&sized, TPM_ALG_KEYEDHASH); + tpm_buf_append_u16(&sized, hash); /* key properties */ flags = 0; flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH; - flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | - TPM2_OA_FIXED_PARENT); - tpm_buf_append_u32(&buf, flags); + flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT); + tpm_buf_append_u32(&sized, flags); /* policy */ - tpm_buf_append_u16(&buf, options->policydigest_len); + tpm_buf_append_u16(&sized, options->policydigest_len); if (options->policydigest_len) - tpm_buf_append(&buf, options->policydigest, - options->policydigest_len); + tpm_buf_append(&sized, options->policydigest, options->policydigest_len); /* public parameters */ - tpm_buf_append_u16(&buf, TPM_ALG_NULL); - tpm_buf_append_u16(&buf, 0); + tpm_buf_append_u16(&sized, TPM_ALG_NULL); + tpm_buf_append_u16(&sized, 0); + + tpm_buf_append(&buf, sized.data, sized.length); /* outside info */ tpm_buf_append_u16(&buf, 0); @@ -313,21 +321,20 @@ int tpm2_seal_trusted(struct tpm_chip *chip, if (rc) goto out; - blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]); + blob_len = tpm_buf_read_u32(&buf, &offset); if (blob_len > MAX_BLOB_SIZE) { rc = -E2BIG; goto out; } - if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) { + if (buf.length - offset < blob_len) { rc = -EFAULT; goto out; } - blob_len = tpm2_key_encode(payload, options, - &buf.data[TPM_HEADER_SIZE + 4], - blob_len); + blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len); out: + tpm_buf_destroy(&sized); tpm_buf_destroy(&buf); if (rc > 0) {