Commit 981eec6c300b ("ext4: only update tune mount options when requested") guarded the s_mount_opts copy in ext4_sb_setparams() with EXT4_TUNE_FL_MOUNT_OPTS, but ext4_validate_tune_sb_params() still rejects an unterminated mount_opts string even when the caller is not asking to change it. A caller that tunes an unrelated field and leaves mount_opts uninitialized can therefore get a spurious -E2BIG. Only validate mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set, and add a KUnit test covering both the validation and copy guards. Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters") Signed-off-by: Theodore Ts'o --- fs/ext4/ioctl-test.c | 40 ++++++++++++++++++++++++++++++++++++++++ fs/ext4/ioctl.c | 3 ++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/fs/ext4/ioctl-test.c b/fs/ext4/ioctl-test.c index 8cdb4f7dd639..a95388a215a0 100644 --- a/fs/ext4/ioctl-test.c +++ b/fs/ext4/ioctl-test.c @@ -609,6 +609,7 @@ static void test_validate_tune_sb_params(struct kunit *test) /* Unterminated mount_opts -> -E2BIG */ memset(¶ms, 0, sizeof(params)); + params.set_flags = EXT4_TUNE_FL_MOUNT_OPTS; memset(params.mount_opts, 'a', sizeof(params.mount_opts)); KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, ¶ms), -E2BIG); @@ -702,6 +703,44 @@ static void test_validate_tune_sb_params(struct kunit *test) KUNIT_EXPECT_EQ(test, params.encoding_flags, 0); } +/* + * Verify that s_mount_opts is only validated and rewritten when + * EXT4_TUNE_FL_MOUNT_OPTS is set, so that tuning an unrelated field does + * not wipe the mount options stored in the superblock. + */ +static void test_tune_sb_mount_opts_guard(struct kunit *test) +{ + struct ioctl_test_ctx *ctx = test->priv; + struct super_block *sb = ctx->sb; + struct ext4_sb_info *sbi = &ctx->sbi; + struct ext4_super_block *es = ctx->es; + struct ext4_tune_sb_params params; + + strscpy_pad(es->s_mount_opts, "journal_checksum"); + + /* Garbage in mount_opts is ignored without EXT4_TUNE_FL_MOUNT_OPTS */ + memset(¶ms, 0, sizeof(params)); + params.set_flags = EXT4_TUNE_FL_ERRORS_BEHAVIOR; + params.errors_behavior = EXT4_ERRORS_RO; + memset(params.mount_opts, 'a', sizeof(params.mount_opts)); + KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, ¶ms), + 0); + + /* Tuning another field leaves s_mount_opts untouched */ + memset(params.mount_opts, 0, sizeof(params.mount_opts)); + ext4_sb_setparams_test(sbi, es, ¶ms); + KUNIT_EXPECT_EQ(test, le16_to_cpu(es->s_errors), EXT4_ERRORS_RO); + KUNIT_EXPECT_STREQ(test, (char *)es->s_mount_opts, "journal_checksum"); + + /* With EXT4_TUNE_FL_MOUNT_OPTS the new value is written */ + params.set_flags = EXT4_TUNE_FL_MOUNT_OPTS; + strscpy(params.mount_opts, "nodelalloc", sizeof(params.mount_opts)); + KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, ¶ms), + 0); + ext4_sb_setparams_test(sbi, es, ¶ms); + KUNIT_EXPECT_STREQ(test, (char *)es->s_mount_opts, "nodelalloc"); +} + /* * Verify ext4_update_backup_sb, ext4_update_superblocks_fn, and * ext4_update_overhead across primary + 4 sparse_super backup groups (1, 3, @@ -1179,6 +1218,7 @@ static void test_force_shutdown_and_ioctl_dispatch(struct kunit *test) static struct kunit_case ioctl_test_cases[] = { KUNIT_CASE(test_sb_callbacks_and_setparams), KUNIT_CASE(test_validate_tune_sb_params), + KUNIT_CASE(test_tune_sb_mount_opts_guard), KUNIT_CASE(test_update_superblocks_and_overhead), KUNIT_CASE(test_swap_inode_data_and_seed), KUNIT_CASE(test_immutable_dax_and_fileattr), diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c index 83630cd40712..5fdbb5133b27 100644 --- a/fs/ext4/ioctl.c +++ b/fs/ext4/ioctl.c @@ -1409,7 +1409,8 @@ static int ext4_validate_tune_sb_params(struct super_block *sb, struct ext4_super_block *es = sbi->s_es; int enabling_casefold = 0; - if (strnlen(params->mount_opts, sizeof(params->mount_opts)) == + if ((params->set_flags & EXT4_TUNE_FL_MOUNT_OPTS) && + strnlen(params->mount_opts, sizeof(params->mount_opts)) == sizeof(params->mount_opts)) return -E2BIG; -- 2.56.1.708.g77570a6a1d-goog