Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp442690iog; Mon, 13 Jun 2022 06:06:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVfHgu0OHL9O7ITx3qjh5NuMd/E1XfqvESl91gphFn0nXTiusazXvR1QARKGaNgNp8ZIM2 X-Received: by 2002:a17:903:2287:b0:164:83cf:bb15 with SMTP id b7-20020a170903228700b0016483cfbb15mr58171207plh.49.1655125556091; Mon, 13 Jun 2022 06:05:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655125556; cv=none; d=google.com; s=arc-20160816; b=x6IAemHEYvr4JDq34SagntofyoHNs0IMqlB5WY9oLTAE8hgefrHr6XffAZRixYXnTm cGbN6A+a2tps14mgXXXnIcTVpeUb+w0tcGi/PIYlwbmfMSEiJLwIykkDJKdtNZk836Mo i9uqfDB+ztNEmeNX8da93+ZVBrVw6bgApWNJqmJQiBCnuLih0Uv3GLhO1/5gla1mr6qn Hm5c5eLyV4uf5Hxt+WYMJxmNxJJYe3ggY5j/222HNX9pkgqhDqNl2nyYi3bhAESW9/4T oHMehFWdKVyc6vHdG7sojzwjpFNmz2fIBIES0IVEKx1zKtBfrJ8Q3QZWGtay4mq/Fizp r1Tw== 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=MN+5hsOQSb59adeJBKM/vs/yIwNjeSY5kmL60Cup23k=; b=tvwYg3Ni8SyEvPWbexNv64xUJYKGe/qDz5p8SfWnZgs3pBhS6INV5RHWcr6C1WNF7m 2/nNRiQWh9tgEs4l9Sy7QP994GpTIssRBc6gB2WLSsA12HC3SNaoO8bte76e7ABpmcDb 21/K0FK3DamoHPAOum6xvrWeu9HaMGmjNe2BdSi8Rxk2BF9ZAQWTRZ9vBB+q+drpSakx RHm7xMRbxLZpWbjr4XB1YZvVSkkevGnq3feTuLUYYjg/9zBKr7+sSrtyix0SmVuTpRl4 F7h1jCparuvk1xmsSqlcf1hLgtmdAi6LV6QBfFlsqzKIXFtdiX5QrUvnM6OmEIZ/vA/3 om+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=mwZEHchf; 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 a6-20020a170902ecc600b0016646a406easi12123794plh.417.2022.06.13.06.05.39; Mon, 13 Jun 2022 06:05:56 -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=mwZEHchf; 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 S1354003AbiFMLbR (ORCPT + 99 others); Mon, 13 Jun 2022 07:31:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353160AbiFMLXE (ORCPT ); Mon, 13 Jun 2022 07:23:04 -0400 Received: from sin.source.kernel.org (sin.source.kernel.org [IPv6:2604:1380:40e1:4800::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CB4523CA72; Mon, 13 Jun 2022 03:42:14 -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 sin.source.kernel.org (Postfix) with ESMTPS id 0F97DCE1109; Mon, 13 Jun 2022 10:42:13 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 02404C34114; Mon, 13 Jun 2022 10:42:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116931; bh=Nua4sSapNUidDo4Iy+eYqZBfqmQhsqK8TfuuR9ObeOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mwZEHchf06lB50bOwXNxjoSJS9hL7pUY9F+7Hku5uL5Bv1wXSDGxpiVFgiGQ0iIdm baZl6D9qPVEdqep1ouoNtq2EzQjMzPYJWHk7YiQ6oW1VTBEnnwzyg2s0D+o/j2Voo0 gWIRzQd9luaV+cFIkd54kDZfcBMmZLgW2z43UOUw= 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.4 227/411] ext4: avoid cycles in directory h-tree Date: Mon, 13 Jun 2022 12:08:20 +0200 Message-Id: <20220613094935.449968316@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094928.482772422@linuxfoundation.org> References: <20220613094928.482772422@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=-8.3 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: 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 @@ -750,12 +750,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); @@ -811,6 +813,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)) { @@ -852,15 +856,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)) {