erofs-utils: zero out garbage trailing data for non-0padding cases
When "-E legacy-compress" is used, lz4 0padding feature will 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 used an in-house customized
lz4 implemention due to LZ4_decompress_safe_partial() didn'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.
Link: https://lore.kernel.org/r/20210430040345.17120-4-xiang@kernel.org
Fixes: 66653ef10a7f ("erofs-utils: introduce compression for regular files")
Reviewed-by: Li Guifu <bluce.lee@aliyun.com>
Signed-off-by: Gao Xiang <xiang@kernel.org>
diff --git a/lib/compress.c b/lib/compress.c
index b8bb89e..deef6a2 100644
--- a/lib/compress.c
+++ b/lib/compress.c
@@ -189,18 +189,22 @@
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;