Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp2195756iof; Tue, 7 Jun 2022 22:52:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzh55XiIquGfaAuzo+YjXT1J/h0UyUwFbOINA0aXitRbDscT865tK0P1bOKFjqoCmGzdbQR X-Received: by 2002:a65:6bcc:0:b0:3f6:1815:f541 with SMTP id e12-20020a656bcc000000b003f61815f541mr27766680pgw.183.1654667548016; Tue, 07 Jun 2022 22:52:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654667548; cv=none; d=google.com; s=arc-20160816; b=nnnbLLlb2m6QETyEEBirA4iSjHrMjfA32QVSueTA4VO6g3xVVGB5m0Lm0JMpvkyppl JgjqkpMnmobKNsvDYI2EOl60h1MRESXU8hmuAbun2CqAdS9H8vFxL/wcumCFtPFP6ixU FjJtE7KiA+icBFkDom4KKUfnbul8Fil1FufSFlctZUqWEi9bvQ5JDI1Of0cnGUuhC9PA EapCgs+nagMIGkTsc0POQwKKMCCfYpGVIUjGHr2kxo4mPbtaHb5NAPazFaeHTnRO+//e F2d/QwAAEP2/wxrNs7QfCUfYpcEl75cwDNILJ0vqzJEvrjvSnM/e8BergGbFldxcQRMI +/lg== 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=CTj5d50faZeM+0Y4s699uGlPori+QozuIgp6UMx1g2A=; b=PM14V3c1Y470wXofrIlVchFOx5apt7x7fdE+nvJtbW9RbOEQVfZlKrhpOlutR+g6QC NSGJw0aMa+bFLn3SQgh3P6Sxu5eKxCGNhJlyNQEGq6BdTw7QyhXv2yk4kOmVHNoj1Iw8 eXwj92ZOUgBGl3Lm++dOKea4vkWLjo/tcvTVNgLnY2I+hm7QU6XO3Hf0pOLIBKP684G3 FTnzJ5lj+Kw2D5uQj5yjHH9vIrUg0ZhYMT+fDPQ1PI6LWB6oPytt9MG9MMq7N2dfA/m5 ER7zq3stGyRK3OZqNCxTOjQBT7OXfZqDpup8jXaMd/wPazwprgpQD5L2e5Zt5qyBYluh 87iw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=VwhS8G4u; 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 w204-20020a627bd5000000b0050d4f5ccb83si13506578pfc.311.2022.06.07.22.52.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jun 2022 22:52:28 -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=VwhS8G4u; 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 15845408A49; Tue, 7 Jun 2022 22:19:12 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1392616AbiFHAw5 (ORCPT + 99 others); Tue, 7 Jun 2022 20:52:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1382933AbiFGWM7 (ORCPT ); Tue, 7 Jun 2022 18:12:59 -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 079D225F430; Tue, 7 Jun 2022 12:19:48 -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 B551F61935; Tue, 7 Jun 2022 19:19:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C1801C385A2; Tue, 7 Jun 2022 19:19:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654629588; bh=jk3GPX/z1hEDFjC03/RcNKJq/h0HQGSSLN8X7A3cII8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VwhS8G4u9U/gVou8hYx+Je3g6I6MsFMQU9a+iO8yvSakVr63VaRE61YbUjYqjXptf YWpGJSkZtFSz4REff9Efg5wx54J5Oo7PgEIfkDHE0xtZ56RB7a+kPy3KQd41sJQpy3 vFgoOdW/WAMTHWLrubDyci5KoCjs2R4uN8Y3tMYw= 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 745/879] ext4: verify dir block before splitting it Date: Tue, 7 Jun 2022 19:04:23 +0200 Message-Id: <20220607165024.481567191@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 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 @@ -277,9 +277,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(struct inode *dir, char *from, char *to, struct dx_map_entry *offsets, @@ -1249,15 +1249,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) { if (ext4_hash_in_dirent(dir)) h.hash = EXT4_DIRENT_HASH(de); @@ -1270,8 +1278,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; } @@ -1943,8 +1950,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 */