Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp695547pxb; Tue, 5 Apr 2022 19:08:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxK6ph8HmD+rmhQM0wNCZciouCymSIuUd/peOplkm/IMaA+lOdKGOl4aceiueSQZXhbDodx X-Received: by 2002:aa7:d1d0:0:b0:41c:c19e:a0d6 with SMTP id g16-20020aa7d1d0000000b0041cc19ea0d6mr6442999edp.179.1649210891601; Tue, 05 Apr 2022 19:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649210891; cv=none; d=google.com; s=arc-20160816; b=cQ4cHRDVRHMknnbiQt4KxzuHr2LaOsMtBoi0jsZtsFwPF6EyL4rI6kkFQDE40dOUdg ax2UMv3oITYSKxOOsPxL3hF2/WuMaMgCS6xyW1O4+9ABN/x8HpyZ3B4TYOLWlp3CT/ax yTjAfbmBcvheVhhS+t+zoXhr3Sw3VLzc8r0gpM/aidi7vxM1JMo5MW2L0cYe6yS60IP8 zc0lXCsKEdoRYgFooNXUuoqOOYidaEpVxTy6r8v23ar068VgX/X/OtZ4rwS0wqfDdAeh /wHMWr3q9e+7oLf09a8qyTNAg7k7ND2Uw+EPyCIssgn54CL73ZCl/JhxcFGAaQUZwXXG NVdw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XgJSru4Z/TApeRSJqDw80NETE5Z2puqEGjLG4oFaCFU=; b=KAEcNuMeXXjfRSyk20h735rJ9A40b2zahQOE/FD2twCnmfvVtoBxG/WsDcg668uLo/ sJ1hleaQUF7nmI3zQ2sGDDZ8LboFaq39wWbHMmtW3PAKKmD+KcX/75cBKttaeDSPsdqz 9x2sIjm36fdGrXLkcDEyaDz8uywsRdkj+WASNvr8bA+0VoVIrg7jsQ3o8XbZGUy12q8J Z4WqLH/TgVFORjLZslNeYBlDaPdGYUpogqv/PRVpEYEvFLhucYnDrskiRXngO09UPogv i0VtvnxROaVjcNPsOfWUV7TR57F1EH4TCyh0D+ZRRiFbpZ6qhRutdB28qpA+4WrNUIIg X9Tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vsHvJ0RW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jo23-20020a170906f6d700b006e7f47d22a9si5294906ejb.434.2022.04.05.19.07.41; Tue, 05 Apr 2022 19:08:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=vsHvJ0RW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387856AbiDEOc4 (ORCPT + 99 others); Tue, 5 Apr 2022 10:32:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55266 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238874AbiDEJdC (ORCPT ); Tue, 5 Apr 2022 05:33:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85D04E35; Tue, 5 Apr 2022 02:20:38 -0700 (PDT) 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 228196165C; Tue, 5 Apr 2022 09:20:38 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 335A4C385A2; Tue, 5 Apr 2022 09:20:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150437; bh=S5JSbPs7QsysBWhnR3G8MjqokpkFfZdd3XOVDP5puGs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vsHvJ0RWpVsq5u0S6uzQnLozMc6FQCossaH3RBYZIYpBrAdz2d7YfS4segeTXX9kX 1AYuVmeBvwCK2PFxRkb8ttB+x8bi0NLSP9de5b2jAS7c8L0ECJXK8ZJ9o2a1LeMoa9 Q39uFuEqjztWn533lHYXebJroX5fBRfJmmzKf5YQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Stefan Berger , Tianjia Zhang , Eric Biggers , Vitaly Chikunov , Jarkko Sakkinen Subject: [PATCH 5.15 056/913] KEYS: asymmetric: enforce that sig algo matches key algo Date: Tue, 5 Apr 2022 09:18:38 +0200 Message-Id: <20220405070341.501507379@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Eric Biggers commit 2abc9c246e0548e52985b10440c9ea3e9f65f793 upstream. 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 Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Greg Kroah-Hartman --- 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(-) --- 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_m 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; } --- a/crypto/asymmetric_keys/public_key.c +++ b/crypto/asymmetric_keys/public_key.c @@ -325,6 +325,21 @@ int public_key_verify_signature(const st 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); --- 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 x5 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) {