All of lore.kernel.org
 help / color / mirror / Atom feed
From: GUO Zihua <guozihua@huawei.com>
To: <keyrings@vger.kernel.org>
Cc: <linux-crypto@vger.kernel.org>, <davem@davemloft.net>,
	<dhowells@redhat.com>, <herbert@gondor.apana.org.au>,
	<gustavoars@kernel.org>, <linux-hardening@vger.kernel.org>
Subject: [PATCH v2] crypto: Use struct_size() helper in kmalloc()
Date: Wed, 18 May 2022 08:56:39 +0800	[thread overview]
Message-ID: <20220518005639.181640-1-guozihua@huawei.com> (raw)

Make use of struct_size() heler for structures containing flexible array
member instead of sizeof() which prevents potential issues as well as
addressing the following sparse warning:

crypto/asymmetric_keys/asymmetric_type.c:155:23: warning: using sizeof
on a flexible structure
crypto/asymmetric_keys/asymmetric_type.c:247:28: warning: using sizeof
on a flexible structure

Reference: https://github.com/KSPP/linux/issues/174

Signed-off-by: GUO Zihua <guozihua@huawei.com>

---

v2:
    Use size_add() helper following Kees Cook's suggestion.
---
 crypto/asymmetric_keys/asymmetric_type.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
index 41a2f0eb4ce4..e020222b1fe5 100644
--- a/crypto/asymmetric_keys/asymmetric_type.c
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -152,7 +152,7 @@ struct asymmetric_key_id *asymmetric_key_generate_id(const void *val_1,
 {
 	struct asymmetric_key_id *kid;
 
-	kid = kmalloc(sizeof(struct asymmetric_key_id) + len_1 + len_2,
+	kid = kmalloc(struct_size(kid, data, size_add(len_1, len_2)),
 		      GFP_KERNEL);
 	if (!kid)
 		return ERR_PTR(-ENOMEM);
@@ -244,7 +244,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
 	if (asciihexlen & 1)
 		return ERR_PTR(-EINVAL);
 
-	match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
+	match_id = kmalloc(struct_size(match_id, data, asciihexlen / 2),
 			   GFP_KERNEL);
 	if (!match_id)
 		return ERR_PTR(-ENOMEM);
-- 
2.36.0


             reply	other threads:[~2022-05-18  0:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-18  0:56 GUO Zihua [this message]
2022-05-18  4:55 ` [PATCH v2] crypto: Use struct_size() helper in kmalloc() Gustavo A. R. Silva
2022-05-18  6:21   ` Guozihua (Scott)

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=20220518005639.181640-1-guozihua@huawei.com \
    --to=guozihua@huawei.com \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=gustavoars@kernel.org \
    --cc=herbert@gondor.apana.org.au \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-hardening@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.