From: Manish Honap vfio_find_cap_start() walks back through pci_config_map while the bytes hold the same capability ID. For two capabilities with the same ID back to back, such as two DVSECs or two Vendor-Specific capabilities (PCI_CAP_ID_VNDR), it returns the start of the first one for an offset in the second. The header of the second one is then at a non-zero offset, and vfio_direct_config_read() returns its ID and Next pointer from the device instead of vconfig. The user leaves the virtualized capability chain there and follows the physical one, which includes the capabilities vfio hides. Take the start from the capability chains in vconfig, which skip hidden capabilities as pci_config_map does. Map at least a full header for a capability that reports a shorter length, so that the chain in vconfig is complete. Fixes: 89e1f7d4c66d ("vfio: Add PCI device driver") Signed-off-by: Manish Honap --- drivers/vfio/pci/vfio_pci_config.c | 48 +++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 9914f3ac69ae..7eb174b6d5fc 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -1129,21 +1129,42 @@ int __init vfio_pci_init_perm_bits(void) return ret; } +/* Abutting capabilities with the same ID cannot be told apart in the map */ static int vfio_find_cap_start(struct vfio_pci_core_device *vdev, int pos) { - u8 cap; - int base = (pos >= PCI_CFG_SPACE_SIZE) ? PCI_CFG_SPACE_SIZE : - PCI_STD_HEADER_SIZEOF; - cap = vdev->pci_config_map[pos]; + int loops = (PCI_CFG_SPACE_SIZE - PCI_STD_HEADER_SIZEOF) / PCI_CAP_SIZEOF; + u8 *map = vdev->pci_config_map; + u8 cpos = vdev->vconfig[PCI_CAPABILITY_LIST]; + int start = 0; - if (cap == PCI_CAP_ID_BASIC) - return 0; + WARN_ON_ONCE(map[pos] >= PCI_CAP_ID_INVALID_VIRT); + + while (cpos && loops--) { + if (cpos <= pos && cpos > start && map[cpos] == map[pos]) + start = cpos; + cpos = vdev->vconfig[cpos + PCI_CAP_LIST_NEXT]; + } + + return start; +} + +static int vfio_find_ecap_start(struct vfio_pci_core_device *vdev, int pos) +{ + int loops = (vdev->pdev->cfg_size - PCI_CFG_SPACE_SIZE) / PCI_CAP_SIZEOF; + int epos = PCI_CFG_SPACE_SIZE, start = 0; + u8 *map = vdev->pci_config_map; + + WARN_ON_ONCE(map[pos] >= PCI_CAP_ID_INVALID_VIRT); + + while (loops-- && epos >= PCI_CFG_SPACE_SIZE) { + u32 header = le32_to_cpu(*(__le32 *)&vdev->vconfig[epos]); - /* XXX Can we have to abutting capabilities of the same type? */ - while (pos - 1 >= base && vdev->pci_config_map[pos - 1] == cap) - pos--; + if (epos <= pos && epos > start && map[epos] == map[pos]) + start = epos; + epos = PCI_EXT_CAP_NEXT(header); + } - return pos; + return start; } static int vfio_msi_config_read(struct vfio_pci_core_device *vdev, int pos, @@ -1594,6 +1615,9 @@ static int vfio_cap_init(struct vfio_pci_core_device *vdev) continue; } + /* vfio_find_cap_start() reads the header from vconfig */ + len = max(len, PCI_CAP_SIZEOF); + /* Sanity check, do we overlap other capabilities? */ for (i = 0; i < len; i++) { if (likely(map[pos + i] == PCI_CAP_ID_INVALID)) @@ -1683,6 +1707,8 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev) hidden = true; } + len = max(len, PCI_CAP_SIZEOF); + for (i = 0; i < len; i++) { if (likely(map[epos + i] == PCI_CAP_ID_INVALID)) continue; @@ -1931,7 +1957,7 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev, else perm = &ecap_perms[cap_id]; - cap_start = vfio_find_cap_start(vdev, *ppos); + cap_start = vfio_find_ecap_start(vdev, *ppos); } else { WARN_ON(cap_id > PCI_CAP_ID_MAX); -- 2.53.0