Skip to contentSkip to conversation

btrfs: minor fixes for handle_direct_tree_backref()

PATCH In review Qu Wenruo 2 participants 5 messages Nov 25 – Nov 26 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

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
author
#1 lore raw

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
index f021ac14babf..f7bc1ea19a0c 100644
@@ -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
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] btrfs: tree-checker: reject parent backref that goes beyond MAX_LEVEL

Quoted 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_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.

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.

Thanks.

Quoted 62 lines
---
 fs/btrfs/tree-checker.c | 24 +++++++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)
fs/btrfs/tree-checker.c +21 -4
index f021ac14babf..f7bc1ea19a0c 100644
@@ -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

author
#3 lore raw

PATCH 2/2 btrfs: backref: reject direct parent if the current level is maxed out

In reply to Qu Wenruo

Similar 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
index a0fb90b7a9ed..e8c299e1c010 100644
@@ -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
Reviewed-by on v1 2/2
#4 lore raw

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(), 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 +0 -0
index a0fb90b7a9ed..e8c299e1c010 100644
@@ -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