Skip to contentSkip to conversation

vfio: mmap()/mprotect() hygiene for MMIO region mappings

PATCH Applied Abdifatah Suruur 2 participants 11 messages Sep 29 – Oct 30

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v2 cover vfio: mmap()/mprotect() hygiene for MMIO region mappings

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(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v2 1/7 vfio/platform: prevent read-only region mappings from becoming writable

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")
---
 drivers/vfio/platform/vfio_platform_common.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/vfio/platform/vfio_platform_common.c +7 -3
index c72db5a99ebda..ab531318f170a 100644
@@ -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;
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/7 vfio/fsl-mc: prevent read-only region mappings from becoming writable

In reply to Abdifatah Suruur

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")
---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/vfio/fsl-mc/vfio_fsl_mc.c +7 -3
index b4c3958201b25..5a6cd78dcc06a 100644
@@ -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;
 
Signature · 1 line
2.53.0
#3 lore raw

PATCH v2 3/7 vfio/cdx: prevent read-only region mappings from becoming writable

In reply to Abdifatah Suruur

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")
---
 drivers/vfio/cdx/main.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/vfio/cdx/main.c +7 -3
index b31ed4be7bdc1..28386fdb01f2e 100644
@@ -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);
 }
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 4/7 vfio/platform: keep logical vm_pgoff in MMIO region mmap

In reply to Abdifatah Suruur

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.

---
 drivers/vfio/platform/vfio_platform_common.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/vfio/platform/vfio_platform_common.c +2 -3
index ab531318f170a..e8cfa76ad7f87 100644
@@ -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);
 }
 
Signature · 1 line
2.53.0
#5 lore raw

PATCH v2 5/7 vfio/fsl-mc: keep logical vm_pgoff in MMIO region mmap

In reply to Abdifatah Suruur

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.

---
 drivers/vfio/fsl-mc/vfio_fsl_mc.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/vfio/fsl-mc/vfio_fsl_mc.c +2 -3
index 5a6cd78dcc06a..15b05b8c0c908 100644
@@ -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);
 }
 
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 6/7 vfio/cdx: keep logical vm_pgoff in MMIO region mmap

In reply to Abdifatah Suruur

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.

---
 drivers/vfio/cdx/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/vfio/cdx/main.c +2 -2
index 28386fdb01f2e..ea55b7767672c 100644
@@ -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);
 }
 
Signature · 1 line
2.53.0
#7 lore raw

PATCH v2 7/7 vfio/cdx: reject non-shared MMIO mmaps

In reply to Abdifatah Suruur

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.

---
 drivers/vfio/cdx/main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/vfio/cdx/main.c +3 -0
index ea55b7767672c..28dc8c8256006 100644
@@ -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;
 
Signature · 1 line
2.53.0
maintainer Applied
#8 lore raw

Re: [PATCH v2 7/7] vfio/cdx: reject non-shared MMIO mmaps

Quoted 11 lines · On Tue, 29 Sep 2026 12:48:48 +0300 Abdifatah Suruur <[email protected]> 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;
	}
Quoted 5 lines

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.

Quoted 7 lines

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

Quoted 23 lines

do. This is defensive hygiene: vfio-cdx does not call unmap_mapping_range() today, so no reachable stale-mapping issue exists.

---
 drivers/vfio/cdx/main.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/vfio/cdx/main.c +3 -0
index ea55b7767672c..28dc8c8256006 100644
@@ -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;
 
#9 lore raw

Hi Alex,

Thanks again for applying the vfio mmap()/mprotect() series for 7.4. I have one process question about stable backports.

Patches 1-3 (vfio/platform, vfio/fsl-mc, vfio/cdx: prevent read-only region mappings from becoming writable) still carry their Fixes tags. They are the same VM_MAYWRITE/mprotect class as CVE-2026-68445 (drm/vc4) and CVE-2026-80724 (ptp/vmclock), and I am tracking the class as a security fix. Would you like those three to carry
Cc: [email protected] so they can be backported once they reach mainline, or do you prefer to keep the whole series 7.4-only?

I understand patches 4-7 were deliberately framed as hygiene (Fixes tags dropped) and I am not asking for those to be marked stable. I am just clarifying the intent for 1-3 so I follow the right process.

If you prefer 7.4-only, no change to the applied patches is needed.

Thanks,
Abdifatah Suruur
[email protected]

maintainer
#10 lore raw
Quoted 13 lines · On Tue, 27 Oct 2026 04:22:04 -0700 (PDT) Abdifatah Suruur…

Hi Alex,

Thanks again for applying the vfio mmap()/mprotect() series for 7.4. I have one process question about stable backports.

Patches 1-3 (vfio/platform, vfio/fsl-mc, vfio/cdx: prevent read-only region mappings from becoming writable) still carry their Fixes tags. They are the same VM_MAYWRITE/mprotect class as CVE-2026-68445 (drm/vc4) and CVE-2026-80724 (ptp/vmclock), and I am tracking the class as a security fix. Would you like those three to carry
Cc: [email protected] so they can be backported once they reach mainline, or do you prefer to keep the whole series 7.4-only?

Quoted 5 lines

I understand patches 4-7 were deliberately framed as hygiene (Fixes tags dropped) and I am not asking for those to be marked stable. I am just clarifying the intent for 1-3 so I follow the right process.

If you prefer 7.4-only, no change to the applied patches is needed.

The commits are immutable now. They can be proposed to stable, but they cannot be amended to include a tag for stable. Personally, I think the backport argument for any of these is weak, thus the framing as hygiene and hardening. IIRC, none of them have any exploit path and largely guard against future work exposing these vulnerabilities. That theoretical future work would largely be feature rather than fix oriented, so the demand for stable seems low to me. Thanks,

Alex