Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Suzuki K Poulose <suzuki.poulose@arm.com>
To: sashiko-reviews@lists.linux.dev
Cc: Marc Zyngier <maz@kernel.org>, Oliver Upton <oupton@kernel.org>,
	kvmarm@lists.linux.dev, kvm@vger.kernel.org,
	Fuad Tabba <tabba@google.com>
Subject: Re: [PATCH v23 09/14] KVM: arm64: Consolidate stage2 unmap range into kvm_stage2_unmap_range
Date: Wed, 7 Oct 2026 10:52:48 +0200	[thread overview]
Message-ID: <6e7f8a1c-73e4-478e-9979-3c3cb01c2306@arm.com> (raw)
In-Reply-To: <sashiko-outbox-162730@kernel.org>

On 07/10/2026 08:49, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
> - [High] kvm: arm64: UAF in stage2_apply_range() when nested_mmus is reallocated
> --
> 
> commit f49216e04631aee4e96534b9604d42f899f085d6
> Author: Suzuki K Poulose <suzuki.poulose@arm.com>
> 
> KVM: arm64: Consolidate stage2 unmap range into kvm_stage2_unmap_range
> 
> This patch simplifies the stage-2 unmap operations by removing
> __unmap_stage2_range() and folding its logic directly into
> kvm_stage2_unmap_range(). It also updates callers to use the consolidated
> function.
> 
>> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
>> index 9ba86450fe4af..3fef16393f568 100644
>> --- a/arch/arm64/kvm/mmu.c
>> +++ b/arch/arm64/kvm/mmu.c
>> @@ -314,36 +314,30 @@ static void invalidate_icache_guest_page(void *va, size_t size)
> [ ... ]
>> -static void __unmap_stage2_range(struct kvm_s2_mmu *mmu, phys_addr_t start, u64 size,
>> -				 bool may_block)
>> +void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
>> +			    u64 size, bool may_block)
>>   {
>>   	struct kvm *kvm = kvm_s2_mmu_to_kvm(mmu);
>> -	phys_addr_t end = start + size;
>> +
>> +	if (kvm_vm_is_protected(kvm))
>> +		return;
>>   
>>   	lockdep_assert_held_write(&kvm->mmu_lock);
>>   	WARN_ON(size & ~PAGE_MASK);
>> -	WARN_ON(stage2_apply_range(mmu, start, end, KVM_PGT_FN(kvm_pgtable_stage2_unmap),
>> -				   may_block));
>> -}
>>   
>> -void kvm_stage2_unmap_range(struct kvm_s2_mmu *mmu, phys_addr_t start,
>> -			    u64 size, bool may_block)
>> -{
>> -	if (kvm_vm_is_protected(kvm_s2_mmu_to_kvm(mmu)))
>> -		return;
>> -
>> -	__unmap_stage2_range(mmu, start, size, may_block);
>> +	WARN_ON(stage2_apply_range(mmu, start, start + size,
>> +				   KVM_PGT_FN(kvm_pgtable_stage2_unmap),
>> +				   may_block));
> 
> [Severity: High]
> Does this allow a use-after-free of the kvm->arch.nested_mmus array if
> may_block is true?
> 
> If kvm_nested_s2_unmap() is called during a memory reclaim event via
> kvm_mmu_notifier_invalidate_range_start(), it caches a pointer to an
> element of kvm->arch.nested_mmus (e.g., mmu = &kvm->arch.nested_mmus[i])
> and passes it here with may_block=true.
> 
> When stage2_apply_range() iterates, it can drop kvm->mmu_lock via
> cond_resched_rwlock_write():
> 
> stage2_apply_range() {
> 	do {
> 		struct kvm_pgtable *pgt = mmu->pgt;
> 		...
> 		if (resched && next != end)
> 			cond_resched_rwlock_write(&kvm->mmu_lock);
> 	} while (addr = next, addr != end);
> }
> 
> Concurrently, could userspace initializing a new vCPU via KVM_ARM_VCPU_INIT
> invoke kvm_vcpu_init_nested(), grab kvm->mmu_lock, reallocate the
> kvm->arch.nested_mmus array, and free the old one?
> 
> kvm_vcpu_init_nested() {
> 	...
> 	swap(kvm->arch.nested_mmus, tmp);
> 	write_unlock(&kvm->mmu_lock);
> 	kvfree(tmp);
> }
> 
> If stage2_apply_range() then reacquires the lock, it would dereference
> mmu->pgt using the mmu pointer that still points into the now-freed
> nested_mmus array.
> 
> Can we ensure the array is not freed while we iterate, or refresh the mmu
> pointer after reacquiring the lock?

This is a pre-existing issue and has been fixed in v7.3-rc5.

commit 33346f8960c7 ("KVM: arm64: nv: Fix life cycle of the nested_mmus 
array")

Suzuki

> 
>>   }
> 


  reply	other threads:[~2026-10-07  8:52 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-07  7:35 [PATCH v23 00/14] KVM: arm64: CCA: Add basic plumbing for Realms Suzuki K Poulose
2026-10-07  7:35 ` [PATCH v23 01/14] KVM: arm64: protected VM: Handle user writes to CNTVCT_EL0/CNTPCT_EL0 Suzuki K Poulose
2026-10-07 10:43   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 02/14] KVM: arm64: Disable Steal time accounting for protected guests Suzuki K Poulose
2026-10-07 10:51   ` Fuad Tabba
2026-10-07 13:10     ` Suzuki K Poulose
2026-10-07  7:35 ` [PATCH v23 03/14] KVM: arm64: Include kvm_emulate.h in kvm/arm_psci.h Suzuki K Poulose
2026-10-07  8:03   ` sashiko-bot
2026-10-07 11:00   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 04/14] KVM: arm64: Avoid including linux/kvm_host.h in kvm_pgtable.h Suzuki K Poulose
2026-10-07  7:35 ` [PATCH v23 05/14] KVM: arm64: Track the type of VM in kvm_arch Suzuki K Poulose
2026-10-07  7:35 ` [PATCH v23 06/14] KVM: arm64: Don't call vcpu_set_pauth_traps for pKVM host Suzuki K Poulose
2026-10-07 11:07   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 07/14] KVM: arm64: Refactor the vcpu_load to allow for VM specific callbacks Suzuki K Poulose
2026-10-07 11:12   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 08/14] KVM: arm64: Add vcpu load/put call backs for flavors Suzuki K Poulose
2026-10-07 11:18   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 09/14] KVM: arm64: Consolidate stage2 unmap range into kvm_stage2_unmap_range Suzuki K Poulose
2026-10-07  7:49   ` sashiko-bot
2026-10-07  8:52     ` Suzuki K Poulose [this message]
2026-10-07 11:23   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 10/14] KVM: arm64: Add VM specific callback for S2 MMU operations Suzuki K Poulose
2026-10-07 11:38   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 11/14] KVM: arm64: Use a local kvm pointer in kvm_handle_guest_abort() Suzuki K Poulose
2026-10-07  7:35 ` [PATCH v23 12/14] KVM: arm64: Abstract out memory abort handling Suzuki K Poulose
2026-10-07 13:45   ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 13/14] KVM: arm64: Mandate VGIC v3 for pKVM VMs and Realms Suzuki K Poulose
2026-10-07 13:45   ` Fuad Tabba
2026-10-08  8:51     ` Suzuki K Poulose
2026-10-08 11:17       ` Fuad Tabba
2026-10-07  7:35 ` [PATCH v23 14/14] KVM: arm64: Prevent unsupported vcpu features for VM types Suzuki K Poulose
2026-10-07 14:19   ` Fuad Tabba

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=6e7f8a1c-73e4-478e-9979-3c3cb01c2306@arm.com \
    --to=suzuki.poulose@arm.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@lists.linux.dev \
    --cc=maz@kernel.org \
    --cc=oupton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tabba@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