Skip to contentSkip to conversation

efi/zboot: Put ZSTD output size where it belongs

PATCH New Ard Biesheuvel 1 participant 3 messages Nov 26

Subsystems (3)
lore
Share & tools
lore

Ard Biesheuvel

author maintainer
#0 lore raw

cover efi/zboot: Put ZSTD output size where it belongs

The intent of the EFI zboot format is that non-EFI loaders can access and decompress the payload using standard libraries and tools. However, the ZSTD decompressed payload size is not stored in the header where the standard library would look for it, but appended to the end similar to how GZIP stores it.

Let's fix this, by capturing and storing the input size explicitly.

This is a prerequisite for upcoming ELF support in EFI zboot, which concatenates several payloads, and therefore does not support appending arbitrary data at the end.

Cc Nathan Chancellor <[email protected]>
Cc Nicolas Schier <[email protected]>

Ard Biesheuvel (2):
scripts/Makefile.lib: Capture decompressed size in ZSTD22 metadata efi/zboot: Use ZSTD frame header for uncompressed payload size

 drivers/firmware/efi/libstub/Makefile.zboot          |  8 +-------
 drivers/firmware/efi/libstub/zboot-decompress-gzip.c |  5 +++--
 drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 17 ++++++++++++++---
 drivers/firmware/efi/libstub/zboot-header.S          |  2 +-
 drivers/firmware/efi/libstub/zboot.lds               |  1 -
 scripts/Makefile.lib                                 | 10 +++++++---
 6 files changed, 26 insertions(+), 17 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/2 scripts/Makefile.lib: Capture decompressed size in ZSTD22 metadata

Tweak the [unused] ZSTD22 compression rule so that the combined size of the uncompressed input is recorded in the ZSTD metadata. This allows ZSTD standard tooling to query it.

---
 scripts/Makefile.lib | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
scripts/Makefile.lib +7 -3
index 19e96b30a122f..5b58577b158af 100644
@@ -344,13 +344,17 @@ quiet_cmd_gzip = GZIP    $@
 
 # Bzip2 and LZMA do not include size in file... so we have to fake that;
 # append the size as a 32-bit littleendian number as gzip does.
-size_append = printf $(shell						\
+total_file_size = $(shell						\
 dec_size=0;								\
 for F in $(real-prereqs); do					\
 	fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F);	\
 	dec_size=$$(expr $$dec_size + $$fsize);				\
 done;									\
-printf "%08x\n" $$dec_size |						\
+printf "%d\n" $$dec_size						\
+)
+
+size_append = printf $(shell						\
+printf "%08x\n" $(total_file_size) |					\
 	sed 's/\(..\)/\1 /g' | {					\
 		read ch0 ch1 ch2 ch3;					\
 		for ch in $$ch3 $$ch2 $$ch1 $$ch0; do			\
@@ -479,7 +483,7 @@ quiet_cmd_zstd = ZSTD    $@
       cmd_zstd = cat $(real-prereqs) | $(ZSTD) -19 > $@
 
 quiet_cmd_zstd22 = ZSTD22  $@
-      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra > $@
+      cmd_zstd22 = cat $(real-prereqs) | $(ZSTD) -22 --ultra --stream-size=$(total_file_size) > $@
 
 quiet_cmd_zstd22_with_size = ZSTD22  $@
       cmd_zstd22_with_size = { cat $(real-prereqs) | $(ZSTD) -22 --ultra; $(size_append); } > $@
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/2 efi/zboot: Use ZSTD frame header for uncompressed payload size

In reply to Ard Biesheuvel

Pass --stream-size to zstd in cmd_zstd22 so the uncompressed content size is recorded in the ZSTD frame header even when compressing from a pipe, switch EFI zboot from zstd22_with_size to zstd22, and use zstd_get_frame_header() in the ZSTD decompressor to obtain the uncompressed allocation size.

This avoids corrupting the embedded ZSTD stream with a 4-byte trailing size word from the perspective of standard zstd tooling, removes the ZBOOT_SIZE_LEN adjustment from the zboot header, and makes reading the trailing 32-bit uncompressed size specific to the GZIP decompressor (where ISIZE is part of the format).

---
 drivers/firmware/efi/libstub/Makefile.zboot          |  8 +-------
 drivers/firmware/efi/libstub/zboot-decompress-gzip.c |  5 +++--
 drivers/firmware/efi/libstub/zboot-decompress-zstd.c | 17 ++++++++++++++---
 drivers/firmware/efi/libstub/zboot-header.S          |  2 +-
 drivers/firmware/efi/libstub/zboot.lds               |  1 -
 5 files changed, 19 insertions(+), 14 deletions(-)
drivers/firmware/efi/libstub/Makefile.zboot +1 -7
index 52baacf3e9d94..1cd3a6ed7f223 100644
@@ -12,16 +12,11 @@ quiet_cmd_copy_and_pad = PAD     $@
 $(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE
 	$(call if_changed,copy_and_pad)
 
-# in GZIP, the appended le32 carrying the uncompressed size is part of the
-# format, but in other cases, we just append it at the end for convenience,
-# causing the original tools to complain when checking image integrity.
 comp-type-y				:= gzip
 zboot-method-y				:= gzip
-zboot-size-len-y			:= 0
 
 comp-type-$(CONFIG_KERNEL_ZSTD)		:= zstd
-zboot-method-$(CONFIG_KERNEL_ZSTD)	:= zstd22_with_size
-zboot-size-len-$(CONFIG_KERNEL_ZSTD)	:= 4
+zboot-method-$(CONFIG_KERNEL_ZSTD)	:= zstd22
 
 $(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE
 	+$(call if_changed,$(zboot-method-y))
@@ -37,7 +32,6 @@ aflags-zboot-header-$(EFI_ZBOOT_FORWARD_CFI) := \
 
 AFLAGS_zboot-header.o += -DMACHINE_TYPE=IMAGE_FILE_MACHINE_$(EFI_ZBOOT_MACH_TYPE) \
 			 -DZBOOT_EFI_PATH="\"$(abspath $(obj)/vmlinuz.efi.elf)\"" \
-			 -DZBOOT_SIZE_LEN=$(zboot-size-len-y) \
 			 -DCOMP_TYPE="\"$(comp-type-y)\"" \
 			 $(aflags-zboot-header-y)
 
drivers/firmware/efi/libstub/zboot-decompress-gzip.c +3 -2
index e97a7e9d3c980..f508816ac26dc 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/efi.h>
+#include <linux/unaligned.h>
 #include <linux/zlib.h>
 
 #include <asm/efi.h>
@@ -12,7 +13,6 @@
 #include "inflate.c"
 
 extern unsigned char _gzdata_start[], _gzdata_end[];
-extern u32 __aligned(1) payload_size;
 
 static struct z_stream_s stream;
 
@@ -38,7 +38,8 @@ efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size)
 		goto out;
 	}
 
-	*alloc_size = payload_size;
+	/* the last 4 bytes of a GZIP stream carry the uncompressed size */
+	*alloc_size = get_unaligned_le32(_gzdata_end - 4);
 	return EFI_SUCCESS;
 out:
 	efi_free(zlib_inflate_workspacesize(), (unsigned long)stream.workspace);
drivers/firmware/efi/libstub/zboot-decompress-zstd.c +14 -3
index bde9d94dd2e3a..8a55a09641370 100644
@@ -9,21 +9,32 @@
 #include "efistub.h"
 
 extern unsigned char _gzdata_start[], _gzdata_end[];
-extern u32 __aligned(1) payload_size;
 
 static size_t wksp_size;
 static void *wksp;
 
 efi_status_t efi_zboot_decompress_init(unsigned long *alloc_size)
 {
+	zstd_frame_header header;
 	efi_status_t status;
+	size_t ret;
+
+	ret = zstd_get_frame_header(&header, _gzdata_start,
+				    _gzdata_end - _gzdata_start);
+	if (ret != 0 ||
+	    header.frameContentSize == 0 ||
+	    header.frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN ||
+	    header.frameContentSize != (unsigned long)header.frameContentSize) {
+		efi_err("ZSTD-compressed data has an invalid frame header\n");
+		return EFI_LOAD_ERROR;
+	}
 
 	wksp_size = zstd_dctx_workspace_bound();
 	status = efi_allocate_pages(wksp_size, (unsigned long *)&wksp, ULONG_MAX);
 	if (status != EFI_SUCCESS)
 		return status;
 
-	*alloc_size = payload_size;
+	*alloc_size = header.frameContentSize;
 	return EFI_SUCCESS;
 }
 
@@ -34,7 +45,7 @@ efi_status_t efi_zboot_decompress(u8 *out, unsigned long outlen)
 	int retval;
 
 	ret = zstd_decompress_dctx(dctx, out, outlen, _gzdata_start,
-				   _gzdata_end - _gzdata_start - 4);
+				   _gzdata_end - _gzdata_start);
 	efi_free(wksp_size, (unsigned long)wksp);
 
 	retval = zstd_get_error_code(ret);
drivers/firmware/efi/libstub/zboot-header.S +1 -1
index b6431edd0fc96..e8da65faf8b7b 100644
@@ -17,7 +17,7 @@ __efistub_efi_zboot_header:
 	.long		IMAGE_DOS_SIGNATURE
 	.ascii		"zimg"					// image type
 	.long		__efistub__gzdata_start - .Ldoshdr	// payload offset
-	.long		__efistub__gzdata_size - ZBOOT_SIZE_LEN	// payload size
+	.long		__efistub__gzdata_size			// payload size
 	.long		0, 0					// reserved
 	.asciz		COMP_TYPE				// compression type
 	.org		.Ldoshdr + 0x38
drivers/firmware/efi/libstub/zboot.lds +0 -1
index a785c01237497..d7633f5d61c3b 100644
@@ -17,7 +17,6 @@ SECTIONS
 	.rodata : ALIGN(8) {
 		__efistub__gzdata_start = .;
 		*(.gzdata)
-		__efistub_payload_size = . - 4;
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
 
Signature · 1 line
2.47.3