From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795959961; b=BV8dOqxZFhqXyWmCb6AxuQKRp2CDhWYTOhadCiNKevf+/3wVFFub5itcbSiJXKUjPuYI SN8N4H+GAkOXXB9Xm/kbaEzU5LZ5VgotEsgV19+EKSUgar0SeoB5e02C8QhxYBw3CcItm xMVWx0qkJFAiL6bE7hCFKxje/6av29EoKYxK92gztES+Qnxw3LX6n0oDsntp7Os4bLnfI chfhJJ+GFNDylZT6zplB5ldZivam5H3hx+VPPcF6pmswKe+VfiOrw5FLnc1FSdEG4vuPs sU9vh8W+mt4lcBIhUUl5dtIhhGwxymSIOQQkQktxffyQYlhNgxITUweIBpuWw6V/FHQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795959961; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Received:Date:Mime-Version:X-Developer-Key: X-Developer-Signature:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=zy8Y/Nx1/u7XePzYwDeNkPZRKGtThkxqXWUMGkY+y68=; b=Mta/YlY+Kb38T1nao+YbPhBe/oYukQ2AI4/hhHjAiBmAlWIlvctHr/hhzr7axLSRLm1P NFQTYruV19yXDfr5xqD6gQk4I87H+MIQ7CV6tovumrmeVQW/xwsQwEzkUT0J+ICiAj1m2 KEWq2WtDJzOZzahdEPvL7JphFuW+937pXasLqwxc8/JCe/p+BPSrBk1xk9PU44mufra6k uAsAxvv1po/rLXx2dY6rppoRJaJR9dwl+CcxtnHP6aXbpGuFuyeNxbQt3YsHPGxYvoWMy 6O/duvmKiBcjiK84qEp4VZQKDbhFX2CXr5csP9QG1IOB/amLrFKWLOkdHjF3rYd9L0g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="YThw/oZU"; arc=none smtp.remote-ip=209.85.128.70 Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FB14606A339 for ; Sun, 29 Nov 2026 13:46:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 1FB14606A339 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=YThw/oZU Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-4a1fdde384cso68307575e9.3 for ; Sun, 29 Nov 2026 05:46:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795959959; x=1796564759; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:mime-version:date:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zy8Y/Nx1/u7XePzYwDeNkPZRKGtThkxqXWUMGkY+y68=; b=YThw/oZUz/+HbfmCMI2v99pB/Ggjt1le8oSF434fHeoVuL65zScM6W+u9J+yoqFlLD xJD/N8aCaC8KHN0Fd1dPKeb1CW1LBC1cREVGfqPt2mWie5SMuvNdb/kks47lin9pLH2y Uxmc8E8JrrXtub/psSwIKbwoEp/l3RkaC8e9PNUWn5ZugN6lUY8GyzqC81h+zxQ26w5b uf0r/ShB/wgUWkqxUPhK52TvPbMjXEMLKNNxPIHxH43LLzpQw56xeWcZb3UgkCPLdqLQ pdHaLrJ4JE30B1Mp0kyL1OhlEw+W2lEaCbo7hS5JO2FjVEvkCUb2cXkjLpTfaLeB16Jd 2jJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795959959; x=1796564759; h=content-type:cc:to:from:subject:message-id:mime-version:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=zy8Y/Nx1/u7XePzYwDeNkPZRKGtThkxqXWUMGkY+y68=; b=Wi9IVMGI8UpVUezIYKfpwWwnF4c7hHiqM8NjnqVaMq77gwt8R4vzYfqNeWarSQvYl+ 2D/1bmiI5kh8cpsR6TjSp6BEs3sFVxAt88gAHBw7EuHeSDZESQC/q0EGJN3AT436Hwfs YqkCCewic0JZRMcIS7t0v7GiKCIUaWBlmGCh+f28OSyQt8FzLxN7qVrEFk+f+tLh5FTX q84tOJQD+5hs0GH7FuecxqHAJ/h4RZ2ss6AQ3jwdruRWd6lPnuNfua8tCrmsCUftv1a6 9OWQzhH7Gdq95P83gDMnt6UZ59/IxIRkXYi5TGQtVwh/IZ7Qk7NZeeaS6UxCTT5+/STl FLYw== X-Gm-Message-State: APuU4Z8UzjTYUp2itNcXH9ZZd0a+/3UI6s+0MpTB8x+GJDWTpIZlFAxz VARzDqs22pttsQYP0uWmIVGiyz6O8GstvPjO9AyAOlRjV6GRLmBRqTqauhyvUS55K9A5yb5FVqZ LYK544VfBRoZzRCvkyWBtPj+w2Re++D1MEd53Vvs3liWrwQ50Bbb9wvC6cdMtqxqdzLkozI+9e+ ImmCU0tZDFSgfBQm1TX3zjrXYajtguRQ== X-Received: from wrth5.prod.google.com ([2002:a5d:5045:0:b0:48e:3c17:c059]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4584:b0:4a7:c366:d791 with SMTP id 5b1f17b1804b1-4bb887e266amr595538225e9.1.1795959959245; Sun, 29 Nov 2026 05:45:59 -0800 (PST) Date: Sun, 29 Nov 2026 14:45:56 +0100 X-Mailing-List: linux-efi@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=6677; i=ardb@kernel.org; h=from:subject; bh=GtcgoqFNJKtyatiJ0aL+AfBqZg4Ft7bzWUNrx8O8zwg=; b=owGbwMvMwCVmkMcZplerG8N4Wi2JIZtHZ8qDU+1JU89ZR5tfu9f7+YDCpi97Hm+bc6eqsXXZh Kj1+mWlHaUsDGJcDLJiiiwCs/++23l6olSt8yxZmDmsTCBDGLg4BWAitokM//OXTTX1b3Uo5927 24X7sP+Fwuwqji0Gz047uz3uqWx6nMzIsG7q5pXlKU+SXK98Epu2Yg5jBduFR+mKR20snE+t4mb XZQUA X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog Message-ID: <20261129134555.1713362-2-ardb+git@google.com> Subject: [PATCH] efi/zboot: Expose in-memory payload size in header From: Ard Biesheuvel To: linux-efi@vger.kernel.org Cc: Ard Biesheuvel Content-Type: text/plain; charset="UTF-8" From: Ard Biesheuvel Record the uncompressed kernel payload's full in-memory size (_end - _text, including .bss and any trailing uninitialized regions) as a 64-bit little-endian integer in the two reserved words at byte offset 0x10 of the EFI zboot header, and expose it to the zboot stub via .rodata so it allocates the full in-memory footprint and zeroes the trailing [payload_size, load_size) region after decompression. This allows loaders and kexec implementations to determine the required destination memory footprint directly from the outer zboot header before decompressing the payload stream, and eliminates the build-time copy_and_pad step that previously zero-padded vmlinux.bin to include .bss. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/Makefile.zboot | 28 ++++++++------------- drivers/firmware/efi/libstub/zboot-header.S | 6 ++++- drivers/firmware/efi/libstub/zboot.c | 15 ++++++++--- drivers/firmware/efi/libstub/zboot.lds | 4 ++- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/drivers/firmware/efi/libstub/Makefile.zboot b/drivers/firmware/efi/libstub/Makefile.zboot index 846ca323435a..f5dc07c5a65a 100644 --- a/drivers/firmware/efi/libstub/Makefile.zboot +++ b/drivers/firmware/efi/libstub/Makefile.zboot @@ -4,34 +4,28 @@ # EFI_ZBOOT_PAYLOAD, EFI_ZBOOT_BFD_TARGET, EFI_ZBOOT_MACH_TYPE and # EFI_ZBOOT_FORWARD_CFI -quiet_cmd_copy_and_pad = PAD $@ - cmd_copy_and_pad = cp $< $@; \ - truncate -s $$(hexdump -s16 -n4 -e '"%u"' $<) $@ - -# Pad the file to the size of the uncompressed image in memory, including BSS -$(obj)/vmlinux.bin: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE - $(call if_changed,copy_and_pad) - comp-type-y := gzip zboot-method-y := gzip comp-type-$(CONFIG_KERNEL_ZSTD) := zstd zboot-method-$(CONFIG_KERNEL_ZSTD) := zstd22 -$(obj)/vmlinuz: $(obj)/vmlinux.bin FORCE +$(obj)/vmlinuz: $(obj)/$(EFI_ZBOOT_PAYLOAD) FORCE +$(call if_changed,$(zboot-method-y)) -# subtract the two symbol values to obtain the offset of struct efi_image_info -get_image_info_offset = $(shell $(NM) -n $(objtree)/vmlinux |\ - grep -E ' _text$$| __efistub_efistub_image_info$$' |\ - awk 'NR == 1 { a = toupper($$1) } \ - NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\ - bc) +# subtract two vmlinux symbol values to obtain a relative offset or size +vmlinux_sym_diff = $(shell $(NM) -n $(objtree)/vmlinux |\ + grep -E ' $(1)$$| $(2)$$' |\ + awk 'NR == 1 { a = toupper($$1) } \ + NR == 2 { print "obase=16; ibase=16; " toupper($$1) "-" a }' |\ + bc) # avoid eager evaluation to prevent references to non-existent build artifacts OBJCOPYFLAGS_vmlinuz.o = -I binary -O $(EFI_ZBOOT_BFD_TARGET) $(EFI_ZBOOT_OBJCOPY_FLAGS) \ --rename-section .data=.gzdata,load,alloc,readonly,contents \ - --add-symbol efistub_image_info_offset=0x$(get_image_info_offset) + --add-symbol efistub_image_info_offset=0x$(call \ + vmlinux_sym_diff,_text,__efistub_efistub_image_info) \ + --add-symbol zboot_load_size=0x$(call vmlinux_sym_diff,_text,_end) $(obj)/vmlinuz.o: $(obj)/vmlinuz FORCE $(call if_changed,objcopy) @@ -62,4 +56,4 @@ OBJCOPYFLAGS_vmlinuz.efi := -O binary $(obj)/vmlinuz.efi: $(obj)/vmlinuz.efi.elf FORCE $(call if_changed,objcopy) -targets += zboot-header.o vmlinux.bin vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi +targets += zboot-header.o vmlinuz vmlinuz.o vmlinuz.efi.elf vmlinuz.efi diff --git a/drivers/firmware/efi/libstub/zboot-header.S b/drivers/firmware/efi/libstub/zboot-header.S index e8da65faf8b7..ba8af9251bf7 100644 --- a/drivers/firmware/efi/libstub/zboot-header.S +++ b/drivers/firmware/efi/libstub/zboot-header.S @@ -18,7 +18,11 @@ __efistub_efi_zboot_header: .ascii "zimg" // image type .long __efistub__gzdata_start - .Ldoshdr // payload offset .long __efistub__gzdata_size // payload size - .long 0, 0 // reserved +#ifdef CONFIG_64BIT + .quad zboot_load_size // load size +#else + .long zboot_load_size, 0x0 // load size +#endif .asciz COMP_TYPE // compression type .org .Ldoshdr + 0x38 .long LINUX_PE_MAGIC diff --git a/drivers/firmware/efi/libstub/zboot.c b/drivers/firmware/efi/libstub/zboot.c index 4089a29068ca..fc37f843557d 100644 --- a/drivers/firmware/efi/libstub/zboot.c +++ b/drivers/firmware/efi/libstub/zboot.c @@ -48,9 +48,10 @@ static efi_status_t efi_zboot_remap_image(const struct efi_image_info *image_inf asmlinkage efi_status_t __efiapi efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) { + extern const u64 zboot_load_size; static efi_guid_t loaded_image_guid = LOADED_IMAGE_PROTOCOL_GUID; + unsigned long image_base, payload_size, alloc_size; char *cmdline_ptr __free(efi_pool) = NULL; - unsigned long image_base, alloc_size; efi_loaded_image_t *image; efi_status_t status; @@ -69,9 +70,15 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) efi_info("Decompressing Linux Kernel...\n"); - status = efi_zboot_decompress_init(&alloc_size); + if (zboot_load_size != (unsigned long)zboot_load_size) + return EFI_LOAD_ERROR; + alloc_size = zboot_load_size; + + status = efi_zboot_decompress_init(&payload_size); if (status != EFI_SUCCESS) return status; + if (payload_size > alloc_size) + return EFI_LOAD_ERROR; // If the architecture has a preferred address for the image, // try that first. @@ -107,10 +114,12 @@ efi_zboot_entry(efi_handle_t handle, efi_system_table_t *systab) } // Decompress the payload into the newly allocated buffer - status = efi_zboot_decompress((void *)image_base, alloc_size); + status = efi_zboot_decompress((void *)image_base, payload_size); if (status != EFI_SUCCESS) goto error; + memset((void *)image_base + payload_size, 0, alloc_size - payload_size); + auto image_info = zboot_get_image_info(image_base); status = efi_zboot_remap_image(image_info, image_base, alloc_size) ?: efi_stub_common(handle, image, image_info, cmdline_ptr); diff --git a/drivers/firmware/efi/libstub/zboot.lds b/drivers/firmware/efi/libstub/zboot.lds index 17d1735472d6..975db6253347 100644 --- a/drivers/firmware/efi/libstub/zboot.lds +++ b/drivers/firmware/efi/libstub/zboot.lds @@ -18,7 +18,9 @@ SECTIONS __efistub__gzdata_end = .; *(.rodata* .init.rodata* .srodata*) - . = ALIGN(4); + . = ALIGN(8); + __efistub_zboot_load_size = .; + QUAD(zboot_load_size); __efistub_efistub_image_info_offset = .; LONG(efistub_image_info_offset); -- 2.56.1.839.g22a8290db9-goog