Skip to contentSkip to conversation

mfd: ls2kbmc: cancel the reset work before the driver data is freed

PATCH In review hyunjungg 3 participants 3 messages Nov 7

Subsystems (3)
lore
Share & tools
lore

hyunjungg

author
#0 lore raw

PATCH 1/1 mfd: ls2kbmc: cancel the reset work before the driver data is freed

From: Hyunjung Ko <[email protected]>

ls2k_bmc_init() INIT_WORK()s ddata->bmc_reset_work (ls2k_bmc_events_fn) and requests two device-managed interrupts (ls2k_bmc_interrupt) that schedule_work(&ddata->bmc_reset_work). The work handler dereferences ddata via container_of().

ddata is devm_kzalloc()'d, so devres frees it when the device is unbound. Nothing cancels bmc_reset_work: free_irq() (run by devres) only waits for the interrupt handler to finish, not for a work item it has already queued. If a BMC reset interrupt fires shortly before unbind, the queued ls2k_bmc_events_fn() can run after ddata has been freed -> use-after-free.

Register the work for device-managed cancellation with devm_work_autocancel(), before the devm_request_irq() calls that arm it, so devres (LIFO) frees the interrupts first, then cancels the work, and only then frees ddata.

Found by code review; not reproduced (needs Loongson-2K BMC hardware and a reset interrupt racing driver unbind).

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
 drivers/mfd/ls2k-bmc-core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mfd/ls2k-bmc-core.c +5 -1
index 408056b..c44291e 100644
@@ -12,6 +12,7 @@
 #include <linux/aperture.h>
 #include <linux/bitfield.h>
 #include <linux/delay.h>
+#include <linux/devm-helpers.h>
 #include <linux/errno.h>
 #include <linux/init.h>
 #include <linux/iopoll.h>
@@ -371,7 +372,10 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 
 	ls2k_bmc_save_pci_data(pdev, ddata);
 
-	INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
+	ret = devm_work_autocancel(ddata->dev, &ddata->bmc_reset_work,
+				   ls2k_bmc_events_fn);
+	if (ret)
+		return ret;
 
 	ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
 			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] mfd: ls2kbmc: uncapitalized commit subject description after prefix It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] mfd: ls2kbmc: uncapitalized commit subject description after prefix
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] mfd: ls2kbmc: premature acpi_unregister_gsi() in ls2k_bmc_init() [1]
--

commit 9b65d53fe564989b2108b75ef86d6d68901a6034
Author: Hyunjung Ko <[email protected]>

mfd: ls2kbmc: cancel the reset work before the driver data is freed

This commit replaces INIT_WORK() with devm_work_autocancel() in ls2k_bmc_init() so that bmc_reset_work is cancelled before devres frees ddata on driver unbind.

Quoted 1 line

mfd: ls2kbmc: cancel the reset work before the driver data is freed

[Severity: Low]
This isn't a bug, but MFD commit subjects capitalize the description after the prefix (i.e. "mfd: ls2kbmc: Cancel the reset work...").

Quoted 4 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
index 408056bfb2fe7..c44291e7c1c21 100644

[ ... ]

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 381

Quoted 12 lines
drivers/mfd/ls2k-bmc-core.c +4 -1
@@ -371,7 +372,10 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 
 	ls2k_bmc_save_pci_data(pdev, ddata);
 
-	INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
+	ret = devm_work_autocancel(ddata->dev, &ddata->bmc_reset_work,
+				   ls2k_bmc_events_fn);
+	if (ret)
+		return ret;
 
 	ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
 			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);

[Severity: Critical]
This problem wasn't introduced by this patch, but in ls2k_bmc_init() [1], does calling acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI) right after devm_request_irq() tear down the active GPIO IRQ mapping?

drivers/mfd/ls2k-bmc-core.c:ls2k_bmc_init() {
    ...
	gpio_irq = acpi_register_gsi(NULL, LS2K_BMC_RESET_GPIO_GSI, ACPI_EDGE_SENSITIVE,
				     ACPI_ACTIVE_LOW);
	if (gpio_irq < 0)
		return gpio_irq;

	ret = devm_request_irq(ddata->dev, gpio_irq, ls2k_bmc_interrupt,
			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc gpio", ddata);
	if (ret)
		dev_err(ddata->dev, "Failed to request LS2KBMC GPIO IRQ %d.\n", gpio_irq);

	acpi_unregister_gsi(LS2K_BMC_RESET_GPIO_GSI);
	return ret;
}

Unregistering the GSI unconditionally leaves the GPIO watchdog interrupt non-functional and disposes of the IRQ descriptor while devres still tracks it, causing a use-after-free when devm_free_irq() runs on unbind or a later probe failure.

[1] https://sashiko.dev/#/bug/linux-cab8bfb6-4077-46f3-a173-ac2e2042cf66

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to hyunjungg

Hi:

Quoted 42 lines · hyunjungg

From: Hyunjung Ko <[email protected]>

ls2k_bmc_init() INIT_WORK()s ddata->bmc_reset_work (ls2k_bmc_events_fn) and requests two device-managed interrupts (ls2k_bmc_interrupt) that schedule_work(&ddata->bmc_reset_work). The work handler dereferences ddata via container_of().

ddata is devm_kzalloc()'d, so devres frees it when the device is unbound. Nothing cancels bmc_reset_work: free_irq() (run by devres) only waits for the interrupt handler to finish, not for a work item it has already queued. If a BMC reset interrupt fires shortly before unbind, the queued ls2k_bmc_events_fn() can run after ddata has been freed -> use-after-free.

Register the work for device-managed cancellation with devm_work_autocancel(), before the devm_request_irq() calls that arm it, so devres (LIFO) frees the interrupts first, then cancels the work, and only then frees ddata.

Found by code review; not reproduced (needs Loongson-2K BMC hardware and a reset interrupt racing driver unbind).

Fixes d952bba3fbb5 ("mfd: ls2kbmc: Add Loongson-2K BMC reset function support")
---
  drivers/mfd/ls2k-bmc-core.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/mfd/ls2k-bmc-core.c +1 -0
index 408056b..c44291e 100644
@@ -12,6 +12,7 @@
  #include <linux/aperture.h>
  #include <linux/bitfield.h>
  #include <linux/delay.h>
+#include <linux/devm-helpers.h>
  #include <linux/errno.h>
  #include <linux/init.h>
  #include <linux/iopoll.h>

drivers/mfd/ls2k-bmc-core.c static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata) · line 378

Quoted 8 lines
drivers/mfd/ls2k-bmc-core.c +4 -1
@@ -371,7 +372,10 @@ static int ls2k_bmc_init(struct ls2k_bmc_ddata *ddata)
 
  	ls2k_bmc_save_pci_data(pdev, ddata);
 
-	INIT_WORK(&ddata->bmc_reset_work, ls2k_bmc_events_fn);
+	ret = devm_work_autocancel(ddata->dev, &ddata->bmc_reset_work,
+				   ls2k_bmc_events_fn);
+	if (ret)
+		return ret;

Lee had already fixed this issue:

https://lore.kernel.org/all/[email protected]/

Quoted 3 lines
drivers/mfd/ls2k-bmc-core.c +0 -0
  	ret = devm_request_irq(&pdev->dev, pdev->irq, ls2k_bmc_interrupt,
  			       IRQF_SHARED | IRQF_TRIGGER_FALLING, "ls2kbmc pcie", ddata);
Signature · 2 lines
Thanks.
Binbin