Keyrings Archive mirror
 help / color / mirror / Atom feed
From: Mahmoud Adam <mngyadam@amazon.com>
To: <dhowells@redhat.com>
Cc: <herbert@gondor.apana.org.au>, <davem@davemloft.net>,
	<keyrings@vger.kernel.org>, <linux-crypto@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>,
	Mahmoud Adam <mngyadam@amazon.com>
Subject: [PATCH v2] KEYS: use kfree_sensitive with key
Date: Thu, 15 Jun 2023 12:57:13 +0000	[thread overview]
Message-ID: <20230615125712.105873-1-mngyadam@amazon.com> (raw)

key might contain private part of the key, so better use
kfree_sensitive to free it

Signed-off-by: Mahmoud Adam <mngyadam@amazon.com>
---
v2: kfree_sensitive only private key

 crypto/asymmetric_keys/public_key.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/crypto/asymmetric_keys/public_key.c b/crypto/asymmetric_keys/public_key.c
index eca5671ad3f2..cd8c4123d936 100644
--- a/crypto/asymmetric_keys/public_key.c
+++ b/crypto/asymmetric_keys/public_key.c
@@ -43,7 +43,10 @@ static void public_key_describe(const struct key *asymmetric_key,
 void public_key_free(struct public_key *key)
 {
 	if (key) {
-		kfree(key->key);
+		if(key->key_is_private)
+			kfree_sensitive(key->key);
+		else
+			kfree(key->key);
 		kfree(key->params);
 		kfree(key);
 	}
@@ -218,7 +221,7 @@ static int software_key_query(const struct kernel_pkey_params *params,
 	ret = 0;

 error_free_key:
-	kfree(key);
+	kfree_sensitive(key);
 error_free_tfm:
 	crypto_free_akcipher(tfm);
 	pr_devel("<==%s() = %d\n", __func__, ret);
@@ -303,7 +306,7 @@ static int software_key_eds_op(struct kernel_pkey_params *params,
 		ret = req->dst_len;

 error_free_key:
-	kfree(key);
+	kfree_sensitive(key);
 error_free_req:
 	akcipher_request_free(req);
 error_free_tfm:
@@ -456,7 +459,7 @@ int public_key_verify_signature(const struct public_key *pkey,
 	ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);

 error_free_key:
-	kfree(key);
+	kfree_sensitive(key);
 error_free_req:
 	akcipher_request_free(req);
 error_free_tfm:
--
2.40.1

             reply	other threads:[~2023-06-15 12:59 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-15 12:57 Mahmoud Adam [this message]
2023-06-16 10:31 ` [PATCH v2] KEYS: use kfree_sensitive with key Herbert Xu
2023-06-16 10:42   ` Adam, Mahmoud

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=20230615125712.105873-1-mngyadam@amazon.com \
    --to=mngyadam@amazon.com \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).