Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp1435931iof; Tue, 7 Jun 2022 05:42:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwA/DrCPdHmR/m4LYPCezkCYthwk0wntQL2I3Fiw9CGcj2Y7Bjt3Xo3eKq5AFJbdUNAM6eE X-Received: by 2002:a17:902:d4cf:b0:167:735a:e7a1 with SMTP id o15-20020a170902d4cf00b00167735ae7a1mr12854024plg.161.1654605727107; Tue, 07 Jun 2022 05:42:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654605727; cv=none; d=google.com; s=arc-20160816; b=kKILwS4mhjFfhzRghSPsxYa6Bw4obXUkGh0nj2+5x1gv54aKKrRD2ygm+xgOHfg9wW MFAmg5KMwJNUbWFXB4v4Cqeglo0jN8EPiREMQ0GnApZil+24oRx8r6I4atUEOA31PZcZ 503PzwZC9lQdnssoPqEA6fynhTVvEpzorvhPgUHcKb3yn5UFoHi2y0c7cwlpyOxReye+ 5OU0zROJW0WwY+H52XV6izEoqyV9JLj0d/GmGQUXnBmsVfs5+1YRqJtgw3iaap1u3ECS qf6KR4/GHUz5W3MGQbvU5oWyOh0FYNDG7Z1ep9RvKGW8RY9+4YHlcdHuaFnPuv/y0reP CImw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Z7uA8uljUpAaa3vPWD6QYj1ZJ0uxM8jJ9A1iG1DO0ys=; b=eMdkCnnsB2mPM6/yNafyq2YHzeq+yVM6fLhny8PnLvKSpXmC/blG3T5CiuPqSN0Br6 AHHapNcwpVAz2zX3WhDGR4hPEAd733reZSkF5BoFeajv28vzhhDWzVupsJlS3dum1Otu 7fc9ewzSOsBBhWRo0efGUYR/1XzJpWw7bN/mFT9ZaMVR7FQnG4c0HeTQx3MstoK/K1Op cMRsDz2YbSwTCcYmwxOo50j/t1qOt51DzzQtalYnx9gnip9IhqIB/6h0EBdO4FDpb10g qOKmtQ25OyCM3DaUI+1p8vLqO13QdNsrgvLLSQsFtp+G7GT2sawWNnktKT761nEjFhwK jqYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mit.edu header.s=outgoing header.b=BkMknYXR; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mit.edu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pg1-20020a17090b1e0100b001e350ed011csi17766841pjb.175.2022.06.07.05.41.48; Tue, 07 Jun 2022 05:42:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=fail header.i=@mit.edu header.s=outgoing header.b=BkMknYXR; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mit.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236549AbiFGEZr (ORCPT + 99 others); Tue, 7 Jun 2022 00:25:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236548AbiFGEZo (ORCPT ); Tue, 7 Jun 2022 00:25:44 -0400 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9169CB82C9 for ; Mon, 6 Jun 2022 21:25:42 -0700 (PDT) Received: from cwcc.thunk.org (pool-108-7-220-252.bstnma.fios.verizon.net [108.7.220.252]) (authenticated bits=0) (User authenticated as tytso@ATHENA.MIT.EDU) by outgoing.mit.edu (8.14.7/8.12.4) with ESMTP id 2574PTaj005600 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 7 Jun 2022 00:25:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mit.edu; s=outgoing; t=1654575931; bh=Z7uA8uljUpAaa3vPWD6QYj1ZJ0uxM8jJ9A1iG1DO0ys=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=BkMknYXR9LXeHTxLG/CWRrBDvRe31mhwLuMMQ1sF8Wg0XTfjf8FE8CAw13N2FXKUe /j/17u9xAQBt740tCItb2BldulHHol55hj3U8on4ug1AnMe59NeR6brUykYmPHeXzb rYw7HA2V1II25Q1/9p80Qx3NE7mMPssnE4VkDcChcjH+Qs483WHc7uMpuXkjOlNIOh czd/xUbA1efzmwT4pu+Cfiswvx1OOR+fFjxAWgkztfFUFWdVOl1y/tWL0FyXENR8rx lX9CIMUrWNnnyEMDJsdb2zHvbZds/8GYD+gu02GcT8tW3+YWvAhsVHi2QHW1XQXVIg Bz11Z5AXP6i0w== Received: by cwcc.thunk.org (Postfix, from userid 15806) id C6B3A15C3E2B; Tue, 7 Jun 2022 00:25:27 -0400 (EDT) From: "Theodore Ts'o" To: Ext4 Developers List Cc: Nils Bars , =?UTF-8?q?Moritz=20Schl=C3=B6gel?= , Nico Schiller , "Theodore Ts'o" Subject: [PATCH 5/7] e2fsck: avoid out-of-bounds write for very deep extent trees Date: Tue, 7 Jun 2022 00:24:42 -0400 Message-Id: <20220607042444.1798015-6-tytso@mit.edu> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20220607042444.1798015-1-tytso@mit.edu> References: <20220607042444.1798015-1-tytso@mit.edu> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_NONE, 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-ext4@vger.kernel.org The kernel doesn't support extent trees deeper than 5 (EXT4_MAX_EXTENT_DEPTH). For this reason we only maintain the extent tree statistics for 5 levels. Avoid out-of-bounds writes and reads if the extent tree is deeper than this. We keep these statistics to determine whether we should rebuild the extent tree. If the extent tree is too deep, we don't need the statistics because we should always rebuild the it. Reported-by: Nils Bars Reported-by: Moritz Schlögel Reported-by: Nico Schiller Signed-off-by: Theodore Ts'o --- e2fsck/extents.c | 10 +++++++++- e2fsck/pass1.c | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/e2fsck/extents.c b/e2fsck/extents.c index 01879f56..86fe00e7 100644 --- a/e2fsck/extents.c +++ b/e2fsck/extents.c @@ -526,7 +526,8 @@ errcode_t e2fsck_check_rebuild_extents(e2fsck_t ctx, ext2_ino_t ino, */ if (info.curr_entry == 1 && !(extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) && - !eti.force_rebuild) { + !eti.force_rebuild && + info.curr_level < MAX_EXTENT_DEPTH_COUNT) { struct extent_tree_level *etl; etl = eti.ext_info + info.curr_level; @@ -580,6 +581,13 @@ errcode_t e2fsck_should_rebuild_extents(e2fsck_t ctx, extents_per_block = (ctx->fs->blocksize - sizeof(struct ext3_extent_header)) / sizeof(struct ext3_extent); + + /* If the extent tree is too deep, then rebuild it. */ + if (info->max_depth > MAX_EXTENT_DEPTH_COUNT) { + pctx->blk = info->max_depth; + op = PR_1E_CAN_COLLAPSE_EXTENT_TREE; + goto rebuild; + } /* * If we can consolidate a level or shorten the tree, schedule the * extent tree to be rebuilt. diff --git a/e2fsck/pass1.c b/e2fsck/pass1.c index 11d7ce93..43972e7c 100644 --- a/e2fsck/pass1.c +++ b/e2fsck/pass1.c @@ -2842,7 +2842,8 @@ static void scan_extent_node(e2fsck_t ctx, struct problem_context *pctx, if (pctx->errcode) return; if (!(ctx->options & E2F_OPT_FIXES_ONLY) && - !pb->eti.force_rebuild) { + !pb->eti.force_rebuild && + info.curr_level < MAX_EXTENT_DEPTH_COUNT) { struct extent_tree_level *etl; etl = pb->eti.ext_info + info.curr_level; -- 2.31.0