Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2194504iof; Tue, 7 Jun 2022 22:49:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxt7vOjvPSbQHkm9T2DJeI9hF6RkUiDAEgtBGxGs26hKeKgFJRBic5fvKm/zEWv6seY3q3t X-Received: by 2002:a17:90b:1b41:b0:1e8:4e69:e735 with SMTP id nv1-20020a17090b1b4100b001e84e69e735mr24109594pjb.9.1654667377643; Tue, 07 Jun 2022 22:49:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667377; cv=none; d=google.com; s=arc-20160816; b=Xxq42o1d1bvZwyv2WMV50nH48CClI6bajlD0KjrgWODeiChttmm2DpzspKBw6pJK/P Hq/koV77ccTd8A37Tg5smOQUgz/LNaXfuIEFT1rre2ZUgX1nFeMZsn52EqBCgFbH6BR4 Pdc7VGNaVHFZPfc/bHQwEitTrv3m8wED3yZ1zwOC14cGrgX748Ma5SZKZPuWkHIX7nzx qweJa9IBmNfxs2pIr+h5JSfNYfXnjHlJcYFQUMdOUndX6G7lstiB8MEwgnrcr6ZP2lwo yMi9JTqRUHwvGSLMuMsVOfySM71c6O1PJvbcFWBH0Aqpy3wpumt4b03/CjSXFzHz3O53 nHnA== 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=m0GjGdkXBBb1cRo9LM+bNRP0X9vIl2IIi5S1GZOt6Bk=; b=BEPCFa0scz1sLi2TobJxgSmuZRAE8odB6R4lOi4K6XV8NRtFtgQ1Jpab2A90bGUs6C vGpQy9dD0U3ZUWlRrADf8X1WLiQopZqExxHz29p7ctAmUhsU2VLVf97TDMpu9P9IpYOd 9txeLOBHkPyzCX8tX14Jsu9VGy/PN15n4+bCQY5KSmD+iXZXn17pum0sph9Tc6KJNFKx 2n7BJJ3lCbw3bv5NLqfiQkwOvFnT730kv/HVBnf8KQZyK2qkFBXVeExQgaXtXJpIP/sC MyesABdCVFU2QPb/Jq6hmDvbD7B/SzJYdogic7vsKzD2FB01w2jxo/+kdayH22yeAQGM 7r/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="vqCr29h/"; 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 o16-20020a170902d4d000b00163eae5d4d4si30139227plg.339.2022.06.07.22.49.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:49:37 -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="vqCr29h/"; 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 out1.vger.email (out1.vger.email [IPv6:2620:137:e000::1:20]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2959C3A15F7; Tue, 7 Jun 2022 22:16:35 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1387099AbiFHAaL (ORCPT + 99 others); Tue, 7 Jun 2022 20:30:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37852 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380035AbiFGWOj (ORCPT ); Tue, 7 Jun 2022 18:14:39 -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 0757E260CCD; Tue, 7 Jun 2022 12:19:51 -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 8C058618EC; Tue, 7 Jun 2022 19:19:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 93E9DC385A2; Tue, 7 Jun 2022 19:19:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629591; bh=wVbbGLDEgiHbwO+8ceB4LaEgkGTDRdVe8SWyZV+ln8w=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vqCr29h/FLXpTniX3UGtcQ/VrKVZXVS7j21OYJF5KOZu3VDNbdtlNBfH+sUMdmuaI e6xpsofLJnDwdxgC0/tGjczF6abE7r2fXPZSOo4ZZkXP3PEy080SE+Cf/pckG02OJv 47W1XCYaoYV0IHeTJI23Ren7M5B8O6dWVLU07+wk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Theodore Tso Subject: [PATCH 5.18 746/879] ext4: avoid cycles in directory h-tree Date: Tue, 7 Jun 2022 19:04:24 +0200 Message-Id: <20220607165024.509940881@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220607165002.659942637@linuxfoundation.org> References: <20220607165002.659942637@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=-3.1 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=unavailable 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: Jan Kara commit 3ba733f879c2a88910744647e41edeefbc0d92b2 upstream. A maliciously corrupted filesystem can contain cycles in the h-tree stored inside a directory. That can easily lead to the kernel corrupting tree nodes that were already verified under its hands while doing a node split and consequently accessing unallocated memory. Fix the problem by verifying traversed block numbers are unique. Cc: stable@vger.kernel.org Signed-off-by: Jan Kara Link: https://lore.kernel.org/r/20220518093332.13986-2-jack@suse.cz Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/namei.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -777,12 +777,14 @@ static struct dx_frame * dx_probe(struct ext4_filename *fname, struct inode *dir, struct dx_hash_info *hinfo, struct dx_frame *frame_in) { - unsigned count, indirect; + unsigned count, indirect, level, i; struct dx_entry *at, *entries, *p, *q, *m; struct dx_root *root; struct dx_frame *frame = frame_in; struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR); u32 hash; + ext4_lblk_t block; + ext4_lblk_t blocks[EXT4_HTREE_LEVEL]; memset(frame_in, 0, EXT4_HTREE_LEVEL * sizeof(frame_in[0])); frame->bh = ext4_read_dirblock(dir, 0, INDEX); @@ -854,6 +856,8 @@ dx_probe(struct ext4_filename *fname, st } dxtrace(printk("Look up %x", hash)); + level = 0; + blocks[0] = 0; while (1) { count = dx_get_count(entries); if (!count || count > dx_get_limit(entries)) { @@ -882,15 +886,27 @@ dx_probe(struct ext4_filename *fname, st dx_get_block(at))); frame->entries = entries; frame->at = at; - if (!indirect--) + + block = dx_get_block(at); + for (i = 0; i <= level; i++) { + if (blocks[i] == block) { + ext4_warning_inode(dir, + "dx entry: tree cycle block %u points back to block %u", + blocks[level], block); + goto fail; + } + } + if (++level > indirect) return frame; + blocks[level] = block; frame++; - frame->bh = ext4_read_dirblock(dir, dx_get_block(at), INDEX); + frame->bh = ext4_read_dirblock(dir, block, INDEX); if (IS_ERR(frame->bh)) { ret_err = (struct dx_frame *) frame->bh; frame->bh = NULL; goto fail; } + entries = ((struct dx_node *) frame->bh->b_data)->entries; if (dx_get_limit(entries) != dx_node_limit(dir)) {