Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH v2 0/2] btrfs: minor fixes in handle_indirect_tree_backref()
@ 2026-11-23 20:37 Qu Wenruo
  2026-11-23 20:37 ` [PATCH v2 1/2] btrfs: reject too high tree level during backref walk Qu Wenruo
  2026-11-23 20:37 ` [PATCH v2 2/2] btrfs: avoid NULL-ptr-dreference after a lowest_level search Qu Wenruo
  0 siblings, 2 replies; 5+ messages in thread
From: Qu Wenruo @ 2026-11-23 20:37 UTC (permalink / raw)
  To: linux-btrfs

[CHANGELOG]
v2:
- Add unlikely() for both patches

The first one is reported by Sashiko, the second one is another bug
exposed during fixing the first one.

Qu Wenruo (2):
  btrfs: reject too high tree level during backref walk
  btrfs: avoid NULL-ptr-dreference after a lowest_level search

 fs/btrfs/backref.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

-- 
2.56.1


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

* [PATCH v2 1/2] btrfs: reject too high tree level during backref walk
  2026-11-23 20:37 [PATCH v2 0/2] btrfs: minor fixes in handle_indirect_tree_backref() Qu Wenruo
@ 2026-11-23 20:37 ` Qu Wenruo
  2026-11-23 20:49   ` Miquel Sabaté Solà
       [not found]   ` <6b04a6e2.0b3c54ea.4a96a.e3b7SMTPIN_ADDED_BROKEN@mx.google.com>
  2026-11-23 20:37 ` [PATCH v2 2/2] btrfs: avoid NULL-ptr-dreference after a lowest_level search Qu Wenruo
  1 sibling, 2 replies; 5+ messages in thread
From: Qu Wenruo @ 2026-11-23 20:37 UTC (permalink / raw)
  To: linux-btrfs

In handle_indirect_tree_backref(), if we got @cur->level set to 7
(the max level valid), @level will be 8, then later path->slots[level]
and path->nodes[level] will all go beyond the array boundary.

Fix it by rejecting such high level before calling btrfs_search_slot().

Link: https://sashiko.dev/#/bug/linux-8a4f92b4-daf7-4f57-8369-8e6ef323652f
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/backref.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index a0fb90b7a9ed..e9434e033372 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 	}
 
 	level = cur->level + 1;
+	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
+		btrfs_put_root(root);
+		btrfs_err(fs_info, "invalid searcch level %d", level);
+		return -EUCLEAN;
+	}
 
 	/* Search the tree to find parent blocks referring to the block */
 	path->search_commit_root = true;
-- 
2.56.1


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

* [PATCH v2 2/2] btrfs: avoid NULL-ptr-dreference after a lowest_level search
  2026-11-23 20:37 [PATCH v2 0/2] btrfs: minor fixes in handle_indirect_tree_backref() Qu Wenruo
  2026-11-23 20:37 ` [PATCH v2 1/2] btrfs: reject too high tree level during backref walk Qu Wenruo
@ 2026-11-23 20:37 ` Qu Wenruo
  1 sibling, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2026-11-23 20:37 UTC (permalink / raw)
  To: linux-btrfs

In handle_indirect_tree_backref(), if we got a corrupted backref item
that has a valid level (e.g. 2), but higher than the subvolume root level
(e.g. 0), btrfs_search_slot() will return 0 or 1 depending on if the key
can be found in the leaf.

But the returned path->nodes[] will be all be NULL.

Later we call btrfs_node_blockptr(path->nodes[level]) will trigger a
NULL pointer dereference.

Fix it by checking if we got a valid eb pointer before calling
btrfs_node_blockptr(), and reject it as we can not find the parent tree
block.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/backref.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index e9434e033372..29737246d481 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -3336,7 +3336,8 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
 		path->slots[level]--;
 
 	eb = path->nodes[level];
-	if (btrfs_node_blockptr(eb, path->slots[level]) != cur->bytenr) {
+	if (unlikely(!eb || btrfs_node_blockptr(eb, path->slots[level]) !=
+		     cur->bytenr)) {
 		btrfs_err(fs_info,
 "couldn't find block (%llu) (level %d) in tree (%llu) with key " BTRFS_KEY_FMT,
 			  cur->bytenr, level - 1, btrfs_root_id(root),
-- 
2.56.1


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

* Re: [PATCH v2 1/2] btrfs: reject too high tree level during backref walk
  2026-11-23 20:37 ` [PATCH v2 1/2] btrfs: reject too high tree level during backref walk Qu Wenruo
@ 2026-11-23 20:49   ` Miquel Sabaté Solà
       [not found]   ` <6b04a6e2.0b3c54ea.4a96a.e3b7SMTPIN_ADDED_BROKEN@mx.google.com>
  1 sibling, 0 replies; 5+ messages in thread
From: Miquel Sabaté Solà @ 2026-11-23 20:49 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

[-- Attachment #1: Type: text/plain, Size: 1112 bytes --]

Qu Wenruo @ 2026-11-24 07:07 +1030:

> In handle_indirect_tree_backref(), if we got @cur->level set to 7
> (the max level valid), @level will be 8, then later path->slots[level]
> and path->nodes[level] will all go beyond the array boundary.
>
> Fix it by rejecting such high level before calling btrfs_search_slot().
>
> Link: https://sashiko.dev/#/bug/linux-8a4f92b4-daf7-4f57-8369-8e6ef323652f
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  fs/btrfs/backref.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
> index a0fb90b7a9ed..e9434e033372 100644
> --- a/fs/btrfs/backref.c
> +++ b/fs/btrfs/backref.c
> @@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
>  	}
>
>  	level = cur->level + 1;
> +	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
> +		btrfs_put_root(root);
> +		btrfs_err(fs_info, "invalid searcch level %d", level);

Very minor, mind the typo in "search".

> +		return -EUCLEAN;
> +	}
>
>  	/* Search the tree to find parent blocks referring to the block */
>  	path->search_commit_root = true;

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 897 bytes --]

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

* Re: [PATCH v2 1/2] btrfs: reject too high tree level during backref walk
       [not found]   ` <6b04a6e2.0b3c54ea.4a96a.e3b7SMTPIN_ADDED_BROKEN@mx.google.com>
@ 2026-11-23 21:09     ` Qu Wenruo
  0 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2026-11-23 21:09 UTC (permalink / raw)
  To: Miquel Sabaté Solà; +Cc: linux-btrfs



在 2026/11/24 07:19, Miquel Sabaté Solà 写道:
> Qu Wenruo @ 2026-11-24 07:07 +1030:
> 
>> In handle_indirect_tree_backref(), if we got @cur->level set to 7
>> (the max level valid), @level will be 8, then later path->slots[level]
>> and path->nodes[level] will all go beyond the array boundary.
>>
>> Fix it by rejecting such high level before calling btrfs_search_slot().
>>
>> Link: https://sashiko.dev/#/bug/linux-8a4f92b4-daf7-4f57-8369-8e6ef323652f
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>> ---
>>   fs/btrfs/backref.c | 5 +++++
>>   1 file changed, 5 insertions(+)
>>
>> diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
>> index a0fb90b7a9ed..e9434e033372 100644
>> --- a/fs/btrfs/backref.c
>> +++ b/fs/btrfs/backref.c
>> @@ -3316,6 +3316,11 @@ static int handle_indirect_tree_backref(struct btrfs_trans_handle *trans,
>>   	}
>>
>>   	level = cur->level + 1;
>> +	if (unlikely(level >= BTRFS_MAX_LEVEL)) {
>> +		btrfs_put_root(root);
>> +		btrfs_err(fs_info, "invalid searcch level %d", level);
> 
> Very minor, mind the typo in "search".

Thanks, I'll get it fixed during merge.

> 
>> +		return -EUCLEAN;
>> +	}
>>
>>   	/* Search the tree to find parent blocks referring to the block */
>>   	path->search_commit_root = true;


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

end of thread, other threads:[~2026-11-23 21:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 20:37 [PATCH v2 0/2] btrfs: minor fixes in handle_indirect_tree_backref() Qu Wenruo
2026-11-23 20:37 ` [PATCH v2 1/2] btrfs: reject too high tree level during backref walk Qu Wenruo
2026-11-23 20:49   ` Miquel Sabaté Solà
     [not found]   ` <6b04a6e2.0b3c54ea.4a96a.e3b7SMTPIN_ADDED_BROKEN@mx.google.com>
2026-11-23 21:09     ` Qu Wenruo
2026-11-23 20:37 ` [PATCH v2 2/2] btrfs: avoid NULL-ptr-dreference after a lowest_level search Qu Wenruo

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