All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb@kernel.org>
To: linux-efi@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nsc@kernel.org>,
	linux-kbuild@vger.kernel.org
Subject: [PATCH 2/2] efi/zboot: Use ZSTD frame header for uncompressed payload size
Date: Thu, 26 Nov 2026 23:44:03 +0100	[thread overview]
Message-ID: <20261126224400.10147-6-ardb@kernel.org> (raw)
In-Reply-To: <20261126224400.10147-4-ardb@kernel.org>

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).

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 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(-)

diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot
index 52baacf3e9d94..1cd3a6ed7f223 100644
--- a/drivers/firmware/efi/libstub/Makefile.zboot
+++ b/drivers/firmware/efi/libstub/Makefile.zboot
@@ -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)
 
diff --git a/drivers/firmware/efi/libstub/zboot-decompress-gzip.c b/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
index e97a7e9d3c980..f508816ac26dc 100644
--- a/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
+++ b/drivers/firmware/efi/libstub/zboot-decompress-gzip.c
@@ -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);
diff --git a/drivers/firmware/efi/libstub/zboot-decompress-zstd.c b/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
index bde9d94dd2e3a..8a55a09641370 100644
--- a/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
+++ b/drivers/firmware/efi/libstub/zboot-decompress-zstd.c
@@ -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);
diff --git a/drivers/firmware/efi/libstub/zboot-header.S b/drivers/firmware/efi/libstub/zboot-header.S
index b6431edd0fc96..e8da65faf8b7b 100644
--- a/drivers/firmware/efi/libstub/zboot-header.S
+++ b/drivers/firmware/efi/libstub/zboot-header.S
@@ -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
diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds
index a785c01237497..d7633f5d61c3b 100644
--- a/drivers/firmware/efi/libstub/zboot.lds
+++ b/drivers/firmware/efi/libstub/zboot.lds
@@ -17,7 +17,6 @@ SECTIONS
 	.rodata : ALIGN(8) {
 		__efistub__gzdata_start = .;
 		*(.gzdata)
-		__efistub_payload_size = . - 4;
 		__efistub__gzdata_end = .;
 		*(.rodata* .init.rodata* .srodata*)
 
-- 
2.47.3


      parent reply	other threads:[~2026-11-26 22:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 22:44 [PATCH 0/2] efi/zboot: Put ZSTD output size where it belongs Ard Biesheuvel
2026-11-26 22:44 ` [PATCH 1/2] scripts/Makefile.lib: Capture decompressed size in ZSTD22 metadata Ard Biesheuvel
2026-11-28 20:14   ` Nathan Chancellor
2026-11-30 16:19     ` Ard Biesheuvel
2026-11-26 22:44 ` Ard Biesheuvel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126224400.10147-6-ardb@kernel.org \
    --to=ardb@kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=nsc@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.