[PATCH v2] ufs: validate fs_cgsize when reading the superblock

From: Guikun Yang

Date: Tue Nov 17 2026 - 12:39:26 EST


Hi Christian,

You were right -- the v1 diff was hand-written and did not apply. This
v2 was generated with git format-patch from a tree I actually built the
change in; both fs/qnx6/inode.c and fs/ufs/super.c compile cleanly and
the patch applies with `git apply --check`.

Changes from v1:
- real index lines, hunk headers and context from git diff
- Fixes:/Signed-off-by: placed in the commit message above the diff
- function signatures match the current tree (new mount API)



---
fs_cgsize is read from disk into uspi->s_cgsize without any check.
Its only consumer derives a buffer count from it:

UCPI_UBH(ucpi)->count = uspi->s_cgsize >> sb->s_blocksize_bits;
...
for (i = 1; i < UCPI_UBH(ucpi)->count; i++)
UCPI_UBH(ucpi)->bh[i] = sb_bread(...);

struct ufs_buffer_head.bh[] has UFS_MAXFRAG entries, so a crafted
fs_cgsize whose block shift exceeds UFS_MAXFRAG writes past bh[]
while loading a cylinder group. Reject a cylinder group size that
is zero, smaller than the fragment size, or whose block-shifted
value would overflow bh[].
---
fs/ufs/super.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index 3569ac92b..6ae7f9cee 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -1140,6 +1140,13 @@ 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 ||
+ uspi->s_cgsize < uspi->s_fsize ||
+ (uspi->s_cgsize >> sb->s_blocksize_bits) > UFS_MAXFRAG) {
+ pr_err("UFS: invalid cylinder group size (%u)\n",
+ 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);
--
2.53.0.windows.1