Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3407222rwa; Tue, 23 Aug 2022 04:22:58 -0700 (PDT) X-Google-Smtp-Source: AA6agR7HNifiLdIrVTGXFcmNMz+NWm9jIWLrtzY+2YHGRFgbk3ggTUvD3+iqS47immVxl/Dz7+uO X-Received: by 2002:a17:902:ce01:b0:172:59a6:fb3d with SMTP id k1-20020a170902ce0100b0017259a6fb3dmr24082194plg.119.1661253778467; Tue, 23 Aug 2022 04:22:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661253778; cv=none; d=google.com; s=arc-20160816; b=cU/Jvf/MPFK55l4zP49KWJfiS18CCw5XTYTDLtUfsyk915QI3//A1vL4c1863VSlXv 769kmO3Zw4Xfqv0AvPfIwwmLsuceDjJ0P3k/XYRzbHYB/HGY9pAKYFQPNd88OQv6FKJu ZQ0A7C9HoyUyhEamBfAgkdyI78o2SoMG7zfMg4ZgpfuUFto3iM8hY+x8/znrn7rSCZcZ w9VuxX5u+iOWFG4DhR2DnKLSAE2lMtE68kgMxK9IhTEGmQo15eY4BT4jcX2gxgn2ThJ3 S3N6ZD2U0ZYtrIdFCcNGg2dm7ZdK63uF0nwTnTzUXiesIU1effhC6iEH2dD3aBNyYUs1 1kNw== 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=L5hbcXCC9QbBzxUouE/DC/Zx0scoIHXDhJS7U2osIBI=; b=hBuS2rizEW6OxG8Nve7WOdEzU0zYoMy3NyV1D6IhoD6GHJZebtzYf0p/AY4Ka4AjhU 0KTRiy9lye+rWyESfcLqoj8VEzZ02wJ2ihM6xlsY3vHcxpNlVoTvl8gq6mNZfmMdXbPo uMCiiELnrehUiB1mETX9hRjb0lKS8I5x9sv6SEy8a98JTwmaH2CrUm2cwG9NMgQjb/Do G4beyrQTlW4bNXYpKgnzUGh6TCmejCYl/WTvTSsYlJ8q8FlKQPro8QFYpEX2g4FE+qKt V5ZRCriSojvkneLFX1t8jS+eIFsWTbQoxFRFhSaohvDxfTNatu15KFMEFQNUyS48EAoa AknA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SNRn40+u; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d6-20020a170903230600b001708af3828fsi7875562plh.62.2022.08.23.04.22.47; Tue, 23 Aug 2022 04:22:58 -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=SNRn40+u; 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 S1352549AbiHWKID (ORCPT + 99 others); Tue, 23 Aug 2022 06:08:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352434AbiHWKBy (ORCPT ); Tue, 23 Aug 2022 06:01:54 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D87163F25; Tue, 23 Aug 2022 01:49:43 -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 7C732B81C5F; Tue, 23 Aug 2022 08:34:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80EC7C433D6; Tue, 23 Aug 2022 08:34:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661243688; bh=nIcfzrungjtKw7y6HO2FvmCrIPM7rRA9aRlAm5poON0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SNRn40+u9fx9QCdCNQEp5e+euBg4J3urz8M4GIsI0qmyxw3Qs6ZEueI3am0t4QG/T 88BsSyDgmRjpuCqj1Kp4XoY9LznOUZyaqpvfU6/CY1UF8+4rmtk2Cksdb59t847vf9 KuChM+FAJ/sleNJhL2F8BZQfE6ZxufnfqE23/nvc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Wenqing Liu , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.19 356/365] f2fs: fix to do sanity check on segment type in build_sit_entries() Date: Tue, 23 Aug 2022 10:04:17 +0200 Message-Id: <20220823080133.179539706@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080118.128342613@linuxfoundation.org> References: <20220823080118.128342613@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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,T_SCC_BODY_TEXT_LINE 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 From: Chao Yu [ Upstream commit 09beadf289d6e300553e60d6e76f13c0427ecab3 ] As Wenqing Liu reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=216285 RIP: 0010:memcpy_erms+0x6/0x10 f2fs_update_meta_page+0x84/0x570 [f2fs] change_curseg.constprop.0+0x159/0xbd0 [f2fs] f2fs_do_replace_block+0x5c7/0x18a0 [f2fs] f2fs_replace_block+0xeb/0x180 [f2fs] recover_data+0x1abd/0x6f50 [f2fs] f2fs_recover_fsync_data+0x12ce/0x3250 [f2fs] f2fs_fill_super+0x4459/0x6190 [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 segment type is invalid, so in f2fs_do_replace_block(), f2fs accesses f2fs_sm_info::curseg_array with out-of-range segment type, result in accessing invalid curseg->sum_blk during memcpy in f2fs_update_meta_page(). Fix this by adding sanity check on segment type in build_sit_entries(). Reported-by: Wenqing Liu Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/segment.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 874c1b9c41a2..ac890c9fa8a1 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -4362,6 +4362,12 @@ static int build_sit_entries(struct f2fs_sb_info *sbi) return err; seg_info_from_raw_sit(se, &sit); + if (se->type >= NR_PERSISTENT_LOG) { + f2fs_err(sbi, "Invalid segment type: %u, segno: %u", + se->type, start); + return -EFSCORRUPTED; + } + sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks; if (f2fs_block_unit_discard(sbi)) { @@ -4410,6 +4416,13 @@ static int build_sit_entries(struct f2fs_sb_info *sbi) break; seg_info_from_raw_sit(se, &sit); + if (se->type >= NR_PERSISTENT_LOG) { + f2fs_err(sbi, "Invalid segment type: %u, segno: %u", + se->type, start); + err = -EFSCORRUPTED; + break; + } + sit_valid_blocks[SE_PAGETYPE(se)] += se->valid_blocks; if (f2fs_block_unit_discard(sbi)) { -- 2.35.1