Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3116784rwb; Mon, 15 Aug 2022 18:31:36 -0700 (PDT) X-Google-Smtp-Source: AA6agR6iX7eGo3RtyVqnm6lbW1qxIWMoo9roQmrnh0AGZn5dkj4QGRUFn34+Wp6tuXPvQ5UTUkbo X-Received: by 2002:a17:907:a0c6:b0:731:87ce:5289 with SMTP id hw6-20020a170907a0c600b0073187ce5289mr12183501ejc.465.1660613496686; Mon, 15 Aug 2022 18:31:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660613496; cv=none; d=google.com; s=arc-20160816; b=qC7dDoiCASLQzzmrY8c8n9XII9ZMizAnrF2fgpJy0JctOONQAJn7HqwxIHuvSsgmzO bMmN0oAYVAeNcPbeR6ROHdGLh5GmcnwLwVInBQsunGIIDNeF7Ke0CLygk/2CcLYYAtgE S020vSifWpOcRVS6tqMeRS8y8sx8KV8/znXFLSKQAlvyBZ2E0tYyWGi7bBky+IsEr/oQ MqbDkUunYZfj6MkiWOEa99sUwishj/rgqi9899c0jdPm4/leicgF6gpnKEVrHQ0uMgud p4xSqOLIMbPRM4z5K1j7Y6ojIv3JsNMh2qvBd2/HurwUJYb3hrBr5wHuYBPCV77zMlR4 laSA== 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=FQ8wuQpiOcOaImBWdHn/FiTjrMR7hmOTFlPyCTNunh8=; b=HzHy3lSa5ugOjEHNl5M/qXM5gRoqyTrQgr705ccjhY26kERzaIlqBO4TS6IZfQvOki Yws+wnneza4nBQvauDsTDnpacDWBE1iqdua9s1U+/+efi+cG4KCDM/zw6UwUqp9pU81T cDpk5atqZQ2SS4UFtTpAp/MrRCvRXxAk8LcIvD+q/oHL9nlBb0+0LmAkGMI4f3vgzGK6 GIaZtXvi6LL4/Jg2EfoSK7Ge9IapoNjF5yEwTEXuCtE4cyJMMQ2jpxXub0W2BwE4jqvI MysQWPzjPuMMfbNXxMHiPvAZX0JovhBtHih3DOsN+Ulrp7vt2GQ3PM/WZylwpobnRSCH UZ6Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="Dm/cD26K"; 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 q10-20020a056402518a00b0043bd77de21esi10107246edd.51.2022.08.15.18.31.11; Mon, 15 Aug 2022 18:31:36 -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="Dm/cD26K"; 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 S1353845AbiHOXip (ORCPT + 99 others); Mon, 15 Aug 2022 19:38:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233663AbiHOXeC (ORCPT ); Mon, 15 Aug 2022 19:34:02 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AE0D065645; Mon, 15 Aug 2022 13:09:00 -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 4876860B6E; Mon, 15 Aug 2022 20:09:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4BE09C433C1; Mon, 15 Aug 2022 20:08:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660594139; bh=QNFvrSfmF8jPkO6xUgA/cBgya04LXm/x8wg9L+Oyn58=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Dm/cD26K5fi7Fn3wbY4Y1NOttyccfcVsPa7NW6eND7ySTgyCIS6LpSyLLA1Jdq0Ea 1iItk3WeQyyKWxWhB3Eph7D2Dq9K84kZhCaOVkteJLGhbRXyqInlUWbBYdzr5xsPnp rna53rHQlaAnCfa0yl0fHMxX8BNyZ8aGsyHEQc7M= 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 , Sasha Levin Subject: [PATCH 5.18 1062/1095] ext4: make sure ext4_append() always allocates new block Date: Mon, 15 Aug 2022 20:07:41 +0200 Message-Id: <20220815180512.959795540@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@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 [ Upstream commit b8a04fe77ef1360fbf73c80fddbdfeaa9407ed1b ] 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: Sasha Levin --- fs/ext4/namei.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 2bc3e4b27204..13b6265848c2 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -54,6 +54,7 @@ static struct buffer_head *ext4_append(handle_t *handle, 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(handle_t *handle, 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)) -- 2.35.1