Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: sashiko-reviews@lists.linux.dev
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH v2 03/10] KVM: x86: Unmap VMAs for KVM-internal memslots when the memslot is freed
Date: Fri, 2 Oct 2026 06:36:25 -0700	[thread overview]
Message-ID: <ar-y8vCZt84a_xm7@google.com> (raw)
In-Reply-To: <20261002091342.50ADA1F000FF@smtp.kernel.org>

On Fri, Oct 02, 2026, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] kvm: double unmap of internal memslot VMA causes host UAF
> --
> 
> commit 120ca6d762cdf95d09e394aff4bb4af6d01288d6
> Author: Sean Christopherson <seanjc@google.com>
> KVM: x86: Unmap VMAs for KVM-internal memslots when the memslot is freed
> 
> This commit unmaps KVM-created VMAs for KVM-internal memslots when the
> associated memslot is freed directly, instead of using x86's internal API,
> to prevent VMA leaks on teardown.
> 
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 79468ddfe4736..e91bd79dd3cc6 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> 
> [ ... ]
> 
> > @@ -10066,6 +10048,16 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
> >  	}
> >  
> >  	kvm_page_track_free_memslot(slot);
> > +
> > +	/*
> > +	 * Free memory regions allocated on behalf of userspace, unless the
> > +	 * memory map has changed due to process exit or fd copying.  Leak the
> > +	 * mapping on failure, e.g. if the task is killed, worst case scenario,
> > +	 * the page(s) will be reclaimed when the process exits.
> > +	 */
> > +	if (current->mm == kvm->mm && slot->id >= KVM_USER_MEM_SLOTS &&
> > +	    !WARN_ON_ONCE(!slot->npages))
> > +		vm_munmap(slot->userspace_addr, slot->npages * PAGE_SIZE);
> >  }
> 
> [Severity: High]
> Does this unconditionally unmap the VMA for every freed internal memslot object,
> potentially leading to a double unmap?
> 
> In __x86_set_memory_region(), the twin memslot objects are created with the

Ugh, SMM, which is the "twin" aspect Sashiko is referring to.

> exact same userspace_addr for each address space:
> 
> 	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
> 		struct kvm_userspace_memory_region2 m;
> 
> 		m.slot = id | (i << 16);
> 		m.flags = 0;
> 		m.guest_phys_addr = gpa;
> 		m.userspace_addr = hva;
> ...
> 
> When the VM is destroyed or the memslot is deleted, KVM iterates over all
> address spaces and calls kvm_arch_free_memslot() for each memslot object.
> 
> Because vm_munmap() is called here without checking if slot->as_id is 0, could
> it unmap the same HVA range multiple times?

Heh, I arrived at the same conclusion before reading this.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e91bd79dd3cc..e55aa26e26cc 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10050,13 +10050,15 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
 	kvm_page_track_free_memslot(slot);
 
 	/*
-	 * Free memory regions allocated on behalf of userspace, unless the
-	 * memory map has changed due to process exit or fd copying.  Leak the
-	 * mapping on failure, e.g. if the task is killed, worst case scenario,
-	 * the page(s) will be reclaimed when the process exits.
+	 * Unmap VMAs allocated on behalf of userspace for internal memslots,
+	 * unless the memory map has changed due to process exit or fd copying.
+	 * Do the unmap only for the non-SMM slot, as the same VMA is used for
+	 * both non-SMM and SMM memslots.  Leak the mapping on failure, e.g.
+	 * if the task is killed.  Worst case scenario, the page(s) will be
+	 * reclaimed when the process exits.
 	 */
 	if (current->mm == kvm->mm && slot->id >= KVM_USER_MEM_SLOTS &&
-	    !WARN_ON_ONCE(!slot->npages))
+	    !slot->as_id && !WARN_ON_ONCE(!slot->npages))
 		vm_munmap(slot->userspace_addr, slot->npages * PAGE_SIZE);
 }

  reply	other threads:[~2026-10-02 13:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 20:22 [PATCH v2 00/10] KVM: Fix+harden against bad uaccess using dying VM Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 01/10] KVM: Reject user accesses to guest memory if current->mm != kvm->mm Sean Christopherson
2026-10-01 21:08   ` James Houghton
2026-10-01 21:18     ` Sean Christopherson
2026-10-01 21:28       ` James Houghton
2026-10-01 20:22 ` [PATCH v2 02/10] KVM: PPC: Flush/zap all memslots on kvm_arch_flush_shadow_all() Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 03/10] KVM: x86: Unmap VMAs for KVM-internal memslots when the memslot is freed Sean Christopherson
2026-10-01 21:26   ` James Houghton
2026-10-02  9:13   ` sashiko-bot
2026-10-02 13:36     ` Sean Christopherson [this message]
2026-10-01 20:22 ` [PATCH v2 04/10] KVM: Disallow setting memslots when the VM is being destroyed Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 05/10] KVM: Destroy memslots immediately after mmu_notifiers are unregistered Sean Christopherson
2026-10-02  9:13   ` sashiko-bot
2026-10-02 14:07     ` Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 06/10] KVM: WARN if KVM attempts to do guest-related uaccess with "wrong" process Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 07/10] KVM: WARN and reject guest-based uaccess if VM is dying Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 08/10] KVM: nVMX: Don't flush shadow VMCS12 to guest memory during vCPU teardown Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 09/10] KVM: nVMX: Don't try to load eVMCS12 page when the VM is dying Sean Christopherson
2026-10-01 20:22 ` [PATCH v2 10/10] KVM: Pre-check uaccesses in KVM's APIs to read/write guest memory Sean Christopherson
2026-10-02  9:13   ` sashiko-bot
2026-10-02 13:54     ` Sean Christopherson
2026-10-02 20:30 ` [syzbot ci] Re: KVM: Fix+harden against bad uaccess using dying VM syzbot ci
2026-10-02 20:39   ` Sean Christopherson

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=ar-y8vCZt84a_xm7@google.com \
    --to=seanjc@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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