* [PATCH v2] crypto: Use struct_size() helper in kmalloc()
@ 2022-05-18 0:56 GUO Zihua
2022-05-18 4:55 ` Gustavo A. R. Silva
0 siblings, 1 reply; 3+ messages in thread
From: GUO Zihua @ 2022-05-18 0:56 UTC (permalink / raw)
To: keyrings
Cc: linux-crypto, davem, dhowells, herbert, gustavoars, linux-hardening
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] crypto: Use struct_size() helper in kmalloc()
2022-05-18 0:56 [PATCH v2] crypto: Use struct_size() helper in kmalloc() GUO Zihua
@ 2022-05-18 4:55 ` Gustavo A. R. Silva
2022-05-18 6:21 ` Guozihua (Scott)
0 siblings, 1 reply; 3+ messages in thread
From: Gustavo A. R. Silva @ 2022-05-18 4:55 UTC (permalink / raw)
To: GUO Zihua
Cc: keyrings, linux-crypto, davem, dhowells, herbert, linux-hardening
On Wed, May 18, 2022 at 08:56:39AM +0800, GUO Zihua wrote:
> 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
The warnings above are not silenced with this patch as struct_size()
internally use sizeof on the struct-with-flex-array.
However, the use of struct_size() instead of the open-coded expressions
in the calls to kmalloc() is correct.
>
> Reference: https://github.com/KSPP/linux/issues/174
I updated this issue on the issue tracker. Please, from now on, just
use that issue as a list of potential open-coded instances to be
audited. :)
>
> Signed-off-by: GUO Zihua <guozihua@huawei.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Thanks
--
Gustavo
>
> ---
>
> 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
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] crypto: Use struct_size() helper in kmalloc()
2022-05-18 4:55 ` Gustavo A. R. Silva
@ 2022-05-18 6:21 ` Guozihua (Scott)
0 siblings, 0 replies; 3+ messages in thread
From: Guozihua (Scott) @ 2022-05-18 6:21 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: keyrings, linux-crypto, davem, dhowells, herbert, linux-hardening
On 2022/5/18 12:55, Gustavo A. R. Silva wrote:
> On Wed, May 18, 2022 at 08:56:39AM +0800, GUO Zihua wrote:
>> 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
>
> The warnings above are not silenced with this patch as struct_size()
> internally use sizeof on the struct-with-flex-array.
>
> However, the use of struct_size() instead of the open-coded expressions
> in the calls to kmalloc() is correct.
>
>>
>> Reference: https://github.com/KSPP/linux/issues/174
>
> I updated this issue on the issue tracker. Please, from now on, just
> use that issue as a list of potential open-coded instances to be
> audited. :)
>
>>
>> Signed-off-by: GUO Zihua <guozihua@huawei.com>
>
> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
>
> Thanks
> --
> Gustavo
>
>>
>> ---
>>
>> 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
>>
> .
Thanks for the review Gustavo.
I'll send a v3 patch and update the commit message. Maybe refer to the
mail from Linus instead.
--
Best
GUO Zihua
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-05-18 6:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 0:56 [PATCH v2] crypto: Use struct_size() helper in kmalloc() GUO Zihua
2022-05-18 4:55 ` Gustavo A. R. Silva
2022-05-18 6:21 ` Guozihua (Scott)
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 NNTP newsgroup(s).