public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] KVM: x86: use array_index_nospec with indices that come from guest
@ 2025-08-04  6:44 Thijs Raymakers
  2025-08-12 10:37 ` David Woodhouse
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Thijs Raymakers @ 2025-08-04  6:44 UTC (permalink / raw)
  To: kvm
  Cc: Thijs Raymakers, stable, Sean Christopherson, Paolo Bonzini,
	Greg Kroah-Hartman

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>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
Sean C. correctly pointed out that max_apic_id is inclusive, while
array_index_nospec is not.

Changes in v3:
- Put the diff and the changes to the feedback in a single patch, instead
  of spreading it out over multiple emails.
- Remove premature SoB.
- Not sent as a In-Reply-To the previous version.

Changes in v2:
- Add one to the length argument of array_index_nospec, so it becomes
  inclusive with max_apic_id.

---
 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..0725d2cae742 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 + 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..43b63f1d1594 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 + 1);
+		if (map->phys_map[dest_id])
+			target = map->phys_map[dest_id]->vcpu;
+	}
 
 	rcu_read_unlock();
 
-- 
2.50.1


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-03-06 12:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-04  6:44 [PATCH v3] KVM: x86: use array_index_nospec with indices that come from guest Thijs Raymakers
2025-08-12 10:37 ` David Woodhouse
2025-08-12 19:48   ` Jim Mattson
2025-08-19 23:11 ` Sean Christopherson
2026-03-05 20:30 ` David Woodhouse
2026-03-05 22:22   ` Jim Mattson
2026-03-05 22:29     ` Sean Christopherson
2026-03-05 22:42       ` David Woodhouse
2026-03-06  1:54         ` Sean Christopherson
2026-03-06  8:05           ` David Woodhouse
2026-03-06 12:02             ` Thijs Raymakers
2026-03-06 10:45   ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox