Skip to content

Commit 7b63f72

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to do sanity check on valid node/block count
As Jungyeon reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=203229 - Overview When mounting the attached crafted image, following errors are reported. Additionally, it hangs on sync after trying to mount it. The image is intentionally fuzzed from a normal f2fs image for testing. Compile options for F2FS are as follows. CONFIG_F2FS_FS=y CONFIG_F2FS_STAT_FS=y CONFIG_F2FS_FS_XATTR=y CONFIG_F2FS_FS_POSIX_ACL=y CONFIG_F2FS_CHECK_FS=y - Reproduces mkdir test mount -t f2fs tmp.img test sync - Kernel message kernel BUG at fs/f2fs/recovery.c:591! RIP: 0010:recover_data+0x12d8/0x1780 Call Trace: f2fs_recover_fsync_data+0x613/0x710 f2fs_fill_super+0x1043/0x1aa0 mount_bdev+0x16d/0x1a0 mount_fs+0x4a/0x170 vfs_kern_mount+0x5d/0x100 do_mount+0x200/0xcf0 ksys_mount+0x79/0xc0 __x64_sys_mount+0x1c/0x20 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9 With corrupted image wihch has out-of-range valid node/block count, during recovery, once we failed due to no free space, it will trigger kernel panic. Adding sanity check on valid node/block count in f2fs_sanity_check_ckpt() to detect such condition, so that potential panic can be avoided. Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent 22d61e2 commit 7b63f72

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

‎fs/f2fs/super.c‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2592,7 +2592,8 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
25922592
unsigned int log_blocks_per_seg;
25932593
unsigned int segment_count_main;
25942594
unsigned int cp_pack_start_sum, cp_payload;
2595-
block_t user_block_count;
2595+
block_t user_block_count, valid_user_blocks;
2596+
block_t avail_node_count, valid_node_count;
25962597
int i, j;
25972598

25982599
total = le32_to_cpu(raw_super->segment_count);
@@ -2627,6 +2628,24 @@ int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
26272628
return 1;
26282629
}
26292630

2631+
valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
2632+
if (valid_user_blocks > user_block_count) {
2633+
f2fs_msg(sbi->sb, KERN_ERR,
2634+
"Wrong valid_user_blocks: %u, user_block_count: %u",
2635+
valid_user_blocks, user_block_count);
2636+
return 1;
2637+
}
2638+
2639+
valid_node_count = le32_to_cpu(ckpt->valid_node_count);
2640+
avail_node_count = sbi->total_node_count - sbi->nquota_files -
2641+
F2FS_RESERVED_NODE_NUM;
2642+
if (valid_node_count > avail_node_count) {
2643+
f2fs_msg(sbi->sb, KERN_ERR,
2644+
"Wrong valid_node_count: %u, avail_node_count: %u",
2645+
valid_node_count, avail_node_count);
2646+
return 1;
2647+
}
2648+
26302649
main_segs = le32_to_cpu(raw_super->segment_count_main);
26312650
blocks_per_seg = sbi->blocks_per_seg;
26322651

0 commit comments

Comments
 (0)