mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Abdifatah Suruur <suruurism@gmail.com>
To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: alex@shazbot.org, eric.auger@redhat.com, smostafa@google.com,
	praan@google.com, ioana.ciornei@nxp.com, nipun.gupta@amd.com,
	nikhil.agarwal@amd.com
Subject: [PATCH 7/7] vfio/cdx: reject non-shared MMIO mmaps
Date: Mon, 21 Sep 2026 15:23:34 +0300	[thread overview]
Message-ID: <20260921122334.2099-8-suruurism@gmail.com> (raw)
In-Reply-To: <20260921122334.2099-1-suruurism@gmail.com>

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>
---
diff --git a/drivers/vfio/cdx/main.c b/drivers/vfio/cdx/main.c
index b31ed4be7bdc1..6d208db0896d6 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;


      parent reply	other threads:[~2026-09-21 12:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 12:23 [PATCH 0/7] vfio: mmap()/mprotect() hygiene for MMIO region mappings Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 1/7] vfio/platform: prevent read-only region mappings from becoming writable Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 2/7] vfio/fsl-mc: " Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 3/7] vfio/cdx: " Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 4/7] vfio/platform: keep logical vm_pgoff in MMIO region mmap Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 5/7] vfio/fsl-mc: " Abdifatah Suruur
2026-09-21 12:23 ` [PATCH 6/7] vfio/cdx: " Abdifatah Suruur
2026-09-21 12:23 ` Abdifatah Suruur [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260921122334.2099-8-suruurism@gmail.com \
    --to=suruurism@gmail.com \
    --cc=alex@shazbot.org \
    --cc=eric.auger@redhat.com \
    --cc=ioana.ciornei@nxp.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nikhil.agarwal@amd.com \
    --cc=nipun.gupta@amd.com \
    --cc=praan@google.com \
    --cc=smostafa@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®