Skip to contentSkip to conversation

hwmon: (gpio-fan) fix use-after-free of alarm_work on unbind

PATCH In review Cong Nguyen 2 participants 2 messages Sep 15 – Sep 16 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 hwmon: (gpio-fan) fix use-after-free of alarm_work on unbind

fan_alarm_irq_handler() schedules alarm_work, but nothing ever cancels it. free_irq() (via devm) only waits for an in-progress IRQ handler, not queued work -- a pending alarm_work can run after fan_data is devm-freed, dereferencing it in fan_alarm_notify().

Cancel it via a devm action registered before devm_request_irq(), so teardown frees the IRQ first, then cancels whatever's already queued. Guenter pointed out cancel_work_sync() still leaves a window if something schedules the work again after it's canceled; use disable_work_sync() instead, which permanently disables it too.

Fixes d6fe1360f42e ("hwmon: add generic GPIO fan driver")
Assisted-by Claude:claude-opus-4

---
Changes in v2:
- Use disable_work_sync() instead of cancel_work_sync(), per Guenter:
cancel_work_sync() only drains the current instance, so anything that calls schedule_work() again afterward would still requeue it. disable_work_sync() permanently disables the work item too.

 drivers/hwmon/gpio-fan.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/hwmon/gpio-fan.c +19 -1
index 084828e1e281..28cfd50aa859 100644
@@ -81,9 +81,16 @@ static ssize_t fan1_alarm_show(struct device *dev,
 
 static DEVICE_ATTR_RO(fan1_alarm);
 
+static void gpio_fan_cancel_alarm_work(void *data)
+{
+	struct gpio_fan_data *fan_data = data;
+
+	disable_work_sync(&fan_data->alarm_work);
+}
+
 static int fan_alarm_init(struct gpio_fan_data *fan_data)
 {
-	int alarm_irq;
+	int alarm_irq, err;
 	struct device *dev = fan_data->dev;
 
 	/*
@@ -95,6 +102,17 @@ static int fan_alarm_init(struct gpio_fan_data *fan_data)
 		return 0;
 
 	INIT_WORK(&fan_data->alarm_work, fan_alarm_notify);
+
+	/*
+	 * Register before devm_request_irq() below: LIFO teardown must free
+	 * the IRQ (stopping new schedule_work() calls) before this cancels
+	 * whatever alarm_work is already queued or running.
+	 */
+	err = devm_add_action_or_reset(dev, gpio_fan_cancel_alarm_work,
+					fan_data);
+	if (err)
+		return err;
+
 	irq_set_irq_type(alarm_irq, IRQ_TYPE_EDGE_BOTH);
 	return devm_request_irq(dev, alarm_irq, fan_alarm_irq_handler,
 				IRQF_SHARED, "GPIO fan alarm", fan_data);
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 11 lines · Cong Nguyen

fan_alarm_irq_handler() schedules alarm_work, but nothing ever cancels it. free_irq() (via devm) only waits for an in-progress IRQ handler, not queued work -- a pending alarm_work can run after fan_data is devm-freed, dereferencing it in fan_alarm_notify().

Cancel it via a devm action registered before devm_request_irq(), so teardown frees the IRQ first, then cancels whatever's already queued. Guenter pointed out cancel_work_sync() still leaves a window if something schedules the work again after it's canceled; use disable_work_sync() instead, which permanently disables it too.

Quoted 6 lines
Fixes d6fe1360f42e ("hwmon: add generic GPIO fan driver")
Assisted-by Claude:claude-opus-4

Turns out this was already fixed with upstream commit a2471ed17b0e ("hwmon:
(gpio-fan) Fix use-after-free in alarm work").

Guenter