2022-10-10 14:25:42

by Dan Carpenter

[permalink] [raw]
Subject: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'

tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head: 493ffd6605b2d3d4dc7008ab927dba319f36671f
commit: 828ca89628bfcb1b8f27535025f69dd00eb55207 KVM: x86: Expose TSC offset controls to userspace
config: x86_64-randconfig-m001-20221010
compiler: gcc-11 (Debian 11.3.0-5) 11.3.0

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Reported-by: Dan Carpenter <[email protected]>

smatch warnings:
arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'

vim +/offset +4988 arch/x86/kvm/x86.c

828ca89628bfcb Oliver Upton 2021-09-16 4962 static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
828ca89628bfcb Oliver Upton 2021-09-16 4963 struct kvm_device_attr *attr)
828ca89628bfcb Oliver Upton 2021-09-16 4964 {
828ca89628bfcb Oliver Upton 2021-09-16 4965 u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
828ca89628bfcb Oliver Upton 2021-09-16 4966 struct kvm *kvm = vcpu->kvm;
828ca89628bfcb Oliver Upton 2021-09-16 4967 int r;
828ca89628bfcb Oliver Upton 2021-09-16 4968
828ca89628bfcb Oliver Upton 2021-09-16 4969 if ((u64)(unsigned long)uaddr != attr->addr)
828ca89628bfcb Oliver Upton 2021-09-16 4970 return -EFAULT;
828ca89628bfcb Oliver Upton 2021-09-16 4971
828ca89628bfcb Oliver Upton 2021-09-16 4972 switch (attr->attr) {
828ca89628bfcb Oliver Upton 2021-09-16 4973 case KVM_VCPU_TSC_OFFSET: {
828ca89628bfcb Oliver Upton 2021-09-16 4974 u64 offset, tsc, ns;
828ca89628bfcb Oliver Upton 2021-09-16 4975 unsigned long flags;
828ca89628bfcb Oliver Upton 2021-09-16 4976 bool matched;
828ca89628bfcb Oliver Upton 2021-09-16 4977
828ca89628bfcb Oliver Upton 2021-09-16 4978 r = -EFAULT;
828ca89628bfcb Oliver Upton 2021-09-16 4979 if (get_user(offset, uaddr))
828ca89628bfcb Oliver Upton 2021-09-16 4980 break;
828ca89628bfcb Oliver Upton 2021-09-16 4981
828ca89628bfcb Oliver Upton 2021-09-16 4982 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
828ca89628bfcb Oliver Upton 2021-09-16 4983
828ca89628bfcb Oliver Upton 2021-09-16 4984 matched = (vcpu->arch.virtual_tsc_khz &&
828ca89628bfcb Oliver Upton 2021-09-16 4985 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz &&
828ca89628bfcb Oliver Upton 2021-09-16 4986 kvm->arch.last_tsc_offset == offset);
828ca89628bfcb Oliver Upton 2021-09-16 4987
828ca89628bfcb Oliver Upton 2021-09-16 @4988 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset;

Smatch hates obvious user triggerable integer overflows... No checking
on offset.

828ca89628bfcb Oliver Upton 2021-09-16 4989 ns = get_kvmclock_base_ns();
828ca89628bfcb Oliver Upton 2021-09-16 4990
828ca89628bfcb Oliver Upton 2021-09-16 4991 __kvm_synchronize_tsc(vcpu, offset, tsc, ns, matched);
828ca89628bfcb Oliver Upton 2021-09-16 4992 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
828ca89628bfcb Oliver Upton 2021-09-16 4993
828ca89628bfcb Oliver Upton 2021-09-16 4994 r = 0;
828ca89628bfcb Oliver Upton 2021-09-16 4995 break;
828ca89628bfcb Oliver Upton 2021-09-16 4996 }
828ca89628bfcb Oliver Upton 2021-09-16 4997 default:
828ca89628bfcb Oliver Upton 2021-09-16 4998 r = -ENXIO;
828ca89628bfcb Oliver Upton 2021-09-16 4999 }
828ca89628bfcb Oliver Upton 2021-09-16 5000
828ca89628bfcb Oliver Upton 2021-09-16 5001 return r;
828ca89628bfcb Oliver Upton 2021-09-16 5002 }


2022-10-10 18:56:16

by Sean Christopherson

[permalink] [raw]
Subject: Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'

On Mon, Oct 10, 2022, Dan Carpenter wrote:
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: 493ffd6605b2d3d4dc7008ab927dba319f36671f
> commit: 828ca89628bfcb1b8f27535025f69dd00eb55207 KVM: x86: Expose TSC offset controls to userspace
> config: x86_64-randconfig-m001-20221010
> compiler: gcc-11 (Debian 11.3.0-5) 11.3.0
>
> If you fix the issue, kindly add following tag where applicable
> | Reported-by: kernel test robot <[email protected]>
> | Reported-by: Dan Carpenter <[email protected]>
>
> smatch warnings:
> arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'
>
> vim +/offset +4988 arch/x86/kvm/x86.c
>
> 828ca89628bfcb Oliver Upton 2021-09-16 4962 static int kvm_arch_tsc_set_attr(struct kvm_vcpu *vcpu,
> 828ca89628bfcb Oliver Upton 2021-09-16 4963 struct kvm_device_attr *attr)
> 828ca89628bfcb Oliver Upton 2021-09-16 4964 {
> 828ca89628bfcb Oliver Upton 2021-09-16 4965 u64 __user *uaddr = (u64 __user *)(unsigned long)attr->addr;
> 828ca89628bfcb Oliver Upton 2021-09-16 4966 struct kvm *kvm = vcpu->kvm;
> 828ca89628bfcb Oliver Upton 2021-09-16 4967 int r;
> 828ca89628bfcb Oliver Upton 2021-09-16 4968
> 828ca89628bfcb Oliver Upton 2021-09-16 4969 if ((u64)(unsigned long)uaddr != attr->addr)
> 828ca89628bfcb Oliver Upton 2021-09-16 4970 return -EFAULT;
> 828ca89628bfcb Oliver Upton 2021-09-16 4971
> 828ca89628bfcb Oliver Upton 2021-09-16 4972 switch (attr->attr) {
> 828ca89628bfcb Oliver Upton 2021-09-16 4973 case KVM_VCPU_TSC_OFFSET: {
> 828ca89628bfcb Oliver Upton 2021-09-16 4974 u64 offset, tsc, ns;
> 828ca89628bfcb Oliver Upton 2021-09-16 4975 unsigned long flags;
> 828ca89628bfcb Oliver Upton 2021-09-16 4976 bool matched;
> 828ca89628bfcb Oliver Upton 2021-09-16 4977
> 828ca89628bfcb Oliver Upton 2021-09-16 4978 r = -EFAULT;
> 828ca89628bfcb Oliver Upton 2021-09-16 4979 if (get_user(offset, uaddr))
> 828ca89628bfcb Oliver Upton 2021-09-16 4980 break;
> 828ca89628bfcb Oliver Upton 2021-09-16 4981
> 828ca89628bfcb Oliver Upton 2021-09-16 4982 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
> 828ca89628bfcb Oliver Upton 2021-09-16 4983
> 828ca89628bfcb Oliver Upton 2021-09-16 4984 matched = (vcpu->arch.virtual_tsc_khz &&
> 828ca89628bfcb Oliver Upton 2021-09-16 4985 kvm->arch.last_tsc_khz == vcpu->arch.virtual_tsc_khz &&
> 828ca89628bfcb Oliver Upton 2021-09-16 4986 kvm->arch.last_tsc_offset == offset);
> 828ca89628bfcb Oliver Upton 2021-09-16 4987
> 828ca89628bfcb Oliver Upton 2021-09-16 @4988 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset;
>
> Smatch hates obvious user triggerable integer overflows... No checking
> on offset.

This is ok, and even necessary, e.g. if the host TSC > guest TSC. Is there anything
we can do in KVM to help Smatch avoid false positives? Or do you/Smatch already
maintain a list of known false positives?

2022-10-11 13:18:35

by Paolo Bonzini

[permalink] [raw]
Subject: Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'

On 10/10/22 20:39, Sean Christopherson wrote:
>> 828ca89628bfcb Oliver Upton 2021-09-16 @4988 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset;
>>
>> Smatch hates obvious user triggerable integer overflows... No checking
>> on offset.
>
> This is ok, and even necessary, e.g. if the host TSC > guest TSC.

(which in fact is the common case). Also this is unsigned which is fine
according to the C standard, though I understand that static analyzers
want to be stricter.

> Is there anything
> we can do in KVM to help Smatch avoid false positives? Or do you/Smatch already
> maintain a list of known false positives?

Seconded.

Paolo

2022-10-12 06:41:40

by Dan Carpenter

[permalink] [raw]
Subject: Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'

On Tue, Oct 11, 2022 at 03:02:28PM +0200, Paolo Bonzini wrote:
> On 10/10/22 20:39, Sean Christopherson wrote:
> > > 828ca89628bfcb Oliver Upton 2021-09-16 @4988 tsc = kvm_scale_tsc(vcpu, rdtsc(), vcpu->arch.l1_tsc_scaling_ratio) + offset;
> > >
> > > Smatch hates obvious user triggerable integer overflows... No checking
> > > on offset.
> >
> > This is ok, and even necessary, e.g. if the host TSC > guest TSC.
>
> (which in fact is the common case). Also this is unsigned which is fine
> according to the C standard, though I understand that static analyzers want
> to be stricter.
>
> > Is there anything
> > we can do in KVM to help Smatch avoid false positives? Or do you/Smatch already
> > maintain a list of known false positives?
>
> Seconded.

Thanks for your feedback.

I could probably make a rule to ignore clock related stuff. That's
honestly been a known source of false positives for a while. I kind of
have the infrastructure so it's not super hard to do actually... I'll do
that.

regards,
dan carpenter