erofs-utils: lib: restrict pcluster size limitations
authorGao Xiang <hsiangkao@linux.alibaba.com>
Sun, 9 Feb 2025 14:38:08 +0000 (22:38 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Sun, 9 Feb 2025 14:52:35 +0000 (22:52 +0800)
Source kernel commit: 7c3ca1838a7831855cbf2e6927a10e0e4723edf6

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250209143808.2984785-1-hsiangkao@linux.alibaba.com
include/erofs_fs.h
lib/zmap.c

index 5672c9963f6b736b185e79b509a36f5859335fe4..269d3021142d911b3055864c9e780465787b02c5 100644 (file)
@@ -297,9 +297,12 @@ enum {
 
 #define EROFS_NAME_LEN      255
 
-/* maximum supported size of a physical compression cluster */
+/* maximum supported encoded size of a physical compressed cluster */
 #define Z_EROFS_PCLUSTER_MAX_SIZE      (1024 * 1024)
 
+/* maximum supported decoded size of a physical compressed cluster */
+#define Z_EROFS_PCLUSTER_MAX_DSIZE     (12 * 1024 * 1024)
+
 /* available compression algorithm types (for h_algorithmtype) */
 enum {
        Z_EROFS_COMPRESSION_LZ4         = 0,
index b84d2147f413d2aea4bfd38ec15e7dac1a340e11..70ff8980b11df672f3baf54220700fc4bdd7b977 100644 (file)
@@ -633,30 +633,29 @@ int z_erofs_map_blocks_iter(struct erofs_inode *vi,
 {
        int err = 0;
 
-       /* when trying to read beyond EOF, leave it unmapped */
-       if (map->m_la >= vi->i_size) {
+       if (map->m_la >= vi->i_size) {  /* post-EOF unmapped extent */
                map->m_llen = map->m_la + 1 - vi->i_size;
                map->m_la = vi->i_size;
                map->m_flags = 0;
-               goto out;
-       }
-
-       err = z_erofs_fill_inode_lazy(vi);
-       if (err)
-               goto out;
-
-       if ((vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER) &&
-           !vi->z_tailextent_headlcn) {
-               map->m_la = 0;
-               map->m_llen = vi->i_size;
-               map->m_flags = EROFS_MAP_MAPPED | EROFS_MAP_FULL_MAPPED |
-                               EROFS_MAP_FRAGMENT;
-               goto out;
+       } else {
+               err = z_erofs_fill_inode_lazy(vi);
+               if (!err) {
+                       if ((vi->z_advise & Z_EROFS_ADVISE_FRAGMENT_PCLUSTER) &&
+                           !vi->z_tailextent_headlcn) {
+                               map->m_la = 0;
+                               map->m_llen = vi->i_size;
+                               map->m_flags = EROFS_MAP_MAPPED |
+                                       EROFS_MAP_FULL_MAPPED | EROFS_MAP_FRAGMENT;
+                       } else {
+                               err = z_erofs_do_map_blocks(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_llen = 0;
        }
-
-       err = z_erofs_do_map_blocks(vi, map, flags);
-out:
-       if (err)
-               map->m_llen = 0;
        return err;
 }