From: Gabriel Krisman Bertazi <krisman@suse.de>
To: viro@zeniv.linux.org.uk, brauner@kernel.org, tytso@mit.edu,
ebiggers@kernel.org, jaegeuk@kernel.org
Cc: linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net,
Gabriel Krisman Bertazi <krisman@suse.de>,
Gabriel Krisman Bertazi <krisman@collabora.com>
Subject: [PATCH v6 5/9] libfs: Validate negative dentries in case-insensitive directories
Date: Wed, 16 Aug 2023 01:07:59 -0400 [thread overview]
Message-ID: <20230816050803.15660-6-krisman@suse.de> (raw)
In-Reply-To: <20230816050803.15660-1-krisman@suse.de>
From: Gabriel Krisman Bertazi <krisman@collabora.com>
Introduce a dentry revalidation helper to check the negative dentries of
case-insensitive filesystems. This helper is based on the fact that a
negative dentry might safe to be reused on a casefolded directory if it
was created during a case-insensitive lookup, because that kind of
lookup verifies not only the exact name doesn't exist in a directory,
but also that *any* case-equivalent name also doesn't exist. The sole
exception is during file creation, in which case we also need to make
sure the name matches case-sensitively, in order to assure the disk
name-preserving semantics.
We cover most creations by checking LOOKUP_CREATE|LOOKUP_RENAME_TARGET
flags. But, while most creations use those flags, there are filesystem
helpers that call lookup for creation with flags==0. Since we can't
know whether those are for creation, just reject the negative dentries
if there are no flags to check.
Note that we avoid taking the ->d_lock while accessing ->d_name, because
it isn't really necessary for the LOOKUP_CREATE/LOOKUP_RENAME_TARGET
case. That is because in every creation path with these flags, we know
the parent inode lock is acquired, at least for reading, thus
stabilizing the d_name, since it prevents the dentry from being
instantiated and negative dentries cannot be moved.
See also the comment in the code.
* Discussion on the ->d_name stability
d_revalidate can only be reached from 4 code paths: lookup_dcache,
__lookup_slow, lookup_open and lookup_fast:
- lookup_dcache only reaches d_revalidate with creation flags when
coming from __lookup_hash, which needs the parent locked already.
- In __lookup_slow, either the parent inode is read-locked by the
caller (lookup_slow), or it is called with no flags (lookup_one*). A
read lock suffices to prevent concurrent ->d_name modifications, with
the exception of a modification inside __d_unalias, which is not a
problem because negative dentries are not allowed to be moved with
__d_move. In addition, d_instantiate shouldn't race with this case
because its callers also acquire the parent inode lock, preventing it
from racing with lookup creation.
- lookup_open also requires the parent to be locked in the creation
case, which is done in open_last_lookups.
- lookup_fast will indeed be called with the parent unlocked, but it
shouldn never be called with LOOKUP_CREATE. Either it is called in the
link_path_walk, where nd->flags doesn't have LOOKUP_CREATE yet or in
open_last_lookups. But, in this case, it also never has LOOKUP_CREATE,
because it is only called on the !O_CREAT case, which means op->intent
doesn't have LOOKUP_CREAT (set in build_open_flags only if O_CREAT is
set).
In addition, for the LOOKUP_RENAME_TARGET, we are doing a rename, so the
parents inodes are also locked.
Reviewed-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Gabriel Krisman Bertazi <krisman@collabora.com>
---
Changes since v5:
- Use IS_CASEFOLDED directly (Eric)
- Reword commit message and comment in the code (Eric)
Changes since v4:
- Drop useless inline declaration (eric)
- Refactor to drop extra identation (Christian)
- Discuss d_instantiate
Changes since v3:
- Add comment regarding creation (Eric)
- Reorder checks to clarify !flags meaning (Eric)
- Add commit message explanaton of the inode read lock wrt.
__d_move. (Eric)
Changes since v2:
- Add comments to all rejection cases (Eric)
- safeguard against filesystem creating dentries without LOOKUP flags
---
fs/libfs.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 54 insertions(+)
diff --git a/fs/libfs.c b/fs/libfs.c
index 5b851315eeed..26bf1b832b0a 100644
--- a/fs/libfs.c
+++ b/fs/libfs.c
@@ -1462,9 +1462,63 @@ static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
return 0;
}
+static int generic_ci_d_revalidate(struct dentry *dentry,
+ const struct qstr *name,
+ unsigned int flags)
+{
+ const struct dentry *parent;
+ const struct inode *dir;
+
+ if (!d_is_negative(dentry))
+ return 1;
+
+ parent = READ_ONCE(dentry->d_parent);
+ dir = READ_ONCE(parent->d_inode);
+
+ if (!dir || !IS_CASEFOLDED(dir))
+ return 1;
+
+ /*
+ * Negative dentries created prior to turning the directory
+ * case-insensitive cannot be trusted, since they don't ensure
+ * any possible case version of the filename doesn't exist.
+ */
+ if (!d_is_casefolded_name(dentry))
+ return 0;
+
+ /*
+ * If the lookup is for creation, then a negative dentry can only be
+ * reused if it's a case-sensitive match, not just a case-insensitive
+ * one. This is needed to make the new file be created with the name
+ * the user specified, preserving case.
+ *
+ * LOOKUP_CREATE or LOOKUP_RENAME_TARGET cover most creations. In these
+ * cases, ->d_name is stable and can be compared to 'name' without
+ * taking ->d_lock because the caller must hold dir->i_rwsem. (This
+ * is because the directory lock blocks the dentry from being
+ * concurrently instantiated, and negative dentries are never moved.)
+ *
+ * All other creations actually use flags==0. These come from the edge
+ * case of filesystems calling functions like lookup_one() that do a
+ * lookup without setting the lookup flags at all. Such lookups might
+ * or might not be for creation, and if not don't guarantee stable
+ * ->d_name. Therefore, invalidate all negative dentries when flags==0.
+ */
+ if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) {
+ if (dentry->d_name.len != name->len ||
+ memcmp(dentry->d_name.name, name->name, name->len))
+ return 0;
+ } else if (!flags) {
+ return 0;
+ }
+
+ return 1;
+}
+
static const struct dentry_operations generic_ci_dentry_ops = {
.d_hash = generic_ci_d_hash,
.d_compare = generic_ci_d_compare,
+ .d_revalidate = generic_ci_d_revalidate,
};
#endif
--
2.41.0
next prev parent reply other threads:[~2023-08-16 5:09 UTC|newest]
Thread overview: 65+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-16 5:07 [PATCH v6 0/9] Support negative dentries on case-insensitive ext4 and f2fs Gabriel Krisman Bertazi
2023-08-16 5:07 ` [PATCH v6 1/9] ecryptfs: Reject casefold directory inodes Gabriel Krisman Bertazi
2023-08-16 5:07 ` [PATCH v6 2/9] 9p: Split ->weak_revalidate from ->revalidate Gabriel Krisman Bertazi
2023-08-16 5:07 ` [PATCH v6 3/9] fs: Expose name under lookup to d_revalidate hooks Gabriel Krisman Bertazi
2023-11-22 20:59 ` Al Viro
2023-08-16 5:07 ` [PATCH v6 4/9] fs: Add DCACHE_CASEFOLDED_NAME flag Gabriel Krisman Bertazi
2023-11-22 20:32 ` Al Viro
2023-08-16 5:07 ` Gabriel Krisman Bertazi [this message]
2023-11-22 20:20 ` [PATCH v6 5/9] libfs: Validate negative dentries in case-insensitive directories Al Viro
2023-08-16 5:08 ` [PATCH v6 6/9] libfs: Chain encryption checks after case-insensitive revalidation Gabriel Krisman Bertazi
2023-08-16 5:08 ` [PATCH v6 7/9] libfs: Merge encrypted_ci_dentry_ops and ci_dentry_ops Gabriel Krisman Bertazi
2023-08-16 5:08 ` [PATCH v6 8/9] ext4: Enable negative dentries on case-insensitive lookup Gabriel Krisman Bertazi
2023-08-16 5:08 ` [PATCH v6 9/9] f2fs: " Gabriel Krisman Bertazi
2023-08-17 17:06 ` [PATCH v6 0/9] Support negative dentries on case-insensitive ext4 and f2fs Eric Biggers
2023-08-21 15:52 ` Christian Brauner
2023-08-21 18:53 ` Gabriel Krisman Bertazi
2023-08-22 9:03 ` Christian Brauner
2023-10-24 22:20 ` Gabriel Krisman Bertazi
2023-10-25 13:32 ` Christian Brauner
2023-10-25 15:19 ` Gabriel Krisman Bertazi
2023-11-19 23:11 ` [f2fs-dev] " Gabriel Krisman Bertazi
[not found] ` <655a9634.630a0220.d50d7.5063SMTPIN_ADDED_BROKEN@mx.google.com>
2023-11-20 15:06 ` Christian Brauner
2023-11-20 16:59 ` Gabriel Krisman Bertazi
2023-11-20 18:07 ` Linus Torvalds
2023-11-21 2:02 ` Theodore Ts'o
2023-11-21 2:29 ` Linus Torvalds
2023-11-21 3:03 ` Linus Torvalds
2023-11-21 5:12 ` Theodore Ts'o
2023-11-22 21:04 ` Al Viro
2023-11-21 2:27 ` Al Viro
2023-11-22 21:19 ` Al Viro
2023-11-23 0:18 ` Linus Torvalds
2023-11-23 5:09 ` Al Viro
2023-11-23 15:57 ` Gabriel Krisman Bertazi
2023-11-23 17:12 ` Al Viro
2023-11-23 17:37 ` Gabriel Krisman Bertazi
2023-11-23 18:24 ` Al Viro
2023-11-23 19:06 ` Gabriel Krisman Bertazi
2023-11-23 19:53 ` Al Viro
2023-11-23 20:15 ` Al Viro
2023-11-24 15:20 ` Gabriel Krisman Bertazi
2023-11-28 0:02 ` Al Viro
2023-11-23 21:52 ` Al Viro
2023-11-24 15:22 ` Gabriel Krisman Bertazi
2023-11-25 22:01 ` Al Viro
2023-11-26 4:52 ` Al Viro
2023-11-26 18:41 ` fun with d_invalidate() vs. d_splice_alias() was " Al Viro
2023-11-27 6:38 ` Al Viro
2023-11-27 15:47 ` Eric W. Biederman
2023-11-27 16:01 ` Eric W. Biederman
2023-11-27 17:25 ` Al Viro
2023-11-27 18:26 ` Al Viro
2023-11-27 16:03 ` Al Viro
2023-11-27 16:14 ` Al Viro
2023-11-27 18:19 ` Eric W. Biederman
2023-11-27 18:43 ` Al Viro
2023-11-27 16:33 ` Christian Brauner
2023-11-29 4:53 ` Al Viro
2023-11-29 10:21 ` Christian Brauner
2023-11-29 15:19 ` Eric W. Biederman
[not found] ` <655f7665.df0a0220.58a21.e84fSMTPIN_ADDED_BROKEN@mx.google.com>
2023-11-23 16:41 ` Linus Torvalds
2023-11-23 1:12 ` Al Viro
2023-11-23 1:22 ` Al Viro
2023-11-22 3:30 ` Gabriel Krisman Bertazi
2024-01-16 19:02 ` patchwork-bot+f2fs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230816050803.15660-6-krisman@suse.de \
--to=krisman@suse.de \
--cc=brauner@kernel.org \
--cc=ebiggers@kernel.org \
--cc=jaegeuk@kernel.org \
--cc=krisman@collabora.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=tytso@mit.edu \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).