btrfs: minor fixes for handle_direct_tree_backref()
cover btrfs: minor fixes for handle_direct_tree_backref()
The first one is a tree-checker enhancement to catch a tree block whose level is already maxed out, but still has a parent that is not itself. This should help us to detect such rare corruption early.
The second is a similar hardening at runtime, for handle_direct_tree_backref(), which is also reported by Sashiko.
Qu Wenruo (2):
btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL
btrfs: backref: reject direct parent if the current level is maxed out
fs/btrfs/backref.c | 11 +++++++++++ fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++--- 2 files changed, 32 insertions(+), 3 deletions(-)
Signature · 1 line
2.56.1
PATCH 1/2 btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL
Inspired by recent fixes to handle_indirect_tree_backref() and the original sashiko report, if a tree block is already at the max level (MAX_LEVEL - 1), it should not have a parent except itself.
The self-parent case is for reloc tree and legacy case where backref_rev is smaller than BTRFS_MIXED_BACKREF_REV.
So reject them when checking the inlined extent item. This is a very low priority hardening, as it's very hard to hit such high level tree block in the real world.
Please note that, this is not going to cover all cases, keyed backref can still introduce the same problem, but with the default skinny metadata, we should catch most cases now.
--- fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)
fs/btrfs/tree-checker.c +21 -3
@@ -1537,6 +1537,7 @@ static int check_extent_item(struct extent_buffer *leaf,struct btrfs_fs_info *fs_info = leaf->fs_info;struct btrfs_extent_item *ei;bool is_tree_block = false; +u8 level =0 ;/* Tree block level, only for tree blocks */ unsigned long ptr;/* Current pointer inside inline refs */ unsigned long end;/* Extent item end */ const u32 item_size =btrfs_item_size (leaf, slot);
@@ -1666,14 +1667,17 @@ static int check_extent_item(struct extent_buffer *leaf,struct btrfs_tree_block_info *info; info = (struct btrfs_tree_block_info *)ptr; -if (unlikely (btrfs_tree_block_level (leaf, info) >=BTRFS_MAX_LEVEL )) { + level =btrfs_tree_block_level (leaf, info); +if (unlikely (level >=BTRFS_MAX_LEVEL )) {extent_err (leaf, slot,"invalid tree block info level, have %u expect [0, %u]" , -btrfs_tree_block_level (leaf, info), -BTRFS_MAX_LEVEL -1 ); + level,BTRFS_MAX_LEVEL -1 );return -EUCLEAN ; } ptr = (unsigned long )(struct btrfs_tree_block_info *)(info +1 ); + }else if (is_tree_block) { +/* METADATA_ITEM_KEY, key->offset is already verified. */ + level = key->offset; }/* Check inline refs */
@@ -1719,6 +1723,20 @@ static int check_extent_item(struct extent_buffer *leaf, inline_offset, fs_info->sectorsize);return -EUCLEAN ; } +/* +* A tree block at the highest level can not have a +* parent node. +* The exception is the root of a reloc tree, or of a +* legacy tree with backref_rev < BTRFS_MIXED_BACKREF_REV, +* which use the block itself as the parent. +*/ +if (unlikely (level ==BTRFS_MAX_LEVEL -1 && + inline_offset != key->objectid)) { +extent_err (leaf, slot, +"invalid inline shared block ref, tree block at level %u can not have parent %llu" , + level, inline_offset); +return -EUCLEAN ; + } inline_refs++;break ;/*
Signature · 1 line
2.56.1
PATCH 2/2 btrfs: backref: reject direct parent if the current level is maxed out
In reply to Qu WenruoSimilar to the check in handle_indirect_tree_backref(), in handle_direct_tree_backref() we have a similar problem that if a tree block at level 7, searching for level 8 parent can lead to out-of-boundary access for btrfs_path structure during tree search.
Fix it by rejecting such backref walk if it's not self-parenting (e.g. reloc root).
--- fs/btrfs/backref.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/btrfs/backref.c +11 -0
@@ -3221,6 +3221,17 @@ static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,return 0 ; } +/* +* Self-parent already checked, and if we're at MAX_LEVEL -1, +* there can not be any higher level parent. +*/ +if (cur->level >=BTRFS_MAX_LEVEL -1 ) { +btrfs_err (cache->fs_info, +"tree block at %llu level %u can not have any parent" , + cur->bytenr, cur->level); +return -EUCLEAN ; + } + edge =btrfs_backref_alloc_edge (cache);if (!edge)return -ENOMEM ;
Signature · 1 line
2.56.1
Re: [PATCH 2/2] btrfs: backref: reject direct parent if the current level is maxed out
Quoted 23 lines · Qu Wenruo
Similar to the check in
handle_indirect_tree_backref(), inhandle_direct_tree_backref()we have a similar problem that if a tree block at level 7, searching for level 8 parent can lead to out-of-boundary access forbtrfs_pathstructure during tree search.Fix it by rejecting such backref walk if it's not self-parenting (e.g. reloc root).
Signed-off-by Qu Wenruo <[email protected]>--- fs/btrfs/backref.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/btrfs/backref.c +0
-0@@ -3221,6 +3221,17 @@ static int handle_direct_tree_backref(struct btrfs_backref_cache *cache,return 0 ; }
fs/btrfs/backref.c static int handle_direct_tree_backref(struct btrfs_backref_cache *cache, · line 3233
Quoted 10 lines
fs/btrfs/backref.c +10
-0+/* +* Self-parent already checked, and if we're at MAX_LEVEL -1, +* there can not be any higher level parent. +*/ +if (cur->level >=BTRFS_MAX_LEVEL -1 ) { +btrfs_err (cache->fs_info, +"tree block at %llu level %u can not have any parent" , + cur->bytenr, cur->level); +return -EUCLEAN ; + }
Please use unlikely(), as all branches that lead into unexpected critical conditions should use it.
Thanks.
Quoted 8 lines
fs/btrfs/backref.c +1
-1+ edge =btrfs_backref_alloc_edge (cache);if (!edge)return -ENOMEM ; --2.56.1
Re: [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL
In reply to Qu WenruoQuoted 12 lines · Qu Wenruo
Inspired by recent fixes to
handle_indirect_tree_backref()and the original sashiko report, if a tree block is already at the max level (MAX_LEVEL- 1), it should not have a parent except itself.The self-parent case is for reloc tree and legacy case where
backref_revis smaller thanBTRFS_MIXED_BACKREF_REV.So reject them when checking the inlined extent item. This is a very low priority hardening, as it's very hard to hit such high level tree block in the real world.
Quoted 5 lines
Please note that, this is not going to cover all cases, keyed backref can still introduce the same problem, but with the default skinny metadata, we should catch most cases now.
Signed-off-by Qu Wenruo <[email protected]>
Thanks.
Quoted 62 lines
--- fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-)fs/btrfs/tree-checker.c +21
-4@@ -1537,6 +1537,7 @@ static int check_extent_item(struct extent_buffer *leaf,struct btrfs_fs_info *fs_info = leaf->fs_info;struct btrfs_extent_item *ei;bool is_tree_block = false; +u8 level =0 ;/* Tree block level, only for tree blocks */ unsigned long ptr;/* Current pointer inside inline refs */ unsigned long end;/* Extent item end */ const u32 item_size =btrfs_item_size (leaf, slot);@@ -1666,14 +1667,17 @@ static int check_extent_item(struct extent_buffer *leaf,struct btrfs_tree_block_info *info; info = (struct btrfs_tree_block_info *)ptr; -if (unlikely (btrfs_tree_block_level (leaf, info) >=BTRFS_MAX_LEVEL )) { + level =btrfs_tree_block_level (leaf, info); +if (unlikely (level >=BTRFS_MAX_LEVEL )) {extent_err (leaf, slot,"invalid tree block info level, have %u expect [0, %u]" , -btrfs_tree_block_level (leaf, info), -BTRFS_MAX_LEVEL -1 ); + level,BTRFS_MAX_LEVEL -1 );return -EUCLEAN ; } ptr = (unsigned long )(struct btrfs_tree_block_info *)(info +1 ); + }else if (is_tree_block) { +/* METADATA_ITEM_KEY, key->offset is already verified. */ + level = key->offset; }/* Check inline refs */ @@ -1719,6 +1723,20 @@ static int check_extent_item(struct extent_buffer *leaf, inline_offset, fs_info->sectorsize);return -EUCLEAN ; } +/* +* A tree block at the highest level can not have a +* parent node. +* The exception is the root of a reloc tree, or of a +* legacy tree with backref_rev < BTRFS_MIXED_BACKREF_REV, +* which use the block itself as the parent. +*/ +if (unlikely (level ==BTRFS_MAX_LEVEL -1 && + inline_offset != key->objectid)) { +extent_err (leaf, slot, +"invalid inline shared block ref, tree block at level %u can not have parent %llu" , + level, inline_offset); +return -EUCLEAN ; + } inline_refs++;break ;/* -- 2.56.1