KVM: x86: use array_index_nospec with indices that come from guest
PATCH v3 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.
---
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(-)
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 6 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
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Greg Kroah-Hartman <[email protected]>---
Sean C. correctly pointed out thatmax_apic_idis inclusive, whilearray_index_nospecis not.
?
Quoted 17 lines · David Woodhouse
Quoted 6 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
Signed-off-by Thijs Raymakers <[email protected]>Cc stable <[email protected]>Cc Sean Christopherson <[email protected]>Cc Paolo Bonzini <[email protected]>Cc Greg Kroah-Hartman <[email protected]>---
Sean C. correctly pointed out thatmax_apic_idis inclusive, whilearray_index_nospecis not.
Quoted 1 line
Fixes 715062970f37 ("KVM: X86: Implement PV sched yield hypercall")
And possibly:
Though, perhaps the blame really lies with commit 4180bf1b655a ("KVM:
X86: Implement "send IPI" hypercall").
Quoted 1 line
?
Sean Christopherson
maintainerQuoted 3 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.
Finally applied to kvm-x86 fixes, thanks!
[1/1] KVM: x86: use array_index_nospec with indices that come from guest
https://github.com/kvm-x86/linux/commit/c87bd4dd43a6
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.
(commit c87bd4dd43a6)
Is this sufficient in the __pv_send_ipi() case?
Quoted 11 lines
arch/x86/kvm/lapic.c +0
-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;arch/x86/kvm/lapic.c +2
-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]) {
vcpu = map->phys_map[min + i]->vcpu;
count += kvm_apic_set_irq(vcpu, irq, NULL);
}
}Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the for_each_set_bit() loop does mean that it won't actually execute past max_apic_id but is that sufficient to protect against *speculative* execution?
I have a variant of this which uses array_index_nospec(min+i, ...) *inside* the loop.
Quoted 27 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count += kvm_apic_set_irq(vcpu, irq, NULL); } }
Quoted 8 lines
Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.
Heh. Me too!
Sean Christopherson
maintainerQuoted 30 lines · Jim Mattson
Quoted 29 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count += kvm_apic_set_irq(vcpu, irq, NULL); } }Do we need to protect [min + i] in the loop, rather than just [min]?
Quoted 8 lines
Quoted 6 lines
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.Heh. Me too!
LOL, OMG, get off your high horses you two and someone send a damn patch!
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jim Mattson
Quoted 33 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count += kvm_apic_set_irq(vcpu, irq, NULL); } }Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.Heh. Me too!
LOL, OMG, get off your high horses you two and someone send a damn patch!
Heh, happy to, but it was actually a genuine question. Our pre-embargo patches did it in the loop but the most likely explanation seemed to be that upstream changed it as a valid optimization (because somehow the loop wasn't vulnerable?), and that we *can* drop the old patches in favour of the upstream one.
If no such reason exists for why the patch got changed, I'm happy to post the delta.
Sean Christopherson
maintainerQuoted 43 lines · David Woodhouse
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jim Mattson
Quoted 33 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count += kvm_apic_set_irq(vcpu, irq, NULL); } }Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.Heh. Me too!
LOL, OMG, get off your high horses you two and someone send a damn patch!
Quoted 8 lines
Heh, happy to, but it was actually a genuine question. Our pre-embargo patches did it in the loop but the most likely explanation seemed to be that upstream changed it as a valid optimization (because somehow the loop wasn't vulnerable?), and that we *can* drop the old patches in favour of the upstream one.
If no such reason exists for why the patch got changed, I'm happy to post the delta.
AFAIK, there was no such justification. I'm pretty sure the only upstream version I've ever seen is what ended up in-tree.
Speculation stuff definitely isn't my area of expertise. Honestly, you, Jim, and a few others are who I'd go bug for answers for this sort of thing, so unless someone chimes in with a strong argument for the current code, I say we go with the more conservative approach.
Quoted 53 lines · Sean Christopherson
Quoted 43 lines · David Woodhouse
Quoted 35 lines · Sean Christopherson
Quoted 34 lines · Jim Mattson
Quoted 33 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count +=
kvm_apic_set_irq(vcpu, irq, NULL); }
}Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.Heh. Me too!
LOL, OMG, get off your high horses you two and someone send a damn patch!
Quoted 8 lines
Heh, happy to, but it was actually a genuine question. Our pre-embargo patches did it in the loop but the most likely explanation seemed to be that upstream changed it as a valid optimization (because somehow the loop wasn't vulnerable?), and that we *can* drop the old patches in favour of the upstream one.
If no such reason exists for why the patch got changed, I'm happy to post the delta.
Quoted 7 lines
AFAIK, there was no such justification. I'm pretty sure the only upstream version I've ever seen is what ended up in-tree.
Speculation stuff definitely isn't my area of expertise. Honestly, you, Jim, and a few others are who I'd go bug for answers for this sort of thing, so unless someone chimes in with a strong argument for the current code, I say we go with the more conservative approach.
Quoted 51 lines · David Woodhouse
Quoted 50 lines · Sean Christopherson
Quoted 38 lines · David Woodhouse
Quoted 29 lines · Sean Christopherson
Quoted 28 lines · Jim Mattson
Quoted 27 lines · David Woodhouse
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.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count +=
kvm_apic_set_irq(vcpu, irq, NULL); }
}Do we need to protect [min + i] in the loop, rather than just [min]?
Quoted 8 lines
Quoted 7 lines
Quoted 6 lines
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.Heh. Me too!
LOL, OMG, get off your high horses you two and someone send a damn patch!
Quoted 8 lines
Heh, happy to, but it was actually a genuine question. Our pre-embargo patches did it in the loop but the most likely explanation seemed to be that upstream changed it as a valid optimization (because somehow the loop wasn't vulnerable?), and that we *can* drop the old patches in favour of the upstream one.
If no such reason exists for why the patch got changed, I'm happy to post the delta.
AFAIK, there was no such justification. I'm pretty sure the only upstream version I've ever seen is what ended up in-tree.
Quoted 6 lines
Quoted 4 lines
Speculation stuff definitely isn't my area of expertise. Honestly, you, Jim, and a few others are who I'd go bug for answers for this sort of thing, so unless someone chimes in with a strong argument for the current code, I say we go with the more conservative approach.
I looked back to my notes, and this seems to be an oversight on my end. The goal of the original patch was to remove a speculative cache load with an arbitrary value of 'min', because it allowed a guest to load any host memory into its cache. I thought it reduced the maximum speculative buffer out-of-bounds load from all of host memory to no out-of-bounds load, but it reduced it to a maximum of BITS_PER_LONG elements of map->phys_map instead.
For the perspective of my original proof-of-concept, I do not see how an attacker could abuse this small window. In theory, depending on the data that sits directly after the phys_map array, it could theory be used to leak a pointer from the host (and probably not much else). Last time I checked, there was no data directly after the phys_map array, but that could change due to any number of reasons that are unrelated to this particular piece of code.
I am in favor of the more conservative approach and using array_index_nospec *inside* of the loop. The original patch does prevent my original proof-of-concept, which is probably why I missed this. I do not think that the current speculative out-of-bounds load is exploitable in practice, but it is better to take a more conservative approach in this case.
Paolo Bonzini
maintainerIl gio 5 mar 2026, 21:31 David Woodhouse <[email protected]> ha scritto:
Quoted 27 lines · Thijs Raymakers
Quoted 4 lines
min and
dest_idare guest-controlled indices. Usingarray_index_nospec()after the bounds checks clamps these values to mitigate speculative execution side-channels.(commit
c87bd4dd43a6)Is this sufficient in the
__pv_send_ipi()case?arch/x86/kvm/lapic.c static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, · line 859
Quoted 11 lines
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]) {vcpu = map->phys_map[min + i]->vcpu; count += kvm_apic_set_irq(vcpu, irq, NULL); } }
Quoted 5 lines
Do we need to protect [min + i] in the loop, rather than just [min]?
The end condition for the
for_each_set_bit()loop does mean that it won't actually execute pastmax_apic_idbut is that sufficient to protect against *speculative* execution?
You would be able to load extra values in the cache but it would not be a fully guest controlled load in the same way that Spectre wants to do it. Spectre works because the value used in the speculative load is way beyond the maximum and points to attacker-controlled memory.
That said it doesn't hurt either, other than a few clock cycles for not hoisting array_index_nospec out of the loop.
Paolo
Quoted 4 lines
I have a variant of this which uses
array_index_nospec(min+i, ...)*inside* the loop.