Skip to contentSkip to conversation

xen-disk: use g_malloc0 to fix build

PATCH Superseded Olaf Hering 4 participants 9 messages Jul 28, 2017

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen-disk: use g_malloc0 to fix build

g_malloc0_n is available since glib-2.24. To allow build with older glib versions use the generic g_malloc0, which is already used in many other places in the code.

Fixes commit 3284fad728 ("xen-disk: add support for multi-page shared rings")

---
 hw/block/xen_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen_disk.c +1 -1
index d42ed7070d..71deec17b0 100644
@@ -1232,7 +1232,7 @@ static int blk_connect(struct XenDevice *xendev)
         return -1;
     }
 
-    domids = g_malloc0_n(blkdev->nr_ring_ref, sizeof(uint32_t));
+    domids = g_malloc0(blkdev->nr_ring_ref * sizeof(uint32_t));
     for (i = 0; i < blkdev->nr_ring_ref; i++) {
         domids[i] = blkdev->xendev.dom;
     }

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#1 lore raw
Quoted 16 lines · Olaf Hering

g_malloc0_n is available since glib-2.24. To allow build with older glib versions use the generic g_malloc0, which is already used in many other places in the code.

Fixes commit 3284fad728 ("xen-disk: add support for multi-page shared rings")

---
 hw/block/xen_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen_disk.c +0 -0
index d42ed7070d..71deec17b0 100644

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 1235

Quoted 5 lines
hw/block/xen_disk.c +1 -1
@@ -1232,7 +1232,7 @@ static int blk_connect(struct XenDevice *xendev)
         return -1;
     }
 
-    domids = g_malloc0_n(blkdev->nr_ring_ref, sizeof(uint32_t));
+    domids = g_malloc0(blkdev->nr_ring_ref * sizeof(uint32_t));

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

domids = g_new0(blkdev->nr_ring_ref, uint32_t)

which preserves the safety of g_malloc0_n?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 2 lines · Eric Blake

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

What could go wrong?
qemu will die either way, I think.

Olaf

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#7 lore raw
In reply to Eric Blake
Quoted 20 lines · Eric Blake
Quoted 19 lines · Olaf Hering

g_malloc0_n is available since glib-2.24. To allow build with older glib versions use the generic g_malloc0, which is already used in many other places in the code.

Fixes commit 3284fad728 ("xen-disk: add support for multi-page shared rings")

---
 hw/block/xen_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen_disk.c +0 -0
index d42ed7070d..71deec17b0 100644
@@ -1232,7 +1232,7 @@ static int blk_connect(struct XenDevice *xendev)
         return -1;
     }
 
Quoted 7 lines

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 1235

Quoted 2 lines
hw/block/xen_disk.c +1 -1
-    domids = g_malloc0_n(blkdev->nr_ring_ref, sizeof(uint32_t));
+    domids = g_malloc0(blkdev->nr_ring_ref * sizeof(uint32_t));

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

domids = g_new0(blkdev->nr_ring_ref, uint32_t)

You mean g_new0(uint32_t, blkdev->nr_ring_ref) but yeah, g_new0 is
better than g_malloc0 pretty much every time.

Regards,
Daniel

Signature · 8 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 :|

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#8 lore raw
In reply to Eric Blake
Quoted 20 lines · Eric Blake
Quoted 19 lines · Olaf Hering

g_malloc0_n is available since glib-2.24. To allow build with older glib versions use the generic g_malloc0, which is already used in many other places in the code.

Fixes commit 3284fad728 ("xen-disk: add support for multi-page shared rings")

---
 hw/block/xen_disk.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/xen_disk.c +0 -0
index d42ed7070d..71deec17b0 100644
@@ -1232,7 +1232,7 @@ static int blk_connect(struct XenDevice *xendev)
         return -1;
     }
 
Quoted 7 lines

hw/block/xen_disk.c static int blk_connect(struct XenDevice *xendev) · line 1235

Quoted 2 lines
hw/block/xen_disk.c +1 -1
-    domids = g_malloc0_n(blkdev->nr_ring_ref, sizeof(uint32_t));
+    domids = g_malloc0(blkdev->nr_ring_ref * sizeof(uint32_t));

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

domids = g_new0(blkdev->nr_ring_ref, uint32_t)

You mean g_new0(uint32_t, blkdev->nr_ring_ref) but yeah, g_new0 is
better than g_malloc0 pretty much every time.

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 :|
#3 lore raw
In reply to Olaf Hering
Quoted 7 lines · Olaf Hering
Quoted 2 lines · Eric Blake

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

What could go wrong?
qemu will die either way, I think.

Dying immediately due to provable multiplication overflow is MUCH better than successfully allocating too-little and then having who-knows-what go wrong down the road because you didn't check for overflow. The latter can sometimes be exploited into CVEs. And maybe you can't overflow, but having to do a non-local audit to prove that is more time spent than just using the right interface from the get-go.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#4 lore raw
In reply to Olaf Hering
Quoted 7 lines · Olaf Hering
Quoted 2 lines · Eric Blake

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

What could go wrong?
qemu will die either way, I think.

Dying immediately due to provable multiplication overflow is MUCH better than successfully allocating too-little and then having who-knows-what go wrong down the road because you didn't check for overflow. The latter can sometimes be exploited into CVEs. And maybe you can't overflow, but having to do a non-local audit to prove that is more time spent than just using the right interface from the get-go.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw
In reply to Olaf Hering
Quoted 7 lines · Olaf Hering
Quoted 2 lines · Eric Blake

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

What could go wrong?
qemu will die either way, I think.

An overflow in the size argument of malloc(), realloc(), etc. is a heap overrun waiting to happen.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#6 lore raw
In reply to Olaf Hering
Quoted 7 lines · Olaf Hering
Quoted 2 lines · Eric Blake

This version is prone to multiplication overflow (well, maybe not, but you have to audit for that). Wouldn't it be better to use:

What could go wrong?
qemu will die either way, I think.

An overflow in the size argument of malloc(), realloc(), etc. is a heap overrun waiting to happen.