[<prev] [next>] [day] [month] [year] [list]
Message-ID: <20220519011918.173538-1-guozihua@huawei.com>
Date: Thu, 19 May 2022 09:19:18 +0800
From: GUO Zihua <guozihua@...wei.com>
To: <keyrings@...r.kernel.org>
CC: <linux-crypto@...r.kernel.org>, <davem@...emloft.net>,
<dhowells@...hat.com>, <herbert@...dor.apana.org.au>,
<gustavoars@...nel.org>, <keescook@...omium.org>,
<linux-hardening@...r.kernel.org>
Subject: [PATCH v4] crypto: Use struct_size() helper with kmalloc()
Make use of struct_size() helper for structures containing flexible array
member instead of sizeof() which prevents potential issues, as well as
increase readability.
Reference: https://lore.kernel.org/all/CAHk-=wiGWjxs7EVUpccZEi6esvjpHJdgHQ=vtUeJ5crL62hx9A@mail.gmail.com/
Signed-off-by: GUO Zihua <guozihua@...wei.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@...nel.org>
Reviewed-by: Kees Cook <keescook@...omium.org>
---
v4:
Change subject from "in kmalloc" to "with kmalloc" following David's
suggestion. Added Kees' review-by.
v3:
Update commit message according to Gustavo's review.
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
Powered by blists - more mailing lists