Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH v3] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
@ 2026-12-03  1:30 Shuai Liu
  2026-12-03  1:47 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-12-03  1:30 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, jack, adilger.kernel, libaokun, ojaswin, ritesh.list,
	yi.zhang, djwong, zhaimingbing, linux-fsdevel, linux-kernel,
	syzbot+68178aa1e39c8606392e, Shuai Liu

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, 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

Fix this by:

- loading sb->s_encoding when runtime tune_sb enables casefold.  The
  encoding is probed (utf8_load + utf8_unload) before the feature is
  committed so a load failure cannot leave an on-disk CASEFOLD feature
  that the filesystem can never mount again.  sb->s_encoding is
  published after the superblock update, under a mutex shared by
  concurrent tune_sb callers so that only one unicode_map is loaded.

- publishing sb->s_encoding with smp_store_release() in
  ext4_encoding_init() so that lockless readers never observe a
  non-NULL map before utf8_load()'s internal stores are visible.

- adding NULL guards with READ_ONCE() on the sb->s_encoding reads in
  ext4_match(), ext4_fname_setup_ci_filename() and ext4fs_dirhash()
  so that the transient feature-vs-encoding window cannot dereference
  a NULL map.

- rejecting runtime casefold enablement in
  ext4_validate_tune_sb_params() when CONFIG_UNICODE is disabled;
  otherwise the feature would be committed while s_encoding can never
  be loaded.

The ext4_encoding_init() stub is moved from super.c into ext4.h so it
is available to callers in other translation units.

Note that rejecting runtime casefold enablement altogether (leaving it
to offline tune2fs) would avoid the feature-flag vs. s_encoding
visibility window entirely.  This fix keeps the feature usable at
runtime since that is what the ioctl is designed for, at the cost of
the guards above; if offline-only enablement is preferred, the probe
and publish can be dropped and the request rejected in
ext4_validate_tune_sb_params() instead.

Verified with QEMU/KASAN two-state testing of the reproducer chain
above: the unpatched kernel reports GPF in utf8nlookup, the patched
kernel completes 1000 lookups in the casefolded directory cleanly.

Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
Link: https://syzbot.org/bug?extid=68178aa1e39c8606392e
Link: https://lore.kernel.org/all/20261202100056.1419-1-zhaimingbing@cmss.chinamobile.com/
Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=68178aa1e39c8606392e
Signed-off-by: Shuai Liu <847807867@qq.com>
---

An independent fix for this bug was posted by Mingbing Zhai (Link
above).  This version additionally probes the encoding before the
feature is committed (a failed utf8_load() after commit leaves an
on-disk CASEFOLD feature that the filesystem can never mount again),
serialises concurrent tune_sb callers so that only one unicode_map is
loaded, publishes s_encoding with release ordering, and guards the
feature-vs-encoding window in the lookup paths.

Changes in v3:
  - Drop generic_set_sb_d_ops() call: updating sb->s_d_op at runtime races
    with concurrent __d_alloc() readers of __s_d_op (Sashiko)
  - Probe encoding loadability before committing the CASEFOLD feature, and
    publish sb->s_encoding after the superblock update instead of loading
    after mnt_drop_write_file(); prevents both an unmountable filesystem
    when utf8_load() fails and the feature-committed-but-encoding-NULL
    window (Sashiko)
  - Serialise concurrent tune_sb enablement with a mutex to avoid leaking
    a unicode_map (Sashiko)
  - Publish s_encoding with smp_store_release() before s_encoding_flags
    (Sashiko)
  - Reject runtime casefold enablement when CONFIG_UNICODE=n (Sashiko)
  - Found during QEMU verification: the feature gate in ext4_encoding_init()
    must be kept; an earlier revision dropped it and broke mount on every
    ext4 filesystem without casefold

Changes in v2:
  - Add NULL guard in ext4_match() and use READ_ONCE() on the s_encoding
    reads in ext4_match(), ext4_fname_setup_ci_filename() and
    ext4fs_dirhash() (Sashiko)
  - Call ext4_encoding_init() while still holding the mnt write access
    (Sashiko)
  - Add Fixes tag (Sashiko)
  - Move the ext4_encoding_init() stub for CONFIG_UNICODE=n into ext4.h

 fs/ext4/ext4.h  | 12 ++++++++++++
 fs/ext4/hash.c  |  3 ++-
 fs/ext4/ioctl.c | 31 +++++++++++++++++++++++++++++++
 fs/ext4/namei.c |  6 +++++-
 fs/ext4/super.c | 40 +++++++++++++++++++++++++++++++---------
 5 files changed, 81 insertions(+), 11 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index dc2c181337a16..c6a338ae44f5d 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -2958,6 +2958,8 @@ 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_encoding_probe(struct super_block *sb, __u16 encoding_id);
 extern int ext4_fname_setup_ci_filename(struct inode *dir,
 					const struct qstr *iname,
 					struct ext4_filename *fname);
@@ -2968,6 +2970,16 @@ 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_encoding_probe(struct super_block *sb, __u16 encoding_id)
+{
+	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..515ac455074d2 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1479,6 +1479,8 @@ static int ext4_validate_tune_sb_params(struct super_block *sb,
 			~le32_to_cpu(es->s_feature_ro_compat);
 		if (params->set_feature_incompat_mask &
 		    EXT4_FEATURE_INCOMPAT_CASEFOLD) {
+			if (!IS_ENABLED(CONFIG_UNICODE))
+				return -EINVAL;
 			enabling_casefold = 1;
 			if (!(params->set_flags & EXT4_TUNE_FL_ENCODING)) {
 				params->encoding = EXT4_ENC_UTF8_12_1;
@@ -1539,6 +1541,7 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
 	struct super_block *sb = file_inode(filp)->i_sb;
 	struct ext4_sb_info *sbi = EXT4_SB(sb);
 	int ret;
+	bool enabling_casefold;
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -1550,11 +1553,39 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
 	if (ret)
 		return ret;
 
+	enabling_casefold = (params.set_flags & EXT4_TUNE_FL_EDIT_FEATURES) &&
+		(params.set_feature_incompat_mask &
+		 EXT4_FEATURE_INCOMPAT_CASEFOLD) &&
+		!ext4_has_feature_casefold(sb);
+
 	ret = mnt_want_write_file(filp);
 	if (ret)
 		return ret;
 
+	/* A failed utf8_load() after commit leaves an unmountable fs. */
+	if (enabling_casefold && !READ_ONCE(sb->s_encoding)) {
+		ret = ext4_encoding_probe(sb, params.encoding);
+		if (ret) {
+			mnt_drop_write_file(filp);
+			return ret;
+		}
+	}
+
 	ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, &params);
+
+	/* Serialise concurrent callers: only one unicode_map may load. */
+	if (ret == 0 && enabling_casefold) {
+		static DEFINE_MUTEX(casefold_enable_lock);
+
+		mutex_lock(&casefold_enable_lock);
+		ret = ext4_encoding_init(sb, sbi->s_es);
+		mutex_unlock(&casefold_enable_lock);
+		if (ret)
+			ext4_msg(sb, KERN_ERR,
+				 "runtime tune_sb: encoding_init failed (%d)",
+				 ret);
+	}
+
 	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..37431e0ffbfab 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -2055,9 +2055,8 @@ static const struct ext4_sb_encodings {
 };
 
 static const struct ext4_sb_encodings *
-ext4_sb_read_encoding(const struct ext4_super_block *es)
+ext4_sb_read_encoding(__u16 magic)
 {
-	__u16 magic = le16_to_cpu(es->s_encoding);
 	int i;
 
 	for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
@@ -4737,16 +4736,16 @@ 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);
+	encoding_info = ext4_sb_read_encoding(le16_to_cpu(es->s_encoding));
 	if (!encoding_info) {
 		ext4_msg(sb, KERN_ERR,
 			"Encoding requested by superblock is unknown");
@@ -4765,6 +4764,7 @@ static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *e
 			encoding_flags);
 		return -EINVAL;
 	}
+
 	ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
 		"%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
 		unicode_major(encoding_info->version),
@@ -4772,14 +4772,36 @@ 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;
+	/* Order wrt utf8_load() stores inside *encoding */
+	smp_store_release(&sb->s_encoding, encoding);
+	WRITE_ONCE(sb->s_encoding_flags, encoding_flags);
 
 	return 0;
 }
-#else
-static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
+
+int ext4_encoding_probe(struct super_block *sb, __u16 encoding_id)
 {
+	const struct ext4_sb_encodings *encoding_info;
+	struct unicode_map *encoding;
+
+	encoding_info = ext4_sb_read_encoding(encoding_id);
+	if (!encoding_info) {
+		ext4_msg(sb, KERN_ERR, "Encoding requested by tune_sb is unknown");
+		return -EINVAL;
+	}
+
+	encoding = utf8_load(encoding_info->version);
+	if (IS_ERR(encoding)) {
+		ext4_msg(sb, KERN_ERR,
+			 "casefold charset %s-%u.%u.%u not supported",
+			 encoding_info->name,
+			 unicode_major(encoding_info->version),
+			 unicode_minor(encoding_info->version),
+			 unicode_rev(encoding_info->version));
+		return -EINVAL;
+	}
+
+	utf8_unload(encoding);
 	return 0;
 }
 #endif
-- 
2.43.0


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

end of thread, other threads:[~2026-12-03  1:47 UTC | newest]

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

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