Skip to content

Commit 2b38efc

Browse files
author
Danilo Krummrich
committed
driver core: platform: 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/[email protected]/ [1] Reported-by: Gui-Dong Han <[email protected]> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220789 Fixes: 3d713e0 ("driver core: platform: add device binding path 'driver_override'") Reviewed-by: Greg Kroah-Hartman <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Danilo Krummrich <[email protected]>
1 parent c5f60e3 commit 2b38efc

6 files changed

Lines changed: 13 additions & 48 deletions

File tree

drivers/base/platform.c

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ static void platform_device_release(struct device *dev)
603603
kfree(pa->pdev.dev.platform_data);
604604
kfree(pa->pdev.mfd_cell);
605605
kfree(pa->pdev.resource);
606-
kfree(pa->pdev.driver_override);
607606
kfree(pa);
608607
}
609608

@@ -1306,38 +1305,9 @@ static ssize_t numa_node_show(struct device *dev,
13061305
}
13071306
static DEVICE_ATTR_RO(numa_node);
13081307

1309-
static ssize_t driver_override_show(struct device *dev,
1310-
struct device_attribute *attr, char *buf)
1311-
{
1312-
struct platform_device *pdev = to_platform_device(dev);
1313-
ssize_t len;
1314-
1315-
device_lock(dev);
1316-
len = sysfs_emit(buf, "%s\n", pdev->driver_override);
1317-
device_unlock(dev);
1318-
1319-
return len;
1320-
}
1321-
1322-
static ssize_t driver_override_store(struct device *dev,
1323-
struct device_attribute *attr,
1324-
const char *buf, size_t count)
1325-
{
1326-
struct platform_device *pdev = to_platform_device(dev);
1327-
int ret;
1328-
1329-
ret = driver_set_override(dev, &pdev->driver_override, buf, count);
1330-
if (ret)
1331-
return ret;
1332-
1333-
return count;
1334-
}
1335-
static DEVICE_ATTR_RW(driver_override);
1336-
13371308
static struct attribute *platform_dev_attrs[] = {
13381309
&dev_attr_modalias.attr,
13391310
&dev_attr_numa_node.attr,
1340-
&dev_attr_driver_override.attr,
13411311
NULL,
13421312
};
13431313

@@ -1377,10 +1347,12 @@ static int platform_match(struct device *dev, const struct device_driver *drv)
13771347
{
13781348
struct platform_device *pdev = to_platform_device(dev);
13791349
struct platform_driver *pdrv = to_platform_driver(drv);
1350+
int ret;
13801351

13811352
/* When driver_override is set, only bind to the matching driver */
1382-
if (pdev->driver_override)
1383-
return !strcmp(pdev->driver_override, drv->name);
1353+
ret = device_match_driver_override(dev, drv);
1354+
if (ret >= 0)
1355+
return ret;
13841356

13851357
/* Attempt an OF style match first */
13861358
if (of_driver_match_device(dev, drv))
@@ -1516,6 +1488,7 @@ static const struct dev_pm_ops platform_dev_pm_ops = {
15161488
const struct bus_type platform_bus_type = {
15171489
.name = "platform",
15181490
.dev_groups = platform_dev_groups,
1491+
.driver_override = true,
15191492
.match = platform_match,
15201493
.uevent = platform_uevent,
15211494
.probe = platform_probe,

drivers/bus/simple-pm-bus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ static int simple_pm_bus_probe(struct platform_device *pdev)
3636
* that's not listed in simple_pm_bus_of_match. We don't want to do any
3737
* of the simple-pm-bus tasks for these devices, so return early.
3838
*/
39-
if (pdev->driver_override)
39+
if (device_has_driver_override(&pdev->dev))
4040
return 0;
4141

4242
match = of_match_device(dev->driver->of_match_table, dev);
@@ -78,7 +78,7 @@ static void simple_pm_bus_remove(struct platform_device *pdev)
7878
{
7979
const void *data = of_device_get_match_data(&pdev->dev);
8080

81-
if (pdev->driver_override || data)
81+
if (device_has_driver_override(&pdev->dev) || data)
8282
return;
8383

8484
dev_dbg(&pdev->dev, "%s\n", __func__);

drivers/clk/imx/clk-scu.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -706,8 +706,7 @@ struct clk_hw *imx_clk_scu_alloc_dev(const char *name,
706706
if (ret)
707707
goto put_device;
708708

709-
ret = driver_set_override(&pdev->dev, &pdev->driver_override,
710-
"imx-scu-clk", strlen("imx-scu-clk"));
709+
ret = device_set_driver_override(&pdev->dev, "imx-scu-clk");
711710
if (ret)
712711
goto put_device;
713712

drivers/slimbus/qcom-ngd-ctrl.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,10 +1535,8 @@ static int of_qcom_slim_ngd_register(struct device *parent,
15351535
ngd->id = id;
15361536
ngd->pdev->dev.parent = parent;
15371537

1538-
ret = driver_set_override(&ngd->pdev->dev,
1539-
&ngd->pdev->driver_override,
1540-
QCOM_SLIM_NGD_DRV_NAME,
1541-
strlen(QCOM_SLIM_NGD_DRV_NAME));
1538+
ret = device_set_driver_override(&ngd->pdev->dev,
1539+
QCOM_SLIM_NGD_DRV_NAME);
15421540
if (ret) {
15431541
platform_device_put(ngd->pdev);
15441542
kfree(ngd);

include/linux/platform_device.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ struct platform_device {
3131
struct resource *resource;
3232

3333
const struct platform_device_id *id_entry;
34-
/*
35-
* Driver name to force a match. Do not set directly, because core
36-
* frees it. Use driver_set_override() to set or clear it.
37-
*/
38-
const char *driver_override;
3934

4035
/* MFD cell pointer */
4136
struct mfd_cell *mfd_cell;

sound/soc/samsung/i2s.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1360,10 +1360,10 @@ static int i2s_create_secondary_device(struct samsung_i2s_priv *priv)
13601360
if (!pdev_sec)
13611361
return -ENOMEM;
13621362

1363-
pdev_sec->driver_override = kstrdup("samsung-i2s", GFP_KERNEL);
1364-
if (!pdev_sec->driver_override) {
1363+
ret = device_set_driver_override(&pdev_sec->dev, "samsung-i2s");
1364+
if (ret) {
13651365
platform_device_put(pdev_sec);
1366-
return -ENOMEM;
1366+
return ret;
13671367
}
13681368

13691369
ret = platform_device_add(pdev_sec);

0 commit comments

Comments
 (0)