mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings
@ 2026-09-29  9:48 Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 1/7] vfio/platform: prevent read-only region mappings from becoming writable Abdifatah Suruur
                   ` (6 more replies)
  0 siblings, 7 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

v2 changes (per Alex Williamson's review of v1):
- 2/7, 3/7: consolidated the duplicated !WRITE flag test into a single
  block, as Pranjal Shrivastava suggested and 1/7 already does.
- 4/7: carried Eric Auger's Reviewed-by.
- Rebuilt with git format-patch from commits, so every patch carries a
  proper diffstat.

These seven patches are hygiene/hardening cleanups of the MMIO region
mmap() paths in vfio-platform, vfio/fsl-mc and vfio/cdx, all in the
same class as commit a5edadbae57e ("ptp: vmclock: prevent read-only
mappings from becoming writable").  They do two things:

 1. Clear VM_MAYWRITE on regions without VFIO_REGION_INFO_FLAG_WRITE
    so that mprotect() cannot upgrade a read-only MMIO mapping to
    writable (patches 1-3).

 2. Keep vma->vm_pgoff in the logical VFIO offset space instead of
    overwriting it with the physical frame number, and reject
    non-shared mmaps where the remap_pfn_range() COW special case
    would overwrite it anyway (patches 4-7).

Proper scoping: no in-tree platform, fsl-mc or cdx device currently
publishes a region without the WRITE flag, and none of the three
drivers calls unmap_mapping_range(), so none of these issues is
reachable today.  The vm_pgoff changes preserve the documented VFIO
core contract that every device mmap is linked to the device inode's
i_mapping so it can be revoked; that shared namespace came from
commit b7c5e64fecfa ("vfio: Create vfio_fs_type with inode per device"), so
Fixes: tags were dropped from patches 4-7 and kept on patches 1-3,
pointing at the commits that added each driver's MMIO mmap support.

Previously posted as standalone patches, then as a series per Alex's
request.  Reviewed-by tags from those reviews are carried on the
affected patches.


Abdifatah Suruur (7):
  vfio/platform: prevent read-only region mappings from becoming
    writable
  vfio/fsl-mc: prevent read-only region mappings from becoming writable
  vfio/cdx: prevent read-only region mappings from becoming writable
  vfio/platform: keep logical vm_pgoff in MMIO region mmap
  vfio/fsl-mc: keep logical vm_pgoff in MMIO region mmap
  vfio/cdx: keep logical vm_pgoff in MMIO region mmap
  vfio/cdx: reject non-shared MMIO mmaps

 drivers/vfio/cdx/main.c                      | 17 ++++++++++++-----
 drivers/vfio/fsl-mc/vfio_fsl_mc.c            | 15 +++++++++------
 drivers/vfio/platform/vfio_platform_common.c | 15 +++++++++------
 3 files changed, 30 insertions(+), 17 deletions(-)

-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/7] vfio/platform: prevent read-only region mappings from becoming writable
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 2/7] vfio/fsl-mc: " Abdifatah Suruur
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_platform_mmap() rejects writable mappings of regions without the
WRITE flag, but leaves VM_MAYWRITE set.  Userspace can map such a region
read-only and then upgrade the mapping to writable with mprotect().

Clear VM_MAYWRITE for regions without the WRITE flag, as i915 does for
its read-only objects and as fixed in drm/vc4 (CVE-2026-68445),
drm/panthor (CVE-2024-53071) and commit a5edadbae57e ("ptp: vmclock:
prevent read-only mappings from becoming writable").

Note this is defensive hardening: no in-tree platform driver currently
publishes a region without the WRITE flag.  The guard costs nothing and
keeps the mmap() interface honest if a read-only region ever appears.

Fixes: fad4d5b1f042 ("vfio/platform: support MMAP of MMIO regions")
Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
Reviewed-by: Mostafa Saleh <smostafa@google.com>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Pranjal Shrivastava <praan@google.com>
---
 drivers/vfio/platform/vfio_platform_common.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index c72db5a99ebda..ab531318f170a 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -591,9 +591,13 @@ int vfio_platform_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma
 			&& (vma->vm_flags & VM_READ))
 		return -EINVAL;
 
-	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
-			&& (vma->vm_flags & VM_WRITE))
-		return -EINVAL;
+	/* Prevent read-only region mappings from being upgraded with mprotect() */
+	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+		if (vma->vm_flags & VM_WRITE)
+			return -EINVAL;
+
+		vm_flags_clear(vma, VM_MAYWRITE);
+	}
 
 	vma->vm_private_data = vdev;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 2/7] vfio/fsl-mc: prevent read-only region mappings from becoming writable
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 1/7] vfio/platform: prevent read-only region mappings from becoming writable Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 3/7] vfio/cdx: " Abdifatah Suruur
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_fsl_mc_mmap() rejects writable mappings of regions without the
WRITE flag, but leaves VM_MAYWRITE set.  Userspace can map such a region
read-only and then upgrade the mapping to writable with mprotect().

Clear VM_MAYWRITE for regions without the WRITE flag, as i915 does for
its read-only objects and as fixed in drm/vc4 (CVE-2026-68445),
drm/panthor (CVE-2024-53071) and commit a5edadbae57e ("ptp: vmclock:
prevent read-only mappings from becoming writable").

Note this is defensive hardening: the fsl-mc bus publishes all device
regions with the WRITE flag set, so no device can currently reach the
read-only path.  The guard costs nothing and keeps the mmap() interface
honest if a read-only region ever appears.

Fixes: 67247289688d4 ("vfio/fsl-mc: Allow userspace to MMAP fsl-mc device MMIO regions")
Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
index b4c3958201b25..5a6cd78dcc06a 100644
--- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
+++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
@@ -403,9 +403,13 @@ static int vfio_fsl_mc_mmap(struct vfio_device *core_vdev,
 			&& (vma->vm_flags & VM_READ))
 		return -EINVAL;
 
-	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)
-			&& (vma->vm_flags & VM_WRITE))
-		return -EINVAL;
+	/* Prevent read-only region mappings from being upgraded with mprotect() */
+	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+		if (vma->vm_flags & VM_WRITE)
+			return -EINVAL;
+
+		vm_flags_clear(vma, VM_MAYWRITE);
+	}
 
 	vma->vm_private_data = mc_dev;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 3/7] vfio/cdx: prevent read-only region mappings from becoming writable
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 1/7] vfio/platform: prevent read-only region mappings from becoming writable Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 2/7] vfio/fsl-mc: " Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 4/7] vfio/platform: keep logical vm_pgoff in MMIO region mmap Abdifatah Suruur
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_cdx_mmap() rejects writable mappings of regions without the WRITE
flag, but leaves VM_MAYWRITE set.  Userspace can map such a region
read-only and then upgrade the mapping to writable with mprotect(),
writing to MMIO regions the device marks read-only.

Clear VM_MAYWRITE for regions without the WRITE flag, as i915 does for
its read-only objects and as fixed in drm/vc4 (CVE-2026-68445),
drm/panthor (CVE-2024-53071) and commit a5edadbae57e ("ptp: vmclock:
prevent read-only mappings from becoming writable").

Note this is defensive hardening: no in-tree cdx device currently
publishes a region without the WRITE flag.  The guard costs nothing and
keeps the mmap() interface honest if a read-only region ever appears.

Fixes: 234489ac56130 ("vfio/cdx: add support for CDX bus")
Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
---
 drivers/vfio/cdx/main.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/cdx/main.c b/drivers/vfio/cdx/main.c
index b31ed4be7bdc1..28386fdb01f2e 100644
--- a/drivers/vfio/cdx/main.c
+++ b/drivers/vfio/cdx/main.c
@@ -281,9 +281,13 @@ static int vfio_cdx_mmap(struct vfio_device *core_vdev,
 	    (vma->vm_flags & VM_READ))
 		return -EPERM;
 
-	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE) &&
-	    (vma->vm_flags & VM_WRITE))
-		return -EPERM;
+	/* Prevent read-only region mappings from being upgraded with mprotect() */
+	if (!(vdev->regions[index].flags & VFIO_REGION_INFO_FLAG_WRITE)) {
+		if (vma->vm_flags & VM_WRITE)
+			return -EPERM;
+
+		vm_flags_clear(vma, VM_MAYWRITE);
+	}
 
 	return vfio_cdx_mmap_mmio(vdev->regions[index], vma);
 }
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 4/7] vfio/platform: keep logical vm_pgoff in MMIO region mmap
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
                   ` (2 preceding siblings ...)
  2026-09-29  9:48 ` [PATCH v2 3/7] vfio/cdx: " Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 5/7] vfio/fsl-mc: " Abdifatah Suruur
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_platform_mmap_mmio() overwrites vma->vm_pgoff with the physical
frame number of the MMIO region and passes it to remap_pfn_range().
The VMA is inserted into the device file's mapping->i_mmap interval
tree keyed by vm_pgoff, which VFIO expects to be the logical file
offset: the core links every device mmap to the device inode's
i_mapping precisely so that unmap_mapping_range() can revoke all
mappings associated with a device (see vfio_device_cdev_open()).

With a raw PFN in vm_pgoff the interval tree entry lands in the wrong
coordinate space and unmap_mapping_range() cannot find the VMA,
leaving stale MMIO mappings behind any revocation attempt.  Keep
vm_pgoff in the logical VFIO offset space, as vfio-pci does, and pass
the physical PFN to remap_pfn_range() explicitly.

This is defensive hygiene: none of vfio-platform, vfio/fsl-mc and
vfio/cdx call unmap_mapping_range() today, so no reachable
stale-mapping issue exists.  Keeping vm_pgoff logical preserves the
VFIO core contract for any future revocation path.

Reviewed-by: Eric Auger <eric.auger@redhat.com>
Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
---
 drivers/vfio/platform/vfio_platform_common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/platform/vfio_platform_common.c b/drivers/vfio/platform/vfio_platform_common.c
index ab531318f170a..e8cfa76ad7f87 100644
--- a/drivers/vfio/platform/vfio_platform_common.c
+++ b/drivers/vfio/platform/vfio_platform_common.c
@@ -559,9 +559,8 @@ static int vfio_platform_mmap_mmio(struct vfio_platform_region region,
 		return -EINVAL;
 
 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
-	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
-
-	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+	return remap_pfn_range(vma, vma->vm_start,
+			       (region.addr >> PAGE_SHIFT) + pgoff,
 			       req_len, vma->vm_page_prot);
 }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 5/7] vfio/fsl-mc: keep logical vm_pgoff in MMIO region mmap
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
                   ` (3 preceding siblings ...)
  2026-09-29  9:48 ` [PATCH v2 4/7] vfio/platform: keep logical vm_pgoff in MMIO region mmap Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 6/7] vfio/cdx: " Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps Abdifatah Suruur
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_fsl_mc_mmap_mmio() overwrites vma->vm_pgoff with the physical
frame number of the MMIO region and passes it to remap_pfn_range().
The VMA is inserted into the device file's mapping->i_mmap interval
tree keyed by vm_pgoff, which VFIO expects to be the logical file
offset: the core links every device mmap to the device inode's
i_mapping precisely so that unmap_mapping_range() can revoke all
mappings associated with a device (see vfio_device_cdev_open()).

With a raw PFN in vm_pgoff the interval tree entry lands in the wrong
coordinate space and unmap_mapping_range() cannot find the VMA,
leaving stale MMIO mappings behind any revocation attempt.  Keep
vm_pgoff in the logical VFIO offset space, as vfio-pci does, and pass
the physical PFN to remap_pfn_range() explicitly.

This is defensive hygiene: none of vfio-platform, vfio/fsl-mc and
vfio/cdx call unmap_mapping_range() today, so no reachable
stale-mapping issue exists.  Keeping vm_pgoff logical preserves the
VFIO core contract for any future revocation path.

Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
index 5a6cd78dcc06a..15b05b8c0c908 100644
--- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c
+++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c
@@ -369,9 +369,8 @@ static int vfio_fsl_mc_mmap_mmio(struct vfio_fsl_mc_region region,
 	if (!region_cacheable)
 		vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
 
-	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
-
-	return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+	return remap_pfn_range(vma, vma->vm_start,
+			       (region.addr >> PAGE_SHIFT) + pgoff,
 			       size, vma->vm_page_prot);
 }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 6/7] vfio/cdx: keep logical vm_pgoff in MMIO region mmap
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
                   ` (4 preceding siblings ...)
  2026-09-29  9:48 ` [PATCH v2 5/7] vfio/fsl-mc: " Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29  9:48 ` [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps Abdifatah Suruur
  6 siblings, 0 replies; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_cdx_mmap_mmio() overwrites vma->vm_pgoff with the physical frame
number of the MMIO region and passes it to io_remap_pfn_range().  The
VMA is inserted into the device file's mapping->i_mmap interval tree
keyed by vm_pgoff, which VFIO expects to be the logical file offset:
the core links every device mmap to the device inode's i_mapping
precisely so that unmap_mapping_range() can revoke all mappings
associated with a device (see vfio_device_cdev_open()).

With a raw PFN in vm_pgoff the interval tree entry lands in the wrong
coordinate space and unmap_mapping_range() cannot find the VMA,
leaving stale MMIO mappings behind any revocation attempt.  Keep
vm_pgoff in the logical VFIO offset space, as vfio-pci does, and pass
the physical PFN to io_remap_pfn_range() explicitly.

This is defensive hygiene: none of vfio-platform, vfio/fsl-mc and
vfio/cdx call unmap_mapping_range() today, so no reachable
stale-mapping issue exists.  Keeping vm_pgoff logical preserves the
VFIO core contract for any future revocation path.

Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
---
 drivers/vfio/cdx/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/vfio/cdx/main.c b/drivers/vfio/cdx/main.c
index 28386fdb01f2e..ea55b7767672c 100644
--- a/drivers/vfio/cdx/main.c
+++ b/drivers/vfio/cdx/main.c
@@ -254,10 +254,10 @@ static int vfio_cdx_mmap_mmio(struct vfio_cdx_region region,
 	if (base + size > region.size)
 		return -EINVAL;
 
-	vma->vm_pgoff = (region.addr >> PAGE_SHIFT) + pgoff;
 	vma->vm_page_prot = pgprot_device(vma->vm_page_prot);
 
-	return io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
+	return io_remap_pfn_range(vma, vma->vm_start,
+				  (region.addr >> PAGE_SHIFT) + pgoff,
 				  size, vma->vm_page_prot);
 }
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps
  2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
                   ` (5 preceding siblings ...)
  2026-09-29  9:48 ` [PATCH v2 6/7] vfio/cdx: " Abdifatah Suruur
@ 2026-09-29  9:48 ` Abdifatah Suruur
  2026-09-29 20:36   ` Alex Williamson
  6 siblings, 1 reply; 9+ messages in thread
From: Abdifatah Suruur @ 2026-09-29  9:48 UTC (permalink / raw)
  To: kvm, linux-kernel
  Cc: alex, eric.auger, smostafa, praan, ioana.ciornei, nipun.gupta,
	nikhil.agarwal

vfio_cdx_mmap() accepts MAP_PRIVATE mappings.  A non-shared mapping
with VM_MAYWRITE is a COW mapping, and remap_pfn_range()'s COW special
case in get_remap_pgoff() then overwrites vma->vm_pgoff with the
physical frame number:

	if (is_cow) {
		if (addr != vm_start || end != vm_end)
			return -EINVAL;
		*vm_pgoff_p = pfn;
	}

The VMA is inserted into the device file's mapping->i_mmap interval
tree keyed by vm_pgoff, which VFIO expects to be the logical file
offset: the core links every device mmap to the device inode's
i_mapping precisely so that unmap_mapping_range() can revoke all
mappings associated with a device (see vfio_device_cdev_open()).

A raw PFN in vm_pgoff lands the interval tree entry in the wrong
coordinate space, so unmap_mapping_range() cannot find the VMA and a
stale MMIO mapping survives any revocation attempt.  Without this
check, a MAP_PRIVATE mapping reintroduces that corruption even with
the logical-vm_pgoff fix in place.

Require VM_SHARED as vfio-pci, vfio/fsl-mc and vfio/platform already
do.  This is defensive hygiene: vfio-cdx does not call
unmap_mapping_range() today, so no reachable stale-mapping issue
exists.

Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
---
 drivers/vfio/cdx/main.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/vfio/cdx/main.c b/drivers/vfio/cdx/main.c
index ea55b7767672c..28dc8c8256006 100644
--- a/drivers/vfio/cdx/main.c
+++ b/drivers/vfio/cdx/main.c
@@ -271,6 +271,9 @@ static int vfio_cdx_mmap(struct vfio_device *core_vdev,
 
 	index = vma->vm_pgoff >> (VFIO_CDX_OFFSET_SHIFT - PAGE_SHIFT);
 
+	if (!(vma->vm_flags & VM_SHARED))
+		return -EINVAL;
+
 	if (index >= cdx_dev->res_count)
 		return -EINVAL;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps
  2026-09-29  9:48 ` [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps Abdifatah Suruur
@ 2026-09-29 20:36   ` Alex Williamson
  0 siblings, 0 replies; 9+ messages in thread
From: Alex Williamson @ 2026-09-29 20:36 UTC (permalink / raw)
  To: Abdifatah Suruur
  Cc: kvm, linux-kernel, eric.auger, smostafa, praan, ioana.ciornei,
	nipun.gupta, nikhil.agarwal, alex

On Tue, 29 Sep 2026 12:48:48 +0300
Abdifatah Suruur <suruurism@gmail.com> wrote:

> vfio_cdx_mmap() accepts MAP_PRIVATE mappings.  A non-shared mapping
> with VM_MAYWRITE is a COW mapping, and remap_pfn_range()'s COW special
> case in get_remap_pgoff() then overwrites vma->vm_pgoff with the
> physical frame number:
> 
> 	if (is_cow) {
> 		if (addr != vm_start || end != vm_end)
> 			return -EINVAL;
> 		*vm_pgoff_p = pfn;
> 	}
> 
> The VMA is inserted into the device file's mapping->i_mmap interval
> tree keyed by vm_pgoff, which VFIO expects to be the logical file
> offset: the core links every device mmap to the device inode's
> i_mapping precisely so that unmap_mapping_range() can revoke all
> mappings associated with a device (see vfio_device_cdev_open()).

It's actually vfio_device_open_file() and vfio_device_fops_cdev_open(),
vfio_device_cdev_open() doesn't exist.
 
> A raw PFN in vm_pgoff lands the interval tree entry in the wrong
> coordinate space, so unmap_mapping_range() cannot find the VMA and a
> stale MMIO mapping survives any revocation attempt.  Without this
> check, a MAP_PRIVATE mapping reintroduces that corruption even with
> the logical-vm_pgoff fix in place.
> 
> Require VM_SHARED as vfio-pci, vfio/fsl-mc and vfio/platform already

vfio-fsl-mc and vfio-platform for consistency.

Applied series with these changes to vfio next branch for v7.4.  Thanks,

Alex

> do.  This is defensive hygiene: vfio-cdx does not call
> unmap_mapping_range() today, so no reachable stale-mapping issue
> exists.
> 
> Signed-off-by: Abdifatah Suruur <suruurism@gmail.com>
> ---
>  drivers/vfio/cdx/main.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/vfio/cdx/main.c b/drivers/vfio/cdx/main.c
> index ea55b7767672c..28dc8c8256006 100644
> --- a/drivers/vfio/cdx/main.c
> +++ b/drivers/vfio/cdx/main.c
> @@ -271,6 +271,9 @@ static int vfio_cdx_mmap(struct vfio_device *core_vdev,
>  
>  	index = vma->vm_pgoff >> (VFIO_CDX_OFFSET_SHIFT - PAGE_SHIFT);
>  
> +	if (!(vma->vm_flags & VM_SHARED))
> +		return -EINVAL;
> +
>  	if (index >= cdx_dev->res_count)
>  		return -EINVAL;
>  


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2026-09-29 20:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-29  9:48 [PATCH v2 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 1/7] vfio/platform: prevent read-only region mappings from becoming writable Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 2/7] vfio/fsl-mc: " Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 3/7] vfio/cdx: " Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 4/7] vfio/platform: keep logical vm_pgoff in MMIO region mmap Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 5/7] vfio/fsl-mc: " Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 6/7] vfio/cdx: " Abdifatah Suruur
2026-09-29  9:48 ` [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps Abdifatah Suruur
2026-09-29 20:36   ` Alex Williamson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®