Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp458473lqp; Wed, 12 Jun 2024 06:55:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXUTJMWTJQWX2fsTSsOwsBY5hMPAqtNFoZNz3xL0ybo2nZ50McYVSvDbhkUrMJH1Ot7S0SUePFsM12rZZI9Kh9ydDvlQXB9ZAUQWkvxjw== X-Google-Smtp-Source: AGHT+IFqGHBnEBcQqBZM2zBLer47F5EgVWSMlz3O0IEgMGA4oJlgolB8HUDdNAtIf6L0jcppR428 X-Received: by 2002:a05:6a00:21d1:b0:704:2d27:c9b4 with SMTP id d2e1a72fcca58-705bce2f6b6mr2486725b3a.14.1718200500975; Wed, 12 Jun 2024 06:55:00 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718200500; cv=pass; d=google.com; s=arc-20160816; b=AOSWod193S6b8osW2I6cQYqKqYdAhNKktDrTkTqPMAOIps5cnAX8j35KmfsOSiiY9H tm7u+6WbsnKBv2zlp0YjIn3OUufAnyyzlDdY1jCyj5yGk9XG32R/TmV1RtEcS+OFPv4D feeYcTI7EShHajWQI9Ns4KX5ZNfZ6HNU6y6DAAvUeknEcYhz7+M1KrLwgNF+hbV6u6OZ Qy90e6WAZi/Tg14P3GUb+sbkgoQYO/DAOWGHcIzDNfyNXJY6TgzaFpS4Dd8Y5GyxeGKu aJ8vR4AepNVO5LM7BriWLaLeK1r/6f3BKAxuY+uGwytCaN6WwaHLwry6VEQ12q5siqQX TGqQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=tbQEkzm7bfPcX8ggDCK0W2HOo+T6bhnps8cfjiY1xow=; fh=5EiZXzLHPnVycrmuCXtsqNACX2kPU+Emmswt88V8iF4=; b=MEzbFBhv+JJ5dsPgCHDD0yAhYnl8HSIde1IvXKNpjiucRPoq6mFVZAD5iCZyle44Lu Kas34UasGf+05HC8gafJkztHA3k1jBAbPwVLVPNd7vpwu2ncJvmHlGfbBpGiSzhovalR GyHDDyiAACn4rLG5rYHBYMTIrJlc/9DbA99iR2cGSsSyrFQKCEhoYv8QGmCRdYyJpjKR mNRVCSlCNIKAPnEnOPTm+zy/tvmGBvlMIvuohCLTajJC4WlD1wZTnd1GbLqfkpoRbzOu 0GMHV0CFLR5R8Prse716N8uPge7uEKlsEN24FMG5XUYNL/G4SDti+Vn7qPsafKZB7Zce gpvg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fvKnVWA6; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-211605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d2e1a72fcca58-705be3ae876si1444850b3a.106.2024.06.12.06.55.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 06:55:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-211605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fvKnVWA6; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-211605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-211605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 130CE284E57 for ; Wed, 12 Jun 2024 13:54:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C8CB217F4EC; Wed, 12 Jun 2024 13:53:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fvKnVWA6" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2B7C17F386; Wed, 12 Jun 2024 13:53:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718200393; cv=none; b=Dd+6ZCK/ng7/RLzLdaAtqzvnvdxSmYCgXBUtIC/+0j2yRS5qWZdCOIUpGCd0AW67mCVxK4Aj9L9n5FmJoUjRR2N3krOhxBRjA5puPvy98ATl7dq1F9RZpCJ6+h7u0bVxZn83RVCc1pcCFw91MpuRuorDsmp8ysuJex52VSiVLUc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718200393; c=relaxed/simple; bh=9ZhMiQb8F+YQkrSmW/uvqq+sOj8IhPHHedOkJlMaFF4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=S495T8X2nM/VLsUsOARHVIyzKT7Wj45+vN0xYrrmqr4ncIHF+I03RMeSAHOHccBNX/YBMo667UmD23eV//CJj4rqCszCGQrsiTOLfQcjii4SyNPoriTllxRQKpz3eKIWoQOw3tXZACtkYQ/pbNa6G5YmvAL1hSSvgA00jtL183M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fvKnVWA6; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27E7AC4AF4D; Wed, 12 Jun 2024 13:53:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1718200393; bh=9ZhMiQb8F+YQkrSmW/uvqq+sOj8IhPHHedOkJlMaFF4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=fvKnVWA67l2XCec23FfIZmoq1TS+spuTVbQympdCm7UMttUJCZLZZLe64EqSf6FXI 8SCU86o5e5aoAixswN3wUaJ9u6l7ri5xkVbcLVW9bKm/5jiyPilSe5xkPiYXKmwTis NtSzPwrdd/y9XD64ii4OrGOevxIt0b3ekkUY846QVMYQnINaBz9qk+4D3+o9V5Ql2q IFHVXcjOrFJi/a6jiLCUYoxUEV5tPmGfvBAInTaRGLW8plyEHBDjz2GPCXSQ06H6TE t7Lgu1AcIMYvUQUtQseFJiMcExfYzQi8SHvi6GCrBOQaWL5mV533wS4UAq6Eh618B6 4jbj+ACrX4XAg== Date: Wed, 12 Jun 2024 15:53:08 +0200 From: Christian Brauner To: NeilBrown Cc: Alexander Viro , Jan Kara , Amir Goldstein , James Clark , ltp@lists.linux.it, linux-nfs@vger.kernel.org, LKML , linux-fsdevel@vger.kernel.org Subject: Re: [PATCH v2] VFS: generate FS_CREATE before FS_OPEN when ->atomic_open used. Message-ID: <20240612-nennung-ungnade-ae9bdc5f8c4c@brauner> References: <171817619547.14261.975798725161704336@noble.neil.brown.name> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <171817619547.14261.975798725161704336@noble.neil.brown.name> On Wed, Jun 12, 2024 at 05:09:55PM +1000, NeilBrown wrote: > > When a file is opened and created with open(..., O_CREAT) we get > both the CREATE and OPEN fsnotify events and would expect them in that > order. For most filesystems we get them in that order because > open_last_lookups() calls fsnofify_create() and then do_open() (from > path_openat()) calls vfs_open()->do_dentry_open() which calls > fsnotify_open(). > > However when ->atomic_open is used, the > do_dentry_open() -> fsnotify_open() > call happens from finish_open() which is called from the ->atomic_open > handler in lookup_open() which is called *before* open_last_lookups() > calls fsnotify_create. So we get the "open" notification before > "create" - which is backwards. ltp testcase inotify02 tests this and > reports the inconsistency. > > This patch lifts the fsnotify_open() call out of do_dentry_open() and > places it higher up the call stack. There are three callers of > do_dentry_open(). > > For vfs_open() and kernel_file_open() the fsnotify_open() is placed > directly in that caller so there should be no behavioural change. > > For finish_open() there are two cases: > - finish_open is used in ->atomic_open handlers. For these we add a > call to fsnotify_open() at the top of do_open() if FMODE_OPENED is > set - which means do_dentry_open() has been called. > - finish_open is used in ->tmpfile() handlers. For these a similar > call to fsnotify_open() is added to vfs_tmpfile() > > With this patch NFSv3 is restored to its previous behaviour (before > ->atomic_open support was added) of generating CREATE notifications > before OPEN, and NFSv4 now has that same correct ordering that is has > not had before. I haven't tested other filesystems. > > Fixes: 7c6c5249f061 ("NFS: add atomic_open for NFSv3 to handle O_TRUNC correctly.") > Reported-by: James Clark > Closes: https://lore.kernel.org/all/01c3bf2e-eb1f-4b7f-a54f-d2a05dd3d8c8@arm.com > Signed-off-by: NeilBrown > --- We should take this is a bugfix because it doesn't change behavior. But then we should follow this up with a patch series that tries to rectify the open/close imbalance because I find that pretty ugly. That's at least my opinion. We should aim to only generate an open event when may_open() succeeds and don't generate a close event when the open has failed. Maybe: +#ifdef CONFIG_FSNOTIFY +#define file_nonotify(f) ((f)->f_mode |= __FMODE_NONOTIFY) +#else +#define file_nonotify(f) ((void)(f)) +#endif will do. Basic open permissions failing should count as failure to open and thus also turn of a close event. The somewhat ugly part is imho that security hooks introduce another layer of complexity. While we do count security_file_permission() as a failure to open we wouldn't e.g., count security_file_post_open() as a failure to open (Though granted here that "*_post_open()" makes it easier.). But it is really ugly that LSMs get to say "no" _after_ the file has been opened. I suspect this is some IMA or EVM thing where they hash the contents or something but it's royally ugly and I complained about this before. But maybe such things should just generate an LSM layer event via fsnotify in the future (FSNOTIFY_MAC) or something... Then userspace can see "Hey, the VFS said yes but then the MAC stuff said no."