From patchwork Wed Feb 12 07:52:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chao Yu X-Patchwork-Id: 13971308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 15C70C02198 for ; Wed, 12 Feb 2025 07:53:03 +0000 (UTC) Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.95) (envelope-from ) id 1ti7YL-0001VS-Pn; Wed, 12 Feb 2025 07:53:02 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1ti7YK-0001VD-HT for linux-f2fs-devel@lists.sourceforge.net; Wed, 12 Feb 2025 07:53:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=bJOGZZH/vMzxETF1c58XYg6DXIoX08oqZaHKs4uDtVc=; b=f1ahoHw4OanHUk2dIlPkKlNOsB u3Dhcdidk+6UGc2txOHFUs1M8lg/gT9ykcZVlVygkvIUupWuVsBwKdSnqfBfeZRciq0j62VR9rHpJ pIl4Yys+9aFbOwLr2ke7YOHvPAReyI2RRX+c0tMDjENzOFPtMbuog42Hl5BMxxIqTL0Y=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:Cc:To:From :Sender:Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:List-Post: List-Owner:List-Archive; bh=bJOGZZH/vMzxETF1c58XYg6DXIoX08oqZaHKs4uDtVc=; b=d VRYjGESaRXozWKj2PzuzswlA1oiPE22KQFbtjXNfbe9TNlsZC1rTSOTn1OkcujZIBLEIrfTIltmsD 5LwEWQ+K6IxtlQoQeGHaRMqHSrh7LBSxpo07GQRVL6fKU3fzvsAB2k/Zx8kHj6/1UPkR6Yzy5Egqp NwVFVo6w+e78LzAk=; Received: from dfw.source.kernel.org ([139.178.84.217]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.95) id 1ti7YK-0000Vo-6A for linux-f2fs-devel@lists.sourceforge.net; Wed, 12 Feb 2025 07:53:00 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 4A33E5C2647; Wed, 12 Feb 2025 07:52:10 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60F25C4CEDF; Wed, 12 Feb 2025 07:52:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1739346769; bh=o6Omrk5mpniLuURjnCYmDYyCsZtUDfiNV2x9rR1U9Ho=; h=From:To:Cc:Subject:Date:From; b=WqcY2wjrJ1jC288l8H7MSpntzmJZqoZw8/M0txvc5h2rC+aIJC+QBk4c/T6BkmQzk 7BgAPQ59eTYHUDjCRzRQDqZbhrVmXSHcXMyUL5taDY5gzKk6KUay2WdmyYPlr7vmei 48hKG7Ai+hMr2wsmiKuZ5cB5DuR31UEcTRJI01ymNqa33NFd8Lejh0gbMUH+cNF88j i5ulx7mURJL5y2uUoyXDJSm4fLo75inh7V0fawj8Ywz7PhJxVsglFRKSKMFl6Meo7F eGEbnE8FPuT4R7QIGqJAknYr5xMrvzbT/e2AJGtBYFrRBmg4qEFwLtm2V2Xxg3Wfq1 MKBeCo3mf7Mbg== To: jaegeuk@kernel.org Date: Wed, 12 Feb 2025 15:52:42 +0800 Message-ID: <20250212075242.988652-1-chao@kernel.org> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog MIME-Version: 1.0 X-Headers-End: 1ti7YK-0000Vo-6A Subject: [f2fs-dev] [PATCH] f2fs: fix to avoid accessing uninitialized curseg X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Chao Yu via Linux-f2fs-devel From: Chao Yu Reply-To: Chao Yu Cc: linux-kernel@vger.kernel.org, syzbot+b6b347b7a4ea1b2e29b6@syzkaller.appspotmail.com, linux-f2fs-devel@lists.sourceforge.net Errors-To: linux-f2fs-devel-bounces@lists.sourceforge.net syzbot reports a f2fs bug as below: F2FS-fs (loop3): Stopped filesystem due to reason: 7 kworker/u8:7: attempt to access beyond end of device BUG: unable to handle page fault for address: ffffed1604ea3dfa RIP: 0010:get_ckpt_valid_blocks fs/f2fs/segment.h:361 [inline] RIP: 0010:has_curseg_enough_space fs/f2fs/segment.h:570 [inline] RIP: 0010:__get_secs_required fs/f2fs/segment.h:620 [inline] RIP: 0010:has_not_enough_free_secs fs/f2fs/segment.h:633 [inline] RIP: 0010:has_enough_free_secs+0x575/0x1660 fs/f2fs/segment.h:649 f2fs_is_checkpoint_ready fs/f2fs/segment.h:671 [inline] f2fs_write_inode+0x425/0x540 fs/f2fs/inode.c:791 write_inode fs/fs-writeback.c:1525 [inline] __writeback_single_inode+0x708/0x10d0 fs/fs-writeback.c:1745 writeback_sb_inodes+0x820/0x1360 fs/fs-writeback.c:1976 wb_writeback+0x413/0xb80 fs/fs-writeback.c:2156 wb_do_writeback fs/fs-writeback.c:2303 [inline] wb_workfn+0x410/0x1080 fs/fs-writeback.c:2343 process_one_work kernel/workqueue.c:3236 [inline] process_scheduled_works+0xa66/0x1840 kernel/workqueue.c:3317 worker_thread+0x870/0xd30 kernel/workqueue.c:3398 kthread+0x7a9/0x920 kernel/kthread.c:464 ret_from_fork+0x4b/0x80 arch/x86/kernel/process.c:148 ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244 Commit 8b10d3653735 ("f2fs: introduce FAULT_NO_SEGMENT") allows to trigger no free segment fault in allocator, then it will update curseg->segno to NULL_SEGNO, though, CP_ERROR_FLAG has been set, f2fs_write_inode() missed to check the flag, and access invalid curseg->segno directly in below call path, then resulting in panic: - f2fs_write_inode - f2fs_is_checkpoint_ready - has_enough_free_secs - has_not_enough_free_secs - __get_secs_required - has_curseg_enough_space - get_ckpt_valid_blocks : access invalid curseg->segno To avoid this issue, let's: - check CP_ERROR_FLAG flag in prior to f2fs_is_checkpoint_ready() in f2fs_write_inode(). - in has_curseg_enough_space(), a) verify status of curseg before accessing its field, and b) grab curseg_mutex lock to avoid race condition. Fixes: 8b10d3653735 ("f2fs: introduce FAULT_NO_SEGMENT") Reported-by: syzbot+b6b347b7a4ea1b2e29b6@syzkaller.appspotmail.com Closes: https://lore.kernel.org/all/67973c2b.050a0220.11b1bb.0089.GAE@google.com Signed-off-by: Chao Yu --- fs/f2fs/inode.c | 7 +++++++ fs/f2fs/segment.h | 27 ++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/fs/f2fs/inode.c b/fs/f2fs/inode.c index 02f1b69d03d8..5c1b515eab36 100644 --- a/fs/f2fs/inode.c +++ b/fs/f2fs/inode.c @@ -799,6 +799,13 @@ int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc) !is_inode_flag_set(inode, FI_DIRTY_INODE)) return 0; + /* + * no need to update inode page, ultimately f2fs_evict_inode() will + * clear dirty status of inode. + */ + if (f2fs_cp_error(sbi)) + return -EIO; + if (!f2fs_is_checkpoint_ready(sbi)) { f2fs_mark_inode_dirty_sync(inode, true); return -ENOSPC; diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h index 943be4f1d6d2..e9fcf2b85b76 100644 --- a/fs/f2fs/segment.h +++ b/fs/f2fs/segment.h @@ -559,15 +559,23 @@ static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi, unsigned int node_blocks, unsigned int data_blocks, unsigned int dent_blocks) { - + struct curseg_info *curseg; unsigned int segno, left_blocks, blocks; int i; /* check current data/node sections in the worst case. */ for (i = CURSEG_HOT_DATA; i < NR_PERSISTENT_LOG; i++) { - segno = CURSEG_I(sbi, i)->segno; - left_blocks = CAP_BLKS_PER_SEC(sbi) - + curseg = CURSEG_I(sbi, i); + + mutex_lock(&curseg->curseg_mutex); + if (!curseg->inited || curseg->segno == NULL_SEGNO) { + left_blocks = 0; + } else { + segno = curseg->segno; + left_blocks = CAP_BLKS_PER_SEC(sbi) - get_ckpt_valid_blocks(sbi, segno, true); + } + mutex_unlock(&curseg->curseg_mutex); blocks = i <= CURSEG_COLD_DATA ? data_blocks : node_blocks; if (blocks > left_blocks) @@ -575,9 +583,18 @@ static inline bool has_curseg_enough_space(struct f2fs_sb_info *sbi, } /* check current data section for dentry blocks. */ - segno = CURSEG_I(sbi, CURSEG_HOT_DATA)->segno; - left_blocks = CAP_BLKS_PER_SEC(sbi) - + curseg = CURSEG_I(sbi, CURSEG_HOT_DATA); + + mutex_lock(&curseg->curseg_mutex); + if (!curseg->inited || curseg->segno == NULL_SEGNO) { + left_blocks = 0; + } else { + segno = curseg->segno; + left_blocks = CAP_BLKS_PER_SEC(sbi) - get_ckpt_valid_blocks(sbi, segno, true); + } + mutex_unlock(&curseg->curseg_mutex); + if (dent_blocks > left_blocks) return false; return true;