btrfs: ensure that a DUP block group has exactly two stripes
PATCH 1/1 btrfs: ensure that a DUP block group has exactly two stripes
We recently had a customer issue with a corrupted filesystem. When trying to mount this image btrfs panicked with a division by zero in calc_stripe_length().
The corrupt chunk had a 'num_stripes' value of 1. calc_stripe_length() takes this value and divides it by the number of copies the RAID profile is expected to have to calculate the amount of data stripes. As a DUP profile is expected to have 2 copies this division resulted in 1/2 = 0. Later then the 'data_stripes' variable is used as a divisor in the stripe length calculation which results in a division by 0 and thus a kernel panic.
When encountering a filesystem with a DUP block group and a 'num_stripes' value unequal to 2, refuse mounting as the image is corrupted and will lead to unexpected behaviour.
--- fs/btrfs/volumes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/volumes.c +1 -1
@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || + (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,
Signature · 1 line
2.16.4
Quoted 11 lines · Johannes Thumshirn
We recently had a customer issue with a corrupted filesystem. When trying to mount this image btrfs panicked with a division by zero in
calc_stripe_length().The corrupt chunk had a '
num_stripes' value of 1.calc_stripe_length()takes this value and divides it by the number of copies the RAID profile is expected to have to calculate the amount of data stripes. As a DUP profile is expected to have 2 copies this division resulted in 1/2 = 0. Later then the 'data_stripes' variable is used as a divisor in the stripe length calculation which results in a division by 0 and thus a kernel panic.
Quoted 7 lines
When encountering a filesystem with a DUP block group and a '
num_stripes' value unequal to 2, refuse mounting as the image is corrupted and will lead to unexpected behaviour.Fixes e06cd3dd7cea ("Btrfs: add validadtion checks for chunk loading")Cc Liu Bo <[email protected]>Signed-off-by Johannes Thumshirn <[email protected]>
Quoted 18 lines
--- fs/btrfs/volumes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/volumes.c +1
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || + (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,
Quoted 11 lines · Johannes Thumshirn
We recently had a customer issue with a corrupted filesystem. When trying to mount this image btrfs panicked with a division by zero in
calc_stripe_length().The corrupt chunk had a '
num_stripes' value of 1.calc_stripe_length()takes this value and divides it by the number of copies the RAID profile is expected to have to calculate the amount of data stripes. As a DUP profile is expected to have 2 copies this division resulted in 1/2 = 0. Later then the 'data_stripes' variable is used as a divisor in the stripe length calculation which results in a division by 0 and thus a kernel panic.
Quoted 7 lines
When encountering a filesystem with a DUP block group and a '
num_stripes' value unequal to 2, refuse mounting as the image is corrupted and will lead to unexpected behaviour.Fixes e06cd3dd7cea ("Btrfs: add validadtion checks for chunk loading")Cc Liu Bo <[email protected]>Signed-off-by Johannes Thumshirn <[email protected]>
Thanks,
Qu
Quoted 18 lines
--- fs/btrfs/volumes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/volumes.c +1
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || + (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,
Quoted 28 lines · Johannes Thumshirn
We recently had a customer issue with a corrupted filesystem. When trying to mount this image btrfs panicked with a division by zero in
calc_stripe_length().The corrupt chunk had a '
num_stripes' value of 1.calc_stripe_length()takes this value and divides it by the number of copies the RAID profile is expected to have to calculate the amount of data stripes. As a DUP profile is expected to have 2 copies this division resulted in 1/2 = 0. Later then the 'data_stripes' variable is used as a divisor in the stripe length calculation which results in a division by 0 and thus a kernel panic.When encountering a filesystem with a DUP block group and a '
num_stripes' value unequal to 2, refuse mounting as the image is corrupted and will lead to unexpected behaviour.Fixes e06cd3dd7cea ("Btrfs: add validadtion checks for chunk loading")Cc Liu Bo <[email protected]>Signed-off-by Johannes Thumshirn <[email protected]>--- fs/btrfs/volumes.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/btrfs/volumes.c +0
-0
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 8 lines
fs/btrfs/volumes.c +1
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes < 1) || (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) || + (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,
It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
Hans
On 13/02/2019 15:32, Hans van Kranenburg wrote:
[...]
Quoted 9 lines
Quoted 9 lines
fs/btrfs/volumes.c +0
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type &BTRFS_BLOCK_GROUP_RAID1 && num_stripes <1 ) || (type &BTRFS_BLOCK_GROUP_RAID5 && num_stripes <2 ) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
Quoted 8 lines
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 5 lines
fs/btrfs/volumes.c +1
-0+ (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
Hmm I guess a degraded RAID1 can have only 1 stripe, doesn't it?
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Quoted 21 lines · Johannes Thumshirn
On 13/02/2019 15:32, Hans van Kranenburg wrote:
[...]Quoted 9 lines
Quoted 9 lines
fs/btrfs/volumes.c +0
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type &BTRFS_BLOCK_GROUP_RAID1 && num_stripes <1 ) || (type &BTRFS_BLOCK_GROUP_RAID5 && num_stripes <2 ) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||Quoted 8 lines
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 5 lines
fs/btrfs/volumes.c +1
-0+ (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
Quoted 1 line
Hmm I guess a degraded RAID1 can have only 1 stripe, doesn't it?
It depends on the POV.
root@ubuntu-virtual:~# fallocate -l 2g /media/scratch/disk1.img root@ubuntu-virtual:~# fallocate -l 2g /media/scratch/disk2.img root@ubuntu-virtual:~# losetup -f /media/scratch/disk1.img root@ubuntu-virtual:~# losetup -f /media/scratch/disk2.img root@ubuntu-virtual:~# mkfs.btrfs -draid1 /dev/loop0 /dev/loop1 root@ubuntu-virtual:~# losetup -d /dev/loop1 root@ubuntu-virtual:~# mount -odegraded /dev/loop0 /media/test/
btrfs inspect-internal dump-tree -t chunk /dev/loop0 | grep -B1 -A5 DATA\|RAID1
item 3 key (FIRST_CHUNK_TREE CHUNK_ITEM 30408704) itemoff 15863 itemsize 112 length 214695936 owner 2 stripe_len 65536 type METADATA|RAID1 io_align 65536 io_width 65536 sector_size 4096 num_stripes 2 sub_stripes 0 stripe 0 devid 2 offset 9437184 dev_uuid adb924e5-830b-4eb0-b28a-f5bee6b709ca stripe 1 devid 1 offset 30408704 -- item 4 key (FIRST_CHUNK_TREE CHUNK_ITEM 245104640) itemoff 15751 itemsize 112 length 214695936 owner 2 stripe_len 65536 type DATA|RAID1 io_align 65536 io_width 65536 sector_size 4096 num_stripes 2 sub_stripes 0 stripe 0 devid 2 offset 224133120 dev_uuid adb924e5-830b-4eb0-b28a-f5bee6b709ca stripe 1 devid 1 offset 245104640
DMESG:
[78623.920424] BTRFS warning (device loop0): devid 2 uuid adb924e5-830b-4eb0-b28a-f5bee6b709ca is missing
Quoted 2 lines
Quoted 21 lines · Johannes Thumshirn
On 13/02/2019 15:32, Hans van Kranenburg wrote:
[...]Quoted 9 lines
Quoted 9 lines
fs/btrfs/volumes.c +0
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type &BTRFS_BLOCK_GROUP_RAID1 && num_stripes <1 ) || (type &BTRFS_BLOCK_GROUP_RAID5 && num_stripes <2 ) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||Quoted 8 lines
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 5 lines
fs/btrfs/volumes.c +1
-0+ (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
Quoted 1 line
Hmm I guess a degraded RAID1 can have only 1 stripe, doesn't it?
It's reading the metadata from disk here. So, there are always still exactly two 'stripe' structs inside the chunk item data.
Hans
On 13/02/2019 15:32, Hans van Kranenburg wrote:
[...]
Quoted 6 lines
Quoted 6 lines
+++
b/fs/btrfs/volumes.cfs/btrfs/volumes.c +0
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type &BTRFS_BLOCK_GROUP_RAID1 && num_stripes <1 ) || (type &BTRFS_BLOCK_GROUP_RAID5 && num_stripes <2 ) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||
Quoted 8 lines
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 5 lines
fs/btrfs/volumes.c +1
-0+ (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
So looking at the code again I think num_stripes == 1 for RAID1 will result in the same division by 0 in calc_stripe_length().
I'll send a patch for RAID1 as well unless David speaks up and says he wants it amended in this one.
Thanks,
Johannes
Signature · 6 lines
Johannes Thumshirn SUSE Labs Filesystems [email protected] +49 911 74053 689 SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg GF: Felix Imendörffer, Jane Smithard, Graham Norton HRB 21284 (AG Nürnberg) Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
Quoted 18 lines · Johannes Thumshirn
On 13/02/2019 15:32, Hans van Kranenburg wrote:
[...]Quoted 6 lines
Quoted 6 lines
+++
b/fs/btrfs/volumes.cfs/btrfs/volumes.c +0
-1@@ -6794,7 +6794,7 @@ static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, (type &BTRFS_BLOCK_GROUP_RAID1 && num_stripes <1 ) || (type &BTRFS_BLOCK_GROUP_RAID5 && num_stripes <2 ) || (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || - (type & BTRFS_BLOCK_GROUP_DUP && num_stripes > 2) ||Quoted 8 lines
fs/btrfs/volumes.c static int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, · line 6800
Quoted 5 lines
fs/btrfs/volumes.c +1
-0+ (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { btrfs_err(fs_info,It looks like the RAID1 check has a similar problem. Shouldn't that check also be != 2 ?
Quoted 5 lines
So looking at the code again I think
num_stripes== 1 for RAID1 will result in the same division by 0 incalc_stripe_length().I'll send a patch for RAID1 as well unless David speaks up and says he wants it amended in this one.
If the explanation and cause is the same, it's ok to put it into one patch I think, but no strong preference here.