[PATCH 2/3] ocfs2: validate chain allocator clusters per group

From: Joseph Qi

Date: Tue Nov 24 2026 - 06:54:44 EST


ocfs2_validate_inode_block() checks cl_count, cl_next_free_rec and
cl_bpc on a chain allocator dinode, but never cl_cpg, even though the
suballocator reads it straight off disk to size every new block group.
A crafted value therefore survives all of the read time checks.

cl_cpg of 0 makes ocfs2_bits_per_group() return 0. The cluster
reservation then succeeds with ac_bits_wanted of 0, and
__ocfs2_claim_clusters() trips BUG_ON(ac->ac_bits_given >=
ac->ac_bits_wanted) on the same open(O_CREAT) path that extends a
metadata allocator.

Reject a bits_per_group outside [2, U16_MAX] on every chain allocator
but the global bitmap, which is exempted exactly as in
ocfs2_dinode_has_bad_chain_bpc(). The lower bound is the invariant
the group extend relies on: bit 0 of a new group is its own
descriptor, so a group of fewer than two bits has nothing to hand
out. The upper bound catches the product being truncated when
ocfs2_block_group_fill() stores it through cpu_to_le16() into the u16
bg_bits, which otherwise updates i_total, i_used and
bg_free_bits_count with a count that does not match the group
actually built.

No valid volume is affected. mkfs writes the same cl_cpg into the
initial groups it creates, and geometry that either did not fit the
u16 bg_bits or left no allocatable bit would have made the allocator
unusable from the start.

The dinode is now rejected at read time, so iget fails and the
operation returns an error instead of reaching the suballocator.

Signed-off-by: Joseph Qi <joseph.qi@xxxxxxxxxxxxxxxxx>
---
fs/ocfs2/inode.c | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)

diff --git a/fs/ocfs2/inode.c b/fs/ocfs2/inode.c
index 8e5f7a240381..e6f89d1e3091 100644
--- a/fs/ocfs2/inode.c
+++ b/fs/ocfs2/inode.c
@@ -254,6 +254,26 @@ static bool ocfs2_dinode_has_bad_chain_bpc(struct super_block *sb,
le16_to_cpu(di->id2.i_chain.cl_bpc) != bpc;
}

+static bool ocfs2_dinode_has_bad_chain_cpg(struct super_block *sb,
+ struct ocfs2_dinode *di)
+{
+ u32 bits_per_group = (u32)le16_to_cpu(di->id2.i_chain.cl_cpg) *
+ (u32)le16_to_cpu(di->id2.i_chain.cl_bpc);
+
+ /*
+ * A block group holds cl_cpg * cl_bpc bits, the first of which is its
+ * own descriptor, and records that count in the u16 bg_bits. Outside
+ * [2, U16_MAX] the group either has no allocatable bit at all or
+ * silently truncates the allocator accounting. The global bitmap is
+ * exempt as in the bits-per-cluster check above: it never grows, and
+ * its group size comes from ocfs2_group_bitmap_size() rather than
+ * from cl_cpg.
+ */
+ return OCFS2_SB(sb)->bitmap_blkno &&
+ OCFS2_SB(sb)->bitmap_blkno != le64_to_cpu(di->i_blkno) &&
+ (bits_per_group < 2 || bits_per_group > U16_MAX);
+}
+
static bool ocfs2_dinode_has_refcount_without_loc(struct ocfs2_dinode *di)
{
return (le16_to_cpu(di->i_dyn_features) & OCFS2_HAS_REFCOUNT_FL) &&
@@ -1919,6 +1939,12 @@ int ocfs2_validate_inode_block(struct super_block *sb,
le16_to_cpu(cl->cl_bpc));
goto bail;
}
+ if (ocfs2_dinode_has_bad_chain_cpg(sb, di)) {
+ rc = ocfs2_error(sb, "Invalid dinode %llu: clusters per group %u\n",
+ (unsigned long long)bh->b_blocknr,
+ le16_to_cpu(cl->cl_cpg));
+ goto bail;
+ }
}

if (ocfs2_dinode_has_refcount_without_loc(di)) {
--
2.39.3