linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: arm/arm64: vgic: fix off-by-one bug in vgic_get_irq()
@ 2018-12-12 20:11 Gustavo A. R. Silva
  2018-12-13 10:52 ` Marc Zyngier
  0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2018-12-12 20:11 UTC (permalink / raw)
  To: Mark Rutland, Christoffer Dall, Marc Zyngier, Will Deacon
  Cc: linux-arm-kernel, kvmarm, linux-kernel, Gustavo A. R. Silva

When using the nospec API, it should be taken into account that:

"...if the CPU speculates past the bounds check then
 * array_index_nospec() will clamp the index within the range of [0,
 * size)."

The above is part of the header for macro array_index_nospec() in
linux/nospec.h

Now, in this particular case, if intid evaluates to exactly VGIC_MAX_SPI
or to exaclty VGIC_MAX_PRIVATE, the array_index_nospec() macro ends up
returning VGIC_MAX_SPI - 1 or VGIC_MAX_PRIVATE - 1 respectively, instead
of VGIC_MAX_SPI or VGIC_MAX_PRIVATE, which, based on the original logic:

	/* SGIs and PPIs */
	if (intid <= VGIC_MAX_PRIVATE)
 		return &vcpu->arch.vgic_cpu.private_irqs[intid];

 	/* SPIs */
	if (intid <= VGIC_MAX_SPI)
 		return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];

are valid values for intid.

Fix this by calling array_index_nospec() macro with VGIC_MAX_PRIVATE + 1
and VGIC_MAX_SPI + 1 as arguments for its parameter size.

Fixes: 41b87599c743 ("KVM: arm/arm64: vgic: fix possible spectre-v1 in vgic_get_irq()")
Cc: stable@vger.kernel.org
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 virt/kvm/arm/vgic/vgic.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/arm/vgic/vgic.c b/virt/kvm/arm/vgic/vgic.c
index 50e25438fb3c..5db40696055a 100644
--- a/virt/kvm/arm/vgic/vgic.c
+++ b/virt/kvm/arm/vgic/vgic.c
@@ -103,13 +103,13 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
 {
 	/* SGIs and PPIs */
 	if (intid <= VGIC_MAX_PRIVATE) {
-		intid = array_index_nospec(intid, VGIC_MAX_PRIVATE);
+		intid = array_index_nospec(intid, VGIC_MAX_PRIVATE + 1);
 		return &vcpu->arch.vgic_cpu.private_irqs[intid];
 	}
 
 	/* SPIs */
 	if (intid <= VGIC_MAX_SPI) {
-		intid = array_index_nospec(intid, VGIC_MAX_SPI);
+		intid = array_index_nospec(intid, VGIC_MAX_SPI + 1);
 		return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
 	}
 
-- 
2.17.1


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

* Re: [PATCH] KVM: arm/arm64: vgic: fix off-by-one bug in vgic_get_irq()
  2018-12-12 20:11 [PATCH] KVM: arm/arm64: vgic: fix off-by-one bug in vgic_get_irq() Gustavo A. R. Silva
@ 2018-12-13 10:52 ` Marc Zyngier
  0 siblings, 0 replies; 2+ messages in thread
From: Marc Zyngier @ 2018-12-13 10:52 UTC (permalink / raw)
  To: Gustavo A. R. Silva, Mark Rutland, Christoffer Dall, Will Deacon
  Cc: kvmarm, linux-arm-kernel, linux-kernel

Hi Gustavo,

On 12/12/2018 20:11, Gustavo A. R. Silva wrote:
> When using the nospec API, it should be taken into account that:
> 
> "...if the CPU speculates past the bounds check then
>  * array_index_nospec() will clamp the index within the range of [0,
>  * size)."
> 
> The above is part of the header for macro array_index_nospec() in
> linux/nospec.h
> 
> Now, in this particular case, if intid evaluates to exactly VGIC_MAX_SPI
> or to exaclty VGIC_MAX_PRIVATE, the array_index_nospec() macro ends up
> returning VGIC_MAX_SPI - 1 or VGIC_MAX_PRIVATE - 1 respectively, instead
> of VGIC_MAX_SPI or VGIC_MAX_PRIVATE, which, based on the original logic:
> 
> 	/* SGIs and PPIs */
> 	if (intid <= VGIC_MAX_PRIVATE)
>  		return &vcpu->arch.vgic_cpu.private_irqs[intid];
> 
>  	/* SPIs */
> 	if (intid <= VGIC_MAX_SPI)
>  		return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
> 
> are valid values for intid.
> 
> Fix this by calling array_index_nospec() macro with VGIC_MAX_PRIVATE + 1
> and VGIC_MAX_SPI + 1 as arguments for its parameter size.
> 
> Fixes: 41b87599c743 ("KVM: arm/arm64: vgic: fix possible spectre-v1 in vgic_get_irq()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
>  virt/kvm/arm/vgic/vgic.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/virt/kvm/arm/vgic/vgic.c b/virt/kvm/arm/vgic/vgic.c
> index 50e25438fb3c..5db40696055a 100644
> --- a/virt/kvm/arm/vgic/vgic.c
> +++ b/virt/kvm/arm/vgic/vgic.c
> @@ -103,13 +103,13 @@ struct vgic_irq *vgic_get_irq(struct kvm *kvm, struct kvm_vcpu *vcpu,
>  {
>  	/* SGIs and PPIs */
>  	if (intid <= VGIC_MAX_PRIVATE) {
> -		intid = array_index_nospec(intid, VGIC_MAX_PRIVATE);
> +		intid = array_index_nospec(intid, VGIC_MAX_PRIVATE + 1);
>  		return &vcpu->arch.vgic_cpu.private_irqs[intid];
>  	}
>  
>  	/* SPIs */
>  	if (intid <= VGIC_MAX_SPI) {
> -		intid = array_index_nospec(intid, VGIC_MAX_SPI);
> +		intid = array_index_nospec(intid, VGIC_MAX_SPI + 1);
>  		return &kvm->arch.vgic.spis[intid - VGIC_NR_PRIVATE_IRQS];
>  	}
>  
> 

That's a good one. Out of curiosity, how did you find it?

Thanks,
	
	M.
-- 
Jazz is not dead. It just smells funny...

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

end of thread, other threads:[~2018-12-13 10:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-12 20:11 [PATCH] KVM: arm/arm64: vgic: fix off-by-one bug in vgic_get_irq() Gustavo A. R. Silva
2018-12-13 10:52 ` Marc Zyngier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).