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/[email protected]/ [1]
Reported-by: Gui-Dong Han <[email protected]>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220789
Fixes: 3cf385713460 ("ARM: 8256/1: driver coamba: add device binding path 
'driver_override'")
Signed-off-by: Danilo Krummrich <[email protected]>
---
 drivers/amba/bus.c       | 37 ++++++-------------------------------
 include/linux/amba/bus.h |  5 -----
 2 files changed, 6 insertions(+), 36 deletions(-)

diff --git a/drivers/amba/bus.c b/drivers/amba/bus.c
index 6d479caf89cb..d721d64a9858 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,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,
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.53.0


Reply via email to