Received: by 2002:a05:6358:5282:b0:b5:90e7:25cb with SMTP id g2csp3460235rwa; Tue, 23 Aug 2022 05:15:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR7Hegmj4PFUTWEGnUtNMnTGo6ExuciTLDa+6sflyr01ja2frGXDNdLIl+oq7cg5DslYPXl/ X-Received: by 2002:a17:906:58d0:b0:73d:8e80:b07e with SMTP id e16-20020a17090658d000b0073d8e80b07emr4189523ejs.488.1661256928805; Tue, 23 Aug 2022 05:15:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661256928; cv=none; d=google.com; s=arc-20160816; b=K9eMCBOA/Zb7cLmSRi9qjWpJruTpKoWKpx1eXAB6GbBd/neiA1izKfgNcF6I13dd8C b/2cfomyU2P9hAj7jMldA6Tby+m/4V0h3M0dOW1pd1F+QljzghjNOX283rCM8JskuAS0 YSeLnmeXSeipIK8cKd040vzYTGuJ3so+xwSc2d8wYduc3Vbq60lgn0pmH+0Ccbodj0SX LN8sivKamoatVi/+6lNGyYnFmxpNMUBf5yPsjrM71jZUoZUr4ewF3xAoRehPbe+JNjc6 d6b5GisOxjt/VQMbhgGE5G3KIZlfWZSmOANC/C+J65tvSZ7RRMJcr4nMVX+smjl0JKXT uTpw== 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=atGN7GEzHxepHOxB2ngfMWgvhN1pzz1/YErCB7I4roE=; b=GK8wwuqV6P0XAZa97Ffa2JHTLhb6eXU6n3ij2buvOxCQLh7qvIRYBk1UIlz/5t9Cp5 C3MYcD3ttB+nCf5wZbCE+O1Pk2c9Z4ONjVD7UrPrT+qiMWnNRLQHsXTD4psRojFiXr3w pIa30rZ6Tks1FeFJ7W5MkwVsg8/SBCM/k+D95l6obuikSl9yqtJehKD+4weGVyx22Pud Q6ZhbCB+ZgncBY4yKd55BH+qmKMaHrFEAvasPWO4Lu8Jls9bcEdgGBKOSgEQtTtmARSW Q3oBuS9UDwnNGkd507c7lTteyjKp9BY0TRnro0BVHSXKpyPtrrk5med0MbXTf2NMk08C 0G6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=fgfJzE0f; 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 gt34-20020a1709072da200b0073d60665f53si8574808ejc.493.2022.08.23.05.15.03; Tue, 23 Aug 2022 05:15: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=fgfJzE0f; 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 S1352771AbiHWKGP (ORCPT + 99 others); Tue, 23 Aug 2022 06:06:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1352243AbiHWKB3 (ORCPT ); Tue, 23 Aug 2022 06:01:29 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4588F6FA21; Tue, 23 Aug 2022 01:49:08 -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 BEEF9B81BF8; Tue, 23 Aug 2022 08:49:07 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19F70C433C1; Tue, 23 Aug 2022 08:49:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1661244546; bh=X80genjRvM2BQRqho1Zf4KlpLP+VqAoEYc/fqf6UN7M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fgfJzE0fVZUaOJyJqRc0kkKkWNPsrd65BcMekk05FqYIhE3VHWgC/IkbDhkFcVKup hnXhT2JloLeYZulHGWvJJJeYTsh276H1GzJadE6PkE5WpdACvFCEs/8QwTsRz31cJB jipgwTVXfYClF+yuevvjlFotgG7zBTa/D7ctjnJU= 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 4.14 153/229] ext4: make sure ext4_append() always allocates new block Date: Tue, 23 Aug 2022 10:25:14 +0200 Message-Id: <20220823080059.141504689@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220823080053.202747790@linuxfoundation.org> References: <20220823080053.202747790@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 @@ -52,6 +52,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; @@ -61,6 +62,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))