From patchwork Fri Nov 24 02:02:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 169152 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce62:0:b0:403:3b70:6f57 with SMTP id o2csp841493vqx; Thu, 23 Nov 2023 18:03:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFgASK88FC/S0bIGMpmwaTIElp7xMnwaue/e3TfI+W1OVt/+dF2pboR6Vq6k9MEBrzHv873 X-Received: by 2002:a17:90b:3e8c:b0:285:866d:b1cf with SMTP id rj12-20020a17090b3e8c00b00285866db1cfmr1086074pjb.6.1700791435188; Thu, 23 Nov 2023 18:03:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700791435; cv=none; d=google.com; s=arc-20160816; b=r4m9fDU1gNpvOZZ/e1aejrjfoMxO5mlZ4VDUKn0hX9wrhJccFQoHVKiz0PCa8rKd17 i9tXlGP3rfZkRe7oRHkLfW3sf61Acjj2NsqClF9uyor08aFomwQqKltWQcfTHHtXfu4n 1jRUdU9ucooGayuj2bjqlG9eO4yAIbm4JwUsqGGehagOwDTbDFWdYgRpgWPoHLcdsZV0 dg68et5qmgxXvDV8uUENx4hUZByaRGVB9/SnHaZzRviu3C1Pw3VQAPz/MWI+SSRAhqer nhvfEGjEVVD3mwQO8NaRKSnqlS+uOnHg2Hc4CpSe8rwixP6FVUrwet6nn4fAyXX3v5P/ xxUg== 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=R2GiS24LCErCAcxnMCwZPip6Z+6acRE3hJNNFCOHzdU=; fh=RBX0EO7BCt4n4lnvPBYFN24o6jFly+IlKRm41n8yo0s=; b=NQ+REW6o+RmNw3OLq25gPuPopuTCui3KDcbzF6eWif6EX8VNu+Yco0tvrafTWaukp/ N795/j5zrXXQCDQP4Nhu0M/QwBWQwKQlhg/yTQ8oSrNHfcMXzu53uQBULgJau1CYaRX4 Pmvuzz7Owh36t1LBX1d5VfGsbRqZADFaCYRAaqjIq16OfSLGgU2d+ALeUBfpx0KJP6jv 5oDwAsKoWLwdxrv8XdIKnzjNv3/b6D9ykwvbLH0kaAqXbBL8g6Se1Daej7Po4hPmEM+/ Ke+oD5g57ZwDOG+FUeMmU7nVzooQp9KXkxN8X99Bt/8RaivOOQMifrhc/dp+VXrPSKmA IhsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CL2VJivG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id m21-20020a17090b069500b00280386ec042si2858217pjz.149.2023.11.23.18.03.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 18:03:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=CL2VJivG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (Postfix) with ESMTP id 3D9618366256; Thu, 23 Nov 2023 18:03:52 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233056AbjKXCDm (ORCPT + 99 others); Thu, 23 Nov 2023 21:03:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229711AbjKXCDP (ORCPT ); Thu, 23 Nov 2023 21:03:15 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97E761BCB for ; Thu, 23 Nov 2023 18:03:06 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A1E88C433C7; Fri, 24 Nov 2023 02:03:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700791386; bh=gVS4iV57gyVIucD1cw7+E+uIf+FtO/Y9r3lFNTxhJg4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CL2VJivGYgezuXCNa+2mvkNx9oMiHZzoaHR7/jk0ojup/NFowjNT5EfWXJAZn5Pxc wpatcIsU5cMZgBf+v1zA0Yx/WN3MJaDqBmWJRGNBdmWMtEdPBf91Hiq70gHtGUzhOr 6Zpi8s/wmGfitAJ7gVWZAOdlRuOsBxFHZgShlxTMHX1zOBvl0vvY8UG+oqqtcukuPN 1cF4yhzGoLItMGTCuI5jX7sNYqUIg22C/8ju1UQ3nmYoCUvrLSWT0U52VbI9rH3HKx 5je2AfTK11DsL+9mUp8Sthmr9kLwkyjEkicPOXIIVFIZIwHhTbTk4mXdHb2IyJoP7o OuGnAVQJy8SlQ== 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 Subject: [PATCH v6 8/8] KEYS: trusted: tpm2: Use struct tpm_buf for sized buffers Date: Fri, 24 Nov 2023 04:02:37 +0200 Message-ID: <20231124020237.27116-9-jarkko@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20231124020237.27116-1-jarkko@kernel.org> References: <20231124020237.27116-1-jarkko@kernel.org> MIME-Version: 1.0 X-Spam-Status: No, score=-1.3 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,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.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 (groat.vger.email [0.0.0.0]); Thu, 23 Nov 2023 18:03:52 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783409079780828358 X-GMAIL-MSGID: 1783409079780828358 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 Reviewed-by: Stefan Berger --- v3 [2023-11-21]: A boundary error check as response for the feeedback from Mario Limenciello: https://lore.kernel.org/linux-integrity/3f9086f6-935f-48a7-889b-c71398422fa1@amd.com/ v2: Use tpm_buf_read_* --- security/keys/trusted-keys/trusted_tpm2.c | 54 +++++++++++++---------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/security/keys/trusted-keys/trusted_tpm2.c b/security/keys/trusted-keys/trusted_tpm2.c index bc700f85f80b..97b1dfca2dba 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,14 @@ int tpm2_seal_trusted(struct tpm_chip *chip, return rc; } + rc = tpm_buf_init_sized(&sized); + 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, NULL /* nonce */, 0, @@ -266,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_reset_sized(&sized); + 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); @@ -312,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]); - if (blob_len > MAX_BLOB_SIZE) { + blob_len = tpm_buf_read_u32(&buf, &offset); + if (blob_len > MAX_BLOB_SIZE || buf.flags & TPM_BUF_BOUNDARY_ERROR) { 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) {