diff mbox series

crypto: public_key - Make sig/tfm local to if clause in software_key_query

Message ID Z_9gygoIHTH7A9Ma@gondor.apana.org.au
State New
Headers show
Series crypto: public_key - Make sig/tfm local to if clause in software_key_query | expand

Commit Message

Herbert Xu April 16, 2025, 7:48 a.m. UTC
The recent code changes in this function triggered a false-positive
maybe-uninitialized warning in software_key_query.  Rearrange the
code by moving the sig/tfm variables into the if clause where they
are actually used.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Comments

Herbert Xu April 20, 2025, 5:48 a.m. UTC | #1
On Sun, Apr 20, 2025 at 07:46:24AM +0200, Lukas Wunner wrote:
>
> Hm, I've tried to reproduce the warning with W=1 and W=2 to no avail
> (with gcc 14).  How did you trigger it?
> 
> FWIW,
> Reviewed-by: Lukas Wunner <lukas@wunner.de>
> 
> I suppose this may have been introduced by 63ba4d67594a ("KEYS:
> asymmetric: Use new crypto interface without scatterlists").

It was with Debian stable gcc 12 so it could've been fixed already.

Cheers,
diff mbox series

Patch

diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index 89dc887d2c5c..e5b177c8e842 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -163,10 +163,8 @@  static u8 *pkey_pack_u32(u8 *dst, u32 val)
 static int software_key_query(const struct kernel_pkey_params *params,
 			      struct kernel_pkey_query *info)
 {
-	struct crypto_akcipher *tfm;
 	struct public_key *pkey = params->key->payload.data[asym_crypto];
 	char alg_name[CRYPTO_MAX_ALG_NAME];
-	struct crypto_sig *sig;
 	u8 *key, *ptr;
 	int ret, len;
 	bool issig;
@@ -191,6 +189,8 @@  static int software_key_query(const struct kernel_pkey_params *params,
 	memset(info, 0, sizeof(*info));
 
 	if (issig) {
+		struct crypto_sig *sig;
+
 		sig = crypto_alloc_sig(alg_name, 0, 0);
 		if (IS_ERR(sig)) {
 			ret = PTR_ERR(sig);
@@ -202,7 +202,7 @@  static int software_key_query(const struct kernel_pkey_params *params,
 		else
 			ret = crypto_sig_set_pubkey(sig, key, pkey->keylen);
 		if (ret < 0)
-			goto error_free_tfm;
+			goto error_free_sig;
 
 		len = crypto_sig_keysize(sig);
 		info->key_size = len;
@@ -221,7 +221,12 @@  static int software_key_query(const struct kernel_pkey_params *params,
 			if (pkey->key_is_private)
 				info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
 		}
+
+error_free_sig:
+		crypto_free_sig(sig);
 	} else {
+		struct crypto_akcipher *tfm;
+
 		tfm = crypto_alloc_akcipher(alg_name, 0, 0);
 		if (IS_ERR(tfm)) {
 			ret = PTR_ERR(tfm);
@@ -233,7 +238,7 @@  static int software_key_query(const struct kernel_pkey_params *params,
 		else
 			ret = crypto_akcipher_set_pub_key(tfm, key, pkey->keylen);
 		if (ret < 0)
-			goto error_free_tfm;
+			goto error_free_akcipher;
 
 		len = crypto_akcipher_maxsize(tfm);
 		info->key_size = len * BITS_PER_BYTE;
@@ -245,15 +250,11 @@  static int software_key_query(const struct kernel_pkey_params *params,
 		info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT;
 		if (pkey->key_is_private)
 			info->supported_ops |= KEYCTL_SUPPORTS_DECRYPT;
+
+error_free_akcipher:
+		crypto_free_akcipher(tfm);
 	}
 
-	ret = 0;
-
-error_free_tfm:
-	if (issig)
-		crypto_free_sig(sig);
-	else
-		crypto_free_akcipher(tfm);
 error_free_key:
 	kfree_sensitive(key);
 	pr_devel("<==%s() = %d\n", __func__, ret);