From patchwork Tue Nov 21 21:17:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 167945 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2b07:b0:403:3b70:6f57 with SMTP id io7csp920800vqb; Tue, 21 Nov 2023 13:18:33 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZ2NV5pmPA7xHQsiC4ws08czja43xl9sXGkkTXLqwjL38Yj2GQEtvOGZBWi2MGnLNzv4BA X-Received: by 2002:a05:6a21:a5a4:b0:157:e4c6:766a with SMTP id gd36-20020a056a21a5a400b00157e4c6766amr360778pzc.41.1700601513005; Tue, 21 Nov 2023 13:18:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700601512; cv=none; d=google.com; s=arc-20160816; b=e5LTZniqamAmXDP99D7wOQB6UyijFup1GoU8ZOBbANPUbP4p96tK9Aoq6wRRZHol44 mfx8RcqIfcq+UeavqcNFytPPd7gX9F5yHOrPh5u7pNdRVmfKIQ8PbDK6qxH1GsJGPO62 WXUNRhYjp19qc1GV8z8i2zwrNx/dLLLiJjr1r7hZ79xiQIk4+fWXnYp/03RIpKjENraB ylLnH1607IvOcTUlQ6s5SH9AbP85SZG3OpU3k+2qwdueiJYEENkPkpI3xwk3YD/bLHSx qfEHg11H63rRHlWjNhQr5NmYVukGIvGxHunq8rhA+yQh+ypIO4K3xngcVVwHJg5scB4H lIZg== 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=DO/PlSTXrBgNOuzwEO/xgufeWFic5838YdJnNm0trDg=; fh=jG+sFESJQJDE1Wcf9yLzJ2BgzLPnNsnyl0eym9Se5T0=; b=NEj/+BnNFfNm4JwtVxgyNwHytdCdMTtXTW+AcoS4AIKvhP3lAKHWST3CprQnf6MC0q ny63om6rzdgkwx/7NiLTYtCUwg2+51zwn+Jz2UjTr2a08ZcoFjzlD7waZaUTV6RtyLFR gTzzjXOFoSmqcgZYbX/kcEEeXgam1T7IMOhRLCVVJjGs0o1dhZpYNq9yZI9kcLoAVquG LLnzIT+cuVltT0wV/2mzjWNJjSgCOvR7vHN6uqnHlz1SnFmWrM/XGZQD6NFaSRMq2eGQ MSR1Vd7ySBHYt2oYNJBY3TzjIHWRD0yhbCeYb99rcqEcHCyKfa+jSCrE0YPBsTB67Krj hLOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Pee/r2sx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id y13-20020a634b0d000000b005bdf5961d87si11007972pga.266.2023.11.21.13.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 13:18:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="Pee/r2sx"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 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 668BD8127F7C; Tue, 21 Nov 2023 13:18:28 -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 S229513AbjKUVSK (ORCPT + 99 others); Tue, 21 Nov 2023 16:18:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52940 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234614AbjKUVSD (ORCPT ); Tue, 21 Nov 2023 16:18:03 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99F971702 for ; Tue, 21 Nov 2023 13:17:49 -0800 (PST) Received: by smtp.kernel.org (Postfix) with ESMTPSA id AC98DC433CB; Tue, 21 Nov 2023 21:17:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1700601469; bh=HqrOyp0nDcoraiIniTb/mB74p53iwObmhl1DoJZn8xs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Pee/r2sx6b03sBYSZL3r7fg708z+YSRe7vZq+c+WpQsYu1zpVj+OGeeoI1oTVc3wu E8HhYRqiSsF0xCfNOdlG9Mvp5dkhkOtKoVbWCaZVNiFPpsK1+BV7cSCMI2rQEQz4bx ASconYTyTGQtva+dH8k784pl/p5GU+I2lnMx6tn+/JtLJthBWTSeOlUio1zRi8ZGBN iJvZtwOhG/PCg2STcsu4gm+FyehfVqmIAanodF34EBHYqwIr3mjxpmUBNQPZwU0+7F /IOQu+v9tCvotcrzug8yCSmAWZHeKwhHkaW1tFfkYJkJp3TdjrkA++ShyrXmaThHGi mH4aAzy+nDPjQ== 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 8/8] KEYS: trusted: tpm2: Use struct tpm_buf for sized buffers Date: Tue, 21 Nov 2023 23:17:17 +0200 Message-ID: <20231121211717.31681-9-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:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1783209932565541988 X-GMAIL-MSGID: 1783209932565541988 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 --- 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) {