From: Guikun Yang <yangguikun19@qq.com>
To: Christian Brauner <brauner@kernel.org>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3] ufs: validate cluster accounting layout when reading cylinder groups
Date: Wed, 18 Nov 2026 17:00:00 +0800 [thread overview]
Message-ID: <tencent_C7873BAA3F35E59B7F44D209D13426EADE05@qq.com> (raw)
In-Reply-To: <20261118-besungen-fingiert-festnageln-9233af206caa@brauner>
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@qq.com>
Signed-off-by: Guikun Yang <yangguikun19@qq.com>
---
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
prev parent reply other threads:[~2026-11-18 14:28 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <patch-ufs-contigsumsize-20261118@qq.com>
2026-11-18 8:00 ` [PATCH v2] ufs: bound the cluster summary index in ufs_clusteracct() Guikun Yang
2026-11-18 13:40 ` Christian Brauner
2026-11-18 9:00 ` Guikun Yang [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=tencent_C7873BAA3F35E59B7F44D209D13426EADE05@qq.com \
--to=yangguikun19@qq.com \
--cc=brauner@kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®