PCI: Avoid potential out-of-bounds read in pci_dev_for_each_resource()
PATCH 1/1 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. mighe 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 get 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)."
--- include/linux/pci.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
include/linux/pci.h +6 -6
@@ -2127,14 +2127,14 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma); (pci_resource_end ((dev), (bar)) ? \ resource_size(pci_resource_n((dev), (bar))) : 0) -#define __pci_dev_for_each_res0 (dev, res, ...) \ - for (unsigned int __b = 0; \ - res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \ +#define __pci_dev_for_each_res0 (dev, res, ...) \ + for (unsigned int __b = 0; \ + __b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \ __b++) -#define __pci_dev_for_each_res1 (dev, res, __b) \ - for (__b = 0; \ - res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \ +#define __pci_dev_for_each_res1 (dev, res, __b) \ + for (__b = 0; \ + __b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \ __b++)#define pci_dev_for_each_resource (dev, res, ...) \
Signature · 1 line
2.40.0.1.gaa8946217a0b
Quoted 12 lines · Andy Shevchenko
Coverity complains that pointer in the
pci_dev_for_each_resource()may be wrong, i.e. mighe 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 get 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)."
Quoted 5 lines
Fixes 09cc90063240 ("PCI: Introduce pci_dev_for_each_resource()")Reported-by Bjorn Helgaas <[email protected]>Suggested-by Jonas Gorski <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>
Thanks, Andy, I'll look at this soon after v6.7-rc1 (probably Nov 12).
Quoted 32 lines
--- include/linux/pci.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)include/linux/pci.h +6
-6@@ -2127,14 +2127,14 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma); (pci_resource_end ((dev), (bar)) ? \ resource_size(pci_resource_n((dev), (bar))) : 0) -#define __pci_dev_for_each_res0 (dev, res, ...) \ - for (unsigned int __b = 0; \ - res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \ +#define __pci_dev_for_each_res0 (dev, res, ...) \ + for (unsigned int __b = 0; \ + __b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \ __b++) -#define __pci_dev_for_each_res1 (dev, res, __b) \ - for (__b = 0; \ - res = pci_resource_n(dev, __b), __b < PCI_NUM_RESOURCES; \ +#define __pci_dev_for_each_res1 (dev, res, __b) \ + for (__b = 0; \ + __b < PCI_NUM_RESOURCES && (res = pci_resource_n(dev, __b)); \ __b++)#define pci_dev_for_each_resource (dev, res, ...) \Signature · 1 line
2.40.0.1.gaa8946217a0b
Quoted 13 lines · Bjorn Helgaas
Quoted 12 lines · Andy Shevchenko
Coverity complains that pointer in the
pci_dev_for_each_resource()may be wrong, i.e. mighe 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 get 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)."
Quoted 7 lines
Quoted 5 lines
Fixes 09cc90063240 ("PCI: Introduce pci_dev_for_each_resource()")Reported-by Bjorn Helgaas <[email protected]>Suggested-by Jonas Gorski <[email protected]>Signed-off-by Andy Shevchenko <[email protected]>Thanks, Andy, I'll look at this soon after v6.7-rc1 (probably Nov 12).
Since it's anyway a non-critical issue, take your time. Based on the tags it may be backported if needed, so business as usual.
Thank you!
Signature · 2 lines
With Best Regards, Andy Shevchenko
Bjorn Helgaas
AppliedFrom: Bjorn Helgaas <[email protected]>
Quoted 8 lines · Andy Shevchenko
Coverity complains that pointer in the
pci_dev_for_each_resource()may be wrong, i.e. mighe 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 get fixed, hence the proposed change.
[...]
Applied to "resource" for v6.8, thanks!
[1/1] PCI: Avoid potential out-of-bounds read in pci_dev_for_each_resource()
commit: 3171e46d677a668eed3086da78671f1e4f5b8405
Best regards,
Signature · 1 line
Bjorn Helgaas <[email protected]>