Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp1856968rwb; Fri, 19 Aug 2022 10:31:39 -0700 (PDT) X-Google-Smtp-Source: AA6agR7JzKRZEehAw/14vfA0HqZnR7i5uucLERhTlJf4qrqMZUphuorGzkg/bqZVs1JnUpOePpxd X-Received: by 2002:a63:e90c:0:b0:422:5ab9:99d6 with SMTP id i12-20020a63e90c000000b004225ab999d6mr7227921pgh.394.1660930298839; Fri, 19 Aug 2022 10:31:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660930298; cv=none; d=google.com; s=arc-20160816; b=GxuCFtNFs/NHBRv3O9+bsH0CHQ3p3fFUsLoW4vsohJsiEQH3n8eLs41GRsx6Wvz+Rk c15DLK3blX6mdWcwisBfB00B/Z5kpp+ENQBUOvEthL3QM5yG2pTrFkdFs1sp1GSLFhl0 gJwav3kFG2lgo/5gFDzi4qEpUV5VPu9K/KEf1OFnrGJPU+c3KPYV/3RZBywRRqEqro1z Ayf238VQQAaycvNrroBaEOZk7j2v6fGN5RTuAuzoKhb+LkKorH74rWOGkIifA6IREMsd 8quw9kEmc0yD6OjxT2Y/jtOGkTwmhN+7v22QyVhSBJS85NCeOPY2MFSxrSZl4loDcmKB Fm5w== 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=b7Ql2X4D61dQ0LHiijPIY4kXM5mBsB4pkaJoI1h2Gw4=; b=svNinz37te8gZqQwJ28g7YMC923wmsJWeLj+gCsnSjSCCwktGyn50S7UVZbO2tz4tY a53CTPJzSaTjDie1MraRqAD+9uSEYRO72j2dSD7jPCrqhASRRJhneK+haMdCydgjVVzc bO38AuOxVXfnFipyomQl52EIs4XFuipJgFcaug3vMKzClY6AeILu6SQBFY51V8o75l52 8Ow12eHWsNwsy6hQFGnWarFuxlVkMVhJa/9unb02YOrLTGgJS05DAU8tRHYArh6wV3I5 +d7Gj8vwPtgETSwNZo34OqS94fXF1ZolsVMWijxE491mM571LLVIru0ddFP5bFL9/OJg x49w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=SM6Mpg5b; 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 h66-20020a636c45000000b0042a46114c85si1717633pgc.514.2022.08.19.10.31.27; Fri, 19 Aug 2022 10:31:38 -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=SM6Mpg5b; 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 S1354460AbiHSQ5A (ORCPT + 99 others); Fri, 19 Aug 2022 12:57:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354856AbiHSQ4W (ORCPT ); Fri, 19 Aug 2022 12:56:22 -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 94A1E115590; Fri, 19 Aug 2022 09:17:07 -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 00F626182A; Fri, 19 Aug 2022 16:12:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1261CC433C1; Fri, 19 Aug 2022 16:12:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660925525; bh=qO4j3AsmeVoKiJYQ3Ug9g5nOvWot0X0HDMxaeW3t0vA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SM6Mpg5bmjFL7y0UuGE73S8ak5zAR0teQ00wnQ5UnWrCFWBQYr0ytsee8MsCkRUw2 fiBWPD0VUOT4jtCUMVwhb+Y0p7ht2e3foUaWXEZijXLLTOyVrdv1J1uNjAok89QbCr p7zVqmfssgh9HD38CG+qQoOEPK6ALh1C8waVXAjw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, Lukas Czerner , Andreas Dilger , Theodore Tso Subject: [PATCH 5.10 519/545] ext4: make sure ext4_append() always allocates new block Date: Fri, 19 Aug 2022 17:44:49 +0200 Message-Id: <20220819153852.758812329@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220819153829.135562864@linuxfoundation.org> References: <20220819153829.135562864@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 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: Lukas Czerner commit b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b upstream. ext4_append() must always allocate a new block, otherwise we run the risk of overwriting existing directory block corrupting the directory tree in the process resulting in all manner of problems later on. Add a sanity check to see if the logical block is already allocated and error out if it is. Cc: stable@kernel.org Signed-off-by: Lukas Czerner Reviewed-by: Andreas Dilger Link: https://lore.kernel.org/r/20220704142721.157985-2-lczerner@redhat.com Signed-off-by: Theodore Ts'o Signed-off-by: Greg Kroah-Hartman --- fs/ext4/namei.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -54,6 +54,7 @@ static struct buffer_head *ext4_append(h struct inode *inode, ext4_lblk_t *block) { + struct ext4_map_blocks map; struct buffer_head *bh; int err; @@ -63,6 +64,21 @@ static struct buffer_head *ext4_append(h return ERR_PTR(-ENOSPC); *block = inode->i_size >> inode->i_sb->s_blocksize_bits; + map.m_lblk = *block; + map.m_len = 1; + + /* + * We're appending new directory block. Make sure the block is not + * allocated yet, otherwise we will end up corrupting the + * directory. + */ + err = ext4_map_blocks(NULL, inode, &map, 0); + if (err < 0) + return ERR_PTR(err); + if (err) { + EXT4_ERROR_INODE(inode, "Logical block already allocated"); + return ERR_PTR(-EFSCORRUPTED); + } bh = ext4_bread(handle, inode, *block, EXT4_GET_BLOCKS_CREATE); if (IS_ERR(bh))