LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH 4/4] KVM: X86: Potential 'index out of range' bug
@ 2021-09-03  7:55 Jiang Jiasheng
  2021-09-03 11:44 ` Vitaly Kuznetsov
  0 siblings, 1 reply; 5+ messages in thread
From: Jiang Jiasheng @ 2021-09-03  7:55 UTC (permalink / raw)
  To: pbonzini, seanjc, vkuznets, wanpengli, jmattson, joro, tglx,
	mingo, bp, x86, hpa, jarkko, dave.hansen
  Cc: kvm, linux-kernel, linux-sgx, Jiang Jiasheng

The kvm_get_vcpu() will call for the array_index_nospec()
with the value of atomic_read(&(v->kvm)->online_vcpus) as size,
and the value of constant '0' as index.
If the size is also '0', it will be unreasonabe
that the index is no less than the size.

Signed-off-by: Jiang Jiasheng <jiasheng@iscas.ac.cn>
---
 arch/x86/kvm/x86.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index e0f4a46..c59013c 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2871,7 +2871,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
 				       offsetof(struct compat_vcpu_info, time));
 	if (vcpu->xen.vcpu_time_info_set)
 		kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
-	if (v == kvm_get_vcpu(v->kvm, 0))
+	if (atomic_read(&(v->kvm)->online_vcpus) > 0 && v == kvm_get_vcpu(v->kvm, 0))
 		kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
 	return 0;
 }
-- 
2.7.4


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

* Re: [PATCH 4/4] KVM: X86: Potential 'index out of range' bug
  2021-09-03  7:55 [PATCH 4/4] KVM: X86: Potential 'index out of range' bug Jiang Jiasheng
@ 2021-09-03 11:44 ` Vitaly Kuznetsov
  2021-09-03 15:03   ` Sean Christopherson
  0 siblings, 1 reply; 5+ messages in thread
From: Vitaly Kuznetsov @ 2021-09-03 11:44 UTC (permalink / raw)
  To: Jiang Jiasheng
  Cc: kvm, linux-kernel, linux-sgx, Jiang Jiasheng, pbonzini, seanjc,
	wanpengli, jmattson, joro, tglx, mingo, bp, x86, hpa, jarkko,
	dave.hansen

Jiang Jiasheng <jiasheng@iscas.ac.cn> writes:

> The kvm_get_vcpu() will call for the array_index_nospec()
> with the value of atomic_read(&(v->kvm)->online_vcpus) as size,
> and the value of constant '0' as index.
> If the size is also '0', it will be unreasonabe
> that the index is no less than the size.
>

Can this really happen?

'online_vcpus' is never decreased, it is increased with every
kvm_vm_ioctl_create_vcpu() call when a new vCPU is created and is set to
0 when all vCPUs are destroyed (kvm_free_vcpus()).

kvm_guest_time_update() takes a vcpu as a parameter, this means that at
least 1 vCPU is currently present so 'online_vcpus' just can't be zero.

> Signed-off-by: Jiang Jiasheng <jiasheng@iscas.ac.cn>
> ---
>  arch/x86/kvm/x86.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index e0f4a46..c59013c 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -2871,7 +2871,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
>  				       offsetof(struct compat_vcpu_info, time));
>  	if (vcpu->xen.vcpu_time_info_set)
>  		kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
> -	if (v == kvm_get_vcpu(v->kvm, 0))
> +	if (atomic_read(&(v->kvm)->online_vcpus) > 0 && v == kvm_get_vcpu(v->kvm, 0))
>  		kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
>  	return 0;
>  }

-- 
Vitaly


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

* Re: [PATCH 4/4] KVM: X86: Potential 'index out of range' bug
  2021-09-03 11:44 ` Vitaly Kuznetsov
@ 2021-09-03 15:03   ` Sean Christopherson
  2021-09-06 11:07     ` Vitaly Kuznetsov
  0 siblings, 1 reply; 5+ messages in thread
From: Sean Christopherson @ 2021-09-03 15:03 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: Jiang Jiasheng, kvm, linux-kernel, linux-sgx, pbonzini,
	wanpengli, jmattson, joro, tglx, mingo, bp, x86, hpa, jarkko,
	dave.hansen

On Fri, Sep 03, 2021, Vitaly Kuznetsov wrote:
> Jiang Jiasheng <jiasheng@iscas.ac.cn> writes:
> 
> > The kvm_get_vcpu() will call for the array_index_nospec()
> > with the value of atomic_read(&(v->kvm)->online_vcpus) as size,
> > and the value of constant '0' as index.
> > If the size is also '0', it will be unreasonabe
> > that the index is no less than the size.
> >
> 
> Can this really happen?
> 
> 'online_vcpus' is never decreased, it is increased with every
> kvm_vm_ioctl_create_vcpu() call when a new vCPU is created and is set to
> 0 when all vCPUs are destroyed (kvm_free_vcpus()).
> 
> kvm_guest_time_update() takes a vcpu as a parameter, this means that at
> least 1 vCPU is currently present so 'online_vcpus' just can't be zero.

Agreed, but doing kvm_get_vcpu() is ugly and overkill.

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 86539c1686fa..cc1cb9a401cd 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
                                       offsetof(struct compat_vcpu_info, time));
        if (vcpu->xen.vcpu_time_info_set)
                kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
-       if (v == kvm_get_vcpu(v->kvm, 0))
+       if (!kvm_vcpu_get_idx(v))
                kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
        return 0;
 }

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

* Re: [PATCH 4/4] KVM: X86: Potential 'index out of range' bug
  2021-09-03 15:03   ` Sean Christopherson
@ 2021-09-06 11:07     ` Vitaly Kuznetsov
  2021-09-08 18:42       ` Sean Christopherson
  0 siblings, 1 reply; 5+ messages in thread
From: Vitaly Kuznetsov @ 2021-09-06 11:07 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Jiang Jiasheng, kvm, linux-kernel, linux-sgx, pbonzini,
	wanpengli, jmattson, joro, tglx, mingo, bp, x86, hpa, jarkko,
	dave.hansen

Sean Christopherson <seanjc@google.com> writes:

> On Fri, Sep 03, 2021, Vitaly Kuznetsov wrote:
>> Jiang Jiasheng <jiasheng@iscas.ac.cn> writes:
>> 
>> > The kvm_get_vcpu() will call for the array_index_nospec()
>> > with the value of atomic_read(&(v->kvm)->online_vcpus) as size,
>> > and the value of constant '0' as index.
>> > If the size is also '0', it will be unreasonabe
>> > that the index is no less than the size.
>> >
>> 
>> Can this really happen?
>> 
>> 'online_vcpus' is never decreased, it is increased with every
>> kvm_vm_ioctl_create_vcpu() call when a new vCPU is created and is set to
>> 0 when all vCPUs are destroyed (kvm_free_vcpus()).
>> 
>> kvm_guest_time_update() takes a vcpu as a parameter, this means that at
>> least 1 vCPU is currently present so 'online_vcpus' just can't be zero.
>
> Agreed, but doing kvm_get_vcpu() is ugly and overkill.
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 86539c1686fa..cc1cb9a401cd 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
>                                        offsetof(struct compat_vcpu_info, time));
>         if (vcpu->xen.vcpu_time_info_set)
>                 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
> -       if (v == kvm_get_vcpu(v->kvm, 0))
> +       if (!kvm_vcpu_get_idx(v))

Do we really need to keep kvm_vcpu_get_idx() around though? It has only
3 users, all in arch/x86/kvm/hyperv.[ch], and the inline simpy returns
'vcpu->vcpu_idx'.

>                 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
>         return 0;
>  }
>

-- 
Vitaly


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

* Re: [PATCH 4/4] KVM: X86: Potential 'index out of range' bug
  2021-09-06 11:07     ` Vitaly Kuznetsov
@ 2021-09-08 18:42       ` Sean Christopherson
  0 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2021-09-08 18:42 UTC (permalink / raw)
  To: Vitaly Kuznetsov
  Cc: Jiang Jiasheng, kvm, linux-kernel, linux-sgx, pbonzini,
	wanpengli, jmattson, joro, tglx, mingo, bp, x86, hpa, jarkko,
	dave.hansen

On Mon, Sep 06, 2021, Vitaly Kuznetsov wrote:
> Sean Christopherson <seanjc@google.com> writes:
> 
> > On Fri, Sep 03, 2021, Vitaly Kuznetsov wrote:
> >> Jiang Jiasheng <jiasheng@iscas.ac.cn> writes:
> >> 
> >> > The kvm_get_vcpu() will call for the array_index_nospec()
> >> > with the value of atomic_read(&(v->kvm)->online_vcpus) as size,
> >> > and the value of constant '0' as index.
> >> > If the size is also '0', it will be unreasonabe
> >> > that the index is no less than the size.
> >> >
> >> 
> >> Can this really happen?
> >> 
> >> 'online_vcpus' is never decreased, it is increased with every
> >> kvm_vm_ioctl_create_vcpu() call when a new vCPU is created and is set to
> >> 0 when all vCPUs are destroyed (kvm_free_vcpus()).
> >> 
> >> kvm_guest_time_update() takes a vcpu as a parameter, this means that at
> >> least 1 vCPU is currently present so 'online_vcpus' just can't be zero.
> >
> > Agreed, but doing kvm_get_vcpu() is ugly and overkill.
> >
> > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> > index 86539c1686fa..cc1cb9a401cd 100644
> > --- a/arch/x86/kvm/x86.c
> > +++ b/arch/x86/kvm/x86.c
> > @@ -2969,7 +2969,7 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
> >                                        offsetof(struct compat_vcpu_info, time));
> >         if (vcpu->xen.vcpu_time_info_set)
> >                 kvm_setup_pvclock_page(v, &vcpu->xen.vcpu_time_info_cache, 0);
> > -       if (v == kvm_get_vcpu(v->kvm, 0))
> > +       if (!kvm_vcpu_get_idx(v))
> 
> Do we really need to keep kvm_vcpu_get_idx() around though? It has only
> 3 users, all in arch/x86/kvm/hyperv.[ch], and the inline simpy returns
> 'vcpu->vcpu_idx'.

Nope, looks like it's a holdover from before the introduction of vcpu_idx.  I'll
send a small series to jettison the wrapper and make the above change.

Thanks!

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

end of thread, other threads:[~2021-09-08 18:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-03  7:55 [PATCH 4/4] KVM: X86: Potential 'index out of range' bug Jiang Jiasheng
2021-09-03 11:44 ` Vitaly Kuznetsov
2021-09-03 15:03   ` Sean Christopherson
2021-09-06 11:07     ` Vitaly Kuznetsov
2021-09-08 18:42       ` Sean Christopherson

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).