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 496F929B764 for ; Mon, 15 Dec 2025 12:27:03 +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=1765801625; cv=none; b=tSowsUKc8QbnL5BkqQgY9B/FvzJwBC75x4txsmWSwZ9DGrESzgL/mJ1INJOUuyv2YYvD6W+dugkQUuzdwCIHB2mKv7H9mfE9L8+4UO3RQKB2VGR7z09BB/sRTiT48YbVoSQVgaKomI49XVZ6wt/xdeqQXkpKHbj78YmSdfSIQ9Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765801625; c=relaxed/simple; bh=Joja1OqbjnTDiSrvEnzMD1A+KIllyS5dmH/tn/ypbGU=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=EB1Qcq2MtfU+NvvldYBCY4ypDj5U6h0adRK7aosWp8Lb5CrCcDKxNjhetvvFjU8GUDu/Tm4tBg34Del8pI4bQanj0ORZmq5pe9A2QSAjumfWY3cq0FhF9RdJ5hsGfieZFN/zx9FGIhfum2Uz4K9Eml/2i8u1E8TO8lxvWrQeXAk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CBqtM3P5; 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="CBqtM3P5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 96749C4CEFB; Mon, 15 Dec 2025 12:27:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1765801623; bh=Joja1OqbjnTDiSrvEnzMD1A+KIllyS5dmH/tn/ypbGU=; h=From:To:Cc:Subject:Date:From; b=CBqtM3P5gCw0AZNdjtoj5AhfFe46qmyzjDV6eGMTzdaLmeGOPL42TqIefivGdrv1i Po/4mvclCBOHgH8JW3BhRfpnVv8lFyZPhVkyBPH+6kunwbfm6XIlATG+XivAqbOK2b FtjUdRLZDyRwg2VJHZZ9Xdx+qqmMfp/nlcN6m0gEiudHGbXqSL4pOZ+r+jNkwC57XG HC0r0xNl/PZMgdxJGBRuo9mdPK3g+nMGoiY4CITM1jRluFSXULnpL+CxZ93peJfXTU pHdUmaYrxq86TQmf6zsqf5kPn0oIHrHOzmmAiIVzAyKkp53dKXXgL4Rl7hNo8DYHEN 6IZ2s4sqsTL1w== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@kernel.org Subject: [PATCH v4 1/2] f2fs: fix to do sanity check on node folio during its writeback Date: Mon, 15 Dec 2025 20:26:53 +0800 Message-ID: <20251215122654.3494829-1-chao@kernel.org> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add more sanity check on node folio during its writeback, if node footer indicates it is an inode: - i_mode should be consistent w/ inode - ofs_of_node should be consistent w/ non-xattr node In addition, if sanity check fails, let's shutdown filesystem to avoid looping to redirty and writeback it. Cc: stable@kernel.org Signed-off-by: Chao Yu --- v4: - rebase the code fs/f2fs/node.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/fs/f2fs/node.c b/fs/f2fs/node.c index 482a362f2625..2bb9e6d35080 100644 --- a/fs/f2fs/node.c +++ b/fs/f2fs/node.c @@ -1504,20 +1504,37 @@ static int sanity_check_node_footer(struct f2fs_sb_info *sbi, struct folio *folio, pgoff_t nid, enum node_type ntype) { + bool is_inode; + if (unlikely(nid != nid_of_node(folio))) goto out_err; + is_inode = IS_INODE(folio); + switch (ntype) { + case NODE_TYPE_REGULAR: + if (is_inode) { + umode_t m = le16_to_cpu(F2FS_INODE(folio)->i_mode); + + if (!S_ISLNK(m) && !S_ISREG(m) && !S_ISDIR(m) && + !S_ISCHR(m) && !S_ISBLK(m) && !S_ISFIFO(m) && + !S_ISSOCK(m)) + goto out_err; + + if (f2fs_has_xattr_block(ofs_of_node(folio))) + goto out_err; + } + break; case NODE_TYPE_INODE: - if (!IS_INODE(folio)) + if (!is_inode) goto out_err; break; case NODE_TYPE_XATTR: - if (!f2fs_has_xattr_block(ofs_of_node(folio))) + if (is_inode || !f2fs_has_xattr_block(ofs_of_node(folio))) goto out_err; break; case NODE_TYPE_NON_INODE: - if (IS_INODE(folio)) + if (is_inode) goto out_err; break; default: @@ -1751,7 +1768,11 @@ static bool __write_node_folio(struct folio *folio, bool atomic, bool *submitted /* get old block addr of this node page */ nid = nid_of_node(folio); - f2fs_bug_on(sbi, folio->index != nid); + + if (sanity_check_node_footer(sbi, folio, nid, NODE_TYPE_REGULAR)) { + f2fs_handle_critical_error(sbi, STOP_CP_REASON_CORRUPTED_NID); + goto redirty_out; + } if (f2fs_get_node_info(sbi, nid, &ni, !do_balance)) goto redirty_out; -- 2.49.0