All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vivek Goyal <vgoyal@redhat.com>
To: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
Cc: linux-fsdevel@vger.kernel.org,
	virtualization@lists.linux-foundation.org,
	linux-kernel@vger.kernel.org, stefanha@redhat.com,
	miklos@szeredi.hu
Subject: Re: [PATCH] fuse: Fix a potential double free in virtio_fs_get_tree
Date: Tue, 23 Mar 2021 13:10:03 -0400	[thread overview]
Message-ID: <20210323171003.GC483930@redhat.com> (raw)
In-Reply-To: <20210323051831.13575-1-lyl2019@mail.ustc.edu.cn>

On Mon, Mar 22, 2021 at 10:18:31PM -0700, Lv Yunlong wrote:
> In virtio_fs_get_tree, fm is allocated by kzalloc() and
> assigned to fsc->s_fs_info by fsc->s_fs_info=fm statement.
> If the kzalloc() failed, it will goto err directly, so that
> fsc->s_fs_info must be non-NULL and fm will be freed.

sget_fc() will either consume fsc->s_fs_info in case a new super
block is allocated and set fsc->s_fs_info. In that case we don't
free fc or fm.

Or, sget_fc() will return with fsc->s_fs_info set in case we already
found a super block. In that case we need to free fc and fm.

In case of error from sget_fc(), fc/fm need to be freed first and
then error needs to be returned to caller.

        if (IS_ERR(sb))
                return PTR_ERR(sb);


If we allocated a new super block in sget_fc(), then next step is
to initialize it.

        if (!sb->s_root) {
                err = virtio_fs_fill_super(sb, fsc);
	}

If we run into errors here, then fc/fm need to be freed.

So current code looks fine to me.

Vivek

> 
> But later fm is freed again when virtio_fs_fill_super() fialed.
> I think the statement if (fsc->s_fs_info) {kfree(fm);} is
> misplaced.
> 
> My patch puts this statement in the correct palce to avoid
> double free.
> 
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> ---
>  fs/fuse/virtio_fs.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
> index 8868ac31a3c0..727cf436828f 100644
> --- a/fs/fuse/virtio_fs.c
> +++ b/fs/fuse/virtio_fs.c
> @@ -1437,10 +1437,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>  
>  	fsc->s_fs_info = fm;
>  	sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc);
> -	if (fsc->s_fs_info) {
> -		fuse_conn_put(fc);
> -		kfree(fm);
> -	}
> +
>  	if (IS_ERR(sb))
>  		return PTR_ERR(sb);
>  
> @@ -1457,6 +1454,11 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>  		sb->s_flags |= SB_ACTIVE;
>  	}
>  
> +	if (fsc->s_fs_info) {
> +		fuse_conn_put(fc);
> +		kfree(fm);
> +	}
> +
>  	WARN_ON(fsc->root);
>  	fsc->root = dget(sb->s_root);
>  	return 0;
> -- 
> 2.25.1
> 
> 

_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

WARNING: multiple messages have this Message-ID (diff)
From: Vivek Goyal <vgoyal@redhat.com>
To: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
Cc: stefanha@redhat.com, miklos@szeredi.hu,
	virtualization@lists.linux-foundation.org,
	linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] fuse: Fix a potential double free in virtio_fs_get_tree
Date: Tue, 23 Mar 2021 13:10:03 -0400	[thread overview]
Message-ID: <20210323171003.GC483930@redhat.com> (raw)
In-Reply-To: <20210323051831.13575-1-lyl2019@mail.ustc.edu.cn>

On Mon, Mar 22, 2021 at 10:18:31PM -0700, Lv Yunlong wrote:
> In virtio_fs_get_tree, fm is allocated by kzalloc() and
> assigned to fsc->s_fs_info by fsc->s_fs_info=fm statement.
> If the kzalloc() failed, it will goto err directly, so that
> fsc->s_fs_info must be non-NULL and fm will be freed.

sget_fc() will either consume fsc->s_fs_info in case a new super
block is allocated and set fsc->s_fs_info. In that case we don't
free fc or fm.

Or, sget_fc() will return with fsc->s_fs_info set in case we already
found a super block. In that case we need to free fc and fm.

In case of error from sget_fc(), fc/fm need to be freed first and
then error needs to be returned to caller.

        if (IS_ERR(sb))
                return PTR_ERR(sb);


If we allocated a new super block in sget_fc(), then next step is
to initialize it.

        if (!sb->s_root) {
                err = virtio_fs_fill_super(sb, fsc);
	}

If we run into errors here, then fc/fm need to be freed.

So current code looks fine to me.

Vivek

> 
> But later fm is freed again when virtio_fs_fill_super() fialed.
> I think the statement if (fsc->s_fs_info) {kfree(fm);} is
> misplaced.
> 
> My patch puts this statement in the correct palce to avoid
> double free.
> 
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> ---
>  fs/fuse/virtio_fs.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
> index 8868ac31a3c0..727cf436828f 100644
> --- a/fs/fuse/virtio_fs.c
> +++ b/fs/fuse/virtio_fs.c
> @@ -1437,10 +1437,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>  
>  	fsc->s_fs_info = fm;
>  	sb = sget_fc(fsc, virtio_fs_test_super, set_anon_super_fc);
> -	if (fsc->s_fs_info) {
> -		fuse_conn_put(fc);
> -		kfree(fm);
> -	}
> +
>  	if (IS_ERR(sb))
>  		return PTR_ERR(sb);
>  
> @@ -1457,6 +1454,11 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>  		sb->s_flags |= SB_ACTIVE;
>  	}
>  
> +	if (fsc->s_fs_info) {
> +		fuse_conn_put(fc);
> +		kfree(fm);
> +	}
> +
>  	WARN_ON(fsc->root);
>  	fsc->root = dget(sb->s_root);
>  	return 0;
> -- 
> 2.25.1
> 
> 


  parent reply	other threads:[~2021-03-23 17:10 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-23  5:18 [PATCH] fuse: Fix a potential double free in virtio_fs_get_tree Lv Yunlong
2021-03-23 14:33 ` Connor Kuehl
2021-03-23 14:33   ` Connor Kuehl
2021-03-23 17:10 ` Vivek Goyal [this message]
2021-03-23 17:10   ` Vivek Goyal
2021-03-25  5:04   ` lyl2019

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210323171003.GC483930@redhat.com \
    --to=vgoyal@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lyl2019@mail.ustc.edu.cn \
    --cc=miklos@szeredi.hu \
    --cc=stefanha@redhat.com \
    --cc=virtualization@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.