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

* Re: [PATCH] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
  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
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-01  0:57 UTC (permalink / raw)
  To: Shuai Liu; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: NULL s_encoding dereference in ext4_match() via
  generic_ci_match()
- [Medium] ext4: unlocked runtime ext4_encoding_init() in
  ext4_ioctl_set_tune_sb()
- [Medium] ext4: racy s_encoding reads in ext4fs_dirhash() and
  ext4_encoding_init()
- [Medium] ext4: runtime casefold enablement in ext4_ioctl_set_tune_sb()
  misses d_op
- [Low] ext4: missing Fixes trailer for commit 04a91570ac67
--

commit a997535bd70f67593a441110fc016309b4deca29
Author: Shuai Liu <847807867@qq.com>

> ext4: fix NULL s_encoding after runtime tune_sb enables casefold
> 
> 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>

[Severity: Low]
This fixes a bug in runtime superblock tuning.  Should it include:

  Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")

[ ... ]

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

[Severity: Medium]
Can ext4fs_dirhash() still pass a NULL um to utf8_casefold() when
dir->i_sb->s_encoding is initialized concurrently at runtime?

ext4fs_dirhash() loads um = dir->i_sb->s_encoding on entry, then re-reads
dir->i_sb->s_encoding in the condition above before calling
utf8_casefold(um, ...):

CPU0                                  CPU1
ext4fs_dirhash()
  um = dir->i_sb->s_encoding; (NULL)
                                      ext4_ioctl_set_tune_sb()
                                        ext4_encoding_init()
                                          sb->s_encoding = encoding;
  if (... && dir->i_sb->s_encoding)
    utf8_casefold(um, ...)  <-- NULL pointer dereference

ext4_encoding_init() also writes sb->s_encoding before sb->s_encoding_flags
without release ordering, so concurrent readers on a live mount can observe
a non-NULL sb->s_encoding before sb->s_encoding_flags is visible.

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

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

ext4_encoding_init() runs after ext4_update_superblocks_fn() has cleared
EXT4_FLAGS_RESIZING and mnt_drop_write_file() has dropped write access.
Because ext4_encoding_init() checks !sb->s_encoding without a lock and then
sleeps in utf8_load(), two callers can both pass the check and overwrite
sb->s_encoding, leaking the first encoding and its symbol_request()
reference on unmount.

In addition, ext4_update_primary_sb() sets EXT4_FEATURE_INCOMPAT_CASEFOLD
in sbi->s_es before backup superblocks are updated, so if a backup update
or journal stop fails (ret != 0), ext4_encoding_init() is skipped even
though casefold is already enabled in sbi->s_es.

[Severity: Medium]
Does enabling casefold at runtime leave generic_ci_dentry_ops unset on the
superblock and existing cached dentries?

At mount time, __ext4_fill_super() calls generic_set_sb_d_ops(sb), which
installs generic_ci_dentry_ops only if sb->s_encoding is already non-NULL.
When sb->s_encoding is populated later via ext4_ioctl_set_tune_sb(),
sb->__s_d_op and existing dentries lack DCACHE_OP_HASH | DCACHE_OP_COMPARE,
so VFS dcache lookups remain case-sensitive while ext4_lookup() matches
case-insensitively on disk, creating duplicate positive dentries with
different casing for the same inode and bypassing strict encoding checks in
generic_ci_d_hash().

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

[Severity: Medium]
When dir->i_sb->s_encoding is NULL on a casefolded directory (for example,
when ext4_encoding_init() fails and logs the warning above), does lookup
still dereference a NULL s_encoding in ext4_match() -> generic_ci_match()?

Returning 0 here with cf_name->name = NULL lets __ext4_find_entry()
continue into ext4_search_dir() -> ext4_match():

fs/ext4/namei.c:ext4_match() {
    ...
	if (IS_CASEFOLDED(parent) &&
	    (!IS_ENCRYPTED(parent) || fscrypt_has_encryption_key(parent))) {
		...
		return generic_ci_match(parent, fname->usr_fname,
					&fname->cf_name, de->name,
					de->name_len) > 0;
	}
    ...
}

Because ext4_match() does not check parent->i_sb->s_encoding, and
fname->cf_name.name is NULL, generic_ci_match() falls back to
utf8_strncasecmp(parent->i_sb->s_encoding, ...) on the first entry ('.')
and dereferences NULL in utf8nlookup().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_A8E6DCBBA4155872E5A59BFE7031C49E2709@qq.com?part=1

^ permalink raw reply	[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