Skip to contentSkip to conversation

watchdog: airoha: Prevent division by zero when clock frequency is zero

PATCH Applied Wayen Yan 3 participants 3 messages Jul 8 Watchdog device drivers

lore
Share & tools
lore

Wayen Yan

author
#0 lore raw

PATCH 1/1 watchdog: airoha: Prevent division by zero when clock frequency is zero

clk_get_rate() can return 0 when the clock provider is not properly configured or the clock is unmanaged. The driver uses wdt_freq as a divisor directly in airoha_wdt_probe() to compute max_timeout and in airoha_wdt_get_timeleft() to compute the remaining time, which results in a division by zero.

Add a check for wdt_freq == 0 in probe and return -EINVAL with dev_err_probe() to prevent the division by zero and provide a diagnostic message.

Fixes 3cf67f3769b8 ("watchdog: Add support for Airoha EN7851 watchdog")
---
 drivers/watchdog/airoha_wdt.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/watchdog/airoha_wdt.c +3 -0
index dc8ca11c14d8..4bd333189b87 100644
@@ -147,6 +147,9 @@ static int airoha_wdt_probe(struct platform_device *pdev)
 
 	/* Watchdog ticks at half the bus rate */
 	airoha_wdt->wdt_freq = clk_get_rate(bus_clk) / 2;
+	if (!airoha_wdt->wdt_freq)
+		return dev_err_probe(dev, -EINVAL,
+				     "invalid clock frequency\n");
 
 	/* Initialize struct watchdog device */
 	wdog_dev = &airoha_wdt->wdog_dev;
Signature · 1 line
2.51.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Unvalidated module parameter directly assigned to watchdog timeout leads to 32-bit integer overflow and immediate system reboot. --

--- Patch [1]: [PATCH] watchdog: airoha: Prevent division by zero when clock frequency is zero --- commit 3c89565f34ffbc693201939b137238a0f581ebf6
Author: Wayen Yan <[email protected]>

watchdog: airoha: Prevent division by zero when clock frequency is zero

This commit adds a check in airoha_wdt_probe to ensure the clock frequency is not zero before using it as a divisor. It prevents a division by zero if the clock provider is not properly configured.

Quoted 13 lines
drivers/watchdog/airoha_wdt.c +3 -0
@@ -147,6 +147,9 @@ static int airoha_wdt_probe(struct platform_device *pdev)
 
 	/* Watchdog ticks at half the bus rate */
 	airoha_wdt->wdt_freq = clk_get_rate(bus_clk) / 2;
+	if (!airoha_wdt->wdt_freq)
+		return dev_err_probe(dev, -EINVAL,
+				     "invalid clock frequency\n");
 
 	/* Initialize struct watchdog device */
 	wdog_dev = &airoha_wdt->wdog_dev;
Quoted 1 line

wdog_dev->timeout = heartbeat;

[Severity: High]
This is a pre-existing issue, but does assigning the heartbeat module parameter directly here bypass watchdog timeout validation?

It appears watchdog_init_timeout() is not used to validate the module parameter. If an out-of-bounds heartbeat parameter is provided, could this lead to an integer overflow in airoha_wdt_start()?

drivers/watchdog/airoha_wdt.c:airoha_wdt_start() {
    ...
    val = wdog_dev->timeout * airoha_wdt->wdt_freq;
    writel(val, airoha_wdt->base + WDT_TIMER_LOAD_VALUE);
    ...
}

With a watchdog frequency of 150MHz, a timeout of 29 or greater results in a multiplication over 4.29 billion, which overflows the 32-bit val variable. This wrap-around would cause the hardware timer to expire prematurely before the core ping worker expects to service it, resulting in an immediate system reset.

Similarly, if heartbeat is 0, is it possible for the timer load value to become 0 and trigger an immediate reset upon device open?

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

clk_get_rate() can return 0 when the clock provider is not properly configured or the clock is unmanaged. The driver uses wdt_freq as a divisor directly in airoha_wdt_probe() to compute max_timeout and in airoha_wdt_get_timeleft() to compute the remaining time, which results in a division by zero.

Quoted 6 lines

Add a check for wdt_freq == 0 in probe and return -EINVAL with dev_err_probe() to prevent the division by zero and provide a diagnostic message.

Fixes 3cf67f3769b8 ("watchdog: Add support for Airoha EN7851 watchdog")

Applied.

Thanks,
Guenter