From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7992E1DA55 for ; Sat, 27 Dec 2025 01:58:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766800701; cv=none; b=kf+O6kYkLUeL1/BWXezTFENOfXm4siCYe0mQrkl62tDi50clkU1iYJyw48yPRQ+buVxvlIn4r6noWTzasqeDhkvlB8rhHRA7RARmvrSC6KpRSRPONIjrgZYLGywyUVzTWi/V6q0I0x625i+UQczYdg46IgFJZWWaO2DUDv6lBAI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766800701; c=relaxed/simple; bh=wVL1FdYC/q35IrTx4f+vuJb7pYG9XVckj8oj5aa4Z8E=; h=Message-ID:Date:MIME-Version:Cc:Subject:To:References:From: In-Reply-To:Content-Type; b=MOZ60l2ceGyUTBVuEPEBmX1x1OhYn6y71jS+TiNdFgB1GgCIRU88zikUTOjbeQ/gfVicJftCc5uBJ66sbMGiH1/bwEK/cf9qYEMjhszDwcVRULVG3et/Us5kq/1H4DWxnYORHmthLwi1whLDrBrbMP50Ki1ie9npDxcbVJ/bBho= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=um36jg/m; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="um36jg/m" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4EFCAC4CEF7; Sat, 27 Dec 2025 01:58:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1766800700; bh=wVL1FdYC/q35IrTx4f+vuJb7pYG9XVckj8oj5aa4Z8E=; h=Date:Cc:Subject:To:References:From:In-Reply-To:From; b=um36jg/mbl8xgAHJVgqAo7x2mT2jd9v0VJioD79yOHHAbg34P754RXz+MQ3VK2HbO g/BEH3KG9YhBPYOfX4HUJFqu+80PZ4/1HiMjsXAWuclc/N3EtycLhfPPY+yFst7WFY ghSY5XSWjV1iVPvovky/OoT0OezmH0QVBH1rI7q836BX5n7ctN97DO9FFm2FZKn9Uw km2Ii8VGi+k9QnB8BRCCdTb1u+z3svjqwpst8r1hxFuPF5ebFXAKmGlrAQP0qZAYD7 mvQP2j+3IihEyDfET1G7Kr/oryyUqUm7uBDw7UnJjEdFUX+LPI8g+RUm0qbCjZ0IKN 1QZq/dFUfre2A== Message-ID: <744bc43e-8ef4-440a-9b9e-219620ee6586@kernel.org> Date: Sat, 27 Dec 2025 09:58:19 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Cc: chao@kernel.org, linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, syzbot+b4444e3c972a7a124187@syzkaller.appspotmail.com Subject: Re: [PATCH v2] f2fs: fix use-after-free in f2fs_write_end_io To: Szymon Wilczek , jaegeuk@kernel.org References: <20251223162823.23606-1-swilczek.lx@gmail.com> <20251226142024.48837-1-swilczek.lx@gmail.com> Content-Language: en-US From: Chao Yu In-Reply-To: <20251226142024.48837-1-swilczek.lx@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 12/26/2025 10:20 PM, Szymon Wilczek wrote: > Syzbot reported a slab-use-after-free issue in f2fs_write_end_io(): > > BUG: KASAN: slab-use-after-free in f2fs_write_end_io+0x9b9/0xb60 > Read of size 4 at addr ffff88804357d170 by task kworker/u4:4/45 > > The race condition occurs between the filesystem unmount path > (kill_f2fs_super) and the asynchronous I/O completion handler > (f2fs_write_end_io). > > When unmounting, kill_f2fs_super() frees the sbi structure. However, if > there are pending CP_DATA writes, the f2fs_write_end_io() callback might > still be running in softirq context and attempt to access sbi->cp_wait, > causing a use-after-free. > > To fix this: > > 1. In f2fs_write_end_io(), check SBI_IS_CLOSE flag early and skip the > wake_up() call if the filesystem is shutting down. Move the wake_up > inside the loop for correct synchronization. > > 2. In kill_f2fs_super(), after f2fs_wait_on_all_pages() returns (meaning > the page count is zero), call synchronize_rcu() before kfree(sbi). > Since bio completion callbacks run in softirq context, which is an > implicit RCU read-side critical section, synchronize_rcu() ensures > all in-flight callbacks have completed before we free sbi. > > The combination of these two changes eliminates the UAF window: the > is_close check provides fast-path optimization (skip wake_up when no > one is waiting), while synchronize_rcu() provides the hard guarantee > that no callback is accessing sbi when we free it. > > Reported-by: syzbot+b4444e3c972a7a124187@syzkaller.appspotmail.com > Closes: https://syzkaller.appspot.com/bug?extid=b4444e3c972a7a124187 > Signed-off-by: Szymon Wilczek > --- > v2: Add synchronize_rcu() to wait for softirq bio callbacks to complete, > addressing the race condition pointed out by Chao Yu where sbi could > be freed while f2fs_write_end_io() was still accessing sbi->cp_wait. > --- > fs/f2fs/data.c | 11 ++++++++--- > fs/f2fs/super.c | 2 ++ > 2 files changed, 10 insertions(+), 3 deletions(-) > > diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c > index c30e69392a62..5808d73c2598 100644 > --- a/fs/f2fs/data.c > +++ b/fs/f2fs/data.c > @@ -318,10 +318,13 @@ static void f2fs_write_end_io(struct bio *bio) > { > struct f2fs_sb_info *sbi; > struct folio_iter fi; > + bool is_close; > > iostat_update_and_unbind_ctx(bio); > sbi = bio->bi_private; > > + is_close = is_sbi_flag_set(sbi, SBI_IS_CLOSE); > + > if (time_to_inject(sbi, FAULT_WRITE_IO)) > bio->bi_status = BLK_STS_IOERR; > > @@ -360,10 +363,12 @@ static void f2fs_write_end_io(struct bio *bio) > f2fs_del_fsync_node_entry(sbi, folio); > folio_clear_f2fs_gcing(folio); > folio_end_writeback(folio); > - } > - if (!get_pages(sbi, F2FS_WB_CP_DATA) && > + > + if (!is_close && type == F2FS_WB_CP_DATA && > + !get_pages(sbi, F2FS_WB_CP_DATA) && > wq_has_sleeper(&sbi->cp_wait)) > - wake_up(&sbi->cp_wait); > + wake_up(&sbi->cp_wait); > + } Do we still need above change? due to below change may guarantee sbi won't be released before soft-irq completion? Thanks, > > bio_put(bio); > } > diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c > index c4c225e09dc4..924bc30d08b6 100644 > --- a/fs/f2fs/super.c > +++ b/fs/f2fs/super.c > @@ -5454,6 +5454,8 @@ static void kill_f2fs_super(struct super_block *sb) > kill_block_super(sb); > /* Release block devices last, after fscrypt_destroy_keyring(). */ > if (sbi) { > + f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA); > + synchronize_rcu(); > destroy_device_list(sbi); > kfree(sbi); > sb->s_fs_info = NULL;