Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp598634pxb; Tue, 5 Apr 2022 15:27:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyK4MVh7pWkb/lzBzyQ8XpIhZsDLnMLPpNg5Dbm6/BzacHBANSCJ2Gd3O74dzn32AnNN/Dy X-Received: by 2002:a05:6a00:140f:b0:4e0:6995:9c48 with SMTP id l15-20020a056a00140f00b004e069959c48mr5871714pfu.59.1649197667513; Tue, 05 Apr 2022 15:27:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649197667; cv=none; d=google.com; s=arc-20160816; b=w8N0q6+VDfptQTlRZM6xxmh03cgI++iGmpFHmgo2ZCFKtjEFwr21ScOY7zOYDha0w8 Bk3eALb9ECdVxyj03B6igfpbC7eW5YFBMwk2WHp24KH7qJxaGp5Abn5ZrqqWDr5nfH50 rKg/gI+Xa/7YyeSyP3XMGtRYpcoBv7VKa52/6L+WKkeTFPQkq47rX6Bsve4e22SBvJD8 AdJ3Zud03B+Q32rlb40psu2Us8eeDgREFD41VAj15hxyJdr4PwLpAkI8P8ZNNYxg4A8Z KL7H8BaX/Au5cBtf1k9fW4Hk0ldtZIWkaJQPdwE6Owmuq8Cx9Qk+YEtTBz7v9QTg5xA2 nX+Q== 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=VTZRVZFkIKfHACYiQbYhd2KQZ21Z2YGbbpFA9EnWwVI=; b=I5J9qeU/O6P99NuENYrIAYZOlgK9pL4NMT5f6ixTQ/D1aPV5DeLJooimQ8dIPQhqNs DFYOhHofWNvloVCS4qqWvXo79RzJlbLLOf/3En2ZMYy/CktsRVmCdovilTIQfb+IDBAo Rma5NHRknjUxJjOHOulJZVIRacAxxMhzYq33nRKyJ0uw8PhE1ugZ8NFGJJUC90dW/itV fbzMA0Gj4rXZYrw8Owpuo8oBizQ0zjxjcMMMnK6L8FQ/3vk/XmR/AYgv8pv4nmnUvD7a oLliMT8GVVP1KqFIROxZDr+aYmI0/aAc5zuXZkAvi2QPBojpwQkW/1nsgsNNy9zxxBH7 +4/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UgxE5zmn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id 20-20020a630f54000000b003816043f076si13964821pgp.619.2022.04.05.15.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 15:27:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UgxE5zmn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E02141F977C; Tue, 5 Apr 2022 15:10:06 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384502AbiDEOSI (ORCPT + 99 others); Tue, 5 Apr 2022 10:18:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238995AbiDEJdP (ORCPT ); Tue, 5 Apr 2022 05:33:15 -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 6B4C89FD7; Tue, 5 Apr 2022 02:21:17 -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 07A5961659; Tue, 5 Apr 2022 09:21:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 187D0C385A2; Tue, 5 Apr 2022 09:21:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649150476; bh=h3+920vsgBKX/DUE/ZduiegpdnGRM1DQ4JHPzMqKf7I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UgxE5zmnR4nq/zQY6NST6nfslvO80/rALzw63b+ZBv3J5Slwk09OeRBDPXgFy8K/L Ibev1lLup4LNAFSjTNQvb52ZsBtgWDVm37GwZ7OIQJhNXypW/bYpHVi+37s/Ctd8Ey TjCZhv7ZpepVe7u2sxwtsohps5M2MQZhhSYbrCAw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Chao Yu , Jaegeuk Kim Subject: [PATCH 5.15 069/913] f2fs: fix to do sanity check on .cp_pack_total_block_count Date: Tue, 5 Apr 2022 09:18:51 +0200 Message-Id: <20220405070341.892160051@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070339.801210740@linuxfoundation.org> References: <20220405070339.801210740@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 commit 5b5b4f85b01604389f7a0f11ef180a725bf0e2d4 upstream. As bughunter reported in bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=215709 f2fs may hang when mounting a fuzzed image, the dmesg shows as below: __filemap_get_folio+0x3a9/0x590 pagecache_get_page+0x18/0x60 __get_meta_page+0x95/0x460 [f2fs] get_checkpoint_version+0x2a/0x1e0 [f2fs] validate_checkpoint+0x8e/0x2a0 [f2fs] f2fs_get_valid_checkpoint+0xd0/0x620 [f2fs] f2fs_fill_super+0xc01/0x1d40 [f2fs] mount_bdev+0x18a/0x1c0 f2fs_mount+0x15/0x20 [f2fs] legacy_get_tree+0x28/0x50 vfs_get_tree+0x27/0xc0 path_mount+0x480/0xaa0 do_mount+0x7c/0xa0 __x64_sys_mount+0x8b/0xe0 do_syscall_64+0x38/0xc0 entry_SYSCALL_64_after_hwframe+0x44/0xae The root cause is cp_pack_total_block_count field in checkpoint was fuzzed to one, as calcuated, two cp pack block locates in the same block address, so then read latter cp pack block, it will block on the page lock due to the lock has already held when reading previous cp pack block, fix it by adding sanity check for cp_pack_total_block_count. Cc: stable@vger.kernel.org Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Greg Kroah-Hartman --- fs/f2fs/checkpoint.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) --- a/fs/f2fs/checkpoint.c +++ b/fs/f2fs/checkpoint.c @@ -867,6 +867,7 @@ static struct page *validate_checkpoint( struct page *cp_page_1 = NULL, *cp_page_2 = NULL; struct f2fs_checkpoint *cp_block = NULL; unsigned long long cur_version = 0, pre_version = 0; + unsigned int cp_blocks; int err; err = get_checkpoint_version(sbi, cp_addr, &cp_block, @@ -874,15 +875,16 @@ static struct page *validate_checkpoint( if (err) return NULL; - if (le32_to_cpu(cp_block->cp_pack_total_block_count) > - sbi->blocks_per_seg) { + cp_blocks = le32_to_cpu(cp_block->cp_pack_total_block_count); + + if (cp_blocks > sbi->blocks_per_seg || cp_blocks <= F2FS_CP_PACKS) { f2fs_warn(sbi, "invalid cp_pack_total_block_count:%u", le32_to_cpu(cp_block->cp_pack_total_block_count)); goto invalid_cp; } pre_version = *version; - cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1; + cp_addr += cp_blocks - 1; err = get_checkpoint_version(sbi, cp_addr, &cp_block, &cp_page_2, version); if (err)