[PATCH v3] ufs: validate cluster accounting layout when reading cylinder groups
From: Guikun Yang
Date: Wed Nov 18 2026 - 09:28:25 EST
Thanks for the review, and for pointing out both bypasses.
You are right that a point-of-use check is insufficient: c_clusteroff
is itself on-disk, bailing after ubh_setbit() desynchronizes the bitmap
and the summary, and a negative back with c_nclusterblks < start writes
at c_clustersumoff - 4. This v3 drops the balloc.c guard entirely and
validates the whole cluster layout in ufs_read_cylinder() where the
fields are read, against s_cgsize (a trusted, mount-time-bounded value).
groups
ufs_clusteracct() derives bh[] indices from three on-disk fields:
c_clustersumoff + (i << 2) with i <= s_contigsumsize
c_clustersumoff + (back << 2) with back possibly negative
c_clusteroff + (block >> 3)
and ubh_get_addr() expands the result to ubh->bh[index >> s_fshift].
None of these fields is validated when read, so a crafted cylinder
group header can index past the 8-entry bh[] array in the 136-byte
ufs_cg_private_info:
UBSAN: array-index-out-of-bounds in fs/ufs/balloc.c:892
KASAN: slab-out-of-bounds Read of size 8
A fix at the point of use is insufficient: bounding the index by
c_clusteroff merely plugs one hole, since c_clusteroff is itself
on-disk, and returning from ufs_clusteracct() after ubh_setbit()
leaves the bitmap and summary out of sync. A negative back with
c_nclusterblks < start also writes at c_clustersumoff - 4, which a
one-sided bound misses.
Validate the layout where the fields are read in ufs_read_cylinder()
instead: the summary array (including the negative-back slot), the
cluster bitmap and the block count must all fit within s_cgsize, the
summary must end before the bitmap, and the offsets must have a sane
lower bound. Reject the whole cylinder group on failure. s_cgsize
is a trusted superblock value already bounded at mount time, unlike
any of the on-disk offsets being checked.
Reported-by: Guikun Yang <yangguikun19@xxxxxx>
Signed-off-by: Guikun Yang <yangguikun19@xxxxxx>
---
fs/ufs/cylinder.c | 38 ++++++++++++++++++++++++++++++++++++++
1 file changed, 38 insertions(+)
diff --git a/fs/ufs/cylinder.c b/fs/ufs/cylinder.c
index b930ee1cf..b6b3a7547 100644
--- a/fs/ufs/cylinder.c
+++ b/fs/ufs/cylinder.c
@@ -69,6 +69,44 @@ static bool ufs_read_cylinder(struct super_block *sb,
ucpi->c_clusteroff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clusteroff);
ucpi->c_nclusterblks = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_nclusterblks);
+ /*
+ * cluster accounting derives indices from on-disk fields:
+ *
+ * c_clustersumoff + (i << 2) with i <= s_contigsumsize
+ * c_clustersumoff + (back << 2) with back possibly negative
+ * c_clusteroff + (block >> 3)
+ *
+ * and ubh_get_addr() turns the result into ubh->bh[index >>
+ * s_fshift]. Bound every one of these against the CG block size
+ * here, where the fields are read, so no later consumer can index
+ * past ucpi->c_ubh.bh[]. Using a trusted bound (s_cgsize) rather
+ * than another on-disk offset is essential.
+ */
+ if ((sbi->s_flags & UFS_CG_MASK) == UFS_CG_44BSD) {
+ u64 cgsize = uspi->s_cgsize;
+ u64 cssz = uspi->s_contigsumsize > 0 ?
+ (u64)uspi->s_contigsumsize : 0;
+ u64 sum_end = (u64)ucpi->c_clustersumoff +
+ (cssz + 1) * sizeof(__fs32);
+
+ /* summary array (incl. the negative-back slot) and the
+ * cluster bitmap must both fit inside the CG block, and
+ * the summary must end before the bitmap starts */
+ u64 bitmap_end = (u64)ucpi->c_clusteroff +
+ DIV_ROUND_UP(ucpi->c_nclusterblks, 8);
+
+ if (sum_end > cgsize || bitmap_end > cgsize ||
+ sum_end > ucpi->c_clusteroff ||
+ ucpi->c_clusteroff < 1 ||
+ ucpi->c_clustersumoff < sizeof(__fs32) ||
+ ucpi->c_nclusterblks > uspi->s_fpg) {
+ ufs_error(sb, __func__,
+ "invalid cluster layout in cylinder group %u\n",
+ cgno);
+ goto failed;
+ }
+ }
+
/* these on-disk values become array and bitmap indices */
if (ucpi->c_cgx != cgno ||
ucpi->c_rotor >= uspi->s_fpg ||
--
2.53.0.windows.1