Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1585336rda; Mon, 23 Oct 2023 18:16:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGm1dhPfHfE3punva+N35Y1AXyKxDj8F4FDpRJ5UaDlLWbSyW6yijvoKENniGnngBpYJPEl X-Received: by 2002:a05:6a21:3b44:b0:169:7d6f:9f22 with SMTP id zy4-20020a056a213b4400b001697d6f9f22mr1169598pzb.54.1698110198729; Mon, 23 Oct 2023 18:16:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698110198; cv=none; d=google.com; s=arc-20160816; b=y3TZjnWL13zO2qpgQmEzJH/giWLHfNXmvgluErv7t4NyGSvpC0tMiB84eLuY1z0u+7 qbQvVK/03ijObdiVgEZlYK38kd32cX8Ta6Ftjkdy4Rqtcj52d6UGb+ufUF2g8N+ImXiD eUC+o8Ds87WuFbeRvCLtOmbQyShqBDSGAWC3Q2fB0AXW/6mlfWoGnbk3V+iEyQIUYN44 LzQavPcFQZuDrwwIaNLn2XdB3C/9jM9gfn/cvaD7CqK8fXPvq+lmetzXXnAH29K6I53P xoYNl8Nwcw974ywM2oXx4XMg2R6kBEptR3E1xsiOA9bR+saxgN/HDuxUV2vN0/M+o2d/ DA0Q== 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=d3RoCvn2UymZvD8mJvp/ET0ccqPd9t0GLvOiHkWWEVU=; fh=dax70/MHhNLgWKSrez5dkHGwrBpSItgDRe/kfg4J95c=; b=yVYSkbuANSfBb5Vtq7ScTh+24y5d28WQuW+ajHTzC9QFprJQLGzKCdkdY5Y0fEwHXu HFz3jay0iK/wsCHWpZKzg3UIkhH5PEhmPUmtSDq81L6n/S/ChTeZz9hMvtx/GgSs6xoX KZTVYNOeOiUbVUnu4tx+wXhXqbNA8WK6o6Qmzc4ypxnfTUM1MHy25b+RM5P8eMfdM+kI D4aZSN4byVmc1200AZuigZ0FJlHUr5FtSkY6AuGmCaGKSQT8DYSmeW7hCpCrBjZQC/EY NpI++fMEZD9G2IuJ1VfYLhuVHdLMvZMPYX8KX0A7r56WZOE6+j4dhSXGWeLpoVIgOkmo yxAA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LYhYVL+0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id q22-20020a056a00085600b0068a3c575900si7672726pfk.84.2023.10.23.18.16.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 18:16:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LYhYVL+0; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id EED0280A5677; Mon, 23 Oct 2023 18:16:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231817AbjJXBQK (ORCPT + 99 others); Mon, 23 Oct 2023 21:16:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231733AbjJXBQF (ORCPT ); Mon, 23 Oct 2023 21:16:05 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8B51910E2; Mon, 23 Oct 2023 18:16:01 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9F1A0C433CB; Tue, 24 Oct 2023 01:16:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698110161; bh=YE+I+eKgeikJsJEwkiUq2IajZIXy3oaayu4RbB8GrBI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LYhYVL+0E8qrtgxJNQ+FdnP2rnBzAAAPVvpweLOCOn/n/kVwgqLqAWL8DP6Apg2sg BFOcedQey88NWa0j8d7NvgOT3ossPhM8VT8obXhLBpPU08Uw/xOshcbNP12BMd5woP t4Rl+wKv03D3HSWi7qczDvKVvht+wXrDvZZFPnLmsiBhwdI3lL91yJDGF9LxOLUCVc s0mozQr2VDmyPt/mbUh1KQkxNtRYvlbBZhVB8HNUMpngnQSgzZAgEgMI5CGmBOxYkD LtnX0QQWT5nnD8kFMHiyuIATBbese5mxYUdXToTsvZYp4vYHixQiY+otL2JsMJcXqC QEVbCBbHiLe/A== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: keyrings@vger.kernel.org, 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" , Jerry Snitselaar , Mario Limonciello , Julien Gomes , linux-kernel@vger.kernel.org (open list), linux-security-module@vger.kernel.org (open list:SECURITY SUBSYSTEM) Subject: [PATCH v3 4/6] tpm: Support TPM2 sized buffers (TPM2B) Date: Tue, 24 Oct 2023 04:15:22 +0300 Message-ID: <20231024011531.442587-5-jarkko@kernel.org> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231024011531.442587-1-jarkko@kernel.org> References: <20231024011531.442587-1-jarkko@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Mon, 23 Oct 2023 18:16:35 -0700 (PDT) Add boolean parameters @alloc and @sized to tpm_buf_init(): * If @alloc is set to false, buf->data is assumed to be pre-feeded and owned by the caller. * If @sized is set to true, the buffer represents a sized buffer (TPM2B). Signed-off-by: Jarkko Sakkinen --- drivers/char/tpm/tpm-buf.c | 32 ++++++++++++++++------- drivers/char/tpm/tpm-sysfs.c | 2 +- drivers/char/tpm/tpm1-cmd.c | 14 +++++----- drivers/char/tpm/tpm2-cmd.c | 22 ++++++++-------- drivers/char/tpm/tpm2-space.c | 4 +-- drivers/char/tpm/tpm_vtpm_proxy.c | 2 +- include/linux/tpm.h | 3 ++- security/keys/trusted-keys/trusted_tpm1.c | 4 +-- security/keys/trusted-keys/trusted_tpm2.c | 6 ++--- 9 files changed, 51 insertions(+), 38 deletions(-) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index fa9a4c51157a..f1d92d7e758d 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -7,22 +7,32 @@ #include /** - * tpm_buf_init() - Initialize from the heap + * tpm_buf_init() - Initialize a TPM buffer * @buf: A @tpm_buf + * @sized: Represent a sized buffer (TPM2B) + * @alloc: Allocate from the heap * * 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) +int tpm_buf_init(struct tpm_buf *buf, bool alloc, bool sized) { - buf->data = (u8 *)__get_free_page(GFP_KERNEL); - if (!buf->data) - return -ENOMEM; + if (alloc) { + buf->data = (u8 *)__get_free_page(GFP_KERNEL); + if (!buf->data) + return -ENOMEM; + } + + if (sized) { + buf->flags = TPM_BUF_SIZED; + buf->length = 2; + } else { + buf->flags = 0; + buf->length = TPM_HEADER_SIZE; + } - buf->flags = 0; - buf->length = TPM_HEADER_SIZE; memset(buf->data, 0, TPM_HEADER_SIZE); return 0; @@ -73,8 +83,6 @@ EXPORT_SYMBOL_GPL(tpm_buf_length); void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length) { - struct tpm_header *head = (struct tpm_header *)buf->data; - /* Return silently if overflow has already happened. */ if (buf->flags & TPM_BUF_OVERFLOW) return; @@ -87,7 +95,11 @@ void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length) memcpy(&buf->data[buf->length], new_data, new_length); buf->length += new_length; - head->length = cpu_to_be32(buf->length); + + if (buf->flags & TPM_BUF_SIZED) + ((__be16 *)buf->data)[0] = cpu_to_be16(buf->length - 2); + else + ((struct tpm_header *)buf->data)->length = cpu_to_be32(buf->length); } EXPORT_SYMBOL_GPL(tpm_buf_append); diff --git a/drivers/char/tpm/tpm-sysfs.c b/drivers/char/tpm/tpm-sysfs.c index 557e7f86d98d..047a7064039e 100644 --- a/drivers/char/tpm/tpm-sysfs.c +++ b/drivers/char/tpm/tpm-sysfs.c @@ -44,7 +44,7 @@ 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)) + if (tpm_buf_init(&tpm_buf, true, false)) goto out_ops; tpm_buf_reset(&tpm_buf, TPM_TAG_RQU_COMMAND, TPM_ORD_READPUBEK); diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c index 28fea4e2daaf..396694018590 100644 --- a/drivers/char/tpm/tpm1-cmd.c +++ b/drivers/char/tpm/tpm1-cmd.c @@ -328,7 +328,7 @@ static int tpm1_startup(struct tpm_chip *chip) dev_info(&chip->dev, "starting up the TPM manually\n"); - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc < 0) return rc; @@ -467,7 +467,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -487,7 +487,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -541,7 +541,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max) u32 recd; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -592,7 +592,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -629,7 +629,7 @@ static int tpm1_continue_selftest(struct tpm_chip *chip) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -761,7 +761,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c index 94dacbf74e0d..1d3e0833641d 100644 --- a/drivers/char/tpm/tpm2-cmd.c +++ b/drivers/char/tpm/tpm2-cmd.c @@ -183,7 +183,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -242,7 +242,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, int rc; int i; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -302,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); + err = tpm_buf_init(&buf, true, false); if (err) return err; @@ -353,7 +353,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) { dev_warn(&chip->dev, "0x%08x was not flushed, out of memory\n", handle); @@ -394,7 +394,7 @@ 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); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -437,7 +437,7 @@ void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return; @@ -467,7 +467,7 @@ static int tpm2_do_selftest(struct tpm_chip *chip) int rc; for (full = 0; full < 2; full++) { - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -504,7 +504,7 @@ int tpm2_probe(struct tpm_chip *chip) struct tpm_buf buf; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -571,7 +571,7 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) int rc; int i = 0; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -661,7 +661,7 @@ int tpm2_get_cc_attrs_tbl(struct tpm_chip *chip) goto out; } - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) goto out; @@ -724,7 +724,7 @@ static int tpm2_startup(struct tpm_chip *chip) dev_info(&chip->dev, "starting up the TPM manually\n"); - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc < 0) return rc; diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c index 43584b4176d6..ab2a863baffb 100644 --- a/drivers/char/tpm/tpm2-space.c +++ b/drivers/char/tpm/tpm2-space.c @@ -76,7 +76,7 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf, unsigned int body_size; int rc; - rc = tpm_buf_init(&tbuf); + rc = tpm_buf_init(&tbuf, true, false); if (rc) return rc; @@ -128,7 +128,7 @@ static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf, unsigned int body_size; int rc; - rc = tpm_buf_init(&tbuf); + rc = tpm_buf_init(&tbuf, true, false); if (rc) return rc; diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c index b2b9a15a4a59..52325abd395d 100644 --- a/drivers/char/tpm/tpm_vtpm_proxy.c +++ b/drivers/char/tpm/tpm_vtpm_proxy.c @@ -399,7 +399,7 @@ 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); - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; diff --git a/include/linux/tpm.h b/include/linux/tpm.h index c355597351c6..687b5173bdab 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -304,6 +304,7 @@ struct tpm_header { enum tpm_buf_flags { TPM_BUF_OVERFLOW = BIT(0), + TPM_BUF_SIZED = BIT(1), }; struct tpm_buf { @@ -328,7 +329,7 @@ struct tpm2_hash { }; -int tpm_buf_init(struct tpm_buf *buf); +int tpm_buf_init(struct tpm_buf *buf, bool alloc, bool sized); 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 fcf0eef79ba0..ed5dc3b45d52 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); + ret = tpm_buf_init(&tb, true, false); 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); + ret = tpm_buf_init(&tb, true, false); if (ret) return ret; diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index c54659d06dcb..c41f30770138 100644 --- a/security/keys/trusted-keys/trusted_tpm2.c +++ b/security/keys/trusted-keys/trusted_tpm2.c @@ -252,7 +252,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, if (rc) return rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) { tpm_put_ops(chip); return rc; @@ -410,7 +410,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip, if (blob_len > payload->blob_len) return -E2BIG; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; @@ -467,7 +467,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, u8 *data; int rc; - rc = tpm_buf_init(&buf); + rc = tpm_buf_init(&buf, true, false); if (rc) return rc; -- 2.42.0