Skip to content

Commit db5d7ab

Browse files
hsiangkaogregkh
authored andcommitted
erofs: fix invalid algorithm for encoded extents
[ Upstream commit 131897c ] 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: [email protected] Closes: https://lore.kernel.org/r/[email protected] Fixes: 1d191b4 ("erofs: implement encoded extent metadata") Thanks-to: Edward Adam Davis <[email protected]> Signed-off-by: Gao Xiang <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent c5fff1c commit db5d7ab

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
@@ -403,10 +403,10 @@ static int z_erofs_map_blocks_fo(struct inode *inode,
403403
.inode = inode,
404404
.map = map,
405405
};
406-
int err = 0;
407-
unsigned int endoff, afmt;
406+
unsigned int endoff;
408407
unsigned long initial_lcn;
409408
unsigned long long ofs, end;
409+
int err;
410410

411411
ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? inode->i_size - 1 : map->m_la;
412412
if (fragment && !(flags & EROFS_GET_BLOCKS_FINDTAIL) &&
@@ -502,20 +502,15 @@ static int z_erofs_map_blocks_fo(struct inode *inode,
502502
err = -EFSCORRUPTED;
503503
goto unmap_out;
504504
}
505-
afmt = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER ?
506-
Z_EROFS_COMPRESSION_INTERLACED :
507-
Z_EROFS_COMPRESSION_SHIFTED;
505+
if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
506+
map->m_algorithmformat = Z_EROFS_COMPRESSION_INTERLACED;
507+
else
508+
map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
509+
} else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
510+
map->m_algorithmformat = vi->z_algorithmtype[1];
508511
} else {
509-
afmt = m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2 ?
510-
vi->z_algorithmtype[1] : vi->z_algorithmtype[0];
511-
if (!(EROFS_I_SB(inode)->available_compr_algs & (1 << afmt))) {
512-
erofs_err(sb, "inconsistent algorithmtype %u for nid %llu",
513-
afmt, vi->nid);
514-
err = -EFSCORRUPTED;
515-
goto unmap_out;
516-
}
512+
map->m_algorithmformat = vi->z_algorithmtype[0];
517513
}
518-
map->m_algorithmformat = afmt;
519514

520515
if ((flags & EROFS_GET_BLOCKS_FIEMAP) ||
521516
((flags & EROFS_GET_BLOCKS_READMORE) &&
@@ -645,9 +640,9 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
645640
{
646641
struct erofs_inode *const vi = EROFS_I(inode);
647642
struct super_block *const sb = inode->i_sb;
648-
int err, headnr;
649-
erofs_off_t pos;
650643
struct z_erofs_map_header *h;
644+
erofs_off_t pos;
645+
int err = 0;
651646

652647
if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags)) {
653648
/*
@@ -661,7 +656,6 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
661656
if (wait_on_bit_lock(&vi->flags, EROFS_I_BL_Z_BIT, TASK_KILLABLE))
662657
return -ERESTARTSYS;
663658

664-
err = 0;
665659
if (test_bit(EROFS_I_Z_INITED_BIT, &vi->flags))
666660
goto out_unlock;
667661

@@ -698,15 +692,6 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
698692
else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
699693
vi->z_idata_size = le16_to_cpu(h->h_idata_size);
700694

701-
headnr = 0;
702-
if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
703-
vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
704-
erofs_err(sb, "unknown HEAD%u format %u for nid %llu, please upgrade kernel",
705-
headnr + 1, vi->z_algorithmtype[headnr], vi->nid);
706-
err = -EOPNOTSUPP;
707-
goto out_unlock;
708-
}
709-
710695
if (!erofs_sb_has_big_pcluster(EROFS_SB(sb)) &&
711696
vi->z_advise & (Z_EROFS_ADVISE_BIG_PCLUSTER_1 |
712697
Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
@@ -745,6 +730,30 @@ static int z_erofs_fill_inode(struct inode *inode, struct erofs_map_blocks *map)
745730
return err;
746731
}
747732

733+
static int z_erofs_map_sanity_check(struct inode *inode,
734+
struct erofs_map_blocks *map)
735+
{
736+
struct erofs_sb_info *sbi = EROFS_I_SB(inode);
737+
738+
if (!(map->m_flags & EROFS_MAP_ENCODED))
739+
return 0;
740+
if (unlikely(map->m_algorithmformat >= Z_EROFS_COMPRESSION_RUNTIME_MAX)) {
741+
erofs_err(inode->i_sb, "unknown algorithm %d @ pos %llu for nid %llu, please upgrade kernel",
742+
map->m_algorithmformat, map->m_la, EROFS_I(inode)->nid);
743+
return -EOPNOTSUPP;
744+
}
745+
if (unlikely(map->m_algorithmformat < Z_EROFS_COMPRESSION_MAX &&
746+
!(sbi->available_compr_algs & (1 << map->m_algorithmformat)))) {
747+
erofs_err(inode->i_sb, "inconsistent algorithmtype %u for nid %llu",
748+
map->m_algorithmformat, EROFS_I(inode)->nid);
749+
return -EFSCORRUPTED;
750+
}
751+
if (unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
752+
map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
753+
return -EOPNOTSUPP;
754+
return 0;
755+
}
756+
748757
int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
749758
int flags)
750759
{
@@ -765,10 +774,8 @@ int z_erofs_map_blocks_iter(struct inode *inode, struct erofs_map_blocks *map,
765774
else
766775
err = z_erofs_map_blocks_fo(inode, map, flags);
767776
}
768-
if (!err && (map->m_flags & EROFS_MAP_ENCODED) &&
769-
unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
770-
map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
771-
err = -EOPNOTSUPP;
777+
if (!err)
778+
err = z_erofs_map_sanity_check(inode, map);
772779
if (err)
773780
map->m_llen = 0;
774781
}

0 commit comments

Comments
 (0)