From nobody Mon Sep 29 21:22:42 2025 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 A4DF3C00140 for ; Tue, 16 Aug 2022 01:07:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348609AbiHPBFn (ORCPT ); Mon, 15 Aug 2022 21:05:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242952AbiHPA73 (ORCPT ); Mon, 15 Aug 2022 20:59:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B897D1A2EAB; Mon, 15 Aug 2022 13:49:57 -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 ams.source.kernel.org (Postfix) with ESMTPS id 94285B8113E; Mon, 15 Aug 2022 20:49:55 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DFAAEC433D6; Mon, 15 Aug 2022 20:49:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660596594; bh=kxzoYOEPOH9+Y+3lO+DaaJftUEo51nXgjw+Ns0g0fJM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lQg5rsoWfPUizxIUwDhT+NC4uMv0TQn4/lyLEVhEYLxpaCoZZ6BEOjeOjvuqLTqeB OJzSTdWqFBb4WxQmeyRv2Zton7ouXjkZhHvVY8zXnFHNax3eW62vFGl6nZ3gvq9azw jfwWGTx/cJXTGTq8b5KKLWZNEkZQYoEx6YL1yieU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Biggers , Tianjia Zhang , Jarkko Sakkinen , Sasha Levin Subject: [PATCH 5.19 1130/1157] KEYS: asymmetric: enforce SM2 signature use pkey algo Date: Mon, 15 Aug 2022 20:08:06 +0200 Message-Id: <20220815180525.589278405@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180439.416659447@linuxfoundation.org> References: <20220815180439.416659447@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Tianjia Zhang [ Upstream commit 0815291a8fd66cdcf7db1445d4d99b0d16065829 ] The signature verification of SM2 needs to add the Za value and recalculate sig->digest, which requires the detection of the pkey_algo in public_key_verify_signature(). As Eric Biggers said, the pkey_algo field in sig is attacker-controlled and should be use pkey->pkey_algo instead of sig->pkey_algo, and secondly, if sig->pkey_algo is NULL, it will also cause signature verification failure. The software_key_determine_akcipher() already forces the algorithms are matched, so the SM3 algorithm is enforced in the SM2 signature, although this has been checked, we still avoid using any algorithm information in the signature as input. Fixes: 215525639631 ("X.509: support OSCCA SM2-with-SM3 certificate verific= ation") Reported-by: Eric Biggers Cc: stable@vger.kernel.org # v5.10+ Signed-off-by: Tianjia Zhang Reviewed-by: Jarkko Sakkinen Signed-off-by: Jarkko Sakkinen Signed-off-by: Sasha Levin --- crypto/asymmetric_keys/public_key.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/p= ublic_key.c index 7c9e6be35c30..2f8352e88860 100644 --- a/crypto/asymmetric_keys/public_key.c +++ b/crypto/asymmetric_keys/public_key.c @@ -304,6 +304,10 @@ static int cert_sig_digest_update(const struct public_= key_signature *sig, =20 BUG_ON(!sig->data); =20 + /* SM2 signatures always use the SM3 hash algorithm */ + if (!sig->hash_algo || strcmp(sig->hash_algo, "sm3") !=3D 0) + return -EINVAL; + ret =3D sm2_compute_z_digest(tfm_pkey, SM2_DEFAULT_USERID, SM2_DEFAULT_USERID_LEN, dgst); if (ret) @@ -414,8 +418,7 @@ int public_key_verify_signature(const struct public_key= *pkey, if (ret) goto error_free_key; =20 - if (sig->pkey_algo && strcmp(sig->pkey_algo, "sm2") =3D=3D 0 && - sig->data_size) { + if (strcmp(pkey->pkey_algo, "sm2") =3D=3D 0 && sig->data_size) { ret =3D cert_sig_digest_update(sig, tfm); if (ret) goto error_free_key; --=20 2.35.1