usb: gadget: s3c-hsudc: Use struct_size() in devm_kzalloc()
authorGustavo A. R. Silva <gustavoars@kernel.org>
Thu, 13 May 2021 19:33:53 +0000 (14:33 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 21 May 2021 12:05:27 +0000 (14:05 +0200)
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.

This code was detected with the help of Coccinelle and, audited and
fixed manually.

Acked-by: Felipe Balbi <balbi@kernel.org>
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Link: https://lore.kernel.org/r/20210513193353.GA196565@embeddedor
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/gadget/udc/s3c-hsudc.c

index 7bd5182..89f1f8c 100644 (file)
@@ -1220,9 +1220,8 @@ static int s3c_hsudc_probe(struct platform_device *pdev)
        struct s3c24xx_hsudc_platdata *pd = dev_get_platdata(&pdev->dev);
        int ret, i;
 
-       hsudc = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsudc) +
-                       sizeof(struct s3c_hsudc_ep) * pd->epnum,
-                       GFP_KERNEL);
+       hsudc = devm_kzalloc(&pdev->dev, struct_size(hsudc, ep, pd->epnum),
+                            GFP_KERNEL);
        if (!hsudc)
                return -ENOMEM;