Skip to contentSkip to conversation

fs: f2fs: fix UAF in f2fs_available_free_memory

PATCH In review Dongliang Mu 2 participants 2 messages Nov 4, 2021 F2FS file system

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 fs: f2fs: fix UAF in f2fs_available_free_memory

if2fs_fill_super
-> f2fs_build_segment_manager
-> create_discard_cmd_control

      -> f2fs_start_discard_thread

It invokes kthread_run to create a thread and run issue_discard_thread.

However, if f2fs_build_node_manager fails, the control flow goes to free_nm and calls f2fs_destroy_node_manager. This function will free sbi->nm_info. However, if issue_discard_thread accesses sbi->nm_info after the deallocation, but before the f2fs_stop_discard_thread, it will cause UAF(Use-after-free).

-> f2fs_destroy_segment_manager
-> destroy_discard_cmd_control

      -> f2fs_stop_discard_thread

Fix this by stopping discard thread before f2fs_destroy_node_manager.

Note that, the commit d6d2b491a82e1 introduces the call of f2fs_available_free_memory into issue_discard_thread.

Fixes d6d2b491a82e ("f2fs: allow to change discard policy based on cached discard cmds")
---
 fs/f2fs/super.c | 2 ++
 1 file changed, 2 insertions(+)
fs/f2fs/super.c +2 -0
index 78ebc306ee2b..dbe040b66802 100644
@@ -4352,6 +4352,8 @@ static int f2fs_fill_super(struct super_block *sb, void *data, int silent)
 free_stats:
 	f2fs_destroy_stats(sbi);
 free_nm:
+	/* stop discard thread before destroying node manager */
+	f2fs_stop_discard_thread(sbi);
 	f2fs_destroy_node_manager(sbi);
 free_sm:
 	f2fs_destroy_segment_manager(sbi);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 17 lines · Dongliang Mu

if2fs_fill_super
-> f2fs_build_segment_manager

    -> create_discard_cmd_control
       -> f2fs_start_discard_thread

It invokes kthread_run to create a thread and run issue_discard_thread.

However, if f2fs_build_node_manager fails, the control flow goes to free_nm and calls f2fs_destroy_node_manager. This function will free sbi->nm_info. However, if issue_discard_thread accesses sbi->nm_info after the deallocation, but before the f2fs_stop_discard_thread, it will cause UAF(Use-after-free).

-> f2fs_destroy_segment_manager

    -> destroy_discard_cmd_control
       -> f2fs_stop_discard_thread
Quoted 7 lines

Fix this by stopping discard thread before f2fs_destroy_node_manager.

Note that, the commit d6d2b491a82e1 introduces the call of f2fs_available_free_memory into issue_discard_thread.

Fixes d6d2b491a82e ("f2fs: allow to change discard policy based on cached discard cmds")

Thanks,