[Bug report] ufs: five unvalidated cylinder-group offsets index bh[8] past the ucpi allocation - wild-pointer RMW on first write

From: Guikun Yang

Date: Thu Nov 12 2026 - 21:21:59 EST


Hi Christian,

Following up on the fs_cgsize report from October 31: the same
cylinder-group load path reads five more offsets from disk without
any bound, and they are used as indices into the fixed bh[8] array
inside the 136-byte ucpi allocation. A crafted 44bsd image with
cg_btotoff = 0x3200 reaches bh[25] on the first block allocation and
does a read-modify-write through whatever pointer it finds there.

UBSAN: array-index-out-of-bounds in fs/ufs/balloc.c:55:17
index 25 is out of range for type 'buffer_head *[8]'
adjust_free_blocks
ufs_alloccg_block
ufs_alloc_fragments
ufs_new_fragments
ufs_inode_getfrag
ufs_getfrag_block
__block_write_begin_int
BUG: KASAN: slab-out-of-bounds in adjust_free_blocks+0x23aa/0x2460
Read of size 8 at addr ffff888006083ad8 by task init/1
which belongs to the cache kmalloc-192 of size 192
allocated 136-byte region [ffff888006083a00, ffff888006083a88)
Kernel panic - not syncing: kasan.fault=panic set ...

The 136-byte allocation is struct ufs_cg_private_info: the embedded
ufs_buffer_head (fragment, count, bh[8]) is 80 bytes, the scalars
24, the offsets 32.

## The gap

c9d263be26 ("ufs: validate cylinder group metadata before caching
it") added this check to ufs_read_cylinder() (fs/ufs/cylinder.c):

/* these on-disk values become array and bitmap indices */
if (ucpi->c_cgx != cgno ||
ucpi->c_rotor >= uspi->s_fpg ||
ucpi->c_frotor >= uspi->s_fpg ||
ucpi->c_irotor >= uspi->s_ipg) {
...
goto failed;
}

The comment is right about the class of values, but the seven lines
immediately above it read seven more offsets of exactly that class,
and none of them is checked:

ucpi->c_btotoff = fs32_to_cpu(sb, ucg->cg_btotoff);
ucpi->c_boff = fs32_to_cpu(sb, ucg->cg_boff);
ucpi->c_iusedoff = fs32_to_cpu(sb, ucg->cg_iusedoff);
ucpi->c_freeoff = fs32_to_cpu(sb, ucg->cg_freeoff);
ucpi->c_nextfreeoff = fs32_to_cpu(sb, ucg->cg_nextfreeoff);
ucpi->c_clustersumoff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clustersumoff);
ucpi->c_clusteroff = fs32_to_cpu(sb, ucg->cg_u.cg_44.cg_clusteroff);

The first consumer is adjust_free_blocks() (fs/ufs/balloc.c:55):

fs32_add(sb, &ubh_cg_blktot(ucpi, cylno), delta);

which expands through ubh_get_addr8() (fs/ufs/util.h:310):

(u8*)(ubh)->bh[(begin) >> uspi->s_fshift]->b_data + ...

with begin = c_btotoff + cylno*4. bh[] has UFS_MAXFRAG (8) entries.
With s_fshift = 9 and c_btotoff = 0x3200 the index is 25, i.e.
ucpi + 200, past the 136-byte object. The load returns a wild
buffer_head pointer and fs32_add() then does a read-modify-write
through it - an arbitrary-address RMW, not just an out-of-bounds
read.

The same expansion applies to cg_boff (ubh_cg_blks) and cg_iusedoff
(ubh_cg_iused); cg_clustersumoff/cg_clusteroff are gated by
s_contigsumsize but the offsets themselves are still unchecked.
cg_freeoff/cg_nextfreeoff have a separate public patch in flight
and are not part of this report.

## Reproduction

A valid 44bsd image (magic, clean state, legal directory and inode
tables) with every other known surface kept legal: cg_cgx = 0 and
rotors 0 (passes c9d263be26), cg_freeoff = 176 with a fitting
bitmap, s_contigsumsize = 0 (cluster accounting early-returns),
fs_cgsize = 512 (the count=1 path from the previous report).

The only difference between the positive and negative image is
cg_btotoff: 0x3200 versus 432.

positive: mount -> open -> write(2) -> UBSAN + KASAN + panic, 3/3
negative: mount -> 0, open -> 3, write -> 4096, no report

Each run regenerates from the base image (the write updates the
filesystem state).

## Suggested fix

Bound the offsets where they are read, next to the existing check,
against the cylinder-group buffer length they index into:

/* these on-disk values become array and bitmap indices */
if (ucpi->c_cgx != cgno || ... ) { ... }
+ if (ucpi->c_btotoff >= uspi->s_cgsize ||
+ ucpi->c_boff >= uspi->s_cgsize ||
+ ucpi->c_iusedoff >= uspi->s_cgsize ||
+ ucpi->c_clustersumoff >= uspi->s_cgsize ||
+ ucpi->c_clusteroff >= uspi->s_cgsize) {
+ ufs_error(sb, __func__,
+ "inconsistent offsets in cylinder group %u\n", cgno);
+ goto failed;
+ }

(The per-cylinder table sizes give tighter bounds - btotoff needs
room for s_ncyl * 4 bytes, boff for the block-position table - but
the buffer-length bound already removes the out-of-object access.)

Reproducer (image builder for both cases, serial logs) available on
request. Happy to send this as a patch as well.

Tested on 7.3.0; fs/ufs/cylinder.c is unchanged in mainline since
c9d263be26 (2026-08-01).

Thanks,
Guikun Yang