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 v3] ext4: fix NULL s_encoding after runtime tune_sb enables casefold
Date: Thu, 03 Dec 2026 01:47:54 +0000	[thread overview]
Message-ID: <sashiko-outbox-246906@kernel.org> (raw)
In-Reply-To: <tencent_F27193C546272ADCA5A46D593AE4139ABE09@qq.com>

Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] ext4: build failure in ext4_ioctl_set_tune_sb() when
  CONFIG_UNICODE=n
- [Medium] ext4: runtime casefold enablement in ext4_ioctl_set_tune_sb()
  lacks ci d_op
- [Medium] ext4: ext4_ioctl_set_tune_sb() sets CASEFOLD before loading
  sb->s_encoding
- [Low] ext4: test_validate_tune_sb_params() KUnit test fails when
  CONFIG_UNICODE=n
- [Low] ext4: ext4_encoding_init() publishes s_encoding before
  s_encoding_flags
- [Low] ext4: unsynchronized validation in ext4_ioctl_set_tune_sb()
  desyncs flags
--

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

ext4: fix NULL s_encoding after runtime tune_sb enables casefold

This commit probes and initializes sb->s_encoding when
EXT4_IOC_SET_TUNE_SB_PARAM enables the casefold feature at runtime, adds
READ_ONCE() NULL checks on s_encoding in directory lookup and hash paths,
and rejects runtime casefold enablement when CONFIG_UNICODE is disabled.

Link: https://syzbot.org/bug?extid=68178aa1e39c8606392e
Link: https://lore.kernel.org/all/20261202100056.1419-1-zhaimingbing@cmss.chinamobile.com/

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

[Severity: Low]
Does this break test_validate_tune_sb_params() in fs/ext4/ioctl-test.c
when CONFIG_EXT4_KUNIT_TESTS is enabled and CONFIG_UNICODE is disabled?
That test unconditionally sets EXT4_FEATURE_INCOMPAT_CASEFOLD and expects
ext4_validate_tune_sb_params_test() to return 0 and default
params.encoding to EXT4_ENC_UTF8_12_1.

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

[Severity: Low]
Can two concurrent EXT4_IOC_SET_TUNE_SB_PARAM calls enabling casefold with
different encoding_flags leave sb->s_encoding_flags out of sync with
sbi->s_es->s_encoding_flags?

CPU0                                  CPU1
ext4_ioctl_set_tune_sb()              ext4_ioctl_set_tune_sb()
  /* encoding_flags = STRICT */         /* encoding_flags = 0 */
  ext4_validate_tune_sb_params()        ext4_validate_tune_sb_params()
  enabling_casefold = true              enabling_casefold = true
  ext4_update_superblocks_fn()
    es->s_encoding_flags = STRICT
  ext4_encoding_init()
    sb->s_encoding = encoding
    sb->s_encoding_flags = STRICT
                                        ext4_update_superblocks_fn()
                                          es->s_encoding_flags = 0
                                        ext4_encoding_init()
                                          if (READ_ONCE(sb->s_encoding))
                                            return 0;  <-- flags not updated

Because validation and enabling_casefold run before any lock is taken,
both callers pass the !ext4_has_feature_casefold(sb) check, and the second
caller overwrites es->s_encoding_flags on disk and in sbi->s_es while
ext4_encoding_init() returns early without updating sb->s_encoding_flags.

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

[Severity: High]
Does this fail to compile when CONFIG_UNICODE is disabled?  In
include/linux/fs/super_types.h, struct super_block only defines s_encoding
inside #if IS_ENABLED(CONFIG_UNICODE), whereas ext4_ioctl_set_tune_sb() is
compiled unconditionally.

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

[Severity: Medium]
Can setting EXT4_FEATURE_INCOMPAT_CASEFOLD in sbi->s_es before
sb->s_encoding is loaded still leave directories with S_CASEFOLD enabled
while sb->s_encoding is NULL?

ext4_update_primary_sb() invokes ext4_sb_setparams() and sets
EXT4_FEATURE_INCOMPAT_CASEFOLD in sbi->s_es before synchronous primary and
backup superblock I/O and before ext4_encoding_init() runs.  In addition,
if ext4_update_superblocks_fn() fails after modifying sbi->s_es (for
example during backup superblock I/O or ext4_journal_stop()), or if the
second utf8_load() allocation in ext4_encoding_init() fails after
ext4_encoding_probe() unloaded the probed map, sbi->s_es retains
EXT4_FEATURE_INCOMPAT_CASEFOLD while sb->s_encoding stays NULL for the
rest of the mount.

Because ext4_fileattr_set() only checks ext4_has_feature_casefold(sb) and
not sb->s_encoding, a directory can be marked EXT4_CASEFOLD_FL
(S_CASEFOLD) while sb->s_encoding is NULL.  In that state, the new !um
early return in ext4_fname_setup_ci_filename() returns 0 before computing
name->hinfo on encrypted casefolded directories, and dx_probe(),
make_indexed_dir(), and ext4_insert_dentry() skip ext4fs_dirhash() when
IS_ENCRYPTED(dir) && IS_CASEFOLDED(dir), writing zero hashes into on-disk
dirents and HTree nodes; on unencrypted casefolded directories,
ext4fs_dirhash() and ext4_match() fall back to non-casefolded hashing and
matching, writing non-casefolded hashes and allowing duplicate
case-colliding entries on disk.

> +		static DEFINE_MUTEX(casefold_enable_lock);
> +
> +		mutex_lock(&casefold_enable_lock);
> +		ret = ext4_encoding_init(sb, sbi->s_es);

[Severity: Medium]
If casefold is enabled at runtime here, what installs
generic_ci_dentry_ops on the superblock and existing dentries?

__ext4_fill_super() calls generic_set_sb_d_ops(sb) once at mount time,
which only installs generic_ci_dentry_ops (DCACHE_OP_HASH |
DCACHE_OP_COMPARE) if sb->s_encoding is already non-NULL.  When
sb->s_encoding is populated later via ext4_ioctl_set_tune_sb(),
generic_ci_dentry_ops is never installed, so VFS dcache lookups in
S_CASEFOLD directories remain case-sensitive while ext4_lookup() matches
case-insensitively on disk.

Looking up an existing file under a different case misses in the dcache
and calls d_splice_alias(inode, dentry) in ext4_lookup(), creating a
second positive hashed dentry alias for a single-link regular file (so
unlinking one case variant drops i_nlink to 0 while leaving the other
positive dentry hashed in the dcache), mutating live subdirectory dentry
names on read-only lookups of a different case, and bypassing
SB_ENC_STRICT_MODE_FL validation in generic_ci_d_hash().

[ ... ]

> 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

[ ... ]

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

[Severity: Low]
Should WRITE_ONCE(sb->s_encoding_flags, encoding_flags) happen before
smp_store_release(&sb->s_encoding, encoding)?

Readers such as ext4_match() check READ_ONCE(parent->i_sb->s_encoding) and
then read s_encoding_flags via sb_no_casefold_compat_fallback() and
generic_ci_match().  Publishing s_encoding before writing s_encoding_flags
lets a concurrent reader observe a non-NULL s_encoding while
s_encoding_flags is still 0.

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

      reply	other threads:[~2026-12-03  1:47 UTC|newest]

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