Skip to contentSkip to conversation

accel/kvm: Check ioctl(KVM_SET_USER_MEMORY_REGION) return value

PATCH Applied Philippe Mathieu-Daudé 3 participants 3 messages Feb 21, 2020 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 accel/kvm: Check ioctl(KVM_SET_USER_MEMORY_REGION) return value

kvm_vm_ioctl() can fail, check its return value, and log an error when it failed. This fixes Coverity CID 1412229:

Unchecked return value (CHECKED_RETURN)

check_return: Calling kvm_vm_ioctl without checking return value

Reported-by Coverity (CID 1412229)
Fixes 235e8982ad3 ("support using KVM_MEM_READONLY flag for regions")
---
 accel/kvm/kvm-all.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +11 -1
index c111312dfd..6df3a4d030 100644
@@ -308,13 +308,23 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo
         /* Set the slot size to 0 before setting the slot to the desired
          * value. This is needed based on KVM commit 75d61fbc. */
         mem.memory_size = 0;
-        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        if (ret < 0) {
+            goto err;
+        }
     }
     mem.memory_size = slot->memory_size;
     ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
     slot->old_flags = mem.flags;
+err:
     trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr,
                               mem.memory_size, mem.userspace_addr, ret);
+    if (ret < 0) {
+        error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=%d,"
+                     " start=0x%" PRIx64 ", size=0x%" PRIx64 ": %s",
+                     __func__, mem.slot, slot->start_addr,
+                     (uint64_t)mem.memory_size, strerror(errno));
+    }
     return ret;
 }
 
Signature · 1 line
2.21.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Philippe Mathieu-Daudé

kvm_vm_ioctl() can fail, check its return value, and log an error when it failed. This fixes Coverity CID 1412229:

Unchecked return value (CHECKED_RETURN)

check_return: Calling kvm_vm_ioctl without checking return value

Reported-by Coverity (CID 1412229)
Fixes 235e8982ad3 ("support using KVM_MEM_READONLY flag for regions")
Signature · 1 line
Peter Xu
maintainer Applied
#2 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 34 lines · Philippe Mathieu-Daudé

kvm_vm_ioctl() can fail, check its return value, and log an error when it failed. This fixes Coverity CID 1412229:

Unchecked return value (CHECKED_RETURN)

check_return: Calling kvm_vm_ioctl without checking return value

Reported-by Coverity (CID 1412229)
Fixes 235e8982ad3 ("support using KVM_MEM_READONLY flag for regions")
---
 accel/kvm/kvm-all.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
accel/kvm/kvm-all.c +5 -1
index c111312dfd..6df3a4d030 100644
@@ -308,13 +308,23 @@ static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo
         /* Set the slot size to 0 before setting the slot to the desired
          * value. This is needed based on KVM commit 75d61fbc. */
         mem.memory_size = 0;
-        kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
+        if (ret < 0) {
+            goto err;
+        }
     }
     mem.memory_size = slot->memory_size;
     ret = kvm_vm_ioctl(s, KVM_SET_USER_MEMORY_REGION, &mem);
     slot->old_flags = mem.flags;
+err:
     trace_kvm_set_user_memory(mem.slot, mem.flags, mem.guest_phys_addr,

accel/kvm/kvm-all.c static int kvm_set_user_memory_region(KVMMemoryListener *kml, KVMSlot *slot, boo · line 330

Quoted 10 lines
accel/kvm/kvm-all.c +6 -0
                               mem.memory_size, mem.userspace_addr, ret);
+    if (ret < 0) {
+        error_report("%s: KVM_SET_USER_MEMORY_REGION failed, slot=%d,"
+                     " start=0x%" PRIx64 ", size=0x%" PRIx64 ": %s",
+                     __func__, mem.slot, slot->start_addr,
+                     (uint64_t)mem.memory_size, strerror(errno));
+    }
     return ret;
 }
 

Queued, thanks.

Paolo