x86/kexec: Secure Boot support
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.
Changes in v2:
- Addressed review comments for specific patches
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/Makefile | 1 + xen/arch/x86/bzimage.c | 47 +---- xen/arch/x86/include/asm/bzimage.h | 44 +++++ xen/arch/x86/include/asm/kexec.h | 9 + xen/arch/x86/include/asm/machine_kexec.h | 2 +- xen/arch/x86/kexec.c | 58 +++++++ xen/arch/x86/machine_kexec.c | 3 +- xen/arch/x86/x86_64/kexec_reloc.S | 23 +++ xen/common/kexec.c | 36 +++- xen/common/kimage.c | 208 ++++++++++++++++++----- xen/include/public/kexec.h | 11 +- xen/include/xen/kimage.h | 20 ++- 12 files changed, 364 insertions(+), 98 deletions(-) create mode 100644 xen/arch/x86/include/asm/kexec.h create mode 100644 xen/arch/x86/kexec.c
Signature · 1 line
2.52.0
PATCH v2 1/7 x86/kexec: add digest checks
From: Ross Lagerwall <[email protected]>
To support UEFI Secure Boot we must check that the kexec data has not changed between signature verification and actual execution. However, this is also a good check to perform generally.
During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.
For now, only kexec crash images are supported.
---
Changes in v2:
- Reword the commit message
- Change the "kexec digest failed" error message
- Remove the != 0 from the call to kimage_verify_digest()
- Remove include sha2.h from kimage.h
- Remove error checking from kimage_calc_one_digest() because map_domain_page() doesn't fail
- Change kimage_calc_digest() to a void return too
- Remove references to KIMAGE_SHA256_REGIONS
It was a purgatory thing which is not used anymore
- Fix style on for ( s = 0; s < image->nr_segments; s++ ) {
- Add newlines and indentation to digest check fail message
- Make kimage_verify_digest() a bool return value
--- xen/common/kexec.c | 8 +++++ xen/common/kimage.c | 73 ++++++++++++++++++++++++++++++++++++++++ xen/include/xen/kimage.h | 6 ++++ 3 files changed, 87 insertions(+)
xen/common/kexec.c +8 -0
@@ -389,6 +389,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]) ) + { +printk (XENLOG_ERR "kexec digest failed, aborting kexec crash transfer\n" ); +return ; + } + kexecing = true;if (kexec_common_shutdown () !=0 )
@@ -943,6 +949,8 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)if ( ret <0 ) goto error; +kimage_calc_digest (kimage, kimage->digest); + ret =kexec_load_slot (kimage);if ( ret <0 ) goto error;
xen/common/kimage.c +73 -0
79 lines not on this page · raw message
xen/include/xen/kimage.h +6 -0
@@ -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); +bool kimage_verify_digest (const struct kexec_image *image); +void kimage_calc_digest (const struct kexec_image *image, +uint8_t digest[SHA2_256_DIGEST_SIZE ]);#endif /* __ASSEMBLER__ */
Signature · 1 line
2.52.0
Re: [PATCH v2 1/7] x86/kexec: add digest checks
Quoted 6 lines · Kevin Lampis
From: Ross Lagerwall <[email protected]>
To support UEFI Secure Boot we must check that the kexec data has not changed between signature verification and actual execution. However, this is also a good check to perform generally.
Quoted 7 lines
During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.
For now, only kexec crash images are supported.
Signed-off-by Ross Lagerwall <[email protected]>Signed-off-by Kevin Lampis <[email protected]>
I guess I'm not quite following here. For secure boot purposes, shouldn't the new kernel already come with a digest (or really with a signature), which we could check in both kexec_load() and kexec_crash()? If we check against a digest we calculated ourselves, we'd apply more trust than we should.
Jan
Re: [PATCH v2 1/7] x86/kexec: add digest checks
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Kevin Lampis
From: Ross Lagerwall <[email protected]>
To support UEFI Secure Boot we must check that the kexec data has not changed between signature verification and actual execution. However, this is also a good check to perform generally.
During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.
For now, only kexec crash images are supported.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Ross Lagerwall <[email protected]>Signed-off-by Kevin Lampis <[email protected]>I guess I'm not quite following here. For secure boot purposes, shouldn't the new kernel already come with a digest (or really with a signature), which we could check in both
kexec_load()andkexec_crash()? If we check against a digest we calculated ourselves, we'd apply more trust than we should.
There are two problems.
One is plain TOCTOU. We load the crash kernel at boot, and we jump to it in the case that something has fatally-but-not-catastrophically gone wrong. Really, the digest here is an integrity check.
One is that Xen cannot perform the signature check on the passed kernel. At least, not without gaining a full X.509 stack and authenticode algorithm, or a PGP implementation or equivalent. ExitBootServices() nukes SHIM_LOCK/LOADER protocols so they cannot be used later in runtime.
Because we are defining the TCB of the system as "Xen + Dom0 kernel", it's fine to let Dom0 do the certificate check and say "trust me, I checked this" to Xen (albeit requiring that dom0 userspace can't issue the hypercall). In the current implementation the dom0 checks the signature of the binary that userspace proposes, and either rejects it or passes it forward to Xen.
All Xen needs to do is ensure that the bytes the dom0 kernel said were good are still the same bytes we're about to jump into. Hence the integrity check, which is a good move even in the non UEFI-SB case.
~Andrew
Re: [PATCH v2 1/7] x86/kexec: add digest checks
Quoted 27 lines · Andrew Cooper
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Kevin Lampis
From: Ross Lagerwall <[email protected]>
To support UEFI Secure Boot we must check that the kexec data has not changed between signature verification and actual execution. However, this is also a good check to perform generally.
During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.
For now, only kexec crash images are supported.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Ross Lagerwall <[email protected]>Signed-off-by Kevin Lampis <[email protected]>I guess I'm not quite following here. For secure boot purposes, shouldn't the new kernel already come with a digest (or really with a signature), which we could check in both
kexec_load()andkexec_crash()? If we check against a digest we calculated ourselves, we'd apply more trust than we should.There are two problems.
One is plain TOCTOU. We load the crash kernel at boot, and we jump to it in the case that something has fatally-but-not-catastrophically gone wrong. Really, the digest here is an integrity check.
Quoted 6 lines
One is that Xen cannot perform the signature check on the passed kernel. At least, not without gaining a full X.509 stack and authenticode algorithm, or a PGP implementation or equivalent.
ExitBootServices()nukes SHIM_LOCK/LOADER protocols so they cannot be used later in runtime.
FWIW at one point Shim did have the option to keep the SHIM_LOCK protocol after ExitBootServices(). We tried to use it but it was buggy and didn't work and was then removed when the new LOADER protocol came in.
Ross
Re: [PATCH v2 1/7] x86/kexec: add digest checks
In reply to Andrew CooperQuoted 40 lines · Andrew Cooper
Quoted 12 lines · Jan Beulich
Quoted 11 lines · Kevin Lampis
From: Ross Lagerwall <[email protected]>
To support UEFI Secure Boot we must check that the kexec data has not changed between signature verification and actual execution. However, this is also a good check to perform generally.
During kexec load, calculate a digest over all the kexec segments. This digest is stored and verified again later prior to entering the image.
For now, only kexec crash images are supported.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Ross Lagerwall <[email protected]>Signed-off-by Kevin Lampis <[email protected]>I guess I'm not quite following here. For secure boot purposes, shouldn't the new kernel already come with a digest (or really with a signature), which we could check in both
kexec_load()andkexec_crash()? If we check against a digest we calculated ourselves, we'd apply more trust than we should.There are two problems.
One is plain TOCTOU. We load the crash kernel at boot, and we jump to it in the case that something has fatally-but-not-catastrophically gone wrong. Really, the digest here is an integrity check.
One is that Xen cannot perform the signature check on the passed kernel. At least, not without gaining a full X.509 stack and authenticode algorithm, or a PGP implementation or equivalent.
ExitBootServices()nukes SHIM_LOCK/LOADER protocols so they cannot be used later in runtime.Because we are defining the TCB of the system as "Xen + Dom0 kernel", it's fine to let Dom0 do the certificate check and say "trust me, I checked this" to Xen (albeit requiring that dom0 userspace can't issue the hypercall). In the current implementation the dom0 checks the signature of the binary that userspace proposes, and either rejects it or passes it forward to Xen.
Quoted 3 lines
All Xen needs to do is ensure that the bytes the dom0 kernel said were good are still the same bytes we're about to jump into. Hence the integrity check, which is a good move even in the non UEFI-SB case.
I.e. "To support UEFI Secure Boot we must check ..." in the description is really misleading.
Jan
PATCH v2 2/7 x86/kexec: rename setup_header to bzimage_header
In reply to Kevin LampisFrom: 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.
---
Changes in v2:
- Remove unneeded casts
- Move setup_header to appropriate header file bzimage.h
- Renamed setup_header to less generic name bzimage_header
--- 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
@@ -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 = 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 = *image_start;int err =bzimage_check (hdr, *image_len);unsigned long output_len;
xen/arch/x86/include/asm/bzimage.h +39 -0
@@ -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
PATCH v2 3/7 x86/kexec: add new struct kimage_segment
In reply to Kevin LampisFrom: Ross Lagerwall <[email protected]>
New struct for internal use because Secure Boot code needs to add an extra field in a future patch.
---
Changes in v2:
- Read into a local xen_kexec_segment_t variable and fill in segments[i] manually instead of casting from a different sized type
- Remove union and _pad from new struct kimage_segment type
- Swap xen_kexec_segment_t to struct kimage_segment in separate patch
--- xen/common/kexec.c | 19 ++++++++++++++----- xen/common/kimage.c | 25 +++++++++++++------------ xen/include/xen/kimage.h | 11 +++++++++-- 3 files changed, 36 insertions(+), 19 deletions(-)
xen/common/kexec.c +14 -5
@@ -920,9 +920,10 @@ static int kexec_load_slot(struct kexec_image *kimage)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 ;
@@ -930,14 +931,22 @@ 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; +xen_kexec_segment_t tmp_seg = {}; +if (copy_from_guest_offset (&tmp_seg, load.segments.h, i,1 ) ) + { + ret = -EFAULT ; + goto error; + } + segments[i].h = tmp_seg.buf.h; + segments[i].buf_size = tmp_seg.buf_size; + segments[i].dest_maddr = tmp_seg.dest_maddr; + segments[i].dest_size = tmp_seg.dest_size; } ret = kimage_alloc(&kimage, load.type, load.arch, load.entry_maddr,
xen/common/kimage.c +13 -12
@@ -81,7 +81,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;
@@ -206,7 +206,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 );
@@ -214,7 +214,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;
@@ -236,7 +236,7 @@ static int kimage_crash_alloc(struct kexec_image **rimage, paddr_t entry, {paddr_t mstart, mend; -if (guest_handle_is_null (segments[i].buf.h) ) +if (guest_handle_is_null (segments[i].h) )continue ; mstart = segments[i].dest_maddr;
@@ -664,7 +664,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;
@@ -698,7 +698,7 @@ static int kimage_load_normal_segment(struct kexec_image *image,return ret; dest_va =__map_domain_page (page); - ret =copy_from_guest_offset (dest_va, segment->buf.h, src_offset, size); + ret =copy_from_guest_offset (dest_va, segment->h, src_offset, size);unmap_domain_page (dest_va);if ( ret )return -EFAULT ;
@@ -717,7 +717,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
@@ -747,7 +747,7 @@ static int kimage_load_crash_segment(struct kexec_image *image,if ( !dest_va )return -EINVAL ; - ret =copy_from_guest_offset (dest_va, segment->buf.h, src_offset, schunk); + ret =copy_from_guest_offset (dest_va, segment->h, src_offset, schunk);memset (dest_va + schunk,0 , dchunk - schunk);unmap_domain_page (dest_va);
@@ -763,12 +763,13 @@ 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; -if ( !guest_handle_is_null (segment->buf.h) ) +if ( !guest_handle_is_null (segment->h) ) {switch ( image->type ) {
@@ -794,7 +795,7 @@ static int kimage_load_segment(struct kexec_image *image, xen_kexec_segment_t *sint 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;
@@ -819,7 +820,7 @@ int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch, }static void 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 +9 -2
@@ -18,12 +18,19 @@typedef paddr_t kimage_entry_t ; +struct kimage_segment { +XEN_GUEST_HANDLE (const_void) h; +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 +51,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
Re: [PATCH v2 3/7] x86/kexec: add new struct kimage_segment
xen/common/kexec.c static int kexec_load_slot(struct kexec_image *kimage) · line 926
Quoted 11 lines · Kevin Lampis
xen/common/kexec.c +2
-1@@ -920,9 +920,10 @@ static int kexec_load_slot(struct kexec_image *kimage)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;
Restrict this variable's scope to ...
xen/common/kexec.c static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg) · line 938
Quoted 11 lines
xen/common/kexec.c +2
-2@@ -930,14 +931,22 @@ 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++ )
... the sole loop it's used in?
xen/common/kexec.c static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg) · line 941
Quoted 5 lines
xen/common/kexec.c +2
-2{ - ret = -EFAULT ; - goto error; +xen_kexec_segment_t tmp_seg = {}; +if (copy_from_guest_offset (&tmp_seg, load.segments.h, i,1 ) )
Nit: Blank line between declaration(s) and statement(s) please.
I also see no need for the variable to have an initializer.
xen/include/xen/kimage.h line 26
Quoted 12 lines
xen/include/xen/kimage.h +6
-0@@ -18,12 +18,19 @@typedef paddr_t kimage_entry_t ; +struct kimage_segment { +XEN_GUEST_HANDLE (const_void) h; +uint64_t buf_size; +uint64_t dest_maddr; +uint64_t dest_size; +};
size_t and paddr_t instead of uint64_t?
Jan
PATCH v2 4/7 x86/kexec: Support non-page-aligned kexec segments
In reply to Kevin LampisFrom: 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.
---
Changes in v2:
- Remove error handling around map_domain_page because it doesn't fail
--- xen/common/kimage.c | 47 +++++++++++++++++++++++++++++++--------- xen/include/xen/kimage.h | 1 + 2 files changed, 38 insertions(+), 10 deletions(-)
xen/common/kimage.c +37 -10
@@ -725,12 +725,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 ) {
@@ -740,24 +742,26 @@ 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->h, src_offset, schunk); -memset (dest_va + schunk,0 , dchunk - schunk); +if ( dest_offset ) +memset (dest_va,0 , dest_offset); + ret =copy_from_guest_offset (dest_va + dest_offset, segment->h, + src_offset, schunk); +memset (dest_va + dest_offset + 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 ;
@@ -798,6 +802,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 ) {
@@ -824,9 +848,11 @@ static void kimage_calc_one_digest(struct sha2_256_state *ctx, {paddr_t dest;unsigned long sbytes; +unsigned int dest_offset; sbytes = segment->buf_size; dest = segment->dest_maddr; + dest_offset = segment->dest_offset;while ( sbytes ) {
@@ -836,15 +862,16 @@ static void kimage_calc_one_digest(struct sha2_256_state *ctx, 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)); -sha2_256_update (ctx, dest_va, schunk); +sha2_256_update (ctx, dest_va + dest_offset, schunk);unmap_domain_page (dest_va); sbytes -= schunk; - dest += dchunk; + dest += dchunk + dest_offset; + dest_offset =0 ; } }
xen/include/xen/kimage.h +1 -0
@@ -23,6 +23,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
PATCH v2 5/7 x86/kexec: Implement new EFI load type
In reply to Kevin LampisFrom: 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.
The new load type only works for Linux bzImages using the x86 64-bit boot protocol documented at linux/Documentation/arch/x86/boot.rst.
---
Changes in v2:
- Move kimage_find_kernel_entry_maddr to arch specific kexec.c file Didn't add stubs for other archs because CONFIG_HAS_KEXEC is x86 only
- When parsing kernel entry check for header magic and boot protocol version
- When parsing kernel entry use setup_sects instead of ROUNDUP
- Update commit message to explain new kexec type only works for x86 linux
- Rename kexec_image->boot_params to entry_arg
- Use size_t for return type of kernel_alignment_offset()
- Remove error handling around map_domain_page because it doesn't fail
--- xen/arch/x86/Makefile | 1 + xen/arch/x86/include/asm/bzimage.h | 5 ++ xen/arch/x86/include/asm/kexec.h | 9 ++++ xen/arch/x86/kexec.c | 58 +++++++++++++++++++++++ xen/common/kexec.c | 9 ++++ xen/common/kimage.c | 75 +++++++++++++++++++----------- xen/include/public/kexec.h | 11 ++++- xen/include/xen/kimage.h | 2 + 8 files changed, 140 insertions(+), 30 deletions(-) create mode 100644 xen/arch/x86/include/asm/kexec.h create mode 100644 xen/arch/x86/kexec.c
xen/arch/x86/Makefile +1 -0
@@ -71,6 +71,7 @@ obj-$(CONFIG_TBOOT) += tboot.o obj-y += hpet.o obj-$(CONFIG_VM_EVENT) += vm_event.o obj-y += xstate.o +obj-$(CONFIG_KEXEC) += kexec.o ifneq ($(CONFIG_PV_SHIM_EXCLUSIVE),y) obj-y += domctl.o
xen/arch/x86/include/asm/bzimage.h +5 -0
@@ -47,4 +47,9 @@ struct __packed bzimage_header { uint32_t payload_length; }; +static inline size_t kernel_alignment_offset (void ) +{ +return offsetof(struct bzimage_header , kernel_alignment); +} +#endif /* __X86_BZIMAGE_H__ */
xen/arch/x86/include/asm/kexec.h new file +9 -0
@@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef ASM_X86_KEXEC_H +#define ASM_X86_KEXEC_H + +struct kexec_image ; +int64_t kimage_find_kernel_entry_maddr (struct kexec_image *image); + +#endif /* ASM_X86_KEXEC_H */
xen/arch/x86/kexec.c new file +58 -0
58 lines not on this page · raw message
xen/common/kexec.c +9 -0
@@ -760,6 +760,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 ;
@@ -859,6 +860,7 @@ static int kexec_exec(XEN_GUEST_HANDLE_PARAM(void) uarg)break ;case KEXEC_TYPE_CRASH : +case KEXEC_TYPE_CRASH_EFI :kexec_crash (CRASHREASON_KEXECCMD );/* Does not return */ break ; }
@@ -960,6 +962,13 @@ static int kexec_load(XEN_GUEST_HANDLE_PARAM(void) uarg)kimage_calc_digest (kimage, kimage->digest); +if ( load.type ==KEXEC_TYPE_CRASH_EFI ) + { + ret =kimage_efi_setup (kimage, load.parameters); +if ( ret <0 ) + goto error; + } + ret =kexec_load_slot (kimage);if ( ret <0 ) goto error;
xen/common/kimage.c +47 -28
123 lines not on this page · raw message
xen/include/public/kexec.h +9 -2
@@ -54,13 +54,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 the +* full 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/* * Perform kexec having previously loaded a kexec or kdump kernel
@@ -167,7 +170,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
@@ -48,6 +48,7 @@ struct kexec_image {paddr_t next_crash_page;uint8_t digest[SHA2_256_DIGEST_SIZE ]; +uint64_t entry_arg; }; int kimage_alloc(struct kexec_image **rimage, uint8_t type, uint16_t arch,
@@ -66,6 +67,7 @@ int kimage_build_ind(struct kexec_image *image, mfn_t ind_mfn,bool kimage_verify_digest (const struct kexec_image *image);void 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
Re: [PATCH v2 5/7] x86/kexec: Implement new EFI load type
xen/arch/x86/Makefile obj-$(CONFIG_TBOOT) += tboot.o · line 74
Quoted 7 lines · Kevin Lampis
xen/arch/x86/Makefile +1
-0@@ -71,6 +71,7 @@ obj-$(CONFIG_TBOOT) += tboot.o obj-y += hpet.o obj-$(CONFIG_VM_EVENT) += vm_event.o obj-y += xstate.o +obj-$(CONFIG_KEXEC) += kexec.o
While there are some anomalies down here, most of this block is suitably sorted (alphabetically). Please insert accordingly.
Quoted 40 lines
xen/arch/x86/kexec.c new file +37
-0@@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include <xen/kexec.h> +#include <xen/kimage.h> +#include <xen/guest_access.h> +#include <asm/bzimage.h> + +/* +* 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 +int64_t kimage_find_kernel_entry_maddr (struct kexec_image *image) +{ +uint64_t dest_maddr; +uint32_t alignment, magic; +uint16_t version; +void *dest_va; +const struct bzimage_header *hdr; +int setup_sects; +size_t kern16_size; + + dest_maddr = image->segments[KERNEL_SEGMENT_IDX ].dest_maddr + + image->segments[KERNEL_SEGMENT_IDX ].dest_offset; + + dest_va =map_domain_page (maddr_to_mfn (dest_maddr)); + + hdr = (const struct bzimage_header *)dest_va; + magic = hdr->header; + version = hdr->version; + alignment = hdr->kernel_alignment; + setup_sects = hdr->setup_sects ==0 ?4 : hdr->setup_sects; + kern16_size = (setup_sects +1 ) *512 ; + +unmap_domain_page (dest_va); + +if ( magic !=0x53726448 || version <0x0202 )
xen/arch/x86/kexec.c line 47
Quoted 10 lines
xen/arch/x86/kexec.c new file +10
-0+return -EINVAL ; + +/* +* 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 +*/ +if ( alignment %0x200000 !=0 || + alignment <0x200000 || + alignment >0x1000000 )
Use MB() to cover some of what the comment currently says? (The second of the checks would also be easier as !alignment, seeing that the first check already excluded all other values below MB(2).)
Quoted 21 lines
xen/include/public/kexec.h +4
-1@@ -54,13 +54,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 the +* full 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/* * Perform kexec having previously loaded a kexec or kdump kernel
xen/include/public/kexec.h typedef struct xen_kexec_load { · line 173
Quoted 5 lines
xen/include/public/kexec.h +1
-1@@ -167,7 +170,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. */
Nit: As you touch comments, please also correct style issues.
xen/include/public/kexec.h typedef struct xen_kexec_load { · line 177
Quoted 4 lines
xen/include/public/kexec.h +4
-0+union { +uint64_t entry_maddr; +uint64_t parameters; + };
We don't want to use language extensions in public headers. I'm actually surprised this still passes the -ansi header check that we do from include/Makefile. Maybe with recent compilers this doesn't do anymore what we expect it to do?
Jan
PATCH v2 6/7 x86/kexec: Pass boot params directly to new kernel
In reply to Kevin LampisWhen 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.
---
Changes in v2:
- Rename kexec_image->boot_params to entry_arg
- Fix indentation
--- 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
@@ -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
@@ -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->entry_arg); }int machine_kexec_get (xen_kexec_range_t *range)
xen/arch/x86/x86_64/kexec_reloc.S +3 -0
@@ -33,6 +33,7 @@ FUNC(kexec_reloc, PAGE_SIZE) /* %rdx - indirection page maddr */ /* %rcx - entry maddr (%rbp) */ /* %r8 - flags */ + /* %r9 - entry arg */ movq %rcx, %rbp
@@ -71,6 +72,8 @@ FUNC(kexec_reloc, PAGE_SIZE) movq %rdx, %rdi call relocate_pages + movq %r9, %rsi /* entry arg */ + /* Need to switch to 32-bit mode? */ testq $KEXEC_RELOC_FLAG_COMPAT, %r8 jnz .L_call_32_bit
Signature · 1 line
2.52.0
PATCH v2 7/7 x86/kexec: Zero GPRs before entering new kernel
In reply to Kevin LampisPurgatory 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.
---
Changes in v2:
- Move the 32bit lines to the correct place
- Reword the patch subject line
- Fix indentation
--- xen/arch/x86/x86_64/kexec_reloc.S | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
xen/arch/x86/x86_64/kexec_reloc.S +20 -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 + 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,7 @@ FUNC(kexec_reloc, PAGE_SIZE) lea compatibility_mode(%rip), %rax push $0x10 push %rax + lretq END(kexec_reloc)
@@ -171,6 +186,11 @@ FUNC_LOCAL(compatibility_mode) xorl %eax, %eax movl %eax, %cr4 + xor %edi, %edi + xor %ebx, %ebx + xor %ecx, %ecx + xor %edx, %edx + /* Jump to the image entry point. */ jmp *%ebp END(compatibility_mode)
Signature · 1 line
2.52.0
Re: [PATCH v2 7/7] x86/kexec: Zero GPRs before entering new kernel
Quoted 12 lines · Kevin Lampis
xen/arch/x86/x86_64/kexec_reloc.S +6
-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
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 96
Quoted 10 lines
xen/arch/x86/x86_64/kexec_reloc.S +8
-0+ 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
Why is %rsi not cleared here? And why is keeping %rbp non-zero okay?
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 113
Quoted 7 lines
xen/arch/x86/x86_64/kexec_reloc.S +1
-0@@ -94,6 +108,7 @@ FUNC(kexec_reloc, PAGE_SIZE) lea compatibility_mode(%rip), %rax push $0x10 push %rax + lretq END(kexec_reloc)
Stray change?
xen/arch/x86/x86_64/kexec_reloc.S FUNC_LOCAL(compatibility_mode) · line 196
Quoted 12 lines
xen/arch/x86/x86_64/kexec_reloc.S +5
-0@@ -171,6 +186,11 @@ FUNC_LOCAL(compatibility_mode) xorl %eax, %eax movl %eax, %cr4 + xor %edi, %edi + xor %ebx, %ebx + xor %ecx, %ecx + xor %edx, %edx + /* Jump to the image entry point. */ jmp *%ebp END(compatibility_mode)
Same question here as to %esi and %ebp.
Jan
Re: [PATCH v2 7/7] x86/kexec: Zero GPRs before entering new kernel
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Kevin Lampis
xen/arch/x86/x86_64/kexec_reloc.S +9
-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 + xor %r10d, %r10d + xor %r11d, %r11d
Quoted 8 lines
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 96
Quoted 7 lines
xen/arch/x86/x86_64/kexec_reloc.S +5
-0+ xor %r12d, %r12d + xor %r13d, %r13d + xor %r14d, %r14d + xor %r15d, %r15d + /* Jump to the image entry point */ jmp *%rbpWhy is %rsi not cleared here? And why is keeping %rbp non-zero okay?
%rsi is the parameter passed in the previous patch.
Linux used to require the entry address in %rbp. I think relocatable configurations can cope without it now, but we don't parse this property of the image.
~Andrew
Re: [PATCH v2 7/7] x86/kexec: Zero GPRs before entering new kernel
Quoted 26 lines · Andrew Cooper
Quoted 16 lines · Jan Beulich
Quoted 15 lines · Kevin Lampis
xen/arch/x86/x86_64/kexec_reloc.S +9
-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 + xor %r10d, %r10d + xor %r11d, %r11dQuoted 8 lines
xen/arch/x86/x86_64/kexec_reloc.S FUNC(kexec_reloc, PAGE_SIZE) · line 96
Quoted 7 lines
xen/arch/x86/x86_64/kexec_reloc.S +5
-0+ xor %r12d, %r12d + xor %r13d, %r13d + xor %r14d, %r14d + xor %r15d, %r15d + /* Jump to the image entry point */ jmp *%rbpWhy is %rsi not cleared here? And why is keeping %rbp non-zero okay?
Quoted 1 line
%rsi is the parameter passed in the previous patch.
Ah, I didn't look closely enough there; I merely checked existing code. A question that arises: What guarantees that in the 32-bit case the value actually fits in %esi (i.e. the upper half of %rsi is zero)? I can't spot any check in patch 6.
Quoted 3 lines
Linux used to require the entry address in %rbp. I think relocatable configurations can cope without it now, but we don't parse this property of the image.
Okay, but the description then better would say why %rbp / %ebp are left alone.
Jan