Skip to contentSkip to conversation

vhost-scsi, ivshmem, savevm fixes for Coverity issues

PATCH Superseded Stefan Hajnoczi 6 participants 11 messages May 30, 2013 – Jun 12, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover vhost-scsi, ivshmem, savevm fixes for Coverity issues

The following fixes address NULL pointer dereferences and resource leaks spotted by Coverity.

Stefan Hajnoczi (3):
vhost-scsi: fix k->set_guest_notifiers() NULL dereference
ivshmem: add missing error exit(2)
savevm: avoid leaking popen(3) file pointer

 hw/misc/ivshmem.c    | 1 +
 hw/scsi/vhost-scsi.c | 2 +-
 savevm.c             | 1 +
 3 files changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH 1/3 vhost-scsi: fix k->set_guest_notifiers() NULL dereference

Coverity picked up a copy-paste bug. In vhost_scsi_start() we check for !k->set_guest_notifiers and error out. The check probably got copied but instead of erroring we actually use the function pointer!

---
 hw/scsi/vhost-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/vhost-scsi.c +1 -1
index d7a1c33..785e93f 100644
@@ -123,7 +123,7 @@ static void vhost_scsi_stop(VHostSCSI *s)
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
     int ret = 0;
 
-    if (!k->set_guest_notifiers) {
+    if (k->set_guest_notifiers) {
         ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
         if (ret < 0) {
                 error_report("vhost guest notifier cleanup failed: %d\n", ret);
Signature · 1 line
1.8.1.4
#6 lore raw

PATCH 3/3 savevm: avoid leaking popen(3) file pointer

In reply to Stefan Hajnoczi

I'm not sure why we check the mode only after invoking popen(3) but we need to close the file pointer.

Spotted by Coverity.

---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)
savevm.c +1 -0
index 31dcce9..75cc72e 100644
@@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);
         return NULL;
     }
 
Signature · 1 line
1.8.1.4
#4 lore raw

PATCH 2/3 ivshmem: add missing error exit(2)

In reply to Stefan Hajnoczi

If the user fails to specify 'chardev' or 'shm' then we cannot continue. Exit right away so that we don't invoke shm_open(3) with a NULL pointer.

It would be nice to replace exit(1) with error returns in the PCI device .init() function, but leave that for another patch since exit(1) is currently used elsewhere.

Spotted by Coverity.

---
 hw/misc/ivshmem.c | 1 +
 1 file changed, 1 insertion(+)
hw/misc/ivshmem.c +1 -0
index a19a6d6..5658f73 100644
@@ -735,6 +735,7 @@ static int pci_ivshmem_init(PCIDevice *dev)
 
         if (s->shmobj == NULL) {
             fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
+            exit(1);
         }
 
         IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);
Signature · 1 line
1.8.1.4
#7 lore raw

Re: [Qemu-devel] [PATCH 3/3] savevm: avoid leaking popen(3) file pointer

In reply to Stefan Hajnoczi
Quoted 17 lines · Stefan Hajnoczi

I'm not sure why we check the mode only after invoking popen(3) but we need to close the file pointer.

Spotted by Coverity.

---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)
savevm.c +0 -0
index 31dcce9..75cc72e 100644

savevm.c QEMUFile *qemu_popen_cmd(const char *command, const char *mode) · line 332

Quoted 4 lines
savevm.c +1 -0
@@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);

You MUST use pclose() (not fclose) on any FILE obtained by popen(), to avoid resource leaks.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#8 lore raw

Re: [Qemu-devel] [PATCH 3/3] savevm: avoid leaking popen(3) file pointer

Quoted 19 lines · Eric Blake
Quoted 18 lines · Stefan Hajnoczi

I'm not sure why we check the mode only after invoking popen(3) but we need to close the file pointer.

Spotted by Coverity.

---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)
savevm.c +0 -0
index 31dcce9..75cc72e 100644
@@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
Quoted 6 lines

savevm.c QEMUFile *qemu_popen_cmd(const char *command, const char *mode) · line 332

Quoted 3 lines
savevm.c +1 -0
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);

You MUST use pclose() (not fclose) on any FILE obtained by popen(), to avoid resource leaks.

Thanks, I didn't know that. Should have checked the popen(3) man page.

Will fix.

Stefan

#5 lore raw

Re: [Qemu-devel] [PATCH 2/3] ivshmem: add missing error exit(2)

In reply to Stefan Hajnoczi
Quoted 21 lines · Stefan Hajnoczi

If the user fails to specify 'chardev' or 'shm' then we cannot continue. Exit right away so that we don't invoke shm_open(3) with a NULL pointer.

It would be nice to replace exit(1) with error returns in the PCI device .init() function, but leave that for another patch since exit(1) is currently used elsewhere.

Spotted by Coverity.

---
 hw/misc/ivshmem.c | 1 +
 1 file changed, 1 insertion(+)
hw/misc/ivshmem.c +0 -0
index a19a6d6..5658f73 100644

hw/misc/ivshmem.c static int pci_ivshmem_init(PCIDevice *dev) · line 741

Quoted 7 lines
hw/misc/ivshmem.c +1 -0
@@ -735,6 +735,7 @@ static int pci_ivshmem_init(PCIDevice *dev)
 
         if (s->shmobj == NULL) {
             fprintf(stderr, "Must specify 'chardev' or 'shm' to ivshmem\n");
+            exit(1);
         }
 
         IVSHMEM_DPRINTF("using shm_open (shm object = %s)\n", s->shmobj);

Botching a hotplug of this device kills the VM. Nasty trap, but no worse than before.

#2 lore raw

Re: [Qemu-devel] [PATCH 1/3] vhost-scsi: fix k->set_guest_notifiers() NULL dereference

In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

Coverity picked up a copy-paste bug. In vhost_scsi_start() we check for !k->set_guest_notifiers and error out. The check probably got copied but instead of erroring we actually use the function pointer!

Hi Paolo,

Looking to pick this up for 1.5.1 along with a few other goodies in your scsi branch:

iscsi: reorganize iscsi_readcapacity_sync
iscsi: simplify freeing of tasks
scsi-disk: scsi-block device for scsi pass-through should not be remo…
scsi-generic: check the return value of bdrv_aio_ioctl in execute_com…
scsi-generic: fix sign extension of READ CAPACITY(10) data
scsi: reset cdrom tray statuses on scsi_disk_reset

Freeze for 1.5.1 is planned for June 19. Willing to pluck from maintainer branches for the more important ones but would prefer upstream if you can send a PULL for these.

Quoted 21 lines
---
 hw/scsi/vhost-scsi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/scsi/vhost-scsi.c +1 -1
index d7a1c33..785e93f 100644
@@ -123,7 +123,7 @@ static void vhost_scsi_stop(VHostSCSI *s)
     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
     int ret = 0;
 
-    if (!k->set_guest_notifiers) {
+    if (k->set_guest_notifiers) {
         ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
         if (ret < 0) {
                 error_report("vhost guest notifier cleanup failed: %d\n", ret);
Signature · 1 line
1.8.1.4
#9 lore raw

Re: [Qemu-devel] [Qemu-stable] [PATCH 3/3] savevm: avoid leaking popen(3) file pointer

In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi

I'm not sure why we check the mode only after invoking popen(3) but we need to close the file pointer.

Spotted by Coverity.

Ping, looking to get this in for 1.5.1

Quoted 20 lines
---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)
savevm.c +1 -0
index 31dcce9..75cc72e 100644
@@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);
         return NULL;
     }
 
Signature · 1 line
1.8.1.4
#10 lore raw

Re: [Qemu-devel] [Qemu-stable] [PATCH 3/3] savevm: avoid leaking popen(3) file pointer

Quoted 22 lines · mdroth
Quoted 8 lines · Stefan Hajnoczi

I'm not sure why we check the mode only after invoking popen(3) but we need to close the file pointer.

Spotted by Coverity.

Ping, looking to get this in for 1.5.1

Quoted 10 lines
---
 savevm.c | 1 +
 1 file changed, 1 insertion(+)
savevm.c +0 -0
index 31dcce9..75cc72e 100644
@@ -329,6 +329,7 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
 
Quoted 5 lines

savevm.c QEMUFile *qemu_popen_cmd(const char *command, const char *mode) · line 334

Quoted 5 lines
savevm.c +1 -0
     if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
+        fclose(stdio_file);
         return NULL;
     }

The v2 I sent had Patch 1 picked up by Paolo and Patch 2 is not yet merged by anyone.

Stefan

#3 lore raw

Re: [Qemu-devel] [PATCH 1/3] vhost-scsi: fix k->set_guest_notifiers() NULL dereference

In reply to mdroth

Il 11/06/2013 18:58, mdroth ha scritto:

Quoted 10 lines

iscsi: reorganize iscsi_readcapacity_sync
iscsi: simplify freeing of tasks
scsi-disk: scsi-block device for scsi pass-through should not be remo…
scsi-generic: check the return value of bdrv_aio_ioctl in execute_com…
scsi-generic: fix sign extension of READ CAPACITY(10) data
scsi: reset cdrom tray statuses on scsi_disk_reset

Freeze for 1.5.1 is planned for June 19. Willing to pluck from maintainer branches for the more important ones but would prefer upstream if you can send a PULL for these.

Yes, I'll be back from holiday on Monday and will send pull requests then.

Paolo