[PATCH 3/4] block/vvfat: Fix leak of mapping_t::path

Philippe Mathieu-Daudé posted 4 patches 4 years, 9 months ago
[PATCH 3/4] block/vvfat: Fix leak of mapping_t::path
Posted by Philippe Mathieu-Daudé 4 years, 9 months ago
read_directory() keeps pointers to alloc'ed data in path ...:

 743 static int read_directory(BDRVVVFATState* s, int mapping_index)
 744 {
 ...
 792         buffer = g_malloc(length);
 ...
 828         /* create mapping for this file */
 829         if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
 830             s->current_mapping = array_get_next(&(s->mapping));
 ...
 847             s->current_mapping->path=buffer;

... but these pointers are never free'd. Free them in vvfat_close(),
to fix (QEMU built with --enable-sanitizers):

  Direct leak of 148 byte(s) in 6 object(s) allocated from:
    #0 0x55d7a363773f in malloc (qemu-system-x86_64+0x1dab73f)
    #1 0x7f55c6447958 in g_malloc (/lib64/libglib-2.0.so.0+0x58958)
    #2 0x55d7a5e17679 in init_directories block/vvfat.c:962:16
    #3 0x55d7a5e1251e in vvfat_open block/vvfat.c:1255:9
    #4 0x55d7a5a5363f in bdrv_open_driver block.c:1526:15
    #5 0x55d7a5a9d369 in bdrv_open_common block.c:1802:11
    #6 0x55d7a5a609f1 in bdrv_open_inherit block.c:3444:11
    #7 0x55d7a5a65411 in bdrv_open_child_bs block.c:3079:10
    #8 0x55d7a5a60079 in bdrv_open_inherit block.c:3391:19
    #9 0x55d7a5a65da3 in bdrv_open block.c:3537:12
    #10 0x55d7a5b33f6a in blk_new_open block/block-backend.c:421:10
    #11 0x55d7a5a0a33e in blockdev_init blockdev.c:610:15
    #12 0x55d7a5a088e7 in drive_new blockdev.c:994:11
    #13 0x55d7a51b10c4 in drive_init_func softmmu/vl.c:636:12
    #14 0x55d7a620e148 in qemu_opts_foreach util/qemu-option.c:1167:14
    #15 0x55d7a51b0e20 in configure_blockdev softmmu/vl.c:695:9
    #16 0x55d7a51a70b5 in qemu_create_early_backends softmmu/vl.c:1895:5
    #17 0x55d7a519bf87 in qemu_init softmmu/vl.c:3551:5
    #18 0x55d7a366f619 in main softmmu/main.c:49:5

Fixes: a046433a161 ("Major overhaul of the virtual FAT driver for read/write support")
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 block/vvfat.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/block/vvfat.c b/block/vvfat.c
index 2cc21787600..c193a816646 100644
--- a/block/vvfat.c
+++ b/block/vvfat.c
@@ -3228,6 +3228,11 @@ static void vvfat_close(BlockDriverState *bs)
 {
     BDRVVVFATState *s = bs->opaque;
 
+    for (unsigned j = 0; j < s->mapping.next; j++) {
+        mapping_t *mapping = array_get(&(s->mapping), j);
+
+        g_free(mapping->path);
+    }
     vvfat_close_current_file(s);
     array_free(&(s->fat));
     array_free(&(s->directory));
-- 
2.26.3

Re: [PATCH 3/4] block/vvfat: Fix leak of mapping_t::path
Posted by Max Reitz 4 years, 9 months ago
On 30.04.21 18:25, Philippe Mathieu-Daudé wrote:
> read_directory() keeps pointers to alloc'ed data in path ...:
> 
>   743 static int read_directory(BDRVVVFATState* s, int mapping_index)
>   744 {
>   ...
>   792         buffer = g_malloc(length);
>   ...
>   828         /* create mapping for this file */
>   829         if(!is_dot && !is_dotdot && (S_ISDIR(st.st_mode) || st.st_size)) {
>   830             s->current_mapping = array_get_next(&(s->mapping));
>   ...
>   847             s->current_mapping->path=buffer;
> 
> ... but these pointers are never free'd.

“Never” is not quite right, they are freed in some places: Namely in 
remove_mapping(), and in handle_renames_and_mkdirs().

Speaking of the latter, which does some commit_t handling – should those 
paths also be freed?  (i.e. the ones in commit_t, in s->commits)

> Free them in vvfat_close(),
> to fix (QEMU built with --enable-sanitizers):
> 
>    Direct leak of 148 byte(s) in 6 object(s) allocated from:
>      #0 0x55d7a363773f in malloc (qemu-system-x86_64+0x1dab73f)
>      #1 0x7f55c6447958 in g_malloc (/lib64/libglib-2.0.so.0+0x58958)
>      #2 0x55d7a5e17679 in init_directories block/vvfat.c:962:16
>      #3 0x55d7a5e1251e in vvfat_open block/vvfat.c:1255:9
>      #4 0x55d7a5a5363f in bdrv_open_driver block.c:1526:15
>      #5 0x55d7a5a9d369 in bdrv_open_common block.c:1802:11
>      #6 0x55d7a5a609f1 in bdrv_open_inherit block.c:3444:11
>      #7 0x55d7a5a65411 in bdrv_open_child_bs block.c:3079:10
>      #8 0x55d7a5a60079 in bdrv_open_inherit block.c:3391:19
>      #9 0x55d7a5a65da3 in bdrv_open block.c:3537:12
>      #10 0x55d7a5b33f6a in blk_new_open block/block-backend.c:421:10
>      #11 0x55d7a5a0a33e in blockdev_init blockdev.c:610:15
>      #12 0x55d7a5a088e7 in drive_new blockdev.c:994:11
>      #13 0x55d7a51b10c4 in drive_init_func softmmu/vl.c:636:12
>      #14 0x55d7a620e148 in qemu_opts_foreach util/qemu-option.c:1167:14
>      #15 0x55d7a51b0e20 in configure_blockdev softmmu/vl.c:695:9
>      #16 0x55d7a51a70b5 in qemu_create_early_backends softmmu/vl.c:1895:5
>      #17 0x55d7a519bf87 in qemu_init softmmu/vl.c:3551:5
>      #18 0x55d7a366f619 in main softmmu/main.c:49:5
> 
> Fixes: a046433a161 ("Major overhaul of the virtual FAT driver for read/write support")
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
>   block/vvfat.c | 5 +++++
>   1 file changed, 5 insertions(+)

First:

Reviewed-by: Max Reitz <mreitz@redhat.com>

Second:

In commit_mappings(), there is this line:

next_mapping->path = mapping->path;

That looks strange.  Should that be a g_strdup(), or am I missing the 
place where only one of them can really exist?

But, well, all of the vvfat code is just strange to me.

> diff --git a/block/vvfat.c b/block/vvfat.c
> index 2cc21787600..c193a816646 100644
> --- a/block/vvfat.c
> +++ b/block/vvfat.c
> @@ -3228,6 +3228,11 @@ static void vvfat_close(BlockDriverState *bs)
>   {
>       BDRVVVFATState *s = bs->opaque;
>   
> +    for (unsigned j = 0; j < s->mapping.next; j++) {
> +        mapping_t *mapping = array_get(&(s->mapping), j);
> +
> +        g_free(mapping->path);
> +    }
>       vvfat_close_current_file(s);
>       array_free(&(s->fat));
>       array_free(&(s->directory));
>