Skip to contentSkip to conversation

watchdog: omap: Do not read the counter while the watchdog is stopped

PATCH In review Nhat Tran 2 participants 2 messages Nov 20

Subsystems (2)
lore
Share & tools
lore

Nhat Tran

author
#0 lore raw

PATCH 1/1 watchdog: omap: Do not read the counter while the watchdog is stopped

omap_wdt only holds a runtime PM reference between omap_wdt_start() and omap_wdt_stop(), so the module is idle and its clock gated whenever the watchdog is not running. omap_wdt_get_timeleft() reads the WCRR register unconditionally, but the watchdog core calls ->get_timeleft() from the sysfs timeleft attribute without checking whether the watchdog is active.

With CONFIG_WATCHDOG_SYSFS=y the attribute is world readable, so any unprivileged user can trigger an external abort on a stopped watchdog. On a BeagleBone Black (AM335x), with panic_on_oops set:

  $ id
  uid=1000(tester) gid=1000(tester) groups=1000(tester)
  $ cat /sys/class/watchdog/watchdog0/timeleft
  Unhandled fault: external abort on non-linefetch (0x1028) at 0xf9e35028
  Internal error: : 1028 [#1] SMP ARM
  CPU: 0 UID: 1000 PID: 239 Comm: cat Not tainted 7.4.0-rc2-00716-g23c62304dde2-dirty #10 VOLUNTARY
  Hardware name: Generic AM33XX (Flattened Device Tree)
  PC is at omap_wdt_get_timeleft+0x4/0x14
  LR is at watchdog_get_timeleft+0x24/0x130
  Call trace:
   omap_wdt_get_timeleft from watchdog_get_timeleft+0x24/0x130
   watchdog_get_timeleft from timeleft_show+0x3c/0x80
   timeleft_show from dev_attr_show+0x18/0x4c
   dev_attr_show from sysfs_kf_seq_show+0xa0/0x138
   sysfs_kf_seq_show from seq_read_iter+0x194/0x4c0
   seq_read_iter from copy_splice_read+0x168/0x2d8
   copy_splice_read from splice_direct_to_actor+0xb4/0x2b4
   splice_direct_to_actor from do_splice_direct_actor+0x84/0xcc
   do_splice_direct_actor from do_splice_direct+0x24/0x30
   do_splice_direct from do_sendfile+0x2fc/0x3a4
   do_sendfile from sys_sendfile64+0x10c/0x128
   sys_sendfile64 from ret_fast_syscall+0x0/0x54
  Kernel panic - not syncing: Fatal exception

Only read the counter while the watchdog is running, under wdev->lock so the check cannot race with start() and stop(), and report 0 time left otherwise.

Tested on a BeagleBone Black: with the watchdog stopped, timeleft now reads 0 for an unprivileged user; with the watchdog started through /dev/watchdog it counts down as before.

Fixes 452fafed839e ("watchdog: omap_wdt: implement get_timeleft")
---
 drivers/watchdog/omap_wdt.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/watchdog/omap_wdt.c +7 -3
index c60bc678262c..82e50244176f 100644
@@ -206,10 +206,14 @@ static unsigned int omap_wdt_get_timeleft(struct watchdog_device *wdog)
 {
 	struct omap_wdt_dev *wdev = to_omap_wdt_dev(wdog);
 	void __iomem *base = wdev->base;
-	u32 value;
+	unsigned int timeleft = 0;
 
-	value = readl_relaxed(base + OMAP_WATCHDOG_CRR);
-	return GET_WCCR_SECS(value);
+	mutex_lock(&wdev->lock);
+	if (wdev->omap_wdt_users)
+		timeleft = GET_WCCR_SECS(readl_relaxed(base + OMAP_WATCHDOG_CRR));
+	mutex_unlock(&wdev->lock);
+
+	return timeleft;
 }
 
 static const struct watchdog_info omap_wdt_info = {
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · Nhat Tran

omap_wdt only holds a runtime PM reference between omap_wdt_start() and omap_wdt_stop(), so the module is idle and its clock gated whenever the watchdog is not running. omap_wdt_get_timeleft() reads the WCRR register unconditionally, but the watchdog core calls ->get_timeleft() from the sysfs timeleft attribute without checking whether the watchdog is active.

With CONFIG_WATCHDOG_SYSFS=y the attribute is world readable, so any

The watchdog can also be stopped while the watchdog device is open, so the problem is a bit broader than that.

Quoted 3 lines

unprivileged user can trigger an external abort on a stopped watchdog. On a BeagleBone Black (AM335x), with panic_on_oops set:

Seems to me the proper generic fix should be in watchdog_get_timeleft(), which should return either return the configured timeout or maybe 0 (I am open to suggestions) if the watchdog is not running.

Thanks,
Guenter