Skip to contentSkip to conversation

PCI: Add BAR index validation in pci_mmap_resource_range()

PATCH New Kaushlendra Kumar 1 participant 1 message Sep 29, 2025 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI: Add BAR index validation in pci_mmap_resource_range()

Add validation to ensure the BAR index is valid before accessing PCI resource information in pci_mmap_resource_range(). This prevents potential out-of-bounds access when an invalid BAR index is passed to the function.

The check uses the existing pci_bar_index_is_valid() helper to validate the BAR index early in the function, returning -EINVAL for invalid indices before any resource operations are performed.

---
 drivers/pci/mmap.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pci/mmap.c +3 -0
index 8da3347a95c4..7a26abc3169f 100644
@@ -28,6 +28,9 @@ int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
 	unsigned long size;
 	int ret;
 
+	if (!pci_bar_index_is_valid(bar))
+		return -EINVAL;
+
 	size = ((pci_resource_len(pdev, bar) - 1) >> PAGE_SHIFT) + 1;
 	if (vma->vm_pgoff + vma_pages(vma) > size)
 		return -EINVAL;
Signature · 1 line
2.34.1