Skip to contentSkip to conversation

Allows Secure Boot for Kexec

PATCH In review Frediano Ziglio 4 participants 9 messages May 6, 2025 – May 12, 2025

lore
Share & tools
lore
#0 lore raw

cover Allows Secure Boot for Kexec

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

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                |  10 +
 xen/include/xen/x86-linux.h           |  62 +++
 xen/lib/sha2-256.c                    |  16 +-
 20 files changed, 1344 insertions(+), 111 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 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 | 10 ++++++++++
 xen/lib/sha2-256.c     | 14 ++++----------
 2 files changed, 14 insertions(+), 10 deletions(-)
xen/include/xen/sha2.h +10 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);
 
xen/lib/sha2-256.c +4 -10
index 19e8252188..896a257ed9 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,7 +171,7 @@ 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, void *_dst)
 {
     uint32_t *dst = _dst;
     unsigned int i, partial = s->count & 63;
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/4] xen/lib: Export additional sha256 functions

Quoted 10 lines · Frediano Ziglio
xen/include/xen/sha2.h +2 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];

xen/include/xen/sha2.h line 23

Quoted 10 lines
xen/include/xen/sha2.h +8 -0
+    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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);

sha2_256_digest() is unlike the others as it holds sha2_256_state internally.  I'd suggest having all of the additions below this point, which group them more nicely.

Can fix on commit.  Otherwise LGTM.

~Andrew

#3 lore raw

Re: [PATCH 1/4] xen/lib: Export additional sha256 functions

Quoted 25 lines · Andrew Cooper
Quoted 10 lines · Frediano Ziglio
xen/include/xen/sha2.h +2 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];

xen/include/xen/sha2.h line 23

Quoted 10 lines
xen/include/xen/sha2.h +8 -0
+    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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);

sha2_256_digest() is unlike the others as it holds sha2_256_state internally.  I'd suggest having all of the additions below this point, which group them more nicely.

Quoted 1 line

Can fix on commit.  Otherwise LGTM.

Not quite.  Now that sha2_256_final() is exported, it should have a proper type for  _dst.  I've folded:

xen/include/xen/sha2.h +0 -0
index 0d55fe640431..cb30e8f8d77c 100644
@@ -21,6 +21,7 @@ struct sha2_256_state {

 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, void *_dst); +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 +0 -0
index 896a257ed9b7..08ef7011a1c3 100644
@@ -171,9 +171,9 @@ void sha2_256_update(struct sha2_256_state *s, const

void *msg,
     memcpy(s->buf + partial, msg, len);
 }

-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 */

in too, which is compatible with the rest of the series too.

~Andrew

#4 lore raw

Re: [PATCH 1/4] xen/lib: Export additional sha256 functions

Quoted 50 lines · Andrew Cooper
Quoted 25 lines · Andrew Cooper
Quoted 10 lines · Frediano Ziglio
xen/include/xen/sha2.h +2 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];

xen/include/xen/sha2.h line 23

Quoted 10 lines
xen/include/xen/sha2.h +8 -0
+    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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);

sha2_256_digest() is unlike the others as it holds sha2_256_state internally. I'd suggest having all of the additions below this point, which group them more nicely.

Quoted 1 line

Can fix on commit. Otherwise LGTM.

Not quite. Now that sha2_256_final() is exported, it should have a proper type for _dst. I've folded:

xen/include/xen/sha2.h +2 -1
index 0d55fe640431..cb30e8f8d77c 100644
@@ -21,6 +21,7 @@ struct sha2_256_state {
 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, void *_dst);
+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 +0 -0
index 896a257ed9b7..08ef7011a1c3 100644

xen/lib/sha2-256.c void sha2_256_update(struct sha2_256_state *s, const · line 178

Quoted 10 lines
xen/lib/sha2-256.c +2 -2
@@ -171,9 +171,9 @@ void sha2_256_update(struct sha2_256_state *s, const
 void *msg,
     memcpy(s->buf + partial, msg, len);
 }
 
-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;

That is we are never going to support architectures with unaligned memory access.

Quoted 7 lines
xen/lib/sha2-256.c +0 -0
     unsigned int i, partial = s->count & 63;
     /* Start padding */

in too, which is compatible with the rest of the series too.

~Andrew

I'll send an updated series with these and all the commits (mail server issues).

Frediano

#5 lore raw

Re: [PATCH 1/4] xen/lib: Export additional sha256 functions

Quoted 53 lines · Frediano Ziglio
Quoted 52 lines · Andrew Cooper
Quoted 25 lines · Andrew Cooper
Quoted 10 lines · Frediano Ziglio
xen/include/xen/sha2.h +2 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];

xen/include/xen/sha2.h line 23

Quoted 10 lines
xen/include/xen/sha2.h +8 -0
+    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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);

sha2_256_digest() is unlike the others as it holds sha2_256_state internally. I'd suggest having all of the additions below this point, which group them more nicely.

Quoted 1 line

Can fix on commit. Otherwise LGTM.

Not quite. Now that sha2_256_final() is exported, it should have a proper type for _dst. I've folded:

xen/include/xen/sha2.h +2 -1
index 0d55fe640431..cb30e8f8d77c 100644
@@ -21,6 +21,7 @@ struct sha2_256_state {
 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, void *_dst);
+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 +0 -0
index 896a257ed9b7..08ef7011a1c3 100644
@@ -171,9 +171,9 @@ void sha2_256_update(struct sha2_256_state *s, const
 void *msg,
     memcpy(s->buf + partial, msg, len);
 }
 
Quoted 8 lines

xen/lib/sha2-256.c void sha2_256_update(struct sha2_256_state *s, const · line 178

Quoted 6 lines
xen/lib/sha2-256.c +2 -2
-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;

That is we are never going to support architectures with unaligned memory access.

Note how it's written to.

    /* Store state in digest */
    for ( i = 0; i < 8; i++ )
        put_unaligned_be32(s->state[i], &dst[i]);

It works just fine on misaligned buffers.

~Andrew

#6 lore raw

Re: [PATCH 1/4] xen/lib: Export additional sha256 functions

In reply to Andrew Cooper
Quoted 26 lines · Andrew Cooper
Quoted 10 lines · Frediano Ziglio
xen/include/xen/sha2.h +2 -0
index 47d97fbf01..ea8bad67e4 100644
@@ -9,6 +9,16 @@
 
 #define SHA2_256_DIGEST_SIZE 32
 
+struct sha2_256_state {
+    uint32_t state[SHA2_256_DIGEST_SIZE / sizeof(uint32_t)];

xen/include/xen/sha2.h line 23

Quoted 10 lines
xen/include/xen/sha2.h +8 -0
+    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, void *_dst);
 void sha2_256_digest(uint8_t digest[SHA2_256_DIGEST_SIZE],
                      const void *msg, size_t len);

sha2_256_digest() is unlike the others as it holds sha2_256_state internally.  I'd suggest having all of the additions below this point, which group them more nicely.

Quoted 1 line

Can fix on commit.  Otherwise LGTM.

I notice this was committed, but isn't this introducing new Misra violations (extern functions without external callers)?

Jan

#7 lore raw

PATCH 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