Skip to content

Commit a78a8bc

Browse files
chaseyugregkh
authored andcommitted
f2fs: fix to do sanity check on direct node in truncate_dnode()
commit a6ec837 upstream. syzbot reports below bug: BUG: KASAN: slab-use-after-free in f2fs_truncate_data_blocks_range+0x122a/0x14c0 fs/f2fs/file.c:574 Read of size 4 at addr ffff88802a25c000 by task syz-executor148/5000 CPU: 1 PID: 5000 Comm: syz-executor148 Not tainted 6.4.0-rc7-syzkaller-00041-ge660abd551f1 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/27/2023 Call Trace: <TASK> __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0xd9/0x150 lib/dump_stack.c:106 print_address_description.constprop.0+0x2c/0x3c0 mm/kasan/report.c:351 print_report mm/kasan/report.c:462 [inline] kasan_report+0x11c/0x130 mm/kasan/report.c:572 f2fs_truncate_data_blocks_range+0x122a/0x14c0 fs/f2fs/file.c:574 truncate_dnode+0x229/0x2e0 fs/f2fs/node.c:944 f2fs_truncate_inode_blocks+0x64b/0xde0 fs/f2fs/node.c:1154 f2fs_do_truncate_blocks+0x4ac/0xf30 fs/f2fs/file.c:721 f2fs_truncate_blocks+0x7b/0x300 fs/f2fs/file.c:749 f2fs_truncate.part.0+0x4a5/0x630 fs/f2fs/file.c:799 f2fs_truncate include/linux/fs.h:825 [inline] f2fs_setattr+0x1738/0x2090 fs/f2fs/file.c:1006 notify_change+0xb2c/0x1180 fs/attr.c:483 do_truncate+0x143/0x200 fs/open.c:66 handle_truncate fs/namei.c:3295 [inline] do_open fs/namei.c:3640 [inline] path_openat+0x2083/0x2750 fs/namei.c:3791 do_filp_open+0x1ba/0x410 fs/namei.c:3818 do_sys_openat2+0x16d/0x4c0 fs/open.c:1356 do_sys_open fs/open.c:1372 [inline] __do_sys_creat fs/open.c:1448 [inline] __se_sys_creat fs/open.c:1442 [inline] __x64_sys_creat+0xcd/0x120 fs/open.c:1442 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x39/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x63/0xcd The root cause is, inodeA references inodeB via inodeB's ino, once inodeA is truncated, it calls truncate_dnode() to truncate data blocks in inodeB's node page, it traverse mapping data from node->i.i_addr[0] to node->i.i_addr[ADDRS_PER_BLOCK() - 1], result in out-of-boundary access. This patch fixes to add sanity check on dnode page in truncate_dnode(), so that, it can help to avoid triggering such issue, and once it encounters such issue, it will record newly introduced ERROR_INVALID_NODE_REFERENCE error into superblock, later fsck can detect such issue and try repairing. Also, it removes f2fs_truncate_data_blocks() for cleanup due to the function has only one caller, and uses f2fs_truncate_data_blocks_range() instead. Reported-and-tested-by: [email protected] Closes: https://lore.kernel.org/linux-f2fs-devel/[email protected] Signed-off-by: Chao Yu <[email protected]> Signed-off-by: Jaegeuk Kim <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 23e7223 commit a78a8bc

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

fs/f2fs/f2fs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3431,7 +3431,6 @@ static inline bool __is_valid_data_blkaddr(block_t blkaddr)
34313431
* file.c
34323432
*/
34333433
int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
3434-
void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
34353434
int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
34363435
int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
34373436
int f2fs_truncate(struct inode *inode);

fs/f2fs/file.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,11 +628,6 @@ void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count)
628628
dn->ofs_in_node, nr_free);
629629
}
630630

631-
void f2fs_truncate_data_blocks(struct dnode_of_data *dn)
632-
{
633-
f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
634-
}
635-
636631
static int truncate_partial_data_page(struct inode *inode, u64 from,
637632
bool cache_only)
638633
{

fs/f2fs/node.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -923,23 +923,33 @@ static int truncate_node(struct dnode_of_data *dn)
923923

924924
static int truncate_dnode(struct dnode_of_data *dn)
925925
{
926+
struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode);
926927
struct page *page;
927928
int err;
928929

929930
if (dn->nid == 0)
930931
return 1;
931932

932933
/* get direct node */
933-
page = f2fs_get_node_page(F2FS_I_SB(dn->inode), dn->nid);
934+
page = f2fs_get_node_page(sbi, dn->nid);
934935
if (PTR_ERR(page) == -ENOENT)
935936
return 1;
936937
else if (IS_ERR(page))
937938
return PTR_ERR(page);
938939

940+
if (IS_INODE(page) || ino_of_node(page) != dn->inode->i_ino) {
941+
f2fs_err(sbi, "incorrect node reference, ino: %lu, nid: %u, ino_of_node: %u",
942+
dn->inode->i_ino, dn->nid, ino_of_node(page));
943+
set_sbi_flag(sbi, SBI_NEED_FSCK);
944+
f2fs_handle_error(sbi, ERROR_INVALID_NODE_REFERENCE);
945+
f2fs_put_page(page, 1);
946+
return -EFSCORRUPTED;
947+
}
948+
939949
/* Make dnode_of_data for parameter */
940950
dn->node_page = page;
941951
dn->ofs_in_node = 0;
942-
f2fs_truncate_data_blocks(dn);
952+
f2fs_truncate_data_blocks_range(dn, ADDRS_PER_BLOCK(dn->inode));
943953
err = truncate_node(dn);
944954
if (err) {
945955
f2fs_put_page(page, 1);

include/linux/f2fs_fs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ enum f2fs_error {
104104
ERROR_INCONSISTENT_SIT,
105105
ERROR_CORRUPTED_VERITY_XATTR,
106106
ERROR_CORRUPTED_XATTR,
107+
ERROR_INVALID_NODE_REFERENCE,
107108
ERROR_MAX,
108109
};
109110

0 commit comments

Comments
 (0)