[PATCH 1/2] erofs-utils: lib: fix invalid algorithm for encoded extents

Gao Xiang hsiangkao at linux.alibaba.com
Tue Mar 17 19:48:26 AEDT 2026


Source kernel commit: 131897c65e2b86cf14bec7379f44aa8fbb407526

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 lib/zmap.c | 66 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 37 insertions(+), 29 deletions(-)

diff --git a/lib/zmap.c b/lib/zmap.c
index 0e7af4e355e7..067e3ddd7cda 100644
--- a/lib/zmap.c
+++ b/lib/zmap.c
@@ -409,10 +409,10 @@ static int z_erofs_map_blocks_fo(struct erofs_inode *vi,
 		.inode = vi,
 		.map = map,
 	};
-	int err = 0;
-	unsigned int endoff, afmt;
+	unsigned int endoff;
 	unsigned long initial_lcn;
 	unsigned long long ofs, end;
+	int err;
 
 	ofs = flags & EROFS_GET_BLOCKS_FINDTAIL ? vi->i_size - 1 : map->m_la;
 	if (fragment && !(flags & EROFS_GET_BLOCKS_FINDTAIL) &&
@@ -507,20 +507,15 @@ static int z_erofs_map_blocks_fo(struct erofs_inode *vi,
 			err = -EFSCORRUPTED;
 			goto out;
 		}
-		afmt = vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER ?
-			Z_EROFS_COMPRESSION_INTERLACED :
-			Z_EROFS_COMPRESSION_SHIFTED;
+		if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
+			map->m_algorithmformat = Z_EROFS_COMPRESSION_INTERLACED;
+		else
+			map->m_algorithmformat = Z_EROFS_COMPRESSION_SHIFTED;
+	} else if (m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2) {
+		map->m_algorithmformat = vi->z_algorithmtype[1];
 	} else {
-		afmt = m.headtype == Z_EROFS_LCLUSTER_TYPE_HEAD2 ?
-			vi->z_algorithmtype[1] : vi->z_algorithmtype[0];
-		if (!(sbi->available_compr_algs & (1 << afmt))) {
-			erofs_err("inconsistent algorithmtype %u for nid %llu",
-				  afmt, vi->nid);
-			err = -EFSCORRUPTED;
-			goto out;
-		}
+		map->m_algorithmformat = vi->z_algorithmtype[0];
 	}
-	map->m_algorithmformat = afmt;
 
 	if (flags & EROFS_GET_BLOCKS_FIEMAP) {
 		err = z_erofs_get_extent_decompressedlen(&m);
@@ -647,10 +642,10 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,
 static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 {
 	struct erofs_sb_info *sbi = vi->sbi;
-	int err = 0, headnr;
-	erofs_off_t pos;
 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
 	struct z_erofs_map_header *h;
+	erofs_off_t pos;
+	int err = 0;
 
 	if (erofs_atomic_read(&vi->flags) & EROFS_I_Z_INITED)
 		return 0;
@@ -686,15 +681,6 @@ static int z_erofs_fill_inode_lazy(struct erofs_inode *vi)
 	else if (vi->z_advise & Z_EROFS_ADVISE_INLINE_PCLUSTER)
 		vi->z_idata_size = le16_to_cpu(h->h_idata_size);
 
-	headnr = 0;
-	if (vi->z_algorithmtype[0] >= Z_EROFS_COMPRESSION_MAX ||
-	    vi->z_algorithmtype[++headnr] >= Z_EROFS_COMPRESSION_MAX) {
-		erofs_err("unknown HEAD%u format %u for nid %llu",
-			  headnr + 1, vi->z_algorithmtype[0], vi->nid | 0ULL);
-		err = -EOPNOTSUPP;
-		goto out_put_metabuf;
-	}
-
 	if (vi->datalayout == EROFS_INODE_COMPRESSED_COMPACT &&
 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_1) ^
 	    !(vi->z_advise & Z_EROFS_ADVISE_BIG_PCLUSTER_2)) {
@@ -720,6 +706,30 @@ out_put_metabuf:
 	return err;
 }
 
+static int z_erofs_map_sanity_check(struct erofs_inode *vi,
+				    struct erofs_map_blocks *map)
+{
+	struct erofs_sb_info *sbi = vi->sbi;
+
+	if (!(map->m_flags & EROFS_MAP_ENCODED))
+		return 0;
+	if (__erofs_unlikely(map->m_algorithmformat >= Z_EROFS_COMPRESSION_RUNTIME_MAX)) {
+		erofs_err("unknown algorithm %d @ pos %llu for nid %llu, please upgrade kernel",
+			  map->m_algorithmformat, map->m_la, vi->nid);
+		return -EOPNOTSUPP;
+	}
+	if (__erofs_unlikely(map->m_algorithmformat < Z_EROFS_COMPRESSION_MAX &&
+			     !(sbi->available_compr_algs & (1 << map->m_algorithmformat)))) {
+		erofs_err("inconsistent algorithmtype %u for nid %llu",
+			  map->m_algorithmformat, vi->nid);
+		return -EFSCORRUPTED;
+	}
+	if (__erofs_unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
+			     map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
 int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 			    struct erofs_map_blocks *map, int flags)
 {
@@ -738,10 +748,8 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 			else
 				err = z_erofs_map_blocks_fo(vi, map, flags);
 		}
-		if (!err && (map->m_flags & EROFS_MAP_ENCODED) &&
-		    __erofs_unlikely(map->m_plen > Z_EROFS_PCLUSTER_MAX_SIZE ||
-				     map->m_llen > Z_EROFS_PCLUSTER_MAX_DSIZE))
-			err = -EOPNOTSUPP;
+		if (!err)
+			err = z_erofs_map_sanity_check(vi, map);
 		if (err)
 			map->m_llen = 0;
 	}
-- 
2.43.5



More information about the Linux-erofs mailing list