Skip to content

Commit 2615aca

Browse files
adam900710kdave
authored andcommitted
btrfs-progs: ctree: Add extra level check for read_node_slot()
Strangely, we have level check in btrfs_print_tree() while we don't have the same check in read_node_slot(). That's to say, for the following corruption, btrfs_search_slot() or btrfs_next_leaf() can return invalid leaf: Parent eb: node XXXXXX level 1 ^^^^^^^ Child should be leaf (level 0) ... key (XXX XXX XXX) block YYYYYY Child eb: leaf YYYYYY level 1 ^^^^^^^ Something went wrong now And for the corrupted leaf returned, later caller can be screwed up easily. Reported-by: Ralph Gauges <ralphgauges@googlemail.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Nikolay Borisov <nborisov@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent e875cd9 commit 2615aca

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

ctree.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "repair.h"
2323
#include "internal.h"
2424
#include "sizes.h"
25+
#include "messages.h"
2526

2627
static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
2728
*root, struct btrfs_path *path, int level);
@@ -642,7 +643,9 @@ static int bin_search(struct extent_buffer *eb, struct btrfs_key *key,
642643
struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
643644
struct extent_buffer *parent, int slot)
644645
{
646+
struct extent_buffer *ret;
645647
int level = btrfs_header_level(parent);
648+
646649
if (slot < 0)
647650
return NULL;
648651
if (slot >= btrfs_header_nritems(parent))
@@ -651,8 +654,20 @@ struct extent_buffer *read_node_slot(struct btrfs_fs_info *fs_info,
651654
if (level == 0)
652655
return NULL;
653656

654-
return read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
657+
ret = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
655658
btrfs_node_ptr_generation(parent, slot));
659+
if (!extent_buffer_uptodate(ret))
660+
return ERR_PTR(-EIO);
661+
662+
if (btrfs_header_level(ret) != level - 1) {
663+
error(
664+
"child eb corrupted: parent bytenr=%llu item=%d parent level=%d child level=%d",
665+
btrfs_header_bytenr(parent), slot,
666+
btrfs_header_level(parent), btrfs_header_level(ret));
667+
free_extent_buffer(ret);
668+
return ERR_PTR(-EIO);
669+
}
670+
return ret;
656671
}
657672

658673
static int balance_level(struct btrfs_trans_handle *trans,

0 commit comments

Comments
 (0)