x86/kexec: Implement crash kexec for Secure Boot
cover x86/kexec: Implement crash kexec for Secure Boot
This replaces the previous series
[PATCH v2 0/4] Allows Secure Boot for Kexec
https://lore.kernel.org/xen-devel/[email protected]/
The main feedback last time was that almost all the purgatory code could be removed.
- The digest check is now done in machine_kexec() instead of purgatory
- GPRs are cleared in kexec_reloc.S which shouldn't affect non-EFI kexec
Kevin Lampis (1):
x86: Implement crash kexec for EFI
Ross Lagerwall (1):
Add lockdown mode
xen/arch/x86/bzimage.c | 40 +--- xen/arch/x86/include/asm/machine_kexec.h | 2 +- xen/arch/x86/machine_kexec.c | 10 +- xen/arch/x86/setup.c | 1 + xen/arch/x86/x86_64/kexec_reloc.S | 16 ++ xen/common/Kconfig | 8 + xen/common/Makefile | 1 + xen/common/kernel.c | 4 + xen/common/kexec.c | 41 +++- xen/common/kimage.c | 264 +++++++++++++++++++---- xen/common/lockdown.c | 56 +++++ xen/include/public/kexec.h | 23 +- xen/include/xen/kimage.h | 23 +- xen/include/xen/lockdown.h | 9 + xen/include/xen/x86-linux.h | 62 ++++++ 15 files changed, 460 insertions(+), 100 deletions(-) create mode 100644 xen/common/lockdown.c create mode 100644 xen/include/xen/lockdown.h create mode 100644 xen/include/xen/x86-linux.h
Signature · 1 line
2.52.0
PATCH 1/2 Add lockdown mode
From: Ross Lagerwall <[email protected]>
The intention of lockdown mode is to prevent attacks from a rogue dom0 userspace from compromising the system. Lockdown mode can be controlled by a Kconfig option and a command-line parameter. It is also enabled automatically when Secure Boot is enabled and it cannot be disabled in that case.
--- xen/arch/x86/setup.c | 1 + xen/common/Kconfig | 8 ++++++ xen/common/Makefile | 1 + xen/common/kernel.c | 4 +++ xen/common/lockdown.c | 56 ++++++++++++++++++++++++++++++++++++++ xen/include/xen/lockdown.h | 9 ++++++ 6 files changed, 79 insertions(+) create mode 100644 xen/common/lockdown.c create mode 100644 xen/include/xen/lockdown.h
xen/arch/x86/setup.c +1 -0
@@ -13,6 +13,7 @@#include <xen/kexec.h> #include <xen/keyhandler.h> #include <xen/lib.h> +#include <xen/lockdown.h> #include <xen/multiboot.h> #include <xen/nodemask.h> #include <xen/numa.h>
xen/common/Kconfig +8 -0
@@ -668,4 +668,12 @@ config PM_STATS Enable collection of performance management statistics to aid in analyzing and tuning power/performance characteristics of the system +config LOCKDOWN_DEFAULT + bool "Enable lockdown mode by default" + default n + help + Lockdown mode prevents attacks from a rogue dom0 userspace from + compromising the system. This is automatically enabled when Secure + Boot is enabled. + endmenu
xen/common/Makefile +1 -0
@@ -26,6 +26,7 @@ obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC) += kimage.o obj-$(CONFIG_LIVEPATCH) += livepatch.o livepatch_elf.o obj-$(CONFIG_LLC_COLORING) += llc-coloring.o +obj-y += lockdown.o obj-$(CONFIG_VM_EVENT) += mem_access.o obj-y += memory.o obj-$(CONFIG_VM_EVENT) += monitor.o
xen/common/kernel.c +4 -0
@@ -14,6 +14,7 @@#include <xen/guest_access.h> #include <xen/hypercall.h> #include <xen/hypfs.h> +#include <xen/lockdown.h> #include <xsm/xsm.h> #include <asm/current.h> #include <public/version.h>
@@ -217,6 +218,9 @@ static void __init _cmdline_parse(const char *cmdline)*/ void __init cmdline_parse (const char *cmdline) { +/* Call this early since it affects command-line parsing */ +lockdown_init (cmdline); +if ( opt_builtin_cmdline[0 ] ) {printk ("Built-in command line: %s\n" , opt_builtin_cmdline);
xen/common/lockdown.c new file +56 -0
56 lines not on this page · raw message
xen/include/xen/lockdown.h new file +9 -0
@@ -0,0 +1,9 @@ +#ifndef XEN__LOCKDOWN_H +#define XEN__LOCKDOWN_H + +#include <xen/types.h> + +bool is_locked_down (void ); +void lockdown_init (const char *cmdline); + +#endif /* XEN__LOCKDOWN_H */
Signature · 1 line
2.52.0
PATCH 2/2 x86: Implement crash kexec for EFI
In reply to Kevin LampisThis change adds a new KEXEC_TYPE_CRASH_EFI load type which is suitable for use when Secure Boot is enabled.
When this load type is used, the caller should not pass purgatory as one of the kexec segments. This is because in Secure Boot mode we cannot accept and run arbitrary unsigned code from dom0 userspace. Instead, Xen prepares any necessary intermediary glue code itself.
When Secure Boot/lockdown mode is enabled only the KEXEC_TYPE_CRASH_EFI type will be accepted.
During load a digest of the kexec segments is calculated and stored. The digest is calculated again during kexec_reboot, this removes the need to re-verify the signatures.
--- xen/arch/x86/bzimage.c | 40 +--- xen/arch/x86/include/asm/machine_kexec.h | 2 +- xen/arch/x86/machine_kexec.c | 10 +- xen/arch/x86/x86_64/kexec_reloc.S | 16 ++ xen/common/kexec.c | 41 +++- xen/common/kimage.c | 264 +++++++++++++++++++---- xen/include/public/kexec.h | 23 +- xen/include/xen/kimage.h | 23 +- xen/include/xen/x86-linux.h | 62 ++++++ 9 files changed, 381 insertions(+), 100 deletions(-) create mode 100644 xen/include/xen/x86-linux.h
xen/arch/x86/bzimage.c +1 -39
@@ -6,6 +6,7 @@#include <xen/gunzip.h> #include <xen/decompress.h> #include <xen/libelf.h> +#include <xen/x86-linux.h> #include <asm/bzimage.h> static __init unsigned long output_length (void *image,unsigned long image_len)
@@ -13,45 +14,6 @@ static __init unsigned long output_length(void *image, unsigned long image_len)return *(uint32_t *)(image + image_len -4 ); } -struct __packed setup_header { -uint8_t _pad0[0x1f1 ];/* skip uninteresting stuff */ -uint8_t setup_sects; -uint16_t root_flags; -uint32_t syssize; -uint16_t ram_size; -uint16_t vid_mode; -uint16_t root_dev; -uint16_t boot_flag; -uint16_t jump; -uint32_t header; -#define HDR_MAGIC "HdrS" -#define HDR_MAGIC_SZ 4 -uint16_t version; -#define VERSION (h,l) (((h)<<8) | (l)) -uint32_t realmode_swtch; -uint16_t start_sys; -uint16_t kernel_version; -uint8_t type_of_loader; -uint8_t loadflags; -uint16_t setup_move_size; -uint32_t code32_start; -uint32_t ramdisk_image; -uint32_t ramdisk_size; -uint32_t bootsect_kludge; -uint16_t heap_end_ptr; -uint16_t _pad1; -uint32_t cmd_line_ptr; -uint32_t initrd_addr_max; -uint32_t kernel_alignment; -uint8_t relocatable_kernel; -uint8_t _pad2[3 ]; -uint32_t cmdline_size; -uint32_t hardware_subarch; -uint64_t hardware_subarch_data; -uint32_t payload_offset; -uint32_t payload_length; - }; -static __init int bzimage_check (struct setup_header *hdr,unsigned long len) {if ( len <sizeof (struct setup_header ) )
xen/arch/x86/include/asm/machine_kexec.h +1 -1
@@ -7,7 +7,7 @@extern void kexec_reloc (unsigned long reloc_code,unsigned long reloc_pt,unsigned long ind_maddr,unsigned long entry_maddr, -unsigned long flags); +unsigned long flags,unsigned long rsi);extern const char kexec_reloc_end[];
xen/arch/x86/machine_kexec.c +9 -1
@@ -154,6 +154,13 @@ void machine_kexec(struct kexec_image *image)int i;unsigned long reloc_flags =0 ; +if ( image->type ==KEXEC_TYPE_CRASH_EFI && +kimage_verify_digest (image) !=0 ) + { +printk (XENLOG_ERR "kexec digest failed, won't boot corrupted image\n" ); +for (;;); + } +/* We are about to permenantly jump out of the Xen context into the kexec * purgatory code. We really dont want to be still servicing interupts. */
@@ -198,7 +205,8 @@ void machine_kexec(struct kexec_image *image)kexec_reloc (page_to_maddr (image->control_code_page),page_to_maddr (image->aux_page), - image->head, image->entry_maddr, reloc_flags); + image->head, image->entry_maddr, reloc_flags, + image->efi_boot_params); }int machine_kexec_get (xen_kexec_range_t *range)
xen/arch/x86/x86_64/kexec_reloc.S +16 -0
@@ -33,6 +33,7 @@ FUNC(kexec_reloc, PAGE_SIZE) /* %rdx - indirection page maddr */ /* %rcx - entry maddr (%rbp) */ /* %r8 - flags */ + /* %r9 - boot params (EFI only) */ movq %rcx, %rbp
@@ -75,6 +76,21 @@ FUNC(kexec_reloc, PAGE_SIZE) testq $KEXEC_RELOC_FLAG_COMPAT, %r8 jnz .L_call_32_bit + movq %r9, %rsi /* boot params */ + xor %rdi, %rdi + xor %rax, %rax + xor %rbx, %rbx + xor %rcx, %rcx + xor %rdx, %rdx + xor %r8, %r8 + xor %r9, %r9 + xor %r10, %r10 + xor %r11, %r11 + xor %r12, %r12 + xor %r13, %r13 + xor %r14, %r14 + xor %r15, %r15 + /* Jump to the image entry point */ jmp *%rbp
xen/common/kexec.c +31 -10
102 lines not on this page · raw message
xen/common/kimage.c +222 -42
375 lines not on this page · raw message
xen/include/public/kexec.h +18 -5
@@ -56,15 +56,24 @@/* * Kexec supports two types of operation: * - kexec into a regular kernel, very similar to a standard reboot -* - KEXEC_TYPE_DEFAULT is used to specify this type +* - KEXEC_TYPE_DEFAULT or KEXEC_TYPE_DEFAULT_EFI are used to specify +* this type +* - in case of KEXEC_TYPE_DEFAULT_EFI the first segment will +* point to full kernel to load and entry point will point to +* parameters * - kexec into a special "crash kernel", aka kexec-on-panic -* - KEXEC_TYPE_CRASH is used to specify this type +* - KEXEC_TYPE_CRASH or KEXEC_TYPE_CRASH_EFI are used to specify this +* type +* - see above for differences between KEXEC_TYPE_CRASH and +* KEXEC_TYPE_CRASH_EFI * - parts of our system may be broken at kexec-on-panic time * - the code should be kept as simple and self-contained as possible */ -#define KEXEC_TYPE_DEFAULT 0 -#define KEXEC_TYPE_CRASH 1 +#define KEXEC_TYPE_DEFAULT 0 +#define KEXEC_TYPE_CRASH 1 +#define KEXEC_TYPE_DEFAULT_EFI 2 +#define KEXEC_TYPE_CRASH_EFI 3/* The kexec implementation for Xen allows the user to load two
@@ -195,7 +204,11 @@ typedef struct xen_kexec_load {XEN_GUEST_HANDLE (xen_kexec_segment_t) h;uint64_t _pad; } segments; -uint64_t entry_maddr;/* image entry point machine address. */ +/* image entry point machine address or parameters in case of EFI. */ +union { +uint64_t entry_maddr; +uint64_t parameters; + }; } xen_kexec_load_t;DEFINE_XEN_GUEST_HANDLE (xen_kexec_load_t);
xen/include/xen/kimage.h +21 -2
@@ -11,18 +11,30 @@#include <xen/list.h> #include <xen/mm.h> +#include <xen/sha2.h> #include <public/kexec.h> #define KEXEC_SEGMENT_MAX 16typedef paddr_t kimage_entry_t ; +struct kimage_segment { +union { +XEN_GUEST_HANDLE (const_void) h; +uint64_t _pad; + } buf; +uint64_t buf_size; +uint64_t dest_maddr; +uint64_t dest_size; +unsigned int dest_offset; +}; +struct kexec_image {uint8_t type;uint16_t arch;uint64_t entry_maddr;uint32_t nr_segments; -xen_kexec_segment_t *segments; +struct kimage_segment *segments;kimage_entry_t head;struct page_info *entry_page;
@@ -37,11 +49,16 @@ struct kexec_image {/* Address of next control page to allocate for crash kernels. */ paddr_t next_crash_page; + +uint8_t digest[SHA2_256_DIGEST_SIZE ]; + +/* Address of boot params. Will be loaded into %rsi. For EFI kexec only. */ +uint64_t efi_boot_params; };int kimage_alloc (struct kexec_image **rimage,uint8_t type,uint16_t arch,uint64_t entry_maddr, -uint32_t nr_segments,xen_kexec_segment_t *segment); +uint32_t nr_segments,struct kimage_segment *segment);void kimage_free (struct kexec_image *image);int kimage_load_segments (struct kexec_image *image); struct page_info *kimage_alloc_control_page(struct kexec_image *image,
@@ -52,6 +69,8 @@ mfn_t kimage_entry_mfn(kimage_entry_t *entry, bool compat);unsigned long kimage_entry_ind (kimage_entry_t *entry,bool compat);int kimage_build_ind (struct kexec_image *image,mfn_t ind_mfn,bool compat); +int kimage_efi_setup (struct kexec_image *image,uint64_t parameters); +int kimage_verify_digest (const struct kexec_image *image);#endif /* __ASSEMBLER__ */
xen/include/xen/x86-linux.h new file +62 -0
62 lines not on this page · raw message
Signature · 1 line
2.52.0
Quoted 26 lines · Kevin Lampis
This replaces the previous series
[PATCH v2 0/4] Allows Secure Boot for Kexec
https://lore.kernel.org/xen-devel/[email protected]/The main feedback last time was that almost all the purgatory code could be removed.
- The digest check is now done inmachine_kexec()instead of purgatory
- GPRs are cleared inkexec_reloc.Swhich shouldn't affect non-EFI kexecKevin Lampis (1):
x86: Implement crash kexec for EFIRoss Lagerwall (1):
Add lockdown modexen/arch/x86/bzimage.c | 40 +--- xen/arch/x86/include/asm/machine_kexec.h | 2 +- xen/arch/x86/machine_kexec.c | 10 +- xen/arch/x86/setup.c | 1 + xen/arch/x86/x86_64/kexec_reloc.S | 16 ++ xen/common/Kconfig | 8 + xen/common/Makefile | 1 + xen/common/kernel.c | 4 + xen/common/kexec.c | 41 +++- xen/common/kimage.c | 264 +++++++++++++++++++---- xen/common/lockdown.c | 56 +++++
Quoted 8 lines
xen/include/public/kexec.h | 23 +- xen/include/xen/kimage.h | 23 +- xen/include/xen/lockdown.h | 9 + xen/include/xen/x86-linux.h | 62 ++++++ 15 files changed, 460 insertions(+), 100 deletions(-) create mode 100644 xen/common/lockdown.c create mode 100644 xen/include/xen/lockdown.h create mode 100644 xen/include/xen/x86-linux.h
Thankyou for doing this work. This is a far more palatable diff-stat than adding all of Purgatory.
But, the lockdown work is still blocked on some open questions and (in practice) the security statement. It only matters for the -EPERMs in the top levels of the hypercall, and that comes from a bad split of patches in the XenServer patchqueue. You'll want to drop it from this series.
The rest of the work (just patch 2) needs splitting into several.
First, moving the thing called setup_header wants to be it's own patch. It wants to move into the existing bzimage.h (itself fixing a bug with the copyright header) and probably wants to be renamed to bzimage_header because, despite that being it's Linux name, "setup" is far too generic.
You have identified that bzimage is x86 specific, and for that reason it should not be included in common/kexec.c. We are going to need an architecture specific way of figuring out the alignment, but looking at kimage_find_kernel_entry_maddr(), why can't we just use the EFI header?
Doing the digest checks should be its own patch. Why is it tied to KEXEC_TYPE_CRASH_EFI? It's applicable to all kexec types. This ought to be first first in the series so it's not tied to EFI.
As to performing and failing the digest checks, it probably wants to be a little earlier. Functions like kexec_crash() want to return rather than hanging, so hwdom_shutdown() can fall through into reboot_or_halt(). kexec_exec() wants to return propagate an error back into the hypercall to identify that the image appears to be corrupt.
Failing the digest check should print both the expected and obtained values. See check_xen_buildid() for a related example that you should follow.
Swapping xen_kexec_segment_t to struct kimage_segment wants to be it's own patch too. It's mechanical but needs to not be mixed in with logical changes.
For the %rsi/boot_params pointer, that wants to be a separate patch too. I wouldn't label it as EFI-only, nor as rsi. Maybe just "arg"? It should be wired up on the 32bit path where it's %esi too, so really you just need to move it out of %r9 before the 32bit split.
Zeroing the other registers is fine, but again wants to be a separate patch. It's unrelated to the rest of the content, and wants to happen on both paths. Also, when zeroing registers, always use the 32bit register form even in 64bit code. See PUSH_AND_CLEAR_GPRS; there are some 64bit CPUs which care.
There are a bunch of style issues too, but lets focus on getting the structure right first.
~Andrew
Quoted 2 lines
You have identified that bzimage is x86 specific, and for that reason it should not be included in
common/kexec.c.
I believe Ross moved the definition of setup_header out of kexec.c because kimage.c also needs to use it now.
Quoted 3 lines
We are going to need an
architecture specific way of figuring out the alignment, but looking atkimage_find_kernel_entry_maddr(), why can't we just use the EFI header?
"Going to need" like "it would be nice" or "we absolutely can't merge this"? :) I feel like I might get bludgeoned to death by a set of bagpipes if I have to tell a certain someone that there's more kexec work to do...
Quoted 3 lines
Why is it [digest checks] tied to
KEXEC_TYPE_CRASH_EFI? It's applicable to all kexec types. This ought to be first first in the series so it's not tied to EFI.
I can do KEXEC_TYPE_CRASH_EFI and KEXEC_TYPE_CRASH but there's an extra layer of complexity with normal kexec.
kimage_load_normal_segment() copies the segments data into temporary, non-contiguous buffers and then sets up a relocation table which kexec_reloc.S uses to copy the segments data into the correct place before jumping to the new kernel. Calculating and verifying the digest for normal kexec would mean new code to walk the relocation table to find every temporary buffer.
As we discussed internally I'd rather not touch the normal kexec code unless I'm fixing all the existing issues and can actually test the changes.
I've sorted out all the other things you mentioned but still in the process of splitting the patches up.
Quoted 4 lines · Kevin Lampis
Quoted 2 lines
You have identified that bzimage is x86 specific, and for that reason it should not be included in
common/kexec.c.I believe Ross moved the definition of
setup_headerout ofkexec.cbecausekimage.calso needs to use it now.
Moving the definition is fine. But it's still x86 specific and cannot be in common code, because you'll break ARM, RISC-V and PPC by doing so.
Quoted 5 lines
Quoted 4 lines
We are going to need an
architecture specific way of figuring out the alignment, but looking atkimage_find_kernel_entry_maddr(), why can't we just use the EFI header?"Going to need" like "it would be nice" or "we absolutely can't merge this"?
The latter. But really it depends on "why can't we just use the EFI header?"
The EFI header is common across architectures, so if we can use that, its easy. If we can't use the EFI header and are forced to inspect the bzImage header, then getting the adequate alignment needs abstracting properly for the common <-> arch split.
Quoted 11 lines
Quoted 3 lines
Why is it [digest checks] tied to
KEXEC_TYPE_CRASH_EFI? It's applicable to all kexec types. This ought to be first first in the series so it's not tied to EFI.I can do
KEXEC_TYPE_CRASH_EFIandKEXEC_TYPE_CRASHbut there's an extra layer of complexity with normal kexec.
kimage_load_normal_segment()copies the segments data into temporary, non-contiguous buffers and then sets up a relocation table whichkexec_reloc.Suses to copy the segments data into the correct place before jumping to the new kernel. Calculating and verifying the digest for normal kexec would mean new code to walk the relocation table to find every temporary buffer.
Oh - we can calculate the digest easily when it's loaded, but we can't check the digest easily at kexec time while we've still got the ability to fail the action.
In which case lets do it only for the crash types in the short term. They're the important ones to check.
It's ok to "return 0; /* TODO, non-crash types */" or equivalent.
~Andrew