When EXT4_IOC_SET_TUNE_SB_PARAM is used to enable the CASEFOLD incompat feature on a mounted filesystem, ext4_sb_setparams() commits the feature bit to the on-disk superblock and the in-memory sbi->s_es copy while sb->s_encoding is left NULL, because ext4_encoding_init() only runs from the mount path. A subsequent FS_IOC_SETFLAGS setting FS_CASEFOLD_FL on a directory succeeds, and any lookup in that directory dereferences the NULL encoding: 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 Runtime casefold enablement cannot be made safe on a live superblock: the feature flag in sbi->s_es, sb->s_encoding, sb->s_encoding_flags and the case-insensitive dcache operations are all consumed by lockless readers (ext4_match(), dentry allocation, dirhash) and cannot be ordered against each other without adding locking to hot paths. A failed utf8_load() after the feature is committed would also leave an on-disk CASEFOLD feature that the filesystem can never mount again, and concurrent enablement attempts can desync sb->s_encoding_flags from the on-disk value. Reject the enablement at validation time by dropping CASEFOLD from EXT4_TUNE_SET_INCOMPAT_SUPP; casefold must be enabled offline with tune2fs, which writes the feature and the encoding fields together. Standalone encoding updates are rejected as well, as they only served the runtime enablement path. This also fixes a build failure with CONFIG_UNICODE=n, where ext4_ioctl_set_tune_sb() referenced sb->s_encoding, which struct super_block only defines when CONFIG_UNICODE is enabled. test_validate_tune_sb_params() is updated to expect -EOPNOTSUPP for runtime casefold enablement. Verified with QEMU/KASAN two-state testing of the reproducer chain above: the unpatched kernel reports GPF in utf8nlookup, the patched kernel rejects the ioctl with -EOPNOTSUPP and the lookup chain stays clean. fs/ext4/ builds with CONFIG_UNICODE=y and CONFIG_UNICODE=n. 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 Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/20261203190908.N2uDMQIX-lkp@intel.com/ Signed-off-by: Shuai Liu <847807867@qq.com> --- An independent fix for this bug was posted by Mingbing Zhai (Link above). This version takes the alternative anticipated in the v3 cover note and rejects runtime casefold enablement altogether, leaving the feature to offline tune2fs, rather than extending the runtime enablement path. Changes in v4: - Switch to rejecting runtime casefold enablement in ext4_validate_tune_sb_params() by dropping CASEFOLD from EXT4_TUNE_SET_INCOMPAT_SUPP: the feature flag in sbi->s_es, sb->s_encoding, sb->s_encoding_flags and the case-insensitive dcache operations are all consumed by lockless readers and cannot be ordered safely against each other on a live superblock, so the runtime enablement path cannot be completed reliably (Sashiko review of v3) - Drop the encoding probe, the tune_sb serialization mutex and the smp_store_release()/READ_ONCE()/NULL-guard machinery, which are no longer needed; ext4_encoding_init() is left unchanged - Fix a build failure with CONFIG_UNICODE=n introduced by the v2/v3 s_encoding guards: struct super_block only defines s_encoding when CONFIG_UNICODE is enabled (Sashiko; also caught by the 0-day build robot, see the Closes link) - Drop the now-unused SB_ENC_SUPP_MASK and the encoding branch of ext4_validate_tune_sb_params(); standalone encoding updates are rejected with -EINVAL - Update test_validate_tune_sb_params() to expect -EOPNOTSUPP for runtime casefold enablement 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/ioctl-test.c | 12 ++++++------ fs/ext4/ioctl.c | 43 +++++++++---------------------------------- 2 files changed, 15 insertions(+), 40 deletions(-) diff --git a/fs/ext4/ioctl-test.c b/fs/ext4/ioctl-test.c index 8cdb4f7dd639b..2108c050c60b5 100644 --- a/fs/ext4/ioctl-test.c +++ b/fs/ext4/ioctl-test.c @@ -595,8 +595,9 @@ static void test_sb_callbacks_and_setparams(struct kunit *test) /* * Verify ext4_validate_tune_sb_params rejects malformed mount_opts, invalid - * flags, out-of-range parameters, unsupported feature edits, and standalone - * encoding updates, while normalizing DIR_INDEX and CASEFOLD enablement. + * flags, out-of-range parameters, unsupported feature edits, standalone + * encoding updates, and runtime CASEFOLD enablement, while normalizing + * DIR_INDEX enablement. */ static void test_validate_tune_sb_params(struct kunit *test) { @@ -686,20 +687,19 @@ static void test_validate_tune_sb_params(struct kunit *test) KUNIT_EXPECT_FALSE(test, uuid_is_null(&uu)); KUNIT_EXPECT_EQ(test, sbi->s_def_hash_version, DX_HASH_HALF_MD4); - /* Encoding without CASEFOLD -> -EINVAL; with CASEFOLD -> UTF8_12_1 */ + /* Standalone encoding updates -> -EINVAL */ memset(¶ms, 0, sizeof(params)); params.set_flags = EXT4_TUNE_FL_ENCODING; params.encoding = EXT4_ENC_UTF8_12_1; KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, ¶ms), -EINVAL); + /* Runtime CASEFOLD enablement -> -EOPNOTSUPP */ memset(¶ms, 0, sizeof(params)); params.set_flags = EXT4_TUNE_FL_EDIT_FEATURES; params.set_feature_incompat_mask = EXT4_FEATURE_INCOMPAT_CASEFOLD; KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, ¶ms), - 0); - KUNIT_EXPECT_EQ(test, params.encoding, EXT4_ENC_UTF8_12_1); - KUNIT_EXPECT_EQ(test, params.encoding_flags, 0); + -EOPNOTSUPP); } /* diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 83630cd40712c..a2e0fef6ff0ab 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -1269,13 +1269,16 @@ static int ext4_ioctl_setuuid(struct file *filp, #define EXT4_TUNE_SET_COMPAT_SUPP \ (EXT4_FEATURE_COMPAT_DIR_INDEX | \ EXT4_FEATURE_COMPAT_STABLE_INODES) +/* + * CASEFOLD is excluded: the feature flag, sb->s_encoding and the dcache + * operations cannot be ordered safely for the lockless readers on a live sb. + */ #define EXT4_TUNE_SET_INCOMPAT_SUPP \ (EXT4_FEATURE_INCOMPAT_EXTENTS | \ EXT4_FEATURE_INCOMPAT_EA_INODE | \ EXT4_FEATURE_INCOMPAT_ENCRYPT | \ EXT4_FEATURE_INCOMPAT_CSUM_SEED | \ - EXT4_FEATURE_INCOMPAT_LARGEDIR | \ - EXT4_FEATURE_INCOMPAT_CASEFOLD) + EXT4_FEATURE_INCOMPAT_LARGEDIR) #define EXT4_TUNE_SET_RO_COMPAT_SUPP \ (EXT4_FEATURE_RO_COMPAT_LARGE_FILE | \ EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \ @@ -1287,9 +1290,6 @@ static int ext4_ioctl_setuuid(struct file *filp, #define EXT4_TUNE_CLEAR_INCOMPAT_SUPP (0) #define EXT4_TUNE_CLEAR_RO_COMPAT_SUPP (0) -#define SB_ENC_SUPP_MASK (SB_ENC_STRICT_MODE_FL | \ - SB_ENC_NO_COMPAT_FALLBACK_FL) - static int ext4_ioctl_get_tune_sb(struct ext4_sb_info *sbi, struct ext4_tune_sb_params __user *params) { @@ -1407,7 +1407,6 @@ static int ext4_validate_tune_sb_params(struct super_block *sb, { struct ext4_sb_info *sbi = EXT4_SB(sb); struct ext4_super_block *es = sbi->s_es; - int enabling_casefold = 0; if (strnlen(params->mount_opts, sizeof(params->mount_opts)) == sizeof(params->mount_opts)) @@ -1477,20 +1476,6 @@ static int ext4_validate_tune_sb_params(struct super_block *sb, ~le32_to_cpu(es->s_feature_incompat); params->set_feature_ro_compat_mask &= ~le32_to_cpu(es->s_feature_ro_compat); - if (params->set_feature_incompat_mask & - EXT4_FEATURE_INCOMPAT_CASEFOLD) { - enabling_casefold = 1; - if (!(params->set_flags & EXT4_TUNE_FL_ENCODING)) { - params->encoding = EXT4_ENC_UTF8_12_1; - params->set_flags |= EXT4_TUNE_FL_ENCODING; - } - if (!(params->set_flags & - EXT4_TUNE_FL_ENCODING_FLAGS)) { - params->encoding_flags = 0; - params->set_flags |= - EXT4_TUNE_FL_ENCODING_FLAGS; - } - } if (params->set_feature_compat_mask & EXT4_FEATURE_COMPAT_DIR_INDEX) { uuid_t uu; @@ -1514,20 +1499,10 @@ static int ext4_validate_tune_sb_params(struct super_block *sb, } } } - if (params->set_flags & EXT4_TUNE_FL_ENCODING) { - if (!enabling_casefold) - return -EINVAL; - if (params->encoding == 0) - params->encoding = EXT4_ENC_UTF8_12_1; - else if (params->encoding != EXT4_ENC_UTF8_12_1) - return -EINVAL; - } - if (params->set_flags & EXT4_TUNE_FL_ENCODING_FLAGS) { - if (!enabling_casefold) - return -EINVAL; - if (params->encoding_flags & ~SB_ENC_SUPP_MASK) - return -EINVAL; - } + if (params->set_flags & EXT4_TUNE_FL_ENCODING) + return -EINVAL; + if (params->set_flags & EXT4_TUNE_FL_ENCODING_FLAGS) + return -EINVAL; return 0; } -- 2.43.0