1 From c769805c79d5acede65d96e5786aa5ebb46c01e0 Mon Sep 17 00:00:00 2001
2 From: Gao Xiang <hsiangkao@linux.alibaba.com>
3 Date: Fri, 2 Jun 2023 11:05:19 +0800
4 Subject: [PATCH 1/2] erofs-utils: fsck: don't allocate/read too large extents
6 Since some crafted EROFS filesystem images could have insane large
7 extents, which causes unexpected bahaviors when extracting data.
9 Fix it by extracting large extents with a buffer of a reasonable
10 maximum size limit and reading multiple times instead.
12 Note that only `--extract` option is impacted.
15 Closes: https://nvd.nist.gov/vuln/detail/CVE-2023-33552
16 Reported-by: Chaoming Yang <lometsj@live.com>
17 Fixes: 412c8f908132 ("erofs-utils: fsck: add --extract=X support to extract to path X")
18 Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
19 Link: https://lore.kernel.org/r/20230602030519.117071-1-hsiangkao@linux.alibaba.com
21 Upstream-Status: Backport
22 Signed-off-by: Ross Burton <ross.burton@arm.com>
24 fsck/main.c | 63 +++++++++++++++++++++++++++++++++++++++++------------
25 1 file changed, 49 insertions(+), 14 deletions(-)
27 diff --git a/fsck/main.c b/fsck/main.c
28 index 6b42252..6689ad8 100644
31 @@ -392,6 +392,8 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
34 while (pos < inode->i_size) {
35 + unsigned int alloc_rawsize;
39 ret = z_erofs_map_blocks_iter(inode, &map,
40 @@ -420,10 +422,28 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
41 if (!(map.m_flags & EROFS_MAP_MAPPED) || !fsckcfg.check_decomp)
44 - if (map.m_plen > raw_size) {
45 - raw_size = map.m_plen;
46 - raw = realloc(raw, raw_size);
48 + if (map.m_plen > Z_EROFS_PCLUSTER_MAX_SIZE) {
50 + erofs_err("invalid pcluster size %" PRIu64 " @ offset %" PRIu64 " of nid %" PRIu64,
51 + map.m_plen, map.m_la,
53 + ret = -EFSCORRUPTED;
56 + alloc_rawsize = Z_EROFS_PCLUSTER_MAX_SIZE;
58 + alloc_rawsize = map.m_plen;
61 + if (alloc_rawsize > raw_size) {
62 + char *newraw = realloc(raw, alloc_rawsize);
69 + raw_size = alloc_rawsize;
73 @@ -434,18 +454,27 @@ static int erofs_verify_inode_data(struct erofs_inode *inode, int outfd)
75 ret = z_erofs_read_one_data(inode, &map, raw, buffer,
76 0, map.m_llen, false);
80 + if (outfd >= 0 && write(outfd, buffer, map.m_llen) < 0)
83 - ret = erofs_read_one_data(&map, raw, 0, map.m_plen);
89 - if (outfd >= 0 && write(outfd, compressed ? buffer : raw,
91 - erofs_err("I/O error occurred when verifying data chunk @ nid %llu",
96 + u64 count = min_t(u64, alloc_rawsize,
99 + ret = erofs_read_one_data(&map, raw, p, count);
103 + if (outfd >= 0 && write(outfd, raw, count) < 0)
105 + map.m_llen -= count;
107 + } while (map.m_llen);
111 @@ -460,6 +489,12 @@ out:
114 return ret < 0 ? ret : 0;
117 + erofs_err("I/O error occurred when verifying data chunk @ nid %llu",
118 + inode->nid | 0ULL);
123 static inline int erofs_extract_dir(struct erofs_inode *inode)