Skip to contentSkip to conversation

x86_64: Harden compressed kernel, part 1

RFC PATCH In review Evgeniy Baskov 3 participants 17 messages Aug 1, 2022 – Aug 4, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86_64: Harden compressed kernel, part 1

This is the first half of changes aimed to increase security of early boot code of compressed kernel for x86_64 by enforcing memory protection on page table level.

It applies memory protection to the compressed kernel code executing outside EFI environment and makes all identity mappings explicit to reduce probability of hiding erroneous memory accesses.

Second half makes kernel more compliant PE image and enforces memory protection for EFISTUB code, thus completing W^X support for compressed kernel.

I'll send second half for review later.

Evgeniy Baskov (8):
x86/boot: Align vmlinuz sections on page size x86/build: Remove RWX sections and align on 4KB x86/boot: Set cr0 to known state in trampoline x86/boot: Increase boot page table size x86/boot: Support 4KB pages for identity mapping x86/boot: Setup memory protection for bzImage code x86/boot: Map memory explicitly
x86/boot: Remove mapping from page fault handler

 arch/x86/boot/compressed/acpi.c         |  21 ++-
 arch/x86/boot/compressed/efi.c          |  19 ++-
 arch/x86/boot/compressed/head_64.S      |   7 +-
 arch/x86/boot/compressed/ident_map_64.c | 128 ++++++++++------
 arch/x86/boot/compressed/kaslr.c        |   4 +
 arch/x86/boot/compressed/misc.c         |  52 ++++++-
 arch/x86/boot/compressed/misc.h         |  16 +-
 arch/x86/boot/compressed/pgtable.h      |  20 ---
 arch/x86/boot/compressed/pgtable_64.c   |   2 +-
 arch/x86/boot/compressed/sev.c          |   6 +-
 arch/x86/boot/compressed/vmlinux.lds.S  |   6 +
 arch/x86/include/asm/boot.h             |  26 ++--
 arch/x86/include/asm/init.h             |   1 +
 arch/x86/include/asm/shared/pgtable.h   |  29 ++++
 arch/x86/kernel/vmlinux.lds.S           |  15 +-
 arch/x86/mm/ident_map.c                 | 186 ++++++++++++++++++++----
 16 files changed, 403 insertions(+), 135 deletions(-)
 delete mode 100644 arch/x86/boot/compressed/pgtable.h
 create mode 100644 arch/x86/include/asm/shared/pgtable.h
Signature · 1 line
2.35.1
#1 lore raw

PATCH 1/8 x86/boot: Align vmlinuz sections on page size

To protect sections on page table level each section needs to be aligned on page size (4KB).

Set sections alignment in linker script.

arch/x86/boot/compressed/vmlinux.lds.S +6 -0
index 112b2375d021..6be90f1a1198 100644
@@ -27,21 +27,27 @@ SECTIONS
 		HEAD_TEXT
 		_ehead = . ;
 	}
+	. = ALIGN(PAGE_SIZE);
 	.rodata..compressed : {
+		_compressed = .;
 		*(.rodata..compressed)
+		_ecompressed = .;
 	}
+	. = ALIGN(PAGE_SIZE);
 	.text :	{
 		_text = .; 	/* Text */
 		*(.text)
 		*(.text.*)
 		_etext = . ;
 	}
+	. = ALIGN(PAGE_SIZE);
 	.rodata : {
 		_rodata = . ;
 		*(.rodata)	 /* read-only data */
 		*(.rodata.*)
 		_erodata = . ;
 	}
+	. = ALIGN(PAGE_SIZE);
 	.data :	{
 		_data = . ;
 		*(.data)
Signature · 1 line
2.35.1
#2 lore raw

PATCH 2/8 x86/build: Remove RWX sections and align on 4KB

In reply to Evgeniy Baskov

Avoid creating sections with maximal privileges to prepare for W^X implementation. Align sections on page size (4KB) to allow protecting them in page table.

arch/x86/kernel/vmlinux.lds.S +8 -7
index 15f29053cec4..6587e0201b50 100644
@@ -102,12 +102,11 @@ jiffies = jiffies_64;
 PHDRS {
 	text PT_LOAD FLAGS(5);          /* R_E */
 	data PT_LOAD FLAGS(6);          /* RW_ */
-#ifdef CONFIG_X86_64
-#ifdef CONFIG_SMP
+#if defined(CONFIG_X86_64) && defined(CONFIG_SMP)
 	percpu PT_LOAD FLAGS(6);        /* RW_ */
 #endif
-	init PT_LOAD FLAGS(7);          /* RWE */
-#endif
+	inittext PT_LOAD FLAGS(5);      /* R_E */
+	init PT_LOAD FLAGS(6);          /* RW_ */
 	note PT_NOTE FLAGS(0);          /* ___ */
 }
 
@@ -226,9 +225,10 @@ SECTIONS
 #endif
 
 	INIT_TEXT_SECTION(PAGE_SIZE)
-#ifdef CONFIG_X86_64
-	:init
-#endif
+	:inittext
+
+	. = ALIGN(PAGE_SIZE);
+
 
 	/*
 	 * Section for code used exclusively before alternatives are run. All
@@ -240,6 +240,7 @@ SECTIONS
 	.altinstr_aux : AT(ADDR(.altinstr_aux) - LOAD_OFFSET) {
 		*(.altinstr_aux)
 	}
+	:init
 
 	INIT_DATA_SECTION(16)
 
Signature · 1 line
2.35.1
#3 lore raw

PATCH 3/8 x86/boot: Set cr0 to known state in trampoline

In reply to Evgeniy Baskov

Ensure WP bit to be set to prevent boot code from writing to non-writable memory pages.

arch/x86/boot/compressed/head_64.S +2 -3
index d33f060900d2..5273367283b7 100644
@@ -619,9 +619,8 @@ SYM_CODE_START(trampoline_32bit_src)
 	/* Set up new stack */
 	leal	TRAMPOLINE_32BIT_STACK_END(%ecx), %esp
 
-	/* Disable paging */
-	movl	%cr0, %eax
-	btrl	$X86_CR0_PG_BIT, %eax
+	/* Disable paging and setup CR0 */
+	movl	$(CR0_STATE & ~X86_CR0_PG), %eax
 	movl	%eax, %cr0
 
 	/* Check what paging mode we want to be in after the trampoline */
Signature · 1 line
2.35.1
#4 lore raw

PATCH 4/8 x86/boot: Increase boot page table size

In reply to Evgeniy Baskov

Previous calculations ignored pages implicitly mapped by ACPI code, so theoretical upper limit is higher than was set.

Using 4KB pages is desirable for better memory protection granularity. Approximately twice as much memory is required for those.

Increase initial page table size to 64 4KB page tables.

arch/x86/include/asm/boot.h +14 -12
index 9191280d9ea3..024d972c248e 100644
@@ -41,22 +41,24 @@
 # define BOOT_STACK_SIZE	0x4000
 
 # define BOOT_INIT_PGT_SIZE	(6*4096)
-# ifdef CONFIG_RANDOMIZE_BASE
 /*
  * Assuming all cross the 512GB boundary:
  * 1 page for level4
- * (2+2)*4 pages for kernel, param, cmd_line, and randomized kernel
- * 2 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP).
- * Total is 19 pages.
+ * (3+3)*2 pages for param and cmd_line
+ * (2+2+S)*2 pages for kernel and randomized kernel, where S is total number
+ *     of sections of kernel. Explanation: 2+2 are upper level page tables.
+ *     We can have only S unaligned parts of section: 1 at the end of the kernel
+ *     and (S-1) at the section borders. The start address of the kernel is
+ *     aligned, so an extra page table. There are at most S=6 sections in
+ *     vmlinux ELF image.
+ * 3 pages for first 2M (video RAM: CONFIG_X86_VERBOSE_BOOTUP).
+ * Total is 36 pages.
+ *
+ * Some pages are also required for UEFI memory map and
+ * ACPI table mappings, so we need to add extra space.
+ * FIXME: Figure out exact amount of pages.
  */
-#  ifdef CONFIG_X86_VERBOSE_BOOTUP
-#   define BOOT_PGT_SIZE	(19*4096)
-#  else /* !CONFIG_X86_VERBOSE_BOOTUP */
-#   define BOOT_PGT_SIZE	(17*4096)
-#  endif
-# else /* !CONFIG_RANDOMIZE_BASE */
-#  define BOOT_PGT_SIZE		BOOT_INIT_PGT_SIZE
-# endif
+# define BOOT_PGT_SIZE		(64*4096)
 
 #else /* !CONFIG_X86_64 */
 # define BOOT_STACK_SIZE	0x1000
Signature · 1 line
2.35.1
#5 lore raw

PATCH 5/8 x86/boot: Support 4KB pages for identity mapping

In reply to Evgeniy Baskov

Current identity mapping code only supports 2M and 1G pages. 4KB pages are desirable for better memory protection granularity in compressed kernel code.

Change identity mapping code to support 4KB pages and memory remapping with different attributes.

arch/x86/include/asm/init.h +1 -0
index 5f1d3c421f68..a8277ee82c51 100644
@@ -8,6 +8,7 @@ struct x86_mapping_info {
 	unsigned long page_flag;	 /* page flag for PMD or PUD entry */
 	unsigned long offset;		 /* ident mapping offset */
 	bool direct_gbpages;		 /* PUD level 1GB page support */
+	bool allow_4kpages;		 /* Allow more granular mappings with 4K pages */
 	unsigned long kernpg_flag;	 /* kernel pagetable flag override */
 };
 
arch/x86/mm/ident_map.c +154 -32

268 lines not on this page · raw message

Signature · 1 line
2.35.1
#6 lore raw

PATCH 6/8 x86/boot: Setup memory protection for bzImage code

In reply to Evgeniy Baskov

Use previously added code to use 4KB pages for mapping. Map compressed and uncompressed kernel with appropriate memory protection attributes. For compressed kernel set them up manually. For uncompressed kernel used flags specified in ELF header.

Move 'boot/compressed/pgtable.h' to common headers to make it accessible from EFISTUB code.

 delete mode 100644 arch/x86/boot/compressed/pgtable.h
 create mode 100644 arch/x86/include/asm/shared/pgtable.h
arch/x86/boot/compressed/head_64.S +1 -1
index 5273367283b7..4cc1463b98e8 100644
@@ -35,7 +35,7 @@
 #include <asm/bootparam.h>
 #include <asm/desc_defs.h>
 #include <asm/trapnr.h>
-#include "pgtable.h"
+#include <asm/shared/pgtable.h>
 
 /*
  * Locally defined symbols should be marked hidden:
arch/x86/boot/compressed/ident_map_64.c +71 -33

178 lines not on this page · raw message

arch/x86/boot/compressed/misc.c +47 -5

91 lines not on this page · raw message

arch/x86/boot/compressed/misc.h +14 -2
index 4910bf230d7b..699b87b7813a 100644
@@ -161,8 +161,20 @@ static inline int count_immovable_mem_regions(void) { return 0; }
 #ifdef CONFIG_X86_5LEVEL
 extern unsigned int __pgtable_l5_enabled, pgdir_shift, ptrs_per_p4d;
 #endif
-extern void kernel_add_identity_map(unsigned long start, unsigned long end);
-
+#ifdef CONFIG_X86_64
+extern unsigned long kernel_add_identity_map(unsigned long start,
+					     unsigned long end,
+					     unsigned int flags);
+#else
+static inline unsigned long kernel_add_identity_map(unsigned long start,
+						    unsigned long end,
+						    unsigned int flags)
+{
+	(void)flags;
+	(void)end;
+	return start;
+}
+#endif
 /* Used by PAGE_KERN* macros: */
 extern pteval_t __default_kernel_pte_mask;
 
arch/x86/boot/compressed/pgtable.h deleted +0 -20
deleted file mode 100644
index cc9b2529a086..000000000000
@@ -1,20 +0,0 @@
-#ifndef BOOT_COMPRESSED_PAGETABLE_H
-#define BOOT_COMPRESSED_PAGETABLE_H
-
-#define TRAMPOLINE_32BIT_SIZE		(2 * PAGE_SIZE)
-
-#define TRAMPOLINE_32BIT_PGTABLE_OFFSET	0
-
-#define TRAMPOLINE_32BIT_CODE_OFFSET	PAGE_SIZE
-#define TRAMPOLINE_32BIT_CODE_SIZE	0x80
-
-#define TRAMPOLINE_32BIT_STACK_END	TRAMPOLINE_32BIT_SIZE
-
-#ifndef __ASSEMBLER__
-
-extern unsigned long *trampoline_32bit;
-
-extern void trampoline_32bit_src(void *return_ptr);
-
-#endif /* __ASSEMBLER__ */
-#endif /* BOOT_COMPRESSED_PAGETABLE_H */
arch/x86/boot/compressed/pgtable_64.c +1 -1
index 2ac12ff4111b..c7cf5a1059a8 100644
@@ -2,7 +2,7 @@
 #include "misc.h"
 #include <asm/e820/types.h>
 #include <asm/processor.h>
-#include "pgtable.h"
+#include <asm/shared/pgtable.h>
 #include "../string.h"
 #include "efi.h"
 
arch/x86/boot/compressed/sev.c +4 -2
index 52f989f6acc2..82a10395cf22 100644
@@ -13,6 +13,7 @@
 #include "misc.h"
 
 #include <asm/pgtable_types.h>
+#include <asm/shared/pgtable.h>
 #include <asm/sev.h>
 #include <asm/trapnr.h>
 #include <asm/trap_pf.h>
@@ -427,10 +428,11 @@ void sev_prep_identity_maps(unsigned long top_level_pgt)
 		unsigned long cc_info_pa = boot_params->cc_blob_address;
 		struct cc_blob_sev_info *cc_info;
 
-		kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info));
+		kernel_add_identity_map(cc_info_pa, cc_info_pa + sizeof(*cc_info), MAP_NOFLUSH);
 
 		cc_info = (struct cc_blob_sev_info *)cc_info_pa;
-		kernel_add_identity_map(cc_info->cpuid_phys, cc_info->cpuid_phys + cc_info->cpuid_len);
+		kernel_add_identity_map(cc_info->cpuid_phys,
+					cc_info->cpuid_phys + cc_info->cpuid_len, MAP_NOFLUSH);
 	}
 
 	sev_verify_cbit(top_level_pgt);
arch/x86/include/asm/shared/pgtable.h new file +30 -0
new file mode 100644
index 000000000000..6527dadf39d6
@@ -0,0 +1,30 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef ASM_SHARED_PAGETABLE_H
+#define ASM_SHARED_PAGETABLE_H
+
+/* Boot time memory mapping flags used in compressed kernel */
+#define MAP_WRITE	0x02 /* Writable memory */
+#define MAP_EXEC	0x04 /* Executable memory */
+#define MAP_ALLOC	0x10 /* Range needs to be allocated */
+#define MAP_PROTECT	0x20 /* Set exact memory attributes for memory range */
+#define MAP_NOFLUSH	0x40 /* Avoid flushing TLB */
+
+#define TRAMPOLINE_32BIT_SIZE		(3 * PAGE_SIZE)
+
+#define TRAMPOLINE_32BIT_PLACEMENT_MAX	(0xA0000)
+
+#define TRAMPOLINE_32BIT_PGTABLE_OFFSET	0
+
+#define TRAMPOLINE_32BIT_CODE_OFFSET	PAGE_SIZE
+#define TRAMPOLINE_32BIT_CODE_SIZE	0x80
+
+#define TRAMPOLINE_32BIT_STACK_END	TRAMPOLINE_32BIT_SIZE
+
+#ifndef __ASSEMBLER__
+
+extern unsigned long *trampoline_32bit;
+
+extern void trampoline_32bit_src(void *return_ptr);
+
+#endif /* __ASSEMBLER__ */
+#endif /* ASM_SHARED_PAGETABLE_H */
Signature · 1 line
2.35.1
#7 lore raw

PATCH 7/8 x86/boot: Map memory explicitly

In reply to Evgeniy Baskov

Implicit mappings hide possible memory errors, e.g. allocations for ACPI tables were not included in boot page table size.

Replace all implicit mappings from page fault handler with explicit mappings.

arch/x86/boot/compressed/acpi.c +20 -1
index 9caf89063e77..633ac56262ee 100644
@@ -93,6 +93,8 @@ static u8 *scan_mem_for_rsdp(u8 *start, u32 length)
 
 	end = start + length;
 
+	kernel_add_identity_map((unsigned long)start, (unsigned long)end, 0);
+
 	/* Search from given start address for the requested length */
 	for (address = start; address < end; address += ACPI_RSDP_SCAN_STEP) {
 		/*
@@ -128,6 +130,9 @@ static acpi_physical_address bios_get_rsdp_addr(void)
 	unsigned long address;
 	u8 *rsdp;
 
+	kernel_add_identity_map((unsigned long)ACPI_EBDA_PTR_LOCATION,
+				(unsigned long)ACPI_EBDA_PTR_LOCATION + 2, 0);
+
 	/* Get the location of the Extended BIOS Data Area (EBDA) */
 	address = *(u16 *)ACPI_EBDA_PTR_LOCATION;
 	address <<= 4;
@@ -215,6 +220,9 @@ static unsigned long get_acpi_srat_table(void)
 	if (!rsdp)
 		return 0;
 
+	kernel_add_identity_map((unsigned long)rsdp,
+				(unsigned long)(rsdp + 1), 0);
+
 	/* Get ACPI root table from RSDP.*/
 	if (!(cmdline_find_option("acpi", arg, sizeof(arg)) == 4 &&
 	    !strncmp(arg, "rsdt", 4)) &&
@@ -235,6 +243,9 @@ static unsigned long get_acpi_srat_table(void)
 	if (len < sizeof(struct acpi_table_header) + size)
 		return 0;
 
+	kernel_add_identity_map((unsigned long)header,
+				(unsigned long)header + len, 0);
+
 	num_entries = (len - sizeof(struct acpi_table_header)) / size;
 	entry = (u8 *)(root_table + sizeof(struct acpi_table_header));
 
@@ -247,8 +258,16 @@ static unsigned long get_acpi_srat_table(void)
 		if (acpi_table) {
 			header = (struct acpi_table_header *)acpi_table;
 
-			if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT))
+			kernel_add_identity_map(acpi_table,
+						acpi_table + sizeof(*header),
+						0);
+
+			if (ACPI_COMPARE_NAMESEG(header->signature, ACPI_SIG_SRAT)) {
+				kernel_add_identity_map(acpi_table,
+							acpi_table + header->length,
+							0);
 				return acpi_table;
+			}
 		}
 		entry += size;
 	}
arch/x86/boot/compressed/efi.c +18 -1
index 6edd034b0b30..ce70103fbbc0 100644
@@ -57,10 +57,14 @@ enum efi_type efi_get_type(struct boot_params *bp)
  */
 unsigned long efi_get_system_table(struct boot_params *bp)
 {
-	unsigned long sys_tbl_pa;
+	static unsigned long sys_tbl_pa __section(".data");
 	struct efi_info *ei;
+	unsigned long sys_tbl_size;
 	enum efi_type et;
 
+	if (sys_tbl_pa)
+		return sys_tbl_pa;
+
 	/* Get systab from boot params. */
 	ei = &bp->efi_info;
 #ifdef CONFIG_X86_64
@@ -73,6 +77,13 @@ unsigned long efi_get_system_table(struct boot_params *bp)
 		return 0;
 	}
 
+	if (efi_get_type(bp) == EFI_TYPE_64)
+		sys_tbl_size = sizeof(efi_system_table_64_t);
+	else
+		sys_tbl_size = sizeof(efi_system_table_32_t);
+
+	kernel_add_identity_map(sys_tbl_pa, sys_tbl_pa + sys_tbl_size, 0);
+
 	return sys_tbl_pa;
 }
 
@@ -92,6 +103,10 @@ static struct efi_setup_data *get_kexec_setup_data(struct boot_params *bp,
 
 	pa_data = bp->hdr.setup_data;
 	while (pa_data) {
+		unsigned long pa_data_end = pa_data + sizeof(struct setup_data)
+					  + sizeof(struct efi_setup_data);
+		kernel_add_identity_map(pa_data, pa_data_end, 0);
+
 		data = (struct setup_data *)pa_data;
 		if (data->type == SETUP_EFI) {
 			esd = (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
@@ -160,6 +175,8 @@ int efi_get_conf_table(struct boot_params *bp, unsigned long *cfg_tbl_pa,
 		return -EINVAL;
 	}
 
+	kernel_add_identity_map(*cfg_tbl_pa, *cfg_tbl_pa + *cfg_tbl_len, 0);
+
 	return 0;
 }
 
arch/x86/boot/compressed/kaslr.c +4 -0
index 4a3f223973f4..073c7cfbd785 100644
@@ -687,6 +687,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 	u32 nr_desc;
 	int i;
 
+	kernel_add_identity_map((unsigned long)e, (unsigned long)(e + 1), 0);
+
 	signature = (char *)&e->efi_loader_signature;
 	if (strncmp(signature, EFI32_LOADER_SIGNATURE, 4) &&
 	    strncmp(signature, EFI64_LOADER_SIGNATURE, 4))
@@ -703,6 +705,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
 	pmap = (e->efi_memmap | ((__u64)e->efi_memmap_hi << 32));
 #endif
 
+	kernel_add_identity_map(pmap, pmap + e->efi_memmap_size, 0);
+
 	nr_desc = e->efi_memmap_size / e->efi_memdesc_size;
 	for (i = 0; i < nr_desc; i++) {
 		md = efi_early_memdesc_ptr(pmap, e->efi_memdesc_size, i);
Signature · 1 line
2.35.1
#8 lore raw

PATCH 8/8 x86/boot: Remove mapping from page fault handler

In reply to Evgeniy Baskov

After every implicit mapping is removed, this code is no longer needed.

Remove memory mapping from page fault handler to ensure that there are no hidden invalid memory accesses.

arch/x86/boot/compressed/ident_map_64.c +10 -16
index 04022c080114..ad24289cc224 100644
@@ -393,27 +393,21 @@ void do_boot_page_fault(struct pt_regs *regs, unsigned long error_code)
 {
 	unsigned long address = native_read_cr2();
 	unsigned long end;
-	bool ghcb_fault;
+	char *msg;
 
-	ghcb_fault = sev_es_check_ghcb_fault(address);
+	if (sev_es_check_ghcb_fault(address))
+		msg = "Page-fault on GHCB page:";
+	else
+		msg = "Unexpected page-fault:";
 
 	address   &= PMD_MASK;
 	end        = address + PMD_SIZE;
 
 	/*
-	 * Check for unexpected error codes. Unexpected are:
-	 *	- Faults on present pages
-	 *	- User faults
-	 *	- Reserved bits set
-	 */
-	if (error_code & (X86_PF_PROT | X86_PF_USER | X86_PF_RSVD))
-		do_pf_error("Unexpected page-fault:", error_code, address, regs->ip);
-	else if (ghcb_fault)
-		do_pf_error("Page-fault on GHCB page:", error_code, address, regs->ip);
-
-	/*
-	 * Error code is sane - now identity map the 2M region around
-	 * the faulting address.
+	 * Since all memory allocations are made explicit
+	 * now, every page fault at this stage is an
+	 * error and the error handler is there only
+	 * for debug purposes.
 	 */
-	kernel_add_identity_map(address, end, MAP_WRITE);
+	do_pf_error(msg, error_code, address, regs->ip);
 }
Signature · 1 line
2.35.1
#9 lore raw
In reply to Evgeniy Baskov
Quoted 3 lines · Evgeniy Baskov

This is the first half of changes aimed to increase security of early boot code of compressed kernel for x86_64 by enforcing memory protection on page table level.

Could you share a little more background here? Hardening is good, but you _can_ have too much of a good thing.

Is this part of the boot cycle becoming a target for attackers in trusted boot environments? Do emerging confidential computing technologies like SEV and TDX cause increased reliance on compressed kernel security?

In other words, why is *THIS* important versus all the other patches floating around out there?

#10 lore raw
Quoted 8 lines · Dave Hansen
Quoted 3 lines · Evgeniy Baskov

This is the first half of changes aimed to increase security of early boot code of compressed kernel for x86_64 by enforcing memory protection on page table level.

Could you share a little more background here? Hardening is good, but you _can_ have too much of a good thing.

Quoted 7 lines

Is this part of the boot cycle becoming a target for attackers in trusted boot environments? Do emerging confidential computing technologies like SEV and TDX cause increased reliance on compressed kernel security?

In other words, why is *THIS* important versus all the other patches floating around out there?

Now compressed kernel code becomes larger, partially because of adding SEV and TDX, so it worth adding memory protection here.

First part implements partial memory protection for every way of booting the kernel, and second adds full W^X implementation specifically for the UEFI code path. First part also contains prerequisite changes for the second part like adding explicit memory allocation in extraction code and adjusting ld-script to produce ELF images suitable for mapping PE sections on top of them with appropriate access rights.

One of pros for this patch set is that is would reveal invalid memory accesses by removing implicit memory mapping and reducing access rights for mapped memory. So it makes further development of the compressed kernel code less error prone.

Furthermore, memory protection indeed makes it harder to attack kernel during boot cycle. And unlike TDX and SEV it does not not only aim to protect kernel from attacks outside of virtualized environments but it also makes attacking the kernel booting on bare metal harder. If some code injection vulnerability lives inside compressed kernel code this will likely make it harder to exploit.

Another thing is that it should not bring any noticeable overhead. Second part can actually reduce overhead slightly by removing the need to copy the kernel image around during boot process and extracting the kernel before exiting EFI boot services.

Second part also makes kernel more spec compliant PE image as a part of an implementation of memory protection in EFI environment. This will allow booting the kernel with stricter implementations of PE loaders, e.g. [1]. And stricter PE loader is really desired, since current EDK II implementation contains numerous problems [2].

[1] https://github.com/acidanthera/audk/tree/secure_pe
[2] https://arxiv.org/pdf/2012.05471.pdf

Thanks,
Evgeniy Baskov

#11 lore raw
Quoted 14 lines · Evgeniy Baskov
Quoted 13 lines · Dave Hansen
Quoted 3 lines · Evgeniy Baskov

This is the first half of changes aimed to increase security of early boot code of compressed kernel for x86_64 by enforcing memory protection on page table level.

Could you share a little more background here?  Hardening is good, but you _can_ have too much of a good thing.

Is this part of the boot cycle becoming a target for attackers in trusted boot environments?  Do emerging confidential computing technologies like SEV and TDX cause increased reliance on compressed kernel security?

Quoted 5 lines
Quoted 2 lines

In other words, why is *THIS* important versus all the other patches floating around out there?

Now compressed kernel code becomes larger, partially because of adding SEV and TDX, so it worth adding memory protection here.

...

Is it fair to say that the problems here are on the potential, theoretical side rather than driven by practical, known issues that our users face?

#12 lore raw
Quoted 18 lines · Dave Hansen
Quoted 17 lines · Evgeniy Baskov
Quoted 15 lines · Dave Hansen
Quoted 3 lines · Evgeniy Baskov

This is the first half of changes aimed to increase security of early boot code of compressed kernel for x86_64 by enforcing memory protection on page table level.

Could you share a little more background here?  Hardening is good, but you _can_ have too much of a good thing.

Is this part of the boot cycle becoming a target for attackers in trusted boot environments?  Do emerging confidential computing technologies like SEV and TDX cause increased reliance on compressed kernel security?

In other words, why is *THIS* important versus all the other patches floating around out there?

Quoted 7 lines
Quoted 2 lines

Now compressed kernel code becomes larger, partially because of adding SEV and TDX, so it worth adding memory protection here.

...

Is it fair to say that the problems here are on the potential, theoretical side rather than driven by practical, known issues that our users face?

Partially. We do have known issues because kernel PE image is not compliant with the MS PE and COFF specification v8.3 referenced by the UEFI specification. UEFI implementations with stricter PE loaders (e.g. mentioned above) fail to boot Linux kernel.

As for hardening side, these improvements are indeed just nice-to-haves. But we believe it is good to have them if they are available for free.

Thanks,
Evgeniy Baskov

#13 lore raw
Quoted 4 lines · Evgeniy Baskov

Partially. We do have known issues because kernel PE image is not compliant with the MS PE and COFF specification v8.3 referenced by the UEFI specification. UEFI implementations with stricter PE loaders (e.g. mentioned above) fail to boot Linux kernel.

That shows me that it's _possible_ to build a more strict PE loader that wouldn't load Linux. But, in practice is anyone using a more strict PE loader? Does anyone actually want that in practice? Or, again, is this more strict PE loader just an academic demonstration?

The README starts:

This branch demonstrates...

That doesn't seem like something that's _important_ to deal with. Sounds like a proof-of-concept.

Don't get me wrong, I'm all for improving thing, even if the benefits are far off. But, let's not fool ourselves.

#14 lore raw
Quoted 8 lines · Dave Hansen

That shows me that it's _possible_ to build a more strict PE loader that wouldn't load Linux. But, in practice is anyone using a more strict PE loader? Does anyone actually want that in practice? Or, again, is this more strict PE loader just an academic demonstration?

The README starts:

Quoted 7 lines

This branch demonstrates...

That doesn't seem like something that's _important_ to deal with. Sounds like a proof-of-concept.

Don't get me wrong, I'm all for improving thing, even if the benefits are far off. But, let's not fool ourselves.

We have commercial closed-source UEFI firmware implementation at ISP RAS that follows the behavior of the secure_pe branch. That firmware is used as a part of [1].

[1] https://www.ispras.ru/en/technologies/asperitas/

Thanks,
Evgeniy Baskov

#15 lore raw
Quoted 14 lines · Evgeniy Baskov
Quoted 13 lines · Dave Hansen

That shows me that it's _possible_ to build a more strict PE loader that wouldn't load Linux. But, in practice is anyone using a more strict PE loader? Does anyone actually want that in practice? Or, again, is this more strict PE loader just an academic demonstration?

The README starts:

This branch demonstrates...

That doesn't seem like something that's _important_ to deal with. Sounds like a proof-of-concept.

Quoted 8 lines
Quoted 2 lines

Don't get me wrong, I'm all for improving thing, even if the benefits are far off. But, let's not fool ourselves.

We have commercial closed-source UEFI firmware implementation at ISP RAS that follows the behavior of the secure_pe branch. That firmware is used as a part of [1].

[1] https://www.ispras.ru/en/technologies/asperitas/

Are there any plans on getting those changes merged back upstream to the main UEFI codebase so that others can test this type of functionality out?

thanks,

greg k-h

#16 lore raw

On 2022-08-04 14:22, Greg KH wrote:
...

Quoted 7 lines

Are there any plans on getting those changes merged back upstream to the main UEFI codebase so that others can test this type of functionality out?

thanks,

greg k-h

The initial prototype of the changes were published as a part of the tianocore/edk2-staging[1], and a more up-to-date open source version was published as a part of the acidanthera/audk. This version is currently being integrated with the EDK II build system, and its malfunctioning is currently the main technical issue for integrating changes into the main branch.

It is hard to estimate when the merge with the edk2 mainline happens, but we are committed to doing this. The amount of changes needed is quite large, and simply getting approval from all the maintainers will take time even if they are all willing to get this in. On the good side, several parties, Microsoft in particular, were interested in upstreaming this code, so we have moderate optimism for the future.

In case you are interested in the details, there is also academic material available, describing the issues and the changes made, which can help to shed some light on the implementation[3][4].

[1] https://github.com/tianocore/edk2-staging/tree/2021-gsoc-secure-loader
[2] https://github.com/acidanthera/audk/tree/secure_pe
[3] https://arxiv.org/pdf/2012.05471.pdf
[4] https://github.com/mhaeuser/ISPRASOpen-SecurePE

Thanks,
Evgeniy Baskov