EXT4_IOC_SET_TUNE_SB_PARAM can enable casefold on a mounted filesystem, but the runtime unicode map (sb->s_encoding) is only built by ext4_encoding_init() from the mount path. With the feature bit set and sb->s_encoding left NULL, a lookup in a casefolded directory dereferences NULL: KASAN: null-ptr-deref in utf8nlookup fs/unicode/utf8-norm.c:306 ext4_fname_setup_ci_filename fs/ext4/namei.c:1226 ext4_lookup fs/ext4/namei.c:1619 Load the encoding after enabling the feature, and refuse to enable it when the kernel has no unicode support, like the mount path does. Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters") Reported-by: syzbot+68178aa1e39c8606392e@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=68178aa1e39c8606392e Reported-by: Guikun Yang Link: https://lore.kernel.org/r/tencent_B079992E4541FB5447D9A4067D6F8D40380A@qq.com Cc: stable@vger.kernel.org --- fs/ext4/ext4.h | 9 +++++++++ fs/ext4/ioctl.c | 11 +++++++++++ fs/ext4/super.c | 7 +------ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index dc2c181337a16..001b95a78cfff 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -3494,6 +3494,15 @@ extern int ext4_convert_meta_bg_test(struct super_block *sb, #endif /* super.c */ +#if IS_ENABLED(CONFIG_UNICODE) +int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es); +#else +static inline int ext4_encoding_init(struct super_block *sb, + struct ext4_super_block *es) +{ + return 0; +} +#endif extern struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block, blk_opf_t op_flags); extern struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb, diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 83630cd40712c..f227304d3490c 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -1480,6 +1480,8 @@ static int ext4_validate_tune_sb_params(struct super_block *sb, if (params->set_feature_incompat_mask & EXT4_FEATURE_INCOMPAT_CASEFOLD) { enabling_casefold = 1; + if (!IS_ENABLED(CONFIG_UNICODE)) + return -EOPNOTSUPP; if (!(params->set_flags & EXT4_TUNE_FL_ENCODING)) { params->encoding = EXT4_ENC_UTF8_12_1; params->set_flags |= EXT4_TUNE_FL_ENCODING; @@ -1555,6 +1557,15 @@ static int ext4_ioctl_set_tune_sb(struct file *filp, return ret; ret = ext4_update_superblocks_fn(sb, ext4_sb_setparams, ¶ms); + + if ((params.set_feature_incompat_mask & + EXT4_FEATURE_INCOMPAT_CASEFOLD) && + ext4_has_feature_casefold(sb)) { + int err = ext4_encoding_init(sb, sbi->s_es); + + if (err && !ret) + ret = err; + } mnt_drop_write_file(filp); if (params.set_flags & EXT4_TUNE_FL_DEF_HASH_ALG) diff --git a/fs/ext4/super.c b/fs/ext4/super.c index 880f664d6c04b..9cdf610293dfe 100644 --- a/fs/ext4/super.c +++ b/fs/ext4/super.c @@ -4737,7 +4737,7 @@ 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; @@ -4777,11 +4777,6 @@ static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *e 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