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

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

      reply	other threads:[~2026-12-01  0:57 UTC|newest]

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