Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp5098234rwb; Tue, 17 Jan 2023 09:09:24 -0800 (PST) X-Google-Smtp-Source: AMrXdXsN+LpV1vtVi6pkMsJNpOKrEBBoR4W9lfS9197z/aKQdrF9NiZmFXV0IRmqI/Jdk7OQFt8v X-Received: by 2002:a05:6a20:e687:b0:b8:2e85:da0a with SMTP id mz7-20020a056a20e68700b000b82e85da0amr3969918pzb.24.1673975364529; Tue, 17 Jan 2023 09:09:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673975364; cv=none; d=google.com; s=arc-20160816; b=AIDDFJL3NmigZAJEYAjdopyYmKVVRRLGOBtOaWQTKd6zpV06S8OUBzrHR2Ym9kT7en QYh7EcfFoo4Cht2WbZq5G4NnrY0dHUj6XRlx4AO7SBALuGokeOGBzsXvi1mlXtIbxTM1 NWN96f0UyOvZ5My4ZucZQbO5sR5jygc/8sx1FWDIgrHNqIEKZQQ/rDGGxyfQwAtSD+E1 XEa/SNW0cLJ4mMskXUuvJqw8sM3kmVbs1eXBNebwebjpU6eR9IteQe5nPp8qP7fPuOZs nKoWlmX78Oi/DOf+NcCOHERRlyL24YUwSVKASO+hxhwATcLROsAMijWPxXUDWQ29RNVl SCqQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=VFgrKKVDGlnjswcyDcS33CkLpy5eaqJX1Jr0XAotnhM=; b=b9+kt1xVqersjO+sHCP1W5yFblowfAEA9VnjbxClbUyS0pUwhDDk82IRo/Oi3FFPki Zmm72tT8JM2AIqG2oqUKCO4Gtrnptn11bsZ2SNujirzcESyUptyADC+iGKaHoHqlYHzM DTBZPTG1QMr0s0gMUMOUq/mNYps4/3tcXjivw5t6beH53Kiu3f5Udxvc6c8OpXWgJIwH RKTDBREl6EiLUL500wgNBaXvo2wnbOqHl0p0k/zsHRBXtyjAoS7EXnpR+69AD48eAnqm Zko+T/iT90R/1Lo87nwOHpK2y/eFg0arpz81/bGzWn36LnQFmy1lJjujjtSN1usqbMVl tg0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.org.uk header.s=zeniv-20220401 header.b="HaeMD/cc"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id q139-20020a632a91000000b004acd11feb3csi32967560pgq.607.2023.01.17.09.09.06; Tue, 17 Jan 2023 09:09:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-ext4-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=@linux.org.uk header.s=zeniv-20220401 header.b="HaeMD/cc"; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zeniv.linux.org.uk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231216AbjAQQ6C (ORCPT + 99 others); Tue, 17 Jan 2023 11:58:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230503AbjAQQ6A (ORCPT ); Tue, 17 Jan 2023 11:58:00 -0500 Received: from zeniv.linux.org.uk (zeniv.linux.org.uk [IPv6:2a03:a000:7:0:5054:ff:fe1c:15ff]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0996939BA3; Tue, 17 Jan 2023 08:57:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=linux.org.uk; s=zeniv-20220401; h=Sender:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=VFgrKKVDGlnjswcyDcS33CkLpy5eaqJX1Jr0XAotnhM=; b=HaeMD/ccoYcTFtebP4vLcZuH+N 1PT/gnYIywzUVrTYt4cocfc0srMQoa3RaxHgzKmDXLk+WfMr/253ydnKgRVwgVB+9PHUJXhpzPUfB GrJhFJ6qm4fFHHP8im0tRC1NIFGojSYQTNOrTNIEDrPIVK+j0N38QZX0qN8fKMPuWOeV3rrjOa1QC 86fVaT2Z4Om0f03eTiiRtGZSC0pRIqKRjoga2/SSD/RCDT8H+cbeSVjfZ5e1Ks9NK4a1TeNTu6A4S cCbV4DV56TzaVVKP8z+/S68D3aMy7u5RwUa2CDrDGzYvlax92Jncw+HlgnSo84fbKlgBsteU2alnF u0goMv6A==; Received: from viro by zeniv.linux.org.uk with local (Exim 4.96 #2 (Red Hat Linux)) id 1pHpHX-002OEN-2m; Tue, 17 Jan 2023 16:57:56 +0000 Date: Tue, 17 Jan 2023 16:57:55 +0000 From: Al Viro To: Jan Kara Cc: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, Ted Tso , linux-xfs@vger.kernel.org Subject: Re: Locking issue with directory renames Message-ID: References: <20230117123735.un7wbamlbdihninm@quack3> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230117123735.un7wbamlbdihninm@quack3> Sender: Al Viro X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,SPF_HELO_NONE,SPF_NONE 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-ext4@vger.kernel.org On Tue, Jan 17, 2023 at 01:37:35PM +0100, Jan Kara wrote: > Hello! > > I've some across an interesting issue that was spotted by syzbot [1]. The > report is against UDF but AFAICS the problem exists for ext4 as well and > possibly other filesystems. The problem is the following: When we are > renaming directory 'dir' say rename("foo/dir", "bar/") we lock 'foo' and > 'bar' but 'dir' is unlocked because the locking done by vfs_rename() is > > if (!is_dir || (flags & RENAME_EXCHANGE)) > lock_two_nondirectories(source, target); > else if (target) > inode_lock(target); > > However some filesystems (e.g. UDF but ext4 as well, I suspect XFS may be > hurt by this as well because it converts among multiple dir formats) need > to update parent pointer in 'dir' and nothing protects this update against > a race with someone else modifying 'dir'. Now this is mostly harmless > because the parent pointer (".." directory entry) is at the beginning of > the directory and stable however if for example the directory is converted > from packed "in-inode" format to "expanded" format as a result of > concurrent operation on 'dir', the filesystem gets corrupted (or crashes as > in case of UDF). > > So we'd need to lock 'source' if it is a directory. Ideally this would > happen in VFS as otherwise I bet a lot of filesystems will get this wrong > so could vfs_rename() lock 'source' if it is a dir as well? Essentially > this would amount to calling lock_two_nondirectories(source, target) > unconditionally but that would become a serious misnomer ;). Al, any > thought? FWIW, I suspect that majority of filesystems that do implement rename do not have that problem. Moreover, on cross-directory rename we already have * tree topology stabilized * source guaranteed not to be an ancestor of target or either of the parents so the method instance should be free to lock the source if it needs to do so. Not sure, I'll need to grep around and take a look at the instances...