watchdog: Fix race condition in registration code
authorGuenter Roeck <linux@roeck-us.net>
Sat, 6 Apr 2013 04:22:43 +0000 (21:22 -0700)
committerWim Van Sebroeck <wim@iguana.be>
Thu, 9 May 2013 06:13:41 +0000 (08:13 +0200)
A race condition exists when registering the first watchdog device.
Sequence of events:

- watchdog_register_device calls watchdog_dev_register
- watchdog_dev_register creates the watchdog misc device by calling
  misc_register.
  At that time, the matching character device (/dev/watchdog0) does not yet
  exist, and old_wdd is not set either.
- Userspace gets an event and opens /dev/watchdog
- watchdog_open is called and sets wdd = old_wdd, which is still NULL,
  and tries to dereference it. This causes the kernel to panic.

Seen with systemd trying to open /dev/watchdog immediately after
it was created.

Reported-by: Arkadiusz Miskiewicz <arekm@maven.pl>
Signed-off-by: Guenter Roeck <linux@roeck-us.net>
Tested-by: Arkadiusz Miskiewicz <arekm@maven.pl>
Signed-off-by: Wim Van Sebroeck <wim@iguana.be>
drivers/watchdog/watchdog_dev.c

index 08b48bbf9f4b2d3c65cf18045ff90c1465c9787f..faf4e189fe423179cb0261e77a3519a2a2b1c03e 100644 (file)
@@ -523,6 +523,7 @@ int watchdog_dev_register(struct watchdog_device *watchdog)
        int err, devno;
 
        if (watchdog->id == 0) {
+               old_wdd = watchdog;
                watchdog_miscdev.parent = watchdog->parent;
                err = misc_register(&watchdog_miscdev);
                if (err != 0) {
@@ -531,9 +532,9 @@ int watchdog_dev_register(struct watchdog_device *watchdog)
                        if (err == -EBUSY)
                                pr_err("%s: a legacy watchdog module is probably present.\n",
                                        watchdog->info->identity);
+                       old_wdd = NULL;
                        return err;
                }
-               old_wdd = watchdog;
        }
 
        /* Fill in the data structures */