erofs-utils: avoid overly large temporary buffers for compressed data
authorGao Xiang <hsiangkao@linux.alibaba.com>
Fri, 14 Feb 2025 16:36:21 +0000 (00:36 +0800)
committerGao Xiang <hsiangkao@linux.alibaba.com>
Sat, 15 Feb 2025 01:01:15 +0000 (09:01 +0800)
... and use `EROFS_MAX_BLOCK_SIZE * 2` to avoid potential issues
with buggy compressors.

Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
Link: https://lore.kernel.org/r/20250214163621.4109215-2-hsiangkao@linux.alibaba.com
lib/compress.c

index a4fe5dc92f83be07997c71dc7da966a2a77a3c25..0b48c06ad63a539bc2e4694c29e0fe1d57b38557 100644 (file)
@@ -26,6 +26,8 @@
 #include "erofs/workqueue.h"
 #endif
 
+#define Z_EROFS_DESTBUF_SZ     (Z_EROFS_PCLUSTER_MAX_SIZE + EROFS_MAX_BLOCK_SIZE * 2)
+
 /* compressing configuration specified by users */
 struct erofs_compress_cfg {
        struct erofs_compress handle;
@@ -554,7 +556,7 @@ static bool z_erofs_fixup_deduped_fragment(struct z_erofs_compress_sctx *ctx)
 static int __z_erofs_compress_one(struct z_erofs_compress_sctx *ctx,
                                  struct z_erofs_inmem_extent *e)
 {
-       static char g_dstbuf[EROFS_CONFIG_COMPR_MAX_SZ + EROFS_MAX_BLOCK_SIZE];
+       static char g_dstbuf[Z_EROFS_DESTBUF_SZ];
        char *dstbuf = ctx->destbuf ?: g_dstbuf;
        struct z_erofs_compress_ictx *ictx = ctx->ictx;
        struct erofs_inode *inode = ictx->inode;
@@ -1218,8 +1220,7 @@ void *z_erofs_mt_wq_tls_alloc(struct erofs_workqueue *wq, void *ptr)
        if (!tls->queue)
                goto err_free_priv;
 
-       tls->destbuf = calloc(1, EROFS_CONFIG_COMPR_MAX_SZ +
-                             EROFS_MAX_BLOCK_SIZE);
+       tls->destbuf = calloc(1, Z_EROFS_DESTBUF_SZ);
        if (!tls->destbuf)
                goto err_free_queue;
 
@@ -1291,6 +1292,7 @@ void z_erofs_mt_workfn(struct erofs_work *work, void *tlsp)
                goto out;
 
        sctx->pclustersize = z_erofs_get_max_pclustersize(inode);
+       DBG_BUGON(sctx->pclustersize > Z_EROFS_PCLUSTER_MAX_SIZE);
        sctx->queue = tls->queue;
        sctx->destbuf = tls->destbuf;
        sctx->chandle = &tls->ccfg[cwork->alg_id].handle;