Skip to contentSkip to conversation

f2fs: fix to avoid use-after-free in f2fs_stop_gc_thread()

PATCH Superseded Chao Yu 2 participants 4 messages Jul 25, 2024 – Jul 29, 2024 F2FS file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Chao Yu

author maintainer
#0 lore raw

PATCH 1/1 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)

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 [email protected]
Fixes 7950e9ac638e ("f2fs: stop gc/discard thread after fs shutdown")
---
 fs/f2fs/file.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/file.c +3 -0
index 7a37f2b393b9..62d72da25754 100644
@@ -2388,7 +2388,10 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
 		}
 	}
 
+	/* grab sb->s_umount to avoid racing w/ remount() */
+	down_read(&sbi->sb->s_umount);
 	ret = f2fs_do_shutdown(sbi, in, readonly);
+	up_read(&sbi->sb->s_umount);
 
 	if (need_drop)
 		mnt_drop_write_file(filp);
Signature · 1 line
2.40.1
maintainer
#1 lore raw
Quoted 56 lines · Chao Yu

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)

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 [email protected]
Fixes 7950e9ac638e ("f2fs: stop gc/discard thread after fs shutdown")
---
 fs/f2fs/file.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/file.c +0 -0
index 7a37f2b393b9..62d72da25754 100644

fs/f2fs/file.c static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) · line 2392

Quoted 5 lines
fs/f2fs/file.c +2 -0
@@ -2388,7 +2388,10 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
 		}
 	}
 
+	/* grab sb->s_umount to avoid racing w/ remount() */
+	down_read(&sbi->sb->s_umount);

Is this safe for freeze_bdev()?

Quoted 7 lines
fs/f2fs/file.c +1 -0
 	ret = f2fs_do_shutdown(sbi, in, readonly);
+	up_read(&sbi->sb->s_umount);
 
 	if (need_drop)
 		mnt_drop_write_file(filp);
Signature · 1 line
2.40.1
maintainer
#2 lore raw
Quoted 60 lines · Jaegeuk Kim
Quoted 59 lines · Chao Yu

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)

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 [email protected]
Fixes 7950e9ac638e ("f2fs: stop gc/discard thread after fs shutdown")
---
 fs/f2fs/file.c | 3 +++
 1 file changed, 3 insertions(+)
fs/f2fs/file.c +0 -0
index 7a37f2b393b9..62d72da25754 100644
@@ -2388,7 +2388,10 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
 		}
 	}
 
Quoted 4 lines

fs/f2fs/file.c static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) · line 2392

Quoted 2 lines
fs/f2fs/file.c +2 -0
+	/* grab sb->s_umount to avoid racing w/ remount() */
+	down_read(&sbi->sb->s_umount);

Is this safe for freeze_bdev()?

bdev_freeze
-> fs_bdev_freeze
-> get_bdev_super
-> bdev_super_lock(bdev, true)

    -> super_lock(sb, true)
     -> __super_lock(sb, true)
      -> down_write(s_umount)
Quoted 8 lines
Quoted 8 lines
fs/f2fs/file.c +1 -0
 	ret = f2fs_do_shutdown(sbi, in, readonly);
+	up_read(&sbi->sb->s_umount);
 
 	if (need_drop)
 		mnt_drop_write_file(filp);
Signature · 1 line
2.40.1
author maintainer
#3 lore raw
Quoted 66 lines · Jaegeuk Kim
Quoted 60 lines · Jaegeuk Kim
Quoted 59 lines · Chao Yu

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)

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 [email protected]
Fixes 7950e9ac638e ("f2fs: stop gc/discard thread after fs shutdown")
---
  fs/f2fs/file.c | 3 +++
  1 file changed, 3 insertions(+)
fs/f2fs/file.c +0 -0
index 7a37f2b393b9..62d72da25754 100644
@@ -2388,7 +2388,10 @@ static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg)
  		}
  	}
 
Quoted 4 lines

fs/f2fs/file.c static int f2fs_ioc_shutdown(struct file *filp, unsigned long arg) · line 2392

Quoted 2 lines
fs/f2fs/file.c +2 -0
+	/* grab sb->s_umount to avoid racing w/ remount() */
+	down_read(&sbi->sb->s_umount);

Is this safe for freeze_bdev()?

Quoted 7 lines

bdev_freeze
-> fs_bdev_freeze
-> get_bdev_super

    -> bdev_super_lock(bdev, true)
     -> super_lock(sb, true)
      -> __super_lock(sb, true)
       -> down_write(s_umount)

Oops, let me check this.

Thanks,

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
fs/f2fs/file.c +1 -0
  	ret = f2fs_do_shutdown(sbi, in, readonly);
+	up_read(&sbi->sb->s_umount);
 
  	if (need_drop)
  		mnt_drop_write_file(filp);
Signature · 1 line
2.40.1