Skip to contentSkip to conversation

KVM: x86: Fully initialize 'struct kvm_lapic_irq' in kvm_pv_kick_cpu_op()

PATCH Superseded Vitaly Kuznetsov 2 participants 3 messages Jun 28, 2022 – Jul 8, 2022

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: x86: Fully initialize 'struct kvm_lapic_irq' in kvm_pv_kick_cpu_op()

'vector' and 'trig_mode' fields of 'struct kvm_lapic_irq' are left uninitialized in kvm_pv_kick_cpu_op(). While these fields are normally not needed for APIC_DM_REMRD, they're still referenced by __apic_accept_irq() for trace_kvm_apic_accept_irq(). Fully initialize the structure to avoid consuming random stack memory.

Fixes a183b638b61c ("KVM: x86: make apic_accept_irq tracepoint more generic")
Reported-by [email protected]
---
 arch/x86/kvm/x86.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +10 -8
index 567d13405445..8a98608dad4f 100644
@@ -9340,15 +9340,17 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
  */
 static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid)
 {
-	struct kvm_lapic_irq lapic_irq;
-
-	lapic_irq.shorthand = APIC_DEST_NOSHORT;
-	lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
-	lapic_irq.level = 0;
-	lapic_irq.dest_id = apicid;
-	lapic_irq.msi_redir_hint = false;
+	struct kvm_lapic_irq lapic_irq = {
+		.vector = 0,
+		.delivery_mode = APIC_DM_REMRD,
+		.dest_mode = APIC_DEST_PHYSICAL,
+		.level = false,
+		.trig_mode = 0,
+		.shorthand = APIC_DEST_NOSHORT,
+		.dest_id = apicid,
+		.msi_redir_hint = false
+	};
 
-	lapic_irq.delivery_mode = APIC_DM_REMRD;
 	kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
 }
 
Signature · 1 line
2.35.3
#1 lore raw
Quoted 28 lines · Vitaly Kuznetsov

'vector' and 'trig_mode' fields of 'struct kvm_lapic_irq' are left uninitialized in kvm_pv_kick_cpu_op(). While these fields are normally not needed for APIC_DM_REMRD, they're still referenced by __apic_accept_irq() for trace_kvm_apic_accept_irq(). Fully initialize the structure to avoid consuming random stack memory.

Fixes a183b638b61c ("KVM: x86: make apic_accept_irq tracepoint more generic")
Reported-by [email protected]
---
 arch/x86/kvm/x86.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +0 -7
index 567d13405445..8a98608dad4f 100644
@@ -9340,15 +9340,17 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
  */
 static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid)
 {
-	struct kvm_lapic_irq lapic_irq;
-
-	lapic_irq.shorthand = APIC_DEST_NOSHORT;
-	lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
-	lapic_irq.level = 0;
-	lapic_irq.dest_id = apicid;
-	lapic_irq.msi_redir_hint = false;

arch/x86/kvm/x86.c static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, · line 9352

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+	struct kvm_lapic_irq lapic_irq = {
+		.vector = 0,
+		.delivery_mode = APIC_DM_REMRD,
+		.dest_mode = APIC_DEST_PHYSICAL,
+		.level = false,
+		.trig_mode = 0,
+		.shorthand = APIC_DEST_NOSHORT,
+		.dest_id = apicid,
+		.msi_redir_hint = false
+	};

What if we rely on the compiler to zero-initialize omitted fields? E.g.

/*
* All other fields are unused for APIC_DM_REMRD, but may be consumed by
* common code, e.g. for tracing. Defer initialization to the compiler. */
struct kvm_lapic_irq lapic_irq = {
.delivery_mode = APIC_DM_REMRD,
.dest_mode = APIC_DEST_PHYSICAL,
.shorthand = APIC_DEST_NOSHORT,
.dest_id = apicid,
};

KVM doesn't actually care about the vector, level, trig_mode, etc... for its magic magic DM_REMRD, i.e. using 0/false is completely arbitrary.

Quoted 8 lines
arch/x86/kvm/x86.c +0 -1
-	lapic_irq.delivery_mode = APIC_DM_REMRD;
 	kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
 }
 
Signature · 1 line
2.35.3
#2 lore raw
Quoted 53 lines · Sean Christopherson
Quoted 28 lines · Vitaly Kuznetsov

'vector' and 'trig_mode' fields of 'struct kvm_lapic_irq' are left uninitialized in kvm_pv_kick_cpu_op(). While these fields are normally not needed for APIC_DM_REMRD, they're still referenced by __apic_accept_irq() for trace_kvm_apic_accept_irq(). Fully initialize the structure to avoid consuming random stack memory.

Fixes a183b638b61c ("KVM: x86: make apic_accept_irq tracepoint more generic")
Reported-by [email protected]
---
 arch/x86/kvm/x86.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/x86.c +0 -7
index 567d13405445..8a98608dad4f 100644
@@ -9340,15 +9340,17 @@ static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
  */
 static void kvm_pv_kick_cpu_op(struct kvm *kvm, int apicid)
 {
-	struct kvm_lapic_irq lapic_irq;
-
-	lapic_irq.shorthand = APIC_DEST_NOSHORT;
-	lapic_irq.dest_mode = APIC_DEST_PHYSICAL;
-	lapic_irq.level = 0;
-	lapic_irq.dest_id = apicid;
-	lapic_irq.msi_redir_hint = false;

arch/x86/kvm/x86.c static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, · line 9352

Quoted 10 lines
arch/x86/kvm/x86.c +10 -0
+	struct kvm_lapic_irq lapic_irq = {
+		.vector = 0,
+		.delivery_mode = APIC_DM_REMRD,
+		.dest_mode = APIC_DEST_PHYSICAL,
+		.level = false,
+		.trig_mode = 0,
+		.shorthand = APIC_DEST_NOSHORT,
+		.dest_id = apicid,
+		.msi_redir_hint = false
+	};

What if we rely on the compiler to zero-initialize omitted fields? E.g.

/*
* All other fields are unused for APIC_DM_REMRD, but may be consumed by
* common code, e.g. for tracing. Defer initialization to the compiler. */
struct kvm_lapic_irq lapic_irq = {
.delivery_mode = APIC_DM_REMRD,
.dest_mode = APIC_DEST_PHYSICAL,
.shorthand = APIC_DEST_NOSHORT,
.dest_id = apicid,
};

Quoted 2 lines

KVM doesn't actually care about the vector, level, trig_mode, etc... for its magic magic DM_REMRD, i.e. using 0/false is completely arbitrary.

Honestly, I just always forget whether zero-initializing of omitted fields is a de-facto or de-jure standard. We don't care much whether these fields are zero of not but some random values in tracing may confuse an unprepared reader.

I'll send v2 with your amendment.

Quoted 10 lines
Quoted 9 lines
arch/x86/kvm/x86.c +0 -1
-	lapic_irq.delivery_mode = APIC_DM_REMRD;
 	kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
 }
 
Signature · 1 line
2.35.3
Signature · 1 line
Vitaly