Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

PCI: 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/DGRGTIRHA62X.3RY09D9SOK77P@kernel.org/ [1]
Reported-by: Gui-Dong Han <hanguidong02@gmail.com>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=220789
Fixes: 782a985d7af2 ("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>

+13 -44
+7 -4
drivers/pci/pci-driver.c
··· 138 138 { 139 139 struct pci_dynid *dynid; 140 140 const struct pci_device_id *found_id = NULL, *ids; 141 + int ret; 141 142 142 143 /* 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) 144 146 return NULL; 145 147 146 148 /* Look at the dynamic ids first, before the static ones */ ··· 166 164 * matching. 167 165 */ 168 166 if (found_id->override_only) { 169 - if (dev->driver_override) 167 + if (ret > 0) 170 168 return found_id; 171 169 } else { 172 170 return found_id; ··· 174 172 } 175 173 176 174 /* driver_override will always match, send a dummy id */ 177 - if (dev->driver_override) 175 + if (ret > 0) 178 176 return &pci_device_id_any; 179 177 return NULL; 180 178 } ··· 454 452 static inline bool pci_device_can_probe(struct pci_dev *pdev) 455 453 { 456 454 return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe || 457 - pdev->driver_override); 455 + device_has_driver_override(&pdev->dev)); 458 456 } 459 457 #else 460 458 static inline bool pci_device_can_probe(struct pci_dev *pdev) ··· 1724 1722 1725 1723 const struct bus_type pci_bus_type = { 1726 1724 .name = "pci", 1725 + .driver_override = true, 1727 1726 .match = pci_bus_match, 1728 1727 .uevent = pci_uevent, 1729 1728 .probe = pci_device_probe,
-28
drivers/pci/pci-sysfs.c
··· 615 615 static DEVICE_ATTR_RO(devspec); 616 616 #endif 617 617 618 - static ssize_t driver_override_store(struct device *dev, 619 - struct device_attribute *attr, 620 - const char *buf, size_t count) 621 - { 622 - struct pci_dev *pdev = to_pci_dev(dev); 623 - int ret; 624 - 625 - ret = driver_set_override(dev, &pdev->driver_override, buf, count); 626 - if (ret) 627 - return ret; 628 - 629 - return count; 630 - } 631 - 632 - static ssize_t driver_override_show(struct device *dev, 633 - struct device_attribute *attr, char *buf) 634 - { 635 - struct pci_dev *pdev = to_pci_dev(dev); 636 - ssize_t len; 637 - 638 - device_lock(dev); 639 - len = sysfs_emit(buf, "%s\n", pdev->driver_override); 640 - device_unlock(dev); 641 - return len; 642 - } 643 - static DEVICE_ATTR_RW(driver_override); 644 - 645 618 static struct attribute *pci_dev_attrs[] = { 646 619 &dev_attr_power_state.attr, 647 620 &dev_attr_resource.attr, ··· 642 669 #ifdef CONFIG_OF 643 670 &dev_attr_devspec.attr, 644 671 #endif 645 - &dev_attr_driver_override.attr, 646 672 &dev_attr_ari_enabled.attr, 647 673 NULL, 648 674 };
-1
drivers/pci/probe.c
··· 2488 2488 pci_release_of_node(pci_dev); 2489 2489 pcibios_release_device(pci_dev); 2490 2490 pci_bus_put(pci_dev->bus); 2491 - kfree(pci_dev->driver_override); 2492 2491 bitmap_free(pci_dev->dma_alias_mask); 2493 2492 dev_dbg(dev, "device released\n"); 2494 2493 kfree(pci_dev);
+2 -3
drivers/vfio/pci/vfio_pci_core.c
··· 1987 1987 pdev->is_virtfn && physfn == vdev->pdev) { 1988 1988 pci_info(vdev->pdev, "Captured SR-IOV VF %s driver_override\n", 1989 1989 pci_name(pdev)); 1990 - pdev->driver_override = kasprintf(GFP_KERNEL, "%s", 1991 - vdev->vdev.ops->name); 1992 - WARN_ON(!pdev->driver_override); 1990 + WARN_ON(device_set_driver_override(&pdev->dev, 1991 + vdev->vdev.ops->name)); 1993 1992 } else if (action == BUS_NOTIFY_BOUND_DRIVER && 1994 1993 pdev->is_virtfn && physfn == vdev->pdev) { 1995 1994 struct pci_driver *drv = pci_dev_driver(pdev);
+4 -2
drivers/xen/xen-pciback/pci_stub.c
··· 598 598 return err; 599 599 } 600 600 601 + static struct pci_driver xen_pcibk_pci_driver; 602 + 601 603 /* Called when 'bind'. This means we must _NOT_ call pci_reset_function or 602 604 * other functions that take the sysfs lock. */ 603 605 static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id) ··· 611 609 612 610 match = pcistub_match(dev); 613 611 614 - if ((dev->driver_override && 615 - !strcmp(dev->driver_override, PCISTUB_DRIVER_NAME)) || 612 + if (device_match_driver_override(&dev->dev, 613 + &xen_pcibk_pci_driver.driver) > 0 || 616 614 match) { 617 615 618 616 if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
-6
include/linux/pci.h
··· 575 575 u8 supported_speeds; /* Supported Link Speeds Vector */ 576 576 phys_addr_t rom; /* Physical address if not from BAR */ 577 577 size_t romlen; /* Length if not from BAR */ 578 - /* 579 - * Driver name to force a match. Do not set directly, because core 580 - * frees it. Use driver_set_override() to set or clear it. 581 - */ 582 - const char *driver_override; 583 - 584 578 unsigned long priv_flags; /* Private flags for the PCI driver */ 585 579 586 580 /* These methods index pci_reset_fn_methods[] */