Skip to contentSkip to conversation

btrfs: tree-checker: verify tree_block_info bounds in check_extent_item()

PATCH Superseded Elkin Cruz 2 participants 2 messages Nov 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Elkin Cruz

author
#0 lore raw

PATCH 1/1 btrfs: tree-checker: verify tree_block_info bounds in check_extent_item()

check_extent_item() only verifies item_size >= sizeof(*ei) before reading struct btrfs_tree_block_info on non-skinny tree block extent items (BTRFS_EXTENT_ITEM_KEY with BTRFS_EXTENT_FLAG_TREE_BLOCK). A crafted filesystem image with 24 <= item_size < 42 causes btrfs_tree_block_level() to read past the item and potentially past the extent buffer's populated folios.

Verify item_size >= sizeof(*ei) + sizeof(*info) before accessing btrfs_tree_block_info.

Fixes f82d1c7ca8ae ("btrfs: tree-checker: Add EXTENT_ITEM and METADATA_ITEM check")

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

 fs/btrfs/tree-checker.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/btrfs/tree-checker.c +7 -0
index d2bf21c7b848..a38eeef7b859 100644
@@ -1665,6 +1665,13 @@ static int check_extent_item(struct extent_buffer *leaf,
 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
 		struct btrfs_tree_block_info *info;
 
+		if (unlikely(item_size < sizeof(*ei) + sizeof(*info))) {
+			extent_err(leaf, slot,
+				   "invalid item size, have %u expect [%zu, %u)",
+				   item_size, sizeof(*ei) + sizeof(*info),
+				   BTRFS_LEAF_DATA_SIZE(fs_info));
+			return -EUCLEAN;
+		}
 		info = (struct btrfs_tree_block_info *)ptr;
 		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
 			extent_err(leaf, slot,
Signature · 1 line
2.56.1.839.g22a8290db9-goog
#1 lore raw
Quoted 7 lines · Elkin Cruz

check_extent_item() only verifies item_size >= sizeof(*ei) before reading struct btrfs_tree_block_info on non-skinny tree block extent items (BTRFS_EXTENT_ITEM_KEY with BTRFS_EXTENT_FLAG_TREE_BLOCK). A crafted filesystem image with 24 <= item_size < 42 causes btrfs_tree_block_level() to read past the item and potentially past the extent buffer's populated folios.

Quoted 6 lines

Verify item_size >= sizeof(*ei) + sizeof(*info) before accessing btrfs_tree_block_info.

Fixes f82d1c7ca8ae ("btrfs: tree-checker: Add EXTENT_ITEM and METADATA_ITEM check")

The check overall looks good to me, but still a minor nitpick inliend below.

Quoted 18 lines

---

I am trying out new Clang static analysis checks for kernel bug classes, and they flagged this code as possibly buggy. The findings were triaged/fixed and commit descriptions were drafted with help of AI, then checked against linux-next (not runtime-tested on hardware).

Thanks,
Elkin

  fs/btrfs/tree-checker.c | 7 +++++++
  1 file changed, 7 insertions(+)
fs/btrfs/tree-checker.c +0 -0
index d2bf21c7b848..a38eeef7b859 100644

fs/btrfs/tree-checker.c static int check_extent_item(struct extent_buffer *leaf, · line 1668

Quoted 4 lines
fs/btrfs/tree-checker.c +1 -0
@@ -1665,6 +1665,13 @@ static int check_extent_item(struct extent_buffer *leaf,
  	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
  		struct btrfs_tree_block_info *info;
 
+		if (unlikely(item_size < sizeof(*ei) + sizeof(*info))) {

I won't call this easy to read, nor following the common pattern used later.

I'd prefer the more common (ptr + sizeof() > end) check instead.

Thanks,
Qu

Quoted 9 lines
fs/btrfs/tree-checker.c +6 -0
+			extent_err(leaf, slot,
+				   "invalid item size, have %u expect [%zu, %u)",
+				   item_size, sizeof(*ei) + sizeof(*info),
+				   BTRFS_LEAF_DATA_SIZE(fs_info));
+			return -EUCLEAN;
+		}
  		info = (struct btrfs_tree_block_info *)ptr;
  		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
  			extent_err(leaf, slot,