Skip to content

Commit 131897c

Browse files
committed
erofs: fix invalid algorithm for encoded extents
The current algorithm sanity checks do not properly apply to new encoded extents. Unify the algorithm check with Z_EROFS_COMPRESSION(_RUNTIME)_MAX and ensure consistency with sbi->available_compr_algs. Reported-and-tested-by: syzbot+5a398eb460ddaa6f242f@syzkaller.appspotmail.com Closes: https://lore.kernel.org/r/68a8bd20.050a0220.37038e.005a.GAE@google.com Fixes: 1d191b4 ("erofs: implement encoded extent metadata") Thanks-to: Edward Adam Davis <eadavis@qq.com> Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
1 parent c17b750 commit 131897c

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

fs/erofs/zmap.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,10 @@ static int z_erofs_map_blocks_fo(struct inode *inode,
394394
.map = map,
395395
.in_mbox = erofs_inode_in_metabox(inode),
396396
};
397-
int err = 0;
398-
unsigned int endoff, afmt;
397+
unsigned int endoff;
399398
unsigned long initial_lcn;
400399
unsigned long long ofs, end;
400+
int err;
401401

402402
ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
403403
if (fragment && !(flags & EROFS_GET_BLOCKS_FINDTAIL) &&
@@ -482,20 +482,15 @@ static int z_erofs_map_blocks_fo(struct inode *inode,
482482
err = -EFSCORRUPTED;
483483
goto unmap_out;
484484
}
485-
afmt = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER ?
486-
Z_EROFS_COMPRESSION_INTERLACED :
487-
Z_EROFS_COMPRESSION_SHIFTED;
485+
if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
486+
map->m_algorithmformat = Z_EROFS_COMPRESSION_INTERLACED;
487+
else
488+
map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
489+
} else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
490+
map->m_algorithmformat = vi->z_algorithmtype[1];
488491
} else {
489-
afmt = m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2 ?
490-
vi->z_algorithmtype[1] : vi->z_algorithmtype[0];
491-
if (!(EROFS_I_SB(inode)->available_compr_algs & (1 << afmt))) {
492-
erofs_err(sb, "inconsistent algorithmtype %u for nid %llu",
493-
afmt, vi->nid);
494-
err = -EFSCORRUPTED;
495-
goto unmap_out;
496-
}
492+
map->m_algorithmformat = vi->z_algorithmtype[0];
497493
}
498-
map->m_algorithmformat = afmt;
499494

500495
if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
501496
((flags & EROFS_GET_BLOCKS_READMORE) &&
@@ -626,9 +621,9 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
626621
{
627622
struct erofs_inode *const vi = EROFS_I(inode);
628623
struct super_block *const sb = inode->i_sb;
629-
int err, headnr;
630-
erofs_off_t pos;
631624
struct z_erofs_map_header *h;
625+
erofs_off_t pos;
626+
int err = 0;
632627

633628
if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
634629
/*
@@ -642,7 +637,6 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
642637
if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
643638
return -ERESTARTSYS;
644639

645-
err = 0;
646640
if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
647641
goto out_unlock;
648642

@@ -679,15 +673,6 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
679673
else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
680674
vi->z_idata_size = le16_to_cpu(h->h_idata_size);
681675

682-
headnr = 0;
683-
if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
684-
vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
685-
erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
686-
headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
687-
err = -EOPNOTSUPP;
688-
goto out_unlock;
689-
}
690-
691676
if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
692677
vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
693678
Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
@@ -726,6 +711,30 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
726711
return err;
727712
}
728713

714+
static int z_erofs_map_sanity_check(struct inode *inode,
715+
struct erofs_map_blocks *map)
716+
{
717+
struct erofs_sb_info *sbi = EROFS_I_SB(inode);
718+
719+
if (!(map->m_flags & EROFS_MAP_ENCODED))
720+
return 0;
721+
if (unlikely(map->m_algorithmformat >= Z_EROFS_COMPRESSION_RUNTIME_MAX)) {
722+
erofs_err(inode->i_sb, "unknown algorithm %d @ pos %llu for nid %llu, please upgrade kernel",
723+
map->m_algorithmformat, map->m_la, EROFS_I(inode)->nid);
724+
return -EOPNOTSUPP;
725+
}
726+
if (unlikely(map->m_algorithmformat < Z_EROFS_COMPRESSION_MAX &&
727+
!(sbi->available_compr_algs & (1 << map->m_algorithmformat)))) {
728+
erofs_err(inode->i_sb, "inconsistent algorithmtype %u for nid %llu",
729+
map->m_algorithmformat, EROFS_I(inode)->nid);
730+
return -EFSCORRUPTED;
731+
}
732+
if (unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
733+
map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
734+
return -EOPNOTSUPP;
735+
return 0;
736+
}
737+
729738
int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
730739
int flags)
731740
{
@@ -746,10 +755,8 @@ int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
746755
else
747756
err = z_erofs_map_blocks_fo(inode, map, flags);
748757
}
749-
if (!err && (map->m_flags & EROFS_MAP_ENCODED) &&
750-
unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
751-
map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
752-
err = -EOPNOTSUPP;
758+
if (!err)
759+
err = z_erofs_map_sanity_check(inode, map);
753760
if (err)
754761
map->m_llen = 0;
755762
}

0 commit comments

Comments
 (0)