rtc: m41t80: clean up watchdog on probe failure
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.
--- drivers/rtc/rtc-m41t80.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/rtc/rtc-m41t80.c +9 -1
@@ -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
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 callingm41t80_remove(), leaving the watchdog misc device and reboot notifier registered.Both watchdog paths use the global
save_clientpointer, 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")Signed-off-by Yuho Choi <[email protected]>--- drivers/rtc/rtc-m41t80.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)drivers/rtc/rtc-m41t80.c +9
-2@@ -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
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@@ -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]>
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()readsi2c_get_clientdata(save_client), i.e. the devres-freedm41t80_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 globalsave_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 "selectWATCHDOG_CORE" inKconfig. 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