Skip to contentSkip to conversation

ext4: reject mount if clusters/inodes per group are not 8-aligned

PATCH Superseded Baokun Li 2 participants 3 messages Jun 8 – Jun 9 1 message not in the archive ext4 file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Baokun Li

author reviewer
#0 lore raw

PATCH 1/1 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 | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/super.c +5 -3
index 6a77db4d3124..3daf4cdcf07e 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,7 +5305,8 @@ 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) {
+	    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\n",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
Signature · 1 line
2.43.7

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

author reviewer
#2 lore raw

I've sent a V2 that includes two additional patches to address the issues Sashiko reported:

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

Thanks,
Baokun

Quoted 90 lines · On 2026/6/8 14:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [Critical] Missing alignment check allows integer truncation in s_itb_per_group, leading to out-of-bounds inode table access and potential local privilege escalation via forged inodes.
- [High] Integer overflow in bigalloc cluster consistency check on 32-bit systems allows mounting filesystems with arbitrary cross-group overlaps.
--

commit 063bdba3eb2bd319656b0b2534e820244460e17a
Author: Baokun Li <[email protected]>

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. 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. Reject such filesystems at mount time by adding the missing bitwise alignment checks.

Quoted 6 lines
fs/ext4/super.c +0 -0
index 245f67d10ded3..98f1cefbc6880 100644
@@ -4475,8 +4475,9 @@ static int ext4_handle_clustersize(struct super_block *sb)
 		sbi->s_cluster_bits = 0;

fs/ext4/super.c static int ext4_handle_clustersize(struct super_block *sb) · line 4483

Quoted 10 lines
fs/ext4/super.c +3 -2
 	}
 	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;
 	}

[Severity: High]
This isn't a bug introduced by this patch, but while we are validating s_clusters_per_group, could the bigalloc consistency check slightly below this in ext4_handle_clustersize() overflow on 32-bit systems?

The check does:

    if (sbi->s_blocks_per_group !=
        (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) {

Since unsigned long is 32 bits on 32-bit systems, the multiplication can wrap around modulo 2^32. For example, if an image has 16392 clusters and a ratio of 262144, the product evaluates to 2^32 + 2097152, matching an s_blocks_per_group of 2097152.

Because bigalloc bypasses the usual s_blocks_per_group size limits, the wrapped validation would succeed. The block allocator could then allocate blocks far outside the truncated group, overwriting metadata of subsequent groups. Should this multiplication be cast to a 64-bit type to prevent the overflow?

[ ... ]

fs/ext4/super.c static int ext4_block_group_meta_init(struct super_block *sb, int silent) · line 5316

Quoted 10 lines
fs/ext4/super.c +2 -1
@@ -5308,7 +5309,8 @@ 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) {
+	    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\n",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;

[Severity: Critical]
This is a pre-existing issue, but does adding the & 7 check go far enough to prevent integer truncation here?

A few lines below this check in ext4_block_group_meta_init(), the code does:

    sbi->s_itb_per_group = sbi->s_inodes_per_group / sbi->s_inodes_per_block;

Since s_inodes_per_group is now guaranteed to be a multiple of 8, but s_inodes_per_block can be 16, 32, etc., this division can still truncate.

Could this truncation lead to out-of-bounds accesses? Looking at __ext4_get_inode_loc() in fs/ext4/inode.c, the block is calculated without checking against the truncated s_itb_per_group:

    block += (inode_offset / inodes_per_block);

This could allow reading or writing to the block following the truncated inode table, potentially allowing local privilege escalation via forged inodes if that block is allocated to a user file. Should we also ensure that s_inodes_per_group is a multiple of s_inodes_per_block here?

reviewer Reviewed-by on v1 1/1
#3 lore raw
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 33 lines
---
 fs/ext4/super.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
fs/ext4/super.c +5 -3
index 6a77db4d3124..3daf4cdcf07e 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,7 +5305,8 @@ 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) {
+	    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\n",
 			 sbi->s_inodes_per_group);
 		return -EINVAL;
Signature · 1 line
2.43.7
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR