Skip to content

Commit c0c2d31

Browse files
dubekbonzini
authored andcommitted
x86/sev: generate SEV kernel loader hashes in x86_load_linux
If SEV is enabled and a kernel is passed via -kernel, pass the hashes of kernel/initrd/cmdline in an encrypted guest page to OVMF for SEV measured boot. Co-developed-by: James Bottomley <[email protected]> Signed-off-by: James Bottomley <[email protected]> Signed-off-by: Dov Murik <[email protected]> Reviewed-by: Daniel P. Berrangé <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent cff0314 commit c0c2d31

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

hw/i386/x86.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "hw/i386/fw_cfg.h"
4848
#include "hw/intc/i8259.h"
4949
#include "hw/rtc/mc146818rtc.h"
50+
#include "target/i386/sev_i386.h"
5051

5152
#include "hw/acpi/cpu_hotplug.h"
5253
#include "hw/irq.h"
@@ -780,6 +781,7 @@ void x86_load_linux(X86MachineState *x86ms,
780781
const char *initrd_filename = machine->initrd_filename;
781782
const char *dtb_filename = machine->dtb;
782783
const char *kernel_cmdline = machine->kernel_cmdline;
784+
SevKernelLoaderContext sev_load_ctx = {};
783785

784786
/* Align to 16 bytes as a paranoia measure */
785787
cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
@@ -926,6 +928,8 @@ void x86_load_linux(X86MachineState *x86ms,
926928
fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
927929
fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
928930
fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
931+
sev_load_ctx.cmdline_data = (char *)kernel_cmdline;
932+
sev_load_ctx.cmdline_size = strlen(kernel_cmdline) + 1;
929933

930934
if (protocol >= 0x202) {
931935
stl_p(header + 0x228, cmdline_addr);
@@ -1007,6 +1011,8 @@ void x86_load_linux(X86MachineState *x86ms,
10071011
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
10081012
fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
10091013
fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
1014+
sev_load_ctx.initrd_data = initrd_data;
1015+
sev_load_ctx.initrd_size = initrd_size;
10101016

10111017
stl_p(header + 0x218, initrd_addr);
10121018
stl_p(header + 0x21c, initrd_size);
@@ -1065,15 +1071,32 @@ void x86_load_linux(X86MachineState *x86ms,
10651071
load_image_size(dtb_filename, setup_data->data, dtb_size);
10661072
}
10671073

1068-
memcpy(setup, header, MIN(sizeof(header), setup_size));
1074+
/*
1075+
* If we're starting an encrypted VM, it will be OVMF based, which uses the
1076+
* efi stub for booting and doesn't require any values to be placed in the
1077+
* kernel header. We therefore don't update the header so the hash of the
1078+
* kernel on the other side of the fw_cfg interface matches the hash of the
1079+
* file the user passed in.
1080+
*/
1081+
if (!sev_enabled()) {
1082+
memcpy(setup, header, MIN(sizeof(header), setup_size));
1083+
}
10691084

10701085
fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
10711086
fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
10721087
fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
1088+
sev_load_ctx.kernel_data = (char *)kernel;
1089+
sev_load_ctx.kernel_size = kernel_size;
10731090

10741091
fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
10751092
fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
10761093
fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
1094+
sev_load_ctx.setup_data = (char *)setup;
1095+
sev_load_ctx.setup_size = setup_size;
1096+
1097+
if (sev_enabled()) {
1098+
sev_add_kernel_loader_hashes(&sev_load_ctx, &error_fatal);
1099+
}
10771100

10781101
option_rom[nb_option_roms].bootindex = 0;
10791102
option_rom[nb_option_roms].name = "linuxboot.bin";

0 commit comments

Comments
 (0)