Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwspinlock: stm32: 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 worst scenario, could lead to heap overflows.

Also, address the following sparse warnings:
drivers/hwspinlock/stm32_hwspinlock.c:84:32: warning: using sizeof on a flexible structure

Link: https://github.com/KSPP/linux/issues/174
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Baolin Wang <baolin.wang7@gmail.com>
Signed-off-by: Bjorn Andersson <bjorn.andersson@linaro.org>
Link: https://lore.kernel.org/r/20220125021353.GA29777@embeddedor

authored by

Gustavo A. R. Silva and committed by
Bjorn Andersson
3e5f1ff7 571c3496

+1 -3
+1 -3
drivers/hwspinlock/stm32_hwspinlock.c
··· 73 73 struct device *dev = &pdev->dev; 74 74 struct stm32_hwspinlock *hw; 75 75 void __iomem *io_base; 76 - size_t array_size; 77 76 int i, ret; 78 77 79 78 io_base = devm_platform_ioremap_resource(pdev, 0); 80 79 if (IS_ERR(io_base)) 81 80 return PTR_ERR(io_base); 82 81 83 - array_size = STM32_MUTEX_NUM_LOCKS * sizeof(struct hwspinlock); 84 - hw = devm_kzalloc(dev, sizeof(*hw) + array_size, GFP_KERNEL); 82 + hw = devm_kzalloc(dev, struct_size(hw, bank.lock, STM32_MUTEX_NUM_LOCKS), GFP_KERNEL); 85 83 if (!hw) 86 84 return -ENOMEM; 87 85