From: Manish Honap <[email protected]>

vfio_pci_bar_rw() keeps the excluded ranges of a BAR off the device, but
two other paths can reach BAR MMIO without it.

An ioeventfd writes the BAR directly when it fires, through
vfio_pci_core_iowriteN(). Refuse an ioeventfd whose range overlaps a
write-excluded range. This replaces the open-coded MSI-X test: the MSI-X
table is a write-excluded range, and the BAR length test already refuses
the part of a table past the BAR.

A dma-buf export of a BAR range lets an importer map it for DMA, which
reaches a range that the BAR keeps off mmap. Refuse an export whose
ranges overlap an mmap-excluded range in validate_dmabuf_input(), so the
dma-buf follows the same rule as mmap.

Suggested-by: Alex Williamson <[email protected]>
Signed-off-by: Manish Honap <[email protected]>
---
 drivers/vfio/pci/vfio_pci_core.c   |  4 ++--
 drivers/vfio/pci/vfio_pci_dmabuf.c | 10 ++++++++--
 drivers/vfio/pci/vfio_pci_priv.h   |  3 +++
 drivers/vfio/pci/vfio_pci_rdwr.c   |  6 ++----
 4 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
index 0b9da823456e..5d54d624982f 100644
--- a/drivers/vfio/pci/vfio_pci_core.c
+++ b/drivers/vfio/pci/vfio_pci_core.c
@@ -988,8 +988,8 @@ static int vfio_pci_for_each_slot_or_bus(struct pci_dev 
*pdev,
        return walk.ret;
 }
 
-static bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev,
-                                       int bar, u64 start, u64 len, u32 flags)
+bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
+                                u64 start, u64 len, u32 flags)
 {
        struct range r = DEFINE_RANGE(start, start + len - 1);
        struct vfio_pci_excluded_range *x;
diff --git a/drivers/vfio/pci/vfio_pci_dmabuf.c 
b/drivers/vfio/pci/vfio_pci_dmabuf.c
index 7d1d34029b95..6f28cc285e78 100644
--- a/drivers/vfio/pci/vfio_pci_dmabuf.c
+++ b/drivers/vfio/pci/vfio_pci_dmabuf.c
@@ -193,7 +193,8 @@ int vfio_pci_core_get_dmabuf_phys(struct 
vfio_pci_core_device *vdev,
 }
 EXPORT_SYMBOL_GPL(vfio_pci_core_get_dmabuf_phys);
 
-static int validate_dmabuf_input(struct vfio_device_feature_dma_buf *dma_buf,
+static int validate_dmabuf_input(struct vfio_pci_core_device *vdev,
+                                struct vfio_device_feature_dma_buf *dma_buf,
                                 struct vfio_region_dma_range *dma_ranges,
                                 size_t *lengthp)
 {
@@ -207,6 +208,11 @@ static int validate_dmabuf_input(struct 
vfio_device_feature_dma_buf *dma_buf,
                if (!len || !PAGE_ALIGNED(offset) || !PAGE_ALIGNED(len))
                        return -EINVAL;
 
+               if (vfio_pci_bar_range_excluded(vdev, dma_buf->region_index,
+                                               offset, len,
+                                               VFIO_PCI_EXCLUDE_MMAP))
+                       return -EINVAL;
+
                if (check_add_overflow(length, len, &length))
                        return -EINVAL;
        }
@@ -260,7 +266,7 @@ int vfio_pci_core_feature_dma_buf(struct 
vfio_pci_core_device *vdev, u32 flags,
        if (IS_ERR(dma_ranges))
                return PTR_ERR(dma_ranges);
 
-       ret = validate_dmabuf_input(&get_dma_buf, dma_ranges, &length);
+       ret = validate_dmabuf_input(vdev, &get_dma_buf, dma_ranges, &length);
        if (ret)
                goto err_free_ranges;
 
diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h
index 6def038cb213..a969ae3025c9 100644
--- a/drivers/vfio/pci/vfio_pci_priv.h
+++ b/drivers/vfio/pci/vfio_pci_priv.h
@@ -55,6 +55,9 @@ struct vfio_pci_excluded_range {
        u32                     flags;
 };
 
+bool vfio_pci_bar_range_excluded(struct vfio_pci_core_device *vdev, int bar,
+                                u64 start, u64 len, u32 flags);
+
 #ifdef CONFIG_VFIO_PCI_VGA
 ssize_t vfio_pci_vga_rw(struct vfio_pci_core_device *vdev, char __user *buf,
                        size_t count, loff_t *ppos, bool iswrite);
diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c
index 04aed725325c..e620798f9007 100644
--- a/drivers/vfio/pci/vfio_pci_rdwr.c
+++ b/drivers/vfio/pci/vfio_pci_rdwr.c
@@ -475,10 +475,8 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, 
loff_t offset,
        if (pos + count > pci_resource_len(pdev, bar))
                return -EINVAL;
 
-       /* Disallow ioeventfds working around MSI-X table writes */
-       if (bar == vdev->msix_bar &&
-           !(pos + count <= vdev->msix_offset ||
-             pos >= vdev->msix_offset + vdev->msix_size))
+       if (vfio_pci_bar_range_excluded(vdev, bar, pos, count,
+                                       VFIO_PCI_EXCLUDE_WRITE))
                return -EINVAL;
 
        io = vfio_pci_core_get_iomap(vdev, bar);
-- 
2.53.0

Reply via email to