erofs-utils: lib: validate algorithm format before use in z_erofs_map_blocks_ext
PATCH 1/1 erofs-utils: lib: validate algorithm format before use in z_erofs_map_blocks_ext
The fmt field read from the on-disk extent is used directly as an index without bounds checking. A crafted image could set fmt to a value exceeding Z_EROFS_COMPRESSION_MAX, causing out-of-bounds access.
Add a bounds check before using fmt-1 as algorithm format index, returning -EOPNOTSUPP for unknown algorithm formats.
--- lib/zmap.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-)
lib/zmap.c +19 -5
@@ -630,8 +630,14 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,if (map->m_plen &Z_EROFS_EXTENT_PLEN_PARTIAL ) map->m_flags |=EROFS_MAP_PARTIAL_REF ; map->m_plen &=Z_EROFS_EXTENT_PLEN_MASK ; -if (fmt) +if (fmt) { +if (fmt -1 >=Z_EROFS_COMPRESSION_MAX ) { +erofs_err ("unknown algorithm format %u for encoded extent, nid %llu" , + fmt -1 , vi->nid |0ULL ); +return -EOPNOTSUPP ; + } map->m_algorithmformat = fmt -1 ; + }else if (interlaced && !((map->m_pa | map->m_plen) & bmask)) map->m_algorithmformat =Z_EROFS_COMPRESSION_INTERLACED ;
@@ -738,10 +744,18 @@ 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 && (map->m_flags &EROFS_MAP_ENCODED )) { +if (__erofs_unlikely (map->m_plen > +Z_EROFS_PCLUSTER_MAX_SIZE || + map->m_llen >Z_EROFS_PCLUSTER_MAX_DSIZE )) { + err = -EOPNOTSUPP ; + }else if (map->m_pa && map->m_plen && + map->m_pa + map->m_plen < map->m_pa) { +erofs_err ("bogus physical extent: pa %" PRIu64" plen %" PRIu64, + map->m_pa, map->m_plen); + err = -EFSCORRUPTED ; + } + }if (err) map->m_llen =0 ; }
Signature · 1 line
2.43.0
Quoted 8 lines · Utkal Singh
The fmt field read from the on-disk extent is used directly as an index without bounds checking. A crafted image could set fmt to a value exceeding
Z_EROFS_COMPRESSION_MAX, causing out-of-bounds access.Add a bounds check before using fmt-1 as algorithm format index, returning
-EOPNOTSUPPfor unknown algorithm formats.Signed-off-by Utkal Singh <[email protected]>
The following commit should be backported instead:
commit 131897c65e2b
Author: Gao Xiang <[email protected]> Date: Sun Aug 24 23:11:57 2025 +0800
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.
Thanks-to: Edward Adam Davis <[email protected]>
PATCH v2 1/1 erofs-utils: release 1.9.1
In reply to Utkal SinghFrom: Gao Xiang <[email protected]>
--- ChangeLog | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
ChangeLog +17 -0
@@ -1,3 +1,20 @@ +erofs-utils 1.9.1 + + * A quick maintenance release includes the following fixes: + - (erofsfuse) Synchronize kernel patches to fix encoded extents + and shared xattrs in the metabox; + - (mkfs.erofs) Do not set LZ4_0PADDING for plain filesystems; + - (fsck.erofs) Support extracting subtrees (Inseob Kim); + - (mount.erofs) Fix a flag-clearing bug that can cause mount + failures (Yifan Zhao); + - (mkfs.erofs) Fix a CPU spin when using `--tar=f` if stdin is + closed (David Scott); + - (mkfs.erofs) Fix crashes in the rebuild mode when the source + images contains xattrs (lishixian); + - Miscellaneous fixes. + + -- Gao Xiang <[email protected]> Wed, 04 Mar 2026 00:00:00 +0800 + erofs-utils 1.9 * This release includes the following updates:
Signature · 1 line
2.43.0
PATCH v2 1/1 erofs-utils: lib: validate algorithm for encoded extents
In reply to Utkal SinghEncoded extents use fmt field as algorithm index without checking available_compr_algs bitmask. The non-encoded path already has this check but the encoded extent path in z_erofs_map_blocks_ext() was missing equivalent validation.
Add available_compr_algs consistency check for encoded extents, following kernel commit 131897c65e2b.
--- lib/zmap.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
lib/zmap.c +11 -2
@@ -630,8 +630,17 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,if (map->m_plen &Z_EROFS_EXTENT_PLEN_PARTIAL ) map->m_flags |=EROFS_MAP_PARTIAL_REF ; map->m_plen &=Z_EROFS_EXTENT_PLEN_MASK ; -if (fmt) - map->m_algorithmformat = fmt -1 ; +if (fmt) { +unsigned int afmt = fmt -1 ; + +if (afmt >=Z_EROFS_COMPRESSION_MAX || + !(sbi->available_compr_algs & (1 << afmt))) { +erofs_err ("unknown algorithm %u for encoded extent, nid %llu" , + afmt, vi->nid |0ULL ); +return -EOPNOTSUPP ; + } + map->m_algorithmformat = afmt; + }else if (interlaced && !((map->m_pa | map->m_plen) & bmask)) map->m_algorithmformat =Z_EROFS_COMPRESSION_INTERLACED ;
Signature · 1 line
2.43.0
Re: [PATCH v2] erofs-utils: lib: validate algorithm for encoded extents
Hi Utkal Singh,
Thank you for your contributions and the recent fixes.
To help us manage the review process more effectively and avoid confusion with duplicate emails, could you please consolidate these random fixes into a *single patch series with a cover letter*in the future? This allows us to track the entire set of changes in one thread.
Additionally, please follow these conventions for follow-ups:
1. *Versioning:*If you modify a patch based on review feedback, please increment the version prefix (e.g., v2, v3) in the subject line.
2. *Resends:*If you are simply resending a patch without changes (e.g., to bump it), please add a *RESEND*prefix to the subject line instead of treating it as a new version.
This will greatly help in tracking which patches need to be merged.
Regarding backporting patches to the kernel, please refer to commit ee2709 in the erofs-utils repository for the preferred commit message format. Specifically, please use square brackets [] to describe any modifications you made to the source kernel commit during the backport process. [ Gao may follow up with additional workflow details if needed. ]
Thanks again for your help.
Yifan Zhao
Quoted 37 lines · Utkal Singh
Encoded extents use fmt field as algorithm index without checking
available_compr_algsbitmask. The non-encoded path already has this check but the encoded extent path inz_erofs_map_blocks_ext()was missing equivalent validation.Add
available_compr_algsconsistency check for encoded extents, following kernel commit131897c65e2b.Signed-off-by Utkal Singh<[email protected]>--- lib/zmap.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)lib/zmap.c +11
-2@@ -630,8 +630,17 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,if (map->m_plen &Z_EROFS_EXTENT_PLEN_PARTIAL ) map->m_flags |=EROFS_MAP_PARTIAL_REF ; map->m_plen &=Z_EROFS_EXTENT_PLEN_MASK ; -if (fmt) - map->m_algorithmformat = fmt -1 ; +if (fmt) { +unsigned int afmt = fmt -1 ; + +if (afmt >=Z_EROFS_COMPRESSION_MAX || + !(sbi->available_compr_algs & (1 << afmt))) { +erofs_err ("unknown algorithm %u for encoded extent, nid %llu" , + afmt, vi->nid |0ULL ); +return -EOPNOTSUPP ; + } + map->m_algorithmformat = afmt; + }else if (interlaced && !((map->m_pa | map->m_plen) & bmask)) map->m_algorithmformat =Z_EROFS_COMPRESSION_INTERLACED ;
Re: [PATCH v2] erofs-utils: lib: validate algorithm for encoded extents
Hi Yifan,
Thank you for the guidance and for clarifying the review workflow.
Understood. I will consolidate related fixes into proper patch series with a cover letter in future submissions instead of sending individual patches.
I will also follow the versioning conventions (v2/v3 for updates and RESEND for unchanged resubmissions) and adopt the backport commit message format described in commit ee2709, including square bracket annotations where applicable.
Thanks again for the review and suggestions.
Best regards,
Utkal Singh
Quoted 67 lines · Yifan Zhao
Hi Utkal Singh,
Thank you for your contributions and the recent fixes.
To help us manage the review process more effectively and avoid confusion with duplicate emails,
could you please consolidate these random fixes into a *single patch series with a cover letter* in the future?
This allows us to track the entire set of changes in one thread. Additionally, please follow these conventions for follow-ups:1. *Versioning:* If you modify a patch based on review feedback, please increment the version prefix (e.g., v2, v3) in the subject line.
2. *Resends:* If you are simply resending a patch without changes (e.g., to bump it), please add a *RESEND* prefix to the subject line instead of treating it as a new version.This will greatly help in tracking which patches need to be merged. Regarding backporting patches to the kernel, please refer to commit ee2709 in the erofs-utils repository for the preferred commit message format. Specifically, please use square brackets [] to describe any modifications you made to the source kernel commit during the backport process. [ Gao may follow up with additional workflow details if needed. ] Thanks again for your help.
Yifan Zhao
On 3/15/2026 10:22 PM, Utkal Singh wrote:
Encoded extents use fmt field as algorithm index without checking
available_compr_algsbitmask. The non-encoded path already has this check but the encoded extent path inz_erofs_map_blocks_ext()was missing equivalent validation.Add
available_compr_algsconsistency check for encoded extents, following kernel commit131897c65e2b.Signed-off-by Utkal Singh <[email protected]> <[email protected]>--- lib/zmap.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)lib/zmap.c +11
-2@@ -630,8 +630,17 @@ static int z_erofs_map_blocks_ext(struct erofs_inode *vi,if (map->m_plen &Z_EROFS_EXTENT_PLEN_PARTIAL ) map->m_flags |=EROFS_MAP_PARTIAL_REF ; map->m_plen &=Z_EROFS_EXTENT_PLEN_MASK ; -if (fmt) - map->m_algorithmformat = fmt -1 ; +if (fmt) { +unsigned int afmt = fmt -1 ; + +if (afmt >=Z_EROFS_COMPRESSION_MAX || + !(sbi->available_compr_algs & (1 << afmt))) { +erofs_err ("unknown algorithm %u for encoded extent, nid %llu" , + afmt, vi->nid |0ULL ); +return -EOPNOTSUPP ; + } + map->m_algorithmformat = afmt; + }else if (interlaced && !((map->m_pa | map->m_plen) & bmask)) map->m_algorithmformat =Z_EROFS_COMPRESSION_INTERLACED ;