Skip to content

Commit f5d4e04

Browse files
konisakpm00
authored andcommitted
nilfs2: fix use-after-free of timer for log writer thread
Patch series "nilfs2: fix log writer related issues". This bug fix series covers three nilfs2 log writer-related issues, including a timer use-after-free issue and potential deadlock issue on unmount, and a potential freeze issue in event synchronization found during their analysis. Details are described in each commit log. This patch (of 3): A use-after-free issue has been reported regarding the timer sc_timer on the nilfs_sc_info structure. The problem is that even though it is used to wake up a sleeping log writer thread, sc_timer is not shut down until the nilfs_sc_info structure is about to be freed, and is used regardless of the thread's lifetime. Fix this issue by limiting the use of sc_timer only while the log writer thread is alive. Link: https://lkml.kernel.org/r/20240520132621.4054-1-konishi.ryusuke@gmail.com Link: https://lkml.kernel.org/r/20240520132621.4054-2-konishi.ryusuke@gmail.com Fixes: fdce895 ("nilfs2: change sc_timer from a pointer to an embedded one in struct nilfs_sc_info") Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com> Reported-by: "Bai, Shuangpeng" <sjb7183@psu.edu> Closes: https://groups.google.com/g/syzkaller/c/MK_LYqtt8ko/m/8rgdWeseAwAJ Tested-by: Ryusuke Konishi <konishi.ryusuke@gmail.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 1901472 commit f5d4e04

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

fs/nilfs2/segment.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,8 +2118,10 @@ static void nilfs_segctor_start_timer(struct nilfs_sc_info *sci)
21182118
{
21192119
spin_lock(&sci->sc_state_lock);
21202120
if (!(sci->sc_state & NILFS_SEGCTOR_COMMIT)) {
2121-
sci->sc_timer.expires = jiffies + sci->sc_interval;
2122-
add_timer(&sci->sc_timer);
2121+
if (sci->sc_task) {
2122+
sci->sc_timer.expires = jiffies + sci->sc_interval;
2123+
add_timer(&sci->sc_timer);
2124+
}
21232125
sci->sc_state |= NILFS_SEGCTOR_COMMIT;
21242126
}
21252127
spin_unlock(&sci->sc_state_lock);
@@ -2320,10 +2322,21 @@ int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,
23202322
*/
23212323
static void nilfs_segctor_accept(struct nilfs_sc_info *sci)
23222324
{
2325+
bool thread_is_alive;
2326+
23232327
spin_lock(&sci->sc_state_lock);
23242328
sci->sc_seq_accepted = sci->sc_seq_request;
2329+
thread_is_alive = (bool)sci->sc_task;
23252330
spin_unlock(&sci->sc_state_lock);
2326-
del_timer_sync(&sci->sc_timer);
2331+
2332+
/*
2333+
* This function does not race with the log writer thread's
2334+
* termination. Therefore, deleting sc_timer, which should not be
2335+
* done after the log writer thread exits, can be done safely outside
2336+
* the area protected by sc_state_lock.
2337+
*/
2338+
if (thread_is_alive)
2339+
del_timer_sync(&sci->sc_timer);
23272340
}
23282341

23292342
/**
@@ -2349,7 +2362,7 @@ static void nilfs_segctor_notify(struct nilfs_sc_info *sci, int mode, int err)
23492362
sci->sc_flush_request &= ~FLUSH_DAT_BIT;
23502363

23512364
/* re-enable timer if checkpoint creation was not done */
2352-
if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) &&
2365+
if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && sci->sc_task &&
23532366
time_before(jiffies, sci->sc_timer.expires))
23542367
add_timer(&sci->sc_timer);
23552368
}
@@ -2539,6 +2552,7 @@ static int nilfs_segctor_thread(void *arg)
25392552
int timeout = 0;
25402553

25412554
sci->sc_timer_task = current;
2555+
timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0);
25422556

25432557
/* start sync. */
25442558
sci->sc_task = current;
@@ -2606,6 +2620,7 @@ static int nilfs_segctor_thread(void *arg)
26062620
end_thread:
26072621
/* end sync. */
26082622
sci->sc_task = NULL;
2623+
timer_shutdown_sync(&sci->sc_timer);
26092624
wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */
26102625
spin_unlock(&sci->sc_state_lock);
26112626
return 0;
@@ -2669,7 +2684,6 @@ static struct nilfs_sc_info *nilfs_segctor_new(struct super_block *sb,
26692684
INIT_LIST_HEAD(&sci->sc_gc_inodes);
26702685
INIT_LIST_HEAD(&sci->sc_iput_queue);
26712686
INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func);
2672-
timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0);
26732687

26742688
sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT;
26752689
sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ;
@@ -2748,7 +2762,6 @@ static void nilfs_segctor_destroy(struct nilfs_sc_info *sci)
27482762

27492763
down_write(&nilfs->ns_segctor_sem);
27502764

2751-
timer_shutdown_sync(&sci->sc_timer);
27522765
kfree(sci);
27532766
}
27542767

0 commit comments

Comments
 (0)