treewide: Convert buses to use generic driver_override
Subsystems (7)
Danilo Krummrich
maintainerv2 cover treewide: Convert buses to use generic driver_override
This is the follow-up of the driver_override generalization in [1], converting the remaining 4 busses and removing the now-unused driver_set_override() helper.
All of them are prone to the potential UAF described in [2], caused by accessing the driver_override field from their corresponding match() callback.
In order to address this, the generalized driver_override field in struct device is protected with a spinlock. The driver-core provides accessors, such as device_match_driver_override(), device_has_driver_override() and device_set_driver_override(), which all ensure proper locking internally.
Additionally, the driver-core provides a driver_override flag in struct bus_type, which, once enabled, automatically registers generic sysfs callbacks, allowing userspace to modify the driver_override field.
This series is based on v7.1-rc1 with no additional dependencies, hence those patches can be picked up by subsystems individually.
[1] https://lore.kernel.org/driver-core/[email protected]/
[2] https://bugzilla.kernel.org/show_bug.cgi?id=220789
[3] https://gitlab.com/driverctl/driverctl/-/blob/0.121/driverctl?ref_type=tags#L99
Changes in v2:
- Rebase on v7.1-rc1
- Drop already merged patches
- vmbus documentation changes as requested by Michael
Danilo Krummrich (5):
amba: use generic driver_override infrastructure
cdx: use generic driver_override infrastructure
Drivers: hv: vmbus: use generic driver_override infrastructure
rpmsg: use generic driver_override infrastructure driver core: remove driver_set_override()
drivers/amba/bus.c | 37 +++------------ drivers/base/driver.c | 75 ------------------------------- drivers/cdx/cdx.c | 40 +++-------------- drivers/hv/vmbus_drv.c | 43 +++++------------- drivers/rpmsg/qcom_glink_native.c | 2 - drivers/rpmsg/rpmsg_core.c | 43 +++--------------- drivers/rpmsg/virtio_rpmsg_bus.c | 1 - include/linux/amba/bus.h | 5 --- include/linux/cdx/cdx_bus.h | 4 -- include/linux/device/driver.h | 2 - include/linux/hyperv.h | 5 --- include/linux/rpmsg.h | 4 -- 12 files changed, 28 insertions(+), 233 deletions(-)
base-commit: 254f49634ee16a731174d2ae34bc50bd5f45e731
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerPATCH v2 1/5 amba: 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]
--- drivers/amba/bus.c | 37 ++++++------------------------------- include/linux/amba/bus.h | 5 ----- 2 files changed, 6 insertions(+), 36 deletions(-)
drivers/amba/bus.c +6 -31
@@ -82,33 +82,6 @@ static void amba_put_disable_pclk(struct amba_device *pcdev) } -static ssize_t driver_override_show (struct device *_dev, -struct device_attribute *attr,char *buf) -{ -struct amba_device *dev =to_amba_device (_dev); -ssize_t len; - -device_lock (_dev); - len =sprintf (buf,"%s\n" , dev->driver_override); -device_unlock (_dev); -return len; -} - -static ssize_t driver_override_store (struct device *_dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct amba_device *dev =to_amba_device (_dev); -int ret; - - ret =driver_set_override (_dev, &dev->driver_override, buf, count); -if (ret) -return ret; - -return count; -} -static DEVICE_ATTR_RW (driver_override); - #define amba_attr_func(name,fmt,arg...) \ static ssize_t name##_show(struct device *_dev, \ struct device_attribute *attr, char *buf) \
@@ -126,7 +99,6 @@ amba_attr_func(resource, "\t%016llx\t%016llx\t%016lx\n",static struct attribute *amba_dev_attrs[] = { &dev_attr_id.attr, &dev_attr_resource.attr, - &dev_attr_driver_override.attr,NULL , };ATTRIBUTE_GROUPS (amba_dev);
@@ -209,10 +181,11 @@ static int amba_match(struct device *dev, const struct device_driver *drv) {struct amba_device *pcdev =to_amba_device (dev);const struct amba_driver *pcdrv =to_amba_driver (drv); +int ret;mutex_lock (&pcdev->periphid_lock);if (!pcdev->periphid) { -int ret =amba_read_periphid (pcdev); + ret =amba_read_periphid (pcdev);/* * Returning any error other than -EPROBE_DEFER from bus match
@@ -230,8 +203,9 @@ static int amba_match(struct device *dev, const struct device_driver *drv)mutex_unlock (&pcdev->periphid_lock);/* When driver_override is set, only bind to the matching driver */ -if (pcdev->driver_override) -return !strcmp (pcdev->driver_override, drv->name); + ret =device_match_driver_override (dev, drv); +if (ret >=0 ) +return ret;return amba_lookup (pcdrv->id_table, pcdev) !=NULL ; }
@@ -436,6 +410,7 @@ static const struct dev_pm_ops amba_pm = {const struct bus_type amba_bustype = { .name ="amba" , .dev_groups = amba_dev_groups, + .driver_override = true, .match = amba_match, .uevent = amba_uevent, .probe = amba_probe,
include/linux/amba/bus.h +0 -5
@@ -71,11 +71,6 @@ struct amba_device {unsigned int cid;struct amba_cs_uci_id uci;unsigned int irq[AMBA_NR_IRQS ]; -/* -* Driver name to force a match. Do not set directly, because core -* frees it. Use driver_set_override() to set or clear it. -*/ -const char *driver_override; };struct amba_driver {
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerPATCH v2 2/5 cdx: use generic driver_override infrastructure
In reply to Danilo KrummrichWhen 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]
--- drivers/cdx/cdx.c | 40 +++++-------------------------------- include/linux/cdx/cdx_bus.h | 4 ---- 2 files changed, 5 insertions(+), 39 deletions(-)
drivers/cdx/cdx.c +5 -35
@@ -156,8 +156,6 @@ static int cdx_unregister_device(struct device *dev, }else {cdx_destroy_res_attr (cdx_dev,MAX_CDX_DEV_RESOURCES );debugfs_remove_recursive (cdx_dev->debugfs_dir); -kfree (cdx_dev->driver_override); - cdx_dev->driver_override =NULL ; }/*
@@ -268,6 +266,7 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv)const struct cdx_driver *cdx_drv =to_cdx_driver (drv);const struct cdx_device_id *found_id =NULL ;const struct cdx_device_id *ids; +int ret;if (cdx_dev->is_bus)return false;
@@ -275,7 +274,8 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv) ids = cdx_drv->match_id_table;/* When driver_override is set, only bind to the matching driver */ -if (cdx_dev->driver_override &&strcmp (cdx_dev->driver_override, drv->name)) + ret =device_match_driver_override (dev, drv); +if (ret ==0 )return false; found_id =cdx_match_id (ids, cdx_dev);
@@ -289,7 +289,7 @@ static int cdx_bus_match(struct device *dev, const struct device_driver *drv)*/ if (!found_id->override_only)return true; -if (cdx_dev->driver_override) +if (ret >0 )return true; ids = found_id +1 ;
@@ -453,36 +453,6 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, }static DEVICE_ATTR_RO (modalias); -static ssize_t driver_override_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct cdx_device *cdx_dev =to_cdx_device (dev); -int ret; - -if (WARN_ON (dev->bus != &cdx_bus_type)) -return -EINVAL ; - - ret =driver_set_override (dev, &cdx_dev->driver_override, buf, count); -if (ret) -return ret; - -return count; -} - -static ssize_t driver_override_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct cdx_device *cdx_dev =to_cdx_device (dev); -ssize_t len; - -device_lock (dev); - len =sysfs_emit (buf,"%s\n" , cdx_dev->driver_override); -device_unlock (dev); -return len; -} -static DEVICE_ATTR_RW (driver_override); -static ssize_t enable_store (struct device *dev,struct device_attribute *attr,const char *buf,size_t count) {
@@ -552,7 +522,6 @@ static struct attribute *cdx_dev_attrs[] = { &dev_attr_class.attr, &dev_attr_revision.attr, &dev_attr_modalias.attr, - &dev_attr_driver_override.attr, NULL, };
@@ -646,6 +615,7 @@ ATTRIBUTE_GROUPS(cdx_bus);const struct bus_type cdx_bus_type = { .name ="cdx" , + .driver_override = true, .match = cdx_bus_match, .probe = cdx_probe, .remove = cdx_remove,
include/linux/cdx/cdx_bus.h +0 -4
@@ -137,9 +137,6 @@ struct cdx_controller {* @enabled: is this bus enabled * @msi_dev_id: MSI Device ID associated with CDX device * @num_msi: Number of MSI's supported by the device -* @driver_override: driver name to force a match; do not set directly, -* because core frees it; use driver_set_override() to -* set or clear it. * @irqchip_lock: lock to synchronize irq/msi configuration * @msi_write_pending: MSI write pending for this device */
@@ -165,7 +162,6 @@ struct cdx_device {bool enabled;u32 msi_dev_id;u32 num_msi; -const char *driver_override;struct mutex irqchip_lock;bool msi_write_pending; };
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerPATCH v2 3/5 Drivers: hv: vmbus: use generic driver_override infrastructure
In reply to Danilo KrummrichWhen 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]
--- drivers/hv/vmbus_drv.c | 43 ++++++++++-------------------------------- include/linux/hyperv.h | 5 ----- 2 files changed, 10 insertions(+), 38 deletions(-)
drivers/hv/vmbus_drv.c +10 -33
@@ -538,34 +538,6 @@ static ssize_t device_show(struct device *dev, }static DEVICE_ATTR_RO (device); -static ssize_t driver_override_store (struct device *dev, -struct device_attribute *attr, -const char *buf,size_t count) -{ -struct hv_device *hv_dev =device_to_hv_device (dev); -int ret; - - ret =driver_set_override (dev, &hv_dev->driver_override, buf, count); -if (ret) -return ret; - -return count; -} - -static ssize_t driver_override_show (struct device *dev, -struct device_attribute *attr,char *buf) -{ -struct hv_device *hv_dev =device_to_hv_device (dev); -ssize_t len; - -device_lock (dev); - len =sysfs_emit (buf,"%s\n" , hv_dev->driver_override); -device_unlock (dev); - -return len; -} -static DEVICE_ATTR_RW (driver_override); -/* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ static struct attribute *vmbus_dev_attrs[] = { &dev_attr_id.attr,
@@ -596,7 +568,6 @@ static struct attribute *vmbus_dev_attrs[] = { &dev_attr_channel_vp_mapping.attr, &dev_attr_vendor.attr, &dev_attr_device.attr, - &dev_attr_driver_override.attr, NULL, };
@@ -708,9 +679,11 @@ static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver * {const guid_t *guid = &dev->dev_type;const struct hv_vmbus_device_id *id; +int ret; -/* When driver_override is set, only bind to the matching driver */ -if (dev->driver_override &&strcmp (dev->driver_override, drv->name)) +/* If a driver override is set, only bind to the matching driver */ + ret =device_match_driver_override (&dev->device, &drv->driver); +if (ret ==0 )return NULL ;/* Look at the dynamic ids first, before the static ones */
@@ -718,8 +691,11 @@ static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver *if (!id) id =hv_vmbus_dev_match (drv->id_table, guid); -/* driver_override will always match, send a dummy id */ -if (!id && dev->driver_override) +/* +* If there's a matching driver override, this function should succeed, +* thus return a dummy device ID if no matching ID is found. +*/ +if (!id && ret >0 ) id = &vmbus_device_null;return id;
@@ -1021,6 +997,7 @@ static const struct dev_pm_ops vmbus_pm = {/* The one and only one */ static const struct bus_type hv_bus = { .name ="vmbus" , + .driver_override = true, .match = vmbus_match, .shutdown = vmbus_shutdown, .remove = vmbus_remove,
include/linux/hyperv.h +0 -5
@@ -1272,11 +1272,6 @@ struct hv_device {u16 device_id;struct device device; -/* -* Driver name to force a match. Do not set directly, because core -* frees it. Use driver_set_override() to set or clear it. -*/ -const char *driver_override;struct vmbus_channel *channel;struct kset *channels_kset;
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerPATCH v2 4/5 rpmsg: use generic driver_override infrastructure
In reply to Danilo KrummrichWhen 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]
--- drivers/rpmsg/qcom_glink_native.c | 2 -- drivers/rpmsg/rpmsg_core.c | 43 +++++-------------------------- drivers/rpmsg/virtio_rpmsg_bus.c | 1 - include/linux/rpmsg.h | 4 --- 4 files changed, 7 insertions(+), 43 deletions(-)
drivers/rpmsg/qcom_glink_native.c +0 -2
@@ -1626,7 +1626,6 @@ static void qcom_glink_rpdev_release(struct device *dev) {struct rpmsg_device *rpdev =to_rpmsg_device (dev); -kfree (rpdev->driver_override);kfree (rpdev); }
@@ -1862,7 +1861,6 @@ static void qcom_glink_device_release(struct device *dev)/* Release qcom_glink_alloc_channel() reference */ kref_put (&channel->refcount, qcom_glink_channel_release); -kfree (rpdev->driver_override);kfree (rpdev); }
drivers/rpmsg/rpmsg_core.c +7 -36
81 lines not on this page · raw message
drivers/rpmsg/virtio_rpmsg_bus.c +0 -1
@@ -374,7 +374,6 @@ static void virtio_rpmsg_release_device(struct device *dev)struct rpmsg_device *rpdev =to_rpmsg_device (dev);struct virtio_rpmsg_channel *vch =to_virtio_rpmsg_channel (rpdev); -kfree (rpdev->driver_override);kfree (vch); }
include/linux/rpmsg.h +0 -4
@@ -41,9 +41,6 @@ struct rpmsg_channel_info {* rpmsg_device - device that belong to the rpmsg bus * @dev: the device struct * @id: device id (used to match between rpmsg drivers and devices) -* @driver_override: driver name to force a match; do not set directly, -* because core frees it; use driver_set_override() to -* set or clear it. * @src: local address * @dst: destination address * @ept: the rpmsg endpoint of this channel
@@ -53,7 +50,6 @@ struct rpmsg_channel_info {struct rpmsg_device {struct device dev;struct rpmsg_device_id id; -const char *driver_override;u32 src;u32 dst;struct rpmsg_endpoint *ept;
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerPATCH v2 5/5 driver core: remove driver_set_override()
In reply to Danilo KrummrichAll buses have been converted from driver_set_override() to the generic driver_override infrastructure introduced in commit cb3d1049f4ea ("driver core: generalize driver_override in struct device").
Buses now either opt into the generic sysfs callbacks via the bus_type::driver_override flag, or use device_set_driver_override() / __device_set_driver_override() directly.
Thus, remove the now-unused driver_set_override() helper.
--- drivers/base/driver.c | 75 ----------------------------------- include/linux/device/driver.h | 2 - 2 files changed, 77 deletions(-)
drivers/base/driver.c +0 -75
@@ -30,81 +30,6 @@ static struct device *next_device(struct klist_iter *i)return dev; } -/** -* driver_set_override() - Helper to set or clear driver override. -* @dev: Device to change -* @override: Address of string to change (e.g. &device->driver_override); -* The contents will be freed and hold newly allocated override. -* @s: NUL-terminated string, new driver name to force a match, pass empty -* string to clear it ("" or "\n", where the latter is only for sysfs -* interface). -* @len: length of @s -* -* Helper to set or clear driver override in a device, intended for the cases -* when the driver_override field is allocated by driver/bus code. -* -* Returns: 0 on success or a negative error code on failure. -*/ -int driver_set_override (struct device *dev,const char **override, -const char *s,size_t len) -{ -const char *new, *old; -char *cp; - -if (!override || !s) -return -EINVAL ; - -/* -* The stored value will be used in sysfs show callback (sysfs_emit()), -* which has a length limit of PAGE_SIZE and adds a trailing newline. -* Thus we can store one character less to avoid truncation during sysfs -* show. -*/ -if (len >= (PAGE_SIZE -1 )) -return -EINVAL ; - -/* -* Compute the real length of the string in case userspace sends us a -* bunch of \0 characters like python likes to do. -*/ - len =strlen (s); - -if (!len) { -/* Empty string passed - clear override */ -device_lock (dev); - old = *override; - *override =NULL ; -device_unlock (dev); -kfree (old); - -return 0 ; - } - - cp =strnchr (s, len,'\n' ); -if (cp) - len = cp - s; - - new =kstrndup (s, len,GFP_KERNEL ); -if (!new) -return -ENOMEM ; - -device_lock (dev); - old = *override; -if (cp != s) { - *override = new; - }else { -/* "\n" passed - clear override */ -kfree (new); - *override =NULL ; - } -device_unlock (dev); - -kfree (old); - -return 0 ; -} -EXPORT_SYMBOL_GPL (driver_set_override); -/** * driver_for_each_device - Iterator for devices bound to a driver. * @drv: Driver we're iterating.
include/linux/device/driver.h +0 -2
@@ -160,8 +160,6 @@ int __must_check driver_create_file(const struct device_driver *driver,void driver_remove_file (const struct device_driver *driver,const struct driver_attribute *attr); -int driver_set_override (struct device *dev,const char **override, -const char *s,size_t len);int __must_check driver_for_each_device (struct device_driver *drv,struct device *start,void *data,device_iter_t fn); struct device *driver_find_device(const struct device_driver *drv,
Signature · 1 line
2.54.0
Danilo Krummrich
maintainerQuoted 2 lines · Danilo Krummrich
This series is based on v7.1-rc1 with no additional dependencies, hence those patches can be picked up by subsystems individually.
Gentle ping on this one; I can alternatively also take those patches through the driver-core tree if you prefer.
Thanks,
Danilo
Danilo Krummrich
maintainerQuoted 6 lines · Danilo Krummrich
Quoted 2 lines · Danilo Krummrich
This series is based on v7.1-rc1 with no additional dependencies, hence those patches can be picked up by subsystems individually.
Gentle ping on this one; I can alternatively also take those patches through the driver-core tree if you prefer.
Since the remaining patches of this series have been out since v7.0-rc5 without any objections, I'm planning to pick it up through the driver-core tree for v7.2-rc1.
Please let me know if there are any concerns or whether you prefer to take individual patches through your own tree.
Thanks,
Danilo
Quoted 15 lines · Danilo Krummrich
This is the follow-up of the
driver_overridegeneralization in [1], converting the remaining 4 busses and removing the now-unuseddriver_set_override()helper.All of them are prone to the potential UAF described in [2], caused by accessing the
driver_overridefield from their correspondingmatch()callback.In order to address this, the generalized
driver_overridefield in struct device is protected with a spinlock. The driver-core provides accessors, such asdevice_match_driver_override(),device_has_driver_override()anddevice_set_driver_override(), which all ensure proper locking internally.Additionally, the driver-core provides a
driver_overrideflag in structbus_type, which, once enabled, automatically registers generic sysfs callbacks, allowing userspace to modify thedriver_overridefield.
Quoted 6 lines
This series is based on v7.1-rc1 with no additional dependencies, hence those patches can be picked up by subsystems individually.
[1] https://lore.kernel.org/driver-core/[email protected]/
[2] https://bugzilla.kernel.org/show_bug.cgi?id=220789
[3] https://gitlab.com/driverctl/driverctl/-/blob/0.121/driverctl?ref_type=tags#L99
Quoted 1 line · Danilo Krummrich
[PATCH v2 0/5] treewide: Convert buses to use generic
driver_override
Applied, thanks!
Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git
[1/5] amba: use generic driver_override infrastructure
commit: 1947229f5f2a
[2/5] cdx: use generic driver_override infrastructure
commit: d541aa1897f6
[3/5] Drivers: hv: vmbus: use generic driver_override infrastructure
commit: 331d8900121a
[4/5] rpmsg: use generic driver_override infrastructure
commit: 55ced13c4292
[5/5] driver core: remove driver_set_override()
commit: 46def663dd34The patches will appear in the next linux-next integration (typically within 24 hours on weekdays).
The patches are in the driver-core-testing branch and will be promoted to driver-core-next after validation.