Skip to content

Commit d818983

Browse files
chaseyuJaegeuk Kim
authored andcommitted
f2fs: fix to avoid NULL pointer dereference f2fs_write_end_io()
butt3rflyh4ck reports a bug as below: When a thread always calls F2FS_IOC_RESIZE_FS to resize fs, if resize fs is failed, f2fs kernel thread would invoke callback function to update f2fs io info, it would call f2fs_write_end_io and may trigger null-ptr-deref in NODE_MAPPING. general protection fault, probably for non-canonical address KASAN: null-ptr-deref in range [0x0000000000000030-0x0000000000000037] RIP: 0010:NODE_MAPPING fs/f2fs/f2fs.h:1972 [inline] RIP: 0010:f2fs_write_end_io+0x727/0x1050 fs/f2fs/data.c:370 <TASK> bio_endio+0x5af/0x6c0 block/bio.c:1608 req_bio_endio block/blk-mq.c:761 [inline] blk_update_request+0x5cc/0x1690 block/blk-mq.c:906 blk_mq_end_request+0x59/0x4c0 block/blk-mq.c:1023 lo_complete_rq+0x1c6/0x280 drivers/block/loop.c:370 blk_complete_reqs+0xad/0xe0 block/blk-mq.c:1101 __do_softirq+0x1d4/0x8ef kernel/softirq.c:571 run_ksoftirqd kernel/softirq.c:939 [inline] run_ksoftirqd+0x31/0x60 kernel/softirq.c:931 smpboot_thread_fn+0x659/0x9e0 kernel/smpboot.c:164 kthread+0x33e/0x440 kernel/kthread.c:379 ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:308 The root cause is below race case can cause leaving dirty metadata in f2fs after filesystem is remount as ro: Thread A Thread B - f2fs_ioc_resize_fs - f2fs_readonly --- return false - f2fs_resize_fs - f2fs_remount - write_checkpoint - set f2fs as ro - free_segment_range - update meta_inode's data Then, if f2fs_put_super() fails to write_checkpoint due to readonly status, and meta_inode's dirty data will be writebacked after node_inode is put, finally, f2fs_write_end_io will access NULL pointer on sbi->node_inode. Thread A IRQ context - f2fs_put_super - write_checkpoint fails - iput(node_inode) - node_inode = NULL - iput(meta_inode) - write_inode_now - f2fs_write_meta_page - f2fs_write_end_io - NODE_MAPPING(sbi) : access NULL pointer on node_inode Fixes: b4b1006 ("f2fs: refactor resize_fs to avoid meta updates in progress") Reported-by: butt3rflyh4ck <butterflyhuangxx@gmail.com> Closes: https://lore.kernel.org/r/1684480657-2375-1-git-send-email-yangtiezhu@loongson.cn Tested-by: butt3rflyh4ck <butterflyhuangxx@gmail.com> Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent bfd4766 commit d818983

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

fs/f2fs/f2fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3834,7 +3834,7 @@ void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
38343834
block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
38353835
int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
38363836
void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
3837-
int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count);
3837+
int f2fs_resize_fs(struct file *filp, __u64 block_count);
38383838
int __init f2fs_create_garbage_collection_cache(void);
38393839
void f2fs_destroy_garbage_collection_cache(void);
38403840
/* victim selection function for cleaning and SSR */

fs/f2fs/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3279,7 +3279,7 @@ static int f2fs_ioc_resize_fs(struct file *filp, unsigned long arg)
32793279
sizeof(block_count)))
32803280
return -EFAULT;
32813281

3282-
return f2fs_resize_fs(sbi, block_count);
3282+
return f2fs_resize_fs(filp, block_count);
32833283
}
32843284

32853285
static int f2fs_ioc_enable_verity(struct file *filp, unsigned long arg)

fs/f2fs/gc.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,8 +2105,9 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
21052105
}
21062106
}
21072107

2108-
int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count)
2108+
int f2fs_resize_fs(struct file *filp, __u64 block_count)
21092109
{
2110+
struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
21102111
__u64 old_block_count, shrunk_blocks;
21112112
struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
21122113
unsigned int secs;
@@ -2144,12 +2145,18 @@ int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count)
21442145
return -EINVAL;
21452146
}
21462147

2148+
err = mnt_want_write_file(filp);
2149+
if (err)
2150+
return err;
2151+
21472152
shrunk_blocks = old_block_count - block_count;
21482153
secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
21492154

21502155
/* stop other GC */
2151-
if (!f2fs_down_write_trylock(&sbi->gc_lock))
2152-
return -EAGAIN;
2156+
if (!f2fs_down_write_trylock(&sbi->gc_lock)) {
2157+
err = -EAGAIN;
2158+
goto out_drop_write;
2159+
}
21532160

21542161
/* stop CP to protect MAIN_SEC in free_segment_range */
21552162
f2fs_lock_op(sbi);
@@ -2169,10 +2176,18 @@ int f2fs_resize_fs(struct f2fs_sb_info *sbi, __u64 block_count)
21692176
out_unlock:
21702177
f2fs_unlock_op(sbi);
21712178
f2fs_up_write(&sbi->gc_lock);
2179+
out_drop_write:
2180+
mnt_drop_write_file(filp);
21722181
if (err)
21732182
return err;
21742183

21752184
freeze_super(sbi->sb);
2185+
2186+
if (f2fs_readonly(sbi->sb)) {
2187+
thaw_super(sbi->sb);
2188+
return -EROFS;
2189+
}
2190+
21762191
f2fs_down_write(&sbi->gc_lock);
21772192
f2fs_down_write(&sbi->cp_global_sem);
21782193

0 commit comments

Comments
 (0)