Skip to contentSkip to conversation

ufs: bound the cluster summary index in ufs_clusteracct()

PATCH In review Guikun Yang 2 participants 3 messages Nov 18 1 message not in the archive VFS

lore
Share & tools
lore

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

#1 lore raw

PATCH v2 1/1 ufs: bound the cluster summary index in ufs_clusteracct()

Resending with the Signed-off-by line added; the diff is unchanged.

fs_contigsumsize is read from the on-disk superblock into uspi->s_contigsumsize with no validation, and c_clustersumoff is read from each cylinder-group header with no validation. In ufs_clusteracct() the two combine into a derived array index:

	i = back + forw + 1;
	if (i > uspi->s_contigsumsize)
		i = uspi->s_contigsumsize;
	fs32_add(sb, ubh_get_addr(UCPI_UBH(ucpi),
			ucpi->c_clustersumoff + (i << 2)), cnt);

The clamp itself uses the attacker-controlled value as its bound. ubh_get_addr() expands to ubh->bh[begin >> s_fshift], so a crafted pair indexes bh[UFS_MAXFRAG] past the end of the 136-byte ufs_cg_private_info allocation:

	UBSAN: array-index-out-of-bounds in fs/ufs/balloc.c:892
	 index 15 is out of range for type 'buffer_head *[8]'
	KASAN: slab-out-of-bounds Read of size 8
	 0 bytes to the right of 136-byte region
	Kernel panic - not syncing

Reproduced 3/3 with a crafted 44bsd image (clustersumoff = 440, fs_contigsumsize = 0x20000, giving i = 1880 and begin = 7960, so begin >> s_fshift = 15). A negative image differing only in the fs_contigsumsize byte (0x20000 -> 0) writes 4096 bytes cleanly. Both 7.3 and mainline are affected; syzbot has no report for this.

This is distinct from the earlier "five unvalidated cylinder-group offsets" report: that fix bounds the offsets against s_cgsize and the reproducer set fs_contigsumsize to 0, disabling cluster accounting. Here all five offsets are in range, cgsize passes the bound added by the fs_cgsize fix, and the overflow comes from the (i << 2) derived index with a large s_contigsumsize.

In a valid FFS layout the cluster summary array sits immediately before the cluster bitmap, so c_clustersumoff + 4*(i+1) must fit below c_clusteroff. Check that invariant before the update and skip the accounting otherwise.

---
 fs/ufs/balloc.c | 3 +++
 1 file changed, 3 insertions(+)
fs/ufs/balloc.c +3 -0
index 628edfde3..3858699db 100644
@@ -889,6 +889,9 @@ static void ufs_clusteracct(struct super_block * sb,
 	i = back + forw + 1;
 	if (i > uspi->s_contigsumsize)
 		i = uspi->s_contigsumsize;
+	if (ucpi->c_clusteroff < (i + 1) * sizeof(__fs32) ||
+	    ucpi->c_clusteroff - (i + 1) * sizeof(__fs32) < ucpi->c_clustersumoff)
+		return;
 	fs32_add(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (i << 2)), cnt);
 	if (back > 0)
 		fs32_sub(sb, (__fs32*)ubh_get_addr(UCPI_UBH(ucpi), ucpi->c_clustersumoff + (back << 2)), cnt);
Signature · 1 line
2.53.0.windows.1
#2 lore raw
Quoted 4 lines · Guikun Yang

In a valid FFS layout the cluster summary array sits immediately before the cluster bitmap, so c_clustersumoff + 4*(i+1) must fit below c_clusteroff. Check that invariant before the update and skip the accounting otherwise.

Meh. That's just pluggin the immediate hole. Two things:

(1) c_clusteroff comes straight from the cylinder group header as well. A large enough c_clusteroff passes your check and we still index past ubh->bh[]. And bailing out after ubh_setbit()/ubh_clrbit() leaves the bitmap and the summary out of sync.

(2) If c_nclusterblks < start forw goes negative. Then i passes your check while c_clustersumoff + (back << 2) still writes out of bounds. With back == 0 and forw == -2 you write at c_clustersumoff - 4.

Both reported by Sashiko.

Please validate this where it's read instead.

#3 lore raw

PATCH v3 1/1 ufs: validate cluster accounting layout when reading cylinder groups

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.

---
 fs/ufs/cylinder.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
fs/ufs/cylinder.c +38 -0
index b930ee1cf..b6b3a7547 100644
@@ -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 ||
Signature · 1 line
2.53.0.windows.1