usb: gadget: aspeed_udc: check endpoint DMA allocation
Subsystems (3)
PATCH 1/1 usb: gadget: aspeed_udc: check endpoint DMA allocation
ast_udc_probe() allocates a coherent DMA buffer used as the backing store for endpoint buffers. ast_udc_init_ep() derives per-endpoint buffer pointers from udc->ep0_buf, so a failed allocation is dereferenced during probe.
Check the allocation before endpoint setup. The existing probe error path called ast_udc_remove(), which unregisters the gadget unconditionally and is not safe before usb_add_gadget_udc() succeeds. Add a local cleanup helper for probe failures so pre-registration failures only unwind the resources that were actually initialized.
This was found by a local static analysis checker for unchecked allocator returns while scanning Linux 6.16. The change was checked by applying it to current mainline and by running checkpatch. I do not have access to Aspeed UDC hardware, so no runtime testing was performed.
---
Note: a 2022 patch attempted to add only a NULL check for this allocation:
https://lore.kernel.org/all/[email protected]/
This version also fixes the probe unwind path so the clock is disabled on allocation failure and usb_del_gadget_udc() is not called before the gadget has been registered.
drivers/usb/gadget/udc/aspeed_udc.c +39 -23
@@ -1434,11 +1434,34 @@ static void ast_udc_init_hw(struct ast_udc_dev *udc)ast_udc_write (udc,0 ,AST_UDC_EP0_CTRL ); } +static void ast_udc_cleanup (struct platform_device *pdev) +{ +struct ast_udc_dev *udc =platform_get_drvdata (pdev); +unsigned long flags; +u32 ctrl; + +spin_lock_irqsave (&udc->lock, flags); + +/* Disable upstream port connection */ + ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; +ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); + +clk_disable_unprepare (udc->clk); + +spin_unlock_irqrestore (&udc->lock, flags); + +if (udc->ep0_buf) +dma_free_coherent (&pdev->dev, +AST_UDC_EP_DMA_SIZE *AST_UDC_NUM_ENDPOINTS , + udc->ep0_buf, + udc->ep0_buf_dma); + + udc->ep0_buf =NULL ; +} +static void ast_udc_remove (struct platform_device *pdev) {struct ast_udc_dev *udc =platform_get_drvdata (pdev); -unsigned long flags; -u32 ctrl;usb_del_gadget_udc (&udc->gadget);if (udc->driver) {
@@ -1453,23 +1476,7 @@ static void ast_udc_remove(struct platform_device *pdev)return ; } -spin_lock_irqsave (&udc->lock, flags); - -/* Disable upstream port connection */ - ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; -ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); - -clk_disable_unprepare (udc->clk); - -spin_unlock_irqrestore (&udc->lock, flags); - -if (udc->ep0_buf) -dma_free_coherent (&pdev->dev, -AST_UDC_EP_DMA_SIZE *AST_UDC_NUM_ENDPOINTS , - udc->ep0_buf, - udc->ep0_buf_dma); - - udc->ep0_buf =NULL ; +ast_udc_cleanup (pdev); }static int ast_udc_probe (struct platform_device *pdev)
@@ -1523,6 +1530,10 @@ static int ast_udc_probe(struct platform_device *pdev)AST_UDC_EP_DMA_SIZE *AST_UDC_NUM_ENDPOINTS , &udc->ep0_buf_dma, GFP_KERNEL); +if (!udc->ep0_buf) { + rc = -ENOMEM ; + goto err_disable_clk; + } udc->gadget.speed =USB_SPEED_UNKNOWN ; udc->gadget.max_speed =USB_SPEED_HIGH ;
@@ -1553,20 +1564,20 @@ static int ast_udc_probe(struct platform_device *pdev) udc->irq =platform_get_irq (pdev,0 );if (udc->irq <0 ) { rc = udc->irq; - goto err; + goto err_cleanup; } rc =devm_request_irq (&pdev->dev, udc->irq, ast_udc_isr,0 ,KBUILD_MODNAME , udc);if (rc) {dev_err (&pdev->dev,"Failed to request interrupt\n" ); - goto err; + goto err_cleanup; } rc =usb_add_gadget_udc (&pdev->dev, &udc->gadget);if (rc) {dev_err (&pdev->dev,"Failed to add gadget udc\n" ); - goto err; + goto err_cleanup; } dev_info(&pdev->dev, "Initialized udc in USB%s mode\n",
@@ -1574,9 +1585,14 @@ static int ast_udc_probe(struct platform_device *pdev)return 0 ; +err_disable_clk: +clk_disable_unprepare (udc->clk); + goto err; +err_cleanup: +ast_udc_cleanup (pdev); + goto err; err:dev_err (&pdev->dev,"Failed to udc probe, rc:0x%x\n" , rc); -ast_udc_remove (pdev);return rc; }
Signature · 1 line
2.51.0
Andrew Jeffery
maintainerQuoted 138 lines · Ruoyu Wang
ast_udc_probe()allocates a coherent DMA buffer used as the backing store for endpoint buffers.ast_udc_init_ep()derives per-endpoint buffer pointers fromudc->ep0_buf, so a failed allocation is dereferenced during probe.Check the allocation before endpoint setup. The existing probe error path called
ast_udc_remove(), which unregisters the gadget unconditionally and is not safe beforeusb_add_gadget_udc()succeeds. Add a local cleanup helper for probe failures so pre-registration failures only unwind the resources that were actually initialized.This was found by a local static analysis checker for unchecked allocator returns while scanning Linux 6.16. The change was checked by applying it to current mainline and by running checkpatch. I do not have access to Aspeed UDC hardware, so no runtime testing was performed.
Fixes 055276c13205 ("usb: gadget: add Aspeed ast2600 udc driver")Signed-off-by Ruoyu Wang <[email protected]>---
Note: a 2022 patch attempted to add only a NULL check for this allocation:
https://lore.kernel.org/all/[email protected]/This version also fixes the probe unwind path so the clock is disabled on allocation failure and
usb_del_gadget_udc()is not called before the gadget has been registered.drivers/usb/gadget/udc/aspeed_udc.c +0
-0@@ -1434,11 +1434,34 @@ static void ast_udc_init_hw(struct ast_udc_dev *udc) ast_udc_write(udc, 0, AST_UDC_EP0_CTRL); }drivers/usb/gadget/udc/aspeed_udc.c +25
-0+static void ast_udc_cleanup (struct platform_device *pdev) +{ +struct ast_udc_dev *udc =platform_get_drvdata (pdev); +unsigned long flags; +u32 ctrl; + +spin_lock_irqsave (&udc->lock, flags); + +/* Disable upstream port connection */ + ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; +ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); + +clk_disable_unprepare (udc->clk); + +spin_unlock_irqrestore (&udc->lock, flags); + +if (udc->ep0_buf) +dma_free_coherent (&pdev->dev, + AST_UDC_EP_DMA_SIZE * AST_UDC_NUM_ENDPOINTS, + udc->ep0_buf, + udc->ep0_buf_dma); + + udc->ep0_buf =NULL ; +} +static void
ast_udc_remove(struct platform_device *pdev){
structast_udc_dev*udc =platform_get_drvdata(pdev);drivers/usb/gadget/udc/aspeed_udc.c +0
-2-unsigned long flags; -u32 ctrl;
usb_del_gadget_udc(&udc->gadget);
if (udc->driver) {drivers/usb/gadget/udc/aspeed_udc.c +0
-0@@ -1453,23 +1476,7 @@ static void ast_udc_remove(struct platform_device *pdev) return; }drivers/usb/gadget/udc/aspeed_udc.c +1
-17-spin_lock_irqsave (&udc->lock, flags); - -/* Disable upstream port connection */ - ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; -ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); - -clk_disable_unprepare (udc->clk); - -spin_unlock_irqrestore (&udc->lock, flags); - -if (udc->ep0_buf) -dma_free_coherent (&pdev->dev, - AST_UDC_EP_DMA_SIZE * AST_UDC_NUM_ENDPOINTS, - udc->ep0_buf, - udc->ep0_buf_dma); - - udc->ep0_buf =NULL ; +ast_udc_cleanup (pdev);}
static intast_udc_probe(struct platform_device *pdev)drivers/usb/gadget/udc/aspeed_udc.c +0
-0@@ -1523,6 +1530,10 @@ static int ast_udc_probe(struct platform_device *pdev)
AST_UDC_EP_DMA_SIZE*AST_UDC_NUM_ENDPOINTS,&udc->ep0_buf_dma,GFP_KERNEL);drivers/usb/gadget/udc/aspeed_udc.c +4
-0+if (!udc->ep0_buf) { + rc = -ENOMEM ; + goto err_disable_clk; + }
udc->gadget.speed=USB_SPEED_UNKNOWN;
udc->gadget.max_speed=USB_SPEED_HIGH;drivers/usb/gadget/udc/aspeed_udc.c +1
-1@@ -1553,20 +1564,20 @@ static int ast_udc_probe(struct platform_device *pdev) udc->irq = platform_get_irq(pdev, 0); if (udc->irq < 0) { rc = udc->irq; - goto err; + goto err_cleanup; }
rc =devm_request_irq(&pdev->dev, udc->irq, ast_udc_isr, 0, KBUILD_MODNAME, udc);
if (rc) {
dev_err(&pdev->dev, "Failed to request interrupt\n");drivers/usb/gadget/udc/aspeed_udc.c +1
-1- goto err; + goto err_cleanup;}
rc =usb_add_gadget_udc(&pdev->dev, &udc->gadget); if (rc) {
dev_err(&pdev->dev, "Failed to add gadget udc\n");drivers/usb/gadget/udc/aspeed_udc.c +1
-1- goto err; + goto err_cleanup;}
dev_info(&pdev->dev, "Initialized udc in USB%s mode\n",drivers/usb/gadget/udc/aspeed_udc.c +0
-0@@ -1574,9 +1585,14 @@ static int ast_udc_probe(struct platform_device *pdev)
return 0;drivers/usb/gadget/udc/aspeed_udc.c +6
-0+err_disable_clk: +clk_disable_unprepare (udc->clk); + goto err; +err_cleanup: +ast_udc_cleanup (pdev); + goto err;
Quoted 1 line
err:
That last goto is unnecessary.
However, I find it unsettling that in a patch fixing resource handling we add a mildly convoluted cleanup path, with portions jumping over each other in this way.
The err_disable_clk label is only used once, and itself jumps down to the err label. This is the case because beyond its goto we free udc-
Quoted 1 line
ep0_bufinast_udc_cleanup(). I think it would make more sense to move
the call to clk_disable_unprepare() into the conditional body of the allocation failure test, then change its goto label to 'err'. That way the above hunk becomes:
+err_cleanup:
+ ast_udc_cleanup(pdev);
err:
...Which seems a bit more natural.
Andrew
Quoted 5 lines
dev_err(&pdev->dev, "Failed to udc probe, rc:0x%x\n", rc);drivers/usb/gadget/udc/aspeed_udc.c +0
-1-ast_udc_remove (pdev);
return rc;
}
Thanks! I will simplify the error path and let err_cleanup fall through to err.
Quoted 177 lines · Andrew Jeffery
Quoted 23 lines · Ruoyu Wang
ast_udc_probe()allocates a coherent DMA buffer used as the backing store for endpoint buffers.ast_udc_init_ep()derives per-endpoint buffer pointers fromudc->ep0_buf, so a failed allocation is dereferenced during probe.Check the allocation before endpoint setup. The existing probe error path called
ast_udc_remove(), which unregisters the gadget unconditionally and is not safe beforeusb_add_gadget_udc()succeeds. Add a local cleanup helper for probe failures so pre-registration failures only unwind the resources that were actually initialized.This was found by a local static analysis checker for unchecked allocator returns while scanning Linux 6.16. The change was checked by applying it to current mainline and by running checkpatch. I do not have access to Aspeed UDC hardware, so no runtime testing was performed.
Fixes 055276c13205 ("usb: gadget: add Aspeed ast2600 udc driver")Signed-off-by Ruoyu Wang <[email protected]>---
Note: a 2022 patch attempted to add only a NULL check for this allocation:
https://lore.kernel.org/all/[email protected]/Review
Quoted 5 lines
This version also fixes the probe unwind path so the clock is disabled on allocation failure and
usb_del_gadget_udc()is not called before the gadget has been registered.(quoted diff) +0
-0
b/drivers/usb/gadget/udc/aspeed_udc.cdrivers/usb/gadget/udc/aspeed_udc.c static void ast_udc_init_hw(struct ast_udc_dev
Quoted 4 lines
index
7fc6696b7..809a7d5b7100644drivers/usb/gadget/udc/aspeed_udc.c +0
-0@@ -1434,11 +1434,34 @@ static void ast_udc_init_hw(struct ast_udc_dev*udc)
Quoted 12 lines
drivers/usb/gadget/udc/aspeed_udc.c +9
-0ast_udc_write (udc,0 ,AST_UDC_EP0_CTRL ); } +static void ast_udc_cleanup (struct platform_device *pdev) +{ +struct ast_udc_dev *udc =platform_get_drvdata (pdev); +unsigned long flags; +u32 ctrl; + +spin_lock_irqsave (&udc->lock, flags); + +/* Disable upstream port connection */ drivers/usb/gadget/udc/aspeed_udc.c static void ast_udc_init_hw(struct ast_udc_dev *udc)
Quoted 10 lines
drivers/usb/gadget/udc/aspeed_udc.c +10
-0+ ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; +ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); + +clk_disable_unprepare (udc->clk); + +spin_unlock_irqrestore (&udc->lock, flags); + +if (udc->ep0_buf) + dma_free_coherent(&pdev->dev, + AST_UDC_EP_DMA_SIZE *
AST_UDC_NUM_ENDPOINTS,drivers/usb/gadget/udc/aspeed_udc.c static void ast_udc_remove(struct platform_device
Quoted 15 lines
drivers/usb/gadget/udc/aspeed_udc.c +6
-2+ udc->ep0_buf, + udc->ep0_buf_dma); + + udc->ep0_buf =NULL ; +} +static void ast_udc_remove (struct platform_device *pdev) {struct ast_udc_dev *udc =platform_get_drvdata (pdev); -unsigned long flags; -u32 ctrl;usb_del_gadget_udc (&udc->gadget);if (udc->driver) {@@ -1453,23 +1476,7 @@ static void ast_udc_remove(struct platform_device*pdev)
Quoted 6 lines
drivers/usb/gadget/udc/aspeed_udc.c +0
-3return ; } -spin_lock_irqsave (&udc->lock, flags); - -/* Disable upstream port connection */ drivers/usb/gadget/udc/aspeed_udc.c static void ast_udc_remove(struct platform_device *pdev)
Quoted 10 lines
drivers/usb/gadget/udc/aspeed_udc.c +0
-10- ctrl =ast_udc_read (udc,AST_UDC_FUNC_CTRL ) & ~USB_UPSTREAM_EN ; -ast_udc_write (udc, ctrl,AST_UDC_FUNC_CTRL ); - -clk_disable_unprepare (udc->clk); - -spin_unlock_irqrestore (&udc->lock, flags); - -if (udc->ep0_buf) - dma_free_coherent(&pdev->dev, - AST_UDC_EP_DMA_SIZE *
AST_UDC_NUM_ENDPOINTS,drivers/usb/gadget/udc/aspeed_udc.c static int ast_udc_probe(struct platform_device
Quoted 9 lines
drivers/usb/gadget/udc/aspeed_udc.c +1
-4- udc->ep0_buf, - udc->ep0_buf_dma); - - udc->ep0_buf =NULL ; +ast_udc_cleanup (pdev); }static int ast_udc_probe (struct platform_device *pdev)@@ -1523,6 +1530,10 @@ static int ast_udc_probe(struct platform_device*pdev)
drivers/usb/gadget/udc/aspeed_udc.c static int ast_udc_probe(struct platform_device
Quoted 11 lines
drivers/usb/gadget/udc/aspeed_udc.c +4
-0AST_UDC_EP_DMA_SIZE *AST_UDC_NUM_ENDPOINTS , &udc->ep0_buf_dma, GFP_KERNEL); +if (!udc->ep0_buf) { + rc = -ENOMEM ; + goto err_disable_clk; + } udc->gadget.speed =USB_SPEED_UNKNOWN ; udc->gadget.max_speed =USB_SPEED_HIGH ;@@ -1553,20 +1564,20 @@ static int ast_udc_probe(struct platform_device*pdev)
drivers/usb/gadget/udc/aspeed_udc.c static int ast_udc_probe(struct platform_device
Quoted 24 lines
drivers/usb/gadget/udc/aspeed_udc.c +3
-3udc->irq =platform_get_irq (pdev,0 );if (udc->irq <0 ) { rc = udc->irq; - goto err; + goto err_cleanup; } rc =devm_request_irq (&pdev->dev, udc->irq, ast_udc_isr,0 ,KBUILD_MODNAME , udc);if (rc) {dev_err (&pdev->dev,"Failed to request interrupt\n" ); - goto err; + goto err_cleanup; } rc =usb_add_gadget_udc (&pdev->dev, &udc->gadget);if (rc) {dev_err (&pdev->dev,"Failed to add gadget udc\n" ); - goto err; + goto err_cleanup; } dev_info(&pdev->dev, "Initialized udc in USB%s mode\n",@@ -1574,9 +1585,14 @@ static int ast_udc_probe(struct platform_device*pdev)
drivers/usb/gadget/udc/aspeed_udc.c static int ast_udc_probe(struct platform_device *pdev) · line 1594
Quoted 10 lines
drivers/usb/gadget/udc/aspeed_udc.c +6
-0return 0 ; +err_disable_clk: +clk_disable_unprepare (udc->clk); + goto err; +err_cleanup: +ast_udc_cleanup (pdev); + goto err; err:That last goto is unnecessary.
However, I find it unsettling that in a patch fixing resource handling we add a mildly convoluted cleanup path, with portions jumping over each other in this way.
The
err_disable_clklabel is only used once, and itself jumps down to the err label. This is the case because beyond its goto we free udc-Quoted 1 line
ep0_bufinast_udc_cleanup(). I think it would make more sense to movethe call to
clk_disable_unprepare()into the conditional body of the allocation failure test, then change its goto label to 'err'. That way the above hunk becomes:+err_cleanup:
+ast_udc_cleanup(pdev);err: ...Which seems a bit more natural.
Andrew
Quoted 5 lines
dev_err(&pdev->dev, "Failed to udc probe, rc:0x%x\n", rc);-
ast_udc_remove(pdev);return rc;}
PATCH v2 1/1 usb: gadget: aspeed_udc: check endpoint DMA allocation
In reply to Ruoyu Wangast_udc_probe() allocates a coherent DMA buffer used as the backing store for endpoint buffers. ast_udc_init_ep() derives per-endpoint buffer pointers from udc->ep0_buf, so a failed allocation is dereferenced during probe.
Check the allocation before endpoint setup. The existing probe error path called ast_udc_remove(), which unregisters the gadget unconditionally and is not safe before usb_add_gadget_udc() succeeds. Add a local cleanup helper for probe failures so pre-registration failures only unwind the resources that were actually initialized.
This was found by a local static analysis checker for unchecked allocator returns while scanning Linux 6.16. The change was checked by applying it to current mainline and by running checkpatch. I do not have access to Aspeed UDC hardware, so no runtime testing was performed.
---
Note: a 2022 patch attempted to add only a NULL check for this allocation:
https://lore.kernel.org/all/[email protected]/
This version also fixes the probe unwind path so the clock is disabled on allocation failure and usb_del_gadget_udc() is not called before the gadget has been registered.
v1:
https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Simplify the allocation-failure cleanup path as suggested by Andrew
Jeffery: disable the clock in the allocation-failure branch and let err_cleanup fall through to err.
drivers/usb/gadget/udc/aspeed_udc.c +32 -18
@@ -1434,25 +1434,12 @@ static void ast_udc_init_hw(struct ast_uast_udc_write (udc,0 ,AST_UDC_EP0_CTRL ); } -static void ast_udc_remove (struct platform_device *pdev) +static void ast_udc_cleanup (struct platform_device *pdev) {struct ast_udc_dev *udc =platform_get_drvdata (pdev);unsigned long flags;u32 ctrl; -usb_del_gadget_udc (&udc->gadget); -if (udc->driver) { -/* -* This is broken as only some cleanup is skipped, *udev is -* freed and the register mapping goes away. Any further usage -* probably crashes. Also the device is unbound, so the skipped -* cleanup is never catched up later. -*/ -dev_alert (&pdev->dev, -"Driver is busy and still going away. Fasten your seat belts!\n" ); -return ; - } -spin_lock_irqsave (&udc->lock, flags);/* Disable upstream port connection */
@@ -1472,6 +1459,26 @@ static void ast_udc_remove(struct platfo udc->ep0_buf =NULL ; } +static void ast_udc_remove (struct platform_device *pdev) +{ +struct ast_udc_dev *udc =platform_get_drvdata (pdev); + +usb_del_gadget_udc (&udc->gadget); +if (udc->driver) { +/* +* This is broken as only some cleanup is skipped, *udev is +* freed and the register mapping goes away. Any further usage +* probably crashes. Also the device is unbound, so the skipped +* cleanup is never catched up later. +*/ +dev_alert (&pdev->dev, +"Driver is busy and still going away. Fasten your seat belts!\n" ); +return ; + } + +ast_udc_cleanup (pdev); +} +static int ast_udc_probe (struct platform_device *pdev) {enum usb_device_speed max_speed;
@@ -1524,6 +1531,12 @@ static int ast_udc_probe(struct platformAST_UDC_NUM_ENDPOINTS , &udc->ep0_buf_dma, GFP_KERNEL); +if (!udc->ep0_buf) { +clk_disable_unprepare (udc->clk); + rc = -ENOMEM ; + goto err; + } + udc->gadget.speed =USB_SPEED_UNKNOWN ; udc->gadget.max_speed =USB_SPEED_HIGH ; udc->creq = udc->reg +AST_UDC_SETUP0 ;
@@ -1553,20 +1566,20 @@ static int ast_udc_probe(struct platform udc->irq =platform_get_irq (pdev,0 );if (udc->irq <0 ) { rc = udc->irq; - goto err; + goto err_cleanup; } rc =devm_request_irq (&pdev->dev, udc->irq, ast_udc_isr,0 ,KBUILD_MODNAME , udc);if (rc) {dev_err (&pdev->dev,"Failed to request interrupt\n" ); - goto err; + goto err_cleanup; } rc =usb_add_gadget_udc (&pdev->dev, &udc->gadget);if (rc) {dev_err (&pdev->dev,"Failed to add gadget udc\n" ); - goto err; + goto err_cleanup; } dev_info(&pdev->dev, "Initialized udc in USB%s mode\n",
@@ -1574,9 +1587,10 @@ static int ast_udc_probe(struct platformreturn 0 ; +err_cleanup: +ast_udc_cleanup (pdev); err:dev_err (&pdev->dev,"Failed to udc probe, rc:0x%x\n" , rc); -ast_udc_remove (pdev);return rc; }
Quoted 11 lines · Ruoyu Wang
ast_udc_probe()allocates a coherent DMA buffer used as the backing store for endpoint buffers.ast_udc_init_ep()derives per-endpoint buffer pointers fromudc->ep0_buf, so a failed allocation is dereferenced during probe.Check the allocation before endpoint setup. The existing probe error path called
ast_udc_remove(), which unregisters the gadget unconditionally and is not safe beforeusb_add_gadget_udc()succeeds. Add a local cleanup helper for probe failures so pre-registration failures only unwind the resources that were actually initialized.
Quoted 7 lines
This was found by a local static analysis checker for unchecked allocator returns while scanning Linux 6.16. The change was checked by applying it to current mainline and by running checkpatch. I do not have access to Aspeed UDC hardware, so no runtime testing was performed.
Fixes 055276c13205 ("usb: gadget: add Aspeed ast2600 udc driver")Signed-off-by Ruoyu Wang <[email protected]>