mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Markus Elfring <Markus.Elfring@web.de>
To: linux-crypto@vger.kernel.org, kernel-janitors@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	Peter Zijlstra <peterz@infradead.org>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: [PATCH v3 3/3] lib/digsig: Use scope-based resource management for two variables in digsig_verify_rsa()
Date: Sat, 12 Oct 2024 17:10:26 +0200	[thread overview]
Message-ID: <3ac3fd6a-755e-4829-bc56-234f5b0f35e4@web.de> (raw)
In-Reply-To: <ee5f409e-4412-41cd-b8d8-92872770f04d@web.de>

From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sat, 12 Oct 2024 15:28:22 +0200

Scope-based resource management became supported for some
programming interfaces by contributions of Peter Zijlstra on 2023-05-26.
See also the commit 54da6a0924311c7cf5015533991e44fb8eb12773 ("locking:
Introduce __cleanup() based infrastructure").

* Thus use the attribute “__free(kfree)”.

* Reduce the scopes for the local variables “out1” and “p”.

* Omit explicit kfree() calls accordingly.

* Add a jump target.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---

V3:
Further adjustments were provided for the demonstration of an evolving
programming interface.


 lib/digsig.c | 87 +++++++++++++++++++++++++---------------------------
 1 file changed, 42 insertions(+), 45 deletions(-)

diff --git a/lib/digsig.c b/lib/digsig.c
index 2481120094ab..1a24677af643 100644
--- a/lib/digsig.c
+++ b/lib/digsig.c
@@ -73,10 +73,9 @@ static int digsig_verify_rsa(struct key *key,
 	unsigned long mlen, mblen;
 	unsigned int l;
 	int head, i;
-	unsigned char *out1 = NULL;
 	const char *m;
 	MPI pkey[2];
-	uint8_t *p, *datap;
+	uint8_t *datap;
 	const uint8_t *endp;
 	const struct user_key_payload *ukp;
 	struct pubkey_hdr *pkh;
@@ -126,58 +125,56 @@ static int digsig_verify_rsa(struct key *key,
 	}

 	err = -ENOMEM;
-
-	out1 = kzalloc(mlen, GFP_KERNEL);
-	if (!out1)
-		goto free_keys;
-
 	{
-		unsigned int nret = siglen;
-		MPI in __free(mpi_free) = mpi_read_from_buffer(sig, &nret);
-
-		if (IS_ERR(in)) {
-			err = PTR_ERR(in);
-			goto in_exit;
-		}
+		unsigned char *out1 __free(kfree) = kzalloc(mlen, GFP_KERNEL);

+		if (out1)
 		{
-			MPI res __free(mpi_free) = mpi_alloc(mpi_get_nlimbs(in) * 2);
-
-			if (!res)
-				goto res_exit;
-
-			err = mpi_powm(res, in, pkey[1], pkey[0]);
-			if (err)
-				goto res_exit;
-
-			if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
-				err = -EINVAL;
-				goto res_exit;
-			}
+			unsigned int nret = siglen;
+			MPI in __free(mpi_free) = mpi_read_from_buffer(sig, &nret);

-			p = mpi_get_buffer(res, &l, NULL);
-			if (!p) {
-				err = -EINVAL;
-				goto res_exit;
+			if (IS_ERR(in)) {
+				err = PTR_ERR(in);
+				goto in_exit;
 			}

-			len = mlen;
-			head = len - l;
-			memset(out1, 0, head);
-			memcpy(out1 + head, p, l);
-
-			kfree(p);
-
-			m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
-
-			if (!m || len != hlen || memcmp(m, h, hlen))
-				err = -EINVAL;
+			{
+				MPI res __free(mpi_free) = mpi_alloc(mpi_get_nlimbs(in) * 2);
+
+				if (!res)
+					goto res_exit;
+
+				err = mpi_powm(res, in, pkey[1], pkey[0]);
+				if (err)
+					goto res_exit;
+
+				if (mpi_get_nlimbs(res) * BYTES_PER_MPI_LIMB > mlen) {
+					err = -EINVAL;
+					goto res_exit;
+				}
+
+				{
+					uint8_t *p __free(kfree) = mpi_get_buffer(res, &l, NULL);
+
+					if (!p) {
+						err = -EINVAL;
+						goto p_exit;
+					}
+
+					len = mlen;
+					head = len - l;
+					memset(out1, 0, head);
+					memcpy(out1 + head, p, l);
+					m = pkcs_1_v1_5_decode_emsa(out1, len, mblen, &len);
+					if (!m || len != hlen || memcmp(m, h, hlen))
+						err = -EINVAL;
+p_exit:
+				}
 res_exit:
-		}
+			}
 in_exit:
+		}
 	}
-
-	kfree(out1);
 free_keys:
 	while (--i >= 0)
 		mpi_free(pkey[i]);
--
2.46.1


  parent reply	other threads:[~2024-10-12 15:10 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-17  9:33 [PATCH] crypto: lib/mpi - Extend support for scope-based resource management Markus Elfring
2024-09-17 23:27 ` kernel test robot
2024-09-18  9:26   ` [PATCH v2] " Markus Elfring
2024-10-05  5:33     ` Herbert Xu
2024-10-05  6:27       ` [PATCH v2 RESEND] " Markus Elfring
2024-10-10  8:40         ` Herbert Xu
2024-10-10  8:58           ` [v2] " Markus Elfring
2024-10-10  9:00             ` Herbert Xu
2024-10-10  9:10               ` Markus Elfring
2024-10-12 15:00           ` [PATCH v3 0/3] lib: Extending " Markus Elfring
2024-10-12 15:05             ` [PATCH v3 1/3] crypto: lib/mpi - Extend " Markus Elfring
2024-10-12 15:08             ` [PATCH v3 2/3] lib/digsig: Use scope-based resource management for two MPI variables in digsig_verify_rsa() Markus Elfring
2024-10-15 21:27               ` kernel test robot
2024-10-16 11:21               ` kernel test robot
2024-10-12 15:10             ` Markus Elfring [this message]
2024-09-18  0:52 ` [PATCH] crypto: lib/mpi - Extend support for scope-based resource management kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3ac3fd6a-755e-4829-bc56-234f5b0f35e4@web.de \
    --to=markus.elfring@web.de \
    --cc=akpm@linux-foundation.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=kernel-janitors@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®