public inbox for linux-bcachefs@vger.kernel.org
 help / color / mirror / Atom feed
From: Brian Foster <bfoster@redhat.com>
To: Su Yue <glass.su@suse.com>
Cc: linux-bcachefs@vger.kernel.org, l@damenly.org
Subject: Re: [PATCH v3] bcachefs: fix memleak in bch2_split_devs
Date: Mon, 8 Jan 2024 10:25:15 -0500	[thread overview]
Message-ID: <ZZwT22xOjpeDDWlr@bfoster> (raw)
In-Reply-To: <20240108151108.1115630-1-glass.su@suse.com>

On Mon, Jan 08, 2024 at 11:11:08PM +0800, Su Yue wrote:
> The pointer dev_name can be modified by strseq(),
> then causes the memleak:
> 
> unreferenced object 0xffff9d08a2916c80 (size 32):
>   comm "mount.bcachefs", pid 9090, jiffies 4295856224 (age 17.564s)
>   hex dump (first 32 bytes):
>     2f 64 65 76 2f 6d 61 70 70 65 72 2f 74 65 73 74  /dev/mapper/test
>     2d 30 00 00 00 00 00 00 00 00 00 00 00 00 00 00  -0..............
>   backtrace:
>     [<00000000c5d3be7d>] __kmem_cache_alloc_node+0x1f3/0x2c0
>     [<0000000052215d26>] __kmalloc_node_track_caller+0x51/0x150
>     [<0000000069fea956>] kstrdup+0x32/0x60
>     [<000000000877fcf1>] bch2_split_devs+0x3f/0x150 [bcachefs]
>     [<000000007ee93204>] bch2_mount+0xcb/0x640 [bcachefs]
>     [<000000002dd1e04b>] legacy_get_tree+0x30/0x60
>     [<000000006afc31d3>] vfs_get_tree+0x28/0xf0
>     [<000000007b0c538e>] path_mount+0x475/0xb60
>     [<0000000092de5882>] __x64_sys_mount+0x105/0x140
>     [<0000000054fc05d8>] do_syscall_64+0x42/0xf0
>     [<00000000df584910>] entry_SYSCALL_64_after_hwframe+0x6e/0x76
> 
> Fix it by copy pointer dev_name at beginning and free the copied
> pointer at end.
> 
> Signed-off-by: Su Yue <glass.su@suse.com>
> ---
> changelog:
> v3:
>     Add the missing the memleak report.
> v2:
>     Remove the unnecessary assignment.    
> ---

LGTM, thanks for the tweak:

Reviewed-by: Brian Foster <bfoster@redhat.com>

Now that I think about it, I suspect Kent might want to move the *s
declaration to precede the while loop, but that's a separate topic. ;)

Brian

>  fs/bcachefs/util.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/fs/bcachefs/util.c b/fs/bcachefs/util.c
> index c2ef7cddaa4f..f927c8a19e24 100644
> --- a/fs/bcachefs/util.c
> +++ b/fs/bcachefs/util.c
> @@ -1186,7 +1186,9 @@ int bch2_split_devs(const char *_dev_name, darray_str *ret)
>  {
>  	darray_init(ret);
>  
> -	char *dev_name = kstrdup(_dev_name, GFP_KERNEL), *s = dev_name;
> +	char *dev_name, *s, *orig;
> +
> +	dev_name = orig = kstrdup(_dev_name, GFP_KERNEL);
>  	if (!dev_name)
>  		return -ENOMEM;
>  
> @@ -1201,10 +1203,10 @@ int bch2_split_devs(const char *_dev_name, darray_str *ret)
>  		}
>  	}
>  
> -	kfree(dev_name);
> +	kfree(orig);
>  	return 0;
>  err:
>  	bch2_darray_str_exit(ret);
> -	kfree(dev_name);
> +	kfree(orig);
>  	return -ENOMEM;
>  }
> -- 
> 2.43.0
> 


  reply	other threads:[~2024-01-08 15:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-08 15:11 [PATCH v3] bcachefs: fix memleak in bch2_split_devs Su Yue
2024-01-08 15:25 ` Brian Foster [this message]
2024-01-11  2:38 ` Kent Overstreet

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=ZZwT22xOjpeDDWlr@bfoster \
    --to=bfoster@redhat.com \
    --cc=glass.su@suse.com \
    --cc=l@damenly.org \
    --cc=linux-bcachefs@vger.kernel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox