Skip to content

Commit 834a61b

Browse files
danobigregkh
authored andcommitted
btrfs: tree-checker: validate number of chunk stripes and parity
commit 85d07fb upstream. If there's no parity and num_stripes < ncopies, a crafted image can trigger a division by zero in calc_stripe_length(). The image was generated through fuzzing. CC: [email protected] # 5.4+ Reviewed-by: Qu Wenruo <[email protected]> Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=209587 Signed-off-by: Daniel Xu <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1cedc54 commit 834a61b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

fs/btrfs/tree-checker.c

+18
Original file line numberDiff line numberDiff line change
@@ -577,18 +577,36 @@ int btrfs_check_chunk_valid(struct extent_buffer *leaf,
577577
u64 type;
578578
u64 features;
579579
bool mixed = false;
580+
int raid_index;
581+
int nparity;
582+
int ncopies;
580583

581584
length = btrfs_chunk_length(leaf, chunk);
582585
stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
583586
num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
584587
sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
585588
type = btrfs_chunk_type(leaf, chunk);
589+
raid_index = btrfs_bg_flags_to_raid_index(type);
590+
ncopies = btrfs_raid_array[raid_index].ncopies;
591+
nparity = btrfs_raid_array[raid_index].nparity;
586592

587593
if (!num_stripes) {
588594
chunk_err(leaf, chunk, logical,
589595
"invalid chunk num_stripes, have %u", num_stripes);
590596
return -EUCLEAN;
591597
}
598+
if (num_stripes < ncopies) {
599+
chunk_err(leaf, chunk, logical,
600+
"invalid chunk num_stripes < ncopies, have %u < %d",
601+
num_stripes, ncopies);
602+
return -EUCLEAN;
603+
}
604+
if (nparity && num_stripes == nparity) {
605+
chunk_err(leaf, chunk, logical,
606+
"invalid chunk num_stripes == nparity, have %u == %d",
607+
num_stripes, nparity);
608+
return -EUCLEAN;
609+
}
592610
if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
593611
chunk_err(leaf, chunk, logical,
594612
"invalid chunk logical, have %llu should aligned to %u",

0 commit comments

Comments
 (0)