kernel-janitors.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch RFC] kvm, cpuid: silence a buffer overflow warning
@ 2014-02-20 12:34 Dan Carpenter
  2014-02-20 13:07 ` Paolo Bonzini
  0 siblings, 1 reply; 5+ messages in thread
From: Dan Carpenter @ 2014-02-20 12:34 UTC (permalink / raw)
  To: Gleb Natapov
  Cc: Paolo Bonzini, Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86,
	kvm, kernel-janitors

This seems like a harmless off by one overflow if "i" is the last
element in the vcpu->arch.cpuid_entries[] array.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
Not tested.  I always wonder if it's worth fixing these or if it's worth
reporting them?  Either of those seem like a lot of work for something
harmless.

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index c6976257eff5..7d02c0fc768c 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -660,7 +660,7 @@ static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
 
 	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
 	/* when no next entry is found, the current entry[i] is reselected */
-	for (j = i + 1; ; j = (j + 1) % nent) {
+	for (j = (i + 1) % nent; ; j = (j + 1) % nent) {
 		struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
 		if (ej->function = e->function) {
 			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;

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

* Re: [patch RFC] kvm, cpuid: silence a buffer overflow warning
  2014-02-20 12:34 [patch RFC] kvm, cpuid: silence a buffer overflow warning Dan Carpenter
@ 2014-02-20 13:07 ` Paolo Bonzini
  2014-02-20 13:18   ` walter harms
  2014-02-20 13:23   ` Dan Carpenter
  0 siblings, 2 replies; 5+ messages in thread
From: Paolo Bonzini @ 2014-02-20 13:07 UTC (permalink / raw)
  To: Dan Carpenter, Gleb Natapov
  Cc: Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86, kvm,
	kernel-janitors

Il 20/02/2014 13:34, Dan Carpenter ha scritto:
> This seems like a harmless off by one overflow if "i" is the last
> element in the vcpu->arch.cpuid_entries[] array.
> 
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> Not tested.  I always wonder if it's worth fixing these or if it's worth
> reporting them?  Either of those seem like a lot of work for something
> harmless.

Could it oops if cpuid_nent is INT_MAX?  If so, it's not entirely harmless.
In this case I'd rather take the occasion to cleanup the code like this
(compile-tested):

diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 9fed5bedaad6..2fd6e7169936 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -656,18 +656,19 @@ out:
 static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
 {
 	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
-	int j, nent = vcpu->arch.cpuid_nent;
+	struct kvm_cpuid_entry2 *ej;
+	int j = i, nent = vcpu->arch.cpuid_nent;
 
 	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
+
 	/* when no next entry is found, the current entry[i] is reselected */
-	for (j = i + 1; ; j = (j + 1) % nent) {
-		struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
-		if (ej->function = e->function) {
-			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
-			return j;
-		}
-	}
-	return 0; /* silence gcc, even though control never reaches here */
+	do {
+		j = (j + 1) % nent;
+		ej = &vcpu->arch.cpuid_entries[j];
+	} while (ej->function != e->function);
+
+	ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
+	return j;
 }
 
 /* find an entry with matching function, matching index (if needed), and that

What do you think?

Paolo

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

* Re: [patch RFC] kvm, cpuid: silence a buffer overflow warning
  2014-02-20 13:07 ` Paolo Bonzini
@ 2014-02-20 13:18   ` walter harms
  2014-02-20 13:19     ` Paolo Bonzini
  2014-02-20 13:23   ` Dan Carpenter
  1 sibling, 1 reply; 5+ messages in thread
From: walter harms @ 2014-02-20 13:18 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Dan Carpenter, Gleb Natapov, Thomas Gleixner, Ingo Molnar,
	H. Peter Anvin, x86, kvm, kernel-janitors



Am 20.02.2014 14:07, schrieb Paolo Bonzini:
> Il 20/02/2014 13:34, Dan Carpenter ha scritto:
>> This seems like a harmless off by one overflow if "i" is the last
>> element in the vcpu->arch.cpuid_entries[] array.
>>
>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>> ---
>> Not tested.  I always wonder if it's worth fixing these or if it's worth
>> reporting them?  Either of those seem like a lot of work for something
>> harmless.
> 
> Could it oops if cpuid_nent is INT_MAX?  If so, it's not entirely harmless.
> In this case I'd rather take the occasion to cleanup the code like this
> (compile-tested):
> 
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 9fed5bedaad6..2fd6e7169936 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -656,18 +656,19 @@ out:
>  static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
>  {
>  	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
> -	int j, nent = vcpu->arch.cpuid_nent;
> +	struct kvm_cpuid_entry2 *ej;
> +	int j = i, nent = vcpu->arch.cpuid_nent;
>  
>  	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
> +
>  	/* when no next entry is found, the current entry[i] is reselected */
> -	for (j = i + 1; ; j = (j + 1) % nent) {
> -		struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
> -		if (ej->function = e->function) {
> -			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
> -			return j;
> -		}
> -	}
> -	return 0; /* silence gcc, even though control never reaches here */
> +	do {
> +		j = (j + 1) % nent;
> +		ej = &vcpu->arch.cpuid_entries[j];
> +	} while (ej->function != e->function);
> +
> +	ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
> +	return j;
>  }
>  
>  /* find an entry with matching function, matching index (if needed), and that
> 
> What do you think?
> 

Is there any guaranty that this will not loop forever ?

an if (i=j) return 0; would be on the save side. (I guess that
these was the idea behind the for).

re,
 wh

> Paolo
> --
> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

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

* Re: [patch RFC] kvm, cpuid: silence a buffer overflow warning
  2014-02-20 13:18   ` walter harms
@ 2014-02-20 13:19     ` Paolo Bonzini
  0 siblings, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2014-02-20 13:19 UTC (permalink / raw)
  To: wharms
  Cc: Dan Carpenter, Gleb Natapov, Thomas Gleixner, Ingo Molnar,
	H. Peter Anvin, x86, kvm, kernel-janitors

Il 20/02/2014 14:18, walter harms ha scritto:
>
>
> Am 20.02.2014 14:07, schrieb Paolo Bonzini:
>> Il 20/02/2014 13:34, Dan Carpenter ha scritto:
>>> This seems like a harmless off by one overflow if "i" is the last
>>> element in the vcpu->arch.cpuid_entries[] array.
>>>
>>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>>> ---
>>> Not tested.  I always wonder if it's worth fixing these or if it's worth
>>> reporting them?  Either of those seem like a lot of work for something
>>> harmless.
>>
>> Could it oops if cpuid_nent is INT_MAX?  If so, it's not entirely harmless.
>> In this case I'd rather take the occasion to cleanup the code like this
>> (compile-tested):
>>
>> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
>> index 9fed5bedaad6..2fd6e7169936 100644
>> --- a/arch/x86/kvm/cpuid.c
>> +++ b/arch/x86/kvm/cpuid.c
>> @@ -656,18 +656,19 @@ out:
>>  static int move_to_next_stateful_cpuid_entry(struct kvm_vcpu *vcpu, int i)
>>  {
>>  	struct kvm_cpuid_entry2 *e = &vcpu->arch.cpuid_entries[i];
>> -	int j, nent = vcpu->arch.cpuid_nent;
>> +	struct kvm_cpuid_entry2 *ej;
>> +	int j = i, nent = vcpu->arch.cpuid_nent;
>>
>>  	e->flags &= ~KVM_CPUID_FLAG_STATE_READ_NEXT;
>> +
>>  	/* when no next entry is found, the current entry[i] is reselected */
>> -	for (j = i + 1; ; j = (j + 1) % nent) {
>> -		struct kvm_cpuid_entry2 *ej = &vcpu->arch.cpuid_entries[j];
>> -		if (ej->function = e->function) {
>> -			ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
>> -			return j;
>> -		}
>> -	}
>> -	return 0; /* silence gcc, even though control never reaches here */
>> +	do {
>> +		j = (j + 1) % nent;
>> +		ej = &vcpu->arch.cpuid_entries[j];
>> +	} while (ej->function != e->function);
>> +
>> +	ej->flags |= KVM_CPUID_FLAG_STATE_READ_NEXT;
>> +	return j;
>>  }
>>
>>  /* find an entry with matching function, matching index (if needed), and that
>>
>> What do you think?
>>
>
> Is there any guaranty that this will not loop forever ?
>
> an if (i=j) return 0; would be on the save side. (I guess that
> these was the idea behind the for).

Once i=j you'll get ej->function = e->function and exit.

Paolo


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

* Re: [patch RFC] kvm, cpuid: silence a buffer overflow warning
  2014-02-20 13:07 ` Paolo Bonzini
  2014-02-20 13:18   ` walter harms
@ 2014-02-20 13:23   ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2014-02-20 13:23 UTC (permalink / raw)
  To: Paolo Bonzini
  Cc: Gleb Natapov, Thomas Gleixner, Ingo Molnar, H. Peter Anvin, x86,
	kvm, kernel-janitors

On Thu, Feb 20, 2014 at 02:07:42PM +0100, Paolo Bonzini wrote:
> Il 20/02/2014 13:34, Dan Carpenter ha scritto:
> > This seems like a harmless off by one overflow if "i" is the last
> > element in the vcpu->arch.cpuid_entries[] array.
> > 
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> > Not tested.  I always wonder if it's worth fixing these or if it's worth
> > reporting them?  Either of those seem like a lot of work for something
> > harmless.
> 
> Could it oops if cpuid_nent is INT_MAX?

cpuid_nent is capped at KVM_MAX_CPUID_ENTRIES (80).

regards,
dan carpenter


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

end of thread, other threads:[~2014-02-20 13:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-02-20 12:34 [patch RFC] kvm, cpuid: silence a buffer overflow warning Dan Carpenter
2014-02-20 13:07 ` Paolo Bonzini
2014-02-20 13:18   ` walter harms
2014-02-20 13:19     ` Paolo Bonzini
2014-02-20 13:23   ` 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 NNTP newsgroup(s).