Commit 38d75297 authored by Curtis Klein's avatar Curtis Klein Committed by Wim Van Sebroeck

watchdog: set cdev owner before adding

When the new watchdog character device is registered, it becomes
available for opening. This creates a race where userspace may open the
device before the character device's owner is set. This results in an
imbalance in module_get calls as the cdev_get in cdev_open will not
increment the reference count on the watchdog driver module.

This causes problems when the watchdog character device is released as
the module loader's reference will also be released. This makes it
impossible to open the watchdog device later on as it now appears that
the module is being unloaded. The open will fail with -ENXIO from
chrdev_open.

The legacy watchdog device will fail with -EBUSY from the try_module_get
in watchdog_open because it's module owner is the watchdog core module
so it can still be opened but it will fail to get a refcount on the
underlying watchdog device driver.

Fixes: 72139dfa ("watchdog: Fix the race between the release of watchdog_core_data and cdev")
Signed-off-by: default avatarCurtis Klein <curtis.klein@hpe.com>
Reviewed-by: default avatarGuenter Roeck <linux@roeck-us.net>
Link: https://lore.kernel.org/r/20231205190522.55153-1-curtis.klein@hpe.comSigned-off-by: default avatarGuenter Roeck <linux@roeck-us.net>
Signed-off-by: default avatarWim Van Sebroeck <wim@linux-watchdog.org>
parent b85ea95d
...@@ -1073,6 +1073,7 @@ static int watchdog_cdev_register(struct watchdog_device *wdd) ...@@ -1073,6 +1073,7 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
/* Fill in the data structures */ /* Fill in the data structures */
cdev_init(&wd_data->cdev, &watchdog_fops); cdev_init(&wd_data->cdev, &watchdog_fops);
wd_data->cdev.owner = wdd->ops->owner;
/* Add the device */ /* Add the device */
err = cdev_device_add(&wd_data->cdev, &wd_data->dev); err = cdev_device_add(&wd_data->cdev, &wd_data->dev);
...@@ -1087,8 +1088,6 @@ static int watchdog_cdev_register(struct watchdog_device *wdd) ...@@ -1087,8 +1088,6 @@ static int watchdog_cdev_register(struct watchdog_device *wdd)
return err; return err;
} }
wd_data->cdev.owner = wdd->ops->owner;
/* Record time of most recent heartbeat as 'just before now'. */ /* Record time of most recent heartbeat as 'just before now'. */
wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1); wd_data->last_hw_keepalive = ktime_sub(ktime_get(), 1);
watchdog_set_open_deadline(wd_data); watchdog_set_open_deadline(wd_data);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment