Skip to content

Commit 85bb534

Browse files
author
Danilo Krummrich
committed
vdpa: use generic driver_override infrastructure
When a driver is probed through __driver_attach(), the bus' match() callback is called without the device lock held, thus accessing the driver_override field without a lock, which can cause a UAF. Fix this by using the driver-core driver_override infrastructure taking care of proper locking internally. Note that calling match() from __driver_attach() without the device lock held is intentional. [1] Link: https://lore.kernel.org/driver-core/DGRGTIRHA62X.3RY09D9SOK77P@kernel.org/ [1] Reported-by: Gui-Dong Han <hanguidong02@gmail.com> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220789 Fixes: 539fec7 ("vdpa: add driver_override support") Acked-by: Eugenio Pérez <eperezma@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Link: https://patch.msgid.link/20260324005919.2408620-9-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 8a700b1 commit 85bb534

2 files changed

Lines changed: 5 additions & 47 deletions

File tree

drivers/vdpa/vdpa.c

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -67,57 +67,20 @@ static void vdpa_dev_remove(struct device *d)
6767

6868
static int vdpa_dev_match(struct device *dev, const struct device_driver *drv)
6969
{
70-
struct vdpa_device *vdev = dev_to_vdpa(dev);
70+
int ret;
7171

7272
/* Check override first, and if set, only use the named driver */
73-
if (vdev->driver_override)
74-
return strcmp(vdev->driver_override, drv->name) == 0;
73+
ret = device_match_driver_override(dev, drv);
74+
if (ret >= 0)
75+
return ret;
7576

7677
/* Currently devices must be supported by all vDPA bus drivers */
7778
return 1;
7879
}
7980

80-
static ssize_t driver_override_store(struct device *dev,
81-
struct device_attribute *attr,
82-
const char *buf, size_t count)
83-
{
84-
struct vdpa_device *vdev = dev_to_vdpa(dev);
85-
int ret;
86-
87-
ret = driver_set_override(dev, &vdev->driver_override, buf, count);
88-
if (ret)
89-
return ret;
90-
91-
return count;
92-
}
93-
94-
static ssize_t driver_override_show(struct device *dev,
95-
struct device_attribute *attr, char *buf)
96-
{
97-
struct vdpa_device *vdev = dev_to_vdpa(dev);
98-
ssize_t len;
99-
100-
device_lock(dev);
101-
len = sysfs_emit(buf, "%s\n", vdev->driver_override);
102-
device_unlock(dev);
103-
104-
return len;
105-
}
106-
static DEVICE_ATTR_RW(driver_override);
107-
108-
static struct attribute *vdpa_dev_attrs[] = {
109-
&dev_attr_driver_override.attr,
110-
NULL,
111-
};
112-
113-
static const struct attribute_group vdpa_dev_group = {
114-
.attrs = vdpa_dev_attrs,
115-
};
116-
__ATTRIBUTE_GROUPS(vdpa_dev);
117-
11881
static const struct bus_type vdpa_bus = {
11982
.name = "vdpa",
120-
.dev_groups = vdpa_dev_groups,
83+
.driver_override = true,
12184
.match = vdpa_dev_match,
12285
.probe = vdpa_dev_probe,
12386
.remove = vdpa_dev_remove,
@@ -132,7 +95,6 @@ static void vdpa_release_dev(struct device *d)
13295
ops->free(vdev);
13396

13497
ida_free(&vdpa_index_ida, vdev->index);
135-
kfree(vdev->driver_override);
13698
kfree(vdev);
13799
}
138100

include/linux/vdpa.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ struct vdpa_mgmt_dev;
7272
* struct vdpa_device - representation of a vDPA device
7373
* @dev: underlying device
7474
* @vmap: the metadata passed to upper layer to be used for mapping
75-
* @driver_override: driver name to force a match; do not set directly,
76-
* because core frees it; use driver_set_override() to
77-
* set or clear it.
7875
* @config: the configuration ops for this device.
7976
* @map: the map ops for this device
8077
* @cf_lock: Protects get and set access to configuration layout.
@@ -90,7 +87,6 @@ struct vdpa_mgmt_dev;
9087
struct vdpa_device {
9188
struct device dev;
9289
union virtio_map vmap;
93-
const char *driver_override;
9490
const struct vdpa_config_ops *config;
9591
const struct virtio_map_ops *map;
9692
struct rw_semaphore cf_lock; /* Protects get/set config */

0 commit comments

Comments
 (0)