Skip to contentSkip to conversation

dmaengine: stm32-mdma: Use struct_size() helper in devm_kzalloc()

PATCH Applied Gustavo A. R. Silva 4 participants 4 messages Sep 29, 2021 – Oct 1, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: stm32-mdma: Use struct_size() helper in devm_kzalloc()

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worse scenario, could lead to heap overflows.

---
 drivers/dma/stm32-mdma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/stm32-mdma.c +2 -1
index 18cbd1e43c2e..d30a4a28d3bf 100644
@@ -1566,7 +1566,8 @@ static int stm32_mdma_probe(struct platform_device *pdev)
 	if (count < 0)
 		count = 0;
 
-	dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev) + sizeof(u32) * count,
+	dmadev = devm_kzalloc(&pdev->dev,
+			      struct_size(dmadev, ahb_addr_masks, count),
 			      GFP_KERNEL);
 	if (!dmadev)
 		return -ENOMEM;
Signature · 1 line
2.27.0
Reviewed-by on v1 1/1
#2 lore raw
In reply to Gustavo A. R. Silva
Quoted 6 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worse scenario, could lead to heap overflows.

Quoted 19 lines
---
  drivers/dma/stm32-mdma.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/dma/stm32-mdma.c +2 -1
index 18cbd1e43c2e..d30a4a28d3bf 100644
@@ -1566,7 +1566,8 @@ static int stm32_mdma_probe(struct platform_device *pdev)
  	if (count < 0)
  		count = 0;
 
-	dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev) + sizeof(u32) * count,
+	dmadev = devm_kzalloc(&pdev->dev,
+			      struct_size(dmadev, ahb_addr_masks, count),
  			      GFP_KERNEL);
  	if (!dmadev)
  		return -ENOMEM;
maintainer Applied
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 3 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version, in order to avoid any potential type mistakes or integer overflows that, in the worse scenario, could lead to heap overflows.

Applied, thanks

Signature · 1 line
~Vinod