Skip to contentSkip to conversation

kvm: x86: potential shift wrapping bug

PATCH In review Dan Carpenter 2 participants 2 messages Nov 24, 2014

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: x86: potential shift wrapping bug

cs.base is declared as a __u64 variable and vector is a u32 so this causes a static checker warning. I'm not very familiar with this code but my understanding is that the user can set "sipi_vector" to any u32 value in kvm_vcpu_ioctl_x86_set_vcpu_events().

arch/x86/kvm/x86.c +1 -1
index 34c8f94..6608115 100644
@@ -7000,7 +7000,7 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, unsigned int vector)
 
 	kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
 	cs.selector = vector << 8;
-	cs.base = vector << 12;
+	cs.base = (u64)vector << 12;
 	kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
 	kvm_rip_write(vcpu, 0);
 }
maintainer
#1 lore raw
Quoted 4 lines · Dan Carpenter

cs.base is declared as a __u64 variable and vector is a u32 so this causes a static checker warning. I'm not very familiar with this code but my understanding is that the user can set "sipi_vector" to any u32 value in kvm_vcpu_ioctl_x86_set_vcpu_events().

The user can do so, but it should not set it to any value greater than
255. So the right fix is to cast to (u8).

Thanks for the report!

Paolo

Quoted 16 lines
arch/x86/kvm/x86.c +1 -1
index 34c8f94..6608115 100644
@@ -7000,7 +7000,7 @@ void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, unsigned int vector)
 
 	kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
 	cs.selector = vector << 8;
-	cs.base = vector << 12;
+	cs.base = (u64)vector << 12;
 	kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
 	kvm_rip_write(vcpu, 0);
 }