From nobody Fri Dec 19 18:41:44 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7FD91C38A2D for ; Mon, 24 Oct 2022 13:13:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235786AbiJXNNH (ORCPT ); Mon, 24 Oct 2022 09:13:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236011AbiJXNK0 (ORCPT ); Mon, 24 Oct 2022 09:10:26 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C94A22BE2D; Mon, 24 Oct 2022 05:24:10 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 8F10BB81205; Mon, 24 Oct 2022 12:06:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id DB809C433D6; Mon, 24 Oct 2022 12:06:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613177; bh=n+CMlShebmcQLDR5asrTsndohyMMu9dY5i7dyKNhIsI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=s9IyUwV5adMnI32yQafYfOpSoilATCOFAwKN/E5VMOMpmDr+Ubf4F6yuuAlIOpmxA AIe5dloC/LyV76zOQyMAusM03bcJ0QWhUe1pVltY+kwx8eDNEi0pVwMaJfHX/cDOTC 9RlkXTzVNTKLY+nOjFBwS5fgLHEcR6e12Xf3Lr4o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wenqing Liu , Chao Yu , Jaegeuk Kim Subject: [PATCH 5.4 038/255] f2fs: fix to do sanity check on summary info Date: Mon, 24 Oct 2022 13:29:08 +0200 Message-Id: <20221024113003.699400716@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113002.471093005@linuxfoundation.org> References: <20221024113002.471093005@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" From: Chao Yu commit c6ad7fd16657ebd34a87a97d9588195aae87597d upstream. As Wenqing Liu reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=3D216456 BUG: KASAN: use-after-free in recover_data+0x63ae/0x6ae0 [f2fs] Read of size 4 at addr ffff8881464dcd80 by task mount/1013 CPU: 3 PID: 1013 Comm: mount Tainted: G W 6.0.0-rc4 #1 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.15.0-1 04/01/2014 Call Trace: dump_stack_lvl+0x45/0x5e print_report.cold+0xf3/0x68d kasan_report+0xa8/0x130 recover_data+0x63ae/0x6ae0 [f2fs] f2fs_recover_fsync_data+0x120d/0x1fc0 [f2fs] f2fs_fill_super+0x4665/0x61e0 [f2fs] mount_bdev+0x2cf/0x3b0 legacy_get_tree+0xed/0x1d0 vfs_get_tree+0x81/0x2b0 path_mount+0x47e/0x19d0 do_mount+0xce/0xf0 __x64_sys_mount+0x12c/0x1a0 do_syscall_64+0x38/0x90 entry_SYSCALL_64_after_hwframe+0x63/0xcd The root cause is: in fuzzed image, SSA table is corrupted: ofs_in_node is larger than ADDRS_PER_PAGE(), result in out-of-range access on 4k-size page. - recover_data - do_recover_data - check_index_in_prev_nodes - f2fs_data_blkaddr This patch adds sanity check on summary info in recovery and GC flow in where the flows rely on them. After patch: [ 29.310883] F2FS-fs (loop0): Inconsistent ofs_in_node:65286 in summary, = ino:0, nid:6, max:1018 Cc: stable@vger.kernel.org Reported-by: Wenqing Liu Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/gc.c | 10 +++++++++- fs/f2fs/recovery.c | 15 ++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -612,7 +612,7 @@ static bool is_alive(struct f2fs_sb_info { struct page *node_page; nid_t nid; - unsigned int ofs_in_node; + unsigned int ofs_in_node, max_addrs; block_t source_blkaddr; =20 nid =3D le32_to_cpu(sum->nid); @@ -638,6 +638,14 @@ static bool is_alive(struct f2fs_sb_info return false; } =20 + max_addrs =3D IS_INODE(node_page) ? DEF_ADDRS_PER_INODE : + DEF_ADDRS_PER_BLOCK; + if (ofs_in_node >=3D max_addrs) { + f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%u, nid:%u, m= ax:%u", + ofs_in_node, dni->ino, dni->nid, max_addrs); + return false; + } + *nofs =3D ofs_of_node(node_page); source_blkaddr =3D datablock_addr(NULL, node_page, ofs_in_node); f2fs_put_page(node_page, 1); --- a/fs/f2fs/recovery.c +++ b/fs/f2fs/recovery.c @@ -406,7 +406,7 @@ static int check_index_in_prev_nodes(str struct dnode_of_data tdn =3D *dn; nid_t ino, nid; struct inode *inode; - unsigned int offset; + unsigned int offset, ofs_in_node, max_addrs; block_t bidx; int i; =20 @@ -432,15 +432,24 @@ static int check_index_in_prev_nodes(str got_it: /* Use the locked dnode page and inode */ nid =3D le32_to_cpu(sum.nid); + ofs_in_node =3D le16_to_cpu(sum.ofs_in_node); + + max_addrs =3D ADDRS_PER_PAGE(dn->node_page, dn->inode); + if (ofs_in_node >=3D max_addrs) { + f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%lu, nid:%u, = max:%u", + ofs_in_node, dn->inode->i_ino, nid, max_addrs); + return -EFSCORRUPTED; + } + if (dn->inode->i_ino =3D=3D nid) { tdn.nid =3D nid; if (!dn->inode_page_locked) lock_page(dn->inode_page); tdn.node_page =3D dn->inode_page; - tdn.ofs_in_node =3D le16_to_cpu(sum.ofs_in_node); + tdn.ofs_in_node =3D ofs_in_node; goto truncate_out; } else if (dn->nid =3D=3D nid) { - tdn.ofs_in_node =3D le16_to_cpu(sum.ofs_in_node); + tdn.ofs_in_node =3D ofs_in_node; goto truncate_out; }