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: fdce895ea5dd ("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>

authored by Ryusuke Konishi and committed by Andrew Morton f5d4e046 1901472f

+19 -6
+19 -6
fs/nilfs2/segment.c
··· 2118 2118 { 2119 2119 spin_lock(&sci->sc_state_lock); 2120 2120 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 + } 2123 2125 sci->sc_state |= NILFS_SEGCTOR_COMMIT; 2124 2126 } 2125 2127 spin_unlock(&sci->sc_state_lock); ··· 2322 2320 */ 2323 2321 static void nilfs_segctor_accept(struct nilfs_sc_info *sci) 2324 2322 { 2323 + bool thread_is_alive; 2324 + 2325 2325 spin_lock(&sci->sc_state_lock); 2326 2326 sci->sc_seq_accepted = sci->sc_seq_request; 2327 + thread_is_alive = (bool)sci->sc_task; 2327 2328 spin_unlock(&sci->sc_state_lock); 2328 - del_timer_sync(&sci->sc_timer); 2329 + 2330 + /* 2331 + * This function does not race with the log writer thread's 2332 + * termination. Therefore, deleting sc_timer, which should not be 2333 + * done after the log writer thread exits, can be done safely outside 2334 + * the area protected by sc_state_lock. 2335 + */ 2336 + if (thread_is_alive) 2337 + del_timer_sync(&sci->sc_timer); 2329 2338 } 2330 2339 2331 2340 /** ··· 2362 2349 sci->sc_flush_request &= ~FLUSH_DAT_BIT; 2363 2350 2364 2351 /* re-enable timer if checkpoint creation was not done */ 2365 - if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && 2352 + if ((sci->sc_state & NILFS_SEGCTOR_COMMIT) && sci->sc_task && 2366 2353 time_before(jiffies, sci->sc_timer.expires)) 2367 2354 add_timer(&sci->sc_timer); 2368 2355 } ··· 2552 2539 int timeout = 0; 2553 2540 2554 2541 sci->sc_timer_task = current; 2542 + timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0); 2555 2543 2556 2544 /* start sync. */ 2557 2545 sci->sc_task = current; ··· 2620 2606 end_thread: 2621 2607 /* end sync. */ 2622 2608 sci->sc_task = NULL; 2609 + timer_shutdown_sync(&sci->sc_timer); 2623 2610 wake_up(&sci->sc_wait_task); /* for nilfs_segctor_kill_thread() */ 2624 2611 spin_unlock(&sci->sc_state_lock); 2625 2612 return 0; ··· 2684 2669 INIT_LIST_HEAD(&sci->sc_gc_inodes); 2685 2670 INIT_LIST_HEAD(&sci->sc_iput_queue); 2686 2671 INIT_WORK(&sci->sc_iput_work, nilfs_iput_work_func); 2687 - timer_setup(&sci->sc_timer, nilfs_construction_timeout, 0); 2688 2672 2689 2673 sci->sc_interval = HZ * NILFS_SC_DEFAULT_TIMEOUT; 2690 2674 sci->sc_mjcp_freq = HZ * NILFS_SC_DEFAULT_SR_FREQ; ··· 2762 2748 2763 2749 down_write(&nilfs->ns_segctor_sem); 2764 2750 2765 - timer_shutdown_sync(&sci->sc_timer); 2766 2751 kfree(sci); 2767 2752 } 2768 2753