[PATCH v3 01/18] lib/decompress_unlz4: use LZ4_decompress_safe() before boot

From: Michal Wilczynski

Date: Wed Dec 02 2026 - 12:34:16 EST


The pre-boot decompressor is the only in-tree user of
LZ4_decompress_fast(), which upstream has deprecated since v1.9. It
never checks the input against the chunk length in the header.

Decode with LZ4_decompress_safe() instead, bounded by the chunk length
and by the output expected of the chunk. Count every chunk against the
size the image ends with: fail a chunk that decodes to any other size
or comes after that size is reached, and an image that ends before it.

x86 decompresses in place; the margin arch/x86/boot/header.S reserves,
(output >> 8) + 128K, is well above the safe decoder's overshoot.

Signed-off-by: Michal Wilczynski <m.wilczynski@xxxxxxxxxxx>
---
lib/decompress_unlz4.c | 18 +++++++++++++-----
1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/lib/decompress_unlz4.c b/lib/decompress_unlz4.c
index 86e9aaec04f6d3b7c49e5ad113a172779884149a..fa8d0615dd290b1a93ceb1f4325f38400e5c9adc 100644
--- a/lib/decompress_unlz4.c
+++ b/lib/decompress_unlz4.c
@@ -155,14 +155,16 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,
}
}
#ifdef PREBOOT
- if (out_len >= uncomp_chunksize) {
+ if (out_len >= uncomp_chunksize)
dest_len = uncomp_chunksize;
- out_len -= dest_len;
- } else
+ else
dest_len = out_len;
+ out_len -= dest_len;

- ret = LZ4_decompress_fast(inp, outp, dest_len);
- chunksize = ret;
+ ret = LZ4_decompress_safe(inp, outp, chunksize, dest_len);
+ /* A chunk once the image's stated size is reached is corrupt. */
+ if (!dest_len || ret != (int)dest_len)
+ ret = -1;
#else
dest_len = uncomp_chunksize;

@@ -197,6 +199,12 @@ STATIC inline int INIT unlz4(u8 *input, long in_len,

exit_3:
ret = 0;
+#ifdef PREBOOT
+ if (out_len) {
+ error("data shorter than its stated size");
+ ret = -1;
+ }
+#endif
exit_2:
if (!input)
large_free(inp_start);

--
2.34.1