Skip to content

Commit 18daa52

Browse files
dtorgregkh
authored andcommitted
driver core: fix potential NULL pointer dereference in dev_uevent()
If userspace reads "uevent" device attribute at the same time as another threads unbinds the device from its driver, change to dev->driver from a valid pointer to NULL may result in crash. Fix this by using READ_ONCE() when fetching the pointer, and take bus' drivers klist lock to make sure driver instance will not disappear while we access it. Use WRITE_ONCE() when setting the driver pointer to ensure there is no tearing. Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Link: https://lore.kernel.org/r/20250311052417.1846985-3-dmitry.torokhov@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 04d3e54 commit 18daa52

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

drivers/base/base.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static inline void subsys_put(struct subsys_private *sp)
7373
kset_put(&sp->subsys);
7474
}
7575

76+
struct subsys_private *bus_to_subsys(const struct bus_type *bus);
7677
struct subsys_private *class_to_subsys(const struct class *class);
7778

7879
struct driver_private {
@@ -182,8 +183,18 @@ void device_driver_detach(struct device *dev);
182183

183184
static inline void device_set_driver(struct device *dev, const struct device_driver *drv)
184185
{
186+
/*
187+
* Majority (all?) read accesses to dev->driver happens either
188+
* while holding device lock or in bus/driver code that is only
189+
* invoked when the device is bound to a driver and there is no
190+
* concern of the pointer being changed while it is being read.
191+
* However when reading device's uevent file we read driver pointer
192+
* without taking device lock (so we do not block there for
193+
* arbitrary amount of time). We use WRITE_ONCE() here to prevent
194+
* tearing so that READ_ONCE() can safely be used in uevent code.
195+
*/
185196
// FIXME - this cast should not be needed "soon"
186-
dev->driver = (struct device_driver *)drv;
197+
WRITE_ONCE(dev->driver, (struct device_driver *)drv);
187198
}
188199

189200
int devres_release_all(struct device *dev);

drivers/base/bus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static int __must_check bus_rescan_devices_helper(struct device *dev,
5757
* NULL. A call to subsys_put() must be done when finished with the pointer in
5858
* order for it to be properly freed.
5959
*/
60-
static struct subsys_private *bus_to_subsys(const struct bus_type *bus)
60+
struct subsys_private *bus_to_subsys(const struct bus_type *bus)
6161
{
6262
struct subsys_private *sp = NULL;
6363
struct kobject *kobj;

drivers/base/core.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,35 @@ static const char *dev_uevent_name(const struct kobject *kobj)
26242624
return NULL;
26252625
}
26262626

2627+
/*
2628+
* Try filling "DRIVER=<name>" uevent variable for a device. Because this
2629+
* function may race with binding and unbinding the device from a driver,
2630+
* we need to be careful. Binding is generally safe, at worst we miss the
2631+
* fact that the device is already bound to a driver (but the driver
2632+
* information that is delivered through uevents is best-effort, it may
2633+
* become obsolete as soon as it is generated anyways). Unbinding is more
2634+
* risky as driver pointer is transitioning to NULL, so READ_ONCE() should
2635+
* be used to make sure we are dealing with the same pointer, and to
2636+
* ensure that driver structure is not going to disappear from under us
2637+
* we take bus' drivers klist lock. The assumption that only registered
2638+
* driver can be bound to a device, and to unregister a driver bus code
2639+
* will take the same lock.
2640+
*/
2641+
static void dev_driver_uevent(const struct device *dev, struct kobj_uevent_env *env)
2642+
{
2643+
struct subsys_private *sp = bus_to_subsys(dev->bus);
2644+
2645+
if (sp) {
2646+
scoped_guard(spinlock, &sp->klist_drivers.k_lock) {
2647+
struct device_driver *drv = READ_ONCE(dev->driver);
2648+
if (drv)
2649+
add_uevent_var(env, "DRIVER=%s", drv->name);
2650+
}
2651+
2652+
subsys_put(sp);
2653+
}
2654+
}
2655+
26272656
static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
26282657
{
26292658
const struct device *dev = kobj_to_dev(kobj);
@@ -2655,8 +2684,8 @@ static int dev_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
26552684
if (dev->type && dev->type->name)
26562685
add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
26572686

2658-
if (dev->driver)
2659-
add_uevent_var(env, "DRIVER=%s", dev->driver->name);
2687+
/* Add "DRIVER=%s" variable if the device is bound to a driver */
2688+
dev_driver_uevent(dev, env);
26602689

26612690
/* Add common DT information about the device */
26622691
of_device_uevent(dev, env);

0 commit comments

Comments
 (0)