Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
@ 2026-12-01  0:45 Shuai Liu
  2026-12-01  0:57 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-12-01  0:45 UTC (permalink / raw)
  To: tytso
  Cc: Shuai Liu, linux-ext4, syzkaller-bugs, adilger.kernel, jack,
	syzbot+68178aa1e39c8606392e

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_fname_setup_ci_filename() which invokes
utf8_casefold(dir->i_sb->s_encoding, ...) 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

Fix: call ext4_encoding_init() after the tune_sb update succeeds so
sb->s_encoding gets loaded at runtime, mirroring what happens during
mount. If encoding_init fails for an unexpected reason (e.g. an
unsupported encoding version) we print a warning but do not roll
back the already-committed superblock change.

Also add defensive NULL checks at both callers that dereference
s_encoding (ext4_fname_setup_ci_filename and ext4fs_dirhash) so any
future code path that enables casefold without loading encoding wont
crash. The defensive checks let the casefold path safely fall back
to the non-casefold code when s_encoding is missing.

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.

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  | 1 +
 fs/ext4/ioctl.c | 9 +++++++++
 fs/ext4/namei.c | 1 +
 fs/ext4/super.c | 7 +------
 5 files changed, 18 insertions(+), 6 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..981eb087ec2b0 100644
--- a/fs/ext4/hash.c
+++ b/fs/ext4/hash.c
@@ -321,6 +321,7 @@ int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
 	struct qstr qstr = {.name = name, .len = len };
 
 	if (len && IS_CASEFOLDED(dir) &&
+	   dir->i_sb->s_encoding &&
 	   (!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..7830089413b31 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1557,6 +1557,15 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
 	ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, &params);
 	mnt_drop_write_file(filp);
 
+	if (ret == 0) {
+		int 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);
+	}
+
 	if (params.set_flags & EXT4_TUNE_FL_DEF_HASH_ALG)
 		sbi->s_def_hash_version = params.def_hash_alg;
 
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f4..385e6d0bda98f 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1214,6 +1214,7 @@ int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname,
 	int len;
 
 	if (!IS_CASEFOLDED(dir) ||
+	    !dir->i_sb->s_encoding ||
 	    (IS_ENCRYPTED(dir) && !fscrypt_has_encryption_key(dir))) {
 		cf_name->name = NULL;
 		return 0;
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 880f664d6c04b..9cdf610293dfe 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4737,7 +4737,7 @@ 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;
@@ -4777,11 +4777,6 @@ static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *e
 
 	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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-01  0:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  0:45 [PATCH] ext4: fix NULL s_encoding after runtime tune_sb enables casefold Shuai Liu
2026-12-01  0:57 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox