Skip to content

Commit c7f114d

Browse files
chaseyuJaegeuk Kim
authored and
Jaegeuk Kim
committed
f2fs: fix to avoid use-after-free in f2fs_stop_gc_thread()
syzbot reports a f2fs bug as below: __dump_stack lib/dump_stack.c:88 [inline] dump_stack_lvl+0x241/0x360 lib/dump_stack.c:114 print_report+0xe8/0x550 mm/kasan/report.c:491 kasan_report+0x143/0x180 mm/kasan/report.c:601 kasan_check_range+0x282/0x290 mm/kasan/generic.c:189 instrument_atomic_read_write include/linux/instrumented.h:96 [inline] atomic_fetch_add_relaxed include/linux/atomic/atomic-instrumented.h:252 [inline] __refcount_add include/linux/refcount.h:184 [inline] __refcount_inc include/linux/refcount.h:241 [inline] refcount_inc include/linux/refcount.h:258 [inline] get_task_struct include/linux/sched/task.h:118 [inline] kthread_stop+0xca/0x630 kernel/kthread.c:704 f2fs_stop_gc_thread+0x65/0xb0 fs/f2fs/gc.c:210 f2fs_do_shutdown+0x192/0x540 fs/f2fs/file.c:2283 f2fs_ioc_shutdown fs/f2fs/file.c:2325 [inline] __f2fs_ioctl+0x443a/0xbe60 fs/f2fs/file.c:4325 vfs_ioctl fs/ioctl.c:51 [inline] __do_sys_ioctl fs/ioctl.c:907 [inline] __se_sys_ioctl+0xfc/0x170 fs/ioctl.c:893 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xf3/0x230 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f The root cause is below race condition, it may cause use-after-free issue in sbi->gc_th pointer. - remount - f2fs_remount - f2fs_stop_gc_thread - kfree(gc_th) - f2fs_ioc_shutdown - f2fs_do_shutdown - f2fs_stop_gc_thread - kthread_stop(gc_th->f2fs_gc_task) : sbi->gc_thread = NULL; We will call f2fs_do_shutdown() in two paths: - for f2fs_ioc_shutdown() path, we should grab sb->s_umount semaphore for fixing. - for f2fs_shutdown() path, it's safe since caller has already grabbed sb->s_umount semaphore. Reported-by: syzbot+1a8e2b31f2ac9bd3d148@syzkaller.appspotmail.com Closes: https://lore.kernel.org/linux-f2fs-devel/0000000000005c7ccb061e032b9b@google.com Fixes: 7950e9a ("f2fs: stop gc/discard thread after fs shutdown") Signed-off-by: Chao Yu <chao@kernel.org> Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
1 parent ebd3309 commit c7f114d

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

fs/f2fs/f2fs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3508,7 +3508,7 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
35083508
int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
35093509
void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
35103510
int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
3511-
bool readonly);
3511+
bool readonly, bool need_lock);
35123512
int f2fs_precache_extents(struct inode *inode);
35133513
int f2fs_fileattr_get(struct dentry *dentry, struct fileattr *fa);
35143514
int f2fs_fileattr_set(struct mnt_idmap *idmap,

fs/f2fs/file.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -2297,7 +2297,7 @@ static int f2fs_ioc_abort_atomic_write(struct file *filp)
22972297
}
22982298

22992299
int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
2300-
bool readonly)
2300+
bool readonly, bool need_lock)
23012301
{
23022302
struct super_block *sb = sbi->sb;
23032303
int ret = 0;
@@ -2344,12 +2344,19 @@ int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
23442344
if (readonly)
23452345
goto out;
23462346

2347+
/* grab sb->s_umount to avoid racing w/ remount() */
2348+
if (need_lock)
2349+
down_read(&sbi->sb->s_umount);
2350+
23472351
f2fs_stop_gc_thread(sbi);
23482352
f2fs_stop_discard_thread(sbi);
23492353

23502354
f2fs_drop_discard_cmd(sbi);
23512355
clear_opt(sbi, DISCARD);
23522356

2357+
if (need_lock)
2358+
up_read(&sbi->sb->s_umount);
2359+
23532360
f2fs_update_time(sbi, REQ_TIME);
23542361
out:
23552362

@@ -2386,7 +2393,7 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
23862393
}
23872394
}
23882395

2389-
ret = f2fs_do_shutdown(sbi, in, readonly);
2396+
ret = f2fs_do_shutdown(sbi, in, readonly, true);
23902397

23912398
if (need_drop)
23922399
mnt_drop_write_file(filp);

fs/f2fs/super.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -2560,7 +2560,7 @@ static int f2fs_remount(struct super_block *sb, int *flags, char *data)
25602560

25612561
static void f2fs_shutdown(struct super_block *sb)
25622562
{
2563-
f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false);
2563+
f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
25642564
}
25652565

25662566
#ifdef CONFIG_QUOTA

0 commit comments

Comments
 (0)