From: Gabriel Krisman Bertazi <krisman@suse.de>
To: Sarthak Kukreti <sarthakkukreti@google.com>,
Theodore Ts'o <tytso@mit.edu>, Jaegeuk Kim <jaegeuk@kernel.org>,
Chao Yu <chao@kernel.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Gabriel Krisman Bertazi <krisman@collabora.co.uk>,
Eric Biggers <ebiggers@google.com>,
Dmitry Torokhov <dtor@google.com>,
linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org,
linux-f2fs-devel@lists.sourceforge.net,
linux-kernel@vger.kernel.org,
Sarthak Kukreti <sarthakkukreti@google.com>
Subject: Re: [PATCH 1/3] libfs: reject casefolded names that fold to ".", "..", or empty
Date: Mon, 16 Nov 2026 14:34:26 -0500 [thread overview]
Message-ID: <878q2swoxp.fsf@cartola> (raw)
In-Reply-To: <20261113065332.929085-2-sarthakkukreti@google.com>
Sarthak Kukreti <sarthakkukreti@google.com> writes:
> Unicode normalization and casefolding (utf8_casefold()) strips code
> points with the Default_Ignorable_Code_Point property, such as U+202E
> (RIGHT-TO-LEFT OVERRIDE) or U+200B (ZERO WIDTH SPACE). As a result:
>
> 1. A filename consisting of "." or ".." followed by ignorable characters
> (e.g. ".\xe2\x80\xae" or "..\xe2\x80\xae") bypasses VFS special-entry
> handling during path walk, but casefolds to "." or "..".
> 2. A filename consisting solely of ignorable characters (e.g.
> "\xe2\x80\xae") casefolds to the empty string "" (length 0), causing
> any two distinct ignorable-only filenames to compare and hash as
> equal.
Hi Sarthak,
Ugh. I agree this is a problem, but I'm also trying to understand first
the real impact beyond filesystem complaints during lookup, if any:
1. Looking up ".\u202e" folds to "." and matches the on-disk "." entry
(de->inode == dir->i_ino) in generic_ci_match(), triggering
EXT4_ERROR_INODE ("linked to parent dir") in ext4_lookup().
2. Looking up "..\u202e" folds to ".." and matches the on-disk ".."
entry, returning -ELOOP via d_splice_alias() (or triggering
EXT4_ERROR_INODE on the root directory).
These two look like a "do stupid things, get stupid prizes" with no
further repercussions beyond the error message. Sure, we can fix them
to throw EINVAL, but are there bigger repercussions I'm missing?
3. Filenames consisting solely of ignorable characters fold to "" (0
bytes), causing distinct ignorable-only filenames to hash and compare
equal and creating entries that e2fsck flags as corruption.
This seems concerning. Do we actually writes multiple files to the disk?
shouldn't they all be folded into the same empty string and rejected or
at least written as a single file? I'm not sure how it'd be writing
multiple files to the disk, that seems like a bigger problem.
Either way, this issue is another example of why treating ignorable code
points as 0-length strings was a bad idea. I'd very much rather
utf8_casefold would treat all ignorable code points as foldable to
themselves, (i.e. commit 5c26d2f1d3f5 ("unicode: Don't special case
ignorable code points")), but that was tried and broke applications
that relied on variant selectors code-points being ignored.
> Update generic_ci_d_hash(), generic_ci_d_compare(), generic_ci_match(),
> and generic_ci_validate_strict_name() to reject case-insensitive
> hashing, matching, and creation of filenames that casefold to "", ".",
> or "..", while preserving exact byte-equality matches (such as NFS
> export lookups of literal ".."). Using a 3-byte stack buffer with
> utf8_casefold() detects all three cases in O(1) space without heap
> allocations.
>
> Fixes: c843843e714c ("fs: Add standard casefolding support")
This bug was just made generic by c843843e714c, but the issue was
introduced by b886ee3e778e in ext4 and the equivalent f2fs support.
> Co-developed-by: Dmitry Torokhov <dtor@google.com>
> Signed-off-by: Dmitry Torokhov <dtor@google.com>
> Signed-off-by: Sarthak Kukreti <sarthakkukreti@google.com>
> ---
> fs/libfs.c | 50 ++++++++++++++++++++++++++++++++++++++++++----
> include/linux/fs.h | 31 ++++++++++++++++------------
> 2 files changed, 64 insertions(+), 17 deletions(-)
>
> diff --git a/fs/libfs.c b/fs/libfs.c
> index 27d7dc16fcb0..54296dfb8444 100644
> --- a/fs/libfs.c
> +++ b/fs/libfs.c
> @@ -1801,8 +1801,11 @@ int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
> {
> const struct dentry *parent;
> const struct inode *dir;
> + const struct unicode_map *um;
> union shortname_store strbuf;
> + unsigned char buf[3];
> struct qstr qstr;
> + int cf_len;
>
> /*
> * Attempt a case-sensitive match first. It is cheaper and
> @@ -1821,6 +1824,19 @@ int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
> if (!dir || !IS_CASEFOLDED(dir))
> return 1;
>
> + if (name_is_dot_dotdot(str, len) ||
> + name_is_dot_dotdot(name->name, name->len))
> + return 1;
This is checking for strictly encoded '.' and '..'. But I'm not sure
that happens? Do we really reach the dcache for '.' and '..' instead of
the name of the parent or cwd?
> +
> + /*
> + * utf8_strncasecmp() only matches if both names casefold to the same
> + * byte sequence, so checking @name covers @qstr as well.
> + */
> + um = dentry->d_sb->s_encoding;
> + cf_len = utf8_casefold(um, name, buf, sizeof(buf));
> + if (cf_len == 0 || (cf_len > 0 && name_is_dot_dotdot(buf, cf_len)))
> + return 1;
I think this is a bit too late in the lookup to detect these
code-points. I'd much rather we fail the lookup outright instead of
attempting to treat them as different spellings of . and .. . But isn't
this dead code because we are rejecting those strings in d_hash through
your hunk below? Is there a way we get here without the -EINVAL in
d_hash first?
If we must do it here, I suppose we'd be better off by doing the full
utf8_casefold and then using utf8_strncasecmp_folded for the comparison,
to avoid two trips to casefold the string.
But the way I'd suggest be handled is by rejecting the ignorable code
points lookup (all, but variant selectors for the reasons above), early
in the lookup path for case-insensitive directories so they never even
get to the filesystem. Wdyt?
> qstr.len = len;
> qstr.name = str;
> /*
> @@ -1839,7 +1855,7 @@ int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
> barrier();
> }
>
> - return utf8_strncasecmp(dentry->d_sb->s_encoding, name, &qstr);
> + return utf8_strncasecmp(um, name, &qstr);
> }
> EXPORT_SYMBOL(generic_ci_d_compare);
>
> @@ -1855,13 +1871,21 @@ int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
> const struct inode *dir = READ_ONCE(dentry->d_inode);
> struct super_block *sb = dentry->d_sb;
> const struct unicode_map *um = sb->s_encoding;
> + unsigned char buf[3];
> int ret;
>
> if (!dir || !IS_CASEFOLDED(dir))
> return 0;
>
> ret = utf8_casefold_hash(um, dentry, str);
> - if (ret < 0 && sb_has_strict_encoding(sb))
> + if (ret < 0) {
> + if (sb_has_strict_encoding(sb))
> + return -EINVAL;
> + return 0;
> + }
> +
> + ret = utf8_casefold(um, str, buf, sizeof(buf));
> + if (ret == 0 || (ret > 0 && name_is_dot_dotdot(buf, ret)))
> return -EINVAL;
> return 0;
Thanks,
--
Gabriel Krisman Bertazi
next prev parent reply other threads:[~2026-11-16 19:34 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 6:53 [PATCH 0/3] fs: reject casefolded names that fold to ".", "..", or empty Sarthak Kukreti
2026-11-13 6:53 ` [PATCH 1/3] libfs: " Sarthak Kukreti
2026-11-13 7:27 ` [f2fs-dev] " sashiko-bot
2026-11-16 19:34 ` Gabriel Krisman Bertazi [this message]
2026-11-16 20:05 ` Eric Biggers
2026-11-13 6:53 ` [PATCH 2/3] ext4: " Sarthak Kukreti
2026-11-13 7:04 ` sashiko-bot
2026-11-13 6:53 ` [PATCH 3/3] f2fs: " Sarthak Kukreti
2026-11-13 7:00 ` sashiko-bot
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=878q2swoxp.fsf@cartola \
--to=krisman@suse.de \
--cc=brauner@kernel.org \
--cc=chao@kernel.org \
--cc=dtor@google.com \
--cc=ebiggers@google.com \
--cc=jaegeuk@kernel.org \
--cc=krisman@collabora.co.uk \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-f2fs-devel@lists.sourceforge.net \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sarthakkukreti@google.com \
--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