Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH] ext4: load the casefold encoding when it is enabled online
@ 2026-12-02 10:00 Mingbing Zhai
  2026-12-02 10:16 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Mingbing Zhai @ 2026-12-02 10:00 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, linux-kernel, stable, yangguikun19,
	syzbot+68178aa1e39c8606392e, Mingbing Zhai

EXT4_IOC_SET_TUNE_SB_PARAM can enable casefold on a mounted
filesystem, but the runtime unicode map (sb->s_encoding) is only
built by ext4_encoding_init() from the mount path.  With the feature
bit set and sb->s_encoding left NULL, a lookup in a casefolded
directory dereferences NULL:

  KASAN: null-ptr-deref in utf8nlookup fs/unicode/utf8-norm.c:306
   ext4_fname_setup_ci_filename fs/ext4/namei.c:1226
   ext4_lookup fs/ext4/namei.c:1619

Load the encoding after enabling the feature, and refuse to enable
it when the kernel has no unicode support, like the mount path does.

Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=68178aa1e39c8606392e
Reported-by: Guikun Yang <yangguikun19@qq.com>
Link: https://lore.kernel.org/r/tencent_B079992E4541FB5447D9A4067D6F8D40380A@qq.com
Cc: stable@vger.kernel.org
---
 fs/ext4/ext4.h  |  9 +++++++++
 fs/ext4/ioctl.c | 11 +++++++++++
 fs/ext4/super.c |  7 +------
 3 files changed, 21 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index dc2c181337a16..001b95a78cfff 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3494,6 +3494,15 @@ extern int ext4_convert_meta_bg_test(struct super_block *sb,
 #endif
 
 /* super.c */
+#if IS_ENABLED(CONFIG_UNICODE)
+int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es);
+#else
+static inline int ext4_encoding_init(struct super_block *sb,
+				      struct ext4_super_block *es)
+{
+	return 0;
+}
+#endif
 extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
 					 sector_t block, blk_opf_t op_flags);
 extern struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 83630cd40712c..f227304d3490c 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1480,6 +1480,8 @@ static int ext4_validate_tune_sb_params(struct super_block *sb,
 		if (params->set_feature_incompat_mask &
 		    EXT4_FEATURE_INCOMPAT_CASEFOLD) {
 			enabling_casefold = 1;
+			if (!IS_ENABLED(CONFIG_UNICODE))
+				return -EOPNOTSUPP;
 			if (!(params->set_flags & EXT4_TUNE_FL_ENCODING)) {
 				params->encoding = EXT4_ENC_UTF8_12_1;
 				params->set_flags |= EXT4_TUNE_FL_ENCODING;
@@ -1555,6 +1557,15 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
 		return ret;
 
 	ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, &params);
+
+	if ((params.set_feature_incompat_mask &
+	     EXT4_FEATURE_INCOMPAT_CASEFOLD) &&
+	    ext4_has_feature_casefold(sb)) {
+		int err = ext4_encoding_init(sb, sbi->s_es);
+
+		if (err && !ret)
+			ret = err;
+	}
 	mnt_drop_write_file(filp);
 
 	if (params.set_flags & EXT4_TUNE_FL_DEF_HASH_ALG)
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

* Re: [PATCH] ext4: load the casefold encoding when it is enabled online
  2026-12-02 10:00 [PATCH] ext4: load the casefold encoding when it is enabled online Mingbing Zhai
@ 2026-12-02 10:16 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02 10:16 UTC (permalink / raw)
  To: Mingbing Zhai; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] ext4: missing Signed-off-by trailer in commit message
- [High] ext4: ext4_ioctl_set_tune_sb() misses generic_ci_dentry_ops on
  casefold enable
- [Medium] ext4: ext4_ioctl_set_tune_sb() enables casefold before
  ext4_encoding_init()
- [Medium] ext4: concurrent ext4_encoding_init() calls leak unicode_map
  and module ref
- [Medium] ext4: test_validate_tune_sb_params() fails when
  CONFIG_UNICODE is disabled
--

commit 091d1e5fe40a962324e509a8c40b8bbc5b4f7c7d
Author: Mingbing Zhai <zhaimingbing@cmss.chinamobile.com>

> ext4: load the casefold encoding when it is enabled online
>
> EXT4_IOC_SET_TUNE_SB_PARAM can enable casefold on a mounted
> filesystem, but the runtime unicode map (sb->s_encoding) is only
> built by ext4_encoding_init() from the mount path.  With the feature
> bit set and sb->s_encoding left NULL, a lookup in a casefolded
> directory dereferences NULL:
>
>   KASAN: null-ptr-deref in utf8nlookup fs/unicode/utf8-norm.c:306
>    ext4_fname_setup_ci_filename fs/ext4/namei.c:1226
>    ext4_lookup fs/ext4/namei.c:1619
>
> Load the encoding after enabling the feature, and refuse to enable
> it when the kernel has no unicode support, like the mount path does.
>
> Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
> Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=68178aa1e39c8606392e
> Reported-by: Guikun Yang <yangguikun19@qq.com>
> Link: https://lore.kernel.org/r/tencent_B079992E4541FB5447D9A4067D6F8D40380A@qq.com
> Cc: stable@vger.kernel.org

[Severity: High]
The commit message is missing a Signed-off-by: trailer.

> diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
> index 83630cd40712c..f227304d3490c 100644
> --- a/fs/ext4/ioctl.c
> +++ b/fs/ext4/ioctl.c
> @@ -1480,6 +1480,8 @@ static int ext4_validate_tune_sb_params(struct super_block *sb,
>  		if (params->set_feature_incompat_mask &
>  		    EXT4_FEATURE_INCOMPAT_CASEFOLD) {
>  			enabling_casefold = 1;
> +			if (!IS_ENABLED(CONFIG_UNICODE))
> +				return -EOPNOTSUPP;
>  			if (!(params->set_flags & EXT4_TUNE_FL_ENCODING)) {
>  				params->encoding = EXT4_ENC_UTF8_12_1;
>  				params->set_flags |= EXT4_TUNE_FL_ENCODING;

[Severity: Medium]
Does this break test_validate_tune_sb_params() when CONFIG_UNICODE is
disabled?

CONFIG_EXT4_KUNIT_TESTS does not depend on CONFIG_UNICODE, and
test_validate_tune_sb_params() in fs/ext4/ioctl-test.c unconditionally
expects 0 when enabling EXT4_FEATURE_INCOMPAT_CASEFOLD:

fs/ext4/ioctl-test.c:test_validate_tune_sb_params() {
    ...
	memset(&params, 0, sizeof(params));
	params.set_flags = EXT4_TUNE_FL_EDIT_FEATURES;
	params.set_feature_incompat_mask = EXT4_FEATURE_INCOMPAT_CASEFOLD;
	KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, &params),
			0);
	KUNIT_EXPECT_EQ(test, params.encoding, EXT4_ENC_UTF8_12_1);
	KUNIT_EXPECT_EQ(test, params.encoding_flags, 0);
    ...
}

Also, EXT4_TUNE_SET_INCOMPAT_SUPP still includes
EXT4_FEATURE_INCOMPAT_CASEFOLD unconditionally, so
ext4_ioctl_get_tune_sb() continues to advertise casefold as supported when
!IS_ENABLED(CONFIG_UNICODE).

> @@ -1555,6 +1557,15 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,
>  		return ret;
>  
>  	ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, &params);
> +
> +	if ((params.set_feature_incompat_mask &
> +	     EXT4_FEATURE_INCOMPAT_CASEFOLD) &&
> +	    ext4_has_feature_casefold(sb)) {
> +		int err = ext4_encoding_init(sb, sbi->s_es);
> +
> +		if (err && !ret)
> +			ret = err;
> +	}
>  	mnt_drop_write_file(filp);

[Severity: High]
Does enabling casefold online leave the VFS dcache without
generic_ci_dentry_ops?

On the mount path in __ext4_fill_super(), ext4_encoding_init() runs before
generic_set_sb_d_ops(sb) and d_make_root(root), which sets
sb->__s_d_op = &generic_ci_dentry_ops and adds DCACHE_OP_HASH |
DCACHE_OP_COMPARE to sb->s_d_flags.  When a filesystem is mounted without
casefold, sb->s_encoding is NULL at mount time, so generic_ci_dentry_ops is
not installed.

Calling ext4_encoding_init() later from ext4_ioctl_set_tune_sb() populates
sb->s_encoding without updating sb->__s_d_op, sb->s_d_flags, or existing
cached dentries.  As a result, VFS dcache lookups in casefolded directories
remain case-sensitive while ext4_lookup() matches case-insensitively on
disk, creating duplicate positive dentry aliases with different casings for
the same regular file inode and bypassing SB_ENC_STRICT_MODE_FL UTF-8
checks in generic_ci_d_hash().

[Severity: Medium]
Can setting EXT4_FEATURE_INCOMPAT_CASEFOLD in ext4_update_superblocks_fn()
before ext4_encoding_init() still leave sb->s_encoding NULL while
ext4_has_feature_casefold(sb) is true?

CPU0                                  CPU1
ext4_ioctl_set_tune_sb()
  ext4_update_superblocks_fn()
    ext4_update_primary_sb()
      ext4_sb_setparams()
        /* sets CASEFOLD in s_es */
                                      ext4_fileattr_set()
                                        /* sets EXT4_CASEFOLD_FL */
                                      ext4_lookup()
                                        ...
                                          utf8nlookup()
                                            /* NULL deref on s_encoding */
  ext4_encoding_init()
    sb->s_encoding = encoding;

ext4_update_primary_sb() and the backup superblock loop perform synchronous
buffer I/O after EXT4_FEATURE_INCOMPAT_CASEFOLD is already live in
sbi->s_es.  In addition, if ext4_encoding_init() fails (for example when
utf8_load() fails to load utf8data.ko or runs out of memory), the feature
bit is not rolled back in sbi->s_es or on disk, and retrying
EXT4_IOC_SET_TUNE_SB_PARAM cannot initialize sb->s_encoding because
ext4_validate_tune_sb_params() masks out features already set in
es->s_feature_incompat.

[Severity: Medium]
Can concurrent EXT4_IOC_SET_TUNE_SB_PARAM calls race in
ext4_encoding_init() and leak a struct unicode_map and utf8data module
reference?

Both ext4_validate_tune_sb_params() and ext4_encoding_init() run outside
the EXT4_FLAGS_RESIZING lock used by ext4_update_superblocks_fn(), and
ext4_encoding_init() is called even when ext4_update_superblocks_fn()
returns -EBUSY.  Two threads enabling casefold concurrently can both retain
EXT4_FEATURE_INCOMPAT_CASEFOLD in params.set_feature_incompat_mask, both
observe sb->s_encoding == NULL in ext4_encoding_init(), and both call
utf8_load(), overwriting the first sb->s_encoding pointer and leaking its
allocation and symbol_request(utf8_data_table) reference.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202100056.1419-1-zhaimingbing@cmss.chinamobile.com?part=1

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

end of thread, other threads:[~2026-12-02 10:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 10:00 [PATCH] ext4: load the casefold encoding when it is enabled online Mingbing Zhai
2026-12-02 10:16 ` sashiko-bot

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