x86/boot: Rework PE header generation
v3 cover x86/boot: Rework PE header generation
From: Ard Biesheuvel <[email protected]>
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 a lot 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.
After this series is applied, the only remaining task performed by the build tool is generating the CRC-32. Even though this checksum is usually wrong (given that distro kernels are signed for secure boot in a way that corrupts the CRC), this feature is retained as we cannot be sure that nobody is relying on this.
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.
Changes since v2:
- rebase onto tip/master
- drop patches that have been picked up already
- fix issue in the linker script that resulted in a bogus setup_size in some cases when using ld.bfd
- fix comment capitalization
Changes since v1:
- drop patch that removed the CRC and the build tool
- do not use fixed setup_size but derive it in the setup.ld linker script
- reorganize the PE header so the .compat section only covers its payload and the padding that follows it
- add hpa's ack to patch #4
Ard Biesheuvel (8):
x86/boot: Grab kernel_info offset from zoffset header directly x86/boot: Set EFI handover offset directly in header asm x86/boot: Define setup size in linker script 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
arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/vmlinux.lds.S | 5 +- arch/x86/boot/header.S | 146 +++++++------ arch/x86/boot/setup.ld | 7 +- arch/x86/boot/tools/build.c | 223 +------------------- 5 files changed, 97 insertions(+), 286 deletions(-)
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
PATCH v3 1/8 x86/boot: Grab kernel_info offset from zoffset header directly
From: Ard Biesheuvel <[email protected]>
Instead 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.
This change has no impact on the resulting bzImage binary.
--- 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 _end;/*----------------------------------------------------------------------*/
@@ -337,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, kernel_info);PARSE_ZOFS (p, _end); p =strchr (p,'\n' );
@@ -419,8 +417,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Grab kernel_info offset from zoffset header directly
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: 2e765c02dcbfc2a8a4527c621a84b9502f6b9bd2 Gitweb: https://git.kernel.org/tip/2e765c02dcbfc2a8a4527c621a84b9502f6b9bd2 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:25 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:42 +02:00
x86/boot: Grab kernel_info offset from zoffset header directly
Instead 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.
This change has no impact on the resulting bzImage binary.
--- 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 _end;/*----------------------------------------------------------------------*/
@@ -337,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, kernel_info);PARSE_ZOFS (p, _end); p =strchr (p,'\n' );
@@ -419,8 +417,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)
PATCH v3 2/8 x86/boot: Set EFI handover offset directly in header asm
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The 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.
This change has no impact on the resulting bzImage binary.
--- 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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Set EFI handover offset directly in header asm
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: eac956345f99dda3d68f4ae6cf7b494105e54780 Gitweb: https://git.kernel.org/tip/eac956345f99dda3d68f4ae6cf7b494105e54780 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:26 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:42 +02:00
x86/boot: Set EFI handover offset directly in header asm
The 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.
This change has no impact on the resulting bzImage binary.
--- 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)
PATCH v3 3/8 x86/boot: Define setup size in linker script
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
The 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.
Let's make things a bit easier by defining the setup_size in the linker script so it can be referenced from the asm code directly, rather than having to rely on the build tool to calculate it. For the time being, add 64 bytes of fixed padding for the .reloc and .compat sections - this will be removed in a subsequent patch after the PE/COFF header has been reorganized.
This change has no impact on the resulting bzImage binary when configured with CONFIG_EFI_MIXED=y.
--- arch/x86/boot/header.S | 2 +- arch/x86/boot/setup.ld | 4 ++++ arch/x86/boot/tools/build.c | 6 ------ 3 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/boot/header.S +1 -1
@@ -231,7 +231,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ .globl hdr hdr: -setup_sects: .byte 0 /* Filled in by build.c */ + .byte setup_sects - 1 root_flags: .word ROOT_RDONLY syssize: .long 0 /* Filled in by build.c */ ram_size: .word 0 /* Obsolete */
arch/x86/boot/setup.ld +4 -0
@@ -39,6 +39,10 @@ SECTIONS .signature : { setup_sig = .; LONG(0x5a5aaa55) + + /* Reserve some extra space for the reloc and compat sections */ + setup_size = ALIGN(ABSOLUTE(.) + 64, 512); + setup_sects = ABSOLUTE(setup_size / 512); }
arch/x86/boot/tools/build.c +0 -6
@@ -48,12 +48,7 @@ typedef unsigned int u32;u8 buf[SETUP_SECT_MAX *512 ];#define PECOFF_RELOC_RESERVE 0x20 - -#ifdef CONFIG_EFI_MIXED #define PECOFF_COMPAT_RESERVE 0x20 -#else -#define PECOFF_COMPAT_RESERVE 0x0 -#endif static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry;
@@ -388,7 +383,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Define setup size in linker script
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: 093ab258e3fb1d1d3afdfd4a69403d44ce90e360 Gitweb: https://git.kernel.org/tip/093ab258e3fb1d1d3afdfd4a69403d44ce90e360 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:27 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:42 +02:00
x86/boot: Define setup size in linker script
The 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.
Let's make things a bit easier by defining the setup_size in the linker script so it can be referenced from the asm code directly, rather than having to rely on the build tool to calculate it. For the time being, add 64 bytes of fixed padding for the .reloc and .compat sections - this will be removed in a subsequent patch after the PE/COFF header has been reorganized.
This change has no impact on the resulting bzImage binary when configured with CONFIG_EFI_MIXED=y.
--- arch/x86/boot/header.S | 2 +- arch/x86/boot/setup.ld | 4 ++++ arch/x86/boot/tools/build.c | 6 ------ 3 files changed, 5 insertions(+), 7 deletions(-)
arch/x86/boot/header.S +1 -1
@@ -231,7 +231,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ .globl hdr hdr: -setup_sects: .byte 0 /* Filled in by build.c */ + .byte setup_sects - 1 root_flags: .word ROOT_RDONLY syssize: .long 0 /* Filled in by build.c */ ram_size: .word 0 /* Obsolete */
arch/x86/boot/setup.ld +4 -0
@@ -39,6 +39,10 @@ SECTIONS .signature : { setup_sig = .; LONG(0x5a5aaa55) + + /* Reserve some extra space for the reloc and compat sections */ + setup_size = ALIGN(ABSOLUTE(.) + 64, 512); + setup_sects = ABSOLUTE(setup_size / 512); }
arch/x86/boot/tools/build.c +0 -6
@@ -48,12 +48,7 @@ typedef unsigned int u32;u8 buf[SETUP_SECT_MAX *512 ];#define PECOFF_RELOC_RESERVE 0x20 - -#ifdef CONFIG_EFI_MIXED #define PECOFF_COMPAT_RESERVE 0x20 -#else -#define PECOFF_COMPAT_RESERVE 0x0 -#endif static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry;
@@ -388,7 +383,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 ));
PATCH v3 4/8 x86/boot: Derive file size from _edata symbol
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Tweak 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.
This change has no impact on the resulting bzImage binary when configured with CONFIG_EFI_STUB=y.
--- 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
@@ -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\|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
@@ -233,7 +233,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ hdr: .byte setup_sects - 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
@@ -52,6 +52,7 @@ u8 buf[SETUP_SECT_MAX*512];static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry; +static unsigned long _edata;static unsigned long _end;/*----------------------------------------------------------------------*/
@@ -308,6 +309,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' );
@@ -320,7 +322,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;
@@ -368,24 +369,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);
@@ -397,13 +388,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Derive file size from _edata symbol
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: aeb92067f6ae994b541d7f9752fe54ed3d108bcc Gitweb: https://git.kernel.org/tip/aeb92067f6ae994b541d7f9752fe54ed3d108bcc Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:28 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:42 +02:00
x86/boot: Derive file size from _edata symbol
Tweak 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.
This change has no impact on the resulting bzImage binary when configured with CONFIG_EFI_STUB=y.
--- 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
@@ -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\|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
@@ -233,7 +233,7 @@ sentinel: .byte 0xff, 0xff /* Used to detect broken loaders */ hdr: .byte setup_sects - 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
@@ -52,6 +52,7 @@ u8 buf[SETUP_SECT_MAX*512];static unsigned long efi_pe_entry;static unsigned long efi32_pe_entry; +static unsigned long _edata;static unsigned long _end;/*----------------------------------------------------------------------*/
@@ -308,6 +309,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' );
@@ -320,7 +322,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;
@@ -368,24 +369,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);
@@ -397,13 +388,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 )
PATCH v3 5/8 x86/boot: Construct PE/COFF .text section from assembler
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Now that the size of the setup block is 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.
This change has no impact on 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
@@ -75,14 +75,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
@@ -105,10 +103,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
@@ -199,18 +194,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Construct PE/COFF .text section from assembler
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: efa089e63b56bdc5eca754b995cb039dd7a5457e Gitweb: https://git.kernel.org/tip/efa089e63b56bdc5eca754b995cb039dd7a5457e Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:29 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:43 +02:00
x86/boot: Construct PE/COFF .text section from assembler
Now that the size of the setup block is 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.
This change has no impact on 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
@@ -75,14 +75,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
@@ -105,10 +103,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
@@ -199,18 +194,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
PATCH v3 6/8 x86/boot: Drop PE/COFF .reloc section
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Ancient 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/setup.ld | 4 +-- arch/x86/boot/tools/build.c | 34 +++----------------- 3 files changed, 7 insertions(+), 51 deletions(-)
arch/x86/boot/header.S +0 -20
@@ -155,26 +155,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/setup.ld +2 -2
@@ -40,8 +40,8 @@ SECTIONS setup_sig = .; LONG(0x5a5aaa55) - /* Reserve some extra space for the reloc and compat sections */ - setup_size = ALIGN(ABSOLUTE(.) + 64, 512); + /* Reserve some extra space for the compat section */ + setup_size = ALIGN(ABSOLUTE(.) + 32, 512); setup_sects = ABSOLUTE(setup_size / 512); }
arch/x86/boot/tools/build.c +5 -29
@@ -47,7 +47,6 @@ typedef unsigned int u32;/* This must be large enough to hold the entire setup */ u8 buf[SETUP_SECT_MAX *512 ]; -#define PECOFF_RELOC_RESERVE 0x20#define PECOFF_COMPAT_RESERVE 0x20static unsigned long efi32_pe_entry;
@@ -180,24 +179,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 );
@@ -214,21 +202,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)
@@ -307,7 +284,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 ;
@@ -316,7 +292,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Drop PE/COFF .reloc section
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: fa5750521e0a4efbc1af05223da9c4bbd6c21c83 Gitweb: https://git.kernel.org/tip/fa5750521e0a4efbc1af05223da9c4bbd6c21c83 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:30 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:43 +02:00
x86/boot: Drop PE/COFF .reloc section
Ancient 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/setup.ld | 4 ++-- arch/x86/boot/tools/build.c | 34 +++++----------------------------- 3 files changed, 7 insertions(+), 51 deletions(-)
arch/x86/boot/header.S +0 -20
@@ -155,26 +155,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/setup.ld +2 -2
@@ -40,8 +40,8 @@ SECTIONS setup_sig = .; LONG(0x5a5aaa55) - /* Reserve some extra space for the reloc and compat sections */ - setup_size = ALIGN(ABSOLUTE(.) + 64, 512); + /* Reserve some extra space for the compat section */ + setup_size = ALIGN(ABSOLUTE(.) + 32, 512); setup_sects = ABSOLUTE(setup_size / 512); }
arch/x86/boot/tools/build.c +5 -29
@@ -47,7 +47,6 @@ typedef unsigned int u32;/* This must be large enough to hold the entire setup */ u8 buf[SETUP_SECT_MAX *512 ]; -#define PECOFF_RELOC_RESERVE 0x20#define PECOFF_COMPAT_RESERVE 0x20static unsigned long efi32_pe_entry;
@@ -180,24 +179,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 );
@@ -214,21 +202,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)
@@ -307,7 +284,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 ;
@@ -316,7 +292,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 );
PATCH v3 7/8 x86/boot: Split off PE/COFF .data section
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
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.
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/header.S | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 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\|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
@@ -75,9 +75,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
@@ -178,9 +178,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
@@ -191,6 +191,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.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Split off PE/COFF .data section
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: 34951f3c28bdf6481d949a20413b2ce7693687b2 Gitweb: https://git.kernel.org/tip/34951f3c28bdf6481d949a20413b2ce7693687b2 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:31 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:43 +02:00
x86/boot: Split off PE/COFF .data section
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.
--- arch/x86/boot/Makefile | 2 +- arch/x86/boot/header.S | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 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\|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
@@ -75,9 +75,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
@@ -178,9 +178,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
@@ -191,6 +191,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 */
PATCH v3 8/8 x86/boot: Increase section and file alignment to 4k/512
In reply to Ard BiesheuvelFrom: Ard Biesheuvel <[email protected]>
Align 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 to accommodate the .compat section, the latter is placed at an arbitrary offset towards the end of 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 .compat section, leaving no gaps in the file coverage, making the signing tools happy.
--- arch/x86/boot/compressed/vmlinux.lds.S | 4 +- arch/x86/boot/header.S | 75 +++++++++------- arch/x86/boot/setup.ld | 7 +- arch/x86/boot/tools/build.c | 90 +------------------- 4 files changed, 51 insertions(+), 125 deletions(-)
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 +43 -32
117 lines not on this page · raw message
arch/x86/boot/setup.ld +4 -3
@@ -36,16 +36,17 @@ SECTIONS . = ALIGN(16); .data : { *(.data*) } + .pecompat : { *(.pecompat) } + PROVIDE(pecompat_fsize = setup_size - pecompat_fstart); + .signature : { setup_sig = .; LONG(0x5a5aaa55) - /* Reserve some extra space for the compat section */ - setup_size = ALIGN(ABSOLUTE(.) + 32, 512); + setup_size = ALIGN(ABSOLUTE(.), 4096); setup_sects = ABSOLUTE(setup_size / 512); } - . = ALIGN(16); .bss : {
arch/x86/boot/tools/build.c +2 -88
120 lines not on this page · raw message
Signature · 1 line
2.42.0.459.ge4e396fd5e-goog
[tip: x86/boot] x86/boot: Increase section and file alignment to 4k/512
The following commit has been merged into the x86/boot branch of tip:
The following commit has been merged into the x86/boot branch of tip:
Commit-ID: 3e3eabe26dc88692d34cf76ca0e0dd331481cc15 Gitweb: https://git.kernel.org/tip/3e3eabe26dc88692d34cf76ca0e0dd331481cc15 Author: Ard Biesheuvel <[email protected]> AuthorDate: Fri, 15 Sep 2023 17:16:32 Committer: Ingo Molnar <[email protected]> CommitterDate: Sun, 17 Sep 2023 19:48:44 +02:00
x86/boot: Increase section and file alignment to 4k/512
Align 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 to accommodate the .compat section, the latter is placed at an arbitrary offset towards the end of 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 .compat section, leaving no gaps in the file coverage, making the signing tools happy.
--- arch/x86/boot/compressed/vmlinux.lds.S | 4 +- arch/x86/boot/header.S | 75 ++++++++++++--------- arch/x86/boot/setup.ld | 7 +- arch/x86/boot/tools/build.c | 90 +------------------------- 4 files changed, 51 insertions(+), 125 deletions(-)
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 +43 -32
117 lines not on this page · raw message
arch/x86/boot/setup.ld +4 -3
@@ -36,16 +36,17 @@ SECTIONS . = ALIGN(16); .data : { *(.data*) } + .pecompat : { *(.pecompat) } + PROVIDE(pecompat_fsize = setup_size - pecompat_fstart); + .signature : { setup_sig = .; LONG(0x5a5aaa55) - /* Reserve some extra space for the compat section */ - setup_size = ALIGN(ABSOLUTE(.) + 32, 512); + setup_size = ALIGN(ABSOLUTE(.), 4096); setup_sects = ABSOLUTE(setup_size / 512); } - . = ALIGN(16); .bss : {
arch/x86/boot/tools/build.c +2 -88
120 lines not on this page · raw message
Quoted 69 lines · Ard Biesheuvel
From: Ard Biesheuvel <[email protected]>
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 a lot 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.
After this series is applied, the only remaining task performed by the build tool is generating the CRC-32. Even though this checksum is usually wrong (given that distro kernels are signed for secure boot in a way that corrupts the CRC), this feature is retained as we cannot be sure that nobody is relying on this.
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.
Changes since v2:
- rebase onto tip/master
- drop patches that have been picked up already
- fix issue in the linker script that resulted in a bogussetup_sizein some cases when using ld.bfd
- fix comment capitalizationChanges since v1:
- drop patch that removed the CRC and the build tool
- do not use fixedsetup_sizebut derive it in the setup.ld linker script
- reorganize the PE header so the .compat section only covers its payload and the padding that follows it
- add hpa's ack to patch #4Cc Evgeniy Baskov <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc Ingo Molnar <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Peter Jones <[email protected]>Cc Matthew Garrett <[email protected]>Cc Gerd Hoffmann <[email protected]>Cc Kees Cook <[email protected]>Cc "H. Peter Anvin" <[email protected]>Ard Biesheuvel (8):
x86/boot: Grabkernel_infooffset from zoffset header directly x86/boot: Set EFI handover offset directly in header asm x86/boot: Define setup size in linker script x86/boot: Derive file size from_edatasymbol 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
Quoted 6 lines
arch/x86/boot/Makefile | 2 +- arch/x86/boot/compressed/vmlinux.lds.S | 5 +- arch/x86/boot/header.S | 146 +++++++------ arch/x86/boot/setup.ld | 7 +- arch/x86/boot/tools/build.c | 223 +------------------- 5 files changed, 97 insertions(+), 286 deletions(-)
Applied to tip:x86/boot, thanks!
Ingo