Skip to content

Commit dfe950d

Browse files
Danilo Krummrichgregkh
authored andcommitted
PCI: use generic driver_override infrastructure
[ Upstream commit 10a4206 ] 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: 782a985 ("PCI: Introduce new device binding path using pci_dev.driver_override") Acked-by: Bjorn Helgaas <bhelgaas@google.com> Acked-by: Alex Williamson <alex@shazbot.org> Tested-by: Gui-Dong Han <hanguidong02@gmail.com> Reviewed-by: Gui-Dong Han <hanguidong02@gmail.com> Link: https://patch.msgid.link/20260324005919.2408620-6-dakr@kernel.org Signed-off-by: Danilo Krummrich <dakr@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 5e85e7a commit dfe950d

6 files changed

Lines changed: 13 additions & 44 deletions

File tree

drivers/pci/pci-driver.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,11 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
138138
{
139139
struct pci_dynid *dynid;
140140
const struct pci_device_id *found_id = NULL, *ids;
141+
int ret;
141142

142143
/* When driver_override is set, only bind to the matching driver */
143-
if (dev->driver_override && strcmp(dev->driver_override, drv->name))
144+
ret = device_match_driver_override(&dev->dev, &drv->driver);
145+
if (ret == 0)
144146
return NULL;
145147

146148
/* Look at the dynamic ids first, before the static ones */
@@ -164,15 +166,15 @@ static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
164166
* matching.
165167
*/
166168
if (found_id->override_only) {
167-
if (dev->driver_override)
169+
if (ret > 0)
168170
return found_id;
169171
} else {
170172
return found_id;
171173
}
172174
}
173175

174176
/* driver_override will always match, send a dummy id */
175-
if (dev->driver_override)
177+
if (ret > 0)
176178
return &pci_device_id_any;
177179
return NULL;
178180
}
@@ -423,7 +425,7 @@ static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
423425
static inline bool pci_device_can_probe(struct pci_dev *pdev)
424426
{
425427
return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe ||
426-
pdev->driver_override);
428+
device_has_driver_override(&pdev->dev));
427429
}
428430
#else
429431
static inline bool pci_device_can_probe(struct pci_dev *pdev)
@@ -1677,6 +1679,7 @@ static void pci_dma_cleanup(struct device *dev)
16771679

16781680
const struct bus_type pci_bus_type = {
16791681
.name = "pci",
1682+
.driver_override = true,
16801683
.match = pci_bus_match,
16811684
.uevent = pci_uevent,
16821685
.probe = pci_device_probe,

drivers/pci/pci-sysfs.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -606,33 +606,6 @@ static ssize_t devspec_show(struct device *dev,
606606
static DEVICE_ATTR_RO(devspec);
607607
#endif
608608

609-
static ssize_t driver_override_store(struct device *dev,
610-
struct device_attribute *attr,
611-
const char *buf, size_t count)
612-
{
613-
struct pci_dev *pdev = to_pci_dev(dev);
614-
int ret;
615-
616-
ret = driver_set_override(dev, &pdev->driver_override, buf, count);
617-
if (ret)
618-
return ret;
619-
620-
return count;
621-
}
622-
623-
static ssize_t driver_override_show(struct device *dev,
624-
struct device_attribute *attr, char *buf)
625-
{
626-
struct pci_dev *pdev = to_pci_dev(dev);
627-
ssize_t len;
628-
629-
device_lock(dev);
630-
len = sysfs_emit(buf, "%s\n", pdev->driver_override);
631-
device_unlock(dev);
632-
return len;
633-
}
634-
static DEVICE_ATTR_RW(driver_override);
635-
636609
static struct attribute *pci_dev_attrs[] = {
637610
&dev_attr_power_state.attr,
638611
&dev_attr_resource.attr,
@@ -660,7 +633,6 @@ static struct attribute *pci_dev_attrs[] = {
660633
#ifdef CONFIG_OF
661634
&dev_attr_devspec.attr,
662635
#endif
663-
&dev_attr_driver_override.attr,
664636
&dev_attr_ari_enabled.attr,
665637
NULL,
666638
};

drivers/pci/probe.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2373,7 +2373,6 @@ static void pci_release_dev(struct device *dev)
23732373
pci_release_of_node(pci_dev);
23742374
pcibios_release_device(pci_dev);
23752375
pci_bus_put(pci_dev->bus);
2376-
kfree(pci_dev->driver_override);
23772376
bitmap_free(pci_dev->dma_alias_mask);
23782377
dev_dbg(dev, "device released\n");
23792378
kfree(pci_dev);

drivers/vfio/pci/vfio_pci_core.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2014,9 +2014,8 @@ static int vfio_pci_bus_notifier(struct notifier_block *nb,
20142014
pdev->is_virtfn && physfn == vdev->pdev) {
20152015
pci_info(vdev->pdev, "Captured SR-IOV VF %s driver_override\n",
20162016
pci_name(pdev));
2017-
pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
2018-
vdev->vdev.ops->name);
2019-
WARN_ON(!pdev->driver_override);
2017+
WARN_ON(device_set_driver_override(&pdev->dev,
2018+
vdev->vdev.ops->name));
20202019
} else if (action == BUS_NOTIFY_BOUND_DRIVER &&
20212020
pdev->is_virtfn && physfn == vdev->pdev) {
20222021
struct pci_driver *drv = pci_dev_driver(pdev);

drivers/xen/xen-pciback/pci_stub.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,8 @@ static int pcistub_seize(struct pci_dev *dev,
618618
return err;
619619
}
620620

621+
static struct pci_driver xen_pcibk_pci_driver;
622+
621623
/* Called when 'bind'. This means we must _NOT_ call pci_reset_function or
622624
* other functions that take the sysfs lock. */
623625
static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
@@ -629,8 +631,8 @@ static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
629631

630632
match = pcistub_match(dev);
631633

632-
if ((dev->driver_override &&
633-
!strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) ||
634+
if (device_match_driver_override(&dev->dev,
635+
&xen_pcibk_pci_driver.driver) > 0 ||
634636
match) {
635637

636638
if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL

include/linux/pci.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,12 +540,6 @@ struct pci_dev {
540540
u8 supported_speeds; /* Supported Link Speeds Vector */
541541
phys_addr_t rom; /* Physical address if not from BAR */
542542
size_t romlen; /* Length if not from BAR */
543-
/*
544-
* Driver name to force a match. Do not set directly, because core
545-
* frees it. Use driver_set_override() to set or clear it.
546-
*/
547-
const char *driver_override;
548-
549543
unsigned long priv_flags; /* Private flags for the PCI driver */
550544

551545
/* These methods index pci_reset_fn_methods[] */

0 commit comments

Comments
 (0)