Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Shuai Liu" <847807867@qq.com>, linux-ext4@vger.kernel.org
Cc: tytso@mit.edu
Subject: Re: [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
Date: Wed, 02 Dec 2026 01:05:29 +0000	[thread overview]
Message-ID: <sashiko-outbox-244564@kernel.org> (raw)
In-Reply-To: <tencent_3040B83162C313DDEEB5D2112CF0F2F34D08@qq.com>

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

      reply	other threads:[~2026-12-02  1:05 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-244564@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=847807867@qq.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox