From patchwork Tue Nov 21 21:17:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 167944 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp920735vqb; Tue, 21 Nov 2023 13:18:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IG1JL65C/DZkwB+EvSqjIWhim7u2OxTYvinhY3hswwqb1fiLgNzGmhqmq1lhCFmhIeXnRFp X-Received: by 2002:a17:90b:3e83:b0:281:61c:1399 with SMTP id rj3-20020a17090b3e8300b00281061c1399mr480368pjb.3.1700601503090; Tue, 21 Nov 2023 13:18:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700601503; cv=none; d=google.com; s=arc-20160816; b=NEI4P3qdL9u4ZGYHR17yCeaO3qCuPq61Pou0NhCsFjKvXa//P1Qi0gPR563Zmu+pXp MkMjJjl9kDbzxULxyxXlrMt9RcA+wjJ/JDPTCchVOy9BvJ52SOQVY/7uKr7Rd0NTwCP9 9bP8j+1WW+QxnCfg1jjGGjKkKl84oAXXCcn3Z43f9TrWWm4NyTbco/Vs9Orwqt2kSmlG WVcMWhvDOJwAONm97eMOHipt7LVAZXkYDd3MRTRdOiRHL73Ef6qywl5w7eoFGbLS2wCw 29NT+Uh0FRxGMoaXiPANxE7kl4jdc76fLUf7rWa1eb5sevRv7ptD03y9B9L2KnbsNss0 iJiA== 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=0ErxfnF694jSGFYuLhZGoGhavy0ByV5DhTAg2rSd+0g=; fh=jG+sFESJQJDE1Wcf9yLzJ2BgzLPnNsnyl0eym9Se5T0=; b=fdOogqoxQhs9DVL3+cKm0t78f7ZCO8zx5WgLRBz5bUktJju3JSm5wR2vcU0flieKpZ R50R/5mXXrFMF1TJPa1ff+J4e8z/oqqOHaHKItxWU/JkWBCgajvhW9u211M9CEv2Do5z YNEAmLIChpNM25FLirUPV42hz0/Fyk8lhzEHu09RRkwSJQ+nMMLNJnnF4DFzL0Tgwi/N MITz+XOPClJIL5DdlDt8dNx0Nm+IOEUBtO+2t920QxCTXgSA64jE6vaWSIgxcd1tObVF BixL0UzhBq8zU4z43MLDcJ0dmZkZB5AyFrphLP5dUGNs1po4KvRncLsa+NmFCWEysLZI x0sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=i3N63W7O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id ei15-20020a17090ae54f00b002800b0fa4f2si12928091pjb.104.2023.11.21.13.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 13:18:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=i3N63W7O; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 81C768127F6A; Tue, 21 Nov 2023 13:18:02 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234572AbjKUVR7 (ORCPT + 99 others); Tue, 21 Nov 2023 16:17:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234578AbjKUVRw (ORCPT ); Tue, 21 Nov 2023 16:17:52 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 950B510E2 for ; Tue, 21 Nov 2023 13:17:43 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A9C15C433C8; Tue, 21 Nov 2023 21:17:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700601463; bh=Gw6t4yKIoJH0jVM58wLhTCrKliCl3oXQG36AlAIlWHE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i3N63W7O+mAyzycndV7HyNu7prGb6dNGDn1fHnPWrHEGNyi10ffUvgBuoZP5OIbil tIr0Bd+r+0S89lcmCNo0LL9PyY5EyjJDPg+iKQHNCvfU6NWtEn0cVO+UMnOsEF441o wkSb43Xk7pWNJUoi6+PE40PH1EHIpmMiZC4kqiYilnISC1xopZ23dA39CBanFkczY9 TIGq0p4yK4UVAydvppuqaS4RvGBNj/4t5v4Ah/OD/23Imtts/iJjrBRmDGYhvWdObV gxWuUH8+JmpLmvA/hfPGEXt1bWBFrVDxkMUiqM5eTZ6lcE+yPcLycDYtp7W5DG9OFG EVoNTpZjI+kfg== From: Jarkko Sakkinen To: linux-integrity@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jarkko Sakkinen , James Bottomley , William Roberts , Stefan Berger , David Howells , Jason Gunthorpe , Mimi Zohar , Mario Limonciello , Jerry Snitselaar , Peter Huewe , James Bottomley , Paul Moore , James Morris , "Serge E. Hallyn" , Julien Gomes Subject: [PATCH v4 6/8] tpm: TPM2B formatted buffers Date: Tue, 21 Nov 2023 23:17:15 +0200 Message-ID: <20231121211717.31681-7-jarkko@kernel.org> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231121211717.31681-1-jarkko@kernel.org> References: <20231121211717.31681-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,T_SCC_BODY_TEXT_LINE 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 21 Nov 2023 13:18:02 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783209921373276483 X-GMAIL-MSGID: 1783209921373276483 Declare tpm_buf_init_sized() and tpm_buf_reset_sized() for creating TPM2B formatted buffers. These buffers are also known as sized buffers in the specifications and literature. Signed-off-by: Jarkko Sakkinen --- v2: [2021-11-21] Refine the API according to the comments for https://lore.kernel.org/linux-integrity/20231024011531.442587-5-jarkko@kernel.org/ --- drivers/char/tpm/tpm-buf.c | 38 +++++++++++++++++++++++++++++++++++--- include/linux/tpm.h | 4 ++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/drivers/char/tpm/tpm-buf.c b/drivers/char/tpm/tpm-buf.c index 545d9c74abf1..12bfade11372 100644 --- a/drivers/char/tpm/tpm-buf.c +++ b/drivers/char/tpm/tpm-buf.c @@ -48,6 +48,36 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) } EXPORT_SYMBOL_GPL(tpm_buf_reset); +/** + * tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer + * @buf: A @tpm_buf + * + * Return: 0 or -ENOMEM + */ +int tpm_buf_init_sized(struct tpm_buf *buf) +{ + buf->data = (u8 *)__get_free_page(GFP_KERNEL); + if (!buf->data) + return -ENOMEM; + + tpm_buf_reset_sized(buf); + return 0; +} +EXPORT_SYMBOL_GPL(tpm_buf_init_sized); + +/** + * tpm_buf_reset_sized() - Initialize a sized buffer + * @buf: A &tpm_buf + */ +void tpm_buf_reset_sized(struct tpm_buf *buf) +{ + buf->flags = TPM_BUF_TPM2B; + buf->length = 2; + buf->data[0] = 0; + buf->data[1] = 0; +} +EXPORT_SYMBOL_GPL(tpm_buf_reset_sized); + void tpm_buf_destroy(struct tpm_buf *buf) { free_page((unsigned long)buf->data); @@ -73,8 +103,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 +115,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_TPM2B) + ((__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/include/linux/tpm.h b/include/linux/tpm.h index 1d7b39b5c383..715db4a91c1f 100644 --- a/include/linux/tpm.h +++ b/include/linux/tpm.h @@ -300,6 +300,8 @@ struct tpm_header { enum tpm_buf_flags { /* the capacity exceeded: */ TPM_BUF_OVERFLOW = BIT(0), + /* TPM2B format: */ + TPM_BUF_TPM2B = BIT(1), }; /* @@ -328,6 +330,8 @@ struct tpm2_hash { int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal); void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal); +int tpm_buf_init_sized(struct tpm_buf *buf); +void tpm_buf_reset_sized(struct tpm_buf *buf); void tpm_buf_destroy(struct tpm_buf *buf); u32 tpm_buf_length(struct tpm_buf *buf); void tpm_buf_append(struct tpm_buf *buf, const u8 *new_data, u16 new_length);