linux/fs/f2fs
Chao Yu 186098f34b f2fs: fix to do sanity check on node footer for non inode dnode
commit c18ecd99e0 upstream.

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/f2fs
xfs_io /mnt/f2fs/foo -c "fpunch 6984k 4k"

Cc: stable@kernel.org
Reported-by: syzbot+b9c7ffd609c3f09416ab@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/linux-f2fs-devel/68a68e27.050a0220.1a3988.0002.GAE@google.com
Signed-off-by: Chao Yu <chao@kernel.org>
Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
History 2025-10-12 13:01:57 +02:00
..
acl.c f2fs: Pass a folio to f2fs_getxattr() 2025-04-28 15:26:42 +00:00
acl.h f2fs: Pass folios to f2fs_init_acl() 2025-04-28 15:26:41 +00:00
checkpoint.c f2fs: Add folio counterparts to page_private_flags functions 2025-07-22 15:57:05 +00:00
compress.c f2fs: Pass a folio to f2fs_cache_compressed_page() 2025-07-22 15:57:50 +00:00
data.c f2fs-for-6.17-rc1 2025-08-04 16:27:21 -07:00
debug.c f2fs: introduce is_cur{seg,sec}() 2025-07-09 18:02:20 +00:00
dir.c f2fs: Remove clear_page_private_all() 2025-07-22 15:58:00 +00:00
extent_cache.c f2fs: Pass a folio to ofs_of_node() 2025-07-22 15:56:53 +00:00
f2fs.h f2fs: fix to do sanity check on node footer for non inode dnode 2025-10-12 13:01:57 +02:00
file.c f2fs-for-6.17-rc1 2025-08-04 16:27:21 -07:00
gc.c f2fs: fix to do sanity check on node footer for non inode dnode 2025-10-12 13:01:57 +02:00
gc.h f2fs: add gc_boost_gc_greedy sysfs node 2025-07-29 15:02:36 +00:00
hash.c f2fs: don't use casefolded comparison for "." and ".." 2022-05-17 11:19:23 -07:00
inline.c f2fs: Add folio counterparts to page_private_flags functions 2025-07-22 15:57:05 +00:00
inode.c f2fs: drop inode from the donation list when the last file is closed 2025-07-30 17:13:12 +00:00
iostat.c f2fs: add async reset zone command support 2023-06-12 13:04:09 -07:00
iostat.h f2fs: use iostat_lat_type directly as a parameter in the iostat_update_and_unbind_ctx() 2023-02-07 10:39:28 -08:00
Kconfig f2fs: switch to using the crc32 library 2024-12-01 17:23:02 -08:00
Makefile f2fs: separate out iostat feature 2021-08-23 10:25:51 -07:00
namei.c f2fs: Use a folio in f2fs_encrypted_get_link() 2025-07-22 15:57:54 +00:00
node.c f2fs: fix to do sanity check on node footer for non inode dnode 2025-10-12 13:01:57 +02:00
node.h f2fs: fix to do sanity check on node footer for non inode dnode 2025-10-12 13:01:57 +02:00
recovery.c f2fs: fix to do sanity check on node footer for non inode dnode 2025-10-12 13:01:57 +02:00
segment.c f2fs: Pass a folio to f2fs_submit_merged_write_cond() 2025-07-22 15:58:05 +00:00
segment.h f2fs: fix to calculate dirty data during has_not_enough_free_secs() 2025-07-28 16:36:42 +00:00
shrinker.c f2fs: handle error cases of memory donation 2025-05-06 15:46:55 +00:00
super.c f2fs: drop inode from the donation list when the last file is closed 2025-07-30 17:13:12 +00:00
sysfs.c f2fs: add gc_boost_gc_greedy sysfs node 2025-07-29 15:02:36 +00:00
verity.c f2fs-6.12-rc1 2024-09-24 15:12:38 -07:00
xattr.c f2fs: Pass a folio to inline_xattr_addr() 2025-04-28 15:26:43 +00:00
xattr.h f2fs: Pass a folio to f2fs_getxattr() 2025-04-28 15:26:42 +00:00