From patchwork Mon Aug 24 08:31:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 265104 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B94BCC433DF for ; Mon, 24 Aug 2020 09:06:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 97D7C206F0 for ; Mon, 24 Aug 2020 09:06:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598259971; bh=G5fuFrD3PcjLpF51DZ/Y1Gr7jPHzRAJ4Hnb7BU65XwU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=AgBKwukCvSSbDxsZQhO3WsxWluztcRz4oRi9qf1o/8jrQQNpsxABDK1nQJgE9tKz1 VoR4b9Km4OUyvSmKX1aykaw64LbrHQ2AA5iwlumbXCP1cjUJ3rXQ25aS6+sbBjhwxW Hn8y+BdvZjvEYM9HC+9vGK/rw/j9A6HOV8ZA9Srw= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726799AbgHXJGH (ORCPT ); Mon, 24 Aug 2020 05:06:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:41478 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730546AbgHXI41 (ORCPT ); Mon, 24 Aug 2020 04:56:27 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DB62C22DBF; Mon, 24 Aug 2020 08:56:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598259387; bh=G5fuFrD3PcjLpF51DZ/Y1Gr7jPHzRAJ4Hnb7BU65XwU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XvAwY7PQ3OLS4BxhOhWwn3p7as4VsdA4mOMM/42NbQiB9DviuDT/B0fN7OhSz44Dc CDbYJl0hfTY94970GTJXwfB6xNZIgNA//JEWbS8yhc/AW9sYhOvN4+edGzdie1YC4x AOZOFobPyJ7li89Rk46xArT8AxxA/5J1sVBxgJLU= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eric Sandeen , Andreas Dilger , Jan Kara , Theodore Tso , Sasha Levin Subject: [PATCH 4.19 39/71] ext4: fix potential negative array index in do_split() Date: Mon, 24 Aug 2020 10:31:30 +0200 Message-Id: <20200824082357.843727253@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200824082355.848475917@linuxfoundation.org> References: <20200824082355.848475917@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Eric Sandeen [ Upstream commit 5872331b3d91820e14716632ebb56b1399b34fe1 ] If for any reason a directory passed to do_split() does not have enough active entries to exceed half the size of the block, we can end up iterating over all "count" entries without finding a split point. In this case, count == move, and split will be zero, and we will attempt a negative index into map[]. Guard against this by detecting this case, and falling back to split-to-half-of-count instead; in this case we will still have plenty of space (> half blocksize) in each split block. Fixes: ef2b02d3e617 ("ext34: ensure do_split leaves enough free space in both blocks") Signed-off-by: Eric Sandeen Reviewed-by: Andreas Dilger Reviewed-by: Jan Kara Link: https://lore.kernel.org/r/f53e246b-647c-64bb-16ec-135383c70ad7@redhat.com Signed-off-by: Theodore Ts'o Signed-off-by: Sasha Levin --- fs/ext4/namei.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index a2425e2d439cf..186a2dd05bd87 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1732,7 +1732,7 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, blocksize, hinfo, map); map -= count; dx_sort_map(map, count); - /* Split the existing block in the middle, size-wise */ + /* Ensure that neither split block is over half full */ size = 0; move = 0; for (i = count-1; i >= 0; i--) { @@ -1742,8 +1742,18 @@ static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, size += map[i].size; move++; } - /* map index at which we will split */ - split = count - move; + /* + * map index at which we will split + * + * If the sum of active entries didn't exceed half the block size, just + * split it in half by count; each resulting block will have at least + * half the space free. + */ + if (i > 0) + split = count - move; + else + split = count/2; + hash2 = map[split].hash; continued = hash2 == map[split - 1].hash; dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n",