Skip to content

Commit 053ab70

Browse files
Liu Bomasoncl
authored andcommitted
Btrfs: check btree node's nritems
When btree node (level = 1) has nritems which equals to zero, we can end up with panic due to insert_ptr()'s BUG_ON(slot > nritems); where slot is 1 and nritems is 0, as copy_for_split() calls insert_ptr(.., path->slots[1] + 1, ...); A invalid value results in the whole mess, this adds the check for btree's node nritems so that we stop reading block when when something is wrong. Signed-off-by: Liu Bo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent 35bbb97 commit 053ab70

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

fs/btrfs/disk-io.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,19 @@ static noinline int check_leaf(struct btrfs_root *root,
613613
return 0;
614614
}
615615

616+
static int check_node(struct btrfs_root *root, struct extent_buffer *node)
617+
{
618+
unsigned long nr = btrfs_header_nritems(node);
619+
620+
if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root)) {
621+
btrfs_crit(root->fs_info,
622+
"corrupt node: block %llu root %llu nritems %lu",
623+
node->start, root->objectid, nr);
624+
return -EIO;
625+
}
626+
return 0;
627+
}
628+
616629
static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
617630
u64 phy_offset, struct page *page,
618631
u64 start, u64 end, int mirror)
@@ -683,6 +696,9 @@ static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
683696
ret = -EIO;
684697
}
685698

699+
if (found_level > 0 && check_node(root, eb))
700+
ret = -EIO;
701+
686702
if (!ret)
687703
set_extent_buffer_uptodate(eb);
688704
err:

0 commit comments

Comments
 (0)