Skip to contentSkip to conversation

rtc: m41t80: clean up watchdog on probe failure

PATCH In review Yuho Choi 2 participants 4 messages Jun 1 – Jun 28 Real time clock (RTC)

lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 rtc: m41t80: clean up watchdog on probe failure

m41t80_probe() registers the watchdog misc device and reboot notifier before registering the RTC device. If RTC device registration fails, probe returns without calling m41t80_remove(), leaving the watchdog misc device and reboot notifier registered.

Both watchdog paths use the global save_client pointer, which can outlive the failed probe and point at driver state that has been released by devres.

Unregister the watchdog misc device and reboot notifier before returning from the RTC registration failure path.

Fixes 10d0c768cc6d ("rtc: m41t80: fix race conditions")
---
 drivers/rtc/rtc-m41t80.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rtc/rtc-m41t80.c +9 -1
index b26afef37d9c..f4a30320c6ed 100644
@@ -1009,9 +1009,17 @@ static int m41t80_probe(struct i2c_client *client)
 
 	rc = devm_rtc_register_device(m41t80_data->rtc);
 	if (rc)
-		return rc;
+		goto err_wdt;
 
 	return 0;
+err_wdt:
+#ifdef CONFIG_RTC_DRV_M41T80_WDT
+	if (m41t80_data->features & M41T80_FEATURE_HT) {
+		misc_deregister(&wdt_dev);
+		unregister_reboot_notifier(&wdt_notifier);
+	}
+#endif
+	return rc;
 }
 
 static void m41t80_remove(struct i2c_client *client)
Signature · 1 line
2.43.0
author
#1 lore raw

Hi,

Just a gentle ping on this patch.

I would appreciate any feedback when you have a chance to review this.

Thanks

Quoted 45 lines · Yuho Choi

m41t80_probe() registers the watchdog misc device and reboot notifier before registering the RTC device. If RTC device registration fails, probe returns without calling m41t80_remove(), leaving the watchdog misc device and reboot notifier registered.

Both watchdog paths use the global save_client pointer, which can outlive the failed probe and point at driver state that has been released by devres.

Unregister the watchdog misc device and reboot notifier before returning from the RTC registration failure path.

Fixes 10d0c768cc6d ("rtc: m41t80: fix race conditions")
---
 drivers/rtc/rtc-m41t80.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rtc/rtc-m41t80.c +9 -2
index b26afef37d9c..f4a30320c6ed 100644
@@ -1009,9 +1009,17 @@ static int m41t80_probe(struct i2c_client *client)
 
        rc = devm_rtc_register_device(m41t80_data->rtc);
        if (rc)
-               return rc;
+               goto err_wdt;
 
        return 0;
+err_wdt:
+#ifdef CONFIG_RTC_DRV_M41T80_WDT
+       if (m41t80_data->features & M41T80_FEATURE_HT) {
+               misc_deregister(&wdt_dev);
+               unregister_reboot_notifier(&wdt_notifier);
+       }
+#endif
+       return rc;
 }
 
 static void m41t80_remove(struct i2c_client *client)
--

2.43.0

#2 lore raw
In reply to Yuho Choi

drivers/rtc/rtc-m41t80.c static int m41t80_probe(struct i2c_client *client) · line 1012

Quoted 10 lines · Yuho Choi
drivers/rtc/rtc-m41t80.c +1 -1
index b26afef37d9c..f4a30320c6ed 100644
@@ -1009,9 +1009,17 @@ static int m41t80_probe(struct i2c_client *client)
 
 	rc = devm_rtc_register_device(m41t80_data->rtc);
 	if (rc)
-		return rc;
+		goto err_wdt;

IMHO the right fix is to move the watchdog to the watchdog core.

The leak comes from the watchdog being built on three non-devres save_client. A probe failure strands all three, after which the watchdog dereferences freed driver state (wdt_ping() reads i2c_get_clientdata(save_client), i.e. the devres-freed m41t80_data).

Built on the watchdog core (CONFIG_WATCHDOG_CORE), none of that machinery is needed:

- devm_watchdog_register_device() unregisters automatically on any probe failure and on unbind.
- watchdog_stop_on_reboot() replaces the reboot notifier.
- watchdog_set/get_drvdata() replaces the global save_client, closing the use-after-free surface itself.
- m41t80_remove() does nothing but watchdog teardown today, so it goes away too.

This is exactly what was done for the sibling driver rtc-ds1374, which carried the same legacy pattern: d3de4beb14a8 ("rtc: ds1374: wdt: Use watchdog core for watchdog part"), net -135 lines, adding "select WATCHDOG_CORE" in Kconfig. It is a usable template.

primitives: a misc device, a reboot notifier, and the global

primitives: a misc device, a reboot notifier, and the global

Signature · 1 line
Alexandre Mergnat <[email protected]>
author
#3 lore raw
Quoted 18 lines · Alexandre Mergnat

IMHO the right fix is to move the watchdog to the watchdog core.

The leak comes from the watchdog being built on three non-devres save_client. A probe failure strands all three, after which the watchdog dereferences freed driver state (wdt_ping() reads i2c_get_clientdata(save_client), i.e. the devres-freed m41t80_data).

Built on the watchdog core (CONFIG_WATCHDOG_CORE), none of that machinery is needed:

- devm_watchdog_register_device() unregisters automatically on any probe failure and on unbind.
- watchdog_stop_on_reboot() replaces the reboot notifier.
- watchdog_set/get_drvdata() replaces the global save_client, closing the use-after-free surface itself.
- m41t80_remove() does nothing but watchdog teardown today, so it goes away too.

Quoted 6 lines

This is exactly what was done for the sibling driver rtc-ds1374, which carried the same legacy pattern: d3de4beb14a8 ("rtc: ds1374: wdt: Use watchdog core for watchdog part"), net -135 lines, adding "select WATCHDOG_CORE" in Kconfig. It is a usable template.

primitives: a misc device, a reboot notifier, and the global

Thanks for the detailed suggestion, Alexandre.

Moving to the watchdog core sounds like a better approach. I will rework this based on the rtc-ds1374 example and send v2.

Thanks,
Yuho