All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Tim Wiederhake" <twiederh@redhat.com>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH 4/4] KVM: x86: Reject user memory regions covering the APIC base
Date: Mon, 06 Jul 2026 10:00:33 +0000	[thread overview]
Message-ID: <20260706100034.86A8B1F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260706092021.3625908-6-twiederh@redhat.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] The newly added APIC base memslot check unconditionally rejects KVM's internal APIC access page, breaking VMX vCPU creation and triggering a guaranteed VMA leak.

Pre-existing issues:
- [High] Preexisting Use-After-Free in KVM_CREATE_IRQCHIP due to lockless read of the memslots array.
--

commit 75a39a0e2b5c8384d4f938fb4214b675289701b6
Author: Tim Wiederhake <twiederh@redhat.com>

KVM: x86: Reject user memory regions covering the APIC base

This commit adds an explicit check in kvm_arch_prepare_memory_region()
to reject user space attempts to map guest memory covering the default
APIC base address when an in-kernel irqchip is in use.

> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index cb7f759a7b21e..f7f2bd4d8b334 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c

[ ... ]

> @@ -10129,12 +10129,19 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,
>  		return -EINVAL;
>  
>  	if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) {
> +		const gfn_t apic_gfn = gpa_to_gfn(APIC_DEFAULT_PHYS_BASE);
> +
>  		if ((new->base_gfn + new->npages - 1) > kvm_mmu_max_gfn())
>  			return -EINVAL;
>  
>  		if (kvm_is_gfn_alias(kvm, new->base_gfn + new->npages - 1))
>  			return -EINVAL;
>  
> +		if (irqchip_in_kernel(kvm) &&
> +		    new->base_gfn <= apic_gfn &&
> +		    apic_gfn < new->base_gfn + new->npages)
> +			return -EEXIST;

[Severity: High]
Could this check inadvertently catch KVM's own internal memslot allocations,
such as APIC_ACCESS_PAGE_PRIVATE_MEMSLOT?

When kvm_alloc_apic_access_page() attempts to allocate the internal memslot
exactly at APIC_DEFAULT_PHYS_BASE, it appears this new check will intercept
it and return -EEXIST, which would break vCPU creation.

Furthermore, if this returns -EEXIST during an internal memslot allocation,
could it cause a VMA memory leak?

Looking at __x86_set_memory_region() in arch/x86/kvm/x86.c:

	hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
		      MAP_SHARED | MAP_ANONYMOUS, 0);
	...
	for (i = 0; i < kvm_arch_nr_memslot_as_ids(kvm); i++) {
		...
		r = kvm_set_internal_memslot(kvm, &m);
		if (r < 0)
			return ERR_PTR_USR(r);
	}

If kvm_set_internal_memslot() fails here because the new check returns
-EEXIST, the error path returns immediately without calling vm_munmap()
to clean up the VMA allocated earlier.

> +
>  		return kvm_alloc_memslot_metadata(kvm, new);
>  	}

[Severity: High]
This isn't a bug introduced by this patch, but while looking at the APIC
base and irqchip code, I noticed a potential use-after-free when
KVM_CREATE_IRQCHIP accesses the memslots array.

Looking at kvm_arch_vm_ioctl() in arch/x86/kvm/x86.c:

	case KVM_CREATE_IRQCHIP: {
		mutex_lock(&kvm->lock);
		...
		r = -EEXIST;
		if (gfn_to_memslot(kvm, gpa_to_gfn(APIC_DEFAULT_PHYS_BASE)))
			goto create_irqchip_unlock;

Is it safe to call gfn_to_memslot() here outside of any SRCU read-side
critical section and without holding kvm->slots_lock?

A concurrent thread executing KVM_SET_USER_MEMORY_REGION (which holds
kvm->slots_lock but not kvm->lock) could install a new memslots array, issue
synchronize_srcu(), and free the old memslots array while KVM_CREATE_IRQCHIP
is still traversing it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260706092021.3625908-2-twiederh@redhat.com?part=4

  reply	other threads:[~2026-07-06 10:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-06  9:20 [PATCH 0/4] KVM: x86: Document and enforce APIC base memory hole Tim Wiederhake
2026-07-06  9:20 ` [PATCH 1/4] KVM: x86: Document that KVM_CREATE_IRQCHIP must precede vcpu creation Tim Wiederhake
2026-07-06 23:22   ` Sean Christopherson
2026-07-06  9:20 ` [PATCH 2/4] KVM: x86: Document APIC base address constraint for in-kernel irqchip Tim Wiederhake
2026-07-06 23:32   ` Sean Christopherson
2026-07-06  9:20 ` [PATCH 3/4] KVM: x86: Reject KVM_CREATE_IRQCHIP if APIC base is already mapped Tim Wiederhake
2026-07-06  9:40   ` sashiko-bot
2026-07-06 22:26     ` Sean Christopherson
2026-07-06  9:20 ` [PATCH 4/4] KVM: x86: Reject user memory regions covering the APIC base Tim Wiederhake
2026-07-06 10:00   ` sashiko-bot [this message]
2026-07-06 15:21 ` [syzbot ci] Re: KVM: x86: Document and enforce APIC base memory hole syzbot ci
2026-07-06 22:26 ` [PATCH 0/4] " 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=20260706100034.86A8B1F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=twiederh@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.