Skip to contentSkip to conversation

Allows Secure Boot for Kexec

PATCH In review Frediano Ziglio 4 participants 11 messages May 7, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

v2 cover Allows Secure Boot for Kexec

From: Frediano Ziglio <[email protected]>

Using EFI Secure Boot all kernel level code should be signed and there should be no way to run unchecked code.
For this reason the Kexec interface needs to be changed in order to allows signature checking.

The purgatory code is included in Xen itself as passing this code from userspace it's not secure (see patches 2/4 and 3/4).

Changes since v1:
- update copyright lines;
- better sha2 declarations.

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

 xen/arch/arm/Makefile                 |   1 +
 xen/arch/arm/kexec.c                  |  27 +
 xen/arch/x86/Makefile                 |   2 +
 xen/arch/x86/bzimage.c                |  40 +-
 xen/arch/x86/kexec.c                  | 125 +++++
 xen/arch/x86/purgatory/.gitignore     |   3 +
 xen/arch/x86/purgatory/Makefile       |  64 +++
 xen/arch/x86/purgatory/config.h       |  37 ++
 xen/arch/x86/purgatory/entry64.S      | 108 ++++
 xen/arch/x86/purgatory/purgatory.c    |  59 +++
 xen/arch/x86/purgatory/setup-x86_64.S |  63 +++
 xen/arch/x86/purgatory/stack.S        |  21 +
 xen/common/Kconfig                    |   1 +
 xen/common/kexec.c                    |  33 +-
 xen/common/kimage.c                   | 703 ++++++++++++++++++++++++--
 xen/include/public/kexec.h            |  23 +-
 xen/include/xen/kimage.h              |  57 ++-
 xen/include/xen/sha2.h                |  12 +
 xen/include/xen/x86-linux.h           |  62 +++
 xen/lib/sha2-256.c                    |  19 +-
 20 files changed, 1348 insertions(+), 112 deletions(-)
 create mode 100644 xen/arch/arm/kexec.c
 create mode 100644 xen/arch/x86/kexec.c
 create mode 100644 xen/arch/x86/purgatory/.gitignore
 create mode 100644 xen/arch/x86/purgatory/Makefile
 create mode 100644 xen/arch/x86/purgatory/config.h
 create mode 100644 xen/arch/x86/purgatory/entry64.S
 create mode 100644 xen/arch/x86/purgatory/purgatory.c
 create mode 100644 xen/arch/x86/purgatory/setup-x86_64.S
 create mode 100644 xen/arch/x86/purgatory/stack.S
 create mode 100644 xen/include/xen/x86-linux.h
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 xen/lib: Export additional sha256 functions

From: Ross Lagerwall <[email protected]>

In future, some code needs to generate a digest over several separate buffers so export the necessary functions to do so.

---
 xen/include/xen/sha2.h | 12 ++++++++++++
 xen/lib/sha2-256.c     | 17 ++++++-----------
 2 files changed, 18 insertions(+), 11 deletions(-)
xen/include/xen/sha2.h +12 -0
index 47d97fbf01..cb30e8f8d7 100644
@@ -12,4 +12,16 @@
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];
+    uint8_t buf[64];
+    size_t count; /* Byte count. */
+};
+
+void sha2_256_init(struct sha2_256_state *s);
+void sha2_256_update(struct sha2_256_state *s, const void *msg,
+                     size_t len);
+void sha2_256_final(struct sha2_256_state *s,
+                    uint8_t digest[SHA2_256_DIGEST_SIZE]);
+
 #endif /* XEN_SHA2_H */
xen/lib/sha2-256.c +6 -11
index 19e8252188..e55e297eff 100644
@@ -10,12 +10,6 @@
 #include <xen/string.h>
 #include <xen/unaligned.h>
 
-struct sha2_256_state {
-    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];
-    uint8_t buf[64];
-    size_t count; /* Byte count. */
-};
-
 static uint32_t choose(uint32_t x, uint32_t y, uint32_t z)
 {
     return z ^ (x & (y ^ z));
@@ -131,7 +125,7 @@ static void sha2_256_transform(uint32_t *state, const void *_input)
     state[4] += e; state[5] += f; state[6] += g; state[7] += h;
 }
 
-static void sha2_256_init(struct sha2_256_state *s)
+void sha2_256_init(struct sha2_256_state *s)
 {
     *s = (struct sha2_256_state){
         .state = {
@@ -147,8 +141,8 @@ static void sha2_256_init(struct sha2_256_state *s)
     };
 }
 
-static void sha2_256_update(struct sha2_256_state *s, const void *msg,
-                            size_t len)
+void sha2_256_update(struct sha2_256_state *s, const void *msg,
+                     size_t len)
 {
     unsigned int partial = s->count & 63;
 
@@ -177,9 +171,10 @@ static void sha2_256_update(struct sha2_256_state *s, const void *msg,
     memcpy(s->buf + partial, msg, len);
 }
 
-static void sha2_256_final(struct sha2_256_state *s, void *_dst)
+void sha2_256_final(struct sha2_256_state *s,
+                    uint8_t digest[SHA2_256_DIGEST_SIZE])
 {
-    uint32_t *dst = _dst;
+    uint32_t *dst = (uint32_t *)digest;
     unsigned int i, partial = s->count & 63;
 
     /* Start padding */
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/4 kexec: Include purgatory in Xen

In reply to Frediano Ziglio

From: Ross Lagerwall <[email protected]>

Purgatory is a binary that runs between two kernels during kexec. When Secure Boot is enabled, it should be signed and verified before being loaded and executed.

Currently, purgatory is built as part of kexec-tools and dynamically modified before being loaded. This prevents signing it at build time. To fix this, build purgatory as a separate object in Xen and modify it at load time in Xen itself. This is the same approach used by Linux.

No behavioural change with this patch other than the puratory binary being included in Xen.

---
 xen/arch/x86/Makefile                 |   1 +
 xen/arch/x86/purgatory/.gitignore     |   3 +
 xen/arch/x86/purgatory/Makefile       |  64 +++++++++++++++
 xen/arch/x86/purgatory/config.h       |  37 +++++++++
 xen/arch/x86/purgatory/entry64.S      | 108 ++++++++++++++++++++++++++
 xen/arch/x86/purgatory/purgatory.c    |  59 ++++++++++++++
 xen/arch/x86/purgatory/setup-x86_64.S |  63 +++++++++++++++
 xen/arch/x86/purgatory/stack.S        |  21 +++++
 xen/lib/sha2-256.c                    |   2 +-
 9 files changed, 357 insertions(+), 1 deletion(-)
 create mode 100644 xen/arch/x86/purgatory/.gitignore
 create mode 100644 xen/arch/x86/purgatory/Makefile
 create mode 100644 xen/arch/x86/purgatory/config.h
 create mode 100644 xen/arch/x86/purgatory/entry64.S
 create mode 100644 xen/arch/x86/purgatory/purgatory.c
 create mode 100644 xen/arch/x86/purgatory/setup-x86_64.S
 create mode 100644 xen/arch/x86/purgatory/stack.S
xen/arch/x86/Makefile +1 -0
index c2f1dcf301..b3ee871ba1 100644
@@ -1,6 +1,7 @@
 obj-y += acpi/
 obj-y += boot/
 obj-y += cpu/
+obj-$(CONFIG_KEXEC) += purgatory/
 obj-y += efi/
 obj-y += genapic/
 obj-$(CONFIG_GUEST) += guest/
xen/arch/x86/purgatory/.gitignore new file +3 -0
new file mode 100644
index 0000000000..9353e3ff26
@@ -0,0 +1,3 @@
+purgatory.chk
+kexec-purgatory.S
+purgatory.ro
xen/arch/x86/purgatory/Makefile new file +64 -0

64 lines not on this page · raw message

xen/arch/x86/purgatory/config.h new file +37 -0
new file mode 100644
index 0000000000..f1f9f41aad
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-only
+ *
+ * Copyright (C) 2025  Cloud Software Group Inc.
+ */
+
+#ifndef __PURGATORY_CONFIG_H__
+#define __PURGATORY_CONFIG_H__
+
+#define BALIGN(size) .balign size
+
+#undef ENTRY
+#define ENTRY(name)         \
+    name:
+
+#define END(name)           \
+    name##_end:
+
+#undef GLOBAL
+#define GLOBAL(name)        \
+    .globl name;            \
+    ENTRY(name)
+
+#define GLOBAL_END(name)    \
+    .globl name##_end;      \
+    END(name)
+
+#define SYM_T_DATA 1
+
+#define ASM_INT(name, val)  \
+    .type name, SYM_T_DATA; \
+    BALIGN(4);              \
+    .hidden name;           \
+    GLOBAL(name)            \
+    .long (val);            \
+    .size name, . - name
+
+#endif // __PURGATORY_CONFIG_H__
xen/arch/x86/purgatory/entry64.S new file +108 -0

108 lines not on this page · raw message

xen/arch/x86/purgatory/purgatory.c new file +59 -0

59 lines not on this page · raw message

xen/arch/x86/purgatory/setup-x86_64.S new file +63 -0

63 lines not on this page · raw message

xen/arch/x86/purgatory/stack.S new file +21 -0
new file mode 100644
index 0000000000..6ac7685cd5
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * purgatory:  stack
+ *
+ * Copyright (C) 2014 Red Hat Inc.
+ * Copyright (C) 2025 Cloud Software Group Inc.
+ *
+ *	This code has been adapted from kexec-tools and Linux.
+ */
+
+#include "config.h"
+
+	/* A stack for the loaded kernel.
+	 * Separate and in the data section so it can be prepopulated.
+	 */
+	.data
+
+BALIGN(4096)
+GLOBAL(stack)
+	.skip 4096
+GLOBAL_END(stack)
xen/lib/sha2-256.c +1 -1
index e55e297eff..09c033f97f 100644
@@ -209,7 +209,7 @@ void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
     sha2_256_final(&s, digest);
 }
 
-#ifdef CONFIG_SELF_TESTS
+#if defined(CONFIG_SELF_TESTS) && !defined(__PURGATORY__)
 
 #include <xen/init.h>
 #include <xen/lib.h>
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/4 kexec: Implement new EFI load types

In reply to Frediano Ziglio

From: Ross Lagerwall <[email protected]>

Add two new EFI load types for kexec. These load types are suitable for use when Secure Boot is enabled.

When these load types are used, the caller should not pass purgatory as one of the kexec segments. Instead, Xen will prepare and supply purgatory itself.

Preparing purgatory involves loading it, applying relocations, and calculating and embedding the checksums of the segments.

---
 xen/arch/arm/Makefile       |   1 +
 xen/arch/arm/kexec.c        |  27 ++
 xen/arch/x86/Makefile       |   1 +
 xen/arch/x86/bzimage.c      |  40 +--
 xen/arch/x86/kexec.c        | 125 +++++++
 xen/common/Kconfig          |   1 +
 xen/common/kexec.c          |  10 +
 xen/common/kimage.c         | 632 +++++++++++++++++++++++++++++++++++-
 xen/include/public/kexec.h  |  23 +-
 xen/include/xen/kimage.h    |  42 +++
 xen/include/xen/x86-linux.h |  62 ++++
 11 files changed, 908 insertions(+), 56 deletions(-)
 create mode 100644 xen/arch/arm/kexec.c
 create mode 100644 xen/arch/x86/kexec.c
 create mode 100644 xen/include/xen/x86-linux.h
xen/arch/arm/Makefile +1 -0
index 129a109d6e..0e652d9594 100644
@@ -36,6 +36,7 @@ obj-y += io.o
 obj-$(CONFIG_IOREQ_SERVER) += ioreq.o
 obj-y += irq.o
 obj-y += kernel.init.o
+obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 obj-$(CONFIG_LLC_COLORING) += llc-coloring.o
 obj-$(CONFIG_VM_EVENT) += mem_access.o
xen/arch/arm/kexec.c new file +27 -0
new file mode 100644
index 0000000000..f8847e16e0
@@ -0,0 +1,27 @@
+/*
+ * SPDX-License-Identifier: GPL-2.0-only
+ * Copyright (c) 2025, Cloud Software Group
+ */
+
+#include <xen/types.h>
+#include <xen/errno.h>
+#include <xen/elfstructs.h>
+#include <xen/kimage.h>
+
+int arch_kexec_apply_relocations_add(struct purgatory_info *pi,
+                                     Elf_Shdr *section, const Elf_Shdr *relsec,
+                                     const Elf_Shdr *symtabsec)
+{
+    return -ENOSYS;
+}
+
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/x86/Makefile +1 -0
index b3ee871ba1..6b11364150 100644
@@ -42,6 +42,7 @@ obj-y += hypercall.o
 obj-y += i387.o
 obj-y += i8259.o
 obj-y += io_apic.o
+obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_LIVEPATCH) += alternative.o livepatch.o
 obj-y += msi.o
 obj-y += msr.o
xen/arch/x86/bzimage.c +1 -39
index 66f648f311..f4d5b584cb 100644
@@ -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/kexec.c new file +125 -0

125 lines not on this page · raw message

xen/common/Kconfig +1 -0
index 4bec78c6f2..674c2bace1 100644
@@ -318,6 +318,7 @@ config KEXEC
 	bool "kexec support"
 	default y
 	depends on HAS_KEXEC
+	select CRYPTO
 	help
 	  Allows a running Xen hypervisor to be replaced with another OS
 	  without rebooting. This is primarily used to execute a crash
xen/common/kexec.c +10 -0
index 84fe8c3597..158f8da6fd 100644
@@ -1132,6 +1132,16 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( ret < 0 )
         goto error;
 
+    if ( load.type == KEXEC_TYPE_DEFAULT_EFI ||
+         load.type == KEXEC_TYPE_CRASH_EFI )
+    {
+        ret = kimage_setup_purgatory(kimage, load.parameters);
+        if (ret)
+            return ret;
+    }
+
+    kimage_terminate(kimage);
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;
xen/common/kimage.c +620 -12

704 lines not on this page · raw message

xen/include/public/kexec.h +18 -5
index 40d79e936b..9bc94c6fd6 100644
@@ -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 +42 -0

66 lines not on this page · raw message

xen/include/xen/x86-linux.h new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 4/4 kexec: Support non-page-aligned kexec segments

In reply to Frediano Ziglio

From: Ross Lagerwall <[email protected]>

With Secure Boot, userspace passes in the entire kernel loaded for verification purposes. However, the kernel's startup32 function needs to be aligned (e.g. to 16 MiB) and this results in the start of the segment not being page-aligned (depending on where the startup32 function lands in the kernel binary). Relax this restriction in Xen to support this use case.

---
 xen/common/kexec.c       | 23 +++++++-----
 xen/common/kimage.c      | 81 +++++++++++++++++++++-------------------
 xen/include/xen/kimage.h | 15 +++++++-
 3 files changed, 70 insertions(+), 49 deletions(-)
xen/common/kexec.c +14 -9
index 158f8da6fd..a7b3958c74 100644
@@ -910,7 +910,7 @@ static uint16_t kexec_load_v1_arch(void)
 }
 
 static int kexec_segments_add_segment(unsigned int *nr_segments,
-                                      xen_kexec_segment_t *segments,
+                                      struct kimage_segment *segments,
                                       mfn_t mfn)
 {
     paddr_t maddr = mfn_to_maddr(mfn);
@@ -936,7 +936,7 @@ static int kexec_segments_add_segment(unsigned int *nr_segments,
 
 static int kexec_segments_from_ind_page(mfn_t mfn,
                                         unsigned int *nr_segments,
-                                        xen_kexec_segment_t *segments,
+                                        struct kimage_segment *segments,
                                         bool compat)
 {
     void *page;
@@ -991,7 +991,7 @@ done:
 static int kexec_do_load_v1(xen_kexec_load_v1_t *load, int compat)
 {
     struct kexec_image *kimage = NULL;
-    xen_kexec_segment_t *segments;
+    struct kimage_segment *segments;
     uint16_t arch;
     unsigned int nr_segments = 0;
     mfn_t ind_mfn = maddr_to_mfn(load->image.indirection_page);
@@ -1001,7 +1001,7 @@ static int kexec_do_load_v1(xen_kexec_load_v1_t *load, int compat)
     if ( arch == EM_NONE )
         return -ENOSYS;
 
-    segments = xmalloc_array(xen_kexec_segment_t, KEXEC_SEGMENT_MAX);
+    segments = xmalloc_array(struct kimage_segment, KEXEC_SEGMENT_MAX);
     if ( segments == NULL )
         return -ENOMEM;
 
@@ -1103,9 +1103,10 @@ static int kexec_load_v1_compat(XEN_GUEST_HANDLE_PARAM(void) uarg)
 static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
 {
     xen_kexec_load_t load;
-    xen_kexec_segment_t *segments;
+    struct kimage_segment *segments;
     struct kexec_image *kimage = NULL;
     int ret;
+    unsigned int i;
 
     if ( copy_from_guest(&load, uarg, 1) )
         return -EFAULT;
@@ -1113,14 +1114,18 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( load.nr_segments >= KEXEC_SEGMENT_MAX )
         return -EINVAL;
 
-    segments = xmalloc_array(xen_kexec_segment_t, load.nr_segments);
+    segments = xmalloc_array(struct kimage_segment, load.nr_segments);
     if ( segments == NULL )
         return -ENOMEM;
 
-    if ( copy_from_guest(segments, load.segments.h, load.nr_segments) )
+    for ( i = 0; i < load.nr_segments; i++ )
     {
-        ret = -EFAULT;
-        goto error;
+        if ( copy_from_guest_offset((xen_kexec_segment_t *)&segments[i],
+                                    load.segments.h, i, 1) )
+        {
+            ret = -EFAULT;
+            goto error;
+        }
     }
 
     ret = kimage_alloc(&kimage, load.type, load.arch, load.entry_maddr,
xen/common/kimage.c +43 -38

202 lines not on this page · raw message

xen/include/xen/kimage.h +13 -2
index 6626058f8b..3099b489b5 100644
@@ -30,6 +30,17 @@ struct purgatory_info {
     Elf_Shdr *sechdrs;
 };
 
+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;
+};
+
 typedef struct xen_kexec_regs {
         uint64_t rax;
         uint64_t rbx;
@@ -55,7 +66,7 @@ struct kexec_image {
     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;
@@ -77,7 +88,7 @@ struct kexec_image {
 
 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,
Signature · 1 line
2.43.0
#5 lore raw
In reply to Frediano Ziglio
Quoted 5 lines · Frediano Ziglio

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

I realise a lot of this is coming from kexec-tools and/or Linux, but it looks very very mad.

From patch 1, we're embedding this in Xen:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 30K May  9 15:24 purgatory.ro

yet -Wa,--strip-local-absolute alone halves the size:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 17K May  9 15:25 purgatory.ro

Looking at purgatory itself, we enter at purgatory_start, load a local GDT, set up a local stack, call into C for the hashing (and nothing else), then jmp to entry64...

... which loads a (different) local GDT, (different) local stack, loads the GPRs and then jumps into the new kernel.

Combined with kexec_reloc(), that's 3x we change GDT and stack in several hundred instructions.

Looking further at patch 2, we only set up 3 GPRs; %rip, %rsp and %rdi pointing the parameter block.

Patch 2 also contains an a large amount of EFI-editing logic (all vulnerable to XSA-25), which AFAICT exists only because purgatory is built non-PIC and wants relocating.  I can't see any external references, or anything that couldn't be resolved at link time for a PIC build.

There are two things which purgatory does which Xen doesn't currently cater for:

1) Setting up the GPRs in that manner
2) The digest checks

#1 is very easy to fix and can probably even be done on the current ABI (older Kexecs using purgatory won't care), and #2 ought to be easy too by extending machine_kexec().  We can do the digest checks unconditionally (it's a sensible check irrespective).

I think that removes the majority of this series, with no loss in functionality?

Given that we're leaving the signature check to the dom0 kernel (which is TCB and therefore can in the UEFI-SB model), we just might be able to get away without any hypercall changes at all?

Thoughts?

~Andrew

#6 lore raw
Quoted 51 lines · Andrew Cooper
Quoted 5 lines · Frediano Ziglio

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

I realise a lot of this is coming from kexec-tools and/or Linux, but it looks very very mad.

From patch 1, we're embedding this in Xen:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 30K May  9 15:24 purgatory.ro

yet -Wa,--strip-local-absolute alone halves the size:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 17K May  9 15:25 purgatory.ro

Looking at purgatory itself, we enter at purgatory_start, load a local GDT, set up a local stack, call into C for the hashing (and nothing else), then jmp to entry64...

... which loads a (different) local GDT, (different) local stack, loads the GPRs and then jumps into the new kernel.

Combined with kexec_reloc(), that's 3x we change GDT and stack in several hundred instructions.

Looking further at patch 2, we only set up 3 GPRs; %rip, %rsp and %rdi pointing the parameter block.

Patch 2 also contains an a large amount of EFI-editing logic (all vulnerable to XSA-25), which AFAICT exists only because purgatory is built non-PIC and wants relocating.  I can't see any external references, or anything that couldn't be resolved at link time for a PIC build.

There are two things which purgatory does which Xen doesn't currently cater for:

1) Setting up the GPRs in that manner
2) The digest checks

#1 is very easy to fix and can probably even be done on the current ABI (older Kexecs using purgatory won't care), and #2 ought to be easy too by extending machine_kexec().  We can do the digest checks unconditionally (it's a sensible check irrespective).

Quoted 8 lines

I think that removes the majority of this series, with no loss in functionality?

Given that we're leaving the signature check to the dom0 kernel (which is TCB and therefore can in the UEFI-SB model), we just might be able to get away without any hypercall changes at all?

Thoughts?

Sorry, one extra thing.  By doing the digest check in machine_kexec(), we can also inform the user that the kexec kernel looks corrupt, and we won't be entering it.  (And probably try to reboot rather than hanging too.)

~Andrew

#7 lore raw
In reply to Andrew Cooper
Quoted 45 lines · Andrew Cooper
Quoted 5 lines · Frediano Ziglio

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

I realise a lot of this is coming from kexec-tools and/or Linux, but it looks very very mad.

From patch 1, we're embedding this in Xen:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 30K May 9 15:24 purgatory.ro

yet -Wa,--strip-local-absolute alone halves the size:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 17K May 9 15:25 purgatory.ro

Looking at purgatory itself, we enter at purgatory_start, load a local GDT, set up a local stack, call into C for the hashing (and nothing else), then jmp to entry64...

... which loads a (different) local GDT, (different) local stack, loads the GPRs and then jumps into the new kernel.

Combined with kexec_reloc(), that's 3x we change GDT and stack in several hundred instructions.

Looking further at patch 2, we only set up 3 GPRs; %rip, %rsp and %rdi pointing the parameter block.

Patch 2 also contains an a large amount of EFI-editing logic (all vulnerable to XSA-25), which AFAICT exists only because purgatory is built non-PIC and wants relocating. I can't see any external references, or anything that couldn't be resolved at link time for a PIC build.

There are two things which purgatory does which Xen doesn't currently cater for:

Quoted 8 lines

1) Setting up the GPRs in that manner
2) The digest checks

#1 is very easy to fix and can probably even be done on the current ABI (older Kexecs using purgatory won't care), and #2 ought to be easy too by extending machine_kexec(). We can do the digest checks unconditionally (it's a sensible check irrespective).

I think the problem of #2 is that doing in the purgatory avoids problems like possible memory corruptions. For instance if the host is crashing due to some corruption it could not always be possible to boot the saved kernel.

Quoted 7 lines

I think that removes the majority of this series, with no loss in functionality?

Given that we're leaving the signature check to the dom0 kernel (which is TCB and therefore can in the UEFI-SB model), we just might be able to get away without any hypercall changes at all?

Yes and no. The user space could not provide the purgatory. But if the kernel is providing it, preventing the user space to send it, I suppose it can be done. At this point however the question is how to change the interface provided to userspace for doing it. It could make sense to have the changes in xen/include/public/kexec.h and let the kernel do the rest.

Quoted 3 lines

Thoughts?

~Andrew

Frediano

#8 lore raw
Quoted 47 lines · Frediano Ziglio
Quoted 46 lines · Andrew Cooper
Quoted 5 lines · Frediano Ziglio

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

I realise a lot of this is coming from kexec-tools and/or Linux, but it looks very very mad.

From patch 1, we're embedding this in Xen:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 30K May 9 15:24 purgatory.ro

yet -Wa,--strip-local-absolute alone halves the size:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 17K May 9 15:25 purgatory.ro

Looking at purgatory itself, we enter at purgatory_start, load a local GDT, set up a local stack, call into C for the hashing (and nothing else), then jmp to entry64...

... which loads a (different) local GDT, (different) local stack, loads the GPRs and then jumps into the new kernel.

Combined with kexec_reloc(), that's 3x we change GDT and stack in several hundred instructions.

Looking further at patch 2, we only set up 3 GPRs; %rip, %rsp and %rdi pointing the parameter block.

Patch 2 also contains an a large amount of EFI-editing logic (all vulnerable to XSA-25), which AFAICT exists only because purgatory is built non-PIC and wants relocating. I can't see any external references, or anything that couldn't be resolved at link time for a PIC build.

There are two things which purgatory does which Xen doesn't currently cater for:

1) Setting up the GPRs in that manner
2) The digest checks

Quoted 9 lines
Quoted 5 lines

#1 is very easy to fix and can probably even be done on the current ABI (older Kexecs using purgatory won't care), and #2 ought to be easy too by extending machine_kexec(). We can do the digest checks unconditionally (it's a sensible check irrespective).

I think the problem of #2 is that doing in the purgatory avoids problems like possible memory corruptions. For instance if the host is crashing due to some corruption it could not always be possible to boot the saved kernel.

It doesn't really matter if Xen does the digest check right at the point of exiting, or purgatory does it moments later.

If there's memory corruption anywhere on this path, we're not making it into the crash kernel whomever does the digest check.

Crashing is a best-effort exercise; it's never guaranteed to be successful.

Quoted 7 lines
Quoted 7 lines

I think that removes the majority of this series, with no loss in functionality?

Given that we're leaving the signature check to the dom0 kernel (which is TCB and therefore can in the UEFI-SB model), we just might be able to get away without any hypercall changes at all?

Quoted 6 lines

Yes and no. The user space could not provide the purgatory. But if the kernel is providing it, preventing the user space to send it, I suppose it can be done. At this point however the question is how to change the interface provided to userspace for doing it. It could make sense to have the changes in xen/include/public/kexec.h and let the kernel do the rest.

I'm not really suggesting any change in userpsace/dom0 kernel.  I'm suggesting "we don't need a purgatory blob at all given two simple changes in Xen."

This in turn means (I think) we can drop all the ELF relocation logic.

I'm unsure whether we need new hypercall subopts or not.  Even if we do, I think the result can still be more simple than currently presented.

~Andrew

#9 lore raw
Quoted 80 lines · Andrew Cooper
Quoted 47 lines · Frediano Ziglio
Quoted 46 lines · Andrew Cooper
Quoted 5 lines · Frediano Ziglio

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

I realise a lot of this is coming from kexec-tools and/or Linux, but it looks very very mad.

From patch 1, we're embedding this in Xen:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 30K May 9 15:24 purgatory.ro

yet -Wa,--strip-local-absolute alone halves the size:

xen.git/xen/arch/x86/purgatory$ ls -lah purgatory.ro -rw-r--r-- 1 andrew andrew 17K May 9 15:25 purgatory.ro

Looking at purgatory itself, we enter at purgatory_start, load a local GDT, set up a local stack, call into C for the hashing (and nothing else), then jmp to entry64...

... which loads a (different) local GDT, (different) local stack, loads the GPRs and then jumps into the new kernel.

Combined with kexec_reloc(), that's 3x we change GDT and stack in several hundred instructions.

Looking further at patch 2, we only set up 3 GPRs; %rip, %rsp and %rdi pointing the parameter block.

Patch 2 also contains an a large amount of EFI-editing logic (all vulnerable to XSA-25), which AFAICT exists only because purgatory is built non-PIC and wants relocating. I can't see any external references, or anything that couldn't be resolved at link time for a PIC build.

There are two things which purgatory does which Xen doesn't currently cater for:

1) Setting up the GPRs in that manner
2) The digest checks

Quoted 9 lines
Quoted 5 lines

#1 is very easy to fix and can probably even be done on the current ABI (older Kexecs using purgatory won't care), and #2 ought to be easy too by extending machine_kexec(). We can do the digest checks unconditionally (it's a sensible check irrespective).

I think the problem of #2 is that doing in the purgatory avoids problems like possible memory corruptions. For instance if the host is crashing due to some corruption it could not always be possible to boot the saved kernel.

It doesn't really matter if Xen does the digest check right at the point of exiting, or purgatory does it moments later.

If there's memory corruption anywhere on this path, we're not making it into the crash kernel whomever does the digest check.

Crashing is a best-effort exercise; it's never guaranteed to be successful.

Quoted 7 lines
Quoted 7 lines

I think that removes the majority of this series, with no loss in functionality?

Given that we're leaving the signature check to the dom0 kernel (which is TCB and therefore can in the UEFI-SB model), we just might be able to get away without any hypercall changes at all?

Quoted 6 lines

Yes and no. The user space could not provide the purgatory. But if the kernel is providing it, preventing the user space to send it, I suppose it can be done. At this point however the question is how to change the interface provided to userspace for doing it. It could make sense to have the changes in xen/include/public/kexec.h and let the kernel do the rest.

Quoted 4 lines

I'm not really suggesting any change in userpsace/dom0 kernel. I'm suggesting "we don't need a purgatory blob at all given two simple changes in Xen."

Maybe it was not clear from my previous comment. A change to the userspace interface is needed. One reason is the mentioned purgatory, it cannot be provided by userspace. The other reason is checking the signature, the kernel should be passed verbatim in order to check it.

Which specific 2 changes are you referring to?

Given that the kernel already does the signature, having the kernel providing the purgatory too would be sensible.

Quoted 6 lines

This in turn means (I think) we can drop all the ELF relocation logic.

I'm unsure whether we need new hypercall subopts or not. Even if we do, I think the result can still be more simple than currently presented.

~Andrew

Possibly the Xen interface could stay the same. Still, as explained above, a userspace change is needed.

Frediano

#10 lore raw
In reply to Frediano Ziglio
Quoted 14 lines · Frediano Ziglio

From: Frediano Ziglio <[email protected]>

Using EFI Secure Boot all kernel level code should be signed and there should be no way to run unchecked code.
For this reason the Kexec interface needs to be changed in order to allows signature checking.

The purgatory code is included in Xen itself as passing this code from userspace it's not secure (see patches 2/4 and 3/4).

Changes since v1:
- update copyright lines;
- better sha2 declarations.

Quoted 5 lines

Ross Lagerwall (4):
xen/lib: Export additional sha256 functions
kexec: Include purgatory in Xen
kexec: Implement new EFI load types
kexec: Support non-page-aligned kexec segments

As a general remark: You're sending all of these patches on Ross' behalf, yet none of them has your own S-o-b. It is my understanding that strictly speaking we wouldn't be permitted to take such patches.

Jan