Skip to contentSkip to conversation

ext4: tighten mount-time superblock geometry validation

PATCH Applied Baokun Li 5 participants 17 messages Jun 8 – Jul 23 1 message not in the archive ext4 file system

lore
Share & tools
lore

Baokun Li

author reviewer
#0 lore raw

v2 cover ext4: tighten mount-time superblock geometry validation

Changes since v1:
* Patch 1: Removed a spurious newline in the error message format string.
* Added Patches 2 and 3 to fix additional issues reported by Sashiko (independent of Patch 1).

v1: https://patch.msgid.link/[email protected]

This series adds missing mount-time sanity checks for superblock geometry parameters, preventing crafted filesystem images from causing bitmap checksum corruption, integer overflow, or out-of-bounds inode table access.

Patch 1 rejects filesystems where s_clusters_per_group or s_inodes_per_group is not 8-aligned, since the bitmap checksum functions operate on whole bytes and would leave trailing bits unprotected.

Patch 2 reduces EXT4_MAX_CLUSTER_LOG_SIZE from 30 to 28 to match the documented 256MB limit in mke2fs, preventing a 32-bit overflow in the blocks-per-group consistency check on bigalloc filesystems.

Patch 3 rejects filesystems where s_inodes_per_group is not a multiple of s_inodes_per_block, preventing truncation in the s_itb_per_group calculation that could lead __ext4_get_inode_loc() to read beyond the inode table.

Baokun Li (3):
ext4: reject mount if clusters/inodes per group are not 8-aligned
ext4: reduce max cluster size to match documented 256MB limit
ext4: reject mount if inodes per group is not a multiple of inodes per
block

 fs/ext4/ext4.h  |  2 +-
 fs/ext4/super.c | 11 +++++++----
 2 files changed, 8 insertions(+), 5 deletions(-)
Signature · 1 line
2.43.7
author reviewer
#1 lore raw

PATCH v2 1/3 ext4: reject mount if clusters/inodes per group are not 8-aligned

The block and inode bitmap checksums are computed over a whole number of
bytes: ext4_inode_bitmap_csum_*() use EXT4_INODES_PER_GROUP(sb) >> 3 and ext4_block_bitmap_csum_*() use EXT4_CLUSTERS_PER_GROUP(sb) / 8 as the length passed to ext4_chksum().

If s_inodes_per_group or s_clusters_per_group is not a multiple of 8, the trailing fractional bits are excluded from the checksum. Those bits are then unprotected, and any incremental csum update path that assumes a byte-aligned bitmap can compute a checksum inconsistent with the full recalculation, corrupting the on-disk bitmap checksum.

Reject such filesystems at mount time by adding the missing " & 7" alignment checks alongside the existing range validation.

---
 fs/ext4/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/super.c +6 -4
index 6a77db4d3124..3ddcb4a8d4db 100644
@@ -4472,8 +4472,9 @@ static int ext4_handle_clustersize(struct super_block *sb)
 		sbi->s_cluster_bits = 0;
 	}
 	sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
-	if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
+	if (sbi->s_clusters_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_clusters_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid #clusters per group: %lu",
 			 sbi->s_clusters_per_group);
 		return -EINVAL;
 	}
@@ -5304,8 +5305,9 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 		return -EINVAL;
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
-	    sbi->s_inodes_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
+	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_inodes_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
 	}
Signature · 1 line
2.43.7
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] ext4: reject mount if clusters/inodes per group are not 8-aligned

Quoted 11 lines · Baokun Li

The block and inode bitmap checksums are computed over a whole number of
bytes: ext4_inode_bitmap_csum_*() use EXT4_INODES_PER_GROUP(sb) >> 3 and ext4_block_bitmap_csum_*() use EXT4_CLUSTERS_PER_GROUP(sb) / 8 as the length passed to ext4_chksum().

If s_inodes_per_group or s_clusters_per_group is not a multiple of 8, the trailing fractional bits are excluded from the checksum. Those bits are then unprotected, and any incremental csum update path that assumes a byte-aligned bitmap can compute a checksum inconsistent with the full recalculation, corrupting the on-disk bitmap checksum.

Quoted 8 lines

Reject such filesystems at mount time by adding the missing " & 7" alignment checks alongside the existing range validation.

Looks good to me. Thanks!

Quoted 32 lines
---
 fs/ext4/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/super.c +6 -4
index 6a77db4d3124..3ddcb4a8d4db 100644
@@ -4472,8 +4472,9 @@ static int ext4_handle_clustersize(struct super_block *sb)
 		sbi->s_cluster_bits = 0;
 	}
 	sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
-	if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
+	if (sbi->s_clusters_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_clusters_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid #clusters per group: %lu",
 			 sbi->s_clusters_per_group);
 		return -EINVAL;
 	}
@@ -5304,8 +5305,9 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 		return -EINVAL;
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
-	    sbi->s_inodes_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
+	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_inodes_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
 	}
reviewer Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] ext4: reject mount if clusters/inodes per group are not 8-aligned

In reply to Baokun Li
Quoted 11 lines · Baokun Li

The block and inode bitmap checksums are computed over a whole number of
bytes: ext4_inode_bitmap_csum_*() use EXT4_INODES_PER_GROUP(sb) >> 3 and ext4_block_bitmap_csum_*() use EXT4_CLUSTERS_PER_GROUP(sb) / 8 as the length passed to ext4_chksum().

If s_inodes_per_group or s_clusters_per_group is not a multiple of 8, the trailing fractional bits are excluded from the checksum. Those bits are then unprotected, and any incremental csum update path that assumes a byte-aligned bitmap can compute a checksum inconsistent with the full recalculation, corrupting the on-disk bitmap checksum.

Quoted 8 lines

Reject such filesystems at mount time by adding the missing " & 7" alignment checks alongside the existing range validation.

Looks good. Feel free to add:

Honza

Quoted 35 lines
---
 fs/ext4/super.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/ext4/super.c +6 -4
index 6a77db4d3124..3ddcb4a8d4db 100644
@@ -4472,8 +4472,9 @@ static int ext4_handle_clustersize(struct super_block *sb)
 		sbi->s_cluster_bits = 0;
 	}
 	sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
-	if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
+	if (sbi->s_clusters_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_clusters_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid #clusters per group: %lu",
 			 sbi->s_clusters_per_group);
 		return -EINVAL;
 	}
@@ -5304,8 +5305,9 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 		return -EINVAL;
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
-	    sbi->s_inodes_per_group > sb->s_blocksize * 8) {
-		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
+	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
+	    sbi->s_inodes_per_group & 7) {
+		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
 	}
Signature · 1 line
2.43.7
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#4 lore raw

PATCH v2 2/3 ext4: reduce max cluster size to match documented 256MB limit

In reply to Baokun Li

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

but EXT4_MAX_CLUSTER_LOG_SIZE was set to 30 (1GB), allowing crafted filesystem images to specify cluster sizes up to 1GB.

On 32-bit systems with bigalloc enabled, the consistency check in ext4_handle_clustersize():

s_blocks_per_group == s_clusters_per_group * (clustersize / blocksize)

can overflow when the cluster ratio is large enough. Since s_blocks_per_group is not range-checked in the bigalloc path, the wrapped product can pass the consistency check, leading to inconsistent group geometry and potential out-of-bounds block allocation.

Reduce EXT4_MAX_CLUSTER_LOG_SIZE to 28 to match the documented 256MB limit. With this cap, the maximum product is:

(blocksize * 8) * (256M / blocksize) = 2^31

which fits safely in a 32-bit unsigned long for all block sizes.

---
 fs/ext4/ext4.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4.h +1 -1
index 94283a991e5c..11e41a864db8 100644
@@ -334,7 +334,7 @@ struct ext4_io_submit {
 #define	EXT4_MAX_BLOCK_SIZE		65536
 #define EXT4_MIN_BLOCK_LOG_SIZE		10
 #define EXT4_MAX_BLOCK_LOG_SIZE		16
-#define EXT4_MAX_CLUSTER_LOG_SIZE	30
+#define EXT4_MAX_CLUSTER_LOG_SIZE	28
 #ifdef __KERNEL__
 # define EXT4_BLOCK_SIZE(s)		((s)->s_blocksize)
 #else
Signature · 1 line
2.43.7
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] ext4: reduce max cluster size to match documented 256MB limit

Quoted 21 lines · Baokun Li

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

but EXT4_MAX_CLUSTER_LOG_SIZE was set to 30 (1GB), allowing crafted filesystem images to specify cluster sizes up to 1GB.

On 32-bit systems with bigalloc enabled, the consistency check in ext4_handle_clustersize():

s_blocks_per_group == s_clusters_per_group * (clustersize / blocksize)

can overflow when the cluster ratio is large enough. Since s_blocks_per_group is not range-checked in the bigalloc path, the wrapped product can pass the consistency check, leading to inconsistent group geometry and potential out-of-bounds block allocation.

Reduce EXT4_MAX_CLUSTER_LOG_SIZE to 28 to match the documented 256MB limit. With this cap, the maximum product is:

Quoted 7 lines

(blocksize * 8) * (256M / blocksize) = 2^31

which fits safely in a 32-bit unsigned long for all block sizes.

It should be pretty safe. I doubt anyone could be using > 1GiB clusters for real-world usage at this point, given the nature of the bug and the amount of space used for metadata (e.g. 1GiB per directory, etc). At that granularity they can use LVM for space management.

Reviewed-by Andreas Dilger <[email protected] <mailto:[email protected]>>
Quoted 20 lines

---
fs/ext4/ext4.h | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4.h +1 -1
index 94283a991e5c..11e41a864db8 100644
@@ -334,7 +334,7 @@ struct ext4_io_submit {
 #define EXT4_MAX_BLOCK_SIZE 65536
 #define EXT4_MIN_BLOCK_LOG_SIZE 10
 #define EXT4_MAX_BLOCK_LOG_SIZE 16
-#define EXT4_MAX_CLUSTER_LOG_SIZE 30
+#define EXT4_MAX_CLUSTER_LOG_SIZE 28
 #ifdef __KERNEL__
 # define EXT4_BLOCK_SIZE(s) ((s)->s_blocksize)
 #else
Signature · 1 line
2.43.7

Cheers, Andreas

#6 lore raw

Re: [PATCH v2 2/3] ext4: reduce max cluster size to match documented 256MB limit

In reply to Baokun Li

Hi, Baokun!

Quoted 3 lines · Baokun Li

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

Hmm, I checked the mke2fs(8)[1] and didn't find this sentence, instead, it said:

Valid cluster-size values range from 2 to 32768 times the filesystem block size and must be a power of 2.

However, the implementation of mkfs does not seem to respect this constraint and instead directly uses static macros to limit the user-specified cluster size.

  #define EXT2_MIN_BLOCK_LOG_SIZE         10      /* 1024 */
  #define EXT2_MIN_CLUSTER_LOG_SIZE       EXT2_MIN_BLOCK_LOG_SIZE
  #define EXT2_MAX_CLUSTER_LOG_SIZE       29      /* 512MB  */

This is confusing, or perhaps I missed something. If I understand correctly, users can now format an image with a maximum cluster size of 512 MB (I tried it, and it worked). If the kernel's EXT4_MAX_CLUSTER_LOG_SIZE is limited to 28, this would cause such existing images to become unmountable, even though I don't think images with such a large cluster size actually exist in practice. Therefore, I'm not sure this is safe.

[1] https://man7.org/linux/man-pages/man8/mke2fs.8.html

Quoted 41 lines

but EXT4_MAX_CLUSTER_LOG_SIZE was set to 30 (1GB), allowing crafted filesystem images to specify cluster sizes up to 1GB.

On 32-bit systems with bigalloc enabled, the consistency check in ext4_handle_clustersize():

s_blocks_per_group == s_clusters_per_group * (clustersize / blocksize)

can overflow when the cluster ratio is large enough. Since s_blocks_per_group is not range-checked in the bigalloc path, the wrapped product can pass the consistency check, leading to inconsistent group geometry and potential out-of-bounds block allocation.

Reduce EXT4_MAX_CLUSTER_LOG_SIZE to 28 to match the documented 256MB limit. With this cap, the maximum product is:

(blocksize * 8) * (256M / blocksize) = 2^31

which fits safely in a 32-bit unsigned long for all block sizes.

---
 fs/ext4/ext4.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4.h +1 -1
index 94283a991e5c..11e41a864db8 100644
@@ -334,7 +334,7 @@ struct ext4_io_submit {
 #define	EXT4_MAX_BLOCK_SIZE		65536
 #define EXT4_MIN_BLOCK_LOG_SIZE		10
 #define EXT4_MAX_BLOCK_LOG_SIZE		16
-#define EXT4_MAX_CLUSTER_LOG_SIZE	30
+#define EXT4_MAX_CLUSTER_LOG_SIZE	28
 #ifdef __KERNEL__
 # define EXT4_BLOCK_SIZE(s)		((s)->s_blocksize)
 #else
author reviewer
#7 lore raw

Re: [PATCH v2 2/3] ext4: reduce max cluster size to match documented 256MB limit

Quoted 1 line · Zhang Yi

Hi, Baokun!

Hi Yi,

Thank you for you review!

Quoted 9 lines · Baokun Li
Quoted 3 lines

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

Hmm, I checked the mke2fs(8)[1] and didn't find this sentence, instead, it said:

Valid cluster-size values range from 2 to 32768 times the filesystem block size and must be a power of 2.

Oh, I was indeed looking at a slightly older man page. It was changed to the current description in commit 87cbb381f2e2 ("e2fsprogs:
misc/mke2fs.8.in: Correct valid cluster-size values").

That commit assumes the number of blocks in a cluster cannot exceed the maximum number of blocks that a single extent can hold (EXT_INIT_MAX_LEN = 32768). I believe this is wrong — a single cluster can be covered by multiple extents. So we should fix the bug in ext_falloc_helper() rather than working around it by adding a restriction to the documentation.

The root cause is in ext_falloc_helper():

    max_uninit_len = EXT_UNINIT_MAX_LEN & ~EXT2FS_CLUSTER_MASK(fs); max_init_len = EXT_INIT_MAX_LEN & ~EXT2FS_CLUSTER_MASK(fs);

When cluster_ratio >= EXT_INIT_MAX_LEN (32768), the & ~CLUSTER_MASK operation zeroes out both values, causing ext2fs_new_range() to receive len=0 and return EXT2_ET_INVALID_ARGUMENT.

The kernel handles this correctly — ext4_ext_map_blocks() simply truncates m_len to EXT_INIT_MAX_LEN and lets the caller loop.  On subsequent iterations, get_implied_cluster_alloc() detects that the requested block falls within an already-allocated cluster by examining adjacent extents, and returns the physical address without allocating a new cluster.

I'll send a patch to fix ext_falloc_helper() so that when cluster_ratio >= EXT_INIT_MAX_LEN, the allocation loop can create multiple extents within the same cluster — skipping ext2fs_new_range() and claim_range() for mid-cluster iterations where the physical blocks are already claimed.

Quoted 8 lines

However, the implementation of mkfs does not seem to respect this constraint and instead directly uses static macros to limit the user-specified cluster size.

  #define EXT2_MIN_BLOCK_LOG_SIZE         10      /* 1024 */
  #define EXT2_MIN_CLUSTER_LOG_SIZE       EXT2_MIN_BLOCK_LOG_SIZE
  #define EXT2_MAX_CLUSTER_LOG_SIZE       29      /* 512MB  */

I was going to set EXT2_MAX_CLUSTER_LOG_SIZE to 28 to align with the kernel's EXT4_MAX_CLUSTER_LOG_SIZE.

Quoted 4 lines

This is confusing, or perhaps I missed something. If I understand correctly, users can now format an image with a maximum cluster size of 512 MB (I tried it, and it worked).

Yes, a 512M cluster size also works correctly on the current kernel. The upper limit on cluster size here is to avoid the 32-bit overflow issue that Sashiko mentioned — theoretically the maximum could be 512M.

It's just that the old mke2fs documentation originally specified 256M as the intended maximum, so both the kernel and e2fsprogs target 256M as the upper limit. If anyone prefers 512M, I'm happy to change it.

Quoted 8 lines

If the kernel's
EXT4_MAX_CLUSTER_LOG_SIZE is limited to 28, this would cause such existing images to become unmountable, even though I don't think images with such a large cluster size actually exist in practice. Therefore, I'm not sure this is safe.

[1] https://man7.org/linux/man-pages/man8/mke2fs.8.html

I personally don't think such images are likely to exist in practice, since formatting with the default 4K block size would already fail.

Thanks,
Baokun

#8 lore raw

Re: [PATCH v2 2/3] ext4: reduce max cluster size to match documented 256MB limit

Quoted 82 lines · Baokun Li
Quoted 1 line · Zhang Yi

Hi, Baokun!

Hi Yi,

Thank you for you review!

Quoted 9 lines · Baokun Li
Quoted 3 lines

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

Hmm, I checked the mke2fs(8)[1] and didn't find this sentence, instead, it said:

Valid cluster-size values range from 2 to 32768 times the filesystem block size and must be a power of 2.

Oh, I was indeed looking at a slightly older man page. It was changed to the current description in commit 87cbb381f2e2 ("e2fsprogs:
misc/mke2fs.8.in: Correct valid cluster-size values").

That commit assumes the number of blocks in a cluster cannot exceed the maximum number of blocks that a single extent can hold (EXT_INIT_MAX_LEN = 32768). I believe this is wrong — a single cluster can be covered by multiple extents. So we should fix the bug in ext_falloc_helper() rather than working around it by adding a restriction to the documentation.

The root cause is in ext_falloc_helper():

    max_uninit_len = EXT_UNINIT_MAX_LEN & ~EXT2FS_CLUSTER_MASK(fs); max_init_len = EXT_INIT_MAX_LEN & ~EXT2FS_CLUSTER_MASK(fs);

When cluster_ratio >= EXT_INIT_MAX_LEN (32768), the & ~CLUSTER_MASK operation zeroes out both values, causing ext2fs_new_range() to receive len=0 and return EXT2_ET_INVALID_ARGUMENT.

The kernel handles this correctly — ext4_ext_map_blocks() simply truncates m_len to EXT_INIT_MAX_LEN and lets the caller loop.  On subsequent iterations, get_implied_cluster_alloc() detects that the requested block falls within an already-allocated cluster by examining adjacent extents, and returns the physical address without allocating a new cluster.

I'll send a patch to fix ext_falloc_helper() so that when cluster_ratio >= EXT_INIT_MAX_LEN, the allocation loop can create multiple extents within the same cluster — skipping ext2fs_new_range() and claim_range() for mid-cluster iterations where the physical blocks are already claimed.

Quoted 8 lines

However, the implementation of mkfs does not seem to respect this constraint and instead directly uses static macros to limit the user-specified cluster size.

  #define EXT2_MIN_BLOCK_LOG_SIZE         10      /* 1024 */
  #define EXT2_MIN_CLUSTER_LOG_SIZE       EXT2_MIN_BLOCK_LOG_SIZE
  #define EXT2_MAX_CLUSTER_LOG_SIZE       29      /* 512MB  */

I was going to set EXT2_MAX_CLUSTER_LOG_SIZE to 28 to align with the kernel's EXT4_MAX_CLUSTER_LOG_SIZE.

Quoted 4 lines

This is confusing, or perhaps I missed something. If I understand correctly, users can now format an image with a maximum cluster size of 512 MB (I tried it, and it worked).

Yes, a 512M cluster size also works correctly on the current kernel. The upper limit on cluster size here is to avoid the 32-bit overflow issue that Sashiko mentioned — theoretically the maximum could be 512M.

It's just that the old mke2fs documentation originally specified 256M as the intended maximum, so both the kernel and e2fsprogs target 256M as the upper limit. If anyone prefers 512M, I'm happy to change it.

Quoted 6 lines

If the kernel's
EXT4_MAX_CLUSTER_LOG_SIZE is limited to 28, this would cause such existing images to become unmountable, even though I don't think images with such a large cluster size actually exist in practice. Therefore, I'm not sure this is safe.

Quoted 7 lines
Quoted 2 lines

[1] https://man7.org/linux/man-pages/man8/mke2fs.8.html

I personally don't think such images are likely to exist in practice, since formatting with the default 4K block size would already fail.

FYI, I tried the following parameter:

 # mkfs.ext4 -F -C 512M -O bigalloc,^has_journal /dev/sda
 mke2fs 1.47.0 (5-Feb-2023)

Warning: bigalloc file systems with a cluster size greater than 16 times the block size is considered experimental /dev/sda contains a ext4 file system

         created on Tue Jun  9 07:01:10 2026

Creating filesystem with 39321600 4k blocks and 304 inodes

Allocating group tables: done
Writing inode tables: done
Writing superblocks and filesystem accounting information: done

After this patch, the kernel refuse to mount this filesystem and complain:

EXT4-fs (sda): Invalid log cluster size: 19

Thanks,
Yi.

reviewer
#9 lore raw

Re: [PATCH v2 2/3] ext4: reduce max cluster size to match documented 256MB limit

In reply to Baokun Li
Quoted 20 lines · Baokun Li

The mke2fs man page documents:

Valid cluster-size values are from 2048 to 256M bytes per cluster.

but EXT4_MAX_CLUSTER_LOG_SIZE was set to 30 (1GB), allowing crafted filesystem images to specify cluster sizes up to 1GB.

On 32-bit systems with bigalloc enabled, the consistency check in ext4_handle_clustersize():

s_blocks_per_group == s_clusters_per_group * (clustersize / blocksize)

can overflow when the cluster ratio is large enough. Since s_blocks_per_group is not range-checked in the bigalloc path, the wrapped product can pass the consistency check, leading to inconsistent group geometry and potential out-of-bounds block allocation.

Reduce EXT4_MAX_CLUSTER_LOG_SIZE to 28 to match the documented 256MB limit. With this cap, the maximum product is:

Quoted 7 lines

(blocksize * 8) * (256M / blocksize) = 2^31

which fits safely in a 32-bit unsigned long for all block sizes.

I guess I'll leave it up to Ted to decide whether 28 or 29 in the right value. Both are fine with me if we are consistent with them :)

Honza

Quoted 20 lines
---
 fs/ext4/ext4.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/ext4.h +1 -1
index 94283a991e5c..11e41a864db8 100644
@@ -334,7 +334,7 @@ struct ext4_io_submit {
 #define	EXT4_MAX_BLOCK_SIZE		65536
 #define EXT4_MIN_BLOCK_LOG_SIZE		10
 #define EXT4_MAX_BLOCK_LOG_SIZE		16
-#define EXT4_MAX_CLUSTER_LOG_SIZE	30
+#define EXT4_MAX_CLUSTER_LOG_SIZE	28
 #ifdef __KERNEL__
 # define EXT4_BLOCK_SIZE(s)		((s)->s_blocksize)
 #else
Signature · 1 line
2.43.7
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author reviewer
#10 lore raw

PATCH v2 3/3 ext4: reject mount if inodes per group is not a multiple of inodes per block

In reply to Baokun Li

If s_inodes_per_group is not a multiple of s_inodes_per_block, the division that computes s_itb_per_group truncates, reserving fewer blocks for the inode table than needed.

On a crafted filesystem image, this allows __ext4_get_inode_loc() to compute a block offset beyond the inode table, reading unrelated data as an inode structure.

Add the missing divisibility check alongside the existing validation in ext4_block_group_meta_init().

---
 fs/ext4/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/ext4/super.c +2 -1
index 3ddcb4a8d4db..5ec9e1ef00c0 100644
@@ -5306,7 +5306,8 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
 	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
-	    sbi->s_inodes_per_group & 7) {
+	    sbi->s_inodes_per_group & 7 ||
+	    sbi->s_inodes_per_group % sbi->s_inodes_per_block) {
 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
Signature · 1 line
2.43.7
Reviewed-by on v2 3/3
#11 lore raw

Re: [PATCH v2 3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block

Quoted 9 lines · Baokun Li

If s_inodes_per_group is not a multiple of s_inodes_per_block, the division that computes s_itb_per_group truncates, reserving fewer blocks for the inode table than needed.

On a crafted filesystem image, this allows __ext4_get_inode_loc() to compute a block offset beyond the inode table, reading unrelated data as an inode structure.

Quoted 6 lines

Add the missing divisibility check alongside the existing validation in ext4_block_group_meta_init().

Looks good. Is this also fixed/checked in e2fsprogs? There is really no reason *not* to use all the space in the last itable block for inodes.

Reviewed-by Andreas Dilger <[email protected] <mailto:[email protected]>>
Quoted 21 lines

---
fs/ext4/super.c | 3 ++-

1 file changed, 2 insertions(+), 1 deletion(-)
fs/ext4/super.c +2 -1
index 3ddcb4a8d4db..5ec9e1ef00c0 100644
@@ -5306,7 +5306,8 @@ static int ext4_block_group_meta_init(struct super_block *sb,
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
 	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
-	    sbi->s_inodes_per_group & 7) {
+	    sbi->s_inodes_per_group & 7 ||
+	    sbi->s_inodes_per_group % sbi->s_inodes_per_block) {
 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
Signature · 1 line
2.43.7

Cheers, Andreas

author reviewer
#12 lore raw

Re: [PATCH v2 3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block

Quoted 9 lines · Andreas Dilger
Quoted 8 lines · Baokun Li

If s_inodes_per_group is not a multiple of s_inodes_per_block, the division that computes s_itb_per_group truncates, reserving fewer blocks for the inode table than needed.

On a crafted filesystem image, this allows __ext4_get_inode_loc() to compute a block offset beyond the inode table, reading unrelated data as an inode structure.

Quoted 6 lines
Quoted 6 lines

Add the missing divisibility check alongside the existing validation in ext4_block_group_meta_init().

Hi Andreas,

Thank you for your review!

Quoted 1 line

Looks good. Is this also fixed/checked in e2fsprogs?

Yes, a similar patchset adding analogous checks to e2fsprogs is also in the works.

Quoted 3 lines

There is really no reason *not* to use all the space in the last itable block for inodes.

Reviewed-by Andreas Dilger <[email protected] <mailto:[email protected]>>

Indeed. Images created by mke2fs do not exhibit this issue; it only occurs with certain crafted, malformed images. Thanks, Baokun

A message that is not in the archive (sent off-list or not yet received): [email protected]

author reviewer
#14 lore raw

Re: [PATCH v2 3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block

Quoted 2 lines · On 2026/6/8 19:23, [email protected] wrote:

Does ext2 need a similar divisibility check to prevent crafted images from triggering out-of-bounds reads?

Given that CONFIG_EXT4_USE_FOR_EXT2 is now widely used in place of CONFIG_EXT2_FS, I don't plan to mirror this check to ext2.

Cheers,
Baokun

Reviewed-by on v2 3/3
#15 lore raw

Re: [PATCH v2 3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block

In reply to Baokun Li
Quoted 8 lines · Baokun Li

If s_inodes_per_group is not a multiple of s_inodes_per_block, the division that computes s_itb_per_group truncates, reserving fewer blocks for the inode table than needed.

On a crafted filesystem image, this allows __ext4_get_inode_loc() to compute a block offset beyond the inode table, reading unrelated data as an inode structure.

Quoted 6 lines

Add the missing divisibility check alongside the existing validation in ext4_block_group_meta_init().

Looks good to me.

Quoted 18 lines
---
 fs/ext4/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/ext4/super.c +2 -1
index 3ddcb4a8d4db..5ec9e1ef00c0 100644
@@ -5306,7 +5306,8 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
 	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
-	    sbi->s_inodes_per_group & 7) {
+	    sbi->s_inodes_per_group & 7 ||
+	    sbi->s_inodes_per_group % sbi->s_inodes_per_block) {
 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
reviewer Reviewed-by on v2 3/3
#16 lore raw

Re: [PATCH v2 3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block

In reply to Baokun Li
Quoted 8 lines · Baokun Li

If s_inodes_per_group is not a multiple of s_inodes_per_block, the division that computes s_itb_per_group truncates, reserving fewer blocks for the inode table than needed.

On a crafted filesystem image, this allows __ext4_get_inode_loc() to compute a block offset beyond the inode table, reading unrelated data as an inode structure.

Quoted 6 lines

Add the missing divisibility check alongside the existing validation in ext4_block_group_meta_init().

Looks good. Feel free to add:

Honza

Quoted 21 lines
---
 fs/ext4/super.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/ext4/super.c +2 -1
index 3ddcb4a8d4db..5ec9e1ef00c0 100644
@@ -5306,7 +5306,8 @@ static int ext4_block_group_meta_init(struct super_block *sb, int silent)
 	}
 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
 	    sbi->s_inodes_per_group > sb->s_blocksize * 8 ||
-	    sbi->s_inodes_per_group & 7) {
+	    sbi->s_inodes_per_group & 7 ||
+	    sbi->s_inodes_per_group % sbi->s_inodes_per_block) {
 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
Signature · 1 line
2.43.7
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#17 lore raw
In reply to Baokun Li
Quoted 8 lines · Baokun Li

Changes since v1:
* Patch 1: Removed a spurious newline in the error message format string.
* Added Patches 2 and 3 to fix additional issues reported by Sashiko (independent of Patch 1).

v1: https://patch.msgid.link/[email protected]

[...]

Applied, thanks!

[1/3] ext4: reject mount if clusters/inodes per group are not 8-aligned

      commit: d42dc7726a003262b73ffef2b744ff57a5488799
[2/3] ext4: reduce max cluster size to match documented 256MB limit
      commit: afa3caf2fbc417f755f36e271c4b3c2d45ccda9d
[3/3] ext4: reject mount if inodes per group is not a multiple of inodes per block
      commit: a34de48f329b29dba2d407ad4168ca604d4768e1

Best regards,

Signature · 1 line
Theodore Ts'o <[email protected]>