f2fs: fix to avoid panic in dec_valid_block_count()
PATCH 1/13 f2fs: fix to avoid panic in dec_valid_block_count()
As Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203209
- Overview
When mounting the attached crafted image and running program, I got this error. Additionally, it hangs on sync after the this script.
The image is intentionally fuzzed from a normal f2fs image for testing and I enabled option CONFIG_F2FS_CHECK_FS on.
- Reproduces
cc poc_01.c
./run.sh f2fs
sync
kernel BUG at fs/f2fs/f2fs.h:1788!
RIP: 0010:f2fs_truncate_data_blocks_range+0x342/0x350 Call Trace: f2fs_truncate_blocks+0x36d/0x3c0 f2fs_truncate+0x88/0x110 f2fs_setattr+0x3e1/0x460 notify_change+0x2da/0x400 do_truncate+0x6d/0xb0 do_sys_ftruncate+0xf1/0x160 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9
The reason is dec_valid_block_count() will trigger kernel panic due to inconsistent count in between inode.i_blocks and actual block.
To avoid panic, let's just print debug message and set SBI_NEED_FSCK to give a hint to fsck for latter repairing.
--- fs/f2fs/f2fs.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
fs/f2fs/f2fs.h +8 -2
@@ -1790,6 +1790,7 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,return -ENOSPC ; } +void f2fs_msg (struct super_block *sb,const char *level,const char *fmt, ...);static inline void dec_valid_block_count (struct f2fs_sb_info *sbi,struct inode *inode,block_t count)
@@ -1798,13 +1799,19 @@ static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,spin_lock (&sbi->stat_lock);f2fs_bug_on (sbi, sbi->total_valid_block_count < (block_t ) count); -f2fs_bug_on (sbi, inode->i_blocks < sectors); sbi->total_valid_block_count -= (block_t )count;if (sbi->reserved_blocks && sbi->current_reserved_blocks < sbi->reserved_blocks) sbi->current_reserved_blocks =min (sbi->reserved_blocks, sbi->current_reserved_blocks + count);spin_unlock (&sbi->stat_lock); +if (inode->i_blocks < sectors) { +f2fs_msg (sbi->sb,KERN_WARNING , +"Inconsistent i_blocks, ino:%lu, iblocks:%zu, sectors:%zu" , + inode->i_ino, inode->i_blocks, sectors); +set_sbi_flag (sbi,SBI_NEED_FSCK ); +return ; + }f2fs_i_blocks_write (inode, count, false, true); }
@@ -2819,7 +2826,6 @@ static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,bool f2fs_is_valid_blkaddr (struct f2fs_sb_info *sbi,block_t blkaddr,int type); -void f2fs_msg (struct super_block *sb,const char *level,const char *fmt, ...);static inline void verify_blkaddr (struct f2fs_sb_info *sbi,block_t blkaddr,int type) {
Signature · 1 line
2.18.0.rc1
PATCH 2/13 f2fs: fix to avoid panic in dec_valid_node_count()
As Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203213
- Overview
When mounting the attached crafted image and running program, I got this error. Additionally, it hangs on sync after running the this script.
The image is intentionally fuzzed from a normal f2fs image for testing and I enabled option CONFIG_F2FS_CHECK_FS on.
- Reproduces
mkdir test
mount -t f2fs tmp.img test
cp a.out test
cd test
sudo ./a.out
sync
kernel BUG at fs/f2fs/f2fs.h:2012!
RIP: 0010:truncate_node+0x2c9/0x2e0 Call Trace: f2fs_truncate_xattr_node+0xa1/0x130 f2fs_remove_inode_page+0x82/0x2d0 f2fs_evict_inode+0x2a3/0x3a0 evict+0xba/0x180 __dentry_kill+0xbe/0x160 dentry_kill+0x46/0x180 dput+0xbb/0x100 do_renameat2+0x3c9/0x550 __x64_sys_rename+0x17/0x20 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9
The reason is dec_valid_node_count() will trigger kernel panic due to inconsistent count in between inode.i_blocks and actual block.
To avoid panic, let's just print debug message and set SBI_NEED_FSCK to give a hint to fsck for latter repairing.
--- fs/f2fs/f2fs.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
fs/f2fs/f2fs.h +10 -3
@@ -2028,7 +2028,6 @@ static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,f2fs_bug_on (sbi, !sbi->total_valid_block_count);f2fs_bug_on (sbi, !sbi->total_valid_node_count); -f2fs_bug_on (sbi, !is_inode && !inode->i_blocks); sbi->total_valid_node_count--; sbi->total_valid_block_count--;
@@ -2038,10 +2037,18 @@ static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,spin_unlock (&sbi->stat_lock); -if (is_inode) +if (is_inode) {dquot_free_inode (inode); -else + }else { +if (!inode->i_blocks) { +f2fs_msg (sbi->sb,KERN_WARNING , +"Inconsistent i_blocks, ino:%lu, iblocks:%zu" , + inode->i_ino, inode->i_blocks); +set_sbi_flag (sbi,SBI_NEED_FSCK ); +return ; + }f2fs_i_blocks_write (inode,1 , false, true); + } }static inline unsigned int valid_node_count (struct f2fs_sb_info *sbi)
Signature · 1 line
2.18.0.rc1
PATCH 3/13 f2fs: remove new blank line of f2fs kernel message
In reply to Chao YuJust removing '\n' in f2fs_msg(, "\n") to avoid redundant new blank line.
--- fs/f2fs/file.c | 4 ++-- fs/f2fs/super.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-)
fs/f2fs/file.c +2 -2
@@ -2587,7 +2587,7 @@ static int f2fs_ioc_flush_device(struct file *filp, unsigned long arg)if (!f2fs_is_multi_device (sbi) || sbi->s_ndevs -1 <= range.dev_num ||__is_large_section (sbi)) {f2fs_msg (sbi->sb,KERN_WARNING , -"Can't flush %u in %d for segs_per_sec %u != 1\n" , +"Can't flush %u in %d for segs_per_sec %u != 1" , range.dev_num, sbi->s_ndevs, sbi->segs_per_sec);return -EINVAL ;
@@ -2869,7 +2869,7 @@ int f2fs_pin_file_control(struct inode *inode, bool inc)if (fi->i_gc_failures[GC_FAILURE_PIN ] > sbi->gc_pin_file_threshold) {f2fs_msg (sbi->sb,KERN_WARNING , -"%s: Enable GC = ino %lx after %x GC trials\n" , +"%s: Enable GC = ino %lx after %x GC trials" , __func__, inode->i_ino, fi->i_gc_failures[GC_FAILURE_PIN ]);clear_inode_flag (inode,FI_PIN_FILE );
fs/f2fs/super.c +4 -4
@@ -2454,7 +2454,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,/* Currently, support only 4KB page cache size */ if (F2FS_BLKSIZE !=PAGE_SIZE ) {f2fs_msg (sb,KERN_INFO , -"Invalid page_cache_size (%lu), supports only 4KB\n" , +"Invalid page_cache_size (%lu), supports only 4KB" ,PAGE_SIZE );return 1 ; }
@@ -2463,7 +2463,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi, blocksize =1 <<le32_to_cpu (raw_super->log_blocksize);if (blocksize !=F2FS_BLKSIZE ) {f2fs_msg (sb,KERN_INFO , -"Invalid blocksize (%u), supports only 4KB\n" , +"Invalid blocksize (%u), supports only 4KB" , blocksize);return 1 ; }
@@ -2471,7 +2471,7 @@ static int sanity_check_raw_super(struct f2fs_sb_info *sbi,/* check log blocks per segment */ if (le32_to_cpu (raw_super->log_blocks_per_seg) !=9 ) {f2fs_msg (sb,KERN_INFO , -"Invalid log blocks per segment (%u)\n" , +"Invalid log blocks per segment (%u)" ,le32_to_cpu (raw_super->log_blocks_per_seg));return 1 ; }
@@ -3113,7 +3113,7 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)#ifndef CONFIG_BLK_DEV_ZONED if (f2fs_sb_has_blkzoned (sbi)) {f2fs_msg (sb,KERN_ERR , -"Zoned block device support is not enabled\n" ); +"Zoned block device support is not enabled" ); err = -EOPNOTSUPP ; goto free_sb_buf; }
Signature · 1 line
2.18.0.rc1
PATCH 4/13 f2fs: fix to clear dirty inode in error path of f2fs_iget()
In reply to Chao YuAs Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203217
- Overview
When mounting the attached crafted image and running program, I got this error. Additionally, it hangs on sync after running the program.
The image is intentionally fuzzed from a normal f2fs image for testing and I enabled option CONFIG_F2FS_CHECK_FS on.
- Reproduces
cc poc_test_05.c
mkdir test
mount -t f2fs tmp.img test
sudo ./a.out
sync
- Messages
kernel BUG at fs/f2fs/inode.c:707!
RIP: 0010:f2fs_evict_inode+0x33f/0x3a0 Call Trace: evict+0xba/0x180 f2fs_iget+0x598/0xdf0 f2fs_lookup+0x136/0x320 __lookup_slow+0x92/0x140 lookup_slow+0x30/0x50 walk_component+0x1c1/0x350 path_lookupat+0x62/0x200 filename_lookup+0xb3/0x1a0 do_readlinkat+0x56/0x110 __x64_sys_readlink+0x16/0x20 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9
During inode loading, __recover_inline_status() can recovery inode status and set inode dirty, once we failed in following process, it will fail the check in f2fs_evict_inode, result in trigger BUG_ON().
Let's clear dirty inode in error path of f2fs_iget() to avoid panic.
--- fs/f2fs/inode.c | 1 + 1 file changed, 1 insertion(+)
fs/f2fs/inode.c +1 -0
@@ -489,6 +489,7 @@ struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)return inode; bad_inode: +f2fs_inode_synced (inode);iget_failed (inode);trace_f2fs_iget_exit (inode, ret);return ERR_PTR (ret);
Signature · 1 line
2.18.0.rc1
PATCH 5/13 f2fs: fix to avoid panic in f2fs_remove_inode_page()
In reply to Chao YuAs Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203219
- Overview
When mounting the attached crafted image and running program, I got this error. Additionally, it hangs on sync after running the program.
The image is intentionally fuzzed from a normal f2fs image for testing and I enabled option CONFIG_F2FS_CHECK_FS on.
- Reproduces
cc poc_06.c
mkdir test
mount -t f2fs tmp.img test
cp a.out test
cd test
sudo ./a.out
sync
- Messages
kernel BUG at fs/f2fs/node.c:1183!
RIP: 0010:f2fs_remove_inode_page+0x294/0x2d0 Call Trace: f2fs_evict_inode+0x2a3/0x3a0 evict+0xba/0x180 __dentry_kill+0xbe/0x160 dentry_kill+0x46/0x180 dput+0xbb/0x100 do_renameat2+0x3c9/0x550 __x64_sys_rename+0x17/0x20 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9
The reason is f2fs_remove_inode_page() will trigger kernel panic due to inconsistent i_blocks value of inode.
To avoid panic, let's just print debug message and set SBI_NEED_FSCK to give a hint to fsck for latter repairing of potential image corruption.
--- fs/f2fs/node.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
fs/f2fs/node.c +7 -2
@@ -1185,8 +1185,13 @@ int f2fs_remove_inode_page(struct inode *inode)f2fs_put_dnode (&dn);return -EIO ; } -f2fs_bug_on (F2FS_I_SB (inode), - inode->i_blocks !=0 && inode->i_blocks !=8 ); + +if (inode->i_blocks !=0 && inode->i_blocks !=8 ) { +f2fs_msg (F2FS_I_SB (inode)->sb,KERN_WARNING , +"Inconsistent i_blocks, ino:%lu, iblocks:%zu" , + inode->i_ino, inode->i_blocks); +set_sbi_flag (F2FS_I_SB (inode),SBI_NEED_FSCK ); + }/* will put inode & node pages */ err =truncate_node (&dn);
Signature · 1 line
2.18.0.rc1
PATCH 6/13 f2fs: fix to do checksum even if inode page is uptodate
In reply to Chao YuAs Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203221
- Overview
When mounting the attached crafted image and running program, this error is reported.
The image is intentionally fuzzed from a normal f2fs image for testing and I enabled option CONFIG_F2FS_CHECK_FS on.
- Reproduces
cc poc_07.c
mkdir test
mount -t f2fs tmp.img test
cp a.out test
cd test
sudo ./a.out
- Messages
kernel BUG at fs/f2fs/node.c:1279!
RIP: 0010:read_node_page+0xcf/0xf0 Call Trace: __get_node_page+0x6b/0x2f0 f2fs_iget+0x8f/0xdf0 f2fs_lookup+0x136/0x320 __lookup_slow+0x92/0x140 lookup_slow+0x30/0x50 walk_component+0x1c1/0x350 path_lookupat+0x62/0x200 filename_lookup+0xb3/0x1a0 do_fchmodat+0x3e/0xa0 __x64_sys_chmod+0x12/0x20 do_syscall_64+0x43/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9
On below paths, we can have opportunity to readahead inode page
- gc_node_segment -> f2fs_ra_node_page
- gc_data_segment -> f2fs_ra_node_page
- f2fs_fill_dentries -> f2fs_ra_node_page
Unlike synchronized read, on readahead path, we can set page uptodate before verifying page's checksum, then read_node_page() will trigger kernel panic once it encounters a uptodated page w/ incorrect checksum.
So considering readahead scenario, we have to do checksum each time when loading inode page even if it is uptodated.
--- fs/f2fs/inode.c | 4 ++-- fs/f2fs/node.c | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-)
fs/f2fs/inode.c +2 -2
@@ -177,8 +177,8 @@ bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)if (provided != calculated)f2fs_msg (sbi->sb,KERN_WARNING , -"checksum invalid, ino = %x, %x vs. %x" , -ino_of_node (page), provided, calculated); +"checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x" , + page->index,ino_of_node (page), provided, calculated);return provided == calculated; }
fs/f2fs/node.c +4 -3
@@ -1286,9 +1286,10 @@ static int read_node_page(struct page *page, int op_flags)int err;if (PageUptodate (page)) { -#ifdef CONFIG_F2FS_CHECK_FS -f2fs_bug_on (sbi, !f2fs_inode_chksum_verify (sbi, page)); -#endif +if (!f2fs_inode_chksum_verify (sbi, page)) { +ClearPageUptodate (page); +return -EBADMSG ; + }return LOCKED_PAGE ; }
Signature · 1 line
2.18.0.rc1
PATCH 7/13 f2fs: fix to do sanity check on free nid
In reply to Chao YuAs Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203225
- Overview
When mounting the attached crafted image and unmounting it, following errors are reported. Additionally, it hangs on sync after unmounting.
The image is intentionally fuzzed from a normal f2fs image for testing. Compile options for F2FS are as follows.CONFIG_F2FS_FS=yCONFIG_F2FS_STAT_FS=yCONFIG_F2FS_FS_XATTR=yCONFIG_F2FS_FS_POSIX_ACL=yCONFIG_F2FS_CHECK_FS=y
- Reproduces
mkdir test
mount -t f2fs tmp.img test
touch test/t
umount test
sync
- Messages
kernel BUG at fs/f2fs/node.c:3073!
RIP: 0010:f2fs_destroy_node_manager+0x2f0/0x300 Call Trace: f2fs_put_super+0xf4/0x270 generic_shutdown_super+0x62/0x110 kill_block_super+0x1c/0x50 kill_f2fs_super+0xad/0xd0 deactivate_locked_super+0x35/0x60 cleanup_mnt+0x36/0x70 task_work_run+0x75/0x90 exit_to_usermode_loop+0x93/0xa0 do_syscall_64+0xba/0xf0 entry_SYSCALL_64_after_hwframe+0x44/0xa9 RIP: 0010:f2fs_destroy_node_manager+0x2f0/0x300
NAT table is corrupted, so reserved meta/node inode ids were added into free list incorrectly, during file creation, since reserved id has cached in inode hash, so it fails the creation and preallocated nid can not be released later, result in kernel panic.
To fix this issue, let's do nid boundary check during free nid loading.
--- fs/f2fs/node.c | 3 +++ 1 file changed, 3 insertions(+)
fs/f2fs/node.c +3 -0
@@ -2089,6 +2089,9 @@ static bool add_free_nid(struct f2fs_sb_info *sbi,if (unlikely (nid ==0 ))return false; +if (unlikely (f2fs_check_nid_range (sbi, nid))) +return false; + i =f2fs_kmem_cache_alloc (free_nid_slab,GFP_NOFS ); i->nid = nid; i->state =FREE_NID ;
Signature · 1 line
2.18.0.rc1
PATCH 8/13 f2fs: fix to avoid panic in do_recover_data()
In reply to Chao YuAs Jungyeon reported in bugzilla:
https://bugzilla.kernel.org/show_bug.cgi?id=203227
- 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=yCONFIG_F2FS_STAT_FS=yCONFIG_F2FS_FS_XATTR=yCONFIG_F2FS_FS_POSIX_ACL=yCONFIG_F2FS_CHECK_FS=y
- Reproduces
mkdir test
mount -t f2fs tmp.img test
sync
- Messages
kernel BUG at fs/f2fs/recovery.c:549!
RIP: 0010:recover_data+0x167a/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
During recovery, if ofs_of_node is inconsistent in between recovered node page and original checkpointed node page, let's just fail recovery instead of making kernel panic.
--- fs/f2fs/recovery.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
fs/f2fs/recovery.c +9 -1
@@ -551,7 +551,15 @@ static int do_recover_data(struct f2fs_sb_info *sbi, struct inode *inode, goto err;f2fs_bug_on (sbi, ni.ino !=ino_of_node (page)); -f2fs_bug_on (sbi,ofs_of_node (dn.node_page) !=ofs_of_node (page)); + +if (ofs_of_node (dn.node_page) !=ofs_of_node (page)) { +f2fs_msg (sbi->sb,KERN_WARNING , +"Inconsistent ofs_of_node, ino:%lu, ofs:%u, %u" , + inode->i_ino,ofs_of_node (dn.node_page), +ofs_of_node (page)); + err = -EFAULT ; + goto err; + }for (; start < end; start++, dn.ofs_in_node++) {block_t src, dest;
Signature · 1 line
2.18.0.rc1