mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot()
@ 2025-02-24  7:51 Ma Ke
  2025-02-24  7:56 ` Qu Wenruo
  2025-02-24 14:24 ` Filipe Manana
  0 siblings, 2 replies; 3+ messages in thread
From: Ma Ke @ 2025-02-24  7:51 UTC (permalink / raw)
  To: clm, josef, dsterba, jeffm; +Cc: linux-btrfs, linux-kernel, Ma Ke, stable

When searching the extent tree to gather the needed extent info,
btrfs_search_old_slot() doesn't check if the target root is NULL or
not, resulting the null-ptr-deref. Add sanity check for btrfs root
before using it in btrfs_search_old_slot().

Found by code review.

Cc: stable@vger.kernel.org
Fixes: 0b246afa62b0 ("btrfs: root->fs_info cleanup, add fs_info convenience variables")
Signed-off-by: Ma Ke <make24@iscas.ac.cn>
---
 fs/btrfs/ctree.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 3dc5a35dd19b..4e2e1c38d33a 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -2232,7 +2232,7 @@ ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
 int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
 			  struct btrfs_path *p, u64 time_seq)
 {
-	struct btrfs_fs_info *fs_info = root->fs_info;
+	struct btrfs_fs_info *fs_info;
 	struct extent_buffer *b;
 	int slot;
 	int ret;
@@ -2241,6 +2241,10 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
 	int lowest_unlock = 1;
 	u8 lowest_level = 0;
 
+	if (!root)
+		return -EINVAL;
+
+	fs_info = root->fs_info;
 	lowest_level = p->lowest_level;
 	WARN_ON(p->nodes[0] != NULL);
 	ASSERT(!p->nowait);
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot()
  2025-02-24  7:51 [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot() Ma Ke
@ 2025-02-24  7:56 ` Qu Wenruo
  2025-02-24 14:24 ` Filipe Manana
  1 sibling, 0 replies; 3+ messages in thread
From: Qu Wenruo @ 2025-02-24  7:56 UTC (permalink / raw)
  To: Ma Ke, clm, josef, dsterba, jeffm; +Cc: linux-btrfs, linux-kernel, stable



在 2025/2/24 18:21, Ma Ke 写道:
> When searching the extent tree to gather the needed extent info,
> btrfs_search_old_slot() doesn't check if the target root is NULL or
> not, resulting the null-ptr-deref. Add sanity check for btrfs root
> before using it in btrfs_search_old_slot().

I do not think it's the case anymore.

Commit 6aecd91a5c5b ("btrfs: avoid NULL pointer dereference if no valid
extent tree") has introduced a check for the extent root, at the very
beginning of scrub_find_fill_first_stripe().

Thus it will never call find_first_extent_item() to search the NULL
extent tree.

Or do you have another case where we need to search extent tree
meanwhile the fs is mounted with rescue=ibadroots?

Thanks,
Qu

>
> Found by code review.
>
> Cc: stable@vger.kernel.org
> Fixes: 0b246afa62b0 ("btrfs: root->fs_info cleanup, add fs_info convenience variables")
> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>   fs/btrfs/ctree.c | 6 +++++-
>   1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
> index 3dc5a35dd19b..4e2e1c38d33a 100644
> --- a/fs/btrfs/ctree.c
> +++ b/fs/btrfs/ctree.c
> @@ -2232,7 +2232,7 @@ ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
>   int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
>   			  struct btrfs_path *p, u64 time_seq)
>   {
> -	struct btrfs_fs_info *fs_info = root->fs_info;
> +	struct btrfs_fs_info *fs_info;
>   	struct extent_buffer *b;
>   	int slot;
>   	int ret;
> @@ -2241,6 +2241,10 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
>   	int lowest_unlock = 1;
>   	u8 lowest_level = 0;
>
> +	if (!root)
> +		return -EINVAL;
> +
> +	fs_info = root->fs_info;
>   	lowest_level = p->lowest_level;
>   	WARN_ON(p->nodes[0] != NULL);
>   	ASSERT(!p->nowait);


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot()
  2025-02-24  7:51 [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot() Ma Ke
  2025-02-24  7:56 ` Qu Wenruo
@ 2025-02-24 14:24 ` Filipe Manana
  1 sibling, 0 replies; 3+ messages in thread
From: Filipe Manana @ 2025-02-24 14:24 UTC (permalink / raw)
  To: Ma Ke; +Cc: clm, josef, dsterba, jeffm, linux-btrfs, linux-kernel, stable

On Mon, Feb 24, 2025 at 7:52 AM Ma Ke <make24@iscas.ac.cn> wrote:
>
> When searching the extent tree to gather the needed extent info,
> btrfs_search_old_slot() doesn't check if the target root is NULL or
> not, resulting the null-ptr-deref. Add sanity check for btrfs root
> before using it in btrfs_search_old_slot().
>
> Found by code review.
>
> Cc: stable@vger.kernel.org
> Fixes: 0b246afa62b0 ("btrfs: root->fs_info cleanup, add fs_info convenience variables")

Besides what was already pointed out by Qu, that you should explain
how can the root be NULL, etc, this is also a completely wrong commit
in the Fixes tag.

You can only get a NULL extent root if the fs was mounted with
ignorebadroots and there is a corruption in the extent tree (or maybe
we got some transient error while attempting to read it like -ENOMEM).

So ignorebadroots was introduced in commit 42437a6386ff ("btrfs:
introduce mount option rescue=ignorebadroots") which is dated from
2020, but that commit you added in the Fixes tag is from 2016, back
when we could never have successfully mounted the fs with a NULL
extent root.

> Signed-off-by: Ma Ke <make24@iscas.ac.cn>
> ---
>  fs/btrfs/ctree.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
> index 3dc5a35dd19b..4e2e1c38d33a 100644
> --- a/fs/btrfs/ctree.c
> +++ b/fs/btrfs/ctree.c
> @@ -2232,7 +2232,7 @@ ALLOW_ERROR_INJECTION(btrfs_search_slot, ERRNO);
>  int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
>                           struct btrfs_path *p, u64 time_seq)
>  {
> -       struct btrfs_fs_info *fs_info = root->fs_info;
> +       struct btrfs_fs_info *fs_info;
>         struct extent_buffer *b;
>         int slot;
>         int ret;
> @@ -2241,6 +2241,10 @@ int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
>         int lowest_unlock = 1;
>         u8 lowest_level = 0;
>
> +       if (!root)
> +               return -EINVAL;
> +
> +       fs_info = root->fs_info;
>         lowest_level = p->lowest_level;
>         WARN_ON(p->nodes[0] != NULL);
>         ASSERT(!p->nowait);
> --
> 2.25.1
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-02-24 14:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-02-24  7:51 [PATCH] btrfs: add a sanity check for btrfs root in btrfs_search_old_slot() Ma Ke
2025-02-24  7:56 ` Qu Wenruo
2025-02-24 14:24 ` Filipe Manana

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®