All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
@ 2026-12-02  0:52 Shuai Liu
  2026-12-02  1:05 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Shuai Liu @ 2026-12-02  0:52 UTC (permalink / raw)
  To: linux-ext4
  Cc: tytso, jack, 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 (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, &params);
+
+	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


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

* Re: [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
  2026-12-02  0:52 [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold Shuai Liu
@ 2026-12-02  1:05 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-02  1:05 UTC (permalink / raw)
  To: Shuai Liu, linux-ext4; +Cc: tytso

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] ext4: runtime generic_set_sb_d_ops() in
  ext4_ioctl_set_tune_sb() is unsafe
- [Medium] ext4: unlocked runtime ext4_encoding_init() leaks unicode_map
  on race
- [Medium] ext4: ext4_ioctl_set_tune_sb() enables casefold before
  ext4_encoding_init()
- [Medium] ext4: missing release barrier and wrong store order in
  ext4_encoding_init()
--

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

ext4: fix NULL s_encoding after runtime tune_sb enables casefold

This commit initializes sb->s_encoding and default dentry operations when
EXT4_IOC_SET_TUNE_SB_PARAM enables the casefold feature at runtime, and
adds NULL checks for s_encoding in the directory hashing and lookup paths.

Link: https://syzbot.org/bug?extid=68178aa1e39c8606392e

[ ... ]

> 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, &params);
> +
> +	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);

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

ext4_update_superblocks_fn() clears EXT4_FLAGS_RESIZING before returning,
and mnt_want_write_file() only takes a shared freeze-protection counter.
Two callers can both pass the unlocked READ_ONCE(sb->s_encoding) check in
ext4_encoding_init(), sleep in utf8_load(), and overwrite sb->s_encoding,
leaking the first unicode_map and symbol_request(utf8_data_table)
reference because ext4_put_super() only unloads sb->s_encoding once.

[Severity: Medium]
Does enabling EXT4_FEATURE_INCOMPAT_CASEFOLD before ext4_encoding_init()
still leave sb->s_encoding as NULL while ext4_has_feature_casefold(sb) is
true?

mnt_want_write_file() does not block concurrent operations, so
ext4_has_feature_casefold(sb) becomes true as soon as
ext4_update_primary_sb() updates sbi->s_es.  Moreover, sb->s_encoding
remains permanently NULL after casefold is committed to sbi->s_es if
ext4_encoding_init() fails (which only logs a warning), if
ext4_update_superblocks_fn() fails after updating the primary superblock,
or if CONFIG_UNICODE is disabled (where ext4_validate_tune_sb_params()
still allows setting EXT4_FEATURE_INCOMPAT_CASEFOLD and the
ext4_encoding_init() stub returns 0).

Because ext4_fileattr_set() only checks ext4_has_feature_casefold(sb),
directories can then be marked with EXT4_CASEFOLD_FL while sb->s_encoding
is NULL.  On an encrypted casefolded directory with a key, the new !um
early return in ext4_fname_setup_ci_filename() skips computing
name->hinfo, while dx_probe(), make_indexed_dir(), and
ext4_insert_dentry() assume fname->hinfo was precomputed and write zero
hashes into on-disk directory entries and HTree nodes.

> +
> +		/*
> +		 * 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);
> +	}

[Severity: Medium]
Can calling generic_set_sb_d_ops() on a live superblock race with
__d_alloc() and cause a NULL pointer dereference on path lookup?

generic_set_sb_d_ops() is a mount-time helper meant to run before
sb->s_root is created.  It calls set_default_d_op(sb,
&generic_ci_dentry_ops), which updates s->__s_d_op and s->s_d_flags with
plain unsynchronized stores while concurrent lookups run __d_alloc():

CPU0                                  CPU1
ext4_ioctl_set_tune_sb()              __d_alloc()
  generic_set_sb_d_ops(sb)
    set_default_d_op(sb, ...)
                                        dentry->d_op = sb->__s_d_op;
      s->__s_d_op = ops;
      s->s_d_flags = ... | flags;
                                        dentry->d_flags = sb->s_d_flags;
                                      ...
                                      link_path_walk()
                                        parent->d_op->d_hash(...)
                                          <-- NULL pointer dereference

Before casefold is enabled, sb->__s_d_op is either NULL or
&generic_encrypted_dentry_ops (where .d_hash and .d_compare are NULL).
If __d_alloc() pairs the old sb->__s_d_op with the new sb->s_d_flags
(DCACHE_OP_HASH | DCACHE_OP_COMPARE), later lookups under that directory
dereference a NULL d_op or d_hash/d_compare pointer.

In addition, VFS lookups test DCACHE_OP_HASH and DCACHE_OP_COMPARE on the
parent directory dentry, so any directory already cached before the ioctl
(such as sb->s_root) keeps its old d_flags, allowing duplicate positive
dentries with different case for the same inode and bypassing
generic_ci_d_hash() strict encoding checks.

[ ... ]

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

[Severity: Medium]
Can publishing sb->s_encoding with WRITE_ONCE() instead of
smp_store_release() allow readers on weakly-ordered architectures to
dereference uninitialized fields of *encoding?

WRITE_ONCE() emits no CPU memory barrier, so the store to sb->s_encoding
can be observed before the stores in utf8_load() that initialize
encoding->tables and encoding->ntab[], causing a lockless
READ_ONCE(dir->i_sb->s_encoding) reader in utf8_casefold() ->
utf8nlookup() to dereference a NULL pointer.

Also, publishing s_encoding_flags before s_encoding when
SB_ENC_STRICT_MODE_FL is set allows generic_ci_validate_strict_name() to
observe sb_has_strict_encoding(dir->i_sb) as true while
dir->i_sb->s_encoding is still NULL:

include/linux/fs.h:generic_ci_validate_strict_name() {
	if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
		return true;
	...
	if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
		return true;
}

That triggers WARN_ON_ONCE() and returns true without validating the
filename in __ext4_add_entry().

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

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

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

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.