LKML Archive mirror
 help / color / mirror / Atom feed
* [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'
@ 2022-10-10 14:15 Dan Carpenter
  2022-10-10 18:39 ` Sean Christopherson
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2022-10-10 14:15 UTC (permalink / raw
  To: kbuild, Oliver Upton; +Cc: lkp, kbuild-all, linux-kernel, Paolo Bonzini

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 <lkp@intel.com>
| Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

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  }


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

* Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'
  2022-10-10 14:15 [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset' Dan Carpenter
@ 2022-10-10 18:39 ` Sean Christopherson
  2022-10-11 13:02   ` Paolo Bonzini
  0 siblings, 1 reply; 4+ messages in thread
From: Sean Christopherson @ 2022-10-10 18:39 UTC (permalink / raw
  To: Dan Carpenter
  Cc: kbuild, Oliver Upton, lkp, kbuild-all, linux-kernel,
	Paolo Bonzini

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 <lkp@intel.com>
> | Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> 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?

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

* Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'
  2022-10-10 18:39 ` Sean Christopherson
@ 2022-10-11 13:02   ` Paolo Bonzini
  2022-10-12  6:34     ` Dan Carpenter
  0 siblings, 1 reply; 4+ messages in thread
From: Paolo Bonzini @ 2022-10-11 13:02 UTC (permalink / raw
  To: Sean Christopherson, Dan Carpenter
  Cc: kbuild, Oliver Upton, lkp, kbuild-all, linux-kernel

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


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

* Re: [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset'
  2022-10-11 13:02   ` Paolo Bonzini
@ 2022-10-12  6:34     ` Dan Carpenter
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2022-10-12  6:34 UTC (permalink / raw
  To: Paolo Bonzini
  Cc: Sean Christopherson, kbuild, Oliver Upton, lkp, kbuild-all,
	linux-kernel

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


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

end of thread, other threads:[~2022-10-12  6:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-10 14:15 [kbuild] arch/x86/kvm/x86.c:4988 kvm_arch_tsc_set_attr() warn: check for integer overflow 'offset' Dan Carpenter
2022-10-10 18:39 ` Sean Christopherson
2022-10-11 13:02   ` Paolo Bonzini
2022-10-12  6:34     ` Dan Carpenter

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 read-only IMAP folder(s) and NNTP newsgroup(s).