btrfs-progs: tests: add fuzzing of superblock

Use btrfs-sb-mod and do some arbitrary changes to super blocks. Then run
'btrfs check'. Check will fail to read the filesystem most of the time
and cannot do any actual repair but must not crash.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-06-19 05:42:33 +02:00
parent d61f8c4c95
commit 8adc70d282
3 changed files with 70 additions and 2 deletions

View File

@ -550,7 +550,7 @@ test-mkfs: btrfs mkfs.btrfs
@echo " [TEST] mkfs-tests.sh" @echo " [TEST] mkfs-tests.sh"
$(Q)bash tests/mkfs-tests.sh $(Q)bash tests/mkfs-tests.sh
test-fuzz: btrfs btrfs-image test-fuzz: btrfs btrfs-image btrfs-sb-mod
@echo " [TEST] fuzz-tests.sh" @echo " [TEST] fuzz-tests.sh"
$(Q)bash tests/fuzz-tests.sh $(Q)bash tests/fuzz-tests.sh

View File

@ -410,7 +410,7 @@ check_prereq()
{ {
# Internal tools for testing, not shipped with the package # Internal tools for testing, not shipped with the package
case "$1" in case "$1" in
btrfs-corrupt-block|btrfs-find-root|btrfs-select-super|fssum) btrfs-corrupt-block|btrfs-find-root|btrfs-select-super|fssum|btrfs-sb-mod)
if ! [ -f "$INTERNAL_BIN/$1" ]; then if ! [ -f "$INTERNAL_BIN/$1" ]; then
_fail "Failed prerequisites: $INTERNAL_BIN/$1"; _fail "Failed prerequisites: $INTERNAL_BIN/$1";
fi fi

View File

@ -0,0 +1,68 @@
#!/bin/bash
# Change fields in super block and do test run of 'btrfs check'
source "$TEST_TOP/common" || exit
source "$TEST_TOP/common.convert" || exit
check_prereq btrfs-sb-mod
setup_root_helper
prepare_test_dev
run_check_mkfs_test_dev
run_check_mount_test_dev
#populate_fs
generate_dataset "small"
generate_dataset "sparse"
run_check_umount_test_dev
# See btrfs-sb-mod --help
fields=(
bytenr
flags
magic
generation
root
chunk_root
log_root
total_bytes
bytes_used
root_dir_objectid
num_devices
sectorsize
nodesize
stripesize
sys_chunk_array_size
chunk_root_generation
compat_flags
compat_ro_flags
incompat_flags
csum_type
root_level
chunk_root_level
log_root_level
cache_generation
uuid_tree_generation
dev_item.devid
dev_item.total_bytes
dev_item.bytes_used
dev_item.io_align
dev_item.io_width
dev_item.sector_size
dev_item.type
dev_item.generation
dev_item.start_offset
dev_item.dev_group
dev_item.seek_speed
dev_item.bandwidth)
for field in "${fields[@]}"; do
for op in -1 +1 ^2 ^4 ^256 +4096 -4096 @; do
run_check $SUDO_HELPER truncate -s 0 image.test
run_check $SUDO_HELPER cp --reflink=auto --sparse=auto "$TEST_DEV" image.test
run_check $SUDO_HELPER "$TOP/btrfs-sb-mod" image.test "$field" "$op"
run_mayfail $SUDO_HELPER "$TOP/btrfs" check image.test
done
done
run_check $SUDO_HELPER rm -f -- image.test