Skip to contentSkip to conversation

KVM: apic: avoid calculating pending eoi from an uninitialized val

PATCH Superseded linmiaohe 3 participants 3 messages Feb 20, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

linmiaohe

author
#0 lore raw

PATCH 1/1 KVM: apic: avoid calculating pending eoi from an uninitialized val

From: Miaohe Lin <[email protected]>

When get user eoi value failed, var val would be uninitialized and result in calculating pending eoi from an uninitialized val. Initialize var val to 0 to fix this case.

---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +1 -1
index 4f14ec7525f6..7e77e94f3176 100644
@@ -626,7 +626,7 @@ static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
 
 static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
 {
-	u8 val;
+	u8 val = 0;
 	if (pv_eoi_get_user(vcpu, &val) < 0)
 		printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n",
 			   (unsigned long long)vcpu->arch.pv_eoi.msr_val);
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · linmiaohe

From: Miaohe Lin <[email protected]>

When get user eoi value failed, var val would be uninitialized and result in calculating pending eoi from an uninitialized val. Initialize var val to 0 to fix this case.

Let me try to suggest an alternative wording,

"When pv_eoi_get_user() fails, 'val' may remain uninitialized and the return value of pv_eoi_get_pending() becomes random. Fix the issue by initializing the variable."

Quoted 11 lines
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index 4f14ec7525f6..7e77e94f3176 100644

arch/x86/kvm/lapic.c static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu) · line 632

Quoted 8 lines
arch/x86/kvm/lapic.c +1 -1
@@ -626,7 +626,7 @@ static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
 
 static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
 {
-	u8 val;
+	u8 val = 0;
 	if (pv_eoi_get_user(vcpu, &val) < 0)
 		printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n",
 			   (unsigned long long)vcpu->arch.pv_eoi.msr_val);

But why compilers don't complain?

Signature · 1 line
Vitaly
#2 lore raw
Quoted 26 lines · Vitaly Kuznetsov
Quoted 5 lines · linmiaohe

From: Miaohe Lin <[email protected]>

When get user eoi value failed, var val would be uninitialized and result in calculating pending eoi from an uninitialized val. Initialize var val to 0 to fix this case.

Let me try to suggest an alternative wording,

"When pv_eoi_get_user() fails, 'val' may remain uninitialized and the return value of pv_eoi_get_pending() becomes random. Fix the issue by initializing the variable."

Quoted 12 lines
---
 arch/x86/kvm/lapic.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/lapic.c +0 -0
index 4f14ec7525f6..7e77e94f3176 100644
@@ -626,7 +626,7 @@ static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
 
Quoted 4 lines

arch/x86/kvm/lapic.c static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu) · line 629

Quoted 4 lines
arch/x86/kvm/lapic.c +1 -1
 static bool pv_eoi_get_pending(struct kvm_vcpu *vcpu)
 {
-	u8 val;
+	u8 val = 0;

Rather than initialize @val, I'd prefer to explicitly handle the error, similar to pv_eoi_clr_pending() and pv_eoi_set_pending(), e.g.

	u8 val;

	if (pv_eoi_get_user(vcpu, &val) < 0) {
		printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n",
			   (unsigned long long)vcpu->arch.pv_eoi.msr_val);
		return false;
	}
	return val & 0x1;
Quoted 7 lines

arch/x86/kvm/lapic.c static inline bool pv_eoi_enabled(struct kvm_vcpu *vcpu) · line 632

Quoted 3 lines
arch/x86/kvm/lapic.c +0 -0
 	if (pv_eoi_get_user(vcpu, &val) < 0)
 		printk(KERN_WARNING "Can't read EOI MSR value: 0x%llx\n",
 			   (unsigned long long)vcpu->arch.pv_eoi.msr_val);

But why compilers don't complain?

Clang might?