Skip to content

Commit ffa4889

Browse files
adam900710kdave
authored andcommitted
btrfs: tree-checker: Check leaf chunk item size
Inspired by btrfs-progs github issue torvalds#208, where chunk item in chunk tree has invalid num_stripes (0). Although that can already be caught by current btrfs_check_chunk_valid(), that function doesn't really check item size as it needs to handle chunk item in super block sys_chunk_array(). This patch will add two extra checks for chunk items in chunk tree: - Basic chunk item size If the item is smaller than btrfs_chunk (which already contains one stripe), exit right now as reading num_stripes may even go beyond eb boundary. - Item size check against num_stripes If item size doesn't match with calculated chunk size, then either the item size or the num_stripes is corrupted. Error out anyway. Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 8f2b50b commit ffa4889

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

fs/btrfs/tree-checker.c

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,44 @@ int btrfs_check_chunk_valid(struct extent_buffer *leaf,
738738
return 0;
739739
}
740740

741+
/*
742+
* Enhanced version of chunk item checker.
743+
*
744+
* The common btrfs_check_chunk_valid() doesn't check item size since it needs
745+
* to work on super block sys_chunk_array which doesn't have full item ptr.
746+
*/
747+
static int check_leaf_chunk_item(struct extent_buffer *leaf,
748+
struct btrfs_chunk *chunk,
749+
struct btrfs_key *key, int slot)
750+
{
751+
int num_stripes;
752+
753+
if (btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) {
754+
chunk_err(leaf, chunk, key->offset,
755+
"invalid chunk item size: have %u expect [%zu, %u)",
756+
btrfs_item_size_nr(leaf, slot),
757+
sizeof(struct btrfs_chunk),
758+
BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
759+
return -EUCLEAN;
760+
}
761+
762+
num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
763+
/* Let btrfs_check_chunk_valid() handle this error type */
764+
if (num_stripes == 0)
765+
goto out;
766+
767+
if (btrfs_chunk_item_size(num_stripes) !=
768+
btrfs_item_size_nr(leaf, slot)) {
769+
chunk_err(leaf, chunk, key->offset,
770+
"invalid chunk item size: have %u expect %lu",
771+
btrfs_item_size_nr(leaf, slot),
772+
btrfs_chunk_item_size(num_stripes));
773+
return -EUCLEAN;
774+
}
775+
out:
776+
return btrfs_check_chunk_valid(leaf, chunk, key->offset);
777+
}
778+
741779
__printf(3, 4)
742780
__cold
743781
static void dev_item_err(const struct extent_buffer *eb, int slot,
@@ -1384,7 +1422,7 @@ static int check_leaf_item(struct extent_buffer *leaf,
13841422
break;
13851423
case BTRFS_CHUNK_ITEM_KEY:
13861424
chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1387-
ret = btrfs_check_chunk_valid(leaf, chunk, key->offset);
1425+
ret = check_leaf_chunk_item(leaf, chunk, key, slot);
13881426
break;
13891427
case BTRFS_DEV_ITEM_KEY:
13901428
ret = check_dev_item(leaf, key, slot);

0 commit comments

Comments
 (0)