KVM: x86: use array_index_nospec with indices that come from guest
PATCH 1/1 KVM: x86: use array_index_nospec with indices that come from guest
From: Thijs Raymakers <[email protected]>
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.
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/lapic.c +2 -0
@@ -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); +for_each_set_bit (i, ipi_bitmap,min ((u32 )BITS_PER_LONG , (map->max_apic_id - min +1 ))) {if (map->phys_map[min + i]) {
arch/x86/kvm/x86.c +5 -2
@@ -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); +if (map->phys_map[dest_id]) + target = map->phys_map[dest_id]->vcpu; + }rcu_read_unlock ();
Signature · 1 line
2.50.1
Sean Christopherson
maintainerQuoted 21 lines · Greg Kroah-Hartman
From: Thijs Raymakers <[email protected]>
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)arch/x86/kvm/lapic.c +0
-0
arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 855
Quoted 4 lines
arch/x86/kvm/lapic.c +1
-0@@ -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);
Quoted 9 lines
arch/x86/kvm/lapic.c +1
-0+for_each_set_bit (i, ipi_bitmap,min ((u32 )BITS_PER_LONG , (map->max_apic_id - min +1 ))) {if (map->phys_map[min + i]) {arch/x86/kvm/x86.c +0
-0
arch/x86/kvm/x86.c static void kvm_sched_yield(struct kvm_vcpu *vcpu, unsigned long dest_id) · line 10055
Quoted 7 lines
arch/x86/kvm/x86.c +2
-2@@ -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.
Quoted 9 lines
arch/x86/kvm/x86.c +3
-0+if (map->phys_map[dest_id]) + target = map->phys_map[dest_id]->vcpu; + }rcu_read_unlock ();Signature · 1 line
2.50.1
PATCH v2 1/1 KVM: x86: use array_index_nospec with indices that come from guest
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.
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
arch/x86/kvm/lapic.c +2 -0
@@ -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]) {
arch/x86/kvm/x86.c +5 -2
@@ -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 ();
Signature · 1 line
2.50.1
Quoted 9 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>
Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)
You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
thanks,
greg k-h
Sean Christopherson
maintainerQuoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.
Quoted 8 lines
Quoted 5 lines
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
But for this, I don't see any reason to send a v3.
Quoted 12 lines · Sean Christopherson
Quoted 11 lines · Greg Kroah-Hartman
Quoted 9 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>
Quoted 4 lines
Quoted 2 lines
Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Keep it please, I was just letting Thijs know.
Quoted 10 lines
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
Quoted 7 lines
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
But for this, I don't see any reason to send a v3.
That's great, thanks.
greg k-h
Quoted 12 lines · Greg Kroah-Hartman
Quoted 11 lines · Sean Christopherson
Quoted 10 lines · Greg Kroah-Hartman
Quoted 9 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>
Quoted 4 lines
Quoted 3 lines
Quoted 2 lines
Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Keep it please, I was just letting Thijs know.
Sorry about that. I was not entirely sure whether tags like Signed-Off should be kept or removed in a new revision. Thanks for the feedback.
Quoted 10 lines
Quoted 10 lines
Quoted 8 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
Changes in v2:
- As noted by Sean Christopherson, max_apic_id is inclusive but array_index_nospec is not. v2 adds one to the array_index_nospec size so the bounds do include max_apic_id
- Link to v1: https://lore.kernel.org/kvm/2025072540-eggbeater-crate-50af@gregkh/T/#u
Quoted 2 lines
Quoted 2 lines
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
Noted, will not do it like that next time.
Quoted 3 lines
Quoted 2 lines
But for this, I don't see any reason to send a v3.
That's great, thanks.
Thanks. I'm fairly new to the process of submitting patches over email, so apologies for my mistakes. Thank you for your patience and feedback, it is much appreciated. If you do prefer a v3 that does include the change log, just let me know.
- Thijs
Quoted 27 lines · Sean Christopherson
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Quoted 8 lines
Quoted 5 lines
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
Quoted 7 lines
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
But for this, I don't see any reason to send a v3.
Any status on this? I don't see it in linux-next at all, nor in 6.17-rc1
thanks,
greg k-h
Sean Christopherson
maintainerQuoted 31 lines · Greg Kroah-Hartman
Quoted 30 lines · Sean Christopherson
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Quoted 8 lines
Quoted 5 lines
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
Quoted 7 lines
Quoted 4 lines
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
But for this, I don't see any reason to send a v3.
Any status on this? I don't see it in linux-next at all, nor in 6.17-rc1
I'll get it applied and sent along to Paolo/Linus this week.
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Greg Kroah-Hartman
Quoted 33 lines · Sean Christopherson
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Quoted 8 lines
Quoted 5 lines
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
But for this, I don't see any reason to send a v3.
Any status on this? I don't see it in linux-next at all, nor in 6.17-rc1
I'll get it applied and sent along to Paolo/Linus this week.
Thanks!
Sean Christopherson
maintainerQuoted 35 lines · Sean Christopherson
Quoted 34 lines · Greg Kroah-Hartman
Quoted 33 lines · Sean Christopherson
Quoted 5 lines · Greg Kroah-Hartman
Quoted 4 lines · Thijs Raymakers
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.Quoted 8 lines
Quoted 5 lines
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Greg Kroah-Hartman <[email protected]>Nit, you shouldn't have added my signed off on a new version, but that's ok, I'm fine with it.
Want me to keep your SoB when applying, or drop it?
Quoted 9 lines
Quoted 4 lines
--- arch/x86/kvm/lapic.c | 2 ++ arch/x86/kvm/x86.c | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-)You also forgot to say what changed down here.
Don't know how strict the KVM maintainers are, I know I require these things fixed up...
I require the same things, but I also don't mind doing fixup when applying if that's the path of least resistance (and it's not a recurring problem).
I also strongly dislike using In-Reply-To for new versions, as it tends to confuse b4, and often confuses me as well.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
But for this, I don't see any reason to send a v3.
Any status on this? I don't see it in linux-next at all, nor in 6.17-rc1
I'll get it applied and sent along to Paolo/Linus this week.
I haven't forgotten about this, but I was out sick most of this week and v6.17-rc1 is crashing on my test systems, so I won't get this sent along until next week. Sorry for the delay.