Skip to content

Commit 3171e46

Browse files
andy-shevbjorn-helgaas
authored andcommitted
PCI: Avoid potential out-of-bounds read in pci_dev_for_each_resource()
Coverity complains that pointer in the pci_dev_for_each_resource() may be wrong, i.e., might be used for the out-of-bounds read. There is no actual issue right now because we have another check afterwards and the out-of-bounds read is not being performed. In any case it's better code with this fixed, hence the proposed change. As Jonas pointed out "It probably makes the code slightly less performant as res will now be checked for being not NULL (which will always be true), but I doubt it will be significant (or in any hot paths)." Fixes: 09cc900 ("PCI: Introduce pci_dev_for_each_resource()") Reported-by: Bjorn Helgaas <[email protected]> Closes: https://lore.kernel.org/r/20230509182122.GA1259567@bhelgaas Suggested-by: Jonas Gorski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Andy Shevchenko <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]>
1 parent b85ea95 commit 3171e46

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

include/linux/pci.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2127,14 +2127,14 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
21272127
(pci_resource_end((dev), (bar)) ? \
21282128
resource_size(pci_resource_n((dev), (bar))) : 0)
21292129

2130-
#define __pci_dev_for_each_res0(dev, res, ...) \
2131-
for (unsigned int __b = 0; \
2132-
res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \
2130+
#define __pci_dev_for_each_res0(dev, res, ...) \
2131+
for (unsigned int __b = 0; \
2132+
__b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \
21332133
__b++)
21342134

2135-
#define __pci_dev_for_each_res1(dev, res, __b) \
2136-
for (__b = 0; \
2137-
res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \
2135+
#define __pci_dev_for_each_res1(dev, res, __b) \
2136+
for (__b = 0; \
2137+
__b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \
21382138
__b++)
21392139

21402140
#define pci_dev_for_each_resource(dev, res, ...) \

0 commit comments

Comments
 (0)