Skip to contentSkip to conversation

erofs-utils: lib: validate algorithm format before use in z_erofs_map_blocks_ext

PATCH In review Utkal Singh 3 participants 6 messages Mar 15 – Mar 16 Library code

lore
Share & tools
lore
#0 lore raw

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
index 0e7af4e..ac5daca 100644
@@ -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
#1 lore raw
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 -EOPNOTSUPP for unknown algorithm formats.

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.

Reported-and-tested-by [email protected]
Fixes 1d191b4ca51d ("erofs: implement encoded extent metadata")

Thanks-to: Edward Adam Davis <[email protected]>

#2 lore raw

PATCH v2 1/1 erofs-utils: release 1.9.1

In reply to Utkal Singh

From: Gao Xiang <[email protected]>

---
 ChangeLog | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
ChangeLog +17 -0
index 8b80850..f47a5e5 100644
@@ -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
#3 lore raw

PATCH v2 1/1 erofs-utils: lib: validate algorithm for encoded extents

In reply to Utkal Singh

Encoded 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
index 0e7af4e..a8d1ca6 100644
@@ -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
#4 lore raw

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_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
index 0e7af4e..a8d1ca6 100644
@@ -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;
#5 lore raw

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_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
index 0e7af4e..a8d1ca6 100644
@@ -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;