Skip to contentSkip to conversation

watchdog: core: Fix module refcount imbalance when open fails

PATCH In review Artur Kowalski 2 participants 2 messages Nov 23 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 watchdog: core: Fix module refcount imbalance when open fails

watchdog_open() doesn't take a module reference for a watchdog that is running in hardware, as watchdog_cdev_register() already holds one for it. If watchdog_start() then fails, which for such a watchdog means the driver's ping failed, the error path drops a module reference anyway.

This drops the reference held for the running watchdog, so the module can be unloaded while the watchdog is running and the core still uses it. Stopping the watchdog through the core later drops the reference a second time, underflowing the module refcount.

Only drop the module reference on the error path if it was taken.

This was found with the help of Sashiko and Claude. It can be triggered by any modular driver whose ping can fail on a watchdog that was running when it was registered. No in-tree driver was checked for that. The fix was build tested only, and no reproducer was run.

Fixes ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")

---
This was found while developing a Tegra20 watchdog driver. A watchdog reset hangs Tegra20's boot ROM once the VDE power partition is off, so the driver stops the watchdog behind the core's back when that happens. Failing pings afterwards would trigger this bug when opening a watchdog left running by the bootloader, so the driver keeps them succeeding until user space has started the watchdog.

---
 drivers/watchdog/watchdog_dev.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/watchdog/watchdog_dev.c +2 -1
index e8c1fcb53c56..9ad715d4d46d 100644
@@ -913,7 +913,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
 	return stream_open(inode, file);
 
 out_mod:
-	module_put(wd_data->wdd->ops->owner);
+	if (!hw_running)
+		module_put(wd_data->wdd->ops->owner);
 out_clear:
 	clear_bit(_WDOG_DEV_OPEN, &wd_data->status);
 	return err;

---
base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
change-id: 20261123-core-wdt-fix-898327dc0ca2

Best regards,

Signature · 1 line
Artur Kowalski <[email protected]>
maintainer
#1 lore raw
Quoted 35 lines · Artur Kowalski

watchdog_open() doesn't take a module reference for a watchdog that is running in hardware, as watchdog_cdev_register() already holds one for it. If watchdog_start() then fails, which for such a watchdog means the driver's ping failed, the error path drops a module reference anyway.

This drops the reference held for the running watchdog, so the module can be unloaded while the watchdog is running and the core still uses it. Stopping the watchdog through the core later drops the reference a second time, underflowing the module refcount.

Only drop the module reference on the error path if it was taken.

This was found with the help of Sashiko and Claude. It can be triggered by any modular driver whose ping can fail on a watchdog that was running when it was registered. No in-tree driver was checked for that. The fix was build tested only, and no reproducer was run.

Fixes ee142889e32f ("watchdog: Introduce WDOG_HW_RUNNING flag")

---
This was found while developing a Tegra20 watchdog driver. A watchdog reset hangs Tegra20's boot ROM once the VDE power partition is off, so the driver stops the watchdog behind the core's back when that happens. Failing pings afterwards would trigger this bug when opening a watchdog left running by the bootloader, so the driver keeps them succeeding until user space has started the watchdog.

---
  drivers/watchdog/watchdog_dev.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/watchdog/watchdog_dev.c +0 -0
index e8c1fcb53c56..9ad715d4d46d 100644

drivers/watchdog/watchdog_dev.c static int watchdog_open(struct inode *inode, struct file *file) · line 917

Quoted 6 lines
drivers/watchdog/watchdog_dev.c +2 -1
@@ -913,7 +913,8 @@ static int watchdog_open(struct inode *inode, struct file *file)
  	return stream_open(inode, file);
 
  out_mod:
-	module_put(wd_data->wdd->ops->owner);
+	if (!hw_running)
+		module_put(wd_data->wdd->ops->owner);

That seems to be identical to upstream commit e1d91be1ec2e6 ("watchdog:
core: Fix unbalanced module_put() in watchdog_open()").

Id suggest to update your tree to v7.4-rc4 or later to avoid hitting a number of other recently fixed issues in the watchdog subsystem core.

Thanks,
Guenter