Skip to contentSkip to conversation

x86/kexec: Secure Boot support

PATCH Superseded Kevin Lampis 4 participants 23 messages Jun 9 – Jun 18

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover x86/kexec: Secure Boot support

These changes allow loading and booting an EFI kexec crash kernel.

For this new EFI kexec type, userspace won't provide purgatory code. Instead Xen jumps directly to the new kernel.

Signature validation is done by the dom0 kernel. Xen then calculates a digest for all the segments stored in memory. This digest is verified again during execution. This digest check obviates the need for Xen to verify Secure Boot signatures directly.

Link to v1: (I should have changed the title and/or version sorry)
https://lore.kernel.org/xen-devel/[email protected]/

Changes in v2:
- Removed dependency on lockdown mode
- Split work into multiple patches
- Move setup_header to appropriate header file bzimage.h
- Renamed setup_header to less generic name bzimage_header
- I couldn't find a way to use the EFI header instead of setup_header
- Extend digest check to non-EFI crash type
- Do digest check earlier for cleaner failure path
- Print expected vs actual digest on failure
- Swap xen_kexec_segment_t to struct kimage_segment in separate patch
- Plumb boot params into assembly code in separate patch
- Rename rsi parameter to arg
- Remove references to "EFI" in boot params assembly code
- Move %rsi assignment earlier so it is valid for the 32bit path too
- Zero general purpose registers in separate patch
- Zero 32bit version of GPRs for performance and correctness
- Zero GPRs on 32bit kexec path too

Ross Lagerwall (5):
x86/kexec: add digest checks
x86/kexec: rename setup_header to bzimage_header x86/kexec: add new struct kimage_segment x86/kexec: Support non-page-aligned kexec segments x86/kexec: Implement new EFI load types

Kevin Lampis (2):
x86/kexec: Pass boot params directly to new kernel x86/kexec: Zero general purpose registers

 xen/arch/x86/bzimage.c                   |  47 +---
 xen/arch/x86/include/asm/bzimage.h       |  44 ++++
 xen/arch/x86/include/asm/machine_kexec.h |   2 +-
 xen/arch/x86/machine_kexec.c             |   3 +-
 xen/arch/x86/x86_64/kexec_reloc.S        |  24 ++
 xen/common/kexec.c                       |  42 +++-
 xen/common/kimage.c                      | 268 +++++++++++++++++++----
 xen/include/public/kexec.h               |  11 +-
 xen/include/xen/kimage.h                 |  23 +-
 9 files changed, 363 insertions(+), 101 deletions(-)
Signature · 1 line
2.52.0
#1 lore raw

PATCH v2 1/7 x86/kexec: add digest checks

From: Ross Lagerwall <[email protected]>

During kexec load a sha256 digest is calculated of all the kexec segments combined. This digest is stored and verified again during kexec execution.

This is a requirement for Secure Boot to ensure that kexec data has not been tampered with or corrupted between signature verification and actual execution.

Only kexec crash is supported. The segments for normal kexec are stored in temporary buffers and moved to their intended destination during execution. To calculate/verify the normal kexec data before relocation would involve walking the relocation table to find every temporary buffer which was out of scope for Secure Boot work.

---
 xen/common/kexec.c       | 10 ++++
 xen/common/kimage.c      | 98 ++++++++++++++++++++++++++++++++++++++++
 xen/include/xen/kimage.h |  6 +++
 3 files changed, 114 insertions(+)
xen/common/kexec.c +10 -0
index 65776a95fd..c920bc6d8a 100644
@@ -383,6 +383,12 @@ void kexec_crash(enum crash_reason reason)
     if ( !test_bit(KEXEC_IMAGE_CRASH_BASE + pos, &kexec_flags) )
         return;
 
+    if ( kimage_verify_digest(kexec_image[KEXEC_IMAGE_CRASH_BASE + pos]) != 0 )
+    {
+        printk(XENLOG_ERR "kexec digest failed, won't boot corrupted image\n");
+        return;
+    }
+
     kexecing = true;
 
     if ( kexec_common_shutdown() != 0 )
@@ -1132,6 +1138,10 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( ret < 0 )
         goto error;
 
+    ret = kimage_calc_digest(kimage, kimage->digest);
+    if ( ret )
+        return ret;
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;
xen/common/kimage.c +98 -0

113 lines not on this page · raw message

xen/include/xen/kimage.h +6 -0
index fccba1d88d..ad52551ba2 100644
@@ -11,6 +11,7 @@
 
 #include <xen/list.h>
 #include <xen/mm.h>
+#include <xen/sha2.h>
 #include <public/kexec.h>
 
 #define KEXEC_SEGMENT_MAX 16
@@ -37,6 +38,8 @@ 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];
 };
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
@@ -52,6 +55,9 @@ 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_verify_digest(const struct kexec_image *image);
+int kimage_calc_digest(const struct kexec_image *image,
+                       uint8_t digest[SHA2_256_DIGEST_SIZE]);
 
 #endif /* __ASSEMBLER__ */
 
Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH 1/7] x86/kexec: add digest checks

Quoted 10 lines · Kevin Lampis

From: Ross Lagerwall <[email protected]>

During kexec load a sha256 digest is calculated of all the kexec segments combined. This digest is stored and verified again during kexec execution.

This is a requirement for Secure Boot to ensure that kexec data has not been tampered with or corrupted between signature verification and actual execution.

Quoted 5 lines

Only kexec crash is supported. The segments for normal kexec are stored in temporary buffers and moved to their intended destination during execution. To calculate/verify the normal kexec data before relocation would involve walking the relocation table to find every temporary buffer which was out of scope for Secure Boot work.

I'd suggest reordering the information for clarity.  How about this:

"In order to support UEFI Secure Boot, we must confirm that the kexec data has not been tampered with or corrupted between signature verification and actual execution.  However, it's a good check to perform generally.

During kexec load, calculate a sha2_256 digest over all the kexec segments.  This is stored and verified again later prior to entering the image.

For now, only kexec crash images are support.  The segments for normal kexec are stored in temporary buffers and moved to their intended destination during execution. The logic to reconstruct the normal kexec images runs long after Xen could cleanly cope with a digest failure."

"out of scope" is not really something that wants to end up in the commit message.

xen/common/kexec.c void kexec_crash(enum crash_reason reason) · line 388

Quoted 11 lines
xen/common/kexec.c +3 -0
index 65776a95fd..c920bc6d8a 100644
@@ -383,6 +383,12 @@ void kexec_crash(enum crash_reason reason)
     if ( !test_bit(KEXEC_IMAGE_CRASH_BASE + pos, &kexec_flags) )
         return;
 
+    if ( kimage_verify_digest(kexec_image[KEXEC_IMAGE_CRASH_BASE + pos]) != 0 )
+    {
+        printk(XENLOG_ERR "kexec digest failed, won't boot corrupted image\n");

"kexec digest failed, aborting kexec crash transfer\n".

The !=0 can be dropped.

xen/common/kimage.c line 23

Quoted 9 lines
xen/common/kimage.c +1 -0
index 6202491f7e..018ef66451 100644
@@ -20,9 +20,12 @@
 #include <xen/mm.h>
 #include <xen/kexec.h>
 #include <xen/kimage.h>
+#include <xen/sha2.h>

You don't ned to include sha2.h here in kimiage.c, seeing as you need it in kimage.h for the struct change.

Quoted 18 lines
xen/common/kimage.c +14 -0
@@ -820,6 +823,101 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
     return result;
 }
 
+static int kimage_calc_one_digest(struct sha2_256_state *ctx,
+                                  xen_kexec_segment_t *segment)
+{
+    paddr_t dest;
+    unsigned long sbytes;
+    int ret = 0;
+
+    sbytes = segment->buf_size;
+    dest = segment->dest_maddr;
+
+    while ( sbytes )
+    {
+        unsigned long dest_mfn;
+        void *dest_va;

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 849

Quoted 10 lines
xen/common/kimage.c +10 -0
+        size_t schunk, dchunk;
+
+        dest_mfn = dest >> PAGE_SHIFT;
+
+        dchunk = PAGE_SIZE;
+        schunk = min(dchunk, sbytes);
+
+        dest_va = map_domain_page(_mfn(dest_mfn));
+        if ( !dest_va )
+            return -EINVAL;

map_domain_page() doesn't fail.

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 855

Quoted 6 lines
xen/common/kimage.c +6 -0
+
+        sha2_256_update(ctx, dest_va, schunk);
+
+        unmap_domain_page(dest_va);
+        if ( ret )
+            return -EFAULT;

ret is always 0.  (This is dead logic from copying the form using copy_from_guest()).

With these dropped, the function can become void.

Quoted 15 lines
xen/common/kimage.c +15 -0
+
+        sbytes -= schunk;
+        dest += dchunk;
+    }
+    return 0;
+}
+
+int kimage_calc_digest(const struct kexec_image *image,
+                       uint8_t digest[SHA2_256_DIGEST_SIZE])
+{
+    int ret;
+    struct sha2_256_state ctx;
+    unsigned int s;
+
+    if ( image->type == KEXEC_TYPE_DEFAULT )

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 880

Quoted 10 lines
xen/common/kimage.c +10 -0
+    {
+        /* TODO implement digest calculation for normal kexec */
+        return 0;
+    }
+
+    if ( image->nr_segments > KIMAGE_SHA256_REGIONS )
+    {
+        dprintk(XENLOG_DEBUG, "More segments than allocated SHA256 regions\n");
+        return -E2BIG;
+    }

This is the only use of KIMAGE_SHA256_REGIONS, but I think it's stale from the version of digest checking in the patchqueue.  I think you can drop it.

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 885

Quoted 5 lines
xen/common/kimage.c +5 -0
+
+
+    sha2_256_init(&ctx);
+
+    for ( s = 0; s < image->nr_segments; s++ ) {

Style.  Brace on next line.  (The code you copied from was wrong.)

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 892

Quoted 7 lines
xen/common/kimage.c +7 -0
+        ret = kimage_calc_one_digest(&ctx, &image->segments[s]);
+        if ( ret )
+            return ret;
+    }
+
+    sha2_256_final(&ctx, digest);
+    return 0;

With kimage_calc_one_digest() becoming void, so can kimage_calc_digest().

Quoted 10 lines
xen/common/kimage.c +10 -0
+}
+
+int kimage_verify_digest(const struct kexec_image *image)
+{
+    uint8_t digest[SHA2_256_DIGEST_SIZE];
+    int ret;
+
+    if ( image->type == KEXEC_TYPE_DEFAULT )
+    {
+        /* TODO implement digest check for normal kexec */

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 912

Quoted 10 lines
xen/common/kimage.c +10 -0
+        return 0;
+    }
+
+    ret = kimage_calc_digest(image, digest);
+    if ( ret )
+        return ret;
+
+    if ( memcmp(digest, image->digest, sizeof(digest)) != 0 )
+    {
+        printk(XENLOG_ERR "kexec digest failed expected %*phN but got %*phN\n",

"kexec digest failed:\n"
"  expected %" STR(SHA2_256_DIGEST_SIZE) "phN\n"
"       got %" STR(SHA2_256_DIGEST_SIZE) "phN\n"

The newlines and tabulation are important for legibility if this really triggers.

In hindsight I should have pointed you at check_digest() for AMD microcode, which is even closer to what you needed.

xen/common/kimage.c int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, · line 915

Quoted 3 lines
xen/common/kimage.c +3 -0
+               SHA2_256_DIGEST_SIZE, image->digest,
+               SHA2_256_DIGEST_SIZE, digest);
+        return 1;

return -ENODATA or -EBADMSG.

Error handling needs to be either truly boolean (in which case the function wants to be bool), or int with -error.

~Andrew

#3 lore raw

Re: [PATCH 1/7] x86/kexec: add digest checks

In reply to Kevin Lampis
Quoted 5 lines · Kevin Lampis
xen/common/kexec.c +0 -0
index 65776a95fd..c920bc6d8a 100644

xen/common/kexec.c static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg) · line 1147

Quoted 10 lines
xen/common/kexec.c +4 -0
@@ -1132,6 +1138,10 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( ret < 0 )
         goto error;
 
+    ret = kimage_calc_digest(kimage, kimage->digest);
+    if ( ret )
+        return ret;
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;

You cannot return in the middle here, or you leak a bunch of allocations.

~Andrew

#4 lore raw

PATCH v2 2/7 x86/kexec: rename setup_header to bzimage_header

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

The newly named bzimage_header is moved to a header file because part of it will be needed for Secure Boot kexec.

---
 xen/arch/x86/bzimage.c             | 47 +++---------------------------
 xen/arch/x86/include/asm/bzimage.h | 39 +++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 43 deletions(-)
xen/arch/x86/bzimage.c +4 -43
index 66f648f311..0bbd8b2758 100644
@@ -13,48 +13,9 @@ 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)
+static __init int bzimage_check(struct bzimage_header *hdr, unsigned long len)
 {
-    if ( len < sizeof(struct setup_header) )
+    if ( len < sizeof(struct bzimage_header) )
         return 0;
 
     if ( memcmp(&hdr->header, HDR_MAGIC, HDR_MAGIC_SZ) != 0 )
@@ -73,7 +34,7 @@ static unsigned long __initdata orig_image_len;
 unsigned long __init bzimage_headroom(void *image_start,
                                       unsigned long image_length)
 {
-    struct setup_header *hdr = (struct setup_header *)image_start;
+    struct bzimage_header *hdr = (struct bzimage_header *)image_start;
     int err;
     unsigned long headroom;
 
@@ -106,7 +67,7 @@ unsigned long __init bzimage_headroom(void *image_start,
 int __init bzimage_parse(void *image_base, void **image_start,
                          unsigned long *image_len)
 {
-    struct setup_header *hdr = (struct setup_header *)(*image_start);
+    struct bzimage_header *hdr = (struct bzimage_header *)(*image_start);
     int err = bzimage_check(hdr, *image_len);
     unsigned long output_len;
 
xen/arch/x86/include/asm/bzimage.h +39 -0
index 7ed69d3910..8c54b21d06 100644
@@ -8,4 +8,43 @@ unsigned long bzimage_headroom(void *image_start, unsigned long image_length);
 int bzimage_parse(void *image_base, void **image_start,
                   unsigned long *image_len);
 
+struct __packed bzimage_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;
+    };
+
 #endif /* __X86_BZIMAGE_H__ */
Signature · 1 line
2.52.0
Reviewed-by on v2 2/7
#5 lore raw

Re: [PATCH 2/7] x86/kexec: rename setup_header to bzimage_header

Quoted 10 lines · Kevin Lampis
xen/arch/x86/bzimage.c +1 -1
@@ -73,7 +34,7 @@ static unsigned long __initdata orig_image_len;
 unsigned long __init bzimage_headroom(void *image_start,
                                       unsigned long image_length)
 {
-    struct setup_header *hdr = (struct setup_header *)image_start;
+    struct bzimage_header *hdr = (struct bzimage_header *)image_start;
     int err;
     unsigned long headroom;
 

xen/arch/x86/bzimage.c unsigned long __init bzimage_headroom(void *image_start, · line 70

Quoted 5 lines
xen/arch/x86/bzimage.c +1 -1
@@ -106,7 +67,7 @@ unsigned long __init bzimage_headroom(void *image_start,
 int __init bzimage_parse(void *image_base, void **image_start,
                          unsigned long *image_len)
 {
-    struct setup_header *hdr = (struct setup_header *)(*image_start);
+    struct bzimage_header *hdr = (struct bzimage_header *)(*image_start);

Both of these casts are bogus, and can be dropped as you're editing the line.  (Probably written by a C++ developer.)

#6 lore raw

PATCH v2 3/7 x86/kexec: add new struct kimage_segment

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

New struct for interal use because Secure Boot code needs to add an extra field in a future patch.

---
 xen/common/kexec.c       | 23 ++++++++++++++---------
 xen/common/kimage.c      | 17 +++++++++--------
 xen/include/xen/kimage.h | 14 ++++++++++++--
 3 files changed, 35 insertions(+), 19 deletions(-)
xen/common/kexec.c +14 -9
index c920bc6d8a..00346fe616 100644
@@ -916,7 +916,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);
@@ -942,7 +942,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;
@@ -997,7 +997,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);
@@ -1007,7 +1007,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;
 
@@ -1109,9 +1109,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;
@@ -1119,14 +1120,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 +9 -8
index 018ef66451..2c3fd3c3b0 100644
@@ -86,7 +86,7 @@ static struct page_info *kimage_alloc_zeroed_page(unsigned memflags)
 
 static int do_kimage_alloc(struct kexec_image **rimage, paddr_t entry,
                            unsigned long nr_segments,
-                           xen_kexec_segment_t *segments, uint8_t type)
+                           struct kimage_segment *segments, uint8_t type)
 {
     struct kexec_image *image;
     unsigned long i;
@@ -211,7 +211,7 @@ out:
 
 static int kimage_normal_alloc(struct kexec_image **rimage, paddr_t entry,
                                unsigned long nr_segments,
-                               xen_kexec_segment_t *segments)
+                               struct kimage_segment *segments)
 {
     return do_kimage_alloc(rimage, entry, nr_segments, segments,
                            KEXEC_TYPE_DEFAULT);
@@ -219,7 +219,7 @@ static int kimage_normal_alloc(struct kexec_image **rimage, paddr_t entry,
 
 static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
                               unsigned long nr_segments,
-                              xen_kexec_segment_t *segments)
+                              struct kimage_segment *segments)
 {
     unsigned long i;
 
@@ -669,7 +669,7 @@ found:
 }
 
 static int kimage_load_normal_segment(struct kexec_image *image,
-                                      xen_kexec_segment_t *segment)
+                                      struct kimage_segment *segment)
 {
     unsigned long to_copy;
     unsigned long src_offset;
@@ -722,7 +722,7 @@ static int kimage_load_normal_segment(struct kexec_image *image,
 }
 
 static int kimage_load_crash_segment(struct kexec_image *image,
-                                     xen_kexec_segment_t *segment)
+                                     struct kimage_segment *segment)
 {
     /*
      * For crash dumps kernels we simply copy the data from user space
@@ -768,7 +768,8 @@ static int kimage_load_crash_segment(struct kexec_image *image,
     return 0;
 }
 
-static int kimage_load_segment(struct kexec_image *image, xen_kexec_segment_t *segment)
+static int kimage_load_segment(struct kexec_image *image,
+                               struct kimage_segment *segment)
 {
     int result = -ENOMEM;
     paddr_t addr;
@@ -799,7 +800,7 @@ static int kimage_load_segment(struct kexec_image *image, xen_kexec_segment_t *s
 
 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)
 {
     int result;
 
@@ -824,7 +825,7 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
 }
 
 static int kimage_calc_one_digest(struct sha2_256_state *ctx,
-                                  xen_kexec_segment_t *segment)
+                                  struct kimage_segment *segment)
 {
     paddr_t dest;
     unsigned long sbytes;
xen/include/xen/kimage.h +12 -2
index ad52551ba2..258349d774 100644
@@ -18,12 +18,22 @@
 
 typedef 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;
+};
+
 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;
@@ -44,7 +54,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.52.0
#7 lore raw

Re: [PATCH 3/7] x86/kexec: add new struct kimage_segment

Quoted 10 lines · Kevin Lampis
xen/common/kexec.c +1 -1
index c920bc6d8a..00346fe616 100644
@@ -1119,14 +1120,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);

xen/common/kexec.c static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg) · line 1130

Quoted 10 lines
xen/common/kexec.c +3 -3
     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) )

This cast is unsafe.

It relies on struct kimage_segment having xen_kexec_segment_t at the start of it, which is not mentioned or enforced anywhere.

In the next patch when you add dest_offset, this leaves parts of segments[] uninitialised.

Read into a local xen_kexec_segment_t variable and fill in segments[i] manually as you're wanting to change it's type.

xen/include/xen/kimage.h line 24

Quoted 12 lines
xen/include/xen/kimage.h +4 -0
index ad52551ba2..258349d774 100644
@@ -18,12 +18,22 @@
 
 typedef paddr_t kimage_entry_t;
 
+struct kimage_segment {
+    union {
+        XEN_GUEST_HANDLE(const_void) h;
+        uint64_t _pad;

After separating the types, get rid of this union and _pad.  It's ABI abuse which needs to not infect anything it doesn't strictly need to.

~Andrew

#8 lore raw

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

In reply to Kevin Lampis

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/kimage.c      | 34 ++++++++++++++++++++++++++++++----
 xen/include/xen/kimage.h |  1 +
 2 files changed, 31 insertions(+), 4 deletions(-)
xen/common/kimage.c +30 -4
index 2c3fd3c3b0..1d872916b9 100644
@@ -730,12 +730,14 @@ static int kimage_load_crash_segment(struct kexec_image *image,
      */
     paddr_t dest;
     unsigned long sbytes, dbytes;
+    unsigned int dest_offset;
     int ret = 0;
     unsigned long src_offset = 0;
 
     sbytes = segment->buf_size;
     dbytes = segment->dest_size;
     dest = segment->dest_maddr;
+    dest_offset = segment->dest_offset;
 
     while ( dbytes )
     {
@@ -745,24 +747,28 @@ static int kimage_load_crash_segment(struct kexec_image *image,
 
         dest_mfn = dest >> PAGE_SHIFT;
 
-        dchunk = PAGE_SIZE;
+        dchunk = PAGE_SIZE - dest_offset;
         schunk = min(dchunk, sbytes);
 
         dest_va = map_domain_page(_mfn(dest_mfn));
         if ( !dest_va )
             return -EINVAL;
 
-        ret = copy_from_guest_offset(dest_va, segment->buf.h, src_offset, schunk);
+        if ( dest_offset )
+            memset(dest_va, 0, dest_offset);
+        ret = copy_from_guest_offset(dest_va + dest_offset, segment->buf.h,
+                                     src_offset, schunk);
         memset(dest_va + schunk, 0, dchunk - schunk);
 
         unmap_domain_page(dest_va);
         if ( ret )
             return -EFAULT;
 
-        dbytes -= dchunk;
+        dbytes -= dchunk + dest_offset;
         sbytes -= schunk;
-        dest += dchunk;
+        dest += dchunk + dest_offset;
         src_offset += schunk;
+        dest_offset = 0;
     }
 
     return 0;
@@ -803,6 +809,26 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
                  uint32_t nr_segments, struct kimage_segment *segment)
 {
     int result;
+    unsigned int i;
+
+    for ( i = 0; i < nr_segments; i++ )
+    {
+        paddr_t mend;
+
+        /*
+         * Stash the destination offset-in-page for use when copying the
+         * buffer later.
+         */
+        segment[i].dest_offset = PAGE_OFFSET(segment[i].dest_maddr);
+
+        /*
+         * Align down the start address to page size and align up the end
+         * address to page size.
+         */
+        mend = segment[i].dest_maddr + segment[i].dest_size;
+        segment[i].dest_maddr &= PAGE_MASK;
+        segment[i].dest_size = ROUNDUP(mend, PAGE_SIZE) - segment[i].dest_maddr;
+    }
 
     switch( type )
     {
xen/include/xen/kimage.h +1 -0
index 258349d774..aab8707cac 100644
@@ -26,6 +26,7 @@ struct kimage_segment {
     uint64_t buf_size;
     uint64_t dest_maddr;
     uint64_t dest_size;
+    unsigned int dest_offset;
 };
 
 struct kexec_image {
Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH 4/7] x86/kexec: Support non-page-aligned kexec segments

Quoted 6 lines · Kevin Lampis

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).

It's startup_32 not startup32.  Yet we hand over in 64bit mode, so why is startup_32 relevant rather than startup_64?

I can't find any hard alignment restriction (beyond 4k).  There is a check involving BP_kernel_alignment, which is set to 2M, which makes sense; it's the alignment we set Xen to too.

So, what's the real bug/problem here?

~Andrew

#10 lore raw

Re: [PATCH 4/7] x86/kexec: Support non-page-aligned kexec segments

Quoted 2 lines

It's startup_32 not startup32.  Yet we hand over in 64bit mode, so why is startup_32 relevant rather than startup_64?

Probably just careless wording. I don't think it was the author's intention to draw a distinction between 32 and 64.

Quoted 4 lines

I can't find any hard alignment restriction (beyond 4k).  There is a check involving BP_kernel_alignment, which is set to 2M, which makes sense; it's the alignment we set Xen to too.
So, what's the real bug/problem here?

For normal kexec the userspace tools chop off the first part of the kernel so everything aligns correctly.

For Secure Boot we need to load the whole kernel unmodified to pass the signature check and that means the startup function is not aligned correctly anymore.

We also changed the userspace tools to add padding to the start of the kernel segment so the startup function is pushed out to the next 2M byte boundary. So Secure Boot kexec does still work without this patch.

#11 lore raw

PATCH v2 5/7 x86/kexec: Implement new EFI load types

In reply to Kevin Lampis

From: Ross Lagerwall <[email protected]>

Add new EFI load type for kexec. This load type 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. Instead, Xen will prepare any glue code needed internally.

---
 xen/arch/x86/include/asm/bzimage.h |   5 ++
 xen/common/kexec.c                 |   9 +++
 xen/common/kimage.c                | 123 +++++++++++++++++++++--------
 xen/include/public/kexec.h         |  11 ++-
 xen/include/xen/kimage.h           |   2 +
 5 files changed, 115 insertions(+), 35 deletions(-)
xen/arch/x86/include/asm/bzimage.h +5 -0
index 8c54b21d06..ed61f9446a 100644
@@ -47,4 +47,9 @@ struct __packed bzimage_header {
         uint32_t        payload_length;
     };
 
+static inline uint64_t kernel_alignment_offset(void)
+{
+    return offsetof(struct bzimage_header, kernel_alignment);
+}
+
 #endif /* __X86_BZIMAGE_H__ */
xen/common/kexec.c +9 -0
index 00346fe616..1ae4c069c1 100644
@@ -750,6 +750,7 @@ static int kexec_load_get_bits(int type, int *base, int *bit)
         *bit = KEXEC_FLAG_DEFAULT_POS;
         break;
     case KEXEC_TYPE_CRASH:
+    case KEXEC_TYPE_CRASH_EFI:
         *base = KEXEC_IMAGE_CRASH_BASE;
         *bit = KEXEC_FLAG_CRASH_POS;
         break;
@@ -848,6 +849,7 @@ static int kexec_exec(XEN_GUEST_HANDLE_PARAM(void) uarg)
         ret = continue_hypercall_on_cpu(0, kexec_reboot, image);
         break;
     case KEXEC_TYPE_CRASH:
+    case KEXEC_TYPE_CRASH_EFI:
         kexec_crash(CRASHREASON_KEXECCMD); /* Does not return */
         break;
     }
@@ -1147,6 +1149,13 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)
     if ( ret )
         return ret;
 
+    if ( load.type == KEXEC_TYPE_CRASH_EFI )
+    {
+        ret = kimage_efi_setup(kimage, load.parameters);
+        if ( ret )
+            return ret;
+    }
+
     ret = kexec_load_slot(kimage);
     if ( ret < 0 )
         goto error;
xen/common/kimage.c +90 -33

174 lines not on this page · raw message

xen/include/public/kexec.h +9 -2
index 40d79e936b..287255ea6f 100644
@@ -58,13 +58,16 @@
  * - kexec into a regular kernel, very similar to a standard reboot
  *   - KEXEC_TYPE_DEFAULT is used to specify this type
  * - 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
+ *   - in case of KEXEC_TYPE_CRASH_EFI the first segment will point to full
+ *     the kernel to load and entry point will point to boot params
  *   - 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_CRASH_EFI 3
 
 
 /* The kexec implementation for Xen allows the user to load two
@@ -195,7 +198,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 +2 -0
index aab8707cac..ba5d5c2b92 100644
@@ -51,6 +51,7 @@ struct kexec_image {
     paddr_t next_crash_page;
 
     uint8_t digest[SHA2_256_DIGEST_SIZE];
+    uint64_t boot_params;
 };
 
 int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
@@ -69,6 +70,7 @@ int kimage_build_ind(struct kexec_image *image, mfn_t ind_mfn,
 int kimage_verify_digest(const struct kexec_image *image);
 int kimage_calc_digest(const struct kexec_image *image,
                        uint8_t digest[SHA2_256_DIGEST_SIZE]);
+int kimage_efi_setup(struct kexec_image *image, uint64_t parameters);
 
 #endif /* __ASSEMBLER__ */
 
Signature · 1 line
2.52.0
#12 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

xen/arch/x86/include/asm/bzimage.h struct __packed bzimage_header { · line 53

Quoted 10 lines · Kevin Lampis
xen/arch/x86/include/asm/bzimage.h +4 -0
@@ -47,4 +47,9 @@ struct __packed bzimage_header {
         uint32_t        payload_length;
     };
 
+static inline uint64_t kernel_alignment_offset(void)
+{
+    return offsetof(struct bzimage_header, kernel_alignment);
+}

Why the uint64_t return type? If at all, size_t. Yet likely unsigned int would suffice.

Jan

#13 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

In reply to Kevin Lampis
Quoted 11 lines · Kevin Lampis

From: Ross Lagerwall <[email protected]>

Add new EFI load type for kexec. This load type 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. Instead, Xen will prepare any glue code needed internally.

Now that you've dropped regular EFI, the subject should become singular.

xen/common/kimage.c line 26

Quoted 10 lines
xen/common/kimage.c +1 -0
index 1d872916b9..9b49455e8e 100644
@@ -22,6 +22,7 @@
 #include <xen/kimage.h>
 #include <xen/sha2.h>
 
+#include <asm/bzimage.h>
 #include <asm/page.h>

This does not compile for non-x86.  But how to fix it depends on a later question.

Quoted 34 lines
xen/common/kimage.c +30 -0
@@ -1064,6 +1064,63 @@ done:
     return ret;
 }
 
+/*
+ * Find the entry point to the new kernel, we need to map the crash region into
+ * memory in order to read the kernel header.
+ */
+#define KERNEL_SEGMENT_IDX 0
+static uint64_t kimage_find_kernel_entry_maddr(struct kexec_image *image)
+{
+    uint64_t alignment_addr;
+    uint32_t alignment;
+    unsigned long dest_mfn;
+    void *dest_va;
+
+    alignment_addr = image->segments[KERNEL_SEGMENT_IDX].dest_maddr +
+                         image->segments[KERNEL_SEGMENT_IDX].dest_offset +
+                         kernel_alignment_offset();
+
+    dest_mfn = alignment_addr >> PAGE_SHIFT;
+    dest_va = map_domain_page(_mfn(dest_mfn));
+    if ( !dest_va )
+        return -EINVAL;
+
+    alignment = *((uint32_t *) ((uint8_t *) dest_va +
+                                                PAGE_OFFSET(alignment_addr)));
+
+    unmap_domain_page(dest_va);
+
+    /*
+     * Ensure the kernel alignment is a valid LOAD_PHYSICAL_ADDR,
+     * which ranges from 0x200000 (2MiB) to 0x1000000 (16Mib) on 64-bit systems
+     * as defined in the kernel x86 Kconfig

xen/common/kimage.c done: · line 1106

Quoted 10 lines
xen/common/kimage.c +10 -0
+     */
+    if ( alignment % 0x200000 != 0 ||
+         alignment < 0x200000 ||
+         alignment > 0x1000000 )
+        return -EINVAL;
+
+    return ROUNDUP(image->segments[KERNEL_SEGMENT_IDX].dest_maddr +
+                       image->segments[KERNEL_SEGMENT_IDX].dest_offset,
+                   alignment) +
+                   0x200;

Ok, so here is startup_64, hidden as a magic constant.

The rest doesn't make sense.  You're finding and checking the alignment field is within the known bounds; fine-ish, but liable to go stale, and not providing a diagnostic when the check fails.

But then, instead of confirming that that segment[0] has the desired alignment, you're applying the alignment to the pointer and picking startup_64 out of thin air.

Either it was loaded correctly (and the alignment is correct), or it was loaded incorrectly and this is unsafe to do, is it not?

~Andrew

#14 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

Quoted 3 lines

Ok, so here is startup_64, hidden as a magic constant. ... you're applying the alignment to the pointer and picking startup_64 out of thin air.

I'm going to be cheeky and play the Linux-does-it-too card.
https://github.com/torvalds/linux/blob/master/arch/x86/kernel/kexec-bzimage64.c#L660

Quoted 2 lines

Either it was loaded correctly (and the alignment is correct), or it was loaded incorrectly and this is unsafe to do, is it not?

This is what the kernel kexec segment looks like in memory

.- dest_maddr + alignment
.-dest_maddr |
v v

   +--------------------------------------+--------------------...
   | padding            | real-mode setup | kernel
   +--------------------------------------+--------------------...
0x000000                               0x200000

In the old non-EFI kexec the 16-bit real-mode setup portion is chopped off by the userspace tools and the segment buffer that Xen receives is just the "kernel" part. But we need to pass both the real-mode setup + kernel to kexec_load to pass Secure Boot verification. And add padding so it straddles a 2M boundary like this.

What the 0x200000 "alignment" variable is doing is skipping over the real-mode setup and padding.

It won't necessarily be 0x200000 though which is why we need to read the value from Linux's setup_header->kernel_alignment.

If you're hinting that perhaps userspace should take care of all this and just tell Xen where startup_64 is then perhaps. That would be a bigger change we need to get input from Ross on though.

#15 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

Quoted 29 lines · Kevin Lampis
Quoted 3 lines

Ok, so here is startup_64, hidden as a magic constant. ... you're applying the alignment to the pointer and picking startup_64 out of thin air.

I'm going to be cheeky and play the Linux-does-it-too card.
https://github.com/torvalds/linux/blob/master/arch/x86/kernel/kexec-bzimage64.c#L660

Quoted 2 lines

Either it was loaded correctly (and the alignment is correct), or it was loaded incorrectly and this is unsafe to do, is it not?

This is what the kernel kexec segment looks like in memory

.- dest_maddr + alignment
.-dest_maddr |
v v
+--------------------------------------+--------------------...
| padding | real-mode setup | kernel
+--------------------------------------+--------------------...
0x000000 0x200000

In the old non-EFI kexec the 16-bit real-mode setup portion is chopped off by the userspace tools and the segment buffer that Xen receives is just the "kernel" part. But we need to pass both the real-mode setup + kernel to kexec_load to pass Secure Boot verification. And add padding so it straddles a 2M boundary like this.

What the 0x200000 "alignment" variable is doing is skipping over the real-mode setup and padding.

Quoted 7 lines

It won't necessarily be 0x200000 though which is why we need to read the value from Linux's setup_header->kernel_alignment.

If you're hinting that perhaps userspace should take care of all this and just tell Xen where startup_64 is then perhaps. That would be a bigger change we need to get input from Ross on though.

It was done in Xen because there was a concern that letting userspace pass in an arbitrary start address would potentially allow a Secure Boot bypass even if it restricted within the bounds of the kernel image.

The patch should probably make it clear that this load type _only_ works for Linux bzImages using the x86 64-bit boot protocol documented at linux/Documentation/arch/x86/boot.rst. I think it needs sanity checks as well to ensure that it has actually been given an x86 bzImage and that the boot protocol version number is as expected.

Since it is x86 specific, maybe it wants to move out of common code?

Ross

#16 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

Quoted 14 lines · Ross Lagerwall
Quoted 13 lines · Kevin Lampis
Quoted 3 lines

Ok, so here is startup_64, hidden as a magic constant. ... you're applying the alignment to the pointer and picking startup_64 out of thin air.

I'm going to be cheeky and play the Linux-does-it-too card.
https://github.com/torvalds/linux/blob/master/arch/x86/kernel/kexec-
bzimage64.c#L660

Quoted 2 lines

Either it was loaded correctly (and the alignment is correct), or it was loaded incorrectly and this is unsafe to do, is it not?

This is what the kernel kexec segment looks like in memory

Quoted 7 lines
Quoted 7 lines

                                           .- dest_maddr + alignment .-dest_maddr                           |
    v                                      v

    +--------------------------------------+--------------------...
    | padding            | real-mode setup | kernel
    +--------------------------------------+--------------------...

0x000000                               0x200000

By "real mode setup", for the purposes here that's the PE+/bzImage64 polygot header?

Where does the (hypercall) segment start point? Surely at the start of the PE+/bzImage64 header?

Quoted 11 lines
Quoted 11 lines

In the old non-EFI kexec the 16-bit real-mode setup portion is chopped off by the userspace tools and the segment buffer that Xen receives is just the "kernel" part. But we need to pass both the real-mode setup + kernel to kexec_load to pass Secure Boot verification. And add padding so it straddles a 2M boundary like this.

What the 0x200000 "alignment" variable is doing is skipping over the real-mode setup and padding.

It won't necessarily be 0x200000 though which is why we need to read the value from Linux's setup_header->kernel_alignment.

What is necessary is that the segment dest_maddr is chosen such that the start of the embedded kernel has correct alignment in the eventual identity map. This property is either true or it's not, and there shouldn't be a round() calculation required to generate the entrypoint.

Quoted 10 lines
Quoted 5 lines

If you're hinting that perhaps userspace should take care of all this and just tell Xen where startup_64 is then perhaps. That would be a bigger change we need to get input from Ross on though.

It was done in Xen because there was a concern that letting userspace pass in an arbitrary start address would potentially allow a Secure Boot bypass even if it restricted within the bounds of the kernel image.

Quoted 7 lines

The patch should probably make it clear that this load type _only_ works for Linux bzImages using the x86 64-bit boot protocol documented at linux/Documentation/arch/x86/boot.rst. I think it needs sanity checks as well to ensure that it has actually been given an x86 bzImage and that the boot protocol version number is as expected.

Since it is x86 specific, maybe it wants to move out of common code?

Yes, that's where we're going, but the constraints need understanding first.

The old load types had nothing really architecture specific in it. It was a bunch of bytes, and an arbitrary point to jump to.

With this new "EFI" load type, we've got structural information. We don't need userspace to pass in a starting point because we can locate it ourselves.

We do want to restrict it to a valid PE+(x86_64)/bzImage64 polygot, and this will require arch specific functions to decide whether we like the image, and to figure out the entrypoint.

~Andrew

#17 lore raw

Re: [PATCH 5/7] x86/kexec: Implement new EFI load types

In reply to Kevin Lampis
Quoted 33 lines · Kevin Lampis
xen/common/kimage.c +5 -10
index 1d872916b9..9b49455e8e 100644
@@ -217,17 +195,13 @@ static int kimage_normal_alloc(struct kexec_image **rimage, paddr_t entry,
                            KEXEC_TYPE_DEFAULT);
 }
 
-static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
-                              unsigned long nr_segments,
-                              struct kimage_segment *segments)
+static int do_kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
+                                 unsigned long nr_segments,
+                                 struct kimage_segment *segments,
+                                 uint8_t type)
 {
     unsigned long i;
 
-    /* Verify we have a valid entry point */
-    if ( (entry < kexec_crash_area.start)
-         || (entry > kexec_crash_area.start + kexec_crash_area.size))
-        return -EADDRNOTAVAIL;
-
     /*
      * Verify we have good destination addresses.  Normally
      * the caller is responsible for making certain we don't
@@ -253,8 +227,28 @@ static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
     }
 
     /* Allocate and initialize a controlling structure. */
-    return do_kimage_alloc(rimage, entry, nr_segments, segments,
-                           KEXEC_TYPE_CRASH);
+    return do_kimage_alloc(rimage, entry, nr_segments, segments, type);

xen/common/kimage.c static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry, · line 240

Quoted 10 lines
xen/common/kimage.c +10 -0
+}
+
+static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry,
+                              unsigned long nr_segments,
+                              struct kimage_segment *segments)
+{
+    /* Verify we have a valid entry point */
+    if ( (entry < kexec_crash_area.start)
+         || (entry > kexec_crash_area.start + kexec_crash_area.size))
+        return -EADDRNOTAVAIL;

I've fixed this style in the for-next branch, so you're not copying around bad style.

~Andrew

#18 lore raw

PATCH v2 6/7 x86/kexec: Pass boot params directly to new kernel

In reply to Kevin Lampis

When doing kexec on an EFI image there is no purgatory and kexec_reloc.S hands off straight to the new kernel. To facilitate this the linux kernel boot params need to be passed through %rsi.

---
 xen/arch/x86/include/asm/machine_kexec.h | 2 +-
 xen/arch/x86/machine_kexec.c             | 3 ++-
 xen/arch/x86/x86_64/kexec_reloc.S        | 3 +++
 3 files changed, 6 insertions(+), 2 deletions(-)
xen/arch/x86/include/asm/machine_kexec.h +1 -1
index 3e189acf24..010e0c7046 100644
@@ -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 arg);
 
 extern const char kexec_reloc_end[];
 
xen/arch/x86/machine_kexec.c +2 -1
index f921eec5aa..2edc1bd0af 100644
@@ -198,7 +198,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->boot_params);
 }
 
 int machine_kexec_get(xen_kexec_range_t *range)
xen/arch/x86/x86_64/kexec_reloc.S +3 -0
index b52d31a654..6a2c839b83 100644
@@ -33,6 +33,7 @@ FUNC(kexec_reloc, PAGE_SIZE)
         /* %rdx - indirection page maddr */
         /* %rcx - entry maddr (%rbp) */
         /* %r8 - flags */
+        /* %r9 - boot params */
 
         movq    %rcx, %rbp
 
@@ -71,6 +72,8 @@ FUNC(kexec_reloc, PAGE_SIZE)
         movq    %rdx, %rdi
         call    relocate_pages
 
+        movq %r9, %rsi /* boot params */
+
         /* Need to switch to 32-bit mode? */
         testq   $KEXEC_RELOC_FLAG_COMPAT, %r8
         jnz     .L_call_32_bit
Signature · 1 line
2.52.0
#19 lore raw

Re: [PATCH 6/7] x86/kexec: Pass boot params directly to new kernel

xen/arch/x86/machine_kexec.c void machine_kexec(struct kexec_image *image) · line 202

Quoted 11 lines · Kevin Lampis
xen/arch/x86/machine_kexec.c +2 -1
index f921eec5aa..2edc1bd0af 100644
@@ -198,7 +198,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->boot_params);

With this in context, the field really ought to be named entry_arg.

Linux boot_params is just one possible option (even if it is the only option right now).

Quoted 16 lines
xen/arch/x86/machine_kexec.c +0 -0
 }
 
 int machine_kexec_get(xen_kexec_range_t *range)
xen/arch/x86/x86_64/kexec_reloc.S +1 -0
index b52d31a654..6a2c839b83 100644
@@ -33,6 +33,7 @@ FUNC(kexec_reloc, PAGE_SIZE)
         /* %rdx - indirection page maddr */
         /* %rcx - entry maddr (%rbp) */
         /* %r8 - flags */
+        /* %r9 - boot params */
 
         movq    %rcx, %rbp
 

xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 76

Quoted 5 lines
xen/arch/x86/x86_64/kexec_reloc.S +2 -0
@@ -71,6 +72,8 @@ FUNC(kexec_reloc, PAGE_SIZE)
         movq    %rdx, %rdi
         call    relocate_pages
 
+        movq %r9, %rsi /* boot params */
+

The surrounding code has not been cleaned up to Xen style yet.  This should be:

        mov     %r9, %rsi /* entry_arg */

~Andrew

#20 lore raw

PATCH v2 7/7 x86/kexec: Zero general purpose registers

In reply to Kevin Lampis

Purgatory zeros these general purpose registers before jumping to the new kernel. When doing kexec on an EFI kernel there is no purgatory so we need to zero the registers here instead. This change shouldn't affect the functionality of legacy kexec.

---
 xen/arch/x86/x86_64/kexec_reloc.S | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
xen/arch/x86/x86_64/kexec_reloc.S +21 -0
index 6a2c839b83..be9e487f7a 100644
@@ -78,6 +78,20 @@ FUNC(kexec_reloc, PAGE_SIZE)
         testq   $KEXEC_RELOC_FLAG_COMPAT, %r8
         jnz     .L_call_32_bit
 
+        xor %edi, %edi
+        xor %eax, %eax
+        xor %ebx, %ebx
+        xor %ecx, %ecx
+        xor %edx, %edx
+        xor %r8d,  %r8d
+        xor %r9d,  %r9d
+        xor %r10d, %r10d
+        xor %r11d, %r11d
+        xor %r12d, %r12d
+        xor %r13d, %r13d
+        xor %r14d, %r14d
+        xor %r15d, %r15d
+
         /* Jump to the image entry point */
         jmp     *%rbp
 
@@ -94,6 +108,13 @@ FUNC(kexec_reloc, PAGE_SIZE)
         lea     compatibility_mode(%rip), %rax
         push    $0x10
         push    %rax
+
+        xor %edi, %edi
+        xor %eax, %eax
+        xor %ebx, %ebx
+        xor %ecx, %ecx
+        xor %edx, %edx
+
         lretq
 END(kexec_reloc)
 
Signature · 1 line
2.52.0
#21 lore raw

Re: [PATCH 7/7] x86/kexec: Zero general purpose registers

xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 87

Quoted 13 lines · Kevin Lampis
xen/arch/x86/x86_64/kexec_reloc.S +7 -0
@@ -78,6 +78,20 @@ FUNC(kexec_reloc, PAGE_SIZE)
         testq   $KEXEC_RELOC_FLAG_COMPAT, %r8
         jnz     .L_call_32_bit
 
+        xor %edi, %edi
+        xor %eax, %eax
+        xor %ebx, %ebx
+        xor %ecx, %ecx
+        xor %edx, %edx
+        xor %r8d,  %r8d
+        xor %r9d,  %r9d

Why the double blanks after the comma here? While that aligns with what follows, it misaligns with what comes earlier.

xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 96

Quoted 9 lines
xen/arch/x86/x86_64/kexec_reloc.S +7 -0
+        xor %r10d, %r10d
+        xor %r11d, %r11d
+        xor %r12d, %r12d
+        xor %r13d, %r13d
+        xor %r14d, %r14d
+        xor %r15d, %r15d
+
         /* Jump to the image entry point */
         jmp     *%rbp

Please take surrounding code as reference as to style, i.e. particularly the number of blanks to insert after the insn mnemonic.

Jan

#22 lore raw

Re: [PATCH 7/7] x86/kexec: Zero general purpose registers

In reply to Kevin Lampis
Quoted 6 lines · Kevin Lampis

Purgatory zeros these general purpose registers before jumping to the new kernel. When doing kexec on an EFI kernel there is no purgatory so we need to zero the registers here instead. This change shouldn't affect the functionality of legacy kexec.

The subject wants to be "x86/kexec: Zero GPRs before entering new kernel" or similar.

Quoted 8 lines
xen/arch/x86/x86_64/kexec_reloc.S +0 -0
index 6a2c839b83..be9e487f7a 100644
@@ -94,6 +108,13 @@ FUNC(kexec_reloc, PAGE_SIZE)
         lea     compatibility_mode(%rip), %rax
         push    $0x10
         push    %rax

xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 120

Quoted 10 lines
xen/arch/x86/x86_64/kexec_reloc.S +7 -0
+
+        xor %edi, %edi
+        xor %eax, %eax
+        xor %ebx, %ebx
+        xor %ecx, %ecx
+        xor %edx, %edx
+
         lretq
 END(kexec_reloc)
 

This hunk is in the wrong place.  It wants to be lower down at the end of compatibility_mode, where there's another /* Jump to the image entry point */ comment.

~Andrew