mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Rik van Riel <riel@surriel.com>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: Sean Christopherson <seanjc@google.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	kernel-team@meta.com
Subject: [PATCH] KVM: reject VMA straddling the dirty ring on mmap
Date: Fri, 30 Oct 2026 18:36:04 -0400	[thread overview]
Message-ID: <20261030183604.3e8e373c@fangorn> (raw)

kvm_vcpu_mmap only tests the VMA's first and last page against the
dirty-ring interval, in order to prevent a MAP_PRIVATE VMA from
getting mapped into the dirty ring, breaking dirty tracking. 

A VMA that starts before the ring and ends after it has neither 
endpoint inside, so the check passes even though the
mapping covers the ring.

Getting this wrong is observable from host userspace: the VMM
(QEMU) mmaps the vCPU fd to reach the dirty ring, and a MAP_PRIVATE
mapping that spans the ring succeeds even though it covers the ring.
Faulting one instantiates a private CoW copy instead of the shared 
ring page. The guest's dirty-ring writes then land in a page the 
VMM never sees, silently breaking dirty-page tracking.

Fix by testing interval overlap instead of endpoint membership.

Tested by creating a VM with a 16-page dirty ring. Mmap the
VCPU fd covering pgoff 0...199 MAP_PRIVATE, then fault a
page inside the ring. Succeeds before this patch, -EINVAL after.

Signed-off-by: Rik van Riel <riel@surriel.com>
---
 virt/kvm/kvm_main.c | 10 +++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4175,9 +4175,15 @@ static const struct vm_operations_struct kvm_vcpu_vm_ops = {
 static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
 {
 	struct kvm_vcpu *vcpu = file->private_data;
+	struct kvm *kvm = vcpu->kvm;
 	unsigned long pages = vma_pages(vma);
+	unsigned long ring_start = KVM_DIRTY_LOG_PAGE_OFFSET;
+	unsigned long ring_end = ring_start + kvm->dirty_ring_size / PAGE_SIZE;
 
-	if ((kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff) ||
-	     kvm_page_in_dirty_ring(vcpu->kvm, vma->vm_pgoff + pages - 1)) &&
+	/*
+	 * An overlapping VMA is rejected when private or executable: a CoW
+	 * fault hides guest writes from the VMM.
+	 */
+	if (vma->vm_pgoff < ring_end && vma->vm_pgoff + pages > ring_start &&
 	    ((vma->vm_flags & VM_EXEC) || !(vma->vm_flags & VM_SHARED)))
 		return -EINVAL;
 


                 reply	other threads:[~2026-10-30 22:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261030183604.3e8e373c@fangorn \
    --to=riel@surriel.com \
    --cc=kernel-team@meta.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@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®