]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
drm/xe/configfs: Enforce canonical device names
authorMichal Wajdeczko <michal.wajdeczko@intel.com>
Tue, 22 Jul 2025 14:10:55 +0000 (16:10 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 13 Nov 2025 20:36:55 +0000 (15:36 -0500)
[ Upstream commit 400a6da1e967c4f117e4757412df06dcfaea0e6a ]

While we expect config directory names to match PCI device name,
currently we are only scanning provided names for domain, bus,
device and function numbers, without checking their format.
This would pass slightly broken entries like:

  /sys/kernel/config/xe/
  ├── 0000:00:02.0000000000000
  │   └── ...
  ├── 0000:00:02.0x
  │   └── ...
  ├──  0: 0: 2. 0
  │   └── ...
  └── 0:0:2.0
      └── ...

To avoid such mistakes, check if the name provided exactly matches
the canonical PCI device address format, which we recreated from
the parsed BDF data. Also simplify scanf format as it can't really
catch all formatting errors.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Lucas De Marchi <lucas.demarchi@intel.com>
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Link: https://lore.kernel.org/r/20250722141059.30707-3-michal.wajdeczko@intel.com
Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/gpu/drm/xe/xe_configfs.c

index 58c1f397c68c9461340b9a30e859a3366e533af1..797508cc6eb173ce7db7479f76557903628f11ea 100644 (file)
@@ -259,12 +259,19 @@ static struct config_group *xe_config_make_device_group(struct config_group *gro
        unsigned int domain, bus, slot, function;
        struct xe_config_device *dev;
        struct pci_dev *pdev;
+       char canonical[16];
        int ret;
 
-       ret = sscanf(name, "%04x:%02x:%02x.%x", &domain, &bus, &slot, &function);
+       ret = sscanf(name, "%x:%x:%x.%x", &domain, &bus, &slot, &function);
        if (ret != 4)
                return ERR_PTR(-EINVAL);
 
+       ret = scnprintf(canonical, sizeof(canonical), "%04x:%02x:%02x.%d", domain, bus,
+                       PCI_SLOT(PCI_DEVFN(slot, function)),
+                       PCI_FUNC(PCI_DEVFN(slot, function)));
+       if (ret != 12 || strcmp(name, canonical))
+               return ERR_PTR(-EINVAL);
+
        pdev = pci_get_domain_bus_and_slot(domain, bus, PCI_DEVFN(slot, function));
        if (!pdev)
                return ERR_PTR(-ENODEV);