From nobody Sun Feb 8 13:27:26 2026 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C3E02A1BA for ; Fri, 26 Dec 2025 14:20:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766758848; cv=none; b=HFdZDhFTc7AlS6Zwb673Vyf3SGdam06w+vPeXDQqeBBNwS+bGnIZ1tWuaYqhacuU9jlBrHSMUCL9+wrrQk+ZDb+uCLV9LJGtMl9CkmEW7k6CgkxVDF1jlnSJaWeJpKWabLmaDLKIcO3jW/srVUkWUA0D+q4pry+vF7rpidZ8sRU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766758848; c=relaxed/simple; bh=+mJeMr2XsKVFH9PYp0WsfQ4PN86PAJx+g7qhclIz+xc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r97J0mGhAIxjwNyP7E7PGAxquqo4MGrMbWd70XhG7H4DfycSFr/S4p7marK8T6EXduXfmL14K8OGOhrzhRg+11IhTPpt5suR8mvuHoBq7PgleeyX2v5L6YQjJ5w6eWdOfgnK8A5VE4i1utko8c3JRbZX+0MzZr4LwCmVaZtqKak= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=MbStL6Mu; arc=none smtp.client-ip=209.85.218.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="MbStL6Mu" Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-b7ce5d6627dso1225019166b.2 for ; Fri, 26 Dec 2025 06:20:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766758844; x=1767363644; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JbYjePTpjmWD5QUf3O7b5Ze3QPROlUswpSL84UsdU88=; b=MbStL6MuT9/2kbRUViky83iJoy7rw3ly5oHRKmKZOYjdzDC0kHGq8TJZHZ+Lebfagl ubwqrjQdWpMH3IoGMD/4HzRwpIYLfOyF8mmJh+BZFUBUPyOjDZf8H2bEyA3VtTLIfBSy Xal3LrS77TTrvs5YbY27BJ2gv2ycQhc6WPB9ucQkgdsrdPM2/53ISxwoju4yURo9ONj7 RNyL3Q8yPXSr5kxVMGnB4kh+e3LJJsZk7CEjFsr9fpoq306H8sbjk05yPakQ/GxDq33X W6omgFtyXqBGPjdrucbQMRIzPFX1gAQS0NwOMV3t30KlE6183ugNWa9+fkkdyvOZ++ZR ZD+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766758844; x=1767363644; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JbYjePTpjmWD5QUf3O7b5Ze3QPROlUswpSL84UsdU88=; b=fLU1rAvmZIoCyIB96AwbDRCfKhNNT6V32BUREmY+lQSoL+yPAHJgVRXGql7GSsPary C6u9wCCRSDzYjcFlNWrq1tUUodQ8GU2j/wpDqANGN2kDF8YnuKoo5EYIGKUEUubMeVmz 7Qm2qcd2F+cIljc3dabGdEwXnU6239vHzJN7F50pc59Q9Qti7pfKSX0EiHSqAV1COnQV dMHQn5ZGkzvg/y8uXVXknVDFTk1U+u0NmojvtkE1EZoJQ3cJOifcscy7e8TuE74Iioyo RSlCSPdFsyPSY/oRZERnT28FOHJuHiGYUky9rq409i8yohmkZ46FwhJBVOl5ivWt/B9m JI6w== X-Forwarded-Encrypted: i=1; AJvYcCW2ZzJWnKDkRe8wiaPTb+iB0B712CHtuN2GsNZ/DhzHzf7HnMhdIuaD7Ng/lEUzFLqIdC+aUf/4xUX8yy0=@vger.kernel.org X-Gm-Message-State: AOJu0YxcL0thSYq32EdWa0VBQQ1QDpsfkDTWPK5PKRjun5BiVMrgyTmI s8uc5mGS6ERHjQuWnz4+CVfVDCf3JNR4aGdSb8pNKDIVmrSa+lF6EPZE X-Gm-Gg: AY/fxX4VyjSvsU8VqlvSmYaEAmgTSnRTJu2WQwnsbG7KovaX/U4QpiHULNyqsmfe0z5 yROmCWgi8ozkZdatKs2hVoRj6007uKGPOWnIP4RNuM5z+1h7/W6VtbxH+P5RSTtYmq/3iNM4qzk A8Ib1FHZB3RqfYOAg9UMYWIBKlqXTwI/2c/30t8BAuaTSJW9VSjyRD5ZN0RPWeOWM4/+TFQJU/P htusfM5kR1rxxjw4OClGj9qWunfo1rJM7NMDN0E8mbd503+/ECB+6eNU6thZOmVAXuy8NKpxAF0 DQQ7Id6U6qnjL1f8xBjdiy/FJR9a6vClHvoLvmZzdV0JYjtfPJ0tK/5PefeVyKTJKwYEcVc1/M5 cjexIzMBCUZIK8wsNXMEwPuU8lQ3QirnEaNpF1UUR/vp6/9S9wPj3ZAKvAxzE2LQj4+aaFQ7h89 MOVVWAWhDv4zgc7g== X-Google-Smtp-Source: AGHT+IG8+HBi/4iyQL7aRggxu4ngBNbdGufAPRhTswtWyIS6MDS3jaUrI3sQRPXi7bfxLEKJqbPnPg== X-Received: by 2002:a17:907:a4b:b0:b7d:1cbb:5deb with SMTP id a640c23a62f3a-b8036f90f16mr2892932866b.27.1766758844151; Fri, 26 Dec 2025 06:20:44 -0800 (PST) Received: from prometheus ([85.11.110.37]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b8037f0cea9sm2332254566b.50.2025.12.26.06.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 Dec 2025 06:20:43 -0800 (PST) From: Szymon Wilczek To: jaegeuk@kernel.org, chao@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, syzbot+b4444e3c972a7a124187@syzkaller.appspotmail.com, Szymon Wilczek Subject: [PATCH v2] f2fs: fix use-after-free in f2fs_write_end_io Date: Fri, 26 Dec 2025 15:20:24 +0100 Message-ID: <20251226142024.48837-1-swilczek.lx@gmail.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20251223162823.23606-1-swilczek.lx@gmail.com> References: <20251223162823.23606-1-swilczek.lx@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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=3Db4444e3c972a7a124187 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; =20 iostat_update_and_unbind_ctx(bio); sbi =3D bio->bi_private; =20 + is_close =3D is_sbi_flag_set(sbi, SBI_IS_CLOSE); + if (time_to_inject(sbi, FAULT_WRITE_IO)) bio->bi_status =3D BLK_STS_IOERR; =20 @@ -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 =3D=3D 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); + } =20 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 =3D NULL; --=20 2.52.0