netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic
@ 2024-02-24 18:19 Erick Archer
  2024-02-24 21:39 ` Sergey Ryazanov
  2024-02-28  2:20 ` patchwork-bot+netdevbpf
  0 siblings, 2 replies; 3+ messages in thread
From: Erick Archer @ 2024-02-24 18:19 UTC (permalink / raw)
  To: Chandrashekar Devegowda, Chiranjeevi Rapolu, Liu Haijun,
	M Chetan Kumar, Ricardo Martinez, Loic Poulain, Sergey Ryazanov,
	Johannes Berg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Gustavo A. R. Silva, Kees Cook
  Cc: Erick Archer, netdev, linux-kernel, linux-hardening

This is an effort to get rid of all multiplications from allocation
functions in order to prevent integer overflows [1][2].

As the "port_prox" variable is a pointer to "struct port_proxy" and
this structure ends in a flexible array:

struct port_proxy {
	[...]
	struct t7xx_port ports[];
};

the preferred way in the kernel is to use the struct_size() helper to
do the arithmetic instead of the argument "size + size * count" in the
devm_kzalloc() function.

This way, the code is more readable and safer.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/160 [2]

Signed-off-by: Erick Archer <erick.archer@gmx.com>
---
 drivers/net/wwan/t7xx/t7xx_port_proxy.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/drivers/net/wwan/t7xx/t7xx_port_proxy.c b/drivers/net/wwan/t7xx/t7xx_port_proxy.c
index 8f5e01705af2..7d6388bf1d7c 100644
--- a/drivers/net/wwan/t7xx/t7xx_port_proxy.c
+++ b/drivers/net/wwan/t7xx/t7xx_port_proxy.c
@@ -543,8 +543,10 @@ static int t7xx_proxy_alloc(struct t7xx_modem *md)
 	struct device *dev = &md->t7xx_dev->pdev->dev;
 	struct port_proxy *port_prox;

-	port_prox = devm_kzalloc(dev, sizeof(*port_prox) +
-				 sizeof(struct t7xx_port) * T7XX_MAX_POSSIBLE_PORTS_NUM,
+	port_prox = devm_kzalloc(dev,
+				 struct_size(port_prox,
+					     ports,
+					     T7XX_MAX_POSSIBLE_PORTS_NUM),
 				 GFP_KERNEL);
 	if (!port_prox)
 		return -ENOMEM;
--
2.25.1


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

* Re: [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic
  2024-02-24 18:19 [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic Erick Archer
@ 2024-02-24 21:39 ` Sergey Ryazanov
  2024-02-28  2:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: Sergey Ryazanov @ 2024-02-24 21:39 UTC (permalink / raw)
  To: Erick Archer, Chandrashekar Devegowda, Chiranjeevi Rapolu,
	Liu Haijun, M Chetan Kumar, Ricardo Martinez, Loic Poulain,
	Johannes Berg, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Gustavo A. R. Silva, Kees Cook
  Cc: netdev, linux-kernel, linux-hardening

On 24.02.2024 20:19, Erick Archer wrote:
> This is an effort to get rid of all multiplications from allocation
> functions in order to prevent integer overflows [1][2].
> 
> As the "port_prox" variable is a pointer to "struct port_proxy" and
> this structure ends in a flexible array:
> 
> struct port_proxy {
> 	[...]
> 	struct t7xx_port ports[];
> };
> 
> the preferred way in the kernel is to use the struct_size() helper to
> do the arithmetic instead of the argument "size + size * count" in the
> devm_kzalloc() function.
> 
> This way, the code is more readable and safer.
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/160 [2]
> 
> Signed-off-by: Erick Archer <erick.archer@gmx.com>

Reviewed-by: Sergey Ryazanov <ryazanov.s.a@gmail.com>

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

* Re: [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic
  2024-02-24 18:19 [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic Erick Archer
  2024-02-24 21:39 ` Sergey Ryazanov
@ 2024-02-28  2:20 ` patchwork-bot+netdevbpf
  1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-28  2:20 UTC (permalink / raw)
  To: Erick Archer
  Cc: chandrashekar.devegowda, chiranjeevi.rapolu, haijun.liu,
	m.chetan.kumar, ricardo.martinez, loic.poulain, ryazanov.s.a,
	johannes, davem, edumazet, kuba, pabeni, gustavoars, keescook,
	netdev, linux-kernel, linux-hardening

Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

On Sat, 24 Feb 2024 19:19:32 +0100 you wrote:
> This is an effort to get rid of all multiplications from allocation
> functions in order to prevent integer overflows [1][2].
> 
> As the "port_prox" variable is a pointer to "struct port_proxy" and
> this structure ends in a flexible array:
> 
> struct port_proxy {
> 	[...]
> 	struct t7xx_port ports[];
> };
> 
> [...]

Here is the summary with links:
  - net: wwan: t7xx: Prefer struct_size over open coded arithmetic
    https://git.kernel.org/netdev/net-next/c/848e34ca2030

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

end of thread, other threads:[~2024-02-28  2:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-24 18:19 [PATCH] net: wwan: t7xx: Prefer struct_size over open coded arithmetic Erick Archer
2024-02-24 21:39 ` Sergey Ryazanov
2024-02-28  2:20 ` patchwork-bot+netdevbpf

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