UEFI emulator for kexec
Subsystems (5)
v2 cover UEFI emulator for kexec
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
After discussion, Ard's approach seems to be a more promising solution to handle PE format kernels once and for all. This series follows that approach and implements an emulator to emulate EFI boot time services, allowing the efistub kernel to self-extract and boot.
Another year has passed, and UKI kernel is more and more frequently used in product. I think it is time to pay effort to resolve this issue.
*** Overview of implement ***
The whole model consits of three parts:
-1. The emulator
It is a self-relocatable PIC code, which is finally linked into kernel, but not export any internal symbol to kernel. It mainly contains: a PE file parser, which loads PE format kernel, a group of functions to emulate efi boot service.
-2. inside kernel, PE-format loader
Its main task is to set up two extra kexec_segment, one for emulator, the other for passing information from the first kernel to emulator.
-3. set up identity mapping only for the memory used by the emulator. Here it relies on kimage_alloc_control_pages() to get pages, which will not stamped during the process of kexec relocate (cp from src to dst). And since the mapping only covers a small range of memory, it cost small amount memory.
*** To do ***
Currently, it only works on arm64 virt machine. For x86, it needs some slightly changes. (I plan to do it in the next version)
Also, this series does not implement a memory allocator, which I plan to implement with the help of bitmap.
About console, currently it hard code for arm64 virt machine, later it should extract the information through ACPI table.
For kdump code, it is not implmented yet. But it should share the majority of this series.
*** Test of this series ***
I have tested this series on arm64 virt machine. There I booted the vmlinuz.efi and kexec_file_load a UKI image, then switch to the second kernel.
I used a modified kexec-tools [4], which just skips the check of the file format and passes the file directly to kernel.
[1]: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#m42abb0ad3c10126b8b3bfae8a596deb707d6f76e
[2]: https://lore.kernel.org/lkml/20230918173607.421d2616@rotkaeppchen/T/
[3]: https://lore.kernel.org/lkml/20230918173607.421d2616@rotkaeppchen/T/#mc60aa591cb7616ceb39e1c98f352383f9ba6e985
[4]: https://github.com/pfliu/kexec-tools.git branch: kexec_uefi_emulator
RFCv1 -> RFCv2:
-1.Support to run UKI kernel by: add LoadImage() and StartImage(), add
PE file relocation support, add InstallMultiProtocol()
-2.Also set up idmap for EFI runtime memory descriptor since UKI's
systemd-stub calls runtime service
-3.Move kexec_pe_image.c from arch/arm64/kernel to kernel/, since it
aims to provide a more general architecture support.
RFCv1: https://lore.kernel.org/linux-efi/[email protected]/
RFCv2: https://github.com/pfliu/linux.git branch kexec_uefi_emulator_RFCv2
Pingfan Liu (9):
efi/libstub: Ask efi_random_alloc() to skip unusable memory efi/libstub: Complete efi_simple_text_output_protocol efi/emulator: Initial rountines to emulate EFI boot time service efi/emulator: Turn on mmu for arm64
kexec: Introduce kexec_pe_image to parse and load PE file
arm64: kexec: Introduce a new member param_mem to kimage_arch
arm64: mm: Change to prototype of
arm64: kexec: Prepare page table for emulator
arm64: kexec: Enable kexec_pe_image
arch/arm64/Kconfig | 4 + arch/arm64/include/asm/kexec.h | 1 + arch/arm64/include/asm/mmu.h | 6 + arch/arm64/kernel/asm-offsets.c | 2 +- arch/arm64/kernel/machine_kexec.c | 103 +++- arch/arm64/kernel/machine_kexec_file.c | 4 + arch/arm64/kernel/relocate_kernel.S | 2 +- arch/arm64/mm/mmu.c | 67 ++- drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/efi_emulator/Makefile | 99 ++++ .../firmware/efi/efi_emulator/amba-pl011.c | 81 +++ .../efi_emulator/arm64_emulator_service.lds | 45 ++ .../firmware/efi/efi_emulator/arm64_proc.S | 175 ++++++ .../firmware/efi/efi_emulator/config_table.c | 25 + drivers/firmware/efi/efi_emulator/core.c | 376 +++++++++++++ .../firmware/efi/efi_emulator/device_handle.c | 138 +++++ drivers/firmware/efi/efi_emulator/earlycon.h | 19 + .../firmware/efi/efi_emulator/efi_emulator.S | 12 + drivers/firmware/efi/efi_emulator/emulator.h | 106 ++++ drivers/firmware/efi/efi_emulator/entry.c | 68 +++ drivers/firmware/efi/efi_emulator/head.S | 10 + drivers/firmware/efi/efi_emulator/lib.c | 73 +++ drivers/firmware/efi/efi_emulator/memory.c | 27 + .../firmware/efi/efi_emulator/memory_api.c | 74 +++ drivers/firmware/efi/efi_emulator/misc.c | 43 ++ drivers/firmware/efi/efi_emulator/pe_loader.c | 173 ++++++ drivers/firmware/efi/efi_emulator/printf.c | 373 +++++++++++++ .../efi/efi_emulator/protocol_device_path.c | 75 +++ .../protocol_simple_text_output.c | 50 ++ drivers/firmware/efi/libstub/efistub.h | 7 + drivers/firmware/efi/libstub/randomalloc.c | 5 + include/linux/efi_emulator.h | 46 ++ include/linux/kexec.h | 6 + kernel/Makefile | 1 + kernel/kexec_pe_image.c | 503 ++++++++++++++++++ 35 files changed, 2764 insertions(+), 36 deletions(-) create mode 100644 drivers/firmware/efi/efi_emulator/Makefile create mode 100644 drivers/firmware/efi/efi_emulator/amba-pl011.c create mode 100644 drivers/firmware/efi/efi_emulator/arm64_emulator_service.lds create mode 100644 drivers/firmware/efi/efi_emulator/arm64_proc.S create mode 100644 drivers/firmware/efi/efi_emulator/config_table.c create mode 100644 drivers/firmware/efi/efi_emulator/core.c create mode 100644 drivers/firmware/efi/efi_emulator/device_handle.c create mode 100644 drivers/firmware/efi/efi_emulator/earlycon.h create mode 100644 drivers/firmware/efi/efi_emulator/efi_emulator.S create mode 100644 drivers/firmware/efi/efi_emulator/emulator.h create mode 100644 drivers/firmware/efi/efi_emulator/entry.c create mode 100644 drivers/firmware/efi/efi_emulator/head.S create mode 100644 drivers/firmware/efi/efi_emulator/lib.c create mode 100644 drivers/firmware/efi/efi_emulator/memory.c create mode 100644 drivers/firmware/efi/efi_emulator/memory_api.c create mode 100644 drivers/firmware/efi/efi_emulator/misc.c create mode 100644 drivers/firmware/efi/efi_emulator/pe_loader.c create mode 100644 drivers/firmware/efi/efi_emulator/printf.c create mode 100644 drivers/firmware/efi/efi_emulator/protocol_device_path.c create mode 100644 drivers/firmware/efi/efi_emulator/protocol_simple_text_output.c create mode 100644 include/linux/efi_emulator.h create mode 100644 kernel/kexec_pe_image.c
Signature · 1 line
2.41.0
RFC v2 1/9 efi/libstub: Ask efi_random_alloc() to skip unusable memory
efi_random_alloc() demands EFI_ALLOCATE_ADDRESS when allocate_pages(), but the current implement can not ensure the selected target locates inside free area, that is to exclude EFI_BOOT_SERVICES_*, EFI_RUNTIME_SERVICES_* etc.
Fix the issue by checking md->type.
--- drivers/firmware/efi/libstub/randomalloc.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/firmware/efi/libstub/randomalloc.c +5 -0
@@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,efi_memory_desc_t *md = (void *)map->map + map_offset;unsigned long slots; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; slots =get_entry_num_slots (md, size,ilog2 (align), alloc_min, alloc_max);MD_NUM_SLOTS (md) = slots;
@@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,efi_physical_addr_t target;unsigned long pages; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; +if (total_mirrored_slots >0 && !(md->attribute &EFI_MEMORY_MORE_RELIABLE ))continue ;
Signature · 1 line
2.41.0
Re: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Quoted 6 lines · Pingfan Liu
efi_random_alloc()demandsEFI_ALLOCATE_ADDRESSwhenallocate_pages(), but the current implement can not ensure the selected target locates inside free area, that is to exclude EFI_BOOT_SERVICES_*, EFI_RUNTIME_SERVICES_* etc.Fix the issue by checking
md->type.
If it is a fix shouldn't this have a fixes tag?
Quoted 13 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/libstub/randomalloc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/firmware/efi/libstub/randomalloc.c +0
-0
drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 81
Quoted 3 lines
drivers/firmware/efi/libstub/randomalloc.c +0
-0@@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,efi_memory_desc_t *md = (void *)map->map + map_offset;unsigned long slots;
I'd add this inline comment:
/* Skip "unconventional" memory: */
Quoted 6 lines
drivers/firmware/efi/libstub/randomalloc.c +2
-0+if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; slots =get_entry_num_slots (md, size,ilog2 (align), alloc_min, alloc_max);MD_NUM_SLOTS (md) = slots;
drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 121
Quoted 9 lines
drivers/firmware/efi/libstub/randomalloc.c +3
-0@@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,efi_physical_addr_t target;unsigned long pages; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; +if (total_mirrored_slots >0 && !(md->attribute &EFI_MEMORY_MORE_RELIABLE ))continue ;
BR, Jarkko
Re: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
Quoted 11 lines · Jarkko Sakkinen
Quoted 6 lines · Pingfan Liu
efi_random_alloc()demandsEFI_ALLOCATE_ADDRESSwhenallocate_pages(), but the current implement can not ensure the selected target locates inside free area, that is to exclude EFI_BOOT_SERVICES_*, EFI_RUNTIME_SERVICES_* etc.Fix the issue by checking
md->type.If it is a fix shouldn't this have a fixes tag?
Yes, I will supplement the following in the next version
Quoted 19 lines
Quoted 13 lines
Signed-off-by Pingfan Liu <[email protected]>Cc Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/libstub/randomalloc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/firmware/efi/libstub/randomalloc.c +0
-0drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 81
Quoted 3 lines
drivers/firmware/efi/libstub/randomalloc.c +0
-0@@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,efi_memory_desc_t *md = (void *)map->map + map_offset;unsigned long slots;I'd add this inline comment:
Quoted 2 lines
/* Skip "unconventional" memory: */
Adopt.
Thanks for your kind review.
Best Regards,
Pingfan
Quoted 18 lines
Quoted 6 lines
drivers/firmware/efi/libstub/randomalloc.c +2
-0+if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; slots =get_entry_num_slots (md, size,ilog2 (align), alloc_min, alloc_max);MD_NUM_SLOTS (md) = slots;drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 121
Quoted 9 lines
drivers/firmware/efi/libstub/randomalloc.c +3
-0@@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,efi_physical_addr_t target;unsigned long pages; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; +if (total_mirrored_slots >0 && !(md->attribute &EFI_MEMORY_MORE_RELIABLE ))continue ;BR, Jarkko
Ard Biesheuvel
maintainerRe: [RFCv2 1/9] efi/libstub: Ask efi_random_alloc() to skip unusable memory
In reply to Pingfan LiuQuoted 20 lines · Pingfan Liu
efi_random_alloc()demandsEFI_ALLOCATE_ADDRESSwhenallocate_pages(), but the current implement can not ensure the selected target locates inside free area, that is to exclude EFI_BOOT_SERVICES_*, EFI_RUNTIME_SERVICES_* etc.Fix the issue by checking
md->type.Signed-off-by Pingfan Liu <[email protected]>Cc Ard Biesheuvel <[email protected]>--- drivers/firmware/efi/libstub/randomalloc.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/firmware/efi/libstub/randomalloc.c +0
-0
drivers/firmware/efi/libstub/randomalloc.c efi_status_t efi_random_alloc(unsigned long size, · line 83
Quoted 5 lines
drivers/firmware/efi/libstub/randomalloc.c +2
-0@@ -79,6 +79,8 @@ efi_status_t efi_random_alloc(unsigned long size,efi_memory_desc_t *md = (void *)map->map + map_offset;unsigned long slots; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ;
This is wrong in 3 different ways:
- md->type is not a bitmask
- || is not bitwise but boolean
- get_entry_num_slots() ignores all memory types except EFI_CONVENTIONAL_MEMORY anyway.
So what exactly are you trying to fix here?
Quoted 16 lines
drivers/firmware/efi/libstub/randomalloc.c +3
-1slots =get_entry_num_slots (md, size,ilog2 (align), alloc_min, alloc_max);MD_NUM_SLOTS (md) = slots;@@ -111,6 +113,9 @@ efi_status_t efi_random_alloc(unsigned long size,efi_physical_addr_t target;unsigned long pages; +if (!(md->type & (EFI_CONVENTIONAL_MEMORY ||EFI_PERSISTENT_MEMORY ))) +continue ; +if (total_mirrored_slots >0 && !(md->attribute &EFI_MEMORY_MORE_RELIABLE ))continue ; --2.41.0
RFC v2 2/9 efi/libstub: Complete efi_simple_text_output_protocol
In reply to Pingfan LiuSome uefi applications e.g systemd-stub uses more members in efi_simple_text_output_protocol, hence extending its members as UEFI specification.
--- drivers/firmware/efi/libstub/efistub.h | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/firmware/efi/libstub/efistub.h +7 -0
@@ -506,6 +506,13 @@ union efi_simple_text_output_protocol { efi_status_t (__efiapi *output_string)(efi_simple_text_output_protocol_t *, efi_char16_t *);void *test_string; +void *query_mode; +void *set_mode; +void *set_attribute; +void *clean_screen; +void *set_cursor_pos; +void *enable_cursor; +void *mode; };struct {u32 reset;
Signature · 1 line
2.41.0
RFC v2 3/9 efi/emulator: Initial rountines to emulate EFI boot time service
In reply to Pingfan LiuFrom: Pingfan Liu <[email protected]>
efi emulator aims to serve the kexec if the kernel wrapped by efistub. It is paired with efistub, so only implements the minimus set of EFI boot service which merely enough boots up efistub.
To simplify the code, the task such as the building of page table etc is shift to the first kernel as more as possible. (This part is implement in the later patch in this series)
To do:
-1. a simple memory allocator
-2. a few extra efi boot service.
--- drivers/firmware/efi/Makefile | 1 + drivers/firmware/efi/efi_emulator/Makefile | 99 +++++ .../firmware/efi/efi_emulator/amba-pl011.c | 81 ++++ .../efi_emulator/arm64_emulator_service.lds | 45 +++ .../firmware/efi/efi_emulator/config_table.c | 25 ++ drivers/firmware/efi/efi_emulator/core.c | 376 ++++++++++++++++++ .../firmware/efi/efi_emulator/device_handle.c | 138 +++++++ drivers/firmware/efi/efi_emulator/earlycon.h | 19 + .../firmware/efi/efi_emulator/efi_emulator.S | 12 + drivers/firmware/efi/efi_emulator/emulator.h | 106 +++++ drivers/firmware/efi/efi_emulator/entry.c | 61 +++ drivers/firmware/efi/efi_emulator/head.S | 10 + drivers/firmware/efi/efi_emulator/lib.c | 73 ++++ drivers/firmware/efi/efi_emulator/memory.c | 27 ++ .../firmware/efi/efi_emulator/memory_api.c | 74 ++++ drivers/firmware/efi/efi_emulator/misc.c | 43 ++ drivers/firmware/efi/efi_emulator/pe_loader.c | 173 ++++++++ drivers/firmware/efi/efi_emulator/printf.c | 373 +++++++++++++++++ .../efi/efi_emulator/protocol_device_path.c | 75 ++++ .../protocol_simple_text_output.c | 50 +++ include/linux/efi_emulator.h | 46 +++ include/linux/kexec.h | 2 + 22 files changed, 1909 insertions(+) create mode 100644 drivers/firmware/efi/efi_emulator/Makefile create mode 100644 drivers/firmware/efi/efi_emulator/amba-pl011.c create mode 100644 drivers/firmware/efi/efi_emulator/arm64_emulator_service.lds create mode 100644 drivers/firmware/efi/efi_emulator/config_table.c create mode 100644 drivers/firmware/efi/efi_emulator/core.c create mode 100644 drivers/firmware/efi/efi_emulator/device_handle.c create mode 100644 drivers/firmware/efi/efi_emulator/earlycon.h create mode 100644 drivers/firmware/efi/efi_emulator/efi_emulator.S create mode 100644 drivers/firmware/efi/efi_emulator/emulator.h create mode 100644 drivers/firmware/efi/efi_emulator/entry.c create mode 100644 drivers/firmware/efi/efi_emulator/head.S create mode 100644 drivers/firmware/efi/efi_emulator/lib.c create mode 100644 drivers/firmware/efi/efi_emulator/memory.c create mode 100644 drivers/firmware/efi/efi_emulator/memory_api.c create mode 100644 drivers/firmware/efi/efi_emulator/misc.c create mode 100644 drivers/firmware/efi/efi_emulator/pe_loader.c create mode 100644 drivers/firmware/efi/efi_emulator/printf.c create mode 100644 drivers/firmware/efi/efi_emulator/protocol_device_path.c create mode 100644 drivers/firmware/efi/efi_emulator/protocol_simple_text_output.c create mode 100644 include/linux/efi_emulator.h
drivers/firmware/efi/Makefile +1 -0
@@ -22,6 +22,7 @@ obj-$(CONFIG_EFI_VARS_PSTORE) += efi-pstore.o obj-$(CONFIG_UEFI_CPER) += cper.o cper_cxl.o obj-$(CONFIG_EFI_RUNTIME_WRAPPERS) += runtime-wrappers.o subdir-$(CONFIG_EFI_STUB) += libstub +obj-$(CONFIG_ARCH_SELECTS_KEXEC_PEIMAGE) += efi_emulator/ obj-$(CONFIG_EFI_BOOTLOADER_CONTROL) += efibc.o obj-$(CONFIG_EFI_TEST) += test/ obj-$(CONFIG_EFI_DEV_PATH_PARSER) += dev-path-parser.o
drivers/firmware/efi/efi_emulator/Makefile new file +99 -0
99 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/amba-pl011.c new file +81 -0
81 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/arm64_emulator_service.lds new file +45 -0
45 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/config_table.c new file +25 -0
@@ -0,0 +1,25 @@ +//SPDX-License-Identifier: GPL-2.0 + +#include "emulator.h" + +static efi_status_t conjoin_memreserve_table (void *table,efi_config_table_t *head) +{ +struct linux_efi_memreserve *new, *next; + + new = (struct linux_efi_memreserve *)table; + new->next =0 ; + next = (struct linux_efi_memreserve *)head->table; +while (next->next !=0 ) + next = (struct linux_efi_memreserve *)next->next; + next->next = (phys_addr_t )new; + +return EFI_SUCCESS ; +} + +efi_status_t conjoin_table (efi_guid_t *uuid,void *table,efi_config_table_t *t) +{ +if (!efi_guidcmp (t->guid,LINUX_EFI_MEMRESERVE_TABLE_GUID )) +return conjoin_memreserve_table (table, t); + +return EFI_OUT_OF_RESOURCES ; +}
drivers/firmware/efi/efi_emulator/core.c new file +376 -0
376 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/device_handle.c new file +138 -0
138 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/earlycon.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#include <linux/efi_emulator.h> +#include <asm/processor.h> +#include <asm/io.h> +#include <asm-generic/io.h> + +struct earlycon { +bool (*match)(struct efi_emulator_param *param,const char *name); +int (*put_str)(const char *str,void *data); +void (*reset)(void *data); +void *data; +const char *name; +}; + +extern struct earlycon pl011; + +extern int pl011_puts (const char *str); +void setup_earlycon (struct efi_emulator_param *param);
drivers/firmware/efi/efi_emulator/efi_emulator.S new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + + .section .rodata, "a" + + .align 8 +_efi_emulator_start: + .globl _efi_emulator_start + .incbin "drivers/firmware/efi/efi_emulator/emulator.raw" + + .align 8 +_efi_emulator_end: + .globl _efi_emulator_end
drivers/firmware/efi/efi_emulator/emulator.h new file +106 -0
106 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/entry.c new file +61 -0
61 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/head.S new file +10 -0
@@ -0,0 +1,10 @@ +//SPDX-License-Identifier: GPL-2.0 +.section ".head.text","ax" + +/* x0 holds the physical address of emulator_param */ +emulator_entry: + ldr x1, [x0] + mov sp, x1 + adrp x2, emulator_main + add x2, x2, #:lo12:emulator_main + br x2
drivers/firmware/efi/efi_emulator/lib.c new file +73 -0
73 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/memory.c new file +27 -0
@@ -0,0 +1,27 @@ +//SPDX-License-Identifier: GPL-2.0 +#include "emulator.h" + +char *heap_start, *heap_end, *heap_cur; + +void initialize_heap (struct efi_emulator_param *param) +{ + heap_start = (char *)param->mempool_start; + heap_end = heap_start + param->mempool_sz; + heap_cur = heap_start; +} + +//2do, the memory management is more complicated since we need to distinguish EFI_BOOT_SERVICE, RUNTIME, LOADER memory descr + +void *aligned_alloc (size_t alignment,size_t size) +{ +char *p; + + p = (char *)ALIGN ((unsigned long )heap_cur, alignment); + heap_cur = p + size; + +//todo, update the efi_memory_desc to include this page, if it crosses the PAGE boundary +//as EFI_BOOT_SERVICE, +return p; +} + +
drivers/firmware/efi/efi_emulator/memory_api.c new file +74 -0
74 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/misc.c new file +43 -0
43 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/pe_loader.c new file +173 -0
173 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/printf.c new file +373 -0
373 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/protocol_device_path.c new file +75 -0
75 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/protocol_simple_text_output.c new file +50 -0
50 lines not on this page · raw message
include/linux/efi_emulator.h new file +46 -0
46 lines not on this page · raw message
include/linux/kexec.h +2 -0
@@ -325,6 +325,7 @@ struct kimage { unsigned int hotplug_support:1;#endif +bool is_pe;#ifdef ARCH_HAS_KIMAGE_ARCH struct kimage_arch arch;#endif
@@ -462,6 +463,7 @@ static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gstatic inline void arch_kexec_pre_free_pages (void *vaddr,unsigned int pages) { }#endif +extern phys_addr_t arch_emulator_prepare_pgtable (struct kimage *kimage);extern bool kexec_file_dbg_print; #define kexec_dprintk(fmt, arg...) \
Signature · 1 line
2.41.0
RFC v2 4/9 efi/emulator: Turn on mmu for arm64
In reply to Pingfan LiuOn arm64, when kexec switches to the new kernel, the mmu is off. So the efi emulator should turn on the mmu to enable the identity mapping at the first stage.
In fact, the kexec switching can run with mmu-on if it enters emulator, but that requires the re-arrangement of the relocate_kernel.S [1]. After that, this patch can be drop.
But let us focus on emulator itself and keep things simple for the time being.
[1]: https://lore.kernel.org/linux-arm-kernel/[email protected]/
--- drivers/firmware/efi/efi_emulator/Makefile | 2 +- .../firmware/efi/efi_emulator/arm64_proc.S | 175 ++++++++++++++++++ drivers/firmware/efi/efi_emulator/entry.c | 7 + 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 drivers/firmware/efi/efi_emulator/arm64_proc.S
drivers/firmware/efi/efi_emulator/Makefile +1 -1
@@ -65,7 +65,7 @@ emulator-y := head.o entry.o \ core.o pe_loader.o memory.o memory_api.o config_table.o misc.o \ device_handle.o protocol_simple_text_output.o protocol_device_path.o \ lib.o printf.o \ - amba-pl011.o + amba-pl011.o arm64_proc.o obj-y := efi_emulator.o
drivers/firmware/efi/efi_emulator/arm64_proc.S new file +175 -0
175 lines not on this page · raw message
drivers/firmware/efi/efi_emulator/entry.c +7 -0
@@ -13,6 +13,13 @@ extern void enable_sctlr_el1(unsigned long scratch_reg);static void arch_handle_mmu (struct efi_emulator_param *param) {if (!param->mmu_on && param->pgd_root) { +unsigned long scratch_reg =0 ; +// in fact, we need SYM_FUNC_START(__cpu_setup), later, set SCTLR_EL1 +// At present, the mmu is not ON +write_sysreg (param->pgd_root, ttbr0_el1); +isb (); +/* scratch_reg asks the C compiler to save x0 */ +enable_sctlr_el1 (scratch_reg); } }
Signature · 1 line
2.41.0
RFC v2 5/9 kexec: Introduce kexec_pe_image to parse and load PE file
In reply to Pingfan LiuAs UEFI becomes popular, a few architectures support to boot a PE format kernel image directly. And the internal of PE format may be different. This introduces a new kexec_file_ops implementation, named pe_image_ops, which prepares UEFI environment for the trampoline code 'efi emulator'.
The pe_image_ops considers efi emulator and its input parameters, 'efi_emulator_param' as two additional kexec_segment. And it constructs efi_memory_desc_t[], encodes efi runtime service info inside the parameter buffer. Finally, it asks architecture implement's page table routine to set up identity map for all memory used in 'efi emulator'
To do: This is a POC version, at present, it aims for arm64, later, it needs abstraction to cope with x86
--- include/linux/kexec.h | 1 + kernel/Makefile | 1 + kernel/kexec_pe_image.c | 503 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 505 insertions(+) create mode 100644 kernel/kexec_pe_image.c
include/linux/kexec.h +1 -0
@@ -385,6 +385,7 @@ static inline int machine_kexec_post_load(struct kimage *image) { return 0; }extern struct kimage *kexec_image;extern struct kimage *kexec_crash_image; +extern const struct kexec_file_ops pe_image_ops;bool kexec_load_permitted (int kexec_image_type);
kernel/Makefile +1 -0
@@ -74,6 +74,7 @@ obj-$(CONFIG_KEXEC_CORE) += kexec_core.o obj-$(CONFIG_CRASH_DUMP) += crash_core.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o +obj-$(CONFIG_ARCH_SELECTS_KEXEC_PEIMAGE) += kexec_pe_image.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o
kernel/kexec_pe_image.c new file +503 -0
503 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC v2 6/9 arm64: kexec: Introduce a new member param_mem to kimage_arch
In reply to Pingfan Liurelocate_kernel will be used either to boot vmlinux directly or PE image. In the latter case, the efi emulator needs efi_emulator_param instead of dtb as input parameter. On the other hand, dtb is still required to pass down through efi_emulator_param to the second kernel, so keep kimage_arch->dtb_mem as scratch and introduce another member 'param_mem'
When booting vmlinux, param_mem equals dtb_mem.
--- arch/arm64/include/asm/kexec.h | 1 + arch/arm64/kernel/asm-offsets.c | 2 +- arch/arm64/kernel/machine_kexec.c | 2 +- arch/arm64/kernel/machine_kexec_file.c | 1 + arch/arm64/kernel/relocate_kernel.S | 2 +- 5 files changed, 5 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kexec.h +1 -0
@@ -109,6 +109,7 @@ int machine_kexec_post_load(struct kimage *image);struct kimage_arch {void *dtb;phys_addr_t dtb_mem; +phys_addr_t param_mem;phys_addr_t kern_reloc;phys_addr_t el2_vectors;phys_addr_t ttbr0;
arch/arm64/kernel/asm-offsets.c +1 -1
@@ -187,7 +187,7 @@ int main(void)BLANK ();#endif #ifdef CONFIG_KEXEC_CORE - DEFINE(KIMAGE_ARCH_DTB_MEM, offsetof(struct kimage, arch.dtb_mem)); + DEFINE(KIMAGE_ARCH_PARAM_MEM, offsetof(struct kimage, arch.param_mem)); DEFINE(KIMAGE_ARCH_EL2_VECTORS, offsetof(struct kimage, arch.el2_vectors)); DEFINE(KIMAGE_ARCH_ZERO_PAGE, offsetof(struct kimage, arch.zero_page)); DEFINE(KIMAGE_ARCH_PHYS_OFFSET, offsetof(struct kimage, arch.phys_offset));
arch/arm64/kernel/machine_kexec.c +1 -1
@@ -192,7 +192,7 @@ void machine_kexec(struct kimage *kimage)cpu_install_idmap (); restart = (void *)__pa_symbol (cpu_soft_restart); -restart (is_hyp_nvhe (), kimage->start, kimage->arch.dtb_mem, +restart (is_hyp_nvhe (), kimage->start, kimage->arch.param_mem,0 ,0 ); }else {void (*kernel_reloc)(struct kimage *kimage);
arch/arm64/kernel/machine_kexec_file.c +1 -0
@@ -184,6 +184,7 @@ int load_other_segments(struct kimage *image, goto out_err; image->arch.dtb = dtb; image->arch.dtb_mem = kbuf.mem; + image->arch.param_mem = image->arch.dtb_mem;kexec_dprintk ("Loaded dtb at 0x%lx bufsz=0x%lx memsz=0x%lx\n" , kbuf.mem, kbuf.bufsz, kbuf.memsz);
arch/arm64/kernel/relocate_kernel.S +1 -1
@@ -44,7 +44,7 @@ SYM_CODE_START(arm64_relocate_new_kernel) */ ldr x28, [x0, #KIMAGE_START] ldr x27, [x0, #KIMAGE_ARCH_EL2_VECTORS] - ldr x26, [x0, #KIMAGE_ARCH_DTB_MEM] + ldr x26, [x0, #KIMAGE_ARCH_PARAM_MEM] /* Setup the list loop variables. */ ldr x18, [x0, #KIMAGE_ARCH_ZERO_PAGE] /* x18 = zero page for BBM */
Signature · 1 line
2.41.0
RFC v2 7/9 arm64: mm: Change to prototype of
In reply to Pingfan LiuThe current allocator of __create_pgd_mapping_locked() takes only one parameter, which reflects the current level of page table.
But kexec_page_alloc() should extra control information from the parameter kimage. In order to make kexec_page_alloc() as an allocator, re-shape the prototype of the allocator and hence of the __create_pgd_mapping_locked().
--- arch/arm64/include/asm/mmu.h | 6 ++++ arch/arm64/mm/mmu.c | 67 ++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 29 deletions(-)
arch/arm64/include/asm/mmu.h +6 -0
@@ -64,6 +64,12 @@ extern void arm64_memblock_init(void);extern void paging_init (void );extern void bootmem_init (void );extern void __iomem *early_io_map (phys_addr_t phys,unsigned long virt); +extern void __create_pgd_mapping_locked (pgd_t *pgdir,phys_addr_t phys, +unsigned long virt,phys_addr_t size, +pgprot_t prot, +phys_addr_t (*pgtable_alloc)(int ,void *), +void *arg, +int flags);extern void create_mapping_noalloc (phys_addr_t phys,unsigned long virt,phys_addr_t size,pgprot_t prot); extern void create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
arch/arm64/mm/mmu.c +38 -29
226 lines not on this page · raw message
Signature · 1 line
2.41.0
RFC v2 8/9 arm64: kexec: Prepare page table for emulator
In reply to Pingfan LiuThe emulator will run in identity mapping, and the first kernel prepares for it. The special allocator resorts to kimage_alloc_control_pages(), which can avoid the allocation on the spot of the destination and overwritten during copying the kernel.
The identity mapping covers only all of the kexec segments and efi runtime service. This reduces the memory consumption of page table.
--- arch/arm64/kernel/machine_kexec.c | 101 ++++++++++++++++++++++++++++-- include/linux/kexec.h | 5 +- 2 files changed, 101 insertions(+), 5 deletions(-)
arch/arm64/kernel/machine_kexec.c +97 -4
@@ -14,6 +14,7 @@#include <linux/reboot.h> #include <linux/set_memory.h> #include <linux/smp.h> +#include <linux/efi_emulator.h> #include <asm/cacheflush.h> #include <asm/cpu_ops.h>
@@ -86,8 +87,22 @@ static void kexec_segment_flush(const struct kimage *kimage) } } +/* todo: alloc page for the pgtable used by efi emulator in crashkernel range */ +static phys_addr_t crash_page_alloc (int unused,void *arg) +{ +struct kimage *kimage = (struct kimage *)arg; +int i; + +//skip kimage->segment[].mem +for (i =0 ; i < kimage->nr_segments; i ++) { +//seg = &kimage->segment[i]; + } +//skip any range allocated +return -1 ; +} +/* Allocates pages for kexec page table */ -static void *kexec_page_alloc (void *arg) +static void *__kexec_page_alloc (void *arg) {struct kimage *kimage = arg;struct page *page =kimage_alloc_control_pages (kimage,0 );
@@ -102,6 +117,82 @@ static void *kexec_page_alloc(void *arg)return vaddr; } +static phys_addr_t kexec_page_alloc (int unused,void *arg) +{ +void *vaddr; + + vaddr =__kexec_page_alloc (arg); +if (!vaddr) +return (phys_addr_t)-1 ; +return virt_to_phys (vaddr); +} + +/* +* This function should be called after all kimage segments have been profiled +* Return physical address of page table's root +*/ +phys_addr_t arch_emulator_prepare_pgtable (struct kimage *kimage, +struct efi_emulator_param *param) +{ +efi_memory_desc_t *md; +struct kexec_segment *seg; +unsigned long paddr, vaddr, sz; +pgd_t *pgd; +typedef phys_addr_t (*alloc_fn )(int ,void *); +alloc_fn alloc; +phys_addr_t pgd_paddr; +int i; + +/* +* Set up pgtable of emulator, either for crash or for reboot. +* All of the segments have been profiled, and kimage_alloc_normal_control_pages() +* will allocate page in safe zone. +* On the other hand, these pages are not in any segment, which means they are +* left, not copied. Hence the radix tree laying out on them is not broken. +*/ +if (kimage->head &IND_DONE ) + alloc = crash_page_alloc; +else + alloc = kexec_page_alloc; + pgd_paddr =alloc (0 , kimage); + pgd = (pgd_t *)phys_to_virt (pgd_paddr); +for (i =0 ; i < kimage->nr_segments; i ++) { + seg = &kimage->segment[i]; + paddr =ALIGN_DOWN (seg->mem,PAGE_SIZE ); + sz =ALIGN (seg->mem - paddr + seg->memsz,PAGE_SIZE ); +kexec_dprintk ("Set up mapping for phyaddr: 0x%lx, size:0x%lx" , paddr, sz); +//todo: distinguish executable segment +__create_pgd_mapping_locked (pgd, paddr, paddr, sz, +PAGE_KERNEL_EXEC , alloc, kimage,0 ); + } + +/* +* UEFI stub can call EFI runtime service either before or after one-shot +* SetVirtualAddressMap(). That means the mapping for +* EFI_RUNTIME_SERVICES_CODE/_DATA should be set up here. +* And the virtual address range occupied by md must be reserved, +* accordingly, its physical address should not be allocated by kexec +* allocator +*/ +for_each_efi_memory_desc (md) { +if (md->attribute &EFI_MEMORY_RUNTIME ) { + vaddr = md->virt_addr; + paddr = md->phys_addr; + sz = md->num_pages *EFI_PAGE_SIZE ; +kexec_dprintk ("Set up mapping for md phyaddr: 0x%lx, virt: 0x%lx, size:0x%lx" , paddr, vaddr, sz); +__create_pgd_mapping_locked (pgd, paddr, vaddr, sz, +PAGE_KERNEL_EXEC , alloc, kimage,0 ); + } + } + +if (param->print_enabled) +__create_pgd_mapping_locked (pgd, param->earlycon_reg_base, + param->earlycon_reg_base, param->earlycon_reg_sz, +pgprot_device (PAGE_KERNEL ), alloc, kimage,0 ); + +return pgd_paddr; +} +int machine_kexec_post_load (struct kimage *kimage) {int rc;
@@ -109,7 +200,7 @@ int machine_kexec_post_load(struct kimage *kimage)void *reloc_code =page_to_virt (kimage->control_code_page);long reloc_size;struct trans_pgd_info info = { - .trans_alloc_page = kexec_page_alloc, + .trans_alloc_page = __kexec_page_alloc, .trans_alloc_arg = kimage, };
@@ -129,7 +220,7 @@ int machine_kexec_post_load(struct kimage *kimage) }/* Create a copy of the linear map */ - trans_pgd =kexec_page_alloc (kimage); + trans_pgd =__kexec_page_alloc (kimage);if (!trans_pgd)return -ENOMEM ; rc =trans_pgd_create_copy (&info, &trans_pgd,PAGE_OFFSET ,PAGE_END );
@@ -145,6 +236,7 @@ int machine_kexec_post_load(struct kimage *kimage) &kimage->arch.t0sz, reloc_code);if (rc)return rc; + kimage->arch.phys_offset =virt_to_phys (kimage) - (long )kimage;/* Flush the reloc_code in preparation for its execution. */
@@ -175,7 +267,6 @@ void machine_kexec(struct kimage *kimage) "Some CPUs may be stale, kdump will be unreliable.\n");pr_info ("Bye!\n" ); -local_daif_mask ();/*
@@ -192,6 +283,7 @@ void machine_kexec(struct kimage *kimage)cpu_install_idmap (); restart = (void *)__pa_symbol (cpu_soft_restart); +/* kimage->start can be either the entry of kernel or efi emulator */ restart (is_hyp_nvhe (), kimage->start, kimage->arch.param_mem,0 ,0 ); }else {
@@ -201,6 +293,7 @@ void machine_kexec(struct kimage *kimage)__hyp_set_vectors (kimage->arch.el2_vectors);cpu_install_ttbr0 (kimage->arch.ttbr0, kimage->arch.t0sz); kernel_reloc = (void *)kimage->arch.kern_reloc; +//tell between the emulator and normal kernel inside the relocate code kernel_reloc (kimage); }
include/linux/kexec.h +4 -1
@@ -22,6 +22,7 @@#include <uapi/linux/kexec.h> #include <linux/verification.h> +#include <linux/efi_emulator.h> extern note_buf_t __percpu *crash_notes;
@@ -464,7 +465,9 @@ static inline int arch_kexec_post_alloc_pages(void *vaddr, unsigned int pages, gstatic inline void arch_kexec_pre_free_pages (void *vaddr,unsigned int pages) { }#endif -extern phys_addr_t arch_emulator_prepare_pgtable (struct kimage *kimage); +extern phys_addr_t arch_emulator_prepare_pgtable (struct kimage *kimage, +struct efi_emulator_param *param); +extern bool kexec_file_dbg_print; #define kexec_dprintk(fmt, arg...) \
Signature · 1 line
2.41.0
RFC v2 9/9 arm64: kexec: Enable kexec_pe_image
In reply to Pingfan LiuNow, anything is ready, enable PE image loading on arm64
--- arch/arm64/Kconfig | 4 ++++ arch/arm64/kernel/machine_kexec_file.c | 3 +++ 2 files changed, 7 insertions(+)
arch/arm64/Kconfig +4 -0
@@ -1550,6 +1550,10 @@ config ARCH_SELECTS_KEXEC_FILE depends on KEXEC_FILE select HAVE_IMA_KEXEC if IMA +config ARCH_SELECTS_KEXEC_PEIMAGE + def_bool y + depends on KEXEC_FILE + config ARCH_SUPPORTS_KEXEC_SIG def_bool y
arch/arm64/kernel/machine_kexec_file.c +3 -0
@@ -24,6 +24,9 @@const struct kexec_file_ops *const kexec_file_loaders[] = { &kexec_image_ops, +#ifdef CONFIG_ARCH_SELECTS_KEXEC_PEIMAGE + &pe_image_ops, +#endifNULL };
Signature · 1 line
2.41.0
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
Lennart
--
Lennart Poettering, Berlin
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Quoted 5 lines
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
It is a pity that it is a common issue persistent with kexec-reboot kernel nowadays.
I am not familiar with TPM and have no clear idea for the time being. (emulating Platform Configuration Registers ?). But since this emulator is held inside a linux kernel image, and the UKI's signature is checked during kexec_file_load. All of them are safe from modification, this security is not an urgent issue.
Thanks for sharing your thoughts and insights.
Best Regards,
Pingfan
Quoted 5 lines
Lennart
--
Lennart Poettering, Berlin
Quoted 24 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Quoted 9 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Hi Pingfan,
If more and more stuff needs coming in, not only the limited boot services then it will be way too complicated and hard to maintain and debug, also the two kexec code paths are duplicated somehow. It is really bad..
I forgot why we can not just extract the kernel from UKI and then load it directly, if the embedded kernel is also signed it should be good?
Thanks
Dave
Quoted 36 lines · Dave Young
Quoted 24 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Quoted 9 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Quoted 7 lines
Hi Pingfan,
If more and more stuff needs coming in, not only the limited boot services then it will be way too complicated and hard to maintain and debug, also the two kexec code paths are duplicated somehow. It is really bad..
OK, I will try to keep things easier. And what do you mean about " two kexec code paths"?
Quoted 3 lines
I forgot why we can not just extract the kernel from UKI and then load it directly, if the embedded kernel is also signed it should be good?
I think the main concern is about the signature.
Thanks,
Pingfan
Quoted 40 lines · Pingfan Liu
Quoted 38 lines · Dave Young
Quoted 24 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Quoted 9 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Hi Pingfan,
Quoted 7 lines
Quoted 5 lines
If more and more stuff needs coming in, not only the limited boot services then it will be way too complicated and hard to maintain and debug, also the two kexec code paths are duplicated somehow. It is really bad..
OK, I will try to keep things easier. And what do you mean about " two kexec code paths"?
I mean we have the EFI and non-EFI kexec implementation. Also for the EFI kexec code for X86 there is something we passed from 1st kernel to 2nd kernel due to no EFI firmware phase, anyway this part can be cleaned up if the emulator can be done gracefully.
Quoted 6 lines
Quoted 4 lines
I forgot why we can not just extract the kernel from UKI and then load it directly, if the embedded kernel is also signed it should be good?
I think the main concern is about the signature.
I thought for the minimum case of kdump, we may just live with kernel
signed only and leave the initrd/cmdline unsigned. Anyway for kexec
reboot it is a problem.
Quoted 5 lines
Thanks,
Pingfan
Hi Dave,
Quoted 2 lines
I forgot why we can not just extract the kernel from UKI and then load it directly, if the embedded kernel is also signed it should be good?
The problem is that in the basic usecase for UKI you only sign the entire UKI PE file and not the included kernel, because you only want that kernel to be run with that one initrd and that one kernel cmdline.
So at a minimum you have to have the signature on the whole UKI checked by the kernel and than have the kernel extract UKI into its parts unless you somehow want to extent trust into userspace to have a helper program do that.
That's what my UKI support implementation from last year did.
v1: https://lore.kernel.org/lkml/[email protected]/
v2: https://lore.kernel.org/lkml/[email protected]/
v3-wip: https://github.com/Cydox/linux/blob/2908db6d8556fa617298cfb713355edaa9e4b095/arch/x86/kernel/kexec-uki.c
It however also lacks support for the "side-car" files. One option to add them would be to load them using subsequent calls to kexec_file_load with a special flag maybe.
TPM measurements are also not done although they are way easier to implement with this approach as we still have the rest of the kernel around.
However TPM measurements in this case would be implemented by the kexec loader in the kernel not by the UKI deciding what to measure. So we would have to have a very firm agreement on what to measure.
Going the UEFI emulator route gives the UKI format (and other (future) formats) way more flexibility. The cost is to potentially implementing a large portion of the UEFI spec, especially if the goal is to support future unknown formats which IIRC was one of the reasons this approach was suggested.
Kind regards,
Jan
Quoted 9 lines · Jan Hendrik Farr
Hi Dave,
Quoted 2 lines
I forgot why we can not just extract the kernel from UKI and then load it directly, if the embedded kernel is also signed it should be good?
The problem is that in the basic usecase for UKI you only sign the entire UKI PE file and not the included kernel, because you only want that kernel to be run with that one initrd and that one kernel cmdline.
Hmm, as replied to Pinfan I thought that both the included kernel and UKI can be signed, and for kdump case kexec_file_load can be used simply.
Quoted 4 lines
So at a minimum you have to have the signature on the whole UKI checked by the kernel and than have the kernel extract UKI into its parts unless you somehow want to extent trust into userspace to have a helper program do that.
extend trust into userspace is hard, previously when Vivek created the kexec_file_load this has been explored and he gave up this option. :(
Pingfan, nice to see you have something done as POC at least, and good to see this topic is live. I just have some worries about the complexity of the emulator though.
Thanks
Dave
Quoted 15 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 7 lines
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Yeah, UKI addons, as well as credential files, and sysext/confext DDIs.
The addons are the most interesting btw, because we load them into memory as PE files, and ask the UEFI to authenticate them.
Quoted 8 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
It doesn't have to be VFAT btw. It just has to be something. For example, it might suffice to take these files, pack them up as cpio or so and pass them along with the UEFI execution. The UEFI emulator would then have to expose them as a file system then.
We are not talking of a bazillion of files here, it's mostly a smallish number of sidecar files I'd expect.
Quoted 5 lines
Quoted 4 lines
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
Quoted 7 lines
It is a pity that it is a common issue persistent with kexec-reboot kernel nowadays.
I am not familiar with TPM and have no clear idea for the time being. (emulating Platform Configuration Registers ?). But since this emulator is held inside a linux kernel image, and the UKI's signature is checked duringkexec_file_load. All of them are safe from modification, this security is not an urgent issue.
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
Lennart
--
Lennart Poettering, Berlin
Quoted 41 lines · Lennart Poettering
Quoted 15 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 7 lines
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Yeah, UKI addons, as well as credential files, and sysext/confext DDIs.
The addons are the most interesting btw, because we load them into memory as PE files, and ask the UEFI to authenticate them.
Quoted 8 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Quoted 8 lines
It doesn't have to be VFAT btw. It just has to be something. For example, it might suffice to take these files, pack them up as cpio or so and pass them along with the UEFI execution. The UEFI emulator would then have to expose them as a file system then.
We are not talking of a bazillion of files here, it's mostly a smallish number of sidecar files I'd expect.
Yes, I think about using <key, value>, where key is the file path, value is the file content.
Quoted 13 lines
Quoted 5 lines
Quoted 4 lines
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
Quoted 7 lines
It is a pity that it is a common issue persistent with kexec-reboot kernel nowadays.
I am not familiar with TPM and have no clear idea for the time being. (emulating Platform Configuration Registers ?). But since this emulator is held inside a linux kernel image, and the UKI's signature is checked duringkexec_file_load. All of them are safe from modification, this security is not an urgent issue.
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
OK, I will look into the details of TPM to see how to bail out.
Thanks,
Pingfan
Quoted 66 lines · Pingfan Liu
Quoted 41 lines · Lennart Poettering
Quoted 15 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 7 lines
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Yeah, UKI addons, as well as credential files, and sysext/confext DDIs.
The addons are the most interesting btw, because we load them into memory as PE files, and ask the UEFI to authenticate them.
Quoted 8 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
Quoted 8 lines
It doesn't have to be VFAT btw. It just has to be something. For example, it might suffice to take these files, pack them up as cpio or so and pass them along with the UEFI execution. The UEFI emulator would then have to expose them as a file system then.
We are not talking of a bazillion of files here, it's mostly a smallish number of sidecar files I'd expect.
Yes, I think about using <key, value>, where key is the file path, value is the file content.
Quoted 13 lines
Quoted 12 lines
Quoted 4 lines
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
It is a pity that it is a common issue persistent with kexec-reboot kernel nowadays.
I am not familiar with TPM and have no clear idea for the time being. (emulating Platform Configuration Registers ?). But since this emulator is held inside a linux kernel image, and the UKI's signature is checked duringkexec_file_load. All of them are safe from modification, this security is not an urgent issue.
Quoted 8 lines
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
OK, I will look into the details of TPM to see how to bail out.
This issue is why I thought a different approach to the UEFI emulator might be useful:
(1) On "kexec -l" execute the EFI binary inside the kernel as a kthread until it exits boot services and record all TPM measurements into a buffer
(2) On "kexec -e" use the kernels tpm driver to actually perform all the prerecorded measurements.
(3) Transition into a "purgatory" that will
clean up the address space to make sure we get to an identity mapping.
(4) Return control to the EFI app at the point it exited boot services
Additional advantage is that we have filesystem access during (1) so it's simple to load additional sidecar files for the UKI.
I have two questions:
1: Does the systemd-stub only perform measurements before exiting boot services or also afterwards?
2: Is it okay to just go to an identity mapping when boot services are exited or is the identidy mapping actually required for the entire execution of the EFI app (I know the UEFI spec calls for this, but I think it should be possible to clean up the address space in a purgatory)?
I played around with this approach last year and got the start of the kernels EFI stub executing in a kthread and calling into provided boot services, but the difficult part is memory allocation and cleaning up the address space in a purgatory.
Quoted 5 lines
Thanks,
Pingfan
Kind regards,
Jan
Quoted 4 lines · Jan Hendrik Farr
I have two questions:
1: Does the systemd-stub only perform measurements before exiting boot services or also afterwards?
Nope. we pass control to the kernel's own stub, and that calls EBS(), not systemd-stub. Hence, no, we are just measuring a things before EBS(), not after.
Lennart
--
Lennart Poettering, Berlin
Quoted 62 lines · Lennart Poettering
Quoted 15 lines · Pingfan Liu
Quoted 13 lines · Lennart Poettering
Quoted 9 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
Quoted 7 lines
Quoted 6 lines
Hmm, systemd's systemd-stub code tries to load certain "side-car" files placed next to the UKI, via the UEFI file system APIs. What's your intention with the UEFI emulator regarding that? The sidecars are somewhat important, because that's how we parameterize otherwise strictly sealed, immutable UKIs.
IIUC, you are referring to UKI addons.
Yeah, UKI addons, as well as credential files, and sysext/confext DDIs.
The addons are the most interesting btw, because we load them into memory as PE files, and ask the UEFI to authenticate them.
Quoted 8 lines
Quoted 3 lines
Hence, what's the story there? implement some form of fs driver (for what fs precisely?) in the emulator too?
As for addon, that is a missing part in this series. I have overlooked this issue. Originally, I thought that there was no need to implement a disk driver and vfat file system, just preload them into memory, and finally present them through the uefi API. I will take a closer look at it and chew on it.
It doesn't have to be VFAT btw. It just has to be something. For example, it might suffice to take these files, pack them up as cpio or so and pass them along with the UEFI execution. The UEFI emulator would then have to expose them as a file system then.
We are not talking of a bazillion of files here, it's mostly a smallish number of sidecar files I'd expect.
Quoted 5 lines
Quoted 4 lines
And regarding tpm? tpms require drivers and i guess at the moment uefi emulator would run those aren't available anymore? but we really should do a separator measurement then. (also there needs to be some way to pass over measurement log of that measurement?)
Quoted 7 lines
It is a pity that it is a common issue persistent with kexec-reboot kernel nowadays.
I am not familiar with TPM and have no clear idea for the time being. (emulating Platform Configuration Registers ?). But since this emulator is held inside a linux kernel image, and the UKI's signature is checked duringkexec_file_load. All of them are safe from modification, this security is not an urgent issue.
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
At present, my emulator returns false to is_efi_secure_boot(), so systemd-stub does not care about the measurement, and moves on.
Could you enlighten me about how systemd utilizes the measurement? I grepped 'TPM2_PCR_KERNEL_CONFIG', and saw the systemd-stub asks to extend PCR. But where is the value checked? I guess the systemd will hang if the check fails.
Thanks,
Pingfan
Quoted 7 lines · Pingfan Liu
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
Quoted 7 lines
At present, my emulator returns false to
is_efi_secure_boot(), so systemd-stub does not care about the measurement, and moves on.Could you enlighten me about how systemd utilizes the measurement? I grepped '
TPM2_PCR_KERNEL_CONFIG', and saw the systemd-stub asks to extend PCR. But where is the value checked? I guess the systemd will hang if the check fails.
systemd's "systemd-pcrlock" tool will look for measurements like that and generate disk encryption TPM policies from that.
Lennart
--
Lennart Poettering, Berlin
Hi Lennart,
I spent some time understanding the systemd-pcrlock and TPM stuff, and got some idea about it. Could you correct me if I'm wrong? Please see the following comments inlined.
Quoted 13 lines · Lennart Poettering
Quoted 10 lines · Pingfan Liu
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
At present, my emulator returns false to
is_efi_secure_boot(), so systemd-stub does not care about the measurement, and moves on.
Quoted 8 lines
Quoted 4 lines
Could you enlighten me about how systemd utilizes the measurement? I grepped '
TPM2_PCR_KERNEL_CONFIG', and saw the systemd-stub asks to extend PCR. But where is the value checked? I guess the systemd will hang if the check fails.systemd's "systemd-pcrlock" tool will look for measurements like that and generate disk encryption TPM policies from that.
Before kexec reboots to the new kernel
systemd-pcrlock can predict the expected PCR value and store it in the file system.
One thing should be noticed is that PCR value can not be affected.
And kexec rebooting happens. systemd-stub extends the PCR value. When the system is up, systemd checks the real PCR value against the expected value rendered by systemd-pcrlock? If matching, all related policies succeed.
Do I understand correctly?
Thanks,
Pingfan
Quoted 25 lines · Pingfan Liu
Hi Lennart,
I spent some time understanding the systemd-pcrlock and TPM stuff, and got some idea about it. Could you correct me if I'm wrong? Please see the following comments inlined.
Quoted 18 lines · Lennart Poettering
Quoted 14 lines · Pingfan Liu
Quoted 6 lines
Hmm, I'd really think about this with some priority. The measurement stuff should not be an afterthought, it typically has major implications on how you design your transitions, because measurements of some component always need to happen *before* you pass control to it, otherwise they are pointless.
At present, my emulator returns false to
is_efi_secure_boot(), so systemd-stub does not care about the measurement, and moves on.Could you enlighten me about how systemd utilizes the measurement? I grepped '
TPM2_PCR_KERNEL_CONFIG', and saw the systemd-stub asks to extend PCR. But where is the value checked? I guess the systemd will hang if the check fails.
Quoted 7 lines
Quoted 3 lines
systemd's "systemd-pcrlock" tool will look for measurements like that and generate disk encryption TPM policies from that.
Before kexec reboots to the new kernel
systemd-pcrlock can predict the expected PCR value and store it in the file system.
I's a set of PCR values pcrlock predicts, one or more for each PCR. It then compiles a TPM "policy" from that, which is identified by a hash, and that hash is then stored in a TPM "nvindex" (which is a bit of memory a tpm provides).
Quoted 1 line
One thing should be noticed is that PCR value can not be affected.
Well, a kexec *should* affect some PCRs. Replacement of the kernel *must* be visible in the measurement logs somehow, in a predictable fashion.
Quoted 4 lines
And kexec rebooting happens. systemd-stub extends the PCR value. When the system is up, systemd checks the real PCR value against the expected value rendered by systemd-pcrlock? If matching, all related policies succeed.
Well, it's not systemd that checks that, but the TPM. i.e. not the untrusted OS but the the suppedly more trusted TPM.
So, key is that we want that measurements take place, the kexec operation *must* be made visible in the measurement logs. But it must be in a well-defined way, and ideally as an extension of the measurements sd-stub currently makes.
(BTW, I personally don't think emulating EFI is really that important. As long as we get the key functionality that sd-stub provides also when doing kexec I am happy. i.e. whether it is sd-stub that does this or some other piece of code doesn't really matter to me. What I do care about is that we can parameterize the invoked kernel in a similar fashion as we can parameterize sd-stub, and that the measurements applied are also equivalent.)
Lennart
--
Lennart Poettering, Berlin
Ard Biesheuvel
maintainerQuoted 93 lines · Pingfan Liu
*** Background ***
As more PE format kernel images are introduced, it post challenge to kexec to cope with the new format.
In my attempt to add support for arm64 zboot image in the kernel [1], Ard suggested using an emulator to tackle this issue. Last year, when Jan tried to introduce UKI support in the kernel [2], Ard mentioned the emulator approach again [3]
After discussion, Ard's approach seems to be a more promising solution to handle PE format kernels once and for all. This series follows that approach and implements an emulator to emulate EFI boot time services, allowing the efistub kernel to self-extract and boot.
Another year has passed, and UKI kernel is more and more frequently used in product. I think it is time to pay effort to resolve this issue.
*** Overview of implement ***
The whole model consits of three parts:-1. The emulator
It is a self-relocatable PIC code, which is finally linked into kernel, but not export any internal symbol to kernel. It mainly contains: a PE file parser, which loads PE format kernel, a group of functions to emulate efi boot service.-2. inside kernel, PE-format loader
Its main task is to set up two extrakexec_segment, one for emulator, the other for passing information from the first kernel to emulator.-3. set up identity mapping only for the memory used by the emulator. Here it relies on
kimage_alloc_control_pages()to get pages, which will not stamped during the process of kexec relocate (cp from src to dst). And since the mapping only covers a small range of memory, it cost small amount memory.*** To do ***
Currently, it only works on arm64 virt machine. For x86, it needs some slightly changes. (I plan to do it in the next version)
Also, this series does not implement a memory allocator, which I plan to implement with the help of bitmap.
About console, currently it hard code for arm64 virt machine, later it should extract the information through ACPI table.
For kdump code, it is not implmented yet. But it should share the majority of this series.
*** Test of this series ***
I have tested this series on arm64 virt machine. There I booted the vmlinuz.efi andkexec_file_loada UKI image, then switch to the second kernel.I used a modified kexec-tools [4], which just skips the check of the file format and passes the file directly to kernel.
[1]: https://lore.kernel.org/linux-arm-kernel/[email protected]/T/#m42abb0ad3c10126b8b3bfae8a596deb707d6f76e
[2]: https://lore.kernel.org/lkml/20230918173607.421d2616@rotkaeppchen/T/
[3]: https://lore.kernel.org/lkml/20230918173607.421d2616@rotkaeppchen/T/#mc60aa591cb7616ceb39e1c98f352383f9ba6e985
[4]: https://github.com/pfliu/kexec-tools.git branch:kexec_uefi_emulatorRFCv1 -> RFCv2:
-1.Support to run UKI kernel by: addLoadImage()andStartImage(), add
PE file relocation support, addInstallMultiProtocol()
-2.Also set up idmap for EFI runtime memory descriptor since UKI's
systemd-stub calls runtime service
-3.Movekexec_pe_image.cfromarch/arm64/kerneltokernel/, since it
aims to provide a more general architecture support.RFCv1: https://lore.kernel.org/linux-efi/[email protected]/
RFCv2: https://github.com/pfliu/linux.git branchkexec_uefi_emulator_RFCv2Cc Ard Biesheuvel <[email protected]>Cc Jan Hendrik Farr <[email protected]>Cc Philipp Rudo <[email protected]>Cc Lennart Poettering <[email protected]>Cc Jarkko Sakkinen <[email protected]>Cc Eric Biederman <[email protected]>Cc Baoquan He <[email protected]>Cc Dave Young <[email protected]>Cc Mark Rutland <[email protected]>Cc Will Deacon <[email protected]>Cc Catalin Marinas <[email protected]>
Quoted 11 lines
Pingfan Liu (9):
efi/libstub: Askefi_random_alloc()to skip unusable memory efi/libstub: Completeefi_simple_text_output_protocolefi/emulator: Initial rountines to emulate EFI boot time service efi/emulator: Turn on mmu for arm64
kexec: Introducekexec_pe_imageto parse and load PE file
arm64: kexec: Introduce a new memberparam_memtokimage_arch
arm64: mm: Change to prototype of
arm64: kexec: Prepare page table for emulator
arm64: kexec: Enablekexec_pe_image
Thanks for putting this RFC together. This is useful work, and gives us food for thought and discussion.
There are a few problems that become apparent when going through these changes.
1. Implementing UEFI entirely is intractable, and unnecessary. Implementing the subset of UEFI that is actually needed to boot Linux *is* tractable, though, but we need to work together to write this down somewhere.
- the EFI stub needs the boot services for the EFI memory map and
the allocation routines
- GRUB needs block I/O
- systemd-stub/UKI needs file I/O to look for sidecars
- etc etc
I implemented a Rust 'efiloader' crate a while ago that encapsulates most of this (it can boot Linux/arm64 on QEMU and boot x86 via GRUB in user space **). Adding file I/O to this should be straight-forward - as Lennart points out, we only need the protocol, it doesn't need to be backed by an actual file system, it just needs to be able to expose other files in the right way.
2. Running the UEFI emulator on bare metal is not going to scale. Cloning UART driver code and MMU code etc is a can of worms that you want to leave closed. And as Lennart points out, there is other hardware (TPM) that needs to be accessible as well. Providing a separate set of drivers for all hardware that the EFI emulator may need to access is not a tractable problem either.
The fix for this, as I see it, is to run the EFI emulator in user space, to the point where the payload calls ExitBootServices(). This will allow all I/O and memory protocol to be implemented trivially, using C library routines. I have a crude prototype** of this running to the point where ExitBootServices() is called (and then it crashes). The tricky yet interesting bit here is how we migrate a chunk of user space memory to the bare metal context that will be created by the kexec syscall later (in which the call to ExitBootServices() would return and proceed with the boot). But the principle is rather straight-forward, and would permit us, e.g., to kexec an OS installer too.
3. We need to figure out how to support TPM and PCRs in the context of kexec. This is a fundamental issue with verified boot, given that the kexec PCR state is necessarily different from the boot state, and so we cannot reuse the TPM directly if we want to pretend that we are doing an ordinary boot in kexec. The alternative is to leave the TPM in a state where the kexec kernel can access its sealed secrets, and mock up the TCG2 EFI protocols using a shim that sits between the TPM hardware (as the real TCG2 protocols will be long gone) and the EFI payload. But as I said, this is a fundamental issue, as the ability to pretend that a kexec boot is a pristine boot would mean that verified boot is broken.
As future work, I'd like to propose to collaborate on some alignment regarding a UEFI baseline for Linux, i.e., the parts that we actually need to boot Linux.
For this series in particular, I don't see a way forward where we adopt this approach, and carry all this code inside the kernel.
Thanks.
Ard.
Quoted 1 line · Ard Biesheuvel
[...]
Hi Ard,
Thanks for sharing your insight and thoughts.
Quoted 23 lines
Thanks for putting this RFC together. This is useful work, and gives us food for thought and discussion.
There are a few problems that become apparent when going through these changes.
1. Implementing UEFI entirely is intractable, and unnecessary. Implementing the subset of UEFI that is actually needed to boot Linux *is* tractable, though, but we need to work together to write this down somewhere.
- the EFI stub needs the boot services for the EFI memory map and
the allocation routines
- GRUB needs block I/O
- systemd-stub/UKI needs file I/O to look for sidecars
- etc etcI implemented a Rust 'efiloader' crate a while ago that encapsulates most of this (it can boot Linux/arm64 on QEMU and boot x86 via GRUB in user space **). Adding file I/O to this should be straight-forward - as Lennart points out, we only need the protocol, it doesn't need to be backed by an actual file system, it just needs to be able to expose other files in the right way.
Quoted 3 lines
2. Running the UEFI emulator on bare metal is not going to scale. Cloning UART driver code and MMU code etc is a can of worms that you want to leave closed. And as Lennart points out, there is other
As for MMU code, if the 1st kernel does not turn it off, it can be eliminated from the emulator code, which should not be hard to implement on arm64. And already done in x86.
Quoted 8 lines
hardware (TPM) that needs to be accessible as well. Providing a separate set of drivers for all hardware that the EFI emulator may need to access is not a tractable problem either.
The fix for this, as I see it, is to run the EFI emulator in user space, to the point where the payload calls
ExitBootServices(). This will allow all I/O and memory protocol to be implemented trivially, using C library routines. I have a crude prototype** of this running
Yes, that is a definitely promising method, By this way, we can handle device operations more elegantly. In fact, I used it to develop and debug part of my emulator service code.
But when debugging x86 efi-stub, I encounter some problem with the privileged instruction, which causes segment fault. It originates from kaslr_get_random_long(). I think it can be worked around by emulating the instruction if the instruction reads the system state. But if the instruction tries to update system state, it can not be fixed since the system is still owned by the kernel instead of owned by the emulator exclusively.
So here we need another agreement on the stub's behavior before ExitBootServices().
Quoted 8 lines
to the point where
ExitBootServices()is called (and then it crashes). The tricky yet interesting bit here is how we migrate a chunk of user space memory to the bare metal context that will be created by the kexec syscall later (in which the call toExitBootServices()would return and proceed with the boot). But the principle is rather straight-forward, and would permit us, e.g., to kexec an OS installer too.
Quoted 5 lines
3. We need to figure out how to support TPM and PCRs in the context of kexec. This is a fundamental issue with verified boot, given that the kexec PCR state is necessarily different from the boot state, and so we cannot reuse the TPM directly if we want to pretend that we are doing an ordinary boot in kexec. The alternative is to leave the TPM
Here, I miss the big picture. Could you enlighten me more about this? As I thought, the linux kernel will not lock itself down onto a specific firmware. So the trust is one direction, i.e. from bootloader to kernel. In UKI case, systemd-stub takes the measurement and extends the PCR 11/12/13 as in
https://uapi-group.org/specifications/specs/linux_tpm_pcr_registry/
Later systemd-pcrlock appraises the value in those registers. If the sections in UKI are intact, the kexec reboot will go smoothly.
Quoted 8 lines
in a state where the kexec kernel can access its sealed secrets, and mock up the TCG2 EFI protocols using a shim that sits between the TPM hardware (as the real TCG2 protocols will be long gone) and the EFI payload. But as I said, this is a fundamental issue, as the ability to pretend that a kexec boot is a pristine boot would mean that verified boot is broken.
Quoted 4 lines
As future work, I'd like to propose to collaborate on some alignment regarding a UEFI baseline for Linux, i.e., the parts that we actually need to boot Linux.
Do you mean that user space code and kernel code? And I think for the user space code, it should be better to integrate the code in kexec-tools so that we have a uniform interface for kexec boot.
Looking forward to the collaboration to make kexec able to boot UKI soon.
Thanks,
Pingfan
Hi Ard,
Hi Jan,
On Wed, 28 Aug 2024 19:08:14 +0200
Ard Biesheuvel <[email protected]> wrote:
[...]
Quoted 29 lines
Thanks for putting this RFC together. This is useful work, and gives us food for thought and discussion.
There are a few problems that become apparent when going through these changes.
1. Implementing UEFI entirely is intractable, and unnecessary. Implementing the subset of UEFI that is actually needed to boot Linux *is* tractable, though, but we need to work together to write this down somewhere.
- the EFI stub needs the boot services for the EFI memory map and
the allocation routines
- GRUB needs block I/O
- systemd-stub/UKI needs file I/O to look for sidecars
- etc etcI implemented a Rust 'efiloader' crate a while ago that encapsulates most of this (it can boot Linux/arm64 on QEMU and boot x86 via GRUB in user space **). Adding file I/O to this should be straight-forward - as Lennart points out, we only need the protocol, it doesn't need to be backed by an actual file system, it just needs to be able to expose other files in the right way.
2. Running the UEFI emulator on bare metal is not going to scale. Cloning UART driver code and MMU code etc is a can of worms that you want to leave closed. And as Lennart points out, there is other hardware (TPM) that needs to be accessible as well. Providing a separate set of drivers for all hardware that the EFI emulator may need to access is not a tractable problem either.
Quoted 11 lines
The fix for this, as I see it, is to run the EFI emulator in user space, to the point where the payload calls
ExitBootServices(). This will allow all I/O and memory protocol to be implemented trivially, using C library routines. I have a crude prototype** of this running to the point whereExitBootServices()is called (and then it crashes). The tricky yet interesting bit here is how we migrate a chunk of user space memory to the bare metal context that will be created by the kexec syscall later (in which the call toExitBootServices()would return and proceed with the boot). But the principle is rather straight-forward, and would permit us, e.g., to kexec an OS installer too.
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
That's why I tend more to Jan's suggestion to include the EFI runtime in the kernel. Alas, that comes with it's own problem, as that requires to run code in the kernel that was never intended to run in kernel context. So even when we can trust the code not to be malicious, we cannot trust it to not accidentally change the system state in a way the kernel doesn't expect...
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
Thanks
Philipp
Quoted 23 lines
3. We need to figure out how to support TPM and PCRs in the context of kexec. This is a fundamental issue with verified boot, given that the kexec PCR state is necessarily different from the boot state, and so we cannot reuse the TPM directly if we want to pretend that we are doing an ordinary boot in kexec. The alternative is to leave the TPM in a state where the kexec kernel can access its sealed secrets, and mock up the TCG2 EFI protocols using a shim that sits between the TPM hardware (as the real TCG2 protocols will be long gone) and the EFI payload. But as I said, this is a fundamental issue, as the ability to pretend that a kexec boot is a pristine boot would mean that verified boot is broken.
As future work, I'd like to propose to collaborate on some alignment regarding a UEFI baseline for Linux, i.e., the parts that we actually need to boot Linux.
For this series in particular, I don't see a way forward where we adopt this approach, and carry all this code inside the kernel.
Thanks.
Ard.
Quoted 7 lines · Philipp Rudo
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
BPF would have some guarantees that are favorable such as programs always end, even faulty ones. It always has implicit "ExitBootServices".
Just a remark.
BR, Jarkko
Quoted 9 lines · Jarkko Sakkinen
Quoted 7 lines · Philipp Rudo
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
Quoted 4 lines
BPF would have some guarantees that are favorable such as programs always end, even faulty ones. It always has implicit "ExitBootServices".
Just a remark.
Some days ago I was thinking could some of the kernel functionality be eBPF at least like in formal theory because most of it is amortized, i.e. does a fixed chunk of work. Not going into that rabbit hole but I really like this idea and could be good experimentation ground for such innovation.
BR, Jarkko
Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · Jarkko Sakkinen
Quoted 7 lines · Philipp Rudo
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
BPF would have some guarantees that are favorable such as programs always end, even faulty ones. It always has implicit "ExitBootServices".
Quoted 7 lines
Quoted 1 line
Just a remark.
Some days ago I was thinking could some of the kernel functionality be eBPF at least like in formal theory because most of it is amortized, i.e. does a fixed chunk of work. Not going into that rabbit hole but I really like this idea and could be good experimentation ground for such innovation.
E.g. let's imagine there would imaginary eBPF-TPM driver framework.
How I would go doing that would be to take the existing TPM driver functionality and provide extra functions and resources available for subsystem specific BPF environment, and have the orhestration code as eBPF. I pretty much concluded that there is a chance that such could work out.
Not something in my immediate table but it is still really interesting idea, as instead of using language to separate "safe" and unsafe" regions you would use "VM" environments to create the walls. In the end of the day that would also great venture for Rust in kernel, i.e. compile that BPF from Rust.
Sorry going of the hook the comment triggered me ;-)
BR, Jarkko
Hi Jarkko,
Quoted 30 lines · On Sat, 07 Sep 2024 14:41:38 +0300 "Jarkko Sakkinen" <[email protected]> wrote:
Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · Jarkko Sakkinen
Quoted 7 lines · Philipp Rudo
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
BPF would have some guarantees that are favorable such as programs always end, even faulty ones. It always has implicit "ExitBootServices".
Quoted 7 lines
Quoted 1 line
Just a remark.
Some days ago I was thinking could some of the kernel functionality be eBPF at least like in formal theory because most of it is amortized, i.e. does a fixed chunk of work. Not going into that rabbit hole but I really like this idea and could be good experimentation ground for such innovation.
E.g. let's imagine there would imaginary eBPF-TPM driver framework.
How I would go doing that would be to take the existing TPM driver functionality and provide extra functions and resources available for subsystem specific BPF environment, and have the orhestration code as eBPF. I pretty much concluded that there is a chance that such could work out.
Quoted 7 lines
Not something in my immediate table but it is still really interesting idea, as instead of using language to separate "safe" and unsafe" regions you would use "VM" environments to create the walls. In the end of the day that would also great venture for Rust in kernel, i.e. compile that BPF from Rust.
Sorry going of the hook the comment triggered me ;-)
I'm glad you like the idea :-)
Sounds like an interesting idea you are having there!
Thanks
Philipp
Quoted 41 lines · Philipp Rudo
Hi Jarkko,
Quoted 36 lines · On Sat, 07 Sep 2024 14:41:38 +0300 "Jarkko Sakkinen" <[email protected]> wrote:
Quoted 13 lines · Jarkko Sakkinen
Quoted 12 lines · Jarkko Sakkinen
Quoted 7 lines · Philipp Rudo
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
BPF would have some guarantees that are favorable such as programs always end, even faulty ones. It always has implicit "ExitBootServices".
Quoted 7 lines
Quoted 1 line
Just a remark.
Some days ago I was thinking could some of the kernel functionality be eBPF at least like in formal theory because most of it is amortized, i.e. does a fixed chunk of work. Not going into that rabbit hole but I really like this idea and could be good experimentation ground for such innovation.
E.g. let's imagine there would imaginary eBPF-TPM driver framework.
How I would go doing that would be to take the existing TPM driver functionality and provide extra functions and resources available for subsystem specific BPF environment, and have the orhestration code as eBPF. I pretty much concluded that there is a chance that such could work out.
Not something in my immediate table but it is still really interesting idea, as instead of using language to separate "safe" and unsafe" regions you would use "VM" environments to create the walls. In the end of the day that would also great venture for Rust in kernel, i.e. compile that BPF from Rust.
Quoted 5 lines
Quoted 1 line
Sorry going of the hook the comment triggered me ;-)
I'm glad you like the idea :-)
Sounds like an interesting idea you are having there!
Yeah, if you go forward with this please CC to me any possible follow-ups :-)
BR, Jarkko
Quoted 8 lines · Philipp Rudo
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
May it would be nice to have a way to "zap" userspace away, i.e. allow the kernel to get rid of all processes in some way, reliable. And then simply start a new userspace, from a trusted definition. Or in other
words: if you don't want to trust the usual userspace, then let's maybe just terminate it, and create it anew, with a clean, pristine definition the old userspace cannot get access to.
Quoted 7 lines
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
ebpf is not turing complete, I am not sure how far you will make it with this, in the various implementations of EFI payloads there are plenty of loops, sometimes IO loops, sometimes hash loops of huge data (for measurements). As I understand ebpf is not really compatible such code.
Lennart
--
Lennart Poettering, Berlin
_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec
Quoted 11 lines · Lennart Poettering
Quoted 8 lines · Philipp Rudo
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
Quoted 6 lines
May it would be nice to have a way to "zap" userspace away, i.e. allow the kernel to get rid of all processes in some way, reliable. And then simply start a new userspace, from a trusted definition. Or in other
words: if you don't want to trust the usual userspace, then let's maybe just terminate it, and create it anew, with a clean, pristine definition the old userspace cannot get access to.
Well, this is an interesting idea!
However, I'm sceptical if this could be done in a secure way. How do we ensure that nothing the old userspace did with the various interfaces to the kernel has no impact on the new userspace? Maybe others can chime in on this? Does kernel_lockdown give more guarantees related to this?
Even if this is possible in a secure way, there is a problem with doing this for kernels that are to be kexec'd on kernel panic. In this approach we can't pre-run them until EBS(), so we would rely on the old kernel to still be intact when we want to kexec reboot.
You could do a system where you kexec into an intermediate kernel. That kernel get's kexec'd with a signed initrd that can use the normal kexec_load syscall to load do any kind of preparation in userspace.
Problem: For that intermediate enviroment we already need a format that combines kernel image, initrd, cmdline all signed in one package aka UKI. Was it the chicken or the egg?
But this shows that if we implemented UKIs the easy way (kernel simply checks signature, extracts the pieces, and kexecs them like normal), this approach could always be used to support kexec for other future formats. They could use the kernels UKI support to boot into an intermediate kernel with UEFI implemented in userspace in the initrd.
So basically support UKIs the easy way and use them to be able to securely zap away userspace and start with a fresh kernel and signed userspace as a way to support other UEFI formats that are not UKI.
Quoted 19 lines
Quoted 8 lines
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
ebpf is not turing complete, I am not sure how far you will make it with this, in the various implementations of EFI payloads there are plenty of loops, sometimes IO loops, sometimes hash loops of huge data (for measurements). As I understand ebpf is not really compatible such code.
Lennart
--
Lennart Poettering, Berlin
Hi Lennart,
Hi Jan,
Quoted 26 lines · On Mon, 9 Sep 2024 12:42:45 +0200 Jan Hendrik Farr <[email protected]> wrote:
Quoted 11 lines · Lennart Poettering
Quoted 8 lines · Philipp Rudo
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
Quoted 6 lines
May it would be nice to have a way to "zap" userspace away, i.e. allow the kernel to get rid of all processes in some way, reliable. And then simply start a new userspace, from a trusted definition. Or in other
words: if you don't want to trust the usual userspace, then let's maybe just terminate it, and create it anew, with a clean, pristine definition the old userspace cannot get access to.Well, this is an interesting idea!
However, I'm sceptical if this could be done in a secure way. How do we ensure that nothing the old userspace did with the various interfaces to the kernel has no impact on the new userspace? Maybe others can chime in on this? Does
kernel_lockdowngive more guarantees related to this?
Quoted 4 lines
Even if this is possible in a secure way, there is a problem with doing this for kernels that are to be kexec'd on kernel panic. In this approach we can't pre-run them until
EBS(), so we would rely on the old kernel to still be intact when we want to kexec reboot.
I don't believe there's a way to do that on running kernels. As Jan pointed out, this cannot be done during reboot, as for kdump that would mean to run after a panic. So it would need to run when the new image is loaded. But at that time your user space is running. Plus you also always have a user space component that triggers kexec. So you cannot simply "zap" user space but have to somehow stash it away, run your trusted user space and, then restore the old user space again. That sounds pretty error prone to me. Plus it will tank your performance every time you do a kexec, which for kdump is every boot...
Quoted 13 lines
You could do a system where you kexec into an intermediate kernel. That kernel get's kexec'd with a signed initrd that can use the normal
kexec_loadsyscall to load do any kind of preparation in userspace.
Problem: For that intermediate enviroment we already need a format that combines kernel image, initrd, cmdline all signed in one package aka UKI. Was it the chicken or the egg?But this shows that if we implemented UKIs the easy way (kernel simply checks signature, extracts the pieces, and kexecs them like normal), this approach could always be used to support kexec for other future formats. They could use the kernels UKI support to boot into an intermediate kernel with UEFI implemented in userspace in the initrd.
Quoted 3 lines
So basically support UKIs the easy way and use them to be able to securely zap away userspace and start with a fresh kernel and signed userspace as a way to support other UEFI formats that are not UKI.
Well, in theory that should work. But I see several problems:
1) How does the first kernel tell the intermediate kernel which file(s) with wich command line to load? In fact, how does the first kernel get the information itself? You would need a new system call that takes two kernel images, one for the intermediate and one for the kernel to load,for that.
Of course you could also build the intermediate UKI during kernel build and include it into the image. Similar to what is done with the purgatory. But that would totally bloat the kernel image.
2) I expect that to be extremely painful to debug, if the intermediate kernel runs into a panic. For sure kdump won't work in that case...
3) Distros would need maintain and test the additional UKI.
4) This approach basically needs to boot twice. But there are people out there who fight to reduce boot times extremely hard. For them every millisecond counts. Telling them that they will need to wait twice as long will be very hard to sell.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
Quoted 5 lines
Quoted 5 lines
ebpf is not turing complete, I am not sure how far you will make it with this, in the various implementations of EFI payloads there are plenty of loops, sometimes IO loops, sometimes hash loops of huge data (for measurements). As I understand ebpf is not really compatible such code.
I don't believe we can simply take all those payloads and recompile them to eBPF. There definitely needs to be some refactoring done first. For example the IO loops you can drop for eBPF and simply map to the corresponding kernel function, letting them do the full IO in one go. There will be cases where that will be more difficult like for hash loops when you have to have the same hash at the end. But I believe even for that ways could be found to get it to work.
Anyway, I'm sure that the picture I have in my head is way oversimplified. There will be many pitfalls to handle for sure. Still I believe it would be a nice experiment.
Thanks
Philipp
Ard Biesheuvel
maintainerQuoted 38 lines · Philipp Rudo
Hi Lennart,
Hi Jan,Quoted 26 lines · On Mon, 9 Sep 2024 12:42:45 +0200 Jan Hendrik Farr <[email protected]> wrote:
Quoted 11 lines · Lennart Poettering
Quoted 8 lines · Philipp Rudo
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
Quoted 6 lines
May it would be nice to have a way to "zap" userspace away, i.e. allow the kernel to get rid of all processes in some way, reliable. And then simply start a new userspace, from a trusted definition. Or in other
words: if you don't want to trust the usual userspace, then let's maybe just terminate it, and create it anew, with a clean, pristine definition the old userspace cannot get access to.Well, this is an interesting idea!
However, I'm sceptical if this could be done in a secure way. How do we ensure that nothing the old userspace did with the various interfaces to the kernel has no impact on the new userspace? Maybe others can chime in on this? Does
kernel_lockdowngive more guarantees related to this?Quoted 4 lines
Even if this is possible in a secure way, there is a problem with doing this for kernels that are to be kexec'd on kernel panic. In this approach we can't pre-run them until
EBS(), so we would rely on the old kernel to still be intact when we want to kexec reboot.
Quoted 10 lines
I don't believe there's a way to do that on running kernels. As Jan pointed out, this cannot be done during reboot, as for kdump that would mean to run after a panic. So it would need to run when the new image is loaded. But at that time your user space is running. Plus you also always have a user space component that triggers kexec. So you cannot simply "zap" user space but have to somehow stash it away, run your trusted user space and, then restore the old user space again. That sounds pretty error prone to me. Plus it will tank your performance every time you do a kexec, which for kdump is every boot...
kdump has a kexec kernel 'standby' to launch when the kernel panics. So for the UKI/EFI payload case, this would imply that the load involves running the payload until EBS() and freezing the state.
Whether execution occurs in true user space or in a deprivileged kernel context is an implementation detail, imho. We don't want to run external code in privileged mode inside the kernel in any case, as this would violate lockdown already. But it should be feasible to have a EFI compatible layer in the kernel that invokes the EFI entrypoint of an image in a way that protects the host kernel. This could be user mode on the CPU or perhaps a minimal KVM virtual machine.
The advantage of this approach is that the whole concept of purgatory can be avoided - the EFI boot phase runs in parallel with the previous kernel, which has full control over authentication and [emulated] PCR externsion, and has ultimate control over whether the kexec reboot is permitted.
Quoted 32 lines
Quoted 13 lines
You could do a system where you kexec into an intermediate kernel. That kernel get's kexec'd with a signed initrd that can use the normal
kexec_loadsyscall to load do any kind of preparation in userspace.
Problem: For that intermediate enviroment we already need a format that combines kernel image, initrd, cmdline all signed in one package aka UKI. Was it the chicken or the egg?But this shows that if we implemented UKIs the easy way (kernel simply checks signature, extracts the pieces, and kexecs them like normal), this approach could always be used to support kexec for other future formats. They could use the kernels UKI support to boot into an intermediate kernel with UEFI implemented in userspace in the initrd.
Quoted 3 lines
So basically support UKIs the easy way and use them to be able to securely zap away userspace and start with a fresh kernel and signed userspace as a way to support other UEFI formats that are not UKI.
Well, in theory that should work. But I see several problems:
1) How does the first kernel tell the intermediate kernel which file(s) with wich command line to load? In fact, how does the first kernel get the information itself? You would need a new system call that takes two kernel images, one for the intermediate and one for the kernel to load,for that.
Of course you could also build the intermediate UKI during kernel build and include it into the image. Similar to what is done with the purgatory. But that would totally bloat the kernel image.
2) I expect that to be extremely painful to debug, if the intermediate kernel runs into a panic. For sure kdump won't work in that case...
Quoted 7 lines
3) Distros would need maintain and test the additional UKI.
4) This approach basically needs to boot twice. But there are people out there who fight to reduce boot times extremely hard. For them every millisecond counts. Telling them that they will need to wait twice as long will be very hard to sell.
I don't think intermediate kernels are the solution here. We need to run as much as possible under the control of the preceding kernel, and minimize the bare metal handover that occurs after EBS(). Adding more code to the purgatory (as this series does) is not acceptable to me, as it is extremely difficult to debug, and duplicates drivers and other logic (making it an 'intermediate kernel' of sorts already)
Quoted 23 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Let me throw an other wild idea in the ring. Instead of implementing a EFI runtime we could also include a eBPF version of the stub into the images. kexec could then extract the eBPF program and let it run just like any other eBPF program with all the pros (and cons) that come with it. That won't be as generic as the EFI runtime, e.g. you couldn't simply kexec any OS installer. On the other hand it would make it easier to port UKIs et al. to non-EFI systems. What do you think?
Quoted 5 lines
Quoted 5 lines
ebpf is not turing complete, I am not sure how far you will make it with this, in the various implementations of EFI payloads there are plenty of loops, sometimes IO loops, sometimes hash loops of huge data (for measurements). As I understand ebpf is not really compatible such code.
I don't believe we can simply take all those payloads and recompile them to eBPF. There definitely needs to be some refactoring done first. For example the IO loops you can drop for eBPF and simply map to the corresponding kernel function, letting them do the full IO in one go. There will be cases where that will be more difficult like for hash loops when you have to have the same hash at the end. But I believe even for that ways could be found to get it to work.
Quoted 4 lines
Anyway, I'm sure that the picture I have in my head is way oversimplified. There will be many pitfalls to handle for sure. Still I believe it would be a nice experiment.
Today, UKI functionality is implemented in terms of EFI API calls. Any solution that needs either a parallel implementation (eBPF vs EFI) or needs to unpack the UKI in order to perform the steps that the UKI would perform itself if it were executed in an EFI environment is a no-go in my opinion.
So either we provide some EFI compatible runtime sufficient to run a UKI, or we re-engineer UKI to be built on top of an abstraction that can be implemented straight-forwardly both on system firmware and in the EFI context.
Quoted 7 lines · Ard Biesheuvel
[...]
kdump has a kexec kernel 'standby' to launch when the kernel panics. So for the UKI/EFI payload case, this would imply that the load involves running the payload until
EBS()and freezing the state.
Quoted 7 lines
Whether execution occurs in true user space or in a deprivileged kernel context is an implementation detail, imho. We don't want to run external code in privileged mode inside the kernel in any case, as this would violate lockdown already. But it should be feasible to have a EFI compatible layer in the kernel that invokes the EFI entrypoint of an image in a way that protects the host kernel. This could be user mode on the CPU or perhaps a minimal KVM virtual machine.
This solution is what I'm currently in favor of (besides my original
approach), see: https://lore.kernel.org/kexec/Zt7EbvWjF9WPCYfn@gardel-login/T/#md4f02b7cb6c694cb28aa8d36fe47a02bd4dc17a4
From the perspective of supporting UKIs the EFI compatibility layer definetly has to be run in the kernel not in regular userspace so that the chain of trust is maintained.
Why do you say that it would have to be a deprivileged kernel context though? In lockdown mode we would enforce a signature check on the PE file, so it shouldn't be any different from loading a signed kernel module from a kernel lockdown perspective, right?
Using KVM is actually something I didn't consider yet, however it would not work in VMs without nested virtualization.
Quoted 5 lines
The advantage of this approach is that the whole concept of purgatory can be avoided - the EFI boot phase runs in parallel with the previous kernel, which has full control over authentication and [emulated] PCR externsion, and has ultimate control over whether the kexec reboot is permitted.
I'm under the impression that we still need some code to run between the kernels to do two things:
1. Clean up the address space, so that it's what the EFI binary expects
2. Verify a checksum on the saved state in case a kernel panic is the result of memory corruption
Sure, the majority of the work (running the EFI binary until EBS()) is done while the old kernel is still running.
Quoted 9 lines
[...]
I don't think intermediate kernels are the solution here. We need to run as much as possible under the control of the preceding kernel, and minimize the bare metal handover that occurs after
EBS(). Adding more code to the purgatory (as this series does) is not acceptable to me, as it is extremely difficult to debug, and duplicates drivers and other logic (making it an 'intermediate kernel' of sorts already)
I agree
Quoted 11 lines
Today, UKI functionality is implemented in terms of EFI API calls. Any solution that needs either a parallel implementation (eBPF vs EFI) or needs to unpack the UKI in order to perform the steps that the UKI would perform itself if it were executed in an EFI environment is a no-go in my opinion.
So either we provide some EFI compatible runtime sufficient to run a UKI, or we re-engineer UKI to be built on top of an abstraction that can be implemented straight-forwardly both on system firmware and in the EFI context.
A solution that can boot UKIs as they exist today is definetly the right choice. Rewriting them for eBPF and maintaining two implementations is way less than ideal.
Quoted 19 lines · Jan Hendrik Farr
Quoted 11 lines · Lennart Poettering
Quoted 8 lines · Philipp Rudo
I mostly agree on what you have wrote. But I see a big problem in running the EFI emulator in user space when it comes to secure boot. The chain of trust ends in the kernel. So it's the kernel that needs to verify that the image to be loaded can be trusted. But when the EFI runtime is in user space the kernel simply cannot do that. Which means, if we want to go this way, we would need to extend the chain of trust to user space. Which will be a whole bucket of worms, not just a can.
Quoted 6 lines
May it would be nice to have a way to "zap" userspace away, i.e. allow the kernel to get rid of all processes in some way, reliable. And then simply start a new userspace, from a trusted definition. Or in other
words: if you don't want to trust the usual userspace, then let's maybe just terminate it, and create it anew, with a clean, pristine definition the old userspace cannot get access to.
Quoted 6 lines
Well, this is an interesting idea!
However, I'm sceptical if this could be done in a secure way. How do we ensure that nothing the old userspace did with the various interfaces to the kernel has no impact on the new userspace? Maybe others can chime in on this? Does
kernel_lockdowngive more guarantees related to this?
Yeah, it's not a trivial thing. I.e. I guess things like sysfs and procfs will retain ownership/access mode. sysctls and sysfs attrs are going to retain their most recently written contents and things like that. Synthetic network interfaces, DM devices, loopback devices all would survive this.
So, no idea how realistic this is, but I would *love* it, not only for this purpose here, but also for the "soft-reboot" logic we have in system these days, which shuts down userspace and starts it up again, as a form of super-fast reboot that doesn't replace the kernel. If we could reliably reset sysfs/sysctl/procfs/… during this, this would be really lovely.
Lennart
--
Lennart Poettering, Berlin
Quoted 1 line · Ard Biesheuvel
[...]
Quoted 6 lines
Thanks for putting this RFC together. This is useful work, and gives us food for thought and discussion.
There are a few problems that become apparent when going through these changes.
Quoted 10 lines
1. Implementing UEFI entirely is intractable, and unnecessary. Implementing the subset of UEFI that is actually needed to boot Linux *is* tractable, though, but we need to work together to write this down somewhere.
- the EFI stub needs the boot services for the EFI memory map and
the allocation routines
- GRUB needs block I/O
- systemd-stub/UKI needs file I/O to look for sidecars
- etc etc
I have created a git repo to hold the record for the current status.
[https://github.com/rhkdump/kexec_uefi.git]
And uefi_subset.md records the minimal requirement of uefi.
But I have a question about "GRUB needs block I/O", is it required? As I know, the kernel image e.g. UKI, zboot will be supported. But why should grub be supported too?
Thanks,
Pingfan
Quoted 49 lines
I implemented a Rust 'efiloader' crate a while ago that encapsulates most of this (it can boot Linux/arm64 on QEMU and boot x86 via GRUB in user space **). Adding file I/O to this should be straight-forward - as Lennart points out, we only need the protocol, it doesn't need to be backed by an actual file system, it just needs to be able to expose other files in the right way.
2. Running the UEFI emulator on bare metal is not going to scale. Cloning UART driver code and MMU code etc is a can of worms that you want to leave closed. And as Lennart points out, there is other hardware (TPM) that needs to be accessible as well. Providing a separate set of drivers for all hardware that the EFI emulator may need to access is not a tractable problem either.
The fix for this, as I see it, is to run the EFI emulator in user space, to the point where the payload calls
ExitBootServices(). This will allow all I/O and memory protocol to be implemented trivially, using C library routines. I have a crude prototype** of this running to the point whereExitBootServices()is called (and then it crashes). The tricky yet interesting bit here is how we migrate a chunk of user space memory to the bare metal context that will be created by the kexec syscall later (in which the call toExitBootServices()would return and proceed with the boot). But the principle is rather straight-forward, and would permit us, e.g., to kexec an OS installer too.3. We need to figure out how to support TPM and PCRs in the context of kexec. This is a fundamental issue with verified boot, given that the kexec PCR state is necessarily different from the boot state, and so we cannot reuse the TPM directly if we want to pretend that we are doing an ordinary boot in kexec. The alternative is to leave the TPM in a state where the kexec kernel can access its sealed secrets, and mock up the TCG2 EFI protocols using a shim that sits between the TPM hardware (as the real TCG2 protocols will be long gone) and the EFI payload. But as I said, this is a fundamental issue, as the ability to pretend that a kexec boot is a pristine boot would mean that verified boot is broken.
As future work, I'd like to propose to collaborate on some alignment regarding a UEFI baseline for Linux, i.e., the parts that we actually need to boot Linux.
For this series in particular, I don't see a way forward where we adopt this approach, and carry all this code inside the kernel.
Thanks.
Ard.