Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1692394pxb; Wed, 9 Feb 2022 02:20:14 -0800 (PST) X-Google-Smtp-Source: ABdhPJxwytIxglqHScRw8PatYlZagpG5QO0ul/zgweexR3PdLhH/JhkuhTjaPfk1G2pbgsOxfjkA X-Received: by 2002:a17:906:25cb:: with SMTP id n11mr1264838ejb.419.1644402013798; Wed, 09 Feb 2022 02:20:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644402013; cv=none; d=google.com; s=arc-20160816; b=VKzhiLJ3AAFLjbiV3kP7akOKwbabx+mTBiOPqPLrgr3QjN4UCIAlXCiaXAC9tXhJz6 hq459lap3IEf536zZVsVkMIStOFYHIMznG+NenznzZD5LIH+Y06EejsIs9+STb9APt18 OS46iPdUs/UzCa/9SGxSnNkcKkFt4cDG/2pA+GpfVNAVFzmVwKwLwdZkyVGDg5OW8jBJ a1univ8fH8jcOSr1Vjii4HpS/1aImcy66uTKDbmPRg6G3psYbKN9P2GUusY2/AVKXTqQ TpyFWexQgr3JpRLdvo8DkezlI9SVBMdmV27N2Xw2mIyjsDGhViLiOg+Gad+eeWHZ39yn wkwQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=c/OXQHVEDR7xZo2T49dclmDSoCUv+BeiuKgXTu/frBE=; b=v8XttySshKEwjyadKJ0mYjg+aRKBtKeHAwqGbhJWVf5+6vN//851y+UlyfqAL5//7C 746UjvGq05SISq7z/YawH4dCmVRyXY/iTs1JNmt1/XOvomI/Tro4VWz2OQYwGRFeGxIi ibzAYgPJi5Mh5QS+ETSWEYOiYizgWyu/dGBzFKBA3vmx3DB4eYYg9tDcrbHdtCT+ATYc r78TFwtcvTWBlPMntrGEypGBronRB2JXy8lbwuQXLb9sMSrbEj7zOY6YHMo5tKtRKdrf RfKG3e8NweFjmHpJDFNb64xG/nVmAhIcjBzOS/StgAYXd0jG+3nkkXksBhJ0u+d5WiUM uERQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i16si13285829edc.401.2022.02.09.02.19.47; Wed, 09 Feb 2022 02:20:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241423AbiBHI6d (ORCPT + 99 others); Tue, 8 Feb 2022 03:58:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352625AbiBHI6c (ORCPT ); Tue, 8 Feb 2022 03:58:32 -0500 X-Greylist: delayed 471 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Tue, 08 Feb 2022 00:58:30 PST Received: from vmicros1.altlinux.org (vmicros1.altlinux.org [194.107.17.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id EA3B7C03FEC1; Tue, 8 Feb 2022 00:58:30 -0800 (PST) Received: from imap.altlinux.org (imap.altlinux.org [194.107.17.38]) by vmicros1.altlinux.org (Postfix) with ESMTP id 1436A72C905; Tue, 8 Feb 2022 11:50:38 +0300 (MSK) Received: from altlinux.org (sole.flsd.net [185.75.180.6]) by imap.altlinux.org (Postfix) with ESMTPSA id E823E4A46EA; Tue, 8 Feb 2022 11:50:37 +0300 (MSK) Date: Tue, 8 Feb 2022 11:50:37 +0300 From: Vitaly Chikunov To: Eric Biggers Cc: keyrings@vger.kernel.org, Jarkko Sakkinen , David Howells , linux-crypto@vger.kernel.org, linux-integrity@vger.kernel.org, Stefan Berger , Gilad Ben-Yossef , Tianjia Zhang , Mimi Zohar , stable@vger.kernel.org Subject: Re: [PATCH v2 1/2] KEYS: asymmetric: enforce that sig algo matches key algo Message-ID: <20220208085037.lo53hi6ohjusr7pv@altlinux.org> References: <20220208052448.409152-1-ebiggers@kernel.org> <20220208052448.409152-2-ebiggers@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=koi8-r Content-Disposition: inline In-Reply-To: <20220208052448.409152-2-ebiggers@kernel.org> X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,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-crypto@vger.kernel.org On Mon, Feb 07, 2022 at 09:24:47PM -0800, Eric Biggers wrote: > From: Eric Biggers > > Most callers of public_key_verify_signature(), including most indirect > callers via verify_signature() as well as pkcs7_verify_sig_chain(), > don't check that public_key_signature::pkey_algo matches > public_key::pkey_algo. These should always match. However, a malicious > signature could intentionally declare an unintended algorithm. It is > essential that such signatures be rejected outright, or that the > algorithm of the *key* be used -- not the algorithm of the signature as > that would allow attackers to choose the algorithm used. > > Currently, public_key_verify_signature() correctly uses the key's > algorithm when deciding which akcipher to allocate. That's good. > However, it uses the signature's algorithm when deciding whether to do > the first step of SM2, which is incorrect. Also, v4.19 and older > kernels used the signature's algorithm for the entire process. > > Prevent such errors by making public_key_verify_signature() enforce that > the signature's algorithm (if given) matches the key's algorithm. > > Also remove two checks of this done by callers, which are now redundant. > > Cc: stable@vger.kernel.org > Tested-by: Stefan Berger > Tested-by: Tianjia Zhang > Signed-off-by: Eric Biggers Reviewed-by: Vitaly Chikunov Thanks, > --- > crypto/asymmetric_keys/pkcs7_verify.c | 6 ------ > crypto/asymmetric_keys/public_key.c | 15 +++++++++++++++ > crypto/asymmetric_keys/x509_public_key.c | 6 ------ > 3 files changed, 15 insertions(+), 12 deletions(-) > > diff --git a/crypto/asymmetric_keys/pkcs7_verify.c b/crypto/asymmetric_keys/pkcs7_verify.c > index 0b4d07aa8811..f94a1d1ad3a6 100644 > --- a/crypto/asymmetric_keys/pkcs7_verify.c > +++ b/crypto/asymmetric_keys/pkcs7_verify.c > @@ -174,12 +174,6 @@ static int pkcs7_find_key(struct pkcs7_message *pkcs7, > pr_devel("Sig %u: Found cert serial match X.509[%u]\n", > sinfo->index, certix); > > - if (strcmp(x509->pub->pkey_algo, sinfo->sig->pkey_algo) != 0) { > - pr_warn("Sig %u: X.509 algo and PKCS#7 sig algo don't match\n", > - sinfo->index); > - continue; > - } > - > sinfo->signer = x509; > return 0; > } > diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c > index 4fefb219bfdc..e36213945686 100644 > --- a/crypto/asymmetric_keys/public_key.c > +++ b/crypto/asymmetric_keys/public_key.c > @@ -325,6 +325,21 @@ int public_key_verify_signature(const struct public_key *pkey, > BUG_ON(!sig); > BUG_ON(!sig->s); > > + /* > + * If the signature specifies a public key algorithm, it *must* match > + * the key's actual public key algorithm. > + * > + * Small exception: ECDSA signatures don't specify the curve, but ECDSA > + * keys do. So the strings can mismatch slightly in that case: > + * "ecdsa-nist-*" for the key, but "ecdsa" for the signature. > + */ > + if (sig->pkey_algo) { > + if (strcmp(pkey->pkey_algo, sig->pkey_algo) != 0 && > + (strncmp(pkey->pkey_algo, "ecdsa-", 6) != 0 || > + strcmp(sig->pkey_algo, "ecdsa") != 0)) > + return -EKEYREJECTED; > + } > + > ret = software_key_determine_akcipher(sig->encoding, > sig->hash_algo, > pkey, alg_name); > diff --git a/crypto/asymmetric_keys/x509_public_key.c b/crypto/asymmetric_keys/x509_public_key.c > index fe14cae115b5..71cc1738fbfd 100644 > --- a/crypto/asymmetric_keys/x509_public_key.c > +++ b/crypto/asymmetric_keys/x509_public_key.c > @@ -128,12 +128,6 @@ int x509_check_for_self_signed(struct x509_certificate *cert) > goto out; > } > > - ret = -EKEYREJECTED; > - if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0 && > - (strncmp(cert->pub->pkey_algo, "ecdsa-", 6) != 0 || > - strcmp(cert->sig->pkey_algo, "ecdsa") != 0)) > - goto out; > - > ret = public_key_verify_signature(cert->pub, cert->sig); > if (ret < 0) { > if (ret == -ENOPKG) { > -- > 2.35.1