ufs: reject oversized cylinder group metadata
PATCH 1/1 ufs: reject oversized cylinder group metadata
The on-disk cylinder group size becomes the fragment count used by ufs_read_cylinder(). A size larger than one filesystem block makes that count exceed UFS_MAXFRAG and drives buffer-head stores beyond the fixed eight-entry array in struct ufs_cg_private_info.
Reject zero-sized cylinder groups and sizes larger than the validated filesystem block size while reading the superblock. Since a filesystem block contains at most UFS_MAXFRAG fragments, every later cylinder cache count is then bounded by its buffer-head array.
--- fs/ufs/super.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/ufs/super.c +5 -0
@@ -1140,6 +1140,11 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc) uspi->s_cssize =fs32_to_cpu (sb, usb1->fs_cssize); uspi->s_cgsize =fs32_to_cpu (sb, usb1->fs_cgsize); +if (uspi->s_cgsize ==0 || uspi->s_cgsize > uspi->s_bsize) { +pr_err ("%s(): invalid cylinder group size %u\n" , + __func__, uspi->s_cgsize); + goto failed; + } uspi->s_ntrak =fs32_to_cpu (sb, usb1->fs_ntrak); uspi->s_nsect =fs32_to_cpu (sb, usb1->fs_nsect); uspi->s_spc =fs32_to_cpu (sb, usb1->fs_spc);
Signature · 1 line
2.43.0
I traced the count assignment this patch protects. ufs_read_cylinder() in fs/ufs/cylinder.c sets UCPI_UBH(ucpi)->count directly from s_cgsize >> s_blocksize_bits and then stores into bh[i] in a loop, so it does not go through the UFS_MAXFRAG clamp that _ubh_bread_() and ubh_bread_uspi() apply in fs/ufs/util.c. An oversized fs_cgsize therefore does drive buffer-head stores past the fixed eight entry array in struct ufs_cg_private_info, as the changelog says.
The new bound is sufficient. ufs_fill_super() already rejects a filesystem whose s_bsize over s_fsize ratio is larger than eight, so once s_cgsize is held at or below s_bsize the derived count stays at or under UFS_MAXFRAG and the bh[] loop is in range. Rejecting a zero sized cylinder group at the same point is a sensible guard as well.