Skip to content

Commit 6be49d1

Browse files
konisakpm00
authored andcommitted
nilfs2: fix potential UAF of struct nilfs_sc_info in nilfs_segctor_thread()
The finalization of nilfs_segctor_thread() can race with nilfs_segctor_kill_thread() which terminates that thread, potentially causing a use-after-free BUG as KASAN detected. At the end of nilfs_segctor_thread(), it assigns NULL to "sc_task" member of "struct nilfs_sc_info" to indicate the thread has finished, and then notifies nilfs_segctor_kill_thread() of this using waitqueue "sc_wait_task" on the struct nilfs_sc_info. However, here, immediately after the NULL assignment to "sc_task", it is possible that nilfs_segctor_kill_thread() will detect it and return to continue the deallocation, freeing the nilfs_sc_info structure before the thread does the notification. This fixes the issue by protecting the NULL assignment to "sc_task" and its notification, with spinlock "sc_state_lock" of the struct nilfs_sc_info. Since nilfs_segctor_kill_thread() does a final check to see if "sc_task" is NULL with "sc_state_lock" locked, this can eliminate the race. Link: https://lkml.kernel.org/r/[email protected] Reported-by: [email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 618a8a9 commit 6be49d1

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

fs/nilfs2/segment.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -2609,11 +2609,10 @@ static int nilfs_segctor_thread(void *arg)
26092609
goto loop;
26102610

26112611
end_thread:
2612-
spin_unlock(&sci->sc_state_lock);
2613-
26142612
/* end sync. */
26152613
sci->sc_task = NULL;
26162614
wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
2615+
spin_unlock(&sci->sc_state_lock);
26172616
return 0;
26182617
}
26192618

0 commit comments

Comments
 (0)