Skip to contentSkip to conversation

kvm: Fix off by one in kvm_for_each_vcpu iteration

PATCH Applied Jeff Mahoney 2 participants 2 messages Apr 13, 2011 – Apr 17, 2011 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: Fix off by one in kvm_for_each_vcpu iteration

This patch avoids gcc issuing the following warning when KVM_MAX_VCPUS=1:
warning: array subscript is above array bounds

kvm_for_each_vcpu currently checks to see if the index for the vcpu is valid /after/ loading it. We don't run into problems because the address is still inside the enclosing struct kvm and we never deference or write to it, so this isn't a security issue.

The warning occurs when KVM_MAX_VCPUS=1 because the increment portion of the loop will *always* cause the loop to load an invalid location since ++idx will always be > 0.

This patch moves the load so that the check occurs before the load and we don't run into the compiler warning.

---
 include/linux/kvm_host.h |    7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
include/linux/kvm_host.h +4 -3
@@ -292,9 +292,10 @@ static inline struct kvm_vcpu *kvm_get_v
 }
 
 #define kvm_for_each_vcpu(idx, vcpup, kvm) \
-	for (idx = 0, vcpup = kvm_get_vcpu(kvm, idx); \
-	     idx < atomic_read(&kvm->online_vcpus) && vcpup; \
-	     vcpup = kvm_get_vcpu(kvm, ++idx))
+	for (idx = 0; \
+	     idx < atomic_read(&kvm->online_vcpus) && \
+	     (vcpup = kvm_get_vcpu(kvm, idx)) != NULL; \
+	     idx++)
 
 int kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id);
 void kvm_vcpu_uninit(struct kvm_vcpu *vcpu);
Signature · 2 lines
Jeff Mahoney
SUSE Labs
Applied
#1 lore raw
Quoted 8 lines · Jeff Mahoney

This patch avoids gcc issuing the following warning when KVM_MAX_VCPUS=1:
warning: array subscript is above array bounds

kvm_for_each_vcpu currently checks to see if the index for the vcpu is valid /after/ loading it. We don't run into problems because the address is still inside the enclosing struct kvm and we never deference or write to it, so this isn't a security issue.

Quoted 6 lines

The warning occurs when KVM_MAX_VCPUS=1 because the increment portion of the loop will *always* cause the loop to load an invalid location since ++idx will always be> 0.

This patch moves the load so that the check occurs before the load and we don't run into the compiler warning.

Applied, thanks.

Signature · 1 line
error compiling committee.c: too many arguments to function