Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp581333iog; Mon, 13 Jun 2022 08:32:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfu1o2VW6/nNbLIN4xBGd8xyi7ViujtvVvU+IWisj/H8fMVlunOubxMa+FQWlm7T41GSkL X-Received: by 2002:a05:6402:438a:b0:42e:985:4944 with SMTP id o10-20020a056402438a00b0042e09854944mr291155edc.283.1655134348985; Mon, 13 Jun 2022 08:32:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655134348; cv=none; d=google.com; s=arc-20160816; b=y2J8qPc2QDnqK/ep7dZfxmxClTEYXLIbHyV9FHdECA7L5jaVAkuyWXp9W/3FfNtWQ1 W9BAPfwvoEMPIwD7As/uaJTdlKglZhETykVmZTY/yXWCyZsSpGDe9WO1kjqc+qyVcrUK HTsCbag3eEj9OLwI7RLOpMc7K+90UGo3sz5XQZXXj4nv1LBKCTy3uNRTMoZpHDjmby0v wNLkWJgEjDUCEpcO4hOF1hZGvt6/KhgLkiQfR9nDHx6guLSu40Z16+u1M0WPE8626haj IkgUrCCRijptn6lFCETNhcgVxtebqsN6u4SVE6g1ElZELJRFco6NOKRVOFQc0Sg8cHV0 CsWw== 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=Bpz/M/anv2HPYHeGM+CLn+I9fVr2yzRQ5I8GnzehLwU=; b=WkIV94NTivm2baT67qjXhUuEzHjZPo71aQQuYvb0o9tRYmZqE10iloHSwgq9pRAJwH d85ZoAeZ2SK2kJd6PKxfJl00XCtX5o3EoAuY8g4TzdT1QZHzWl0pX00FggIM/EdQENpj AuAS552jjIlMwvfc9bhPXJZb4Jeg5KmkId5aQ+DsWRIc9qsLs3tv8prm6d8AfJ3ihocp VHoQ8QB6S5lGp4tpZDpwet8LU8cOkOym87AfRfubmcyN7cqH9NV5wosphh4hpK3/X6WL mfpMIewpV0ycnyhItMMXfLh2OtjzFna0pMAfeTWzzCV8vWY8tqSiUZ5W6g69cODdh7/N ekiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=pL4qnCZa; 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 kx5-20020a170907774500b006ee402acc45si6779129ejc.166.2022.06.13.08.32.03; Mon, 13 Jun 2022 08:32:28 -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=pL4qnCZa; 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 S1347036AbiFMKvZ (ORCPT + 99 others); Mon, 13 Jun 2022 06:51:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348312AbiFMKtQ (ORCPT ); Mon, 13 Jun 2022 06:49:16 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 915A82DD61; Mon, 13 Jun 2022 03:26:54 -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 3AE9DB80EA4; Mon, 13 Jun 2022 10:26:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3CE3C34114; Mon, 13 Jun 2022 10:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655116012; bh=YHjvGl6ychesEsr7+fE6a8S92nWp+f9c5YIk2ncoTWM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pL4qnCZatVxfxou6bW6n4W3a65F4gi+tae4XhRzacOulA0ljIovt119yqlpbQqkec JezyztolgFP6QRLxXEapDTVQyTpGxGfsLBUlgSsXYqrrG2JOWy+3mlLJZJ8RBxxqWv EWG5lae1jOVAZFkgOREUfxgm3IgttATBH81BZOvY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jan Kara , Theodore Tso Subject: [PATCH 4.14 108/218] ext4: avoid cycles in directory h-tree Date: Mon, 13 Jun 2022 12:09:26 +0200 Message-Id: <20220613094923.835766646@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094908.257446132@linuxfoundation.org> References: <20220613094908.257446132@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 @@ -747,12 +747,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); @@ -808,6 +810,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)) { @@ -849,15 +853,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)) {