From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=203.205.221.191 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796172766; b=NTb89eVh0ZlIseTePVgfqzxNEfKta3qVgenF1EQxgUu4e9Grd0h71egqAUEiNr3wIvO1 Jk9jYO7fo39IA/CQ3QaR/Lb9L7v9o+7mw3rOWradoCBUL15COUtR+RZkEwyARciopMQ/S sTKUVomY6okY2kvqMUSDuLK8WFehJPBLO5vz68zx5yHQLRP+8NgX5ibVqCbKuXPy4Gru5 MjKIJsy3bjF4kHXybXKW/aR+OeEy1u4ZTEP8OlB4yWuZPO0ec/0XpqYZdWCJ9jbT7NYjJ s0j2djcPIHcpqePZTJAByUo0CSgDNpb8E3HscY+90bHiAN1A5+hJBTh90T73CHGnXDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796172766; 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-Type:Content-Transfer-Encoding; bh=e0+8IBnN1IPlA7sxYirWWfwXP7gYQf4ksZAbHdb8rSM=; b=x+wF1ew1xZmNtLpyTm94SnTiIVYgEV6z/2EEUdPviO+FRYAlDjpsmHGm4STJWooVE+um m3CARdl9yvo3q6tTROV4VZ20w/+Ixp/T02YsW1TYhQCCpDKakWGNCKu+9m8/Cce+YriN8 3fnR6ByMduqcLqRsSqGB41+pxEjlFOXEFnPMK/Q0bGkCfrcJ0CjAbD3zjGMMdi/Qy7ibB Kt/nzVXqLwqevRp/NWF2M2VncqCi15t/PwfHzLx8yTfPXyzLq9CdQmu5froWcqhHPTG0K zk1SpAoPKCmQbwSPT10uUCtemWyXFh1gS/g/MkCKtNvKf2WBgNbhJ1BEjVSuO0UPLtQ== 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=xvHYUkzC; arc=none smtp.remote-ip=203.205.221.191 Received: from out203-205-221-191.mail.qq.com (out203-205-221-191.mail.qq.com [203.205.221.191]) (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 0E6CB606A31A; Wed, 2 Dec 2026 00:52:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0E6CB606A31A 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=xvHYUkzC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1796172755; bh=e0+8IBnN1IPlA7sxYirWWfwXP7gYQf4ksZAbHdb8rSM=; h=From:To:Cc:Subject:Date; b=xvHYUkzCLzTogIG4BI2uj8o8zQ0ymERg1BTQpWHhB/fGA7S+CZtp5rkiNcZBByV4L z7pP3VXgo59L7zgFoL4kP7LkZUPWBBDS/99AL5tvkrBR8O3LxeGwSdIiie9mCOs7ft uXZfRE7+M+wbFw2NISnHcG1MHfdQ+n/S2D+WLBD4= Received: from shuai-VMware-Virtual-Platform.. ([39.165.245.102]) by newxmesmtplogicsvrsza63-0.qq.com (NewEsmtp) with SMTP id D21118A3; Wed, 02 Dec 2026 08:52:33 +0800 X-QQ-mid: xmsmtpt1796172753t51vvbyfz Message-ID: X-QQ-XMAILINFO: OVFdYp27KdlJQepeg5Rz9wmxcnQpbwwx3L3GNOTFJZthj1g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nmO0GDLwCy/eQefM4g== X-QQ-XMRINFO: Nq+8W0+stu50tPAe92KXseR0ZZmBTk3gLg== From: Shuai Liu <847807867@qq.com> To: linux-ext4@vger.kernel.org Cc: tytso@mit.edu, jack@suse.cz, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com, Shuai Liu <847807867@qq.com> Subject: [PATCH v2] ext4: fix NULL s_encoding after runtime tune_sb enables casefold Date: Wed, 2 Dec 2026 08:52:32 +0800 X-OQ-MSGID: <20261202005232.2461785-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-Type: text/plain; charset=UTF-8 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 (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, ¶ms); + + 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