jbd2: Fix use after free in kjournald2()
PATCH 1/1 jbd2: Fix use after free in kjournald2()
Below is the synchronization issue between unmount and kjournald2 contexts, which results into use after free issue in kjournald2(). Fix this issue by using journal->j_state_lock to synchronize the wait_event() done in journal_kill_thread() and the wake_up() done in kjournald2().
TASK 1:
umount cmd:
|--jbd2_journal_destroy() {
|--journal_kill_thread() {
write_lock(&journal->j_state_lock);
journal->j_flags |= JBD2_UNMOUNT;
...
write_unlock(&journal->j_state_lock);
wake_up(&journal->j_wait_commit); TASK 2 wakes up here:
kjournald2() {
...
checks JBD2_UNMOUNT flag and calls goto end-loop;
...
end_loop:
write_unlock(&journal->j_state_lock);
journal->j_task = NULL; --> If this thread gets
pre-empted here, then TASK 1 wait_event will
exit even before this thread is completely
done.
wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
...
write_lock(&journal->j_state_lock);
write_unlock(&journal->j_state_lock);
}
|--kfree(journal);
}
}
wake_up(&journal->j_wait_done_commit); --> this step
now results into use after free issue.
}--- fs/jbd2/journal.c | 2 ++ 1 file changed, 2 insertions(+)
fs/jbd2/journal.c +2 -0
@@ -278,9 +278,11 @@ static int kjournald2(void *arg) end_loop:write_unlock (&journal->j_state_lock);del_timer_sync (&journal->j_commit_timer); +write_lock (&journal->j_state_lock); journal->j_task =NULL ;wake_up (&journal->j_wait_done_commit);jbd_debug (1 ,"Journal thread exiting.\n" ); +write_unlock (&journal->j_state_lock);return 0 ; }
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Quoted 37 lines · Sahitya Tummala
Below is the synchronization issue between unmount and kjournald2 contexts, which results into use after free issue in
kjournald2(). Fix this issue by usingjournal->j_state_lockto synchronize thewait_event()done injournal_kill_thread()and thewake_up()done inkjournald2().TASK 1:
umount cmd:|--jbd2_journal_destroy() { |--journal_kill_thread() { write_lock(&journal->j_state_lock); journal->j_flags |= JBD2_UNMOUNT; ... write_unlock(&journal->j_state_lock); wake_up(&journal->j_wait_commit); TASK 2 wakes up here: kjournald2() { ... checks JBD2_UNMOUNT flag and calls goto end-loop; ... end_loop: write_unlock(&journal->j_state_lock); journal->j_task = NULL; --> If this thread gets pre-empted here, then TASK 1 wait_event will exit even before this thread is completely done. wait_event(journal->j_wait_done_commit, journal->j_task == NULL); ... write_lock(&journal->j_state_lock); write_unlock(&journal->j_state_lock); } |--kfree(journal); } } wake_up(&journal->j_wait_done_commit); --> this step now results into use after free issue. }
Quoted 1 line
Signed-off-by Sahitya Tummala <[email protected]>
Yeah, what you write looks possible (although rather unlikely). Thanks for catching this. One small nit below:
fs/jbd2/journal.c static int kjournald2(void *arg) · line 281
Quoted 9 lines
fs/jbd2/journal.c +1
-0@@ -278,9 +278,11 @@ static int kjournald2(void *arg) end_loop:write_unlock (&journal->j_state_lock);del_timer_sync (&journal->j_commit_timer); +write_lock (&journal->j_state_lock);
There's no good reason to do del_timer_sync() outside of j_state_lock. This is not performance critical code and commit_timeout is trivial and cannot block on anything. So just keep j_state_lock locked upto the place where you unlock it now...
Honza
Quoted 12 lines
fs/jbd2/journal.c +1
-0journal->j_task =NULL ;wake_up (&journal->j_wait_done_commit);jbd_debug (1 ,"Journal thread exiting.\n" ); +write_unlock (&journal->j_state_lock);return 0 ; }Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
Quoted 38 lines · Jan Kara
Quoted 37 lines · Sahitya Tummala
Below is the synchronization issue between unmount and kjournald2 contexts, which results into use after free issue in
kjournald2(). Fix this issue by usingjournal->j_state_lockto synchronize thewait_event()done injournal_kill_thread()and thewake_up()done inkjournald2().TASK 1:
umount cmd:|--jbd2_journal_destroy() { |--journal_kill_thread() { write_lock(&journal->j_state_lock); journal->j_flags |= JBD2_UNMOUNT; ... write_unlock(&journal->j_state_lock); wake_up(&journal->j_wait_commit); TASK 2 wakes up here: kjournald2() { ... checks JBD2_UNMOUNT flag and calls goto end-loop; ... end_loop: write_unlock(&journal->j_state_lock); journal->j_task = NULL; --> If this thread gets pre-empted here, then TASK 1 wait_event will exit even before this thread is completely done. wait_event(journal->j_wait_done_commit, journal->j_task == NULL); ... write_lock(&journal->j_state_lock); write_unlock(&journal->j_state_lock); } |--kfree(journal); } } wake_up(&journal->j_wait_done_commit); --> this step now results into use after free issue. }
Quoted 3 lines
Quoted 1 line
Signed-off-by Sahitya Tummala <[email protected]>Yeah, what you write looks possible (although rather unlikely). Thanks for catching this. One small nit below:
Yes, it was observed only once and is very hard to reproduce.
Quoted 5 lines
Quoted 5 lines
fs/jbd2/journal.c +0
-0@@ -278,9 +278,11 @@ static int kjournald2(void *arg)
Quoted 9 lines
fs/jbd2/journal.c static int kjournald2(void *arg) · line 281
Quoted 4 lines
fs/jbd2/journal.c +1
-0end_loop:write_unlock (&journal->j_state_lock);del_timer_sync (&journal->j_commit_timer); +write_lock (&journal->j_state_lock);There's no good reason to do
del_timer_sync()outside ofj_state_lock. This is not performance critical code andcommit_timeoutis trivial and cannot block on anything. So just keepj_state_locklocked upto the place where you unlock it now...
Sure, I will update the patch.
Quoted 13 lines
Honza
Quoted 12 lines
fs/jbd2/journal.c +1
-0journal->j_task =NULL ;wake_up (&journal->j_wait_done_commit);jbd_debug (1 ,"Journal thread exiting.\n" ); +write_unlock (&journal->j_state_lock);return 0 ; }Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.
Signature · 2 lines
Qualcomm India Private Limited, on behalf of Qualcomm Innovation Center, Inc. Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project.