f2fs: fix to do sanity check on node footer for non inode dnode
Chao Yu
maintainerPATCH 1/1 f2fs: fix to do sanity check on node footer for non inode dnode
As syzbot reported below:
------------[ cut here ]------------ kernel BUG at fs/f2fs/file.c:1243! Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI CPU: 0 UID: 0 PID: 5354 Comm: syz.0.0 Not tainted 6.17.0-rc1-syzkaller-00211-g90d970cade8e #0 PREEMPT(full) RIP: 0010:f2fs_truncate_hole+0x69e/0x6c0 fs/f2fs/file.c:1243 Call Trace: <TASK> f2fs_punch_hole+0x2db/0x330 fs/f2fs/file.c:1306 f2fs_fallocate+0x546/0x990 fs/f2fs/file.c:2018 vfs_fallocate+0x666/0x7e0 fs/open.c:342 ksys_fallocate fs/open.c:366 [inline] __do_sys_fallocate fs/open.c:371 [inline] __se_sys_fallocate fs/open.c:369 [inline] __x64_sys_fallocate+0xc0/0x110 fs/open.c:369 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f1e65f8ebe9
w/ a fuzzed image, f2fs may encounter panic due to it detects inconsistent truncation range in direct node in f2fs_truncate_hole().
The root cause is: a non-inode dnode may has the same footer.ino and footer.nid, so the dnode will be parsed as an inode, then ADDRS_PER_PAGE() may return wrong blkaddr count which may be 923 typically, by chance, dn.ofs_in_node is equal to 923, then count can be calculated to 0 in below statement, later it will trigger panic w/ f2fs_bug_on(, count == 0 || ...).
count = min(end_offset - dn.ofs_in_node, pg_end - pg_start);
This patch introduces a new node_type NODE_TYPE_NON_INODE, then allowing passing the new_type to sanity_check_node_footer in f2fs_get_node_folio() to detect corruption that a non-inode dnode has the same footer.ino and footer.nid.
Scripts to reproduce:
mkfs.f2fs -f /dev/vdb
mount /dev/vdb /mnt/f2fs
touch /mnt/f2fs/foo
touch /mnt/f2fs/bar
dd if=/dev/zero of=/mnt/f2fs/foo bs=1M count=8 umount /mnt/f2fs
inject.f2fs --node --mb i_nid --nid 4 --idx 0 --val 5 /dev/vdb mount /dev/vdb /mnt/f2fsxfs_io /mnt/f2fs/foo -c "fpunch 6984k 4k"
--- fs/f2fs/f2fs.h | 4 +++- fs/f2fs/gc.c | 4 ++-- fs/f2fs/node.c | 58 +++++++++++++++++++++++++++++++--------------- fs/f2fs/node.h | 1 + fs/f2fs/recovery.c | 2 +- 5 files changed, 46 insertions(+), 23 deletions(-)
fs/f2fs/f2fs.h +3 -1
@@ -3817,6 +3817,7 @@ void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);* node.c */ struct node_info ; +enum node_type ;int f2fs_check_nid_range (struct f2fs_sb_info *sbi,nid_t nid);bool f2fs_available_free_memory (struct f2fs_sb_info *sbi,int type);
@@ -3839,7 +3840,8 @@ int f2fs_remove_inode_page(struct inode *inode);struct folio *f2fs_new_inode_folio (struct inode *inode);struct folio *f2fs_new_node_folio (struct dnode_of_data *dn,unsigned int ofs);void f2fs_ra_node_page (struct f2fs_sb_info *sbi,nid_t nid); -struct folio *f2fs_get_node_folio (struct f2fs_sb_info *sbi,pgoff_t nid); +struct folio *f2fs_get_node_folio (struct f2fs_sb_info *sbi,pgoff_t nid, +enum node_type node_type);struct folio *f2fs_get_inode_folio (struct f2fs_sb_info *sbi,pgoff_t ino);struct folio *f2fs_get_xnode_folio (struct f2fs_sb_info *sbi,pgoff_t xnid);int f2fs_move_node_folio (struct folio *node_folio,int gc_type);
fs/f2fs/gc.c +2 -2
@@ -1071,7 +1071,7 @@ static int gc_node_segment(struct f2fs_sb_info *sbi, }/* phase == 2 */ - node_folio =f2fs_get_node_folio (sbi, nid); + node_folio =f2fs_get_node_folio (sbi, nid,NODE_TYPE_REGULAR );if (IS_ERR (node_folio))continue ;
@@ -1145,7 +1145,7 @@ static bool is_alive(struct f2fs_sb_info *sbi, struct f2fs_summary *sum, nid =le32_to_cpu (sum->nid); ofs_in_node =le16_to_cpu (sum->ofs_in_node); - node_folio =f2fs_get_node_folio (sbi, nid); + node_folio =f2fs_get_node_folio (sbi, nid,NODE_TYPE_REGULAR );if (IS_ERR (node_folio))return false;
fs/f2fs/node.c +39 -19
97 lines not on this page · raw message
fs/f2fs/node.h +1 -0
@@ -57,6 +57,7 @@ enum node_type {NODE_TYPE_REGULAR ,NODE_TYPE_INODE ,NODE_TYPE_XATTR , + NODE_TYPE_NON_INODE, };/*
fs/f2fs/recovery.c +1 -1
@@ -548,7 +548,7 @@ static int check_index_in_prev_nodes(struct f2fs_sb_info *sbi, }/* Get the node page */ - node_folio =f2fs_get_node_folio (sbi, nid); + node_folio =f2fs_get_node_folio (sbi, nid,NODE_TYPE_REGULAR );if (IS_ERR (node_folio))return PTR_ERR (node_folio);
Signature · 1 line
2.40.1
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:
Hello:
This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:
Quoted 21 lines · On Sat, 23 Aug 2025 13:45:34 +0800 you wrote:
As syzbot reported below:
------------[ cut here ]------------ kernel BUG at fs/f2fs/file.c:1243! Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI CPU: 0 UID: 0 PID: 5354 Comm: syz.0.0 Not tainted 6.17.0-rc1-syzkaller-00211-g90d970cade8e #0 PREEMPT(full) RIP: 0010:f2fs_truncate_hole+0x69e/0x6c0 fs/f2fs/file.c:1243 Call Trace: <TASK> f2fs_punch_hole+0x2db/0x330 fs/f2fs/file.c:1306 f2fs_fallocate+0x546/0x990 fs/f2fs/file.c:2018 vfs_fallocate+0x666/0x7e0 fs/open.c:342 ksys_fallocate fs/open.c:366 [inline] __do_sys_fallocate fs/open.c:371 [inline] __se_sys_fallocate fs/open.c:369 [inline] __x64_sys_fallocate+0xc0/0x110 fs/open.c:369 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f1e65f8ebe9
Quoted 1 line
[...]
Here is the summary with links:
- [f2fs-dev] f2fs: fix to do sanity check on node footer for non inode dnode
https://git.kernel.org/jaegeuk/f2fs/c/c18ecd99e0c7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html