btrfs-progs: add detection for unexpected file extents
cover btrfs-progs: add detection for unexpected file extents
There is a kernel patch (*) which mentioned a corrupted fs image where there is at least one file extent item which shouldn't exist, as the inode is not a symlink or regular file.
It turns out btrfs check can not detect such case.
The first 3 patches introduce the ability to generate such image.
The 4th patch adds the detection ability, and the last one is the test case.
*: https://lore.kernel.org/linux-btrfs/[email protected]/
Qu Wenruo (5):
btrfs-progs: corrupt-block: allow inode corruption to use --value
option
btrfs-progs: corrupt-block: allow dir item corruption to use --value
option
btrfs-progs: corrupt-block: allow corrupting dir item flags
btrfs-progs: check: detect invalid file extent items
btrfs-progs: fsck-test: add a test case for unexpected file extent
items
btrfs-corrupt-block.c | 68 ++++++++++-------- check/main.c | 5 ++ check/mode-lowmem.c | 26 ++++--- .../075-blk-inode-with-data/default.img.xz | Bin 0 -> 2104 bytes .../075-blk-inode-with-data/test.sh | 16 +++++ 5 files changed, 77 insertions(+), 38 deletions(-) create mode 100644 tests/fsck-tests/075-blk-inode-with-data/default.img.xz create mode 100755 tests/fsck-tests/075-blk-inode-with-data/test.sh
--
2.54.0
PATCH 1/5 btrfs-progs: corrupt-block: allow inode corruption to use --value option
This allows us to do more accurate inode item corruption, not only to inject random number, but more targeted values.
This feature will be later utilized to change inode's mode from REG to BLK.
--- btrfs-corrupt-block.c | 51 +++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 26 deletions(-)
btrfs-corrupt-block.c +25 -26
@@ -694,14 +694,13 @@ out: }static int corrupt_inode (struct btrfs_trans_handle *trans, -struct btrfs_root *root,u64 inode,char *field) +struct btrfs_root *root,u64 inode,char *field, +u64 bogus) {struct btrfs_inode_item *ei;struct btrfs_path *path;struct btrfs_key key;enum btrfs_inode_field corrupt_field =convert_inode_field (field); -u64 bogus; -u64 orig;int ret;if (corrupt_field ==BTRFS_INODE_FIELD_BAD ) {
@@ -741,49 +740,49 @@ static int corrupt_inode(struct btrfs_trans_handle *trans, struct btrfs_inode_item);switch (corrupt_field) {case BTRFS_INODE_FIELD_ISIZE : - orig =btrfs_inode_size (path->nodes[0 ], ei); - bogus =generate_u64 (orig); +if (bogus == (u64)-1 ) + bogus =generate_u64 (btrfs_inode_size (path->nodes[0 ], ei));btrfs_set_inode_size (path->nodes[0 ], ei, bogus);break ;case BTRFS_INODE_FIELD_NBYTES : - orig =btrfs_inode_nbytes (path->nodes[0 ], ei); - bogus =generate_u64 (orig); +if (bogus == (u64)-1 ) + bogus =generate_u64 (btrfs_inode_nbytes (path->nodes[0 ], ei));btrfs_set_inode_nbytes (path->nodes[0 ], ei, bogus);break ;case BTRFS_INODE_FIELD_NLINK : - orig =btrfs_inode_nlink (path->nodes[0 ], ei); - bogus =generate_u32 (orig); -btrfs_set_inode_nlink (path->nodes[0 ], ei, bogus); +if (bogus == (u64)-1 ) + bogus =generate_u32 (btrfs_inode_nlink (path->nodes[0 ], ei)); +btrfs_set_inode_nlink (path->nodes[0 ], ei, (u32 )bogus);break ;case BTRFS_INODE_FIELD_GENERATION : - orig =btrfs_inode_generation (path->nodes[0 ], ei); - bogus =generate_u64 (orig); +if (bogus == (u64)-1 ) + bogus =generate_u64 (btrfs_inode_generation (path->nodes[0 ], ei));btrfs_set_inode_generation (path->nodes[0 ], ei, bogus);break ;case BTRFS_INODE_FIELD_TRANSID : - orig =btrfs_inode_transid (path->nodes[0 ], ei); - bogus =generate_u64 (orig); +if (bogus == (u64)-1 ) + bogus =generate_u64 (btrfs_inode_transid (path->nodes[0 ], ei));btrfs_set_inode_transid (path->nodes[0 ], ei, bogus);break ;case BTRFS_INODE_FIELD_BLOCK_GROUP : - orig =btrfs_inode_block_group (path->nodes[0 ], ei); - bogus =generate_u64 (orig); +if (bogus == (u64)-1 ) + bogus =generate_u64 (btrfs_inode_block_group (path->nodes[0 ], ei));btrfs_set_inode_block_group (path->nodes[0 ], ei, bogus);break ;case BTRFS_INODE_FIELD_MODE : - orig =btrfs_inode_mode (path->nodes[0 ], ei); - bogus =generate_u32 (orig); -btrfs_set_inode_mode (path->nodes[0 ], ei, bogus); +if (bogus == (u64)-1 ) + bogus =generate_u32 (btrfs_inode_mode (path->nodes[0 ], ei)); +btrfs_set_inode_mode (path->nodes[0 ], ei, (u32 )bogus);break ;case BTRFS_INODE_FIELD_UID : - orig =btrfs_inode_uid (path->nodes[0 ], ei); - bogus =generate_u32 (orig); -btrfs_set_inode_uid (path->nodes[0 ], ei, bogus); +if (bogus == (u64)-1 ) + bogus =generate_u32 (btrfs_inode_uid (path->nodes[0 ], ei)); +btrfs_set_inode_uid (path->nodes[0 ], ei, (u32 )bogus);break ;case BTRFS_INODE_FIELD_GID : - orig =btrfs_inode_gid (path->nodes[0 ], ei); - bogus =generate_u32 (orig); -btrfs_set_inode_gid (path->nodes[0 ], ei, bogus); +if (bogus == (u64)-1 ) + bogus =generate_u32 (btrfs_inode_gid (path->nodes[0 ], ei)); +btrfs_set_inode_gid (path->nodes[0 ], ei, (u32 )bogus);break ;default : ret = -EINVAL ;
@@ -1542,7 +1541,7 @@ int main(int argc, char **argv)BUG_ON (IS_ERR (trans));if (file_extent == (u64)-1 ) {printf ("corrupting inode\n" ); - ret =corrupt_inode (trans, root, inode, field); + ret =corrupt_inode (trans, root, inode, field, bogus_value); }else { ret =corrupt_file_extent (trans, root, inode, file_extent, field, bogus_value);
Signature · 1 line
2.54.0
PATCH 2/5 btrfs-progs: corrupt-block: allow dir item corruption to use --value option
In reply to Qu WenruoFor now this only affects DIR_ITEM location objectid corruption, but later this will be utilized for more dir item member corruption.
--- btrfs-corrupt-block.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
btrfs-corrupt-block.c +4 -4
@@ -625,7 +625,7 @@ out: }static int corrupt_dir_item (struct btrfs_root *root,struct btrfs_key *key, -char *field) +char *field,u64 bogus) {struct btrfs_trans_handle *trans;struct btrfs_dir_item *di;
@@ -636,7 +636,6 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,unsigned long name_ptr;enum btrfs_dir_item_field corrupt_field =convert_dir_item_field (field); -u64 bogus;u16 name_len;int ret;
@@ -677,7 +676,8 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key, goto out;case BTRFS_DIR_ITEM_LOCATION_OBJECTID :btrfs_dir_item_key_to_cpu (path->nodes[0 ], di, &location); - bogus =generate_u64 (location.objectid); +if (bogus == (u64)-1 ) + bogus =generate_u64 (location.objectid); location.objectid = bogus;btrfs_cpu_key_to_disk (&disk_key, &location);btrfs_set_dir_item_key (path->nodes[0 ], di, &disk_key);
@@ -1559,7 +1559,7 @@ int main(int argc, char **argv)if (corrupt_di) {if (!key.objectid || *field ==0 )usage (&corrupt_block_cmd,1 ); - ret =corrupt_dir_item (target_root, &key, field); + ret =corrupt_dir_item (target_root, &key, field, bogus_value); goto out_close; }if (csum_bytenr) {
Signature · 1 line
2.54.0
PATCH 3/5 btrfs-progs: corrupt-block: allow corrupting dir item flags
In reply to Qu WenruoWith this feature and the previous inode corruption, we can change the type in inode item, dir item, dir index to be consistent with each other.
--- btrfs-corrupt-block.c | 9 +++++++++ 1 file changed, 9 insertions(+)
btrfs-corrupt-block.c +9 -0
@@ -343,6 +343,7 @@ enum btrfs_file_extent_field {enum btrfs_dir_item_field {BTRFS_DIR_ITEM_NAME ,BTRFS_DIR_ITEM_LOCATION_OBJECTID , +BTRFS_DIR_ITEM_FLAGS ,BTRFS_DIR_ITEM_BAD , };
@@ -448,6 +449,8 @@ static enum btrfs_dir_item_field convert_dir_item_field(char *field)return BTRFS_DIR_ITEM_NAME ;if (strncmp (field,"location_objectid" ,FIELD_BUF_LEN ) ==0 )return BTRFS_DIR_ITEM_LOCATION_OBJECTID ; +if (strncmp (field,"flags" ,FIELD_BUF_LEN ) ==0 ) +return BTRFS_DIR_ITEM_FLAGS ;return BTRFS_DIR_ITEM_BAD ; }
@@ -683,6 +686,12 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,btrfs_set_dir_item_key (path->nodes[0 ], di, &disk_key);btrfs_mark_buffer_dirty (path->nodes[0 ]); goto out; +case BTRFS_DIR_ITEM_FLAGS : +if (bogus == (u64)-1 ) + bogus =generate_u8 (btrfs_dir_flags (path->nodes[0 ], di)); +btrfs_set_dir_flags (path->nodes[0 ], di, bogus); +btrfs_mark_buffer_dirty (path->nodes[0 ]); + goto out;default : ret = -EINVAL ; goto out;
Signature · 1 line
2.54.0
PATCH 4/5 btrfs-progs: check: detect invalid file extent items
In reply to Qu WenruoFile extent items should only exist for regular and symlink inodes. And for symlink inodes, the file extent must be inlined.
Add such check for lowmem mode to detect those invalid file extents.
Now for a corrupted image with the following fs tree layout:
item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160
generation 3 transid 9 size 12 nbytes 16384 block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0 sequence 1 flags 0x0(none)
item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12
index 0 namelen 2 name: ..
item 2 key (256 DIR_ITEM 496027801) itemoff 16075 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV transid 9 data_len 0 name_len 6
name: foobar
item 3 key (256 DIR_INDEX 2) itemoff 16039 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV transid 9 data_len 0 name_len 6
name: foobar
item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
generation 9 transid 9 size 8192 nbytes 8192 block group 0 mode 60660 links 1 uid 0 gid 0 rdev 0 sequence 2 flags 0x0(none)
item 5 key (257 INODE_REF 256) itemoff 15863 itemsize 16
index 2 namelen 6 name: foobar
item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
generation 9 type 1 (regular)
extent data disk byte 13631488 nr 8192 extent data offset 0 nr 8192 ram 8192 extent compression 0 (none)
Lowmem mode will detect the error like the following:
[5/8] checking fs roots
ERROR: root 5 ino 257 should not have file extent
ERROR: root 5 INODE[257] nbytes 8192 not equal to extent_size 0
ERROR: errors found in fs roots
found 172032 bytes used, error(s) found
For the original mode, it's less strict than the lowmem mode, and it only rejects the obvious cases, without the extra verification on inlined extent for symlinks:
[5/8] checking fs roots
root 5 inode 257 errors 40, bad file extent
ERROR: errors found in fs roots
found 172032 bytes used, error(s) found
--- check/main.c | 5 +++++ check/mode-lowmem.c | 26 ++++++++++++++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-)
check/main.c +5 -0
@@ -844,6 +844,11 @@ static void maybe_free_inode_rec(struct cache_tree *inode_cache,if (!rec->found_inode_item)return ; +/* If it's not REG or SYMLNK, there should be no file extent. */ +if (is_valid_imode (rec->imode) && !S_ISREG (rec->imode) && + !S_ISLNK (rec->imode) && rec->found_file_extent) + rec->errors |=I_ERR_BAD_FILE_EXTENT ; + filetype =imode_to_type (rec->imode);list_for_each_entry_safe (backref, tmp, &rec->backrefs, list) {if (backref->found_dir_item && backref->found_dir_index) {
check/mode-lowmem.c +18 -8
@@ -2093,8 +2093,8 @@ static int check_file_extent_inline(struct btrfs_root *root,* Return 0 if no error occurred. */ static int check_file_extent (struct btrfs_root *root,struct btrfs_path *path, -unsigned int nodatasum,u64 isize,u64 *size, -u64 *end) +unsigned int nodatasum,u64 isize,u32 mode, +u64 *size,u64 *end) {struct btrfs_file_extent_item *fi;struct btrfs_key fkey;
@@ -2119,6 +2119,14 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path, fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item); extent_type =btrfs_file_extent_type (node, fi); +/* Only regular and symlink can have file extents. */ +if (is_valid_imode (mode) && !S_ISREG (mode) && !S_ISLNK (mode)) { + err |=FILE_EXTENT_ERROR ; +error ("root %llu ino %llu should not have file extent" , +btrfs_root_id (root), fkey.objectid); +return err; + } +/* Check extent type */ if (extent_type != BTRFS_FILE_EXTENT_REG && extent_type != BTRFS_FILE_EXTENT_PREALLOC &&
@@ -2129,6 +2137,13 @@ static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,return err; } +if (S_ISLNK (mode) && extent_type !=BTRFS_FILE_EXTENT_INLINE ) { + err |=FILE_EXTENT_ERROR ; +error ("root %llu ino %llu should not have regular/prealloc file extent" , + root->objectid, fkey.objectid); +return err; + } +/* Check inline extent */ if (extent_type ==BTRFS_FILE_EXTENT_INLINE )return check_file_extent_inline (root, path, size, end);
@@ -2807,12 +2822,7 @@ static int check_inode_item(struct btrfs_root *root, struct btrfs_path *path) err |= ret;break ;case BTRFS_EXTENT_DATA_KEY : -if (dir) { -warning ("root %llu DIR INODE[%llu] shouldn't EXTENT_DATA[%llu %llu]" , - root->objectid, inode_id, key.objectid, - key.offset); - } - ret =check_file_extent (root, path, nodatasum, isize, + ret =check_file_extent (root, path, nodatasum, isize, mode, &extent_size, &extent_end); err |= ret;break ;
Signature · 1 line
2.54.0
PATCH 5/5 btrfs-progs: fsck-test: add a test case for unexpected file extent items
In reply to Qu WenruoThe image is created using btrfs-corrupt-block, the fs tree leaf looks like this:
item 0 key (256 INODE_ITEM 0) itemoff 16123 itemsize 160
generation 3 transid 9 size 12 nbytes 16384 block group 0 mode 40755 links 1 uid 0 gid 0 rdev 0 sequence 1 flags 0x0(none)
item 1 key (256 INODE_REF 256) itemoff 16111 itemsize 12
index 0 namelen 2 name: ..
item 2 key (256 DIR_ITEM 496027801) itemoff 16075 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV <<< transid 9 data_len 0 name_len 6
name: foobar
item 3 key (256 DIR_INDEX 2) itemoff 16039 itemsize 36
location key (257 INODE_ITEM 0) type BLKDEV <<< transid 9 data_len 0 name_len 6
name: foobar
item 4 key (257 INODE_ITEM 0) itemoff 15879 itemsize 160
generation 9 transid 9 size 8192 nbytes 8192 block group 0 mode 60660 links 1 uid 0 gid 0 rdev 0 <<< sequence 2 flags 0x0(none)
item 5 key (257 INODE_REF 256) itemoff 15863 itemsize 16
index 2 namelen 6 name: foobar
item 6 key (257 EXTENT_DATA 0) itemoff 15810 itemsize 53
generation 9 type 1 (regular)
extent data disk byte 13631488 nr 8192 extent data offset 0 nr 8192 ram 8192
extent compression 0 (none)
The image is generated by the following steps:
# mkfs.btrfs -f $dev # mount $dev $mnt # xfs_io -f -c "pwrite 0 4k" $mnt/foobar # umount $mnt
## Change the inode's mode from REG to BLK
# btrfs-corrupt-block -i 257 -f mode --value 25008 $dev
## Change the dir item's flag from REG to BLK
# btrfs-corrupt-block -D 256,84,496027801 -f flags --value 4 $dev
## Change the dir index's flag from REG to BLK
# btrfs-corrupt-block -D 256,96,2 -f flags --value 4 $dev
The inode's mode and dir flags all match, but there is one unexpected file extent item there.
Both lowmem and original mode should detect such problem.
--- .../075-blk-inode-with-data/default.img.xz | Bin 0 -> 2104 bytes .../fsck-tests/075-blk-inode-with-data/test.sh | 16 ++++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/fsck-tests/075-blk-inode-with-data/default.img.xz create mode 100755 tests/fsck-tests/075-blk-inode-with-data/test.sh
tests/fsck-tests/075-blk-inode-with-data/default.img.xz new file binary +0 -0
tests/fsck-tests/075-blk-inode-with-data/test.sh new file +16 -0
@@ -0,0 +1,16 @@ +#!/bin/bash +# +# Verify that check can detect unexpected file extent items for special inodes +# which should not have any file extent. + +source "$TEST_TOP/common" || exit + +check_prereq btrfs + +check_image() { + # The image contains an BLK inode, with a file extent. + # The check should report an error. + run_mustfail "unexpected file extent not detected" "$TOP/btrfs" check "$1" +} + +check_all_images
Signature · 1 line
2.54.0