AMBA devices still keep driver_override in bus-private storage.

The sysfs write side updates that string through driver_set_override(),
which replaces the pointer and frees the old value. However,
driver_match_device() can call amba_match() from __driver_attach()
without holding the device lock, and amba_match() still dereferences
that private pointer directly.

That means a bind/unbind or reprobe path can race with a concurrent
driver_override update and make amba_match() compare against freed
memory.

Fix this by switching AMBA to the driver-core driver_override
infrastructure. This lets the core own the sysfs attribute and storage,
and uses device_match_driver_override() for the locked read in the match
path.

Link: https://lore.kernel.org/driver-core/[email protected]/
Fixes: 3cf385713460 ("ARM: 8256/1: driver coamba: add device binding path 
'driver_override'")
Cc: [email protected]
Signed-off-by: Runyu Xiao <[email protected]>
---
drivers/amba/bus.c       | 35 +++++------------------------------
include/linux/amba/bus.h |  5 -----
 2 files changed, 5 insertions(+), 35 deletions(-)

diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
index 6d479caf89cb..df8333f90906 100644
--- a/drivers/amba/bus.c
+++ b/drivers/amba/bus.c
@@ -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,6 +181,7 @@ 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) {
@@ -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;
 }
@@ -435,6 +409,7 @@ static const struct dev_pm_ops amba_pm = {
  */
 const struct bus_type amba_bustype = {
        .name           = "amba",
+       .driver_override = true,
        .dev_groups     = amba_dev_groups,
        .match          = amba_match,
        .uevent         = amba_uevent,
diff --git a/include/linux/amba/bus.h b/include/linux/amba/bus.h
index 9946276aff73..6c54d5c0d21f 100644
--- a/include/linux/amba/bus.h
+++ b/include/linux/amba/bus.h
@@ -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 {
-- 
2.34.1

Reply via email to