Skip to contentSkip to conversation

fs: fuse: dax: set fc->dax to NULL in fuse_dax_conn_free()

PATCH Applied Hangyu Hua 4 participants 6 messages Nov 16, 2023 – Dec 4, 2023

Subsystems (2)
lore
Share & tools
lore

Hangyu Hua

author
#0 lore raw

PATCH 1/1 fs: fuse: dax: set fc->dax to NULL in fuse_dax_conn_free()

fuse_dax_conn_free() will be called when fuse_fill_super_common() fails after fuse_dax_conn_alloc(). Then deactivate_locked_super() in virtio_fs_get_tree() will call virtio_kill_sb() to release the discarded superblock. This will call fuse_dax_conn_free() again in fuse_conn_put(), resulting in a possible double free.

Fixes 1dd539577c42 ("virtiofs: add a mount option to enable dax")
---
 fs/fuse/dax.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dax.c +1 -0
index 23904a6a9a96..12ef91d170bb 100644
@@ -1222,6 +1222,7 @@ void fuse_dax_conn_free(struct fuse_conn *fc)
 	if (fc->dax) {
 		fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
 		kfree(fc->dax);
+		fc->dax = NULL;
 	}
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Hangyu Hua

fuse_dax_conn_free() will be called when fuse_fill_super_common() fails after fuse_dax_conn_alloc(). Then deactivate_locked_super() in virtio_fs_get_tree() will call virtio_kill_sb() to release the discarded superblock. This will call fuse_dax_conn_free() again in fuse_conn_put(), resulting in a possible double free.

Fixes 1dd539577c42 ("virtiofs: add a mount option to enable dax")
Quoted 16 lines
---
 fs/fuse/dax.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dax.c +1 -0
index 23904a6a9a96..12ef91d170bb 100644
@@ -1222,6 +1222,7 @@ void fuse_dax_conn_free(struct fuse_conn *fc)
 	if (fc->dax) {
 		fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
 		kfree(fc->dax);
+		fc->dax = NULL;
 	}
 }
 
Signature · 2 lines
Thanks,
Jingbo
maintainer
#2 lore raw
In reply to Hangyu Hua
Quoted 18 lines · Hangyu Hua

fuse_dax_conn_free() will be called when fuse_fill_super_common() fails after fuse_dax_conn_alloc(). Then deactivate_locked_super() in virtio_fs_get_tree() will call virtio_kill_sb() to release the discarded superblock. This will call fuse_dax_conn_free() again in fuse_conn_put(), resulting in a possible double free.

Fixes 1dd539577c42 ("virtiofs: add a mount option to enable dax")
---
 fs/fuse/dax.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dax.c +0 -0
index 23904a6a9a96..12ef91d170bb 100644

fs/fuse/dax.c void fuse_dax_conn_free(struct fuse_conn *fc) · line 1225

Quoted 4 lines
fs/fuse/dax.c +1 -0
@@ -1222,6 +1222,7 @@ void fuse_dax_conn_free(struct fuse_conn *fc)
        if (fc->dax) {
                fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
                kfree(fc->dax);
+               fc->dax = NULL;

Is there a reason not to simply remove the fuse_dax_conn_free() call from the cleanup path in fuse_fill_super_common()?

Thanks,
Miklos

Quoted 6 lines
fs/fuse/dax.c +0 -1
        }
 }
 
--

2.34.1

author
#3 lore raw
Quoted 14 lines · Miklos Szeredi
Quoted 13 lines · Hangyu Hua

fuse_dax_conn_free() will be called when fuse_fill_super_common() fails after fuse_dax_conn_alloc(). Then deactivate_locked_super() in virtio_fs_get_tree() will call virtio_kill_sb() to release the discarded superblock. This will call fuse_dax_conn_free() again in fuse_conn_put(), resulting in a possible double free.

Fixes 1dd539577c42 ("virtiofs: add a mount option to enable dax")
---
  fs/fuse/dax.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/dax.c void fuse_dax_conn_free(struct fuse_conn *fc) · line 1225

Quoted 9 lines
fs/fuse/dax.c +1 -0
index 23904a6a9a96..12ef91d170bb 100644
@@ -1222,6 +1222,7 @@ void fuse_dax_conn_free(struct fuse_conn *fc)
         if (fc->dax) {
                 fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
                 kfree(fc->dax);
+               fc->dax = NULL;

Is there a reason not to simply remove the fuse_dax_conn_free() call from the cleanup path in fuse_fill_super_common()?

I think setting fc->dax to NULL keeps the memory allocation and release functions together in fuse_fill_super_common more readable. What do you think?

Thanks,
Hangyu

Quoted 11 lines

Thanks,
Miklos

Quoted 6 lines
fs/fuse/dax.c +0 -1
         }
  }
 
--

2.34.1

Acked-by
#4 lore raw
Quoted 28 lines · Hangyu Hua
Quoted 14 lines · Miklos Szeredi
Quoted 13 lines · Hangyu Hua

fuse_dax_conn_free() will be called when fuse_fill_super_common() fails after fuse_dax_conn_alloc(). Then deactivate_locked_super() in virtio_fs_get_tree() will call virtio_kill_sb() to release the discarded superblock. This will call fuse_dax_conn_free() again in fuse_conn_put(), resulting in a possible double free.

Fixes 1dd539577c42 ("virtiofs: add a mount option to enable dax")
---
  fs/fuse/dax.c | 1 +
  1 file changed, 1 insertion(+)
Quoted 12 lines

fs/fuse/dax.c void fuse_dax_conn_free(struct fuse_conn *fc) · line 1225

Quoted 9 lines
fs/fuse/dax.c +1 -0
index 23904a6a9a96..12ef91d170bb 100644
@@ -1222,6 +1222,7 @@ void fuse_dax_conn_free(struct fuse_conn *fc)
         if (fc->dax) {
                 fuse_free_dax_mem_ranges(&fc->dax->free_ranges);
                 kfree(fc->dax);
+               fc->dax = NULL;

Is there a reason not to simply remove the fuse_dax_conn_free() call from the cleanup path in fuse_fill_super_common()?

Quoted 3 lines

I think setting fc->dax to NULL keeps the memory allocation and release functions together in fuse_fill_super_common more readable. What do you think?

I agree with this. fuse_fill_super_common() calls fuse_dax_conn_alloc() which in-turn initializes fc->dax. If fuse_fill_super_common() fails later after calling fuse_dax_conn_alloc(), then cleanup of fc->dax and other associated stuff in same function makes sense.

As a code reader I would like to know how fc->dax is being freed in case of error and its right there in the error path (err_free_dax:).

I think I set the fc->dax upon initialization. Upon failure I freed the data structures but did not set fc->dax back to NULL.

To me, this patch looks reasonable.

Thanks
Vivek

Quoted 16 lines

Thanks,
Hangyu

Quoted 11 lines

Thanks,
Miklos

Quoted 6 lines
fs/fuse/dax.c +0 -1
         }
  }
 
--

2.34.1

maintainer Applied
#5 lore raw
Quoted 15 lines · Vivek Goyal
Quoted 3 lines · Hangyu Hua

I think setting fc->dax to NULL keeps the memory allocation and release functions together in fuse_fill_super_common more readable. What do you think?

I agree with this. fuse_fill_super_common() calls fuse_dax_conn_alloc() which in-turn initializes fc->dax. If fuse_fill_super_common() fails later after calling fuse_dax_conn_alloc(), then cleanup of fc->dax and other associated stuff in same function makes sense.

As a code reader I would like to know how fc->dax is being freed in case of error and its right there in the error path (err_free_dax:).

Quoted 6 lines

I think I set the fc->dax upon initialization. Upon failure I freed the data structures but did not set fc->dax back to NULL.

To me, this patch looks reasonable.

Applied, thanks.

The patch is clearly correct, so I have not major issue with it. It
just bothers me a little that we have two cleanup instances when we only need one, but I bow to the opinion of the majority.

Thanks,
Miklos