Skip to content

Commit e13d315

Browse files
committed
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: 8d2517a ("erofs: fix up compacted indexes for block size < 4096") Fixes: 1a5223c ("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>
1 parent a429b76 commit e13d315

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

fs/erofs/zmap.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ static int z_erofs_load_full_lcluster(struct z_erofs_maprecorder *m,
5555
} else {
5656
m->partialref = !!(advise & Z_EROFS_LI_PARTIAL_REF);
5757
m->clusterofs = le16_to_cpu(di->di_clusterofs);
58-
if (m->clusterofs >= 1 << vi->z_lclusterbits) {
59-
DBG_BUGON(1);
60-
return -EFSCORRUPTED;
61-
}
6258
m->pblk = le32_to_cpu(di->di_u.blkaddr);
6359
}
6460
return 0;
@@ -240,21 +236,29 @@ static int z_erofs_load_compact_lcluster(struct z_erofs_maprecorder *m,
240236
static int z_erofs_load_lcluster_from_disk(struct z_erofs_maprecorder *m,
241237
unsigned int lcn, bool lookahead)
242238
{
239+
struct erofs_inode *vi = EROFS_I(m->inode);
240+
int err;
241+
242+
if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT) {
243+
err = z_erofs_load_compact_lcluster(m, lcn, lookahead);
244+
} else {
245+
DBG_BUGON(vi->datalayout != EROFS_INODE_COMPRESSED_FULL);
246+
err = z_erofs_load_full_lcluster(m, lcn);
247+
}
248+
if (err)
249+
return err;
250+
243251
if (m->type >= Z_EROFS_LCLUSTER_TYPE_MAX) {
244252
erofs_err(m->inode->i_sb, "unknown type %u @ lcn %u of nid %llu",
245-
m->type, lcn, EROFS_I(m->inode)->nid);
253+
m->type, lcn, EROFS_I(m->inode)->nid);
246254
DBG_BUGON(1);
247255
return -EOPNOTSUPP;
256+
} else if (m->type != Z_EROFS_LCLUSTER_TYPE_NONHEAD &&
257+
m->clusterofs >= (1 << vi->z_lclusterbits)) {
258+
DBG_BUGON(1);
259+
return -EFSCORRUPTED;
248260
}
249-
250-
switch (EROFS_I(m->inode)->datalayout) {
251-
case EROFS_INODE_COMPRESSED_FULL:
252-
return z_erofs_load_full_lcluster(m, lcn);
253-
case EROFS_INODE_COMPRESSED_COMPACT:
254-
return z_erofs_load_compact_lcluster(m, lcn, lookahead);
255-
default:
256-
return -EINVAL;
257-
}
261+
return 0;
258262
}
259263

260264
static int z_erofs_extent_lookback(struct z_erofs_maprecorder *m,

0 commit comments

Comments
 (0)