From: Boris Burkov <boris@bur.io>
To: fdmanana@kernel.org
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH] btrfs: fix dangling nodes in tree-mod-log after error in btrfs_tree_mod_log_insert_root()
Date: Fri, 18 Sep 2026 10:04:45 -0700 [thread overview]
Message-ID: <20260918170445.GA2900089@zen.localdomain> (raw)
In-Reply-To: <d2bc9a10e82d87166dbd60797a93a69560f02411.1789750133.git.fdmanana@suse.com>
On Fri, Sep 18, 2026 at 05:49:14PM +0100, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> If in btrfs_tree_mod_log_insert_root() there is an error in the call to
> tree_mod_log_insert() (the only possible error is -EEXIST, which means we
> have a bug or some memory corruption maybe) we free all the nodes in the
> 'tm_list' array but we don't remove them from the tree-mod-log rbtree,
> which can result in use-after-free bugs later.
>
> So make sure we remove the nodes from the rbtree before freeing them after
> an error.
>
> Fixes: 5de865eebb83 ("Btrfs: fix tree mod logging")
> Assisted-by: LLM (found the bug)
Reviewed-by: Boris Burkov <boris@bur.io>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> ---
> fs/btrfs/tree-mod-log.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/tree-mod-log.c b/fs/btrfs/tree-mod-log.c
> index a8094928f4c9..f2a00a7df1cd 100644
> --- a/fs/btrfs/tree-mod-log.c
> +++ b/fs/btrfs/tree-mod-log.c
> @@ -498,8 +498,11 @@ int btrfs_tree_mod_log_insert_root(struct extent_buffer *old_root,
>
> free_tms:
> if (tm_list) {
> - for (i = 0; i < nritems; i++)
> + for (i = 0; i < nritems; i++) {
> + if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
> + rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
> kfree(tm_list[i]);
> + }
> kfree(tm_list);
> }
> kfree(tm);
> --
> 2.47.2
>
next prev parent reply other threads:[~2026-09-18 17:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-18 16:49 [PATCH] btrfs: fix dangling nodes in tree-mod-log after error in btrfs_tree_mod_log_insert_root() fdmanana
2026-09-18 17:04 ` Boris Burkov [this message]
2026-09-18 17:27 ` [PATCH v2] " fdmanana
2026-09-18 17:46 ` [PATCH v3] " fdmanana
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=20260918170445.GA2900089@zen.localdomain \
--to=boris@bur.io \
--cc=fdmanana@kernel.org \
--cc=linux-btrfs@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