[PATCH 1/3] ARM: compressed: Pass the actual output length to the decompressor
From: Jonathan Neuschäfer
Date: Wed Apr 12 2023 - 17:22:51 EST
ZSTD writes outside of the space that is necessary for the uncompressed
data, when it is told it has unlimited output length. To fix this, pass
the actual output length (the length of the uncompressed kernel) to the
decompressor.
The uncompressed length is already stored as a little endian 32-bit
constant before the input_data_end symbol.
Signed-off-by: Jonathan Neuschäfer <j.neuschaefer@xxxxxxx>
---
arch/arm/boot/compressed/decompress.c | 4 ++--
arch/arm/boot/compressed/misc.c | 12 ++++++++++--
2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/arch/arm/boot/compressed/decompress.c b/arch/arm/boot/compressed/decompress.c
index 74255e8198314..3d098b84ee391 100644
--- a/arch/arm/boot/compressed/decompress.c
+++ b/arch/arm/boot/compressed/decompress.c
@@ -59,7 +59,7 @@ extern char * strchrnul(const char *, int);
#include "../../../../lib/decompress_unlz4.c"
#endif
-int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x))
+int do_decompress(u8 *input, int len, u8 *output, int outlen, void (*error)(char *x))
{
- return __decompress(input, len, NULL, NULL, output, 0, NULL, error);
+ return __decompress(input, len, NULL, NULL, output, outlen, NULL, error);
}
diff --git a/arch/arm/boot/compressed/misc.c b/arch/arm/boot/compressed/misc.c
index abfed1aa2baa8..8402b29bccc82 100644
--- a/arch/arm/boot/compressed/misc.c
+++ b/arch/arm/boot/compressed/misc.c
@@ -22,6 +22,7 @@ unsigned int __machine_arch_type;
#include <linux/compiler.h> /* for inline */
#include <linux/types.h>
#include <linux/linkage.h>
+#include <asm/unaligned.h>
#include "misc.h"
#ifdef CONFIG_ARCH_EP93XX
#include "misc-ep93xx.h"
@@ -131,17 +132,24 @@ asmlinkage void __div0(void)
error("Attempting division by 0!");
}
-extern int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
+extern int do_decompress(u8 *input, int len, u8 *output, int outlen,
+ void (*error)(char *x));
+static u32 get_inflated_image_size(void)
+{
+ return get_unaligned_le32(input_data_end - 4);
+}
void
decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
unsigned long free_mem_ptr_end_p,
int arch_id)
{
+ unsigned long output_data_len;
int ret;
output_data = (unsigned char *)output_start;
+ output_data_len = get_inflated_image_size();
free_mem_ptr = free_mem_ptr_p;
free_mem_end_ptr = free_mem_ptr_end_p;
__machine_arch_type = arch_id;
@@ -153,7 +161,7 @@ decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
putstr("Uncompressing Linux...");
ret = do_decompress(input_data, input_data_end - input_data,
- output_data, error);
+ output_data, output_data_len, error);
if (ret)
error("decompressor returned an error");
else
--
2.39.2