Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp572828iog; Mon, 13 Jun 2022 08:22:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzfll3WJiq7B5BpMy+LQKBKZfrU/4MjlGKgy3pR58UZvaCiVQRltQRH0vKx0D8LCkz9CqiL X-Received: by 2002:a17:90a:4897:b0:1c7:5fce:cbcd with SMTP id b23-20020a17090a489700b001c75fcecbcdmr16356714pjh.45.1655133770179; Mon, 13 Jun 2022 08:22:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655133770; cv=none; d=google.com; s=arc-20160816; b=Z6PLM/GBBGIVJZ7PmpZ3x7iW71eJeOpHkC920fhVfG793vpvUyrmi5S9yLgupnu1OX FbC2kWYTn210vr1xAI09L77Z7mlItFBkgBAW7rC7srIW/+qfFZk+DLxsz7YhYBbJXSdE Muai1NyXUnt9DL5LwgwhDRLzaGhwaXG85ItFfu2NSASSksiVEASCH+Su8/hrOndHpwLC XVJsKWLz7GZP5cr488Xi80DuDZpt0+gkF7B46hSstZ1YsmLjPYiA9YQaax6DN47gBVIl DFTsefKunFnKZhcuvbK5jMdIT9mun6nrweWxJg+UsjBAhICBAYEA9npW4dP4QhwlHwVc aw+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=WuQNdvN+U7ouM9RDXxw/RRi5nCyHe8fLvAFc1Eeoovw=; b=oSRkE7subS1Y+08ZNyaNcu9dvz1xatW6a5Ag17T+CFNeVtGc2+EA+uyoBXju2Do0Fy Ca5DEg8nfxPv/G94GF8bMmUSgn+2adJMgtxDcI/f8t3rlpgaXfPJxnBiWpRtDkJnUvfy N7woyEsanagwGobvKJvDzHiFWU0//sDqoO4R7ljfuhIloNfiOhJjRWRrW1e6bdYdAHHc D3MRY6ckvJHBqrWNkc1JDsb7Dd5I5+wDInQevnC1fc/RxDR2R21WYVcYKqy2sa/BJfmb G6ZDJTRz6pgKiQRusUS2fMONrYENik/wrAz+zPR6hR3t/CdLmR/gqnr8MZSxV0KdJBou rPmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oi1ZERJ3; 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 r8-20020a632b08000000b004085af5007bsi4498745pgr.428.2022.06.13.08.22.38; Mon, 13 Jun 2022 08:22:50 -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=oi1ZERJ3; 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 S243329AbiFMKWb (ORCPT + 99 others); Mon, 13 Jun 2022 06:22:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43272 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243172AbiFMKUu (ORCPT ); Mon, 13 Jun 2022 06:20:50 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 38C1921E18; Mon, 13 Jun 2022 03:17:39 -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 B8AF9B80E5C; Mon, 13 Jun 2022 10:17:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C4B2C34114; Mon, 13 Jun 2022 10:17:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115456; bh=3zyHo8E2lTCNCDsARKLRjHsXBpTO5uFQeK4ZbSZk0rs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oi1ZERJ336p1pwujH6RBnzq8hQdcG+msTT/ylMNO+hsDE+TMgYNwdq8CqX8uOFOol q2QWjP/zSniwz+bBLO7MxRNvuefH0oO4zDcwfDDuMKGHtCkb8gKZnTjChS3CrABGvI SqemY+EuyxRRDBpL5lSVitwLW92Y9jW9Z3bT8dHg= 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.9 080/167] ext4: verify dir block before splitting it Date: Mon, 13 Jun 2022 12:09:14 +0200 Message-Id: <20220613094859.679175410@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@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 46c116b920ebec58031f0a78c5ea9599b0d2a371 upstream. Before splitting a directory block verify its directory entries are sane so that the splitting code does not access memory it should not. Cc: stable@vger.kernel.org Signed-off-by: Jan Kara Link: https://lore.kernel.org/r/20220518093332.13986-1-jack@suse.cz Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/namei.c | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -270,9 +270,9 @@ static struct dx_frame *dx_probe(struct struct dx_hash_info *hinfo, struct dx_frame *frame); static void dx_release(struct dx_frame *frames); -static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, - unsigned blocksize, struct dx_hash_info *hinfo, - struct dx_map_entry map[]); +static int dx_make_map(struct inode *dir, struct buffer_head *bh, + struct dx_hash_info *hinfo, + struct dx_map_entry *map_tail); static void dx_sort_map(struct dx_map_entry *map, unsigned count); static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to, struct dx_map_entry *offsets, int count, unsigned blocksize); @@ -1185,15 +1185,23 @@ static inline int search_dirblock(struct * Create map of hash values, offsets, and sizes, stored at end of block. * Returns number of entries mapped. */ -static int dx_make_map(struct inode *dir, struct ext4_dir_entry_2 *de, - unsigned blocksize, struct dx_hash_info *hinfo, +static int dx_make_map(struct inode *dir, struct buffer_head *bh, + struct dx_hash_info *hinfo, struct dx_map_entry *map_tail) { int count = 0; - char *base = (char *) de; + struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *)bh->b_data; + unsigned int buflen = bh->b_size; + char *base = bh->b_data; struct dx_hash_info h = *hinfo; - while ((char *) de < base + blocksize) { + if (ext4_has_metadata_csum(dir->i_sb)) + buflen -= sizeof(struct ext4_dir_entry_tail); + + while ((char *) de < base + buflen) { + if (ext4_check_dir_entry(dir, NULL, de, bh, base, buflen, + ((char *)de) - base)) + return -EFSCORRUPTED; if (de->name_len && de->inode) { ext4fs_dirhash(de->name, de->name_len, &h); map_tail--; @@ -1203,8 +1211,7 @@ static int dx_make_map(struct inode *dir count++; cond_resched(); } - /* XXX: do we need to check rec_len == 0 case? -Chris */ - de = ext4_next_entry(de, blocksize); + de = ext4_next_entry(de, dir->i_sb->s_blocksize); } return count; } @@ -1755,8 +1762,11 @@ static struct ext4_dir_entry_2 *do_split /* create map in the end of data2 block */ map = (struct dx_map_entry *) (data2 + blocksize); - count = dx_make_map(dir, (struct ext4_dir_entry_2 *) data1, - blocksize, hinfo, map); + count = dx_make_map(dir, *bh, hinfo, map); + if (count < 0) { + err = count; + goto journal_error; + } map -= count; dx_sort_map(map, count); /* Ensure that neither split block is over half full */