Skip to contentSkip to conversation

backup-top: fix a memory leak in bdrv_backup_top_append()

PATCH In review [email protected] 2 participants 3 messages Jan 20, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 backup-top: fix a memory leak in bdrv_backup_top_append()

From: Pan Nengyuan <[email protected]>

top->opaque is aleardy malloced in bdrv_new_open_driver(), and then change the pointer but without freeing it. It will cause a memory leak, the leak stack is as follow:

Direct leak of 24 byte(s) in 1 object(s) allocated from:
#0 0x7ff6f7be4970 (/lib64/libasan.so.5+0xef970) ??:? #1 0x7ff6f723849d (/lib64/libglib-2.0.so.0+0x5249d) ??:?

  #2 0x564c0d44caae (./x86_64-softmmu/qemu-system-x86_64+0x3b40aae)  /mnt/sdb/qemu/block.c:1289
  #3 0x564c0d44dbaf (./x86_64-softmmu/qemu-system-x86_64+0x3b41baf)  /mnt/sdb/qemu/block.c:1359
  #4 0x564c0d71618f (./x86_64-softmmu/qemu-system-x86_64+0x3e0a18f)  /mnt/sdb/qemu/block/backup-top.c:190
  #5 0x564c0d7001be (./x86_64-softmmu/qemu-system-x86_64+0x3df41be)  /mnt/sdb/qemu/block/backup.c:439
  #6 0x564c0c8ebef8 (./x86_64-softmmu/qemu-system-x86_64+0x2fdfef8)  /mnt/sdb/qemu/blockdev.c:3580
  #7 0x564c0c8ed0cb (./x86_64-softmmu/qemu-system-x86_64+0x2fe10cb)  /mnt/sdb/qemu/blockdev.c:3690
  #8 0x564c0c8ed177 (./x86_64-softmmu/qemu-system-x86_64+0x2fe1177)  /mnt/sdb/qemu/blockdev.c:3704
  #9 0x564c0d316388 (./x86_64-softmmu/qemu-system-x86_64+0x3a0a388)  /mnt/sdb/qemu/build/qapi/qapi-commands-block-core.c:439
  #10 0x564c0d7ff7fa (./x86_64-softmmu/qemu-system-x86_64+0x3ef37fa)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
  #11 0x564c0d7ffcb8 (./x86_64-softmmu/qemu-system-x86_64+0x3ef3cb8)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:175 (discriminator 4)
  #12 0x564c0d2704ef (./x86_64-softmmu/qemu-system-x86_64+0x39644ef)  /mnt/sdb/qemu/monitor/qmp.c:145
  #13 0x564c0d2712de (./x86_64-softmmu/qemu-system-x86_64+0x39652de)  /mnt/sdb/qemu/monitor/qmp.c:234 (discriminator 4)
---
 block/backup-top.c | 1 +
 1 file changed, 1 insertion(+)
block/backup-top.c +1 -0
index 818d3f26b4..d565f05520 100644
@@ -196,6 +196,7 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
     }
 
     top->total_sectors = source->total_sectors;
+    g_free(top->opaque);
     top->opaque = state = g_new0(BDRVBackupTopState, 1);
 
     bdrv_ref(target);
Signature · 1 line
2.21.0.windows.1
#1 lore raw

Hi Pan!

Better is to drop extra allocation. Correct patch already posted here:
https://lists.gnu.org/archive/html/qemu-devel/2019-12/msg05067.html

Quoted 41 lines · 20.01.2020 10:47, [email protected] wrote:

From: Pan Nengyuan <[email protected]>

top->opaque is aleardy malloced in bdrv_new_open_driver(), and then change the pointer but without freeing it. It will cause a memory leak, the leak stack is as follow:

Direct leak of 24 byte(s) in 1 object(s) allocated from:

   #0 0x7ff6f7be4970 (/lib64/libasan.so.5+0xef970)  ??:?
   #1 0x7ff6f723849d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
   #2 0x564c0d44caae (./x86_64-softmmu/qemu-system-x86_64+0x3b40aae)  /mnt/sdb/qemu/block.c:1289
   #3 0x564c0d44dbaf (./x86_64-softmmu/qemu-system-x86_64+0x3b41baf)  /mnt/sdb/qemu/block.c:1359
   #4 0x564c0d71618f (./x86_64-softmmu/qemu-system-x86_64+0x3e0a18f)  /mnt/sdb/qemu/block/backup-top.c:190
   #5 0x564c0d7001be (./x86_64-softmmu/qemu-system-x86_64+0x3df41be)  /mnt/sdb/qemu/block/backup.c:439
   #6 0x564c0c8ebef8 (./x86_64-softmmu/qemu-system-x86_64+0x2fdfef8)  /mnt/sdb/qemu/blockdev.c:3580
   #7 0x564c0c8ed0cb (./x86_64-softmmu/qemu-system-x86_64+0x2fe10cb)  /mnt/sdb/qemu/blockdev.c:3690
   #8 0x564c0c8ed177 (./x86_64-softmmu/qemu-system-x86_64+0x2fe1177)  /mnt/sdb/qemu/blockdev.c:3704
   #9 0x564c0d316388 (./x86_64-softmmu/qemu-system-x86_64+0x3a0a388)  /mnt/sdb/qemu/build/qapi/qapi-commands-block-core.c:439
   #10 0x564c0d7ff7fa (./x86_64-softmmu/qemu-system-x86_64+0x3ef37fa)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
   #11 0x564c0d7ffcb8 (./x86_64-softmmu/qemu-system-x86_64+0x3ef3cb8)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:175 (discriminator 4)
   #12 0x564c0d2704ef (./x86_64-softmmu/qemu-system-x86_64+0x39644ef)  /mnt/sdb/qemu/monitor/qmp.c:145
   #13 0x564c0d2712de (./x86_64-softmmu/qemu-system-x86_64+0x39652de)  /mnt/sdb/qemu/monitor/qmp.c:234 (discriminator 4)
---
  block/backup-top.c | 1 +
  1 file changed, 1 insertion(+)
block/backup-top.c +1 -0
index 818d3f26b4..d565f05520 100644
@@ -196,6 +196,7 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
      }
 
      top->total_sectors = source->total_sectors;
+    g_free(top->opaque);
      top->opaque = state = g_new0(BDRVBackupTopState, 1);
 
      bdrv_ref(target);
Signature · 2 lines
Best regards,
Vladimir
#2 lore raw
Quoted 5 lines · Vladimir Sementsov-Ogievskiy

Hi Pan!

Better is to drop extra allocation. Correct patch already posted here:
https://lists.gnu.org/archive/html/qemu-devel/2019-12/msg05067.html

Yes, it's better.

Thanks.

Quoted 44 lines
Quoted 41 lines · 20.01.2020 10:47, [email protected] wrote:

From: Pan Nengyuan <[email protected]>

top->opaque is aleardy malloced in bdrv_new_open_driver(), and then change the pointer but without freeing it. It will cause a memory leak, the leak stack is as follow:

Direct leak of 24 byte(s) in 1 object(s) allocated from:

   #0 0x7ff6f7be4970 (/lib64/libasan.so.5+0xef970)  ??:?
   #1 0x7ff6f723849d (/lib64/libglib-2.0.so.0+0x5249d)  ??:?
   #2 0x564c0d44caae (./x86_64-softmmu/qemu-system-x86_64+0x3b40aae)  /mnt/sdb/qemu/block.c:1289
   #3 0x564c0d44dbaf (./x86_64-softmmu/qemu-system-x86_64+0x3b41baf)  /mnt/sdb/qemu/block.c:1359
   #4 0x564c0d71618f (./x86_64-softmmu/qemu-system-x86_64+0x3e0a18f)  /mnt/sdb/qemu/block/backup-top.c:190
   #5 0x564c0d7001be (./x86_64-softmmu/qemu-system-x86_64+0x3df41be)  /mnt/sdb/qemu/block/backup.c:439
   #6 0x564c0c8ebef8 (./x86_64-softmmu/qemu-system-x86_64+0x2fdfef8)  /mnt/sdb/qemu/blockdev.c:3580
   #7 0x564c0c8ed0cb (./x86_64-softmmu/qemu-system-x86_64+0x2fe10cb)  /mnt/sdb/qemu/blockdev.c:3690
   #8 0x564c0c8ed177 (./x86_64-softmmu/qemu-system-x86_64+0x2fe1177)  /mnt/sdb/qemu/blockdev.c:3704
   #9 0x564c0d316388 (./x86_64-softmmu/qemu-system-x86_64+0x3a0a388)  /mnt/sdb/qemu/build/qapi/qapi-commands-block-core.c:439
   #10 0x564c0d7ff7fa (./x86_64-softmmu/qemu-system-x86_64+0x3ef37fa)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:132
   #11 0x564c0d7ffcb8 (./x86_64-softmmu/qemu-system-x86_64+0x3ef3cb8)  /mnt/sdb/qemu/qapi/qmp-dispatch.c:175 (discriminator 4)
   #12 0x564c0d2704ef (./x86_64-softmmu/qemu-system-x86_64+0x39644ef)  /mnt/sdb/qemu/monitor/qmp.c:145
   #13 0x564c0d2712de (./x86_64-softmmu/qemu-system-x86_64+0x39652de)  /mnt/sdb/qemu/monitor/qmp.c:234 (discriminator 4)
---
  block/backup-top.c | 1 +
  1 file changed, 1 insertion(+)
block/backup-top.c +1 -0
index 818d3f26b4..d565f05520 100644
@@ -196,6 +196,7 @@ BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
      }
 
      top->total_sectors = source->total_sectors;
+    g_free(top->opaque);
      top->opaque = state = g_new0(BDRVBackupTopState, 1);
 
      bdrv_ref(target);