From: Shuai Liu <847807867@qq.com>
To: linux-ext4@vger.kernel.org
Cc: tytso@mit.edu, jack@suse.cz, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org,
syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com,
Shuai Liu <847807867@qq.com>
Subject: [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
Date: Wed, 2 Dec 2026 08:52:32 +0800 [thread overview]
Message-ID: <tencent_3040B83162C313DDEEB5D2112CF0F2F34D08@qq.com> (raw)
When EXT4_IOC_SET_TUNE_SB_PARAM is used at runtime to enable the
CASEFOLD incompat feature, ext4_validate_tune_sb_params() auto-sets
encoding parameters (defaulting to UTF8_12_1), and ext4_sb_setparams()
updates the on-disk superblock and the in-memory sbi->s_es copy.
However, ext4_ioctl_set_tune_sb() never calls ext4_encoding_init()
to actually load sb->s_encoding.
This leaves sb->s_encoding as NULL while ext4_has_feature_casefold()
returns true. A subsequent FS_IOC_SETFLAGS setting FS_CASEFOLD_FL on
a directory succeeds (the tune_sb validation passes), and any lookup
in that directory calls ext4_match() or ext4_fname_setup_ci_filename()
which invoke utf8_casefold() or generic_ci_match() with a NULL um
pointer.
Reproducer trace from syzbot:
mount(ext4) → EXT4_IOC_SET_TUNE_SB_PARAM(enables casefold) →
mkdir → FS_IOC_SETFLAGS(FS_CASEFOLD_FL) → open triggers lookup →
utf8_casefold(NULL, ...) → utf8nlookup+0x39 NULL ptr deref → GPF
The v1 fix called ext4_encoding_init() after tune_sb and added
defensive NULL checks in ext4_fname_setup_ci_filename() and
ext4fs_dirhash(). Sashiko AI review found five additional issues;
this v2 addresses them all:
1. ext4_match() was missed in v1 — it calls generic_ci_match() which
dereferences sb->s_encoding directly. Add a NULL guard there and
use READ_ONCE() on the s_encoding reads in ext4_match() and
ext4_fname_setup_ci_filename().
2. ext4_encoding_init() was called after mnt_drop_write_file(),
leaving a window where sb feature flags said casefold but
sb->s_encoding was still NULL. Move the call before the drop.
3. The s_encoding/s_encoding_flags assignments in
ext4_encoding_init() were not ordered, so a concurrent reader
could see a mismatched pair. Use WRITE_ONCE() with flags
published first to guarantee either-both-old or both-new.
4. ext4fs_dirhash() read s_encoding twice without READ_ONCE.
Consolidate into a single READ_ONCE() + use the local variable.
5. Missing Fixes trailer for the commit that introduced tune_sb.
Also call generic_set_sb_d_ops() after encoding_init so that newly
created dentries get case-insensitive VFS dcache lookups. Existing
dentries keep their original d_op (a pre-existing limitation of
runtime casefold enablement).
The ext4_encoding_init() stub was previously defined as static inline in
super.c for CONFIG_UNICODE=n. Move the stub declaration into ext4.h so it
is available to callers in other translation units.
Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
Link: https://syzbot.org/bug?extid=68178aa1e39c8606392e
Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=68178aa1e39c8606392e
Signed-off-by: Shuai Liu <847807867@qq.com>
---
fs/ext4/ext4.h | 6 ++++++
fs/ext4/hash.c | 3 ++-
fs/ext4/ioctl.c | 27 +++++++++++++++++++++++++++
fs/ext4/namei.c | 6 +++++-
fs/ext4/super.c | 18 +++++++++---------
5 files changed, 49 insertions(+), 11 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index dc2c181337a16..1425ad717442b 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2958,6 +2958,7 @@ extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
#if IS_ENABLED(CONFIG_UNICODE)
+extern int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es);
extern int ext4_fname_setup_ci_filename(struct inode *dir,
const struct qstr *iname,
struct ext4_filename *fname);
@@ -2968,6 +2969,11 @@ static inline void ext4_fname_free_ci_filename(struct ext4_filename *fname)
fname->cf_name.name = NULL;
}
#else
+static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
+{
+ return 0;
+}
+
static inline int ext4_fname_setup_ci_filename(struct inode *dir,
const struct qstr *iname,
struct ext4_filename *fname)
diff --git a/fs/ext4/hash.c b/fs/ext4/hash.c
index 978bd92da0ad3..59c40b313d267 100644
--- a/fs/ext4/hash.c
+++ b/fs/ext4/hash.c
@@ -315,12 +315,13 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
struct dx_hash_info *hinfo)
{
#if IS_ENABLED(CONFIG_UNICODE)
- const struct unicode_map *um = dir->i_sb->s_encoding;
+ const struct unicode_map *um = READ_ONCE(dir->i_sb->s_encoding);
int r, dlen;
unsigned char *buff;
struct qstr qstr = {.name = name, .len = len };
if (len && IS_CASEFOLDED(dir) &&
+ um &&
(!IS_ENCRYPTED(dir) || fscrypt_has_encryption_key(dir))) {
buff = kzalloc(PATH_MAX, GFP_KERNEL);
if (!buff)
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 83630cd40712c..b661a6bea2ff1 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1555,6 +1555,33 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
return ret;
ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, ¶ms);
+
+ if (ret == 0) {
+ int enc_ret;
+
+ /*
+ * Call ext4_encoding_init() before mnt_drop_write_file()
+ * so that the in-memory s_encoding is established while
+ * we still hold the write barrier. This prevents the
+ * window where ext4_has_feature_casefold() returns true
+ * but sb->s_encoding is still NULL.
+ */
+ enc_ret = ext4_encoding_init(sb, sbi->s_es);
+ if (enc_ret < 0)
+ ext4_msg(sb, KERN_WARNING,
+ "runtime tune_sb: encoding_init failed (%d)",
+ enc_ret);
+
+ /*
+ * After runtime casefold enablement, switch the default
+ * dentry operations so that newly created dentries
+ * perform case-insensitive VFS dcache lookups.
+ */
+ if (IS_ENABLED(CONFIG_UNICODE) &&
+ ext4_has_feature_casefold(sb))
+ generic_set_sb_d_ops(sb);
+ }
+
mnt_drop_write_file(filp);
if (params.set_flags & EXT4_TUNE_FL_DEF_HASH_ALG)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f4..0daa6a4bba30d 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1211,9 +1211,12 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
struct qstr *cf_name = &name->cf_name;
unsigned char *buf;
struct dx_hash_info *hinfo = &name->hinfo;
+ const struct unicode_map *um;
int len;
+ um = READ_ONCE(dir->i_sb->s_encoding);
if (!IS_CASEFOLDED(dir) ||
+ !um ||
(IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
cf_name->name = NULL;
return 0;
@@ -1223,7 +1226,7 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
if (!buf)
return -ENOMEM;
- len = utf8_casefold(dir->i_sb->s_encoding, iname, buf, EXT4_NAME_LEN);
+ len = utf8_casefold(um, iname, buf, EXT4_NAME_LEN);
if (len <= 0) {
kfree(buf);
buf = NULL;
@@ -1266,6 +1269,7 @@ static bool ext4_match(struct inode *parent,
#if IS_ENABLED(CONFIG_UNICODE)
if (IS_CASEFOLDED(parent) &&
+ READ_ONCE(parent->i_sb->s_encoding) &&
(!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
/*
* Just checking IS_ENCRYPTED(parent) below is not
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 880f664d6c04b..8e466a7c14e5b 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4737,13 +4737,14 @@ static int ext4_inode_info_init(struct super_block *sb,
}
#if IS_ENABLED(CONFIG_UNICODE)
-static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
+int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
{
const struct ext4_sb_encodings *encoding_info;
struct unicode_map *encoding;
__u16 encoding_flags = le16_to_cpu(es->s_encoding_flags);
- if (!ext4_has_feature_casefold(sb) || sb->s_encoding)
+ if (!ext4_has_feature_casefold(sb) ||
+ READ_ONCE(sb->s_encoding))
return 0;
encoding_info = ext4_sb_read_encoding(es);
@@ -4772,16 +4773,15 @@ static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *e
unicode_rev(encoding_info->version),
encoding_flags);
- sb->s_encoding = encoding;
- sb->s_encoding_flags = encoding_flags;
+ /*
+ * Publish s_encoding_flags before s_encoding so that concurrent
+ * readers either see both old values or both new values.
+ */
+ WRITE_ONCE(sb->s_encoding_flags, encoding_flags);
+ WRITE_ONCE(sb->s_encoding, encoding);
return 0;
}
-#else
-static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
-{
- return 0;
-}
#endif
static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es)
--
2.43.0
reply other threads:[~2026-12-02 0:52 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=tencent_3040B83162C313DDEEB5D2112CF0F2F34D08@qq.com \
--to=847807867@qq.com \
--cc=jack@suse.cz \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com \
--cc=tytso@mit.edu \
/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
all inboxes | Powered by JetHome®