Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

erofs: avoid infinite loops due to corrupted subpage compact indexes

Robert reported an infinite loop observed by two crafted images.

The root cause is that `clusterofs` can be larger than `lclustersize`
for !NONHEAD `lclusters` in corrupted subpage compact indexes, e.g.:

blocksize = lclustersize = 512 lcn = 6 clusterofs = 515

Move the corresponding check for full compress indexes to
`z_erofs_load_lcluster_from_disk()` to also cover subpage compact
compress indexes.

It also fixes the position of `m->type >= Z_EROFS_LCLUSTER_TYPE_MAX`
check, since it should be placed right after
`z_erofs_load_{compact,full}_lcluster()`.

Fixes: 8d2517aaeea3 ("erofs: fix up compacted indexes for block size < 4096")
Fixes: 1a5223c182fd ("erofs: do sanity check on m->type in z_erofs_load_compact_lcluster()")
Reported-by: Robert Morris <rtm@csail.mit.edu>
Closes: https://lore.kernel.org/r/35167.1760645886@localhost
Reviewed-by: Hongbo Li <lihongbo22@huawei.com>
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>

Gao Xiang e13d315a a429b761

+18 -14
+18 -14
fs/erofs/zmap.c
··· 55 55 } else { 56 56 m->partialref = !!(advise & Z_EROFS_LI_PARTIAL_REF); 57 57 m->clusterofs = le16_to_cpu(di->di_clusterofs); 58 - if (m->clusterofs >= 1 << vi->z_lclusterbits) { 59 - DBG_BUGON(1); 60 - return -EFSCORRUPTED; 61 - } 62 58 m->pblk = le32_to_cpu(di->di_u.blkaddr); 63 59 } 64 60 return 0; ··· 236 240 static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m, 237 241 unsigned int lcn, bool lookahead) 238 242 { 243 + struct erofs_inode *vi = EROFS_I(m->inode); 244 + int err; 245 + 246 + if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT) { 247 + err = z_erofs_load_compact_lcluster(m, lcn, lookahead); 248 + } else { 249 + DBG_BUGON(vi->datalayout != EROFS_INODE_COMPRESSED_FULL); 250 + err = z_erofs_load_full_lcluster(m, lcn); 251 + } 252 + if (err) 253 + return err; 254 + 239 255 if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) { 240 256 erofs_err(m->inode->i_sb, "unknown type %u @ lcn %u of nid %llu", 241 - m->type, lcn, EROFS_I(m->inode)->nid); 257 + m->type, lcn, EROFS_I(m->inode)->nid); 242 258 DBG_BUGON(1); 243 259 return -EOPNOTSUPP; 260 + } else if (m->type != Z_EROFS_LCLUSTER_TYPE_NONHEAD && 261 + m->clusterofs >= (1 << vi->z_lclusterbits)) { 262 + DBG_BUGON(1); 263 + return -EFSCORRUPTED; 244 264 } 245 - 246 - switch (EROFS_I(m->inode)->datalayout) { 247 - case EROFS_INODE_COMPRESSED_FULL: 248 - return z_erofs_load_full_lcluster(m, lcn); 249 - case EROFS_INODE_COMPRESSED_COMPACT: 250 - return z_erofs_load_compact_lcluster(m, lcn, lookahead); 251 - default: 252 - return -EINVAL; 253 - } 265 + return 0; 254 266 } 255 267 256 268 static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,