When "-E legacy-compress" is used, lz4 0padding feature would be
disabled by default in order to support old kernels (< Linux v5.3).

In that case, the current mkfs leaves previous garbage data after
valid compressed data if the length becomes shorter. This doesn't
matter for kernels >= v5.0 since LZ4_decompress_safe_partial()
is used.

However, for staging erofs v4.19, it uses an in-house customized lz4
implemention due to LZ4_decompress_safe_partial() doesn't work as
expected at that time, yet it doesn't allow trailing random data in
practice or decompression failure could happen.

I don't think it really matters since "obsoleted_mkfs" works perfectly
for such old staging versions (v4.19). Anyway, trailing garbage data
sounds unreasonable, so let's zero out it now.

Fixes: 66653ef10a7f ("erofs-utils: zero out garbage trailing data for 
non-0padding cases")
Signed-off-by: Gao Xiang <[email protected]>
---
 lib/compress.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/lib/compress.c b/lib/compress.c
index b8bb89e7ae9d..deef6a2c8ae3 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -189,18 +189,22 @@ nocompression:
                        ctx->compressedblks = 1;
                        raw = true;
                } else {
-                       const unsigned int used = ret & (EROFS_BLKSIZ - 1);
-                       const unsigned int margin =
-                               erofs_sb_has_lz4_0padding() && used ?
-                                       EROFS_BLKSIZ - used : 0;
+                       const unsigned int tailused = ret & (EROFS_BLKSIZ - 1);
+                       const unsigned int padding =
+                               erofs_sb_has_lz4_0padding() && tailused ?
+                                       EROFS_BLKSIZ - tailused : 0;
 
                        ctx->compressedblks = DIV_ROUND_UP(ret, EROFS_BLKSIZ);
+                       /* zero out garbage trailing data for non-0padding */
+                       if (!erofs_sb_has_lz4_0padding())
+                               memset(dst + ret, 0,
+                                      roundup(ret, EROFS_BLKSIZ) - ret);
 
                        /* write compressed data */
                        erofs_dbg("Writing %u compressed data to %u of %u 
blocks",
                                  count, ctx->blkaddr, ctx->compressedblks);
 
-                       ret = blk_write(dst - margin, ctx->blkaddr,
+                       ret = blk_write(dst - padding, ctx->blkaddr,
                                        ctx->compressedblks);
                        if (ret)
                                return ret;
-- 
2.20.1

Reply via email to