Message ID | 20220517080532.31015-1-guozihua@huawei.com (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | crypto: Use struct_size() helper in kmalloc() | expand |
On Tue, May 17, 2022 at 04:05:32PM +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 > > Reference: https://github.com/KSPP/linux/issues/174 > > Signed-off-by: GUO Zihua <guozihua@huawei.com> > --- > 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..96a99a91bf17 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, len_1 + len_2), Please use the size_add() helper for this open-coded add here. > 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), There is no size_div(), but that's ok here because the denominator is an constant expression. > GFP_KERNEL); > if (!match_id) > return ERR_PTR(-ENOMEM); > -- > 2.36.0 >
On 2022/5/18 5:26, Kees Cook wrote: > On Tue, May 17, 2022 at 04:05:32PM +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 >> >> Reference: https://github.com/KSPP/linux/issues/174 >> >> Signed-off-by: GUO Zihua <guozihua@huawei.com> >> --- >> 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..96a99a91bf17 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, len_1 + len_2), > > Please use the size_add() helper for this open-coded add here. > >> 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), > > There is no size_div(), but that's ok here because the denominator is an > constant expression. > >> GFP_KERNEL); >> if (!match_id) >> return ERR_PTR(-ENOMEM); >> -- >> 2.36.0 >> > Thanks Kees, A v2 patch has been submitted.
diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c index 41a2f0eb4ce4..96a99a91bf17 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, 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);
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> --- crypto/asymmetric_keys/asymmetric_type.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)