Skip to contentSkip to conversation

erofs: fix missing unmap if z_erofs_get_extent_compressedlen() fails

PATCH In review Gao Xiang 3 participants 5 messages Dec 5, 2022 – Dec 6, 2022 EROFS file system

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

PATCH 1/2 erofs: fix missing unmap if z_erofs_get_extent_compressedlen() fails

Otherwise, meta buffers could be leaked.

Fixes cec6e93beadf ("erofs: support parsing big pcluster compress indexes")
---
 fs/erofs/zmap.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/erofs/zmap.c +2 -4
index 749a5ac943f4..98eff1259de4 100644
@@ -694,7 +694,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 		map->m_pa = blknr_to_addr(m.pblk);
 		err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
 		if (err)
-			goto out;
+			goto unmap_out;
 	}
 
 	if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN) {
@@ -718,14 +718,12 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 		if (!err)
 			map->m_flags |= EROFS_MAP_FULL_MAPPED;
 	}
+
 unmap_out:
 	erofs_unmap_metabuf(&m.map->buf);
-
-out:
 	erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
 		  __func__, map->m_la, map->m_pa,
 		  map->m_llen, map->m_plen, map->m_flags);
-
 	return err;
 }
 
Signature · 1 line
2.24.4
author
#1 lore raw

PATCH 2/2 erofs: validate the extent length for uncompressed pclusters

syzkaller reported a KASAN use-after-free:
https://syzkaller.appspot.com/bug?extid=2ae90e873e97f1faf6f2

The referenced fuzzed image actually has two issues:
- m_pa == 0 as a non-inlined pcluster;
- The logical length is longer than its physical length.

The first issue has already been addressed. This patch addresses the second issue by checking the extent length validity.

Reported-by [email protected]
Fixes 02827e1796b3 ("staging: erofs: add erofs_map_blocks_iter")
---
 fs/erofs/zmap.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/erofs/zmap.c +5 -0
index 98eff1259de4..0150570c33aa 100644
@@ -698,6 +698,11 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 	}
 
 	if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN) {
+		if (map->m_llen > map->m_plen) {
+			DBG_BUGON(1);
+			err = -EFSCORRUPTED;
+			goto unmap_out;
+		}
 		if (vi->z_advise & Z_EROFS_ADVISE_INTERLACED_PCLUSTER)
 			map->m_algorithmformat =
 				Z_EROFS_COMPRESSION_INTERLACED;
Signature · 1 line
2.24.4
maintainer Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] erofs: validate the extent length for uncompressed pclusters

Quoted 7 lines · Gao Xiang

syzkaller reported a KASAN use-after-free:
https://syzkaller.appspot.com/bug?extid=2ae90e873e97f1faf6f2

The referenced fuzzed image actually has two issues:
- m_pa == 0 as a non-inlined pcluster;
- The logical length is longer than its physical length.

Quoted 6 lines

The first issue has already been addressed. This patch addresses the second issue by checking the extent length validity.

Reported-by [email protected]
Fixes 02827e1796b3 ("staging: erofs: add erofs_map_blocks_iter")

Thanks,

reviewer Reviewed-by on v1 1/2
#3 lore raw
In reply to Gao Xiang
Quoted 4 lines · On Mon, 5 Dec 2022 23:00:49 +0800 Gao Xiang <[email protected]>…

Otherwise, meta buffers could be leaked.

Fixes cec6e93beadf ("erofs: support parsing big pcluster compress indexes")
Quoted 33 lines
---
 fs/erofs/zmap.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
fs/erofs/zmap.c +2 -4
index 749a5ac943f4..98eff1259de4 100644
@@ -694,7 +694,7 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 		map->m_pa = blknr_to_addr(m.pblk);
 		err = z_erofs_get_extent_compressedlen(&m, initial_lcn);
 		if (err)
-			goto out;
+			goto unmap_out;
 	}
 
 	if (m.headtype == Z_EROFS_VLE_CLUSTER_TYPE_PLAIN) {
@@ -718,14 +718,12 @@ static int z_erofs_do_map_blocks(struct inode *inode,
 		if (!err)
 			map->m_flags |= EROFS_MAP_FULL_MAPPED;
 	}
+
 unmap_out:
 	erofs_unmap_metabuf(&m.map->buf);
-
-out:
 	erofs_dbg("%s, m_la %llu m_pa %llu m_llen %llu m_plen %llu m_flags 0%o",
 		  __func__, map->m_la, map->m_pa,
 		  map->m_llen, map->m_plen, map->m_flags);
-
 	return err;
 }