Skip to contentSkip to conversation

kvm: Clear variables which may not be used

PATCH In review Li Zhang 2 participants 3 messages Dec 6, 2021

lore
Share & tools
lore

Li Zhang

author
#0 lore raw

PATCH 1/1 kvm: Clear variables which may not be used

The variables msi, route in kvm_irqchip_send_msi may be uninitialised values in some cases. It's necessary to clear them.

---
 accel/kvm/kvm-all.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
accel/kvm/kvm-all.c +2 -3
index eecd8031cf..bd50dc6b80 100644
@@ -1913,10 +1913,8 @@ static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
 
 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
 {
-    struct kvm_msi msi;
-    KVMMSIRoute *route;
-
     if (kvm_direct_msi_allowed) {
+        struct kvm_msi msi;
         msi.address_lo = (uint32_t)msg.address;
         msi.address_hi = msg.address >> 32;
         msi.data = le32_to_cpu(msg.data);
@@ -1926,6 +1924,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
         return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
     }
 
+    KVMMSIRoute *route;
     route = kvm_lookup_msi_route(s, msg);
     if (!route) {
         int virq;
Signature · 1 line
2.31.1
#1 lore raw
Quoted 2 lines · Li Zhang

The variables msi, route in kvm_irqchip_send_msi may be uninitialised values in some cases. It's necessary to clear them.

You say the patch is going to 'clear them' but....

Quoted 11 lines
---
 accel/kvm/kvm-all.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
accel/kvm/kvm-all.c +0 -0
index eecd8031cf..bd50dc6b80 100644

accel/kvm/kvm-all.c static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg) · line 1917

Quoted 8 lines
accel/kvm/kvm-all.c +1 -3
@@ -1913,10 +1913,8 @@ static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
 
 int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
 {
-    struct kvm_msi msi;
-    KVMMSIRoute *route;
-
     if (kvm_direct_msi_allowed) {
+        struct kvm_msi msi;

...but this is still an uninitialized declaration.

accel/kvm/kvm-all.c static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg) · line 1920

Quoted 3 lines
accel/kvm/kvm-all.c +0 -0
         msi.address_lo = (uint32_t)msg.address;
         msi.address_hi = msg.address >> 32;
         msi.data = le32_to_cpu(msg.data);

I guess the bug you were wanting to fix is that this code only initializes 5 out of 6 struct fields, before calling the ioctl.

accel/kvm/kvm-all.c int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) · line 1927

Quoted 5 lines
accel/kvm/kvm-all.c +1 -0
@@ -1926,6 +1924,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
         return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
     }
 
+    KVMMSIRoute *route;

This was initialized correctly before and didn't need moving

accel/kvm/kvm-all.c int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) · line 1930

Quoted 3 lines
accel/kvm/kvm-all.c +0 -0
     route = kvm_lookup_msi_route(s, msg);
     if (!route) {
         int virq;

Regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
author
#2 lore raw
Quoted 4 lines · Daniel P. Berrangé
Quoted 2 lines · Li Zhang

The variables msi, route in kvm_irqchip_send_msi may be uninitialised values in some cases. It's necessary to clear them.

You say the patch is going to 'clear them' but....

Ah, sorry for my bad. And this is not correct fix.

Quoted 21 lines
Quoted 11 lines
---
  accel/kvm/kvm-all.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
accel/kvm/kvm-all.c +0 -0
index eecd8031cf..bd50dc6b80 100644

accel/kvm/kvm-all.c static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg) · line 1917

Quoted 8 lines
accel/kvm/kvm-all.c +1 -3
@@ -1913,10 +1913,8 @@ static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg)
 
  int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
  {
-    struct kvm_msi msi;
-    KVMMSIRoute *route;
-
      if (kvm_direct_msi_allowed) {
+        struct kvm_msi msi;

...but this is still an uninitialized declaration.

Quoted 6 lines

accel/kvm/kvm-all.c static KVMMSIRoute *kvm_lookup_msi_route(KVMState *s, MSIMessage msg) · line 1920

Quoted 3 lines
accel/kvm/kvm-all.c +0 -0
          msi.address_lo = (uint32_t)msg.address;
          msi.address_hi = msg.address >> 32;
          msi.data = le32_to_cpu(msg.data);

I guess the bug you were wanting to fix is that this code only initializes 5 out of 6 struct fields, before calling the ioctl.

Yes, you are right.

Quoted 12 lines

accel/kvm/kvm-all.c int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) · line 1927

Quoted 5 lines
accel/kvm/kvm-all.c +1 -0
@@ -1926,6 +1924,7 @@ int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg)
          return kvm_vm_ioctl(s, KVM_SIGNAL_MSI, &msi);
      }
 
+    KVMMSIRoute *route;

This was initialized correctly before and didn't need moving

accel/kvm/kvm-all.c int kvm_irqchip_send_msi(KVMState *s, MSIMessage msg) · line 1930

Quoted 3 lines
accel/kvm/kvm-all.c +0 -0
      route = kvm_lookup_msi_route(s, msg);
      if (!route) {
          int virq;

Regards,
Daniel