Skip to contentSkip to conversation

vfio/pci: Make VFIO_PCI_OFFSET_TO_INDEX() return unsigned

PATCH Applied Matt Evans 2 participants 2 messages May 11 – May 14

Subsystems (2)
lore
Share & tools
lore

Matt Evans

author
#0 lore raw

PATCH 1/1 vfio/pci: Make VFIO_PCI_OFFSET_TO_INDEX() return unsigned

VFIO_PCI_OFFSET_TO_INDEX() is used in several places with a signed parameter (e.g. loff_t). Because it makes no sense for a BAR/resource index to be negative, enforce this in the macro.

This fixes at least one current issue, where vfio_pci_ioeventfd() uses this macro with an unvalidated signed loff_t returned into a signed type, leading to a possible negative array access. This instance does test against an out-of-bounds positive value, so treating the index as unsigned fixes this issue.

Fixes 89e1f7d4c66d8 ("vfio: Add PCI device driver")

---

This patch follows up on a nearby issue found during review, discussed here:

https://lore.kernel.org/kvm/[email protected]/

 include/linux/vfio_pci_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/vfio_pci_core.h +1 -1
index 2ebba746c18f..89165b769e5c 100644
@@ -21,7 +21,7 @@
 #define VFIO_PCI_CORE_H
 
 #define VFIO_PCI_OFFSET_SHIFT   40
-#define VFIO_PCI_OFFSET_TO_INDEX(off)	(off >> VFIO_PCI_OFFSET_SHIFT)
+#define VFIO_PCI_OFFSET_TO_INDEX(off)	((u64)(off) >> VFIO_PCI_OFFSET_SHIFT)
 #define VFIO_PCI_INDEX_TO_OFFSET(index)	((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
 #define VFIO_PCI_OFFSET_MASK	(((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
 
Signature · 1 line
2.47.3
maintainer Applied
#1 lore raw
Quoted 27 lines · On Mon, 11 May 2026 07:46:42 -0700 Matt Evans <[email protected]> wrote:

VFIO_PCI_OFFSET_TO_INDEX() is used in several places with a signed parameter (e.g. loff_t). Because it makes no sense for a BAR/resource index to be negative, enforce this in the macro.

This fixes at least one current issue, where vfio_pci_ioeventfd() uses this macro with an unvalidated signed loff_t returned into a signed type, leading to a possible negative array access. This instance does test against an out-of-bounds positive value, so treating the index as unsigned fixes this issue.

Fixes 89e1f7d4c66d8 ("vfio: Add PCI device driver")

---

This patch follows up on a nearby issue found during review, discussed here:

https://lore.kernel.org/kvm/[email protected]/

 include/linux/vfio_pci_core.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/vfio_pci_core.h +0 -0
index 2ebba746c18f..89165b769e5c 100644

include/linux/vfio_pci_core.h line 26

Quoted 7 lines
include/linux/vfio_pci_core.h +1 -1
@@ -21,7 +21,7 @@
 #define VFIO_PCI_CORE_H
 
 #define VFIO_PCI_OFFSET_SHIFT   40
-#define VFIO_PCI_OFFSET_TO_INDEX(off)	(off >> VFIO_PCI_OFFSET_SHIFT)
+#define VFIO_PCI_OFFSET_TO_INDEX(off)	((u64)(off) >> VFIO_PCI_OFFSET_SHIFT)
 #define VFIO_PCI_INDEX_TO_OFFSET(index)	((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
 #define VFIO_PCI_OFFSET_MASK	(((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)

Applied to vfio for-linus branch for v7.1-rc. Thanks,

Alex