Skip to contentSkip to conversation

usb: host: max3421: Fix potential resource leak on IRQ request failure

PATCH New Guangshuo Li 2 participants 2 messages Nov 16 USB

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: host: max3421: Fix potential resource leak on IRQ request failure

If request_irq() fails after usb_add_hcd(), probe drops the HCD reference without removing the registered root hub. The root hub and its HCD reference are left behind.

Remove the HCD before unwinding the SPI thread. Keep the transfer buffers and platform data alive until the thread has stopped.

This issue was found by our static analysis tool under development.

Fixes 2d53139f3162 ("Add support for using a MAX3421E chip as a host driver.")
---
 drivers/usb/host/max3421-hcd.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/usb/host/max3421-hcd.c +9 -8
index 73e76d0e6973..09ba92c41a0d 100644
@@ -1911,23 +1911,24 @@ max3421_probe(struct spi_device *spi)
 			     IRQF_TRIGGER_LOW, "max3421", hcd);
 	if (retval < 0) {
 		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
-		goto error;
+		goto remove_hcd;
 	}
 	return 0;
 
+remove_hcd:
+	usb_remove_hcd(hcd);
 error:
-	if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
-		devm_kfree(&spi->dev, pdata);
-		spi->dev.platform_data = NULL;
-	}
-
 	if (hcd) {
-		kfree(max3421_hcd->tx);
-		kfree(max3421_hcd->rx);
 		if (!IS_ERR_OR_NULL(max3421_hcd->spi_thread))
 			kthread_stop(max3421_hcd->spi_thread);
+		kfree(max3421_hcd->tx);
+		kfree(max3421_hcd->rx);
 		usb_put_hcd(hcd);
 	}
+	if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
+		devm_kfree(&spi->dev, pdata);
+		spi->dev.platform_data = NULL;
+	}
 	return retval;
 }
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] usb: host: max3421-hcd…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] usb: host: max3421-hcd: use-after-free in max3421_irq_handler() [1]
--

commit bf9ea93a3f4c8766b32b8f1ff53f8a0863775270
Author: Guangshuo Li <[email protected]>

usb: host: max3421: Fix potential resource leak on IRQ request failure

This commit calls usb_remove_hcd() when request_irq() fails in max3421_probe() and reorders the error cleanup so the SPI thread is stopped before freeing the transfer buffers and platform data.

Quoted 16 lines
drivers/usb/host/max3421-hcd.c +3 -1
index 73e76d0e69738..09ba92c41a0d5 100644
@@ -1911,23 +1911,24 @@ max3421_probe(struct spi_device *spi)
 			     IRQF_TRIGGER_LOW, "max3421", hcd);
 	if (retval < 0) {
 		dev_err(&spi->dev, "failed to request irq %d\n", spi->irq);
-		goto error;
+		goto remove_hcd;
 	}
 	return 0;
 
+remove_hcd:
+	usb_remove_hcd(hcd);
 error:

drivers/usb/host/max3421-hcd.c max3421_probe(struct spi_device *spi) · line 1923

Quoted 10 lines
drivers/usb/host/max3421-hcd.c +0 -7
-	if (IS_ENABLED(CONFIG_OF) && dev->of_node && pdata) {
-		devm_kfree(&spi->dev, pdata);
-		spi->dev.platform_data = NULL;
-	}
-
 	if (hcd) {
-		kfree(max3421_hcd->tx);
-		kfree(max3421_hcd->rx);
 		if (!IS_ERR_OR_NULL(max3421_hcd->spi_thread))
 			kthread_stop(max3421_hcd->spi_thread);

[Severity: Critical]
This problem wasn't introduced by this patch, but can max3421_remove() trigger a use-after-free on max3421_hcd->spi_thread in max3421_irq_handler() [1]?

CPU0 CPU1
max3421_remove()
kthread_stop(max3421_hcd->spi_thread)

                                        max3421_irq_handler()
                                          if (max3421_hcd->spi_thread)
                                            wake_up_process(...)
                                              <-- use-after-free

free_irq(spi->irq, hcd)

max3421_remove() stops max3421_hcd->spi_thread (which drops the task_struct reference) without clearing the pointer, before calling free_irq(). An interrupt firing in that window wakes the freed task_struct. Calling free_irq() before kthread_stop() in max3421_remove() would avoid this race.

[1] https://sashiko.dev/#/bug/linux-1a37fb82-5226-4771-845f-e2e6c320c91c

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1