Linux EXT4 FS development
 help / color / mirror / Atom feed
From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM
Date: Wed, 11 Nov 2026 23:46:52 -0500	[thread overview]
Message-ID: <20261112044712.1577296-4-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>

EXT4_IOC_SET_TUNE_SB_PARAM allows setting the casefold feature (along
with s_encoding and s_encoding_flags) on a mounted file system, but it
only updates the on-disk superblock.  The in-memory state needed by
casefolding is set up exclusively at mount time:

 - sb->s_encoding is loaded by ext4_encoding_init(), so it stays NULL;
 - the casefolding dentry operations are installed before the root
   dentry is created, and can't be retrofitted onto a live dcache;
 - nothing checks for CONFIG_UNICODE, so a kernel without it can set
   a feature that then prevents the file system from being mounted.

Since ext4_has_feature_casefold() becomes true as soon as the primary
superblock is updated, userspace can then set EXT4_CASEFOLD_FL on an
empty directory, and the next lookup or create in it passes the NULL
sb->s_encoding to utf8_casefold(), crashing the kernel.

Casefolding has to be enabled while the file system is unmounted, so
stop advertising and accepting the casefold feature and the
EXT4_TUNE_FL_ENCODING{,_FLAGS} operations, and remove the code that
handled them.

Fixes: 04a91570ac67 ("ext4: implemet new ioctls to set and get superblock parameters")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
 fs/ext4/ioctl-test.c | 35 ++++++++++++++++++++-------------
 fs/ext4/ioctl.c      | 47 +++++++-------------------------------------
 2 files changed, 28 insertions(+), 54 deletions(-)

diff --git a/fs/ext4/ioctl-test.c b/fs/ext4/ioctl-test.c
index c3eee774bcfa..fab2b9f8ff35 100644
--- a/fs/ext4/ioctl-test.c
+++ b/fs/ext4/ioctl-test.c
@@ -522,8 +522,6 @@ static void test_sb_callbacks_and_setparams(struct kunit *test)
 			   EXT4_TUNE_FL_DEF_HASH_ALG |
 			   EXT4_TUNE_FL_RAID_STRIDE |
 			   EXT4_TUNE_FL_RAID_STRIPE_WIDTH |
-			   EXT4_TUNE_FL_ENCODING |
-			   EXT4_TUNE_FL_ENCODING_FLAGS |
 			   EXT4_TUNE_FL_MOUNT_OPTS |
 			   EXT4_TUNE_FL_EDIT_FEATURES |
 			   EXT4_TUNE_FL_FORCE_FSCK;
@@ -539,8 +537,6 @@ static void test_sb_callbacks_and_setparams(struct kunit *test)
 	params.def_hash_alg = DX_HASH_HALF_MD4;
 	params.raid_stride = 16;
 	params.raid_stripe_width = 128;
-	params.encoding = EXT4_ENC_UTF8_12_1;
-	params.encoding_flags = SB_ENC_STRICT_MODE_FL;
 	strscpy(params.mount_opts, "nodelalloc,noatime",
 		sizeof(params.mount_opts));
 	params.set_feature_compat_mask = EXT4_FEATURE_COMPAT_DIR_INDEX;
@@ -563,9 +559,6 @@ static void test_sb_callbacks_and_setparams(struct kunit *test)
 	KUNIT_EXPECT_EQ(test, es->s_def_hash_version, DX_HASH_TEA);
 	KUNIT_EXPECT_EQ(test, le16_to_cpu(es->s_raid_stride), 16);
 	KUNIT_EXPECT_EQ(test, le32_to_cpu(es->s_raid_stripe_width), 128U);
-	KUNIT_EXPECT_EQ(test, le16_to_cpu(es->s_encoding), EXT4_ENC_UTF8_12_1);
-	KUNIT_EXPECT_EQ(test, le16_to_cpu(es->s_encoding_flags),
-			SB_ENC_STRICT_MODE_FL);
 	KUNIT_EXPECT_STREQ(test, (char *)es->s_mount_opts,
 			   "nodelalloc,noatime");
 	KUNIT_EXPECT_NE(test,
@@ -583,8 +576,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, encoding
+ * updates and online casefold enablement, while normalizing DIR_INDEX
+ * enablement.
  */
 static void test_validate_tune_sb_params(struct kunit *test)
 {
@@ -675,20 +669,33 @@ 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 */
+	/* Encoding changes and online casefold enablement -> -EOPNOTSUPP */
 	memset(&params, 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, &params),
-			-EINVAL);
+			-EOPNOTSUPP);
+
+	memset(&params, 0, sizeof(params));
+	params.set_flags = EXT4_TUNE_FL_ENCODING_FLAGS;
+	KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, &params),
+			-EOPNOTSUPP);
 
 	memset(&params, 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, &params),
-			0);
-	KUNIT_EXPECT_EQ(test, params.encoding, EXT4_ENC_UTF8_12_1);
-	KUNIT_EXPECT_EQ(test, params.encoding_flags, 0);
+			-EOPNOTSUPP);
+
+	memset(&params, 0, sizeof(params));
+	params.set_flags = EXT4_TUNE_FL_FEATURES;
+	params.feature_compat = le32_to_cpu(es->s_feature_compat);
+	params.feature_incompat = le32_to_cpu(es->s_feature_incompat) |
+				  EXT4_FEATURE_INCOMPAT_CASEFOLD;
+	params.feature_ro_compat = le32_to_cpu(es->s_feature_ro_compat);
+	KUNIT_EXPECT_EQ(test, ext4_validate_tune_sb_params_test(sb, &params),
+			-EOPNOTSUPP);
+	KUNIT_EXPECT_FALSE(test, ext4_has_feature_casefold(sb));
 }
 
 /*
diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
index 37ad549ad693..8537d5766732 100644
--- a/fs/ext4/ioctl.c
+++ b/fs/ext4/ioctl.c
@@ -1269,9 +1269,13 @@ static int ext4_ioctl_setuuid(struct file *filp,
 	EXT4_TUNE_FL_DEF_HASH_ALG | EXT4_TUNE_FL_RAID_STRIDE | \
 	EXT4_TUNE_FL_RAID_STRIPE_WIDTH | EXT4_TUNE_FL_MOUNT_OPTS | \
 	EXT4_TUNE_FL_FEATURES | EXT4_TUNE_FL_EDIT_FEATURES | \
-	EXT4_TUNE_FL_FORCE_FSCK | EXT4_TUNE_FL_ENCODING | \
-	EXT4_TUNE_FL_ENCODING_FLAGS)
+	EXT4_TUNE_FL_FORCE_FSCK)
 
+/*
+ * Casefolding can't be enabled on a mounted file system: sb->s_encoding
+ * and the casefolding dentry operations are only set up at mount time,
+ * before any dentries exist.
+ */
 #define EXT4_TUNE_SET_COMPAT_SUPP \
 		(EXT4_FEATURE_COMPAT_DIR_INDEX |	\
 		 EXT4_FEATURE_COMPAT_STABLE_INODES)
@@ -1280,8 +1284,7 @@ static int ext4_ioctl_setuuid(struct file *filp,
 		 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 |	\
@@ -1293,9 +1296,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)
 {
@@ -1376,10 +1376,6 @@ static void ext4_sb_setparams(struct ext4_sb_info *sbi,
 	if (params->set_flags & EXT4_TUNE_FL_RAID_STRIPE_WIDTH)
 		es->s_raid_stripe_width =
 			cpu_to_le32(params->raid_stripe_width);
-	if (params->set_flags & EXT4_TUNE_FL_ENCODING)
-		es->s_encoding = cpu_to_le16(params->encoding);
-	if (params->set_flags & EXT4_TUNE_FL_ENCODING_FLAGS)
-		es->s_encoding_flags = cpu_to_le16(params->encoding_flags);
 	if (params->set_flags & EXT4_TUNE_FL_MOUNT_OPTS)
 		strscpy_pad(es->s_mount_opts, params->mount_opts);
 	if (params->set_flags & EXT4_TUNE_FL_EDIT_FEATURES) {
@@ -1411,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 ((params->set_flags & EXT4_TUNE_FL_MOUNT_OPTS) &&
 	    strnlen(params->mount_opts, sizeof(params->mount_opts)) ==
@@ -1482,20 +1477,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;
@@ -1519,20 +1500,6 @@ 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;
-	}
 
 	return 0;
 }
-- 
2.56.0.542.g3ef89b7020-goog


  parent reply	other threads:[~2026-11-12  4:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12  4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12  4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12  4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12  4:46 ` Theodore Ts'o [this message]
2026-11-12  4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12  4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12  4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12  4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12  4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12  4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12  4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12  4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12  4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12  4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12  4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12  4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12  4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12  4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12  4:47 ` [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges Theodore Ts'o

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261112044712.1577296-4-tytso@mit.edu \
    --to=tytso@mit.edu \
    --cc=linux-ext4@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox