x86/boot: Rework PE header generation
Subsystems (3)
Ard Biesheuvel
maintainercover x86/boot: Rework PE header generation
Now that the EFI stub boot flow no longer relies on memory that is executable and writable at the same time, we can reorganize the PE/COFF view of the kernel image and expose the decompressor binary's code and r/o data as a .text section and data/bss as a .data section, using 4k alignment and limited permissions.
Doing so is necessary for compatibility with hardening measures that are being rolled out on x86 PCs built to run Windows (i.e., the majority of them). The EFI boot environment that the Linux EFI stub executes in is especially sensitive to safety issues, given that a vulnerability in the loader of one OS can be abused to attack another.
In true x86 fashion, this is more complicated than on other architectures, which have implemented this code/data split with 4k alignment from the beginning. The complicating factor here is that the boot image consists of two different parts, which are stitched together and fixed up using a special build tool.
The first three patches simplify the x86 EFI stub code so it does not even bother reading the setup header from the image - passing arguments this way is not supported by EFI boot anyway.
Then, the bzImage is simplified and reorganized, primarily by:
- dropping the ancient 'bugger off' message occupying much of the header space
- using a fixed size of 16k for the setup block
- setting header values from asm instead of via the build tool
Finally, the payload is split into .text and .data, and the section and file alignment increased to 4k/512 respectively.
The only remaining task performed by the build tool is generating the CRC-32 that is fundamentally broken in practice and never used, so that is dropped entirely at the end.
This supersedes the work proposed by Evgeniy last year, which did a major rewrite of the build tool in order to clean it up, before updating it to generate the new 4k aligned image layout. As this series proves, the build tool is mostly unnecessary, and we have too many of those already.
Ard Biesheuvel (17):
x86/efi: Drop EFI stub .bss from .data section
x86/efi: Disregard setup header of loaded image
x86/efi: Drop alignment flags from PE section headers x86/boot: Remove the 'bugger off' message
x86/boot: Omit compression buffer from PE/COFF image memory footprint x86/boot: Drop redundant code setting the root device x86/boot: Grab kernel_info offset from zoffset header directly x86/boot: Drop references to startup_64
x86/boot: Set EFI handover offset directly in header asm x86/boot: Drop workaround for binutils 2.14 in linker script ASSERTs x86/boot: Use fixed size of 16k for setup block
x86/boot: Derive file size from _edata symbol
x86/boot: Construct PE/COFF .text section from assembler x86/boot: Drop PE/COFF .reloc section
x86/boot: Split off PE/COFF .data section
x86/boot: Increase section and file alignment to 4k/512 x86/boot: Drop CRC-32 checksum and the build tool that generates it
Documentation/arch/x86/boot.rst | 10 - arch/x86/boot/Makefile | 12 +- arch/x86/boot/compressed/vmlinux.lds.S | 5 +- arch/x86/boot/header.S | 217 ++++----- arch/x86/boot/setup.ld | 21 +- arch/x86/boot/tools/.gitignore | 2 - arch/x86/boot/tools/build.c | 502 -------------------- drivers/firmware/efi/libstub/Makefile | 7 - drivers/firmware/efi/libstub/x86-stub.c | 46 +- 9 files changed, 113 insertions(+), 709 deletions(-) delete mode 100644 arch/x86/boot/tools/.gitignore delete mode 100644 arch/x86/boot/tools/build.c
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 1/17 x86/efi: Drop EFI stub .bss from .data section
Now that the EFI stub always zero inits its BSS section upon entry, there is no longer a need to place the BSS symbols carried by the stub into the .data section.
--- arch/x86/boot/compressed/vmlinux.lds.S | 1 - drivers/firmware/efi/libstub/Makefile | 7 ------- 2 files changed, 8 deletions(-)
arch/x86/boot/compressed/vmlinux.lds.S +0 -1
@@ -47,7 +47,6 @@ SECTIONS _data = . ; *(.data) *(.data.*) - *(.bss.efistub) _edata = . ; } . = ALIGN(L1_CACHE_BYTES);
drivers/firmware/efi/libstub/Makefile +0 -7
@@ -108,13 +108,6 @@ lib-y := $(patsubst %.o,%.stub.o,$(lib-y)) # https://bugs.llvm.org/show_bug.cgi?id=46480 STUBCOPY_FLAGS-y += --remove-section=.note.gnu.property -# -# For x86, bootloaders like systemd-boot or grub-efi do not zero-initialize the -# .bss section, so the .bss section of the EFI stub needs to be included in the -# .data section of the compressed kernel to ensure initialization. Rename the -# .bss section here so it's easy to pick out in the linker script. -# -STUBCOPY_FLAGS-$(CONFIG_X86) += --rename-section .bss=.bss.efistub,load,alloc STUBCOPY_RELOC-$(CONFIG_X86_32) := R_386_32 STUBCOPY_RELOC-$(CONFIG_X86_64) := R_X86_64_64
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 2/17 x86/efi: Disregard setup header of loaded image
In reply to Ard BiesheuvelThe native EFI entrypoint does not take a struct boot_params from the loader, but instead, it constructs one from scratch, using the setup header data placed at the start of the image.
This setup header is placed in a way that permits legacy loaders to manipulate the contents (i.e., to pass the kernel command line or the address and size of an initial ramdisk), but EFI boot does not use it in that way - it only copies the contents that were placed there at build time, but EFI loaders will not (and should not) manipulate the setup header to configure the boot. (Commit 63bf28ceb3ebbe76 "efi: x86: Wipe setup_data on pure EFI boot" deals with some of the fallout of using setup_data in a way that breaks EFI boot.)
As it turns out, there is another reason why copying this header is slightly problematic: it is placed at a fixed offset of 0x1f1 bytes into the image, and this means the PE/COFF view of the image payload has to start there as the setup header is consumed by the program and therefore part of the payload, and this leaves little space to describe all the sections of the image. The only reason to describe the setup header is that the EFI stub may copy this setup header at boot time, but beyond that, none of the contents of this header section are needed by the running code. If the setup header does not need to be accessed at runtime, there is no need to describe it in the first place, freeing up a slot in the section header array.
And actually, none of the non-zero values that are copied from the setup header into the EFI stub's struct boot_params are relevant to the boot now that the EFI stub no longer enters via the legacy decompressor. So omit the copy altogether.
--- drivers/firmware/efi/libstub/x86-stub.c | 46 +++----------------- 1 file changed, 6 insertions(+), 40 deletions(-)
drivers/firmware/efi/libstub/x86-stub.c +6 -40
@@ -449,9 +449,8 @@ void __noreturn efi_stub_entry(efi_handle_t handle, efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,efi_system_table_t *sys_table_arg) { -struct boot_params *boot_params; -struct setup_header *hdr; -void *image_base; + static struct boot_params boot_params __page_aligned_bss; +struct setup_header *hdr = &boot_params.hdr;efi_guid_t proto =LOADED_IMAGE_PROTOCOL_GUID ;int options_size =0 ;efi_status_t status;
@@ -469,30 +468,9 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,efi_exit (handle, status); } - image_base =efi_table_attr (image, image_base); - - status =efi_allocate_pages (sizeof (struct boot_params ), - (unsigned long *)&boot_params,ULONG_MAX ); -if (status !=EFI_SUCCESS ) { -efi_err ("Failed to allocate lowmem for boot params\n" ); -efi_exit (handle, status); - } - -memset (boot_params,0x0 ,sizeof (struct boot_params )); - - hdr = &boot_params->hdr; - -/* Copy the setup header from the second sector to boot_params */ -memcpy (&hdr->jump, image_base +512 , -sizeof (struct setup_header ) - offsetof(struct setup_header , jump)); - -/* -* Fill out some of the header fields ourselves because the -* EFI firmware loader doesn't load the first sector. -*/ +/* assign the setup_header fields that the kernel actually cares about */ hdr->root_flags =1 ; hdr->vid_mode =0xffff ; - hdr->boot_flag =0xAA55 ; hdr->type_of_loader =0x21 ;
@@ -501,25 +479,13 @@ efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,if (!cmdline_ptr) goto fail; -efi_set_u64_split ((unsigned long )cmdline_ptr, - &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr); +efi_set_u64_split ((unsigned long )cmdline_ptr, &hdr->cmd_line_ptr, + &boot_params.ext_cmd_line_ptr); - hdr->ramdisk_image =0 ; - hdr->ramdisk_size =0 ; - -/* -* Disregard any setup data that was provided by the bootloader: -* setup_data could be pointing anywhere, and we have no way of -* authenticating or validating the payload. -*/ - hdr->setup_data =0 ; - -efi_stub_entry (handle, sys_table_arg, boot_params); +efi_stub_entry (handle, sys_table_arg, &boot_params);/* not reached */ fail: -efi_free (sizeof (struct boot_params ), (unsigned long )boot_params); -efi_exit (handle, status); }
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 3/17 x86/efi: Drop alignment flags from PE section headers
In reply to Ard BiesheuvelThe section header flags for alignment are documented in the PE/COFF spec as only being applicable to PE object files, not PE executables such as the Linux bzImage, so let's drop them from the PE header.
--- arch/x86/boot/header.S | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/boot/header.S +4 -8
@@ -209,8 +209,7 @@ section_table: .word 0 # NumberOfLineNumbers .long IMAGE_SCN_CNT_CODE | \ IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_EXECUTE | \ - IMAGE_SCN_ALIGN_16BYTES # Characteristics + IMAGE_SCN_MEM_EXECUTE # Characteristics # # The EFI application loader requires a relocation section
@@ -230,8 +229,7 @@ section_table: .word 0 # NumberOfLineNumbers .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_DISCARDABLE | \ - IMAGE_SCN_ALIGN_1BYTES # Characteristics + IMAGE_SCN_MEM_DISCARDABLE # Characteristics #ifdef CONFIG_EFI_MIXED #
@@ -249,8 +247,7 @@ section_table: .word 0 # NumberOfLineNumbers .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_DISCARDABLE | \ - IMAGE_SCN_ALIGN_1BYTES # Characteristics + IMAGE_SCN_MEM_DISCARDABLE # Characteristics #endif #
@@ -271,8 +268,7 @@ section_table: .word 0 # NumberOfLineNumbers .long IMAGE_SCN_CNT_CODE | \ IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_EXECUTE | \ - IMAGE_SCN_ALIGN_16BYTES # Characteristics + IMAGE_SCN_MEM_EXECUTE # Characteristics .set section_count, (. - section_table) / 40 #endif /* CONFIG_EFI_STUB */
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 4/17 x86/boot: Remove the 'bugger off' message
In reply to Ard BiesheuvelAncient (pre-2003) x86 kernels could boot from a floppy disk straight from the BIOS, using a small real mode boot stub at the start of the image where the BIOS would expect the boot record (or boot block) to appear.
Due to its limitations (kernel size < 1 MiB, no support for IDE, USB or El Torito floppy emulation), this support was dropped, and a Linux aware bootloader is now always required to boot the kernel from a legacy BIOS.
To smoothen this transition, the boot stub was not removed entirely, but replaced with one that just prints an error message telling the user to install a bootloader.
As it is unlikely that anyone doing direct floppy boot with such an ancient kernel is going to upgrade to v6.5+ and expect that this boot method still works, printing this message is kind of pointless, and so it should be possible to remove the logic that emits it.
Let's free up this space so it can be used to expand the PE header in a subsequent patch.
--- arch/x86/boot/header.S | 49 -------------------- arch/x86/boot/setup.ld | 7 +-- 2 files changed, 4 insertions(+), 52 deletions(-)
arch/x86/boot/header.S +0 -49
@@ -38,64 +38,15 @@ SYSSEG = 0x1000 /* historical load address >> 4 */ .code16 .section ".bstext", "ax" - - .global bootsect_start -bootsect_start: #ifdef CONFIG_EFI_STUB # "MZ", MS-DOS header .word MZ_MAGIC -#endif - - # Normalize the start address - ljmp $BOOTSEG, $start2 - -start2: - movw %cs, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %ss - xorw %sp, %sp - sti - cld - - movw $bugger_off_msg, %si - -msg_loop: - lodsb - andb %al, %al - jz bs_die - movb $0xe, %ah - movw $7, %bx - int $0x10 - jmp msg_loop - -bs_die: - # Allow the user to press a key, then reboot - xorw %ax, %ax - int $0x16 - int $0x19 - - # int 0x19 should never return. In case it does anyway, - # invoke the BIOS reset code... - ljmp $0xf000,$0xfff0 - -#ifdef CONFIG_EFI_STUB .org 0x38 # # Offset to the PE header. # .long LINUX_PE_MAGIC .long pe_header -#endif /* CONFIG_EFI_STUB */ - - .section ".bsdata", "a" -bugger_off_msg: - .ascii "Use a boot loader.\r\n" - .ascii "\n" - .ascii "Remove disk and press any key to reboot...\r\n" - .byte 0 - -#ifdef CONFIG_EFI_STUB pe_header: .long PE_MAGIC
arch/x86/boot/setup.ld +4 -3
@@ -10,10 +10,11 @@ ENTRY(_start) SECTIONS { . = 0; - .bstext : { *(.bstext) } - .bsdata : { *(.bsdata) } + .bstext : { + *(.bstext) + . = 495; + } =0xffffffff - . = 495; .header : { *(.header) } .entrytext : { *(.entrytext) } .inittext : { *(.inittext) }
Signature · 1 line
2.39.2
Re: [PATCH 04/17] x86/boot: Remove the 'bugger off' message
Quoted 17 lines · Ard Biesheuvel
Ancient (pre-2003) x86 kernels could boot from a floppy disk straight from the BIOS, using a small real mode boot stub at the start of the image where the BIOS would expect the boot record (or boot block) to appear.
Due to its limitations (kernel size < 1 MiB, no support for IDE, USB or El Torito floppy emulation), this support was dropped, and a Linux aware bootloader is now always required to boot the kernel from a legacy BIOS.
To smoothen this transition, the boot stub was not removed entirely, but replaced with one that just prints an error message telling the user to install a bootloader.
As it is unlikely that anyone doing direct floppy boot with such an ancient kernel is going to upgrade to v6.5+ and expect that this boot method still works, printing this message is kind of pointless, and so it should be possible to remove the logic that emits it.
Quoted 5 lines
Let's free up this space so it can be used to expand the PE header in a subsequent patch.
Signed-off-by Ard Biesheuvel <[email protected]
Good riddance.
Ard Biesheuvel
maintainerPATCH 5/17 x86/boot: Omit compression buffer from PE/COFF image memory footprint
In reply to Ard BiesheuvelNow that the EFI stub decompresses the kernel and hands over to the decompressed image directly, there is no longer a need to provide a decompression buffer as part of the .BSS allocation of the PE/COFF image. It also means the PE/COFF image can be loaded anywhere in memory, and setting the preferred image base is unnecessary. So drop the handling of this from the header and from the build tool.
--- arch/x86/boot/header.S | 6 +-- arch/x86/boot/tools/build.c | 50 +++----------------- 2 files changed, 8 insertions(+), 48 deletions(-)
arch/x86/boot/header.S +2 -4
@@ -90,12 +90,10 @@ optional_header: #endif extra_header_fields: - # PE specification requires ImageBase to be 64k aligned - .set image_base, (LOAD_PHYSICAL_ADDR + 0xffff) & ~0xffff #ifdef CONFIG_X86_32 - .long image_base # ImageBase + .long 0 # ImageBase #else - .quad image_base # ImageBase + .quad 0 # ImageBase #endif .long 0x20 # SectionAlignment .long 0x20 # FileAlignment
arch/x86/boot/tools/build.c +6 -44
100 lines not on this page · raw message
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 6/17 x86/boot: Drop redundant code setting the root device
In reply to Ard BiesheuvelThe root device defaults to 0,0 and is no longer configurable at build time [0], so there is no need for the build tool to ever write to this field.
[0] 079f85e624189292 ("x86, build: Do not set the root_dev field in bzImage")
--- arch/x86/boot/header.S | 2 +- arch/x86/boot/tools/build.c | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-)
arch/x86/boot/header.S +1 -1
@@ -236,7 +236,7 @@ root_flags: .word ROOT_RDONLY syssize: .long 0 /* Filled in by build.c */ ram_size: .word 0 /* Obsolete */ vid_mode: .word SVGA_MODE -root_dev: .word 0 /* Filled in by build.c */ +root_dev: .word 0 /* Default to major/minor 0/0 */ boot_flag: .word 0xAA55 # offset 512, entry point
arch/x86/boot/tools/build.c +0 -7
@@ -40,10 +40,6 @@ typedef unsigned char u8;typedef unsigned short u16 ;typedef unsigned int u32 ; -#define DEFAULT_MAJOR_ROOT 0 -#define DEFAULT_MINOR_ROOT 0 -#define DEFAULT_ROOT_DEV (DEFAULT_MAJOR_ROOT << 8 | DEFAULT_MINOR_ROOT) -/* Minimal number of setup sectors */ #define SETUP_SECT_MIN 5#define SETUP_SECT_MAX 64
@@ -399,9 +395,6 @@ int main(int argc, char ** argv)update_pecoff_setup_and_reloc (i); -/* Set the default root device */ -put_unaligned_le16 (DEFAULT_ROOT_DEV , &buf[508 ]); -/* Open and stat the kernel file */ fd =open (argv[2 ],O_RDONLY );if (fd <0 )
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 7/17 x86/boot: Grab kernel_info offset from zoffset header directly
In reply to Ard BiesheuvelInstead of parsing zoffset.h and poking the kernel_info offset value into the header from the build tool, just grab the value directly in the asm file that describes this header.
--- arch/x86/boot/header.S | 2 +- arch/x86/boot/tools/build.c | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-)
arch/x86/boot/header.S +1 -1
@@ -526,7 +526,7 @@ pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr init_size: .long INIT_SIZE # kernel initialization size handover_offset: .long 0 # Filled in by build.c -kernel_info_offset: .long 0 # Filled in by build.c +kernel_info_offset: .long ZO_kernel_info # End of setup header #####################################################
arch/x86/boot/tools/build.c +0 -4
@@ -59,7 +59,6 @@ static unsigned long efi32_stub_entry;static unsigned long efi64_stub_entry;static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry; -static unsigned long kernel_info;static unsigned long startup_64;static unsigned long _end;
@@ -339,7 +338,6 @@ static void parse_zoffset(char *fname)PARSE_ZOFS (p, efi64_stub_entry);PARSE_ZOFS (p, efi_pe_entry);PARSE_ZOFS (p, efi32_pe_entry); -PARSE_ZOFS (p, kernel_info);PARSE_ZOFS (p, startup_64);PARSE_ZOFS (p, _end);
@@ -422,8 +420,6 @@ int main(int argc, char ** argv)update_pecoff_text (setup_sectors *512 , i + (sys_size *16 ));efi_stub_entry_update (); -/* Update kernel_info offset. */ -put_unaligned_le32 (kernel_info, &buf[0x268 ]); crc =partial_crc32 (buf, i, crc);if (fwrite (buf,1 , i, dest) != i)
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 8/17 x86/boot: Drop references to startup_64
In reply to Ard BiesheuvelThe x86 boot image generation tool assign a default value to startup_64 and subsequently parses the actual value from zoffset.h but it never actually uses the value anywhere. So remove this code.
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/tools/build.c | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-)
arch/x86/boot/Makefile +1 -1
@@ -89,7 +89,7 @@ $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE SETUP_OBJS = $(addprefix $(obj)/,$(setup-y)) -sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|startup_64\|efi32_stub_entry\|efi64_stub_entry\|efi_pe_entry\|efi32_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|z_.*\)$$/\#define ZO_\2 0x\1/p' +sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi32_stub_entry\|efi64_stub_entry\|efi_pe_entry\|efi32_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|z_.*\)$$/\#define ZO_\2 0x\1/p' quiet_cmd_zoffset = ZOFFSET $@ cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@
arch/x86/boot/tools/build.c +0 -3
@@ -59,7 +59,6 @@ static unsigned long efi32_stub_entry;static unsigned long efi64_stub_entry;static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry; -static unsigned long startup_64;static unsigned long _end;/*----------------------------------------------------------------------*/
@@ -263,7 +262,6 @@ static void efi_stub_defaults(void) efi_pe_entry =0x10 ;#else efi_pe_entry =0x210 ; - startup_64 =0x200 ;#endif }
@@ -338,7 +336,6 @@ static void parse_zoffset(char *fname)PARSE_ZOFS (p, efi64_stub_entry);PARSE_ZOFS (p, efi_pe_entry);PARSE_ZOFS (p, efi32_pe_entry); -PARSE_ZOFS (p, startup_64);PARSE_ZOFS (p, _end); p =strchr (p,'\n' );
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 9/17 x86/boot: Set EFI handover offset directly in header asm
In reply to Ard BiesheuvelThe offsets of the EFI handover entrypoints are available to the assembler when constructing the header, so there is no need to set them from the build tool afterwards.
--- arch/x86/boot/header.S | 18 ++++++++++++++- arch/x86/boot/tools/build.c | 24 -------------------- 2 files changed, 17 insertions(+), 25 deletions(-)
arch/x86/boot/header.S +17 -1
@@ -524,8 +524,24 @@ pref_address: .quad LOAD_PHYSICAL_ADDR # preferred load addr # define INIT_SIZE VO_INIT_SIZE #endif + .macro __handover_offset +#ifndef CONFIG_EFI_HANDOVER_PROTOCOL + .long 0 +#elif !defined(CONFIG_X86_64) + .long ZO_efi32_stub_entry +#else + /* Yes, this is really how we defined it :( */ + .long ZO_efi64_stub_entry - 0x200 +#ifdef CONFIG_EFI_MIXED + .if ZO_efi32_stub_entry != ZO_efi64_stub_entry - 0x200 + .error "32-bit and 64-bit EFI entry points do not match" + .endif +#endif +#endif + .endm + init_size: .long INIT_SIZE # kernel initialization size -handover_offset: .long 0 # Filled in by build.c +handover_offset: __handover_offset kernel_info_offset: .long ZO_kernel_info # End of setup header #####################################################
arch/x86/boot/tools/build.c +0 -24
@@ -55,8 +55,6 @@ u8 buf[SETUP_SECT_MAX*512];#define PECOFF_COMPAT_RESERVE 0x0#endif -static unsigned long efi32_stub_entry; -static unsigned long efi64_stub_entry;static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry;static unsigned long _end;
@@ -265,31 +263,12 @@ static void efi_stub_defaults(void)#endif } -static void efi_stub_entry_update (void ) -{ -unsigned long addr = efi32_stub_entry; - -#ifdef CONFIG_EFI_HANDOVER_PROTOCOL -#ifdef CONFIG_X86_64 -/* Yes, this is really how we defined it :( */ - addr = efi64_stub_entry -0x200 ; -#endif - -#ifdef CONFIG_EFI_MIXED -if (efi32_stub_entry != addr) -die ("32-bit and 64-bit EFI entry points do not match\n" ); -#endif -#endif -put_unaligned_le32 (addr, &buf[0x264 ]); -} -#else static inline void update_pecoff_setup_and_reloc (unsigned int size) {}static inline void update_pecoff_text (unsigned int text_start,unsigned int file_sz) {}static inline void efi_stub_defaults (void ) {} -static inline void efi_stub_entry_update (void ) {}static inline int reserve_pecoff_reloc_section (int c) {
@@ -332,8 +311,6 @@ static void parse_zoffset(char *fname) p = (char *)buf;while (p && *p) { -PARSE_ZOFS (p, efi32_stub_entry); -PARSE_ZOFS (p, efi64_stub_entry);PARSE_ZOFS (p, efi_pe_entry);PARSE_ZOFS (p, efi32_pe_entry);PARSE_ZOFS (p, _end);
@@ -416,7 +393,6 @@ int main(int argc, char ** argv)update_pecoff_text (setup_sectors *512 , i + (sys_size *16 )); -efi_stub_entry_update (); crc =partial_crc32 (buf, i, crc);if (fwrite (buf,1 , i, dest) != i)
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 10/17 x86/boot: Drop workaround for binutils 2.14 in linker script ASSERTs
In reply to Ard BiesheuvelThe minimum binutils version was bumped to 2.20 in 2017 so there is no longer a need to work around quirks in older versions than that. So drop some meaningless linker script assignments to '.' of ASSERT() return values.
--- arch/x86/boot/setup.ld | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
arch/x86/boot/setup.ld +3 -7
@@ -56,12 +56,8 @@ SECTIONS *(.note*) } - /* - * The ASSERT() sink to . is intentional, for binutils 2.14 compatibility: - */ - . = ASSERT(_end <= 0x8000, "Setup too big!"); - . = ASSERT(hdr == 0x1f1, "The setup header has the wrong offset!"); + ASSERT(_end <= 0x8000, "Setup too big!") + ASSERT(hdr == 0x1f1, "The setup header has the wrong offset!") /* Necessary for the very-old-loader check to work... */ - . = ASSERT(__end_init <= 5*512, "init sections too big!"); - + ASSERT(__end_init <= 5*512, "init sections too big!") }
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 11/17 x86/boot: Use fixed size of 16k for setup block
In reply to Ard BiesheuvelThe setup block contains the real mode startup code that is used when booting from a legacy BIOS, along with the boot_params/setup_data that is used by legacy x86 bootloaders to pass the command line and initial ramdisk parameters, among other things.
The setup block also contains the PE/COFF header of the entire combined image, which includes the compressed kernel image, the decompressor and the EFI stub.
This PE header describes the layout of the executable image in memory, and currently, the fact that the setup block precedes it makes it rather fiddly to get the right values into the right place in the final image.
One complicating factor here is the variable setup block size, and given that we will need to round up the setup block size to page size anyway in a subsequent patch (in order to be able to use different permissions for .text and .data), let's round it up to a fixed size of 16 KiB and be done with it.
Note that Clang does not optimize for size as aggressively as GCC when using the -Os option, but it supports -Oz for this purpose, so pass that if the compiler supports it.
--- arch/x86/boot/Makefile | 1 + arch/x86/boot/header.S | 6 +++++- arch/x86/boot/setup.ld | 1 + arch/x86/boot/tools/build.c | 12 +++++------- 4 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/boot/Makefile +1 -0
@@ -69,6 +69,7 @@ KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables +KBUILD_CFLAGS += $(call cc-option,-Oz) GCOV_PROFILE := n UBSAN_SANITIZE := n
arch/x86/boot/header.S +5 -1
@@ -36,6 +36,10 @@ SYSSEG = 0x1000 /* historical load address >> 4 */ #define ROOT_RDONLY 1 #endif + /* The setup block has a fixed size: 32 * 512 == 16k */ + .globl setup_size + .set setup_size, 0x4000 + .code16 .section ".bstext", "ax" #ifdef CONFIG_EFI_STUB
@@ -231,7 +235,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ .globl hdr hdr: -setup_sects: .byte 0 /* Filled in by build.c */ +setup_sects: .byte (setup_size / 512) - 1 root_flags: .word ROOT_RDONLY syssize: .long 0 /* Filled in by build.c */ ram_size: .word 0 /* Obsolete */
arch/x86/boot/setup.ld +1 -0
@@ -57,6 +57,7 @@ SECTIONS } ASSERT(_end <= 0x8000, "Setup too big!") + ASSERT(__bss_start <= setup_size, "Setup image size too big!") ASSERT(hdr == 0x1f1, "The setup header has the wrong offset!") /* Necessary for the very-old-loader check to work... */ ASSERT(__end_init <= 5*512, "init sections too big!")
arch/x86/boot/tools/build.c +5 -7
@@ -40,12 +40,10 @@ typedef unsigned char u8;typedef unsigned short u16 ;typedef unsigned int u32 ; -/* Minimal number of setup sectors */ -#define SETUP_SECT_MIN 5 -#define SETUP_SECT_MAX 64 +#define SETUP_SECT_NUM 32/* This must be large enough to hold the entire setup */ -u8 buf[SETUP_SECT_MAX *512 ]; +u8 buf[(SETUP_SECT_NUM +1 )*512 ];#define PECOFF_RELOC_RESERVE 0x20
@@ -360,8 +358,9 @@ int main(int argc, char ** argv)/* Pad unused space with zeros */ setup_sectors = (c +511 ) /512 ; -if (setup_sectors <SETUP_SECT_MIN ) - setup_sectors =SETUP_SECT_MIN ; +if (setup_sectors >SETUP_SECT_NUM ) +die ("setup size exceeds maximum" ); + setup_sectors =SETUP_SECT_NUM ; i = setup_sectors*512 ;memset (buf+c,0 , i-c);
@@ -388,7 +387,6 @@ int main(int argc, char ** argv)#endif /* Patch the setup code with the appropriate size parameters */ - buf[0x1f1 ] = setup_sectors-1 ;put_unaligned_le32 (sys_size, &buf[0x1f4 ]);update_pecoff_text (setup_sectors *512 , i + (sys_size *16 ));
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 12/17 x86/boot: Derive file size from _edata symbol
In reply to Ard BiesheuvelTweak the linker script so that the value of _edata represents the decompressor binary's file size rounded up to the appropriate alignment. This removes the need to calculate it in the build tool, and will make it easier to refer to the file size from the header directly in subsequent changes to the PE header layout.
While adding _edata to the sed regex that parses the compressed vmlinux's symbol list, tweak the regex a bit for conciseness.
Note that the resulting binary is identical (for CONFIG_EFI_STUB=y builds)
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/vmlinux.lds.S | 3 ++ arch/x86/boot/header.S | 2 +- arch/x86/boot/tools/build.c | 30 +++++--------------- 4 files changed, 12 insertions(+), 25 deletions(-)
arch/x86/boot/Makefile +1 -1
@@ -90,7 +90,7 @@ $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE SETUP_OBJS = $(addprefix $(obj)/,$(setup-y)) -sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi32_stub_entry\|efi64_stub_entry\|efi_pe_entry\|efi32_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|z_.*\)$$/\#define ZO_\2 0x\1/p' +sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_edata\|z_.*\)$$/\#define ZO_\2 0x\1/p' quiet_cmd_zoffset = ZOFFSET $@ cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@
arch/x86/boot/compressed/vmlinux.lds.S +3 -0
@@ -47,6 +47,9 @@ SECTIONS _data = . ; *(.data) *(.data.*) + + /* add 4 bytes of extra space for a CRC-32 checksum */ + . = ALIGN(. + 4, 0x20); _edata = . ; } . = ALIGN(L1_CACHE_BYTES);
arch/x86/boot/header.S +1 -1
@@ -237,7 +237,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ hdr: setup_sects: .byte (setup_size / 512) - 1 root_flags: .word ROOT_RDONLY -syssize: .long 0 /* Filled in by build.c */ +syssize: .long ZO__edata / 16 ram_size: .word 0 /* Obsolete */ vid_mode: .word SVGA_MODE root_dev: .word 0 /* Default to major/minor 0/0 */
arch/x86/boot/tools/build.c +7 -23
@@ -55,6 +55,7 @@ u8 buf[(SETUP_SECT_NUM+1)*512];static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry; +static unsigned long _edata;static unsigned long _end;/*----------------------------------------------------------------------*/
@@ -311,6 +312,7 @@ static void parse_zoffset(char *fname)while (p && *p) {PARSE_ZOFS (p, efi_pe_entry);PARSE_ZOFS (p, efi32_pe_entry); +PARSE_ZOFS (p, _edata);PARSE_ZOFS (p, _end); p =strchr (p,'\n' );
@@ -323,7 +325,6 @@ int main(int argc, char ** argv) {unsigned int i, sz, setup_sectors;int c; -u32 sys_size;struct stat sb;FILE *file, *dest;int fd;
@@ -372,24 +373,14 @@ int main(int argc, char ** argv)die ("Unable to open `%s': %m" , argv[2 ]);if (fstat (fd, &sb))die ("Unable to stat `%s': %m" , argv[2 ]); - sz = sb.st_size; +if (_edata != sb.st_size) +die ("Unexpected file size `%s': %u != %u" , argv[2 ], _edata, + sb.st_size); + sz = _edata -4 ; kernel =mmap (NULL , sz,PROT_READ ,MAP_SHARED , fd,0 );if (kernel ==MAP_FAILED )die ("Unable to mmap '%s': %m" , argv[2 ]); -/* Number of 16-byte paragraphs, including space for a 4-byte CRC */ - sys_size = (sz +15 +4 ) /16 ; -#ifdef CONFIG_EFI_STUB -/* -* COFF requires minimum 32-byte alignment of sections, and -* adding a signature is problematic without that alignment. -*/ - sys_size = (sys_size +1 ) & ~1 ; -#endif - -/* Patch the setup code with the appropriate size parameters */ -put_unaligned_le32 (sys_size, &buf[0x1f4 ]); - -update_pecoff_text (setup_sectors *512 , i + (sys_size *16 )); +update_pecoff_text (setup_sectors *512 , i + _edata); crc =partial_crc32 (buf, i, crc);
@@ -401,13 +392,6 @@ int main(int argc, char ** argv)if (fwrite (kernel,1 , sz, dest) != sz)die ("Writing kernel failed" ); -/* Add padding leaving 4 bytes for the checksum */ -while (sz++ < (sys_size*16 ) -4 ) { - crc =partial_crc32_one ('\0' , crc); -if (fwrite ("\0" ,1 ,1 , dest) !=1 ) -die ("Writing padding failed" ); - } -/* Write the CRC */ put_unaligned_le32 (crc, buf);if (fwrite (buf,1 ,4 , dest) !=4 )
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 13/17 x86/boot: Construct PE/COFF .text section from assembler
In reply to Ard BiesheuvelNow that the size of the setup block is fixed and visible to the assembler, it is possible to populate the PE/COFF header fields from the asm code directly, instead of poking the values into the binary using the build tool. This will make it easier to reorganize the section layout without having to tweak the build tool in lockstep.
Note that this change results in no differences in the resulting bzImage binary.
--- arch/x86/boot/header.S | 22 +++------ arch/x86/boot/tools/build.c | 47 -------------------- 2 files changed, 7 insertions(+), 62 deletions(-)
arch/x86/boot/header.S +7 -15
@@ -79,14 +79,12 @@ optional_header: .byte 0x02 # MajorLinkerVersion .byte 0x14 # MinorLinkerVersion - # Filled in by build.c - .long 0 # SizeOfCode + .long setup_size + ZO__end - 0x200 # SizeOfCode .long 0 # SizeOfInitializedData .long 0 # SizeOfUninitializedData - # Filled in by build.c - .long 0x0000 # AddressOfEntryPoint + .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint .long 0x0200 # BaseOfCode #ifdef CONFIG_X86_32
@@ -109,10 +107,7 @@ extra_header_fields: .word 0 # MinorSubsystemVersion .long 0 # Win32VersionValue - # - # The size of the bzImage is written in tools/build.c - # - .long 0 # SizeOfImage + .long setup_size + ZO__end # SizeOfImage .long 0x200 # SizeOfHeaders .long 0 # CheckSum
@@ -203,18 +198,15 @@ section_table: IMAGE_SCN_MEM_DISCARDABLE # Characteristics #endif - # - # The offset & size fields are filled in by build.c. - # .ascii ".text" .byte 0 .byte 0 .byte 0 - .long 0 - .long 0x0 # startup_{32,64} - .long 0 # Size of initialized data + .long ZO__end + .long setup_size + .long ZO__edata # Size of initialized data # on disk - .long 0x0 # startup_{32,64} + .long setup_size .long 0 # PointerToRelocations .long 0 # PointerToLineNumbers .word 0 # NumberOfRelocations
arch/x86/boot/tools/build.c +0 -47
90 lines not on this page · raw message
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 14/17 x86/boot: Drop PE/COFF .reloc section
In reply to Ard BiesheuvelAncient buggy EFI loaders may have required a .reloc section to be present at some point in time, but this has not been true for a long time so the .reloc section can just be dropped.
--- arch/x86/boot/header.S | 20 ----------- arch/x86/boot/tools/build.c | 35 +++----------------- 2 files changed, 5 insertions(+), 50 deletions(-)
arch/x86/boot/header.S +0 -20
@@ -159,26 +159,6 @@ section_table: IMAGE_SCN_MEM_READ | \ IMAGE_SCN_MEM_EXECUTE # Characteristics - # - # The EFI application loader requires a relocation section - # because EFI applications must be relocatable. The .reloc - # offset & size fields are filled in by build.c. - # - .ascii ".reloc" - .byte 0 - .byte 0 - .long 0 - .long 0 - .long 0 # SizeOfRawData - .long 0 # PointerToRawData - .long 0 # PointerToRelocations - .long 0 # PointerToLineNumbers - .word 0 # NumberOfRelocations - .word 0 # NumberOfLineNumbers - .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ - IMAGE_SCN_MEM_READ | \ - IMAGE_SCN_MEM_DISCARDABLE # Characteristics - #ifdef CONFIG_EFI_MIXED # # The offset & size fields are filled in by build.c.
arch/x86/boot/tools/build.c +5 -30
@@ -45,8 +45,6 @@ typedef unsigned int u32;/* This must be large enough to hold the entire setup */ u8 buf[(SETUP_SECT_NUM +1 )*512 ]; -#define PECOFF_RELOC_RESERVE 0x20 -#ifdef CONFIG_EFI_MIXED #define PECOFF_COMPAT_RESERVE 0x20#else
@@ -183,24 +181,13 @@ static void update_pecoff_section_header(char *section_name, u32 offset, u32 sizupdate_pecoff_section_header_fields (section_name, offset, size, size, offset); } -static void update_pecoff_setup_and_reloc (unsigned int size) +static void update_pecoff_setup (unsigned int size) {u32 setup_offset =0x200 ; -u32 reloc_offset = size -PECOFF_RELOC_RESERVE -PECOFF_COMPAT_RESERVE ; -#ifdef CONFIG_EFI_MIXED -u32 compat_offset = reloc_offset +PECOFF_RELOC_RESERVE ; -#endif -u32 setup_size = reloc_offset - setup_offset; +u32 compat_offset = size -PECOFF_COMPAT_RESERVE ; +u32 setup_size = compat_offset - setup_offset;update_pecoff_section_header (".setup" , setup_offset, setup_size); -update_pecoff_section_header (".reloc" , reloc_offset,PECOFF_RELOC_RESERVE ); - -/* -* Modify .reloc section contents with a single entry. The -* relocation is applied to offset 10 of the relocation section. -*/ -put_unaligned_le32 (reloc_offset +10 , &buf[reloc_offset]); -put_unaligned_le32 (10 , &buf[reloc_offset +4 ]);#ifdef CONFIG_EFI_MIXED update_pecoff_section_header (".compat" , compat_offset,PECOFF_COMPAT_RESERVE );
@@ -217,21 +204,10 @@ static void update_pecoff_setup_and_reloc(unsigned int size) #endif } -static int reserve_pecoff_reloc_section (int c) -{ -/* Reserve 0x20 bytes for .reloc section */ -memset (buf+c,0 ,PECOFF_RELOC_RESERVE ); -return PECOFF_RELOC_RESERVE ; -} -#else -static inline void update_pecoff_setup_and_reloc (unsigned int size) {} +static inline void update_pecoff_setup (unsigned int size) {} -static inline int reserve_pecoff_reloc_section (int c) -{ -return 0 ; -}#endif /* CONFIG_EFI_STUB */ static int reserve_pecoff_compat_section (int c)
@@ -310,7 +286,6 @@ int main(int argc, char ** argv)fclose (file); c +=reserve_pecoff_compat_section (c); - c +=reserve_pecoff_reloc_section (c);/* Pad unused space with zeros */ setup_sectors = (c +511 ) /512 ;
@@ -320,7 +295,7 @@ int main(int argc, char ** argv) i = setup_sectors*512 ;memset (buf+c,0 , i-c); -update_pecoff_setup_and_reloc (i); +update_pecoff_setup (i);/* Open and stat the kernel file */ fd =open (argv[2 ],O_RDONLY );
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 15/17 x86/boot: Split off PE/COFF .data section
In reply to Ard BiesheuvelDescribe the code and data of the decompressor binary using separate .text and .data PE/COFF sections, so that we will be able to map them using restricted permissions once we increase the section and file alignment sufficiently. This avoids the need for memory mappings that are writable and executable at the same time, which is something that is best avoided for security reasons.
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/header.S | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-)
arch/x86/boot/Makefile +1 -1
@@ -90,7 +90,7 @@ $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE SETUP_OBJS = $(addprefix $(obj)/,$(setup-y)) -sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_edata\|z_.*\)$$/\#define ZO_\2 0x\1/p' +sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_e\?data\|z_.*\)$$/\#define ZO_\2 0x\1/p' quiet_cmd_zoffset = ZOFFSET $@ cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@
arch/x86/boot/header.S +15 -4
@@ -79,9 +79,9 @@ optional_header: .byte 0x02 # MajorLinkerVersion .byte 0x14 # MinorLinkerVersion - .long setup_size + ZO__end - 0x200 # SizeOfCode + .long ZO__data # SizeOfCode - .long 0 # SizeOfInitializedData + .long ZO__end - ZO__data # SizeOfInitializedData .long 0 # SizeOfUninitializedData .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint
@@ -182,9 +182,9 @@ section_table: .byte 0 .byte 0 .byte 0 - .long ZO__end + .long ZO__data .long setup_size - .long ZO__edata # Size of initialized data + .long ZO__data # Size of initialized data # on disk .long setup_size .long 0 # PointerToRelocations
@@ -195,6 +195,17 @@ section_table: IMAGE_SCN_MEM_READ | \ IMAGE_SCN_MEM_EXECUTE # Characteristics + .ascii ".data\0\0\0" + .long ZO__end - ZO__data # VirtualSize + .long setup_size + ZO__data # VirtualAddress + .long ZO__edata - ZO__data # SizeOfRawData + .long setup_size + ZO__data # PointerToRawData + + .long 0, 0, 0 + .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ + IMAGE_SCN_MEM_READ | \ + IMAGE_SCN_MEM_WRITE # Characteristics + .set section_count, (. - section_table) / 40 #endif /* CONFIG_EFI_STUB */
Signature · 1 line
2.39.2
Re: [PATCH 15/17] x86/boot: Split off PE/COFF .data section
Hi Ard,
Thanks for your effort! Not sure what the documentation policies are, but might it be worth mentioning that we cannot have .rdata at this time, because current-gen EFI will map it as RW?
Best regards,
Marvin
Quoted 75 lines · Ard Biesheuvel
Describe the code and data of the decompressor binary using separate .text and .data PE/COFF sections, so that we will be able to map them using restricted permissions once we increase the section and file alignment sufficiently. This avoids the need for memory mappings that are writable and executable at the same time, which is something that is best avoided for security reasons.
Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/boot/Makefile| 2 +-arch/x86/boot/header.S| 19 +++++++++++++++----2 files changed, 16 insertions(+), 5 deletions(-)arch/x86/boot/Makefile +1
-1@@ -90,7 +90,7 @@ $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE SETUP_OBJS = $(addprefix $(obj)/,$(setup-y)) -sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_edata\|z_.*\)$$/\#define ZO_\2 0x\1/p' +sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_e\?data\|z_.*\)$$/\#define ZO_\2 0x\1/p'
quiet_cmd_zoffset= ZOFFSET $@cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@arch/x86/boot/header.S +15
-4@@ -79,9 +79,9 @@ optional_header: .byte 0x02 # MajorLinkerVersion .byte 0x14 # MinorLinkerVersion - .long setup_size + ZO__end - 0x200 # SizeOfCode + .long ZO__data # SizeOfCode - .long 0 # SizeOfInitializedData + .long ZO__end - ZO__data # SizeOfInitializedData .long 0 # SizeOfUninitializedData .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint@@ -182,9 +182,9 @@ section_table: .byte 0 .byte 0 .byte 0 - .long ZO__end + .long ZO__data .long setup_size - .long ZO__edata # Size of initialized data + .long ZO__data # Size of initialized data # on disk .long setup_size .long 0 # PointerToRelocations@@ -195,6 +195,17 @@ section_table: IMAGE_SCN_MEM_READ | \ IMAGE_SCN_MEM_EXECUTE # Characteristics + .ascii ".data\0\0\0" + .long ZO__end - ZO__data # VirtualSize + .long setup_size + ZO__data # VirtualAddress + .long ZO__edata - ZO__data # SizeOfRawData + .long setup_size + ZO__data # PointerToRawData + + .long 0, 0, 0 + .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ + IMAGE_SCN_MEM_READ | \ + IMAGE_SCN_MEM_WRITE # Characteristics + .set section_count, (. - section_table) / 40 #endif /* CONFIG_EFI_STUB */Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerRe: [PATCH 15/17] x86/boot: Split off PE/COFF .data section
Quoted 5 lines · Marvin Häuser
Hi Ard,
Thanks for your effort! Not sure what the documentation policies are, but might it be worth mentioning that we cannot have .rdata at this time, because current-gen EFI will map it as RW?
Yeah I'll mention this in the next version.
Quoted 79 lines
Best regards,
MarvinQuoted 75 lines · Ard Biesheuvel
Describe the code and data of the decompressor binary using separate .text and .data PE/COFF sections, so that we will be able to map them using restricted permissions once we increase the section and file alignment sufficiently. This avoids the need for memory mappings that are writable and executable at the same time, which is something that is best avoided for security reasons.
Signed-off-by Ard Biesheuvel <[email protected]>---
arch/x86/boot/Makefile| 2 +-arch/x86/boot/header.S| 19 +++++++++++++++----2 files changed, 16 insertions(+), 5 deletions(-)arch/x86/boot/Makefile +1
-1@@ -90,7 +90,7 @@ $(obj)/vmlinux.bin: $(obj)/compressed/vmlinux FORCE SETUP_OBJS = $(addprefix $(obj)/,$(setup-y)) -sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_edata\|z_.*\)$$/\#define ZO_\2 0x\1/p' +sed-zoffset := -e 's/^\([0-9a-fA-F]*\) [a-zA-Z] \(startup_32\|efi.._stub_entry\|efi\(32\)\?_pe_entry\|input_data\|kernel_info\|_end\|_ehead\|_text\|_e\?data\|z_.*\)$$/\#define ZO_\2 0x\1/p'
quiet_cmd_zoffset= ZOFFSET $@cmd_zoffset = $(NM) $< | sed -n $(sed-zoffset) > $@arch/x86/boot/header.S +15
-5@@ -79,9 +79,9 @@ optional_header: .byte 0x02 # MajorLinkerVersion .byte 0x14 # MinorLinkerVersion - .long setup_size + ZO__end - 0x200 # SizeOfCode + .long ZO__data # SizeOfCode - .long 0 # SizeOfInitializedData + .long ZO__end - ZO__data # SizeOfInitializedData .long 0 # SizeOfUninitializedData .long setup_size + ZO_efi_pe_entry # AddressOfEntryPoint@@ -182,9 +182,9 @@ section_table: .byte 0 .byte 0 .byte 0 - .long ZO__end + .long ZO__data .long setup_size - .long ZO__edata # Size of initialized data + .long ZO__data # Size of initialized data # on disk .long setup_size .long 0 # PointerToRelocations@@ -195,6 +195,17 @@ section_table: IMAGE_SCN_MEM_READ | \ IMAGE_SCN_MEM_EXECUTE # Characteristics + .ascii ".data\0\0\0" + .long ZO__end - ZO__data # VirtualSize + .long setup_size + ZO__data # VirtualAddress + .long ZO__edata - ZO__data # SizeOfRawData + .long setup_size + ZO__data # PointerToRawData + + .long 0, 0, 0 + .long IMAGE_SCN_CNT_INITIALIZED_DATA | \ + IMAGE_SCN_MEM_READ | \ + IMAGE_SCN_MEM_WRITE # Characteristics + .set section_count, (. - section_table) / 40 #endif /* CONFIG_EFI_STUB */ --2.39.2
Ard Biesheuvel
maintainerPATCH 16/17 x86/boot: Increase section and file alignment to 4k/512
In reply to Ard BiesheuvelAlign x86 with other EFI architectures, and increase the section alignment to the EFI page size (4k), so that firmware is able to honour the section permission attributes and map code read-only and data non-executable.
There are a number of requirements that have to be taken into account:
- the sign tools get cranky when there are gaps between sections in the file view of the image
- the virtual offset of each section must be aligned to the image's section alignment
- the file offset *and size* of each section must be aligned to the image's file alignment
- the image size must be aligned to the section alignment
- each section's virtual offset must be greater than or equal to the size of the headers.
In order to meet all these requirements, while avoiding the need for lots of padding the accommodate the .pecompat section, the latter is placed at an arbitrary offset >= 4k in the image, but aligned to the minimum file alignment (512 bytes). The space before the .text section is therefore distributed between the PE header, the .setup section and the .pecompat section, leaving no gaps in the file coverage, making the signing tools happy.
The virtual placement of the .pecompat section is at the end of the image. Whether or not the data gets loaded there depends on how the PE loader interprets the EFI_IMAGE_SCN_MEM_DISCARDABLE section attribute, but this doesn't really matter as the contents are only relevant to mixed mode capable PE loaders anyway.
--- arch/x86/boot/Makefile | 1 + arch/x86/boot/compressed/vmlinux.lds.S | 4 +- arch/x86/boot/header.S | 81 +++++++++-------- arch/x86/boot/setup.ld | 3 +- arch/x86/boot/tools/build.c | 91 -------------------- 5 files changed, 51 insertions(+), 129 deletions(-)
arch/x86/boot/Makefile +1 -0
@@ -68,6 +68,7 @@ targets += cpustr.h KBUILD_CFLAGS := $(REALMODE_CFLAGS) -D_SETUP KBUILD_AFLAGS := $(KBUILD_CFLAGS) -D__ASSEMBLY__ KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=) +KBUILD_CFLAGS += $(call cc-option,-Oz) KBUILD_CFLAGS += -fno-asynchronous-unwind-tables KBUILD_CFLAGS += $(call cc-option,-Oz) GCOV_PROFILE := n
arch/x86/boot/compressed/vmlinux.lds.S +2 -2
@@ -43,13 +43,13 @@ SECTIONS *(.rodata.*) _erodata = . ; } - .data : { + .data : ALIGN(0x1000) { _data = . ; *(.data) *(.data.*) /* add 4 bytes of extra space for a CRC-32 checksum */ - . = ALIGN(. + 4, 0x20); + . = ALIGN(. + 4, 0x200); _edata = . ; } . = ALIGN(L1_CACHE_BYTES);
arch/x86/boot/header.S +46 -35
120 lines not on this page · raw message
arch/x86/boot/setup.ld +2 -1
@@ -25,7 +25,8 @@ SECTIONS .text32 : { *(.text32) } . = ALIGN(16); - .rodata : { *(.rodata*) } + .rodata : { *(.pecompat) *(.rodata*) } + PROVIDE(pecompat_fsize = setup_size - pecompat_fstart); .videocards : { video_cards = .;
arch/x86/boot/tools/build.c +0 -91
121 lines not on this page · raw message
Signature · 1 line
2.39.2
Ard Biesheuvel
maintainerPATCH 17/17 x86/boot: Drop CRC-32 checksum and the build tool that generates it
In reply to Ard BiesheuvelThe only remaining task carried out by the boot/tools/build.c build tool is generating the CRC-32 checksum of the bzImage. This feature was added in commit
7d6e737c8d2698b6 ("x86: add a crc32 checksum to the kernel image.")
without any motivation (or any commit log text, for that matter). This checksum is not verified by any known bootloader, and given that
a) the checksum of the entire bzImage is reported by most tools (zlib, rhash) as 0xffffffff and not 0x0 as documented,
b) the checksum is corrupted when the image is signed for secure boot, which means that no distro ships x86 images with valid CRCs,
it seems quite unlikely that this checksum is being used, so let's just drop it, along with the tool that generates it.
--- Documentation/arch/x86/boot.rst | 10 - arch/x86/boot/Makefile | 8 +- arch/x86/boot/compressed/vmlinux.lds.S | 3 +- arch/x86/boot/tools/.gitignore | 2 - arch/x86/boot/tools/build.c | 245 -------------------- 5 files changed, 4 insertions(+), 264 deletions(-)
Documentation/arch/x86/boot.rst +0 -10
@@ -1028,16 +1028,6 @@ Offset/size: 0x000c/4 This field contains maximal allowed type for setup_data and setup_indirect structs. -The Image Checksum -================== - -From boot protocol version 2.08 onwards the CRC-32 is calculated over -the entire file using the characteristic polynomial 0x04C11DB7 and an -initial remainder of 0xffffffff. The checksum is appended to the -file; therefore the CRC of the file up to the limit specified in the -syssize field of the header is always 0. - - The Kernel Command Line =======================
arch/x86/boot/Makefile +3 -5
@@ -48,7 +48,6 @@ setup-y += video-vesa.o setup-y += video-bios.o targets += $(setup-y) -hostprogs := tools/build hostprogs += mkcpustr HOST_EXTRACFLAGS += -I$(srctree)/tools/include \
@@ -77,11 +76,10 @@ UBSAN_SANITIZE := n $(obj)/bzImage: asflags-y := $(SVGA_MODE) quiet_cmd_image = BUILD $@ -silent_redirect_image = >/dev/null -cmd_image = $(obj)/tools/build $(obj)/setup.bin $(obj)/vmlinux.bin \ - $(obj)/zoffset.h $@ $($(quiet)redirect_image) + cmd_image = truncate -s %4K $(obj)/setup.bin; \ + cat $(obj)/setup.bin $(obj)/vmlinux.bin >$@ -$(obj)/bzImage: $(obj)/setup.bin $(obj)/vmlinux.bin $(obj)/tools/build FORCE +$(obj)/bzImage: $(obj)/setup.bin $(obj)/vmlinux.bin FORCE $(call if_changed,image) @$(kecho) 'Kernel: $@ is ready' ' (#'$(or $(KBUILD_BUILD_VERSION),`cat .version`)')'
arch/x86/boot/compressed/vmlinux.lds.S +1 -2
@@ -48,8 +48,7 @@ SECTIONS *(.data) *(.data.*) - /* add 4 bytes of extra space for a CRC-32 checksum */ - . = ALIGN(. + 4, 0x200); + . = ALIGN(0x200); _edata = . ; } . = ALIGN(L1_CACHE_BYTES);
arch/x86/boot/tools/.gitignore deleted +0 -2
@@ -1,2 +0,0 @@ -# SPDX-License-Identifier: GPL-2.0-only -build
arch/x86/boot/tools/build.c deleted +0 -245
245 lines not on this page · raw message
Signature · 1 line
2.39.2
H. Peter Anvin
reviewerRe: [PATCH 17/17] x86/boot: Drop CRC-32 checksum and the build tool that generates it
Quoted 9 lines · Ard Biesheuvel
The only remaining task carried out by the
boot/tools/build.cbuild tool is generating the CRC-32 checksum of the bzImage. This feature was added in commit
7d6e737c8d2698b6("x86: add a crc32 checksum to the kernel image.")without any motivation (or any commit log text, for that matter). This checksum is not verified by any known bootloader, and given that
Quoted 8 lines
a) the checksum of the entire bzImage is reported by most tools (zlib,
rhash) as 0xffffffff and not 0x0 as documented,b) the checksum is corrupted when the image is signed for secure boot, which means that no distro ships x86 images with valid CRCs,
it seems quite unlikely that this checksum is being used, so let's just drop it, along with the tool that generates it.
This one I have concerns with.
-hpa
Ard Biesheuvel
maintainerRe: [PATCH 17/17] x86/boot: Drop CRC-32 checksum and the build tool that generates it
Quoted 18 lines · H. Peter Anvin
Quoted 14 lines · Ard Biesheuvel
The only remaining task carried out by the
boot/tools/build.cbuild tool is generating the CRC-32 checksum of the bzImage. This feature was added in commit
7d6e737c8d2698b6("x86: add a crc32 checksum to the kernel image.")without any motivation (or any commit log text, for that matter). This checksum is not verified by any known bootloader, and given that
a) the checksum of the entire bzImage is reported by most tools (zlib,
rhash) as 0xffffffff and not 0x0 as documented,b) the checksum is corrupted when the image is signed for secure boot, which means that no distro ships x86 images with valid CRCs,
Quoted 6 lines
Quoted 3 lines
it seems quite unlikely that this checksum is being used, so let's just drop it, along with the tool that generates it.
This one I have concerns with.
I understand. I deliberately put this change at the very end because I was anticipating some debate on this.
Do you have any recollection of why this CRC32 was introduced in the first place? The commit logs are empty and the lore thread doesn't contain any justification either.
H. Peter Anvin
reviewerRe: [PATCH 17/17] x86/boot: Drop CRC-32 checksum and the build tool that generates it
Quoted 8 lines · Ard Biesheuvel
I understand. I deliberately put this change at the very end because I was anticipating some debate on this.
Do you have any recollection of why this CRC32 was introduced in the first place? The commit logs are empty and the lore thread doesn't contain any justification either.
The justification is that firmware is notoriously unreliable and gives the boot loader an independent way to verify the load and have a fallback, rather than jumping to the kernel and having the decompressor fail.
At this time it is impossible to know what might rely on it. The EFI signing issue aside, there are a ton of Linux bootloaders for non-EFI systems using the BIOS or raw kernel entry points - and there is no telling what those environments might do.
-hpa
Ard Biesheuvel
maintainerRe: [PATCH 17/17] x86/boot: Drop CRC-32 checksum and the build tool that generates it
Quoted 16 lines · H. Peter Anvin
Quoted 8 lines · Ard Biesheuvel
I understand. I deliberately put this change at the very end because I was anticipating some debate on this.
Do you have any recollection of why this CRC32 was introduced in the first place? The commit logs are empty and the lore thread doesn't contain any justification either.
The justification is that firmware is notoriously unreliable and gives the boot loader an independent way to verify the load and have a fallback, rather than jumping to the kernel and having the decompressor fail.
Quoted 5 lines
At this time it is impossible to know what might rely on it. The EFI signing issue aside, there are a ton of Linux bootloaders for non-EFI systems using the BIOS or raw kernel entry points - and there is no telling what those environments might do.
Fair enough.
The PE header happens to have a checksum field that a) is not used by EFI at all, and b) is not covered by the signature. So it wouldn't be too hard to put a doctored value in that field that forces the CRC-32 of the entire image to 0xffff_ffff *after* signing, and this would even work if the image did not have a CRC-32 at the end in the first place. (So the signing tools wouldn't need to know whether they are signing a x86 bzImage or some other PE executable). Note that pesign and sbsign currently follow the PE/windows spec for generating this checksum, but EFI never looks at it. (It is documented as being intended for checksumming critical system DLLs on Windows)
But that is a lot of trouble for no known use case, so let's not bother with that right now. But I'll withdraw this patch from the series.
Thanks,
Ard.