linux-wireless.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc()
@ 2024-01-19 17:16 Erick Archer
  2024-01-19 17:44 ` Gustavo A. R. Silva
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Erick Archer @ 2024-01-19 17:16 UTC (permalink / raw)
  To: Stanislaw Gruszka, Kalle Valo, Gustavo A. R. Silva
  Cc: Erick Archer, linux-wireless, linux-kernel, linux-hardening

As noted in the "Deprecated Interfaces, Language Features, Attributes,
and Conventions" documentation [1], size calculations (especially
multiplication) should not be performed in memory allocator (or similar)
function arguments due to the risk of them overflowing. This could lead
to values wrapping around and a smaller allocation being made than the
caller was expecting. Using those allocations could lead to linear
overflows of heap memory and other misbehaviors.

So, use the purpose specific kcalloc() function instead of the argument
size * count in the kzalloc() function.

Also, it is preferred to use sizeof(*pointer) instead of sizeof(type)
due to the type of the variable can change and one needs not change the
former (unlike the latter).

Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/162
Signed-off-by: Erick Archer <erick.archer@gmx.com>
---
 drivers/net/wireless/intel/iwlegacy/common.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/drivers/net/wireless/intel/iwlegacy/common.c b/drivers/net/wireless/intel/iwlegacy/common.c
index 17570d62c896..9d33a66a49b5 100644
--- a/drivers/net/wireless/intel/iwlegacy/common.c
+++ b/drivers/net/wireless/intel/iwlegacy/common.c
@@ -3438,9 +3438,7 @@ il_init_geos(struct il_priv *il)
 	if (!channels)
 		return -ENOMEM;

-	rates =
-	    kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
-		    GFP_KERNEL);
+	rates = kcalloc(RATE_COUNT_LEGACY, sizeof(*rates), GFP_KERNEL);
 	if (!rates) {
 		kfree(channels);
 		return -ENOMEM;
--
2.25.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc()
  2024-01-19 17:16 [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc() Erick Archer
@ 2024-01-19 17:44 ` Gustavo A. R. Silva
  2024-01-21 12:15 ` Stanislaw Gruszka
  2024-01-23 11:51 ` Kalle Valo
  2 siblings, 0 replies; 4+ messages in thread
From: Gustavo A. R. Silva @ 2024-01-19 17:44 UTC (permalink / raw)
  To: Erick Archer, Stanislaw Gruszka, Kalle Valo, Gustavo A. R. Silva
  Cc: linux-wireless, linux-kernel, linux-hardening



On 1/19/24 11:16, Erick Archer wrote:
> As noted in the "Deprecated Interfaces, Language Features, Attributes,
> and Conventions" documentation [1], size calculations (especially
> multiplication) should not be performed in memory allocator (or similar)
> function arguments due to the risk of them overflowing. This could lead
> to values wrapping around and a smaller allocation being made than the
> caller was expecting. Using those allocations could lead to linear
> overflows of heap memory and other misbehaviors.
> 
> So, use the purpose specific kcalloc() function instead of the argument
> size * count in the kzalloc() function.
> 
> Also, it is preferred to use sizeof(*pointer) instead of sizeof(type)
> due to the type of the variable can change and one needs not change the
> former (unlike the latter).
> 
> Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/162
> Signed-off-by: Erick Archer <erick.archer@gmx.com>

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Thanks!
-- 
Gustavo

> ---
>   drivers/net/wireless/intel/iwlegacy/common.c | 4 +---
>   1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/drivers/net/wireless/intel/iwlegacy/common.c b/drivers/net/wireless/intel/iwlegacy/common.c
> index 17570d62c896..9d33a66a49b5 100644
> --- a/drivers/net/wireless/intel/iwlegacy/common.c
> +++ b/drivers/net/wireless/intel/iwlegacy/common.c
> @@ -3438,9 +3438,7 @@ il_init_geos(struct il_priv *il)
>   	if (!channels)
>   		return -ENOMEM;
> 
> -	rates =
> -	    kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
> -		    GFP_KERNEL);
> +	rates = kcalloc(RATE_COUNT_LEGACY, sizeof(*rates), GFP_KERNEL);
>   	if (!rates) {
>   		kfree(channels);
>   		return -ENOMEM;
> --
> 2.25.1
> 
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc()
  2024-01-19 17:16 [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc() Erick Archer
  2024-01-19 17:44 ` Gustavo A. R. Silva
@ 2024-01-21 12:15 ` Stanislaw Gruszka
  2024-01-23 11:51 ` Kalle Valo
  2 siblings, 0 replies; 4+ messages in thread
From: Stanislaw Gruszka @ 2024-01-21 12:15 UTC (permalink / raw)
  To: Erick Archer
  Cc: Kalle Valo, Gustavo A. R. Silva, linux-wireless, linux-kernel,
	linux-hardening

On Fri, Jan 19, 2024 at 06:16:55PM +0100, Erick Archer wrote:
> As noted in the "Deprecated Interfaces, Language Features, Attributes,
> and Conventions" documentation [1], size calculations (especially
> multiplication) should not be performed in memory allocator (or similar)
> function arguments due to the risk of them overflowing. This could lead
> to values wrapping around and a smaller allocation being made than the
> caller was expecting. Using those allocations could lead to linear
> overflows of heap memory and other misbehaviors.
> 
> So, use the purpose specific kcalloc() function instead of the argument
> size * count in the kzalloc() function.
> 
> Also, it is preferred to use sizeof(*pointer) instead of sizeof(type)
> due to the type of the variable can change and one needs not change the
> former (unlike the latter).
> 
> Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/162
> Signed-off-by: Erick Archer <erick.archer@gmx.com>

Acked-by: Stanislaw Gruszka <stf_xl@wp.pl>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc()
  2024-01-19 17:16 [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc() Erick Archer
  2024-01-19 17:44 ` Gustavo A. R. Silva
  2024-01-21 12:15 ` Stanislaw Gruszka
@ 2024-01-23 11:51 ` Kalle Valo
  2 siblings, 0 replies; 4+ messages in thread
From: Kalle Valo @ 2024-01-23 11:51 UTC (permalink / raw)
  To: Erick Archer
  Cc: Stanislaw Gruszka, Gustavo A. R. Silva, Erick Archer,
	linux-wireless, linux-kernel, linux-hardening

Erick Archer <erick.archer@gmx.com> wrote:

> As noted in the "Deprecated Interfaces, Language Features, Attributes,
> and Conventions" documentation [1], size calculations (especially
> multiplication) should not be performed in memory allocator (or similar)
> function arguments due to the risk of them overflowing. This could lead
> to values wrapping around and a smaller allocation being made than the
> caller was expecting. Using those allocations could lead to linear
> overflows of heap memory and other misbehaviors.
> 
> So, use the purpose specific kcalloc() function instead of the argument
> size * count in the kzalloc() function.
> 
> Also, it is preferred to use sizeof(*pointer) instead of sizeof(type)
> due to the type of the variable can change and one needs not change the
> former (unlike the latter).
> 
> Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/162
> Signed-off-by: Erick Archer <erick.archer@gmx.com>
> Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
> Acked-by: Stanislaw Gruszka <stf_xl@wp.pl>

Patch applied to wireless-next.git, thanks.

acf868ff60b1 wifi: iwlegacy: Use kcalloc() instead of kzalloc()

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/20240119171655.7740-1-erick.archer@gmx.com/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-01-23 11:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-01-19 17:16 [PATCH] wifi: iwlegacy: Use kcalloc() instead of kzalloc() Erick Archer
2024-01-19 17:44 ` Gustavo A. R. Silva
2024-01-21 12:15 ` Stanislaw Gruszka
2024-01-23 11:51 ` Kalle Valo

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).