All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: kvm@vger.kernel.org, Thijs Raymakers <thijs@raymakers.nl>,
	stable <stable@kernel.org>,  Paolo Bonzini <pbonzini@redhat.com>
Subject: Re: [PATCH] KVM: x86: use array_index_nospec with indices that come from guest
Date: Thu, 24 Jul 2025 06:38:34 -0700	[thread overview]
Message-ID: <aII3WuhvJb3sY8HG@google.com> (raw)
In-Reply-To: <2025072400-amendment-thieving-675f@gregkh>

On Thu, Jul 24, 2025, Greg Kroah-Hartman wrote:
> From: Thijs Raymakers <thijs@raymakers.nl>
> 
> min and dest_id are guest-controlled indices. Using array_index_nospec()
> after the bounds checks clamps these values to mitigate speculative
> execution side-channels.
> 
> Signed-off-by: Thijs Raymakers <thijs@raymakers.nl>
> Cc: stable <stable@kernel.org>
> Cc: Sean Christopherson <seanjc@google.com>
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
>  arch/x86/kvm/lapic.c | 2 ++
>  arch/x86/kvm/x86.c   | 7 +++++--
>  2 files changed, 7 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 73418dc0ebb2..e10d6ad236c9 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -852,6 +852,8 @@ static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map,
>  	if (min > map->max_apic_id)
>  		return 0;
>  
> +	min = array_index_nospec(min, map->max_apic_id);

This is wrong, max_apic_id is inclusive, whereas array_index_nospec() takes a
size/length as the second argument.  I.e. this needs to be:

	min = array_index_nospec(min, map->max_apic_id + 1);

> +
>  	for_each_set_bit(i, ipi_bitmap,
>  		min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
>  		if (map->phys_map[min + i]) {
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 93636f77c42d..872e43defa67 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -10051,8 +10051,11 @@ static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id)
>  	rcu_read_lock();
>  	map = rcu_dereference(vcpu->kvm->arch.apic_map);
>  
> -	if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
> -		target = map->phys_map[dest_id]->vcpu;
> +	if (likely(map) && dest_id <= map->max_apic_id) {
> +		dest_id = array_index_nospec(dest_id, map->max_apic_id);

Same thing here.

> +		if (map->phys_map[dest_id])
> +			target = map->phys_map[dest_id]->vcpu;
> +	}
>  
>  	rcu_read_unlock();
>  
> -- 
> 2.50.1
> 

  reply	other threads:[~2025-07-24 13:38 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-24  6:00 [PATCH] KVM: x86: use array_index_nospec with indices that come from guest Greg Kroah-Hartman
2025-07-24 13:38 ` Sean Christopherson [this message]
2025-07-24 14:22   ` [PATCH v2] " Thijs Raymakers
2025-07-24 18:36     ` Greg Kroah-Hartman
2025-07-24 19:04       ` Sean Christopherson
2025-07-25  4:42         ` Greg Kroah-Hartman
2025-07-25 10:24           ` Thijs Raymakers
2025-08-11 11:34         ` Greg Kroah-Hartman
2025-08-11 14:35           ` Sean Christopherson
2025-08-11 15:16             ` Greg Kroah-Hartman
2025-08-15 22:23             ` 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=aII3WuhvJb3sY8HG@google.com \
    --to=seanjc@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=stable@kernel.org \
    --cc=thijs@raymakers.nl \
    /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.