From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=162.62.58.211 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796261449; b=KW0OXzEfA110NHaAeegSNff1zAvQZOSgvaQ19YlXeQyZj19lRrDNXgfnu5w5MOmBwk2j Q1+vAvNh3aDd/HEpaXrcWWJMa9dulBKtxgwqZlUU956I6DQwj1oE+R1wVRjmObQt3EAV6 u8bvkIdE0EN9h6xScE26REIIVjpqxbaKGEXYUS7PuztZch7DTGHcU/v9sTjJ/aV8bv6bY mp2MNjUSieSbXac3WIPwxhvv+xiJmZe8tiP7+QIvCtMzvfK+AiLS7eNiy8lKN2lpUfcPv lOCQo3pFox1aV8Gcp2ugMEE3v8E7qrsbnhUK6K985PmzbQ/i9zXTp4S1h1jjSkiaAcg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796261449; h=DMARC-Filter:DKIM-Signature:Received:X-QQ-mid:Message-ID: X-QQ-XMAILINFO:X-QQ-XMRINFO:From:To:Cc:Subject:Date:X-OQ-MSGID: X-Mailer:MIME-Version:Content-Transfer-Encoding; bh=aPkew3HyVUMbWi4FC0dzrb7ZmwbAX7EI8VU4V4/BRGQ=; b=i7da5/emm7HW4FS8aqVOgPZ3XVGLg4ODeW275LKX0CAcR1Ko/nD3MZeOlXDCKTdgDiz2 deV0DNBbu5A1WVqkdj650WrOMGGu+Kzmt+293F0ZHHIE6gBmzmSCBOY1WECydSs3m/DA9 XEgwDtkfOTOBL2cEf6FxsZhAPykf4U4AzpLjyRN0kx8KoIXwUPFTQC1PRFKJ8JqXgteR1 qzEdO5Lk9i/I4ZjaJUaOiEq24UsTjwqhMYrbAMlFtQwsd95CHP9BVqOS3BXaz0euGd5YM rw4GD7fiVD26NTW/ByfzDTk801CQ0rK0SKurZSMfbJFpMR2bxHZCRa9J1rb759ns5Kw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=qq.com; spf=pass smtp.mailfrom=qq.com; dkim=pass header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=eT9cv5zQ; arc=none smtp.remote-ip=162.62.58.211 Received: from out162-62-58-211.mail.qq.com (out162-62-58-211.mail.qq.com [162.62.58.211]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 117D8606A322; Thu, 3 Dec 2026 01:30:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 117D8606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=qq.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=qq.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=qq.com header.i=@qq.com header.a=rsa-sha256 header.s=s201512 header.b=eT9cv5zQ DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1796261442; bh=aPkew3HyVUMbWi4FC0dzrb7ZmwbAX7EI8VU4V4/BRGQ=; h=From:To:Cc:Subject:Date; b=eT9cv5zQXVCs4Ov/W1Bc1JJItLDnBcwq7oK0j1W/YYwzdw2v09i1hg1q4wZ0mhcQ5 xjwcLobaLYszVYbLh1hk6bYgqRn5UQPwpigoUr4StDfeS9aKp+jR7kyohd/oTQvl47 ayU6M8fmKeS8buZ0LraTBh1HhOkCIqN4mqtFqZuY= Received: from shuai-VMware-Virtual-Platform.. ([39.165.245.102]) by newxmesmtplogicsvrszb51-0.qq.com (NewEsmtp) with SMTP id 7A7108AA; Thu, 03 Dec 2026 09:30:39 +0800 X-QQ-mid: xmsmtpt1796261439t1d1ndk4w Message-ID: X-QQ-XMAILINFO: MRMtjO3A6C9XOfPoxrrkttqbZmRzjdXTCjZ+AC+7T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2HKx8Bg== X-QQ-XMRINFO: NyFYKkN4Ny6FuXrnB5Ye7Aabb3ujjtK+gg== From: Shuai Liu <847807867@qq.com> To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, jack@suse.cz, adilger.kernel@dilger.ca, libaokun@linux.alibaba.com, ojaswin@linux.ibm.com, ritesh.list@gmail.com, yi.zhang@huawei.com, djwong@kernel.org, zhaimingbing@cmss.chinamobile.com, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com, Shuai Liu <847807867@qq.com> Subject: [PATCH v3] ext4: fix NULL s_encoding after runtime tune_sb enables casefold Date: Thu, 3 Dec 2026 09:30:38 +0800 X-OQ-MSGID: <20261203013038.2919276-1-847807867@qq.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, 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 Fix this by: - loading sb->s_encoding when runtime tune_sb enables casefold. The encoding is probed (utf8_load + utf8_unload) before the feature is committed so a load failure cannot leave an on-disk CASEFOLD feature that the filesystem can never mount again. sb->s_encoding is published after the superblock update, under a mutex shared by concurrent tune_sb callers so that only one unicode_map is loaded. - publishing sb->s_encoding with smp_store_release() in ext4_encoding_init() so that lockless readers never observe a non-NULL map before utf8_load()'s internal stores are visible. - adding NULL guards with READ_ONCE() on the sb->s_encoding reads in ext4_match(), ext4_fname_setup_ci_filename() and ext4fs_dirhash() so that the transient feature-vs-encoding window cannot dereference a NULL map. - rejecting runtime casefold enablement in ext4_validate_tune_sb_params() when CONFIG_UNICODE is disabled; otherwise the feature would be committed while s_encoding can never be loaded. The ext4_encoding_init() stub is moved from super.c into ext4.h so it is available to callers in other translation units. Note that rejecting runtime casefold enablement altogether (leaving it to offline tune2fs) would avoid the feature-flag vs. s_encoding visibility window entirely. This fix keeps the feature usable at runtime since that is what the ioctl is designed for, at the cost of the guards above; if offline-only enablement is preferred, the probe and publish can be dropped and the request rejected in ext4_validate_tune_sb_params() instead. Verified with QEMU/KASAN two-state testing of the reproducer chain above: the unpatched kernel reports GPF in utf8nlookup, the patched kernel completes 1000 lookups in the casefolded directory cleanly. Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters") Link: https://syzbot.org/bug?extid=68178aa1e39c8606392e Link: https://lore.kernel.org/all/20261202100056.1419-1-zhaimingbing@cmss.chinamobile.com/ Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com Closes: https://syzbot.org/bug?extid=68178aa1e39c8606392e Signed-off-by: Shuai Liu <847807867@qq.com> --- An independent fix for this bug was posted by Mingbing Zhai (Link above). This version additionally probes the encoding before the feature is committed (a failed utf8_load() after commit leaves an on-disk CASEFOLD feature that the filesystem can never mount again), serialises concurrent tune_sb callers so that only one unicode_map is loaded, publishes s_encoding with release ordering, and guards the feature-vs-encoding window in the lookup paths. Changes in v3: - Drop generic_set_sb_d_ops() call: updating sb->s_d_op at runtime races with concurrent __d_alloc() readers of __s_d_op (Sashiko) - Probe encoding loadability before committing the CASEFOLD feature, and publish sb->s_encoding after the superblock update instead of loading after mnt_drop_write_file(); prevents both an unmountable filesystem when utf8_load() fails and the feature-committed-but-encoding-NULL window (Sashiko) - Serialise concurrent tune_sb enablement with a mutex to avoid leaking a unicode_map (Sashiko) - Publish s_encoding with smp_store_release() before s_encoding_flags (Sashiko) - Reject runtime casefold enablement when CONFIG_UNICODE=n (Sashiko) - Found during QEMU verification: the feature gate in ext4_encoding_init() must be kept; an earlier revision dropped it and broke mount on every ext4 filesystem without casefold Changes in v2: - Add NULL guard in ext4_match() and use READ_ONCE() on the s_encoding reads in ext4_match(), ext4_fname_setup_ci_filename() and ext4fs_dirhash() (Sashiko) - Call ext4_encoding_init() while still holding the mnt write access (Sashiko) - Add Fixes tag (Sashiko) - Move the ext4_encoding_init() stub for CONFIG_UNICODE=n into ext4.h fs/ext4/ext4.h | 12 ++++++++++++ fs/ext4/hash.c | 3 ++- fs/ext4/ioctl.c | 31 +++++++++++++++++++++++++++++++ fs/ext4/namei.c | 6 +++++- fs/ext4/super.c | 40 +++++++++++++++++++++++++++++++--------- 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index dc2c181337a16..c6a338ae44f5d 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -2958,6 +2958,8 @@ 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_encoding_probe(struct super_block *sb, __u16 encoding_id); extern int ext4_fname_setup_ci_filename(struct inode *dir, const struct qstr *iname, struct ext4_filename *fname); @@ -2968,6 +2970,16 @@ 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_encoding_probe(struct super_block *sb, __u16 encoding_id) +{ + 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..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; 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); + 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)) { + 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, ¶ms); + + /* Serialise concurrent callers: only one unicode_map may load. */ + if (ret == 0 && enabling_casefold) { + static DEFINE_MUTEX(casefold_enable_lock); + + mutex_lock(&casefold_enable_lock); + ret = ext4_encoding_init(sb, sbi->s_es); + mutex_unlock(&casefold_enable_lock); + if (ret) + ext4_msg(sb, KERN_ERR, + "runtime tune_sb: encoding_init failed (%d)", + ret); + } + 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..37431e0ffbfab 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -2055,9 +2055,8 @@ static const struct ext4_sb_encodings { }; static const struct ext4_sb_encodings * -ext4_sb_read_encoding(const struct ext4_super_block *es) +ext4_sb_read_encoding(__u16 magic) { - __u16 magic = le16_to_cpu(es->s_encoding); int i; for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++) @@ -4737,16 +4736,16 @@ 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); + encoding_info = ext4_sb_read_encoding(le16_to_cpu(es->s_encoding)); if (!encoding_info) { ext4_msg(sb, KERN_ERR, "Encoding requested by superblock is unknown"); @@ -4765,6 +4764,7 @@ static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *e encoding_flags); return -EINVAL; } + ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: " "%s-%u.%u.%u with flags 0x%hx", encoding_info->name, unicode_major(encoding_info->version), @@ -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); return 0; } -#else -static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es) + +int ext4_encoding_probe(struct super_block *sb, __u16 encoding_id) { + const struct ext4_sb_encodings *encoding_info; + struct unicode_map *encoding; + + encoding_info = ext4_sb_read_encoding(encoding_id); + if (!encoding_info) { + ext4_msg(sb, KERN_ERR, "Encoding requested by tune_sb is unknown"); + return -EINVAL; + } + + encoding = utf8_load(encoding_info->version); + if (IS_ERR(encoding)) { + ext4_msg(sb, KERN_ERR, + "casefold charset %s-%u.%u.%u not supported", + encoding_info->name, + unicode_major(encoding_info->version), + unicode_minor(encoding_info->version), + unicode_rev(encoding_info->version)); + return -EINVAL; + } + + utf8_unload(encoding); return 0; } #endif -- 2.43.0