From patchwork Mon Oct 24 11:27:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 9042 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp442245wru; Mon, 24 Oct 2022 06:07:10 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6D6qFyN7hbXzP/GF/tSKXYrHVmXI48LU+8kMh9FSZ4dU1dYQ39FUDT5hP2I7A/5cbBigxn X-Received: by 2002:a17:90b:1648:b0:20b:f0ae:2169 with SMTP id il8-20020a17090b164800b0020bf0ae2169mr72270465pjb.173.1666616830104; Mon, 24 Oct 2022 06:07:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666616830; cv=none; d=google.com; s=arc-20160816; b=revNI3LkycNJE6z2zciMNDjRb1YzgPwHmXnoD1McYfFunlfe/9fOu1ZPSLBgMQ6QgI Iyu2rp8MvSzGBbih/CiuJ1WGKfmp4kRe8QcQmLurhbDe7ILVIIK6Oty9VUV6SQ9uMjJf 4DTzsx8M5uA4Wtfn17q+u+7mgFdWB8XygUEHdcrLZBXcgSIzaicLu9Wbj/VSVDwxgg76 MU+Rx8uOGXYTeMV8xXAkw1JwDq+nqHfpWwvwxKaBVk1kQr57CiG6uxN1gut2e/tgPEUf 3ICch31Rxm4brudR4C3+/R7pjQAwma8a8ZHsYv7kzHSSmlO+Ny993OeG6S6FFUUOeGgx fpSg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qV7rjmUJ3mlEg1Y7JCqkPrfhCbnyQZBHJb5ZjfaP8lU=; b=UIk/SOjKxkneIAq4uVLBxROo6Q/uE92g1abGPUn8kEh8wME3RxusHPKWxMiQ2mzQXN px56YUeQ6LL/RisFkEKchodXCHdtUNDZ04PQRrj8hUOW4BqHX15lsrR1aS6yKwtiO6I0 GVt3HdzbKcA9P/sQpP3RV/5b5qgx+1TexI/aHtWRvKe6+Wf/wxjkXJvPk31l9ylnTzEm +Sk79EVciEkblLSrECeKc75/CeeXJANU7aIa+ZLnTpabu5MpRivSUoE1AYyCQBzWqE0P vuaapU+Q3BkqyU28SiQ3Pvz090NSiSbzn+/OoiK+lT1aGMGxt0xuDlrv39WWwJmFgItB p/vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HaLG4wTx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j7-20020aa78dc7000000b005669cd93898si29465277pfr.233.2022.10.24.06.06.50; Mon, 24 Oct 2022 06:07:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HaLG4wTx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235380AbiJXNGR (ORCPT + 99 others); Mon, 24 Oct 2022 09:06:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235343AbiJXNEa (ORCPT ); Mon, 24 Oct 2022 09:04:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8957342E59; Mon, 24 Oct 2022 05:20:11 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 409BE611B0; Mon, 24 Oct 2022 12:19:29 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52EF1C433C1; Mon, 24 Oct 2022 12:19:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1666613968; bh=d7FUz7ZekITfTM4wA8NgK0dQkSexU0vVYwnJ8dNbTvo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HaLG4wTxfQrf5sHgZ46p4IcASCa5CmndLRN1WY2xQ8QJS28SyMa8RTJQfhoTzp1Jb cGNdkrVkrBqYppXYTFTG4rF/+pXXkNdNxJA1PFiNeOsMTXuRWDJlyBDrjh1zyGlllX c1DPv4k+M2mfvzQKPnDPlOG8kAQ+BxC/GKKi+pio= 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.10 052/390] f2fs: fix to do sanity check on summary info Date: Mon, 24 Oct 2022 13:27:29 +0200 Message-Id: <20221024113024.853480982@linuxfoundation.org> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221024113022.510008560@linuxfoundation.org> References: <20221024113022.510008560@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747574409578905238?= X-GMAIL-MSGID: =?utf-8?q?1747574409578905238?= From: Chao Yu commit c6ad7fd16657ebd34a87a97d9588195aae87597d upstream. As Wenqing Liu reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216456 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 Signed-off-by: Pavel Machek Reviewed-by: Chao Yu --- 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 @@ -977,7 +977,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; nid = le32_to_cpu(sum->nid); @@ -1003,6 +1003,14 @@ static bool is_alive(struct f2fs_sb_info return false; } + max_addrs = IS_INODE(node_page) ? DEF_ADDRS_PER_INODE : + DEF_ADDRS_PER_BLOCK; + if (ofs_in_node >= max_addrs) { + f2fs_err(sbi, "Inconsistent ofs_in_node:%u in summary, ino:%u, nid:%u, max:%u", + ofs_in_node, dni->ino, dni->nid, max_addrs); + return false; + } + *nofs = ofs_of_node(node_page); source_blkaddr = data_blkaddr(NULL, node_page, ofs_in_node); f2fs_put_page(node_page, 1); --- a/fs/f2fs/recovery.c +++ b/fs/f2fs/recovery.c @@ -437,7 +437,7 @@ static int check_index_in_prev_nodes(str struct dnode_of_data tdn = *dn; nid_t ino, nid; struct inode *inode; - unsigned int offset; + unsigned int offset, ofs_in_node, max_addrs; block_t bidx; int i; @@ -463,15 +463,24 @@ static int check_index_in_prev_nodes(str got_it: /* Use the locked dnode page and inode */ nid = le32_to_cpu(sum.nid); + ofs_in_node = le16_to_cpu(sum.ofs_in_node); + + max_addrs = ADDRS_PER_PAGE(dn->node_page, dn->inode); + if (ofs_in_node >= 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 == nid) { tdn.nid = nid; if (!dn->inode_page_locked) lock_page(dn->inode_page); tdn.node_page = dn->inode_page; - tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); + tdn.ofs_in_node = ofs_in_node; goto truncate_out; } else if (dn->nid == nid) { - tdn.ofs_in_node = le16_to_cpu(sum.ofs_in_node); + tdn.ofs_in_node = ofs_in_node; goto truncate_out; }