Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1219648pxb; Thu, 14 Apr 2022 00:43:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwA/s4LSlPOooSw35UQlDhIkHyeLDfBfSFAbUIAxF0eeH8gEbnrX9tslEeZuo0dHMR9CYSO X-Received: by 2002:a17:90b:1c8f:b0:1b8:c6dc:ca61 with SMTP id oo15-20020a17090b1c8f00b001b8c6dcca61mr2349170pjb.13.1649922180162; Thu, 14 Apr 2022 00:43:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649922180; cv=none; d=google.com; s=arc-20160816; b=yqJ4YMzsiNSSI7VDrA17lxNSfhoWPftEwfhw3j7cjensHtjhS2Cyiwdza++uEO/M3C hpuSyk2dRyVon/bq/wHiDGXmG2UfTa9DhHrYbozdM2mlPqYeGft2bP7tgzqIBbEHA0Ow oi0rCyeq/gMoygPJs7jjOjpeznIygJvH2LB5VK2V/dHvlbcshfhwE076s4mc/m+yiOuR LgSofJ+eHvw3bwJEu6z8fNhM1mogKKQRUuajh8BRNA1dOFLx0Iq/AMJgSa4a+hWI13W0 l2qsaYGKg912M9vCqaIK+5WcPMdCZiqWsL8U6fQducUYyMrfrUf9WfVRMwqczelrkv9S jTFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from:mime-version :content-transfer-encoding:dkim-signature:dkim-signature; bh=2H9CaxklyDAJLGL4FtyBvmjheljZo4+V4Lxk2wsGl1E=; b=fVxVxJYI+kofyR9llkBC8G8NHwp9Ko1kGYZH7OZNYJ81vfd9x7iCFGuyAyRt+3aDbB RQNiKHXazhxMd/7Wr7esETOoxpe6RwpGQMBX3Ln6PhjIRO4jQMQ6gAnaV03jrzcszHK1 gfatJbE8LPIXu/2lC5Ta/1du1ISFbS0TQUbwC+7O8mOVfky//Ro+nRemxqeaG+6lOVSg AY1zx6newBwN7cFet3Ugg8H4gYBoQ4Yf3Xy1gR9obzl6Rhkgt7VW07VQUEoo1+CFBfGn E0BPDZuoNij8yLcgfVeDaEY+GSYhsxjKlYnWl9Qq2oJmunWVT28pDBiTARwgc1g0vAyt MndA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.de header.s=susede2_rsa header.b=s379EHVw; dkim=neutral (no key) header.i=@suse.de header.b=fJ7wGxdq; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p1-20020a631e41000000b003816043f141si7872946pgm.822.2022.04.14.00.42.46; Thu, 14 Apr 2022 00:43:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-nfs-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=@suse.de header.s=susede2_rsa header.b=s379EHVw; dkim=neutral (no key) header.i=@suse.de header.b=fJ7wGxdq; spf=pass (google.com: domain of linux-nfs-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-nfs-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=suse.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229549AbiDNEAI (ORCPT + 99 others); Thu, 14 Apr 2022 00:00:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229485AbiDNEAI (ORCPT ); Thu, 14 Apr 2022 00:00:08 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80A961EAD6; Wed, 13 Apr 2022 20:57:44 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id BC4D121607; Thu, 14 Apr 2022 03:57:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1649908662; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2H9CaxklyDAJLGL4FtyBvmjheljZo4+V4Lxk2wsGl1E=; b=s379EHVwfEPC+UxmS6ZdE5iX4K5azANvXaJg9Xi5znQZocDobrVbNRmc+ux5xY43WEM5XO 4wyU+Lgzn5PUnj9O0Pbfdvv9HkB+w4qbmXWVUBG/A7YpRPop5FgoL6B24lIVZG926QTYfx yt+AQ/aDU+mHweZ08yv+AG8qvLPFx1E= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1649908662; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=2H9CaxklyDAJLGL4FtyBvmjheljZo4+V4Lxk2wsGl1E=; b=fJ7wGxdqhIvDNH5xr3UZuNeHSG4o0NOyfanWK3KQCtkwq+IT6uyMi8615jemR5AL6qH8OZ D6rknhH08Le4mCCA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 91CCB134D9; Thu, 14 Apr 2022 03:57:40 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id xJaTE7SbV2K0FwAAMHmgww (envelope-from ); Thu, 14 Apr 2022 03:57:40 +0000 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 From: "NeilBrown" To: Linus Torvalds , Al Viro Cc: David Disseldorp , Jeff Layton , linux-nfs@vger.kernel.org, linux-fsdevel@vger.kernel.org, LKML Subject: [PATCH resend] VFS: filename_create(): fix incorrect intent. Date: Thu, 14 Apr 2022 13:57:35 +1000 Message-id: <164990865594.11576.14265561452573398586@noble.neil.brown.name> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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-nfs@vger.kernel.org When asked to create a path ending '/', but which is not to be a directory (LOOKUP_DIRECTORY not set), filename_create() will never try to create the file. If it doesn't exist, -ENOENT is reported. However, it still passes LOOKUP_CREATE|LOOKUP_EXCL to the filesystems ->lookup() function, even though there is no intent to create. This is misleading and can cause incorrect behaviour. If you try ln -s foo /path/dir/ where 'dir' is a directory on an NFS filesystem which is not currently known in the dcache, this will fail with ENOENT. As the name is not in the dcache, nfs_lookup gets called with LOOKUP_CREATE|LOOKUP_EXCL and so it returns NULL without performing any lookup, with the expectation that a subsequent call to create the target will be made, and the lookup can be combined with the creation. In the case with a trailing '/' and no LOOKUP_DIRECTORY, that call is never made. Instead filename_create() sees that the dentry is not (yet) positive and returns -ENOENT - even though the directory actually exists. So only set LOOKUP_CREATE|LOOKUP_EXCL if there really is an intent to create, and use the absence of these flags to decide if -ENOENT should be returned. Note that filename_parentat() is only interested in LOOKUP_REVAL, so we split that out and store it in 'reval_flag'. __lookup_hash() then gets reval_flag combined with whatever create flags were determined to be needed. Reviewed-by: David Disseldorp Reviewed-by: Jeff Layton Signed-off-by: NeilBrown --- fs/namei.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 3f1829b3ab5b..509657fdf4f5 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3673,18 +3673,14 @@ static struct dentry *filename_create(int dfd, struct= filename *name, { struct dentry *dentry =3D ERR_PTR(-EEXIST); struct qstr last; + bool want_dir =3D lookup_flags & LOOKUP_DIRECTORY; + unsigned int reval_flag =3D lookup_flags & LOOKUP_REVAL; + unsigned int create_flags =3D LOOKUP_CREATE | LOOKUP_EXCL; int type; int err2; int error; - bool is_dir =3D (lookup_flags & LOOKUP_DIRECTORY); =20 - /* - * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any - * other flags passed in are ignored! - */ - lookup_flags &=3D LOOKUP_REVAL; - - error =3D filename_parentat(dfd, name, lookup_flags, path, &last, &type); + error =3D filename_parentat(dfd, name, reval_flag, path, &last, &type); if (error) return ERR_PTR(error); =20 @@ -3698,11 +3694,13 @@ static struct dentry *filename_create(int dfd, struct= filename *name, /* don't fail immediately if it's r/o, at least try to report other errors = */ err2 =3D mnt_want_write(path->mnt); /* - * Do the final lookup. + * Do the final lookup. Suppress 'create' if there is a trailing + * '/', and a directory wasn't requested. */ - lookup_flags |=3D LOOKUP_CREATE | LOOKUP_EXCL; + if (last.name[last.len] && !want_dir) + create_flags =3D 0; inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); - dentry =3D __lookup_hash(&last, path->dentry, lookup_flags); + dentry =3D __lookup_hash(&last, path->dentry, reval_flag | create_flags); if (IS_ERR(dentry)) goto unlock; =20 @@ -3716,7 +3714,7 @@ static struct dentry *filename_create(int dfd, struct f= ilename *name, * all is fine. Let's be bastards - you had / on the end, you've * been asking for (non-existent) directory. -ENOENT for you. */ - if (unlikely(!is_dir && last.name[last.len])) { + if (unlikely(!create_flags)) { error =3D -ENOENT; goto fail; } --=20 2.35.2