From patchwork Thu Jan 13 23:54:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 532484 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3BECEC43219 for ; Thu, 13 Jan 2022 23:56:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238311AbiAMX4d (ORCPT ); Thu, 13 Jan 2022 18:56:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238300AbiAMX4d (ORCPT ); Thu, 13 Jan 2022 18:56:33 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD8D6C061574; Thu, 13 Jan 2022 15:56:32 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C027461CF1; Thu, 13 Jan 2022 23:56:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DF986C36AE3; Thu, 13 Jan 2022 23:56:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642118191; bh=Mp6gbaL894zOeNPb9/NhECPfC4xl3NlRjbmF7UoF8Pc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mPG0e1lMdt7gHmBTJttZearTWDJuRYa1IawM1GtNWyuoVQmqmZoOgsc6/+P1e5RxO zXjV+i9zqOAscI/JKVulAAnUoi9Q9qcU+kIPHx6BMwL9sgL8Q+FgORgzHEN4EoEr+3 9NY32XWfjvV2FT11+COIyHdbbsTC6CXNPnGvE8UoZerhUtxoydHxlIpqdMTl8f5U58 3tThZhCce32q0Fvtql3TOKteDFsp1gEktdLR4nd5OKsiYOEVy8IaDMoxPeLYPRx4ey USoPf9QaooiFZGi15ab1XuWnd0Y56+BLSrvRs/9UPsKSItI0oUm52OZie9GIY/N86m cL1Nc7ufANswQ== From: Eric Biggers To: keyrings@vger.kernel.org, David Howells , Jarkko Sakkinen Cc: Denis Kenzior , Marcel Holtmann , James Morris , linux-crypto@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 1/3] KEYS: asym_tpm: fix buffer overreads in extract_key_parameters() Date: Thu, 13 Jan 2022 15:54:38 -0800 Message-Id: <20220113235440.90439-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220113235440.90439-1-ebiggers@kernel.org> References: <20220113235440.90439-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers extract_key_parameters() can read past the end of the input buffer due to buggy and missing bounds checks. Fix it as follows: - Before reading each key length field, verify that there are at least 4 bytes remaining. - Avoid integer overflows when validating size fields; 'sz + 12' and '4 + sz' overflowed if 'sz' is near U32_MAX. - Before saving the pointer to the public key, check that it doesn't run past the end of the buffer. Fixes: f8c54e1ac4b8 ("KEYS: asym_tpm: extract key size & public key [ver #2]") Cc: # v4.20+ Signed-off-by: Eric Biggers --- crypto/asymmetric_keys/asym_tpm.c | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/crypto/asymmetric_keys/asym_tpm.c b/crypto/asymmetric_keys/asym_tpm.c index 0959613560b9..60d20d44c885 100644 --- a/crypto/asymmetric_keys/asym_tpm.c +++ b/crypto/asymmetric_keys/asym_tpm.c @@ -814,7 +814,6 @@ static int extract_key_parameters(struct tpm_key *tk) { const void *cur = tk->blob; uint32_t len = tk->blob_len; - const void *pub_key; uint32_t sz; uint32_t key_len; @@ -845,14 +844,14 @@ static int extract_key_parameters(struct tpm_key *tk) return -EBADMSG; sz = get_unaligned_be32(cur + 8); - if (len < sz + 12) - return -EBADMSG; /* Move to TPM_RSA_KEY_PARMS */ - len -= 12; cur += 12; + len -= 12; /* Grab the RSA key length */ + if (len < 4) + return -EBADMSG; key_len = get_unaligned_be32(cur); switch (key_len) { @@ -866,29 +865,36 @@ static int extract_key_parameters(struct tpm_key *tk) } /* Move just past TPM_KEY_PARMS */ + if (len < sz) + return -EBADMSG; cur += sz; len -= sz; if (len < 4) return -EBADMSG; - sz = get_unaligned_be32(cur); - if (len < 4 + sz) - return -EBADMSG; + cur += 4; + len -= 4; /* Move to TPM_STORE_PUBKEY */ - cur += 4 + sz; - len -= 4 + sz; + if (len < sz) + return -EBADMSG; + cur += sz; + len -= sz; /* Grab the size of the public key, it should jive with the key size */ + if (len < 4) + return -EBADMSG; sz = get_unaligned_be32(cur); + cur += 4; + len -= 4; if (sz > 256) return -EINVAL; - - pub_key = cur + 4; + if (len < sz) + return -EBADMSG; tk->key_len = key_len; - tk->pub_key = pub_key; + tk->pub_key = cur; tk->pub_key_len = sz; return 0; From patchwork Thu Jan 13 23:54:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 531819 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7705C433F5 for ; Thu, 13 Jan 2022 23:56:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238287AbiAMX4d (ORCPT ); Thu, 13 Jan 2022 18:56:33 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:50064 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235807AbiAMX4c (ORCPT ); Thu, 13 Jan 2022 18:56:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 22BD861D0D; Thu, 13 Jan 2022 23:56:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DF96C36AF2; Thu, 13 Jan 2022 23:56:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642118191; bh=oKepsBiUvipoUsRwWzzacVjvRjKomlWW+JhTo6LpOqw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ir6qVvGMqy8tGaEwNG8lRfmidg8fOgoDFJv1HZ9COA+XCdjul4h5hupLpTZWqhhsY stlBZA0WEnB10iWf18x2J1Sp/Eh4GkBrl4dcd7GRpk+fuXU+d/aByYP8KaeKyCKW8D UNjwI8bT4v7ieWiXdq8sNZtD0z9VsKdDZppX6GY0YmFB79npAgheHhPck/ndau7KFy GoTbwultgCUAs9wlEK1r8/YVd1LIq6N0KsESPRU9PBecR5sJZbi/GZFEtlvATzs7XJ /P/5XHEZa+w4+LFAjNaZIfwXg+ZgPO67irBpW281MHDhbMOGhvQVZXJVcbP3TpmKvb np6Z2rIQeJdPw== From: Eric Biggers To: keyrings@vger.kernel.org, David Howells , Jarkko Sakkinen Cc: Denis Kenzior , Marcel Holtmann , James Morris , linux-crypto@vger.kernel.org Subject: [PATCH 2/3] KEYS: asym_tpm: fix incorrect comment Date: Thu, 13 Jan 2022 15:54:39 -0800 Message-Id: <20220113235440.90439-3-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220113235440.90439-1-ebiggers@kernel.org> References: <20220113235440.90439-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers tpm_key_create() doesn't actually load the key into the TPM. Fix the comment to describe what the function does. Signed-off-by: Eric Biggers Acked-by: Jarkko Sakkinen --- crypto/asymmetric_keys/asym_tpm.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crypto/asymmetric_keys/asym_tpm.c b/crypto/asymmetric_keys/asym_tpm.c index 60d20d44c885..2e365a221fbe 100644 --- a/crypto/asymmetric_keys/asym_tpm.c +++ b/crypto/asymmetric_keys/asym_tpm.c @@ -900,7 +900,11 @@ static int extract_key_parameters(struct tpm_key *tk) return 0; } -/* Given the blob, parse it and load it into the TPM */ +/* + * Verify that a supported TPM is present, then parse the key blob. We don't + * actually load the key into the TPM here; that happens only for the actual + * sign and decrypt operations. + */ struct tpm_key *tpm_key_create(const void *blob, uint32_t blob_len) { int r; From patchwork Thu Jan 13 23:54:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 532485 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F072AC4332F for ; Thu, 13 Jan 2022 23:56:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235807AbiAMX4d (ORCPT ); Thu, 13 Jan 2022 18:56:33 -0500 Received: from dfw.source.kernel.org ([139.178.84.217]:50072 "EHLO dfw.source.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238256AbiAMX4c (ORCPT ); Thu, 13 Jan 2022 18:56:32 -0500 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 70BDE61D0E; Thu, 13 Jan 2022 23:56:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A8C32C36AF3; Thu, 13 Jan 2022 23:56:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1642118191; bh=OYwT1/oic13UOKNCipi3UaLYBkRKybjJlJF3WtRhuG4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RvWoRghf90qh2LA7WNHy1/8tcMzHX8zcw/Gmsa/qP/q8lePpPoEW9GtFkmjQW9bd5 JFAotM4xGK02KKcpmZAJ0fKSUm5hMcoAEvNtILJJbvljTSXpZ6yk1i0CZ3ctTiygpu tP2mzFLJdsViO3WAQhkfl6bckXDCDLpM72JAa1EdwgBKX42bI4WznUgtSlcMUBzKa8 xtOh0FXxUQrp2KC0m2VfNWN5Nauo9v1U4fzanbtNrTQmuHfgHD5Fezy+tUAhu5ubFa NuxucdotQX2gwrucH5fRRPepv27AY5ta6rRX18OYpb4ke5KGKRDUFuNX4F4/x/DW3F 7vuiOnsp1SN9A== From: Eric Biggers To: keyrings@vger.kernel.org, David Howells , Jarkko Sakkinen Cc: Denis Kenzior , Marcel Holtmann , James Morris , linux-crypto@vger.kernel.org Subject: [PATCH 3/3] KEYS: asym_tpm: rename derive_pub_key() Date: Thu, 13 Jan 2022 15:54:40 -0800 Message-Id: <20220113235440.90439-4-ebiggers@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220113235440.90439-1-ebiggers@kernel.org> References: <20220113235440.90439-1-ebiggers@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers derive_pub_key() doesn't actually derive a key; it just formats one. Rename it accordingly. Signed-off-by: Eric Biggers --- crypto/asymmetric_keys/asym_tpm.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crypto/asymmetric_keys/asym_tpm.c b/crypto/asymmetric_keys/asym_tpm.c index 2e365a221fbe..d2e0036d34a5 100644 --- a/crypto/asymmetric_keys/asym_tpm.c +++ b/crypto/asymmetric_keys/asym_tpm.c @@ -336,7 +336,7 @@ static inline uint8_t *encode_tag_length(uint8_t *buf, uint8_t tag, return buf + 3; } -static uint32_t derive_pub_key(const void *pub_key, uint32_t len, uint8_t *buf) +static uint32_t format_pub_key(const void *pub_key, uint32_t len, uint8_t *buf) { uint8_t *cur = buf; uint32_t n_len = definite_length(len) + 1 + len + 1; @@ -409,7 +409,7 @@ static int tpm_key_query(const struct kernel_pkey_params *params, if (IS_ERR(tfm)) return PTR_ERR(tfm); - der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, + der_pub_key_len = format_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len); @@ -463,7 +463,7 @@ static int tpm_key_encrypt(struct tpm_key *tk, if (IS_ERR(tfm)) return PTR_ERR(tfm); - der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, + der_pub_key_len = format_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len); @@ -758,7 +758,7 @@ static int tpm_key_verify_signature(const struct key *key, if (IS_ERR(tfm)) return PTR_ERR(tfm); - der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len, + der_pub_key_len = format_pub_key(tk->pub_key, tk->pub_key_len, der_pub_key); ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);