Skip to contentSkip to conversation

x86/sev: Measured Linux SEV guest with kernel/initrd/cmdline

PATCH Superseded Dov Murik 4 participants 13 messages Jun 24, 2021 – Jul 30, 2021

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Dov Murik

author
#0 lore raw

v3 cover x86/sev: Measured Linux SEV guest with kernel/initrd/cmdline

Currently booting with -kernel/-initrd/-append is not supported in SEV confidential guests, because the content of these blobs is not measured and therefore not trusted by the SEV guest.

However, in some cases the kernel, initrd, and cmdline are not secret but should not be modified by the host. In such a case, we want to verify inside the trusted VM that the kernel, initrd, and cmdline are indeed the ones expected by the Guest Owner, and only if that is the case go on and boot them up (removing the need for grub inside OVMF in that mode).

To support that, OVMF adds a special area for hashes of kernel/initrd/cmdline; that area is expected to be filled by QEMU and encrypted as part of the initial SEV guest launch. This in turn makes the hashes part of the PSP measured content, and OVMF can trust these inputs if they match the hashes.

This series adds an SEV function to generate the table of hashes for OVMF and encrypt it (patch 1/2), and calls this function if SEV is enabled when the kernel/initrd/cmdline are prepared (patch 2/2).

Corresponding OVMF support was submitted to edk2-devel [1] (patch series "Measured SEV boot with kernel/initrd/cmdline"); it's still under review.

[1] https://edk2.groups.io/g/devel/topic/patch_v1_0_8_measured_sev/83074450

---

v3 changes:
- initrd hash is now mandatory; if no -initrd is passed, calculate the hash of the empty buffer. This is now aligned with the OVMF behaviour which verifies the empty initrd (correctly).
- make SevHashTable entries fixed: 3 entries for cmdline, initrd, and kernel.
- in sev_add_kernel_loader_hashes: first calculate all the hashes, only then fill-in the hashes table in the guest's memory.
- Use g_assert_not_reached in sev-stub.c.
- Use QEMU_PACKED attribute for structs.
- Use QemuUUID type for guids.
- in sev_add_kernel_loader_hashes: use ARRAY_SIZE(iov) instead of literal 2.

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v2 changes:
- Extract main functionality to sev.c (with empty stub in sev-stub.c)
- Use sev_enabled() instead of machine->cgs->ready to detect SEV guest
- Coding style changes

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Dov Murik (2):
sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux
boot
x86/sev: generate SEV kernel loader hashes in x86_load_linux

 target/i386/sev_i386.h |  12 ++++
 hw/i386/x86.c          |  25 +++++++-
 target/i386/sev-stub.c |   5 ++
 target/i386/sev.c      | 137 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)

base-commit: b22726abdfa54592d6ad88f65b0297c0e8b363e2

Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v3 1/2 sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux boot

Add the sev_add_kernel_loader_hashes function to calculate the hashes of the kernel/initrd/cmdline and fill a designated OVMF encrypted hash table area. For this to work, OVMF must support an encrypted area to place the data which is advertised via a special GUID in the OVMF reset table.

The hashes of each of the files is calculated (or the string in the case of the cmdline with trailing '\0' included). Each entry in the hashes table is GUID identified and since they're passed through the sev_encrypt_flash interface, the hashes will be accumulated by the PSP measurement (SEV_LAUNCH_MEASURE).

---
 target/i386/sev_i386.h |  12 ++++
 target/i386/sev-stub.c |   5 ++
 target/i386/sev.c      | 137 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 154 insertions(+)
target/i386/sev_i386.h +12 -0
index ae6d840478..deb3eec409 100644
@@ -28,6 +28,17 @@
 #define SEV_POLICY_DOMAIN       0x10
 #define SEV_POLICY_SEV          0x20
 
+typedef struct KernelLoaderContext {
+    char *setup_data;
+    size_t setup_size;
+    char *kernel_data;
+    size_t kernel_size;
+    char *initrd_data;
+    size_t initrd_size;
+    char *cmdline_data;
+    size_t cmdline_size;
+} KernelLoaderContext;
+
 extern bool sev_es_enabled(void);
 extern uint64_t sev_get_me_mask(void);
 extern SevInfo *sev_get_info(void);
@@ -37,5 +48,6 @@ extern char *sev_get_launch_measurement(void);
 extern SevCapability *sev_get_capabilities(Error **errp);
 extern SevAttestationReport *
 sev_get_attestation_report(const char *mnonce, Error **errp);
+extern bool sev_add_kernel_loader_hashes(KernelLoaderContext *ctx, Error **errp);
 
 #endif
target/i386/sev-stub.c +5 -0
index 0227cb5177..addb089f36 100644
@@ -81,3 +81,8 @@ sev_get_attestation_report(const char *mnonce, Error **errp)
     error_setg(errp, "SEV is not available in this QEMU");
     return NULL;
 }
+
+bool sev_add_kernel_loader_hashes(KernelLoaderContext *ctx, Error **errp)
+{
+    g_assert_not_reached();
+}
target/i386/sev.c +137 -0

155 lines not on this page · raw message

Signature · 1 line
2.25.1
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux boot

Quoted 12 lines · Dov Murik

Add the sev_add_kernel_loader_hashes function to calculate the hashes of the kernel/initrd/cmdline and fill a designated OVMF encrypted hash table area. For this to work, OVMF must support an encrypted area to place the data which is advertised via a special GUID in the OVMF reset table.

The hashes of each of the files is calculated (or the string in the case of the cmdline with trailing '\0' included). Each entry in the hashes table is GUID identified and since they're passed through the sev_encrypt_flash interface, the hashes will be accumulated by the PSP measurement (SEV_LAUNCH_MEASURE).

Quoted 4 lines

---

author
#3 lore raw

Re: [PATCH v3 1/2] sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux boot

Quoted 13 lines · Connor Kuehl
Quoted 12 lines · Dov Murik

Add the sev_add_kernel_loader_hashes function to calculate the hashes of the kernel/initrd/cmdline and fill a designated OVMF encrypted hash table area. For this to work, OVMF must support an encrypted area to place the data which is advertised via a special GUID in the OVMF reset table.

The hashes of each of the files is calculated (or the string in the case of the cmdline with trailing '\0' included). Each entry in the hashes table is GUID identified and since they're passed through the sev_encrypt_flash interface, the hashes will be accumulated by the PSP measurement (SEV_LAUNCH_MEASURE).

Quoted 7 lines
Quoted 4 lines

---

Thanks, Connor!

-Dov

author
#4 lore raw

PATCH v3 2/2 x86/sev: generate SEV kernel loader hashes in x86_load_linux

In reply to Dov Murik

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.

---
 hw/i386/x86.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)
hw/i386/x86.c +24 -1
index d30cf27e29..830c8f6f58 100644
@@ -45,6 +45,7 @@
 #include "hw/i386/fw_cfg.h"
 #include "hw/intc/i8259.h"
 #include "hw/rtc/mc146818rtc.h"
+#include "target/i386/sev_i386.h"
 
 #include "hw/acpi/cpu_hotplug.h"
 #include "hw/irq.h"
@@ -778,6 +779,7 @@ void x86_load_linux(X86MachineState *x86ms,
     const char *initrd_filename = machine->initrd_filename;
     const char *dtb_filename = machine->dtb;
     const char *kernel_cmdline = machine->kernel_cmdline;
+    KernelLoaderContext kernel_loader_context = {};
 
     /* Align to 16 bytes as a paranoia measure */
     cmdline_size = (strlen(kernel_cmdline) + 16) & ~15;
@@ -924,6 +926,8 @@ void x86_load_linux(X86MachineState *x86ms,
     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, strlen(kernel_cmdline) + 1);
     fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
+    kernel_loader_context.cmdline_data = (char *)kernel_cmdline;
+    kernel_loader_context.cmdline_size = strlen(kernel_cmdline) + 1;
 
     if (protocol >= 0x202) {
         stl_p(header + 0x228, cmdline_addr);
@@ -1005,6 +1009,8 @@ void x86_load_linux(X86MachineState *x86ms,
         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_ADDR, initrd_addr);
         fw_cfg_add_i32(fw_cfg, FW_CFG_INITRD_SIZE, initrd_size);
         fw_cfg_add_bytes(fw_cfg, FW_CFG_INITRD_DATA, initrd_data, initrd_size);
+        kernel_loader_context.initrd_data = initrd_data;
+        kernel_loader_context.initrd_size = initrd_size;
 
         stl_p(header + 0x218, initrd_addr);
         stl_p(header + 0x21c, initrd_size);
@@ -1063,15 +1069,32 @@ void x86_load_linux(X86MachineState *x86ms,
         load_image_size(dtb_filename, setup_data->data, dtb_size);
     }
 
-    memcpy(setup, header, MIN(sizeof(header), setup_size));
+    /*
+     * If we're starting an encrypted VM, it will be OVMF based, which uses the
+     * efi stub for booting and doesn't require any values to be placed in the
+     * kernel header.  We therefore don't update the header so the hash of the
+     * kernel on the other side of the fw_cfg interface matches the hash of the
+     * file the user passed in.
+     */
+    if (!sev_enabled()) {
+        memcpy(setup, header, MIN(sizeof(header), setup_size));
+    }
 
     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_ADDR, prot_addr);
     fw_cfg_add_i32(fw_cfg, FW_CFG_KERNEL_SIZE, kernel_size);
     fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
+    kernel_loader_context.kernel_data = (char *)kernel;
+    kernel_loader_context.kernel_size = kernel_size;
 
     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_ADDR, real_addr);
     fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, setup_size);
     fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA, setup, setup_size);
+    kernel_loader_context.setup_data = (char *)setup;
+    kernel_loader_context.setup_size = setup_size;
+
+    if (sev_enabled()) {
+        sev_add_kernel_loader_hashes(&kernel_loader_context, &error_fatal);
+    }
 
     option_rom[nb_option_roms].bootindex = 0;
     option_rom[nb_option_roms].name = "linuxboot.bin";
Signature · 1 line
2.25.1
#5 lore raw
In reply to Dov Murik

Hi Paolo,

Please consider this series[1] for inclusion into your next pull request.

Just a note that this series has a companion series that is getting upstreamed into OVMF[2]

[1] Patchwork link, if convenient: https://patchwork.kernel.org/project/qemu-devel/cover/[email protected]/
[2] https://bugzilla.tianocore.org/show_bug.cgi?id=3457#c6

Thank you,

Connor

Quoted 63 lines · Dov Murik

Currently booting with -kernel/-initrd/-append is not supported in SEV confidential guests, because the content of these blobs is not measured and therefore not trusted by the SEV guest.

However, in some cases the kernel, initrd, and cmdline are not secret but should not be modified by the host. In such a case, we want to verify inside the trusted VM that the kernel, initrd, and cmdline are indeed the ones expected by the Guest Owner, and only if that is the case go on and boot them up (removing the need for grub inside OVMF in that mode).

To support that, OVMF adds a special area for hashes of kernel/initrd/cmdline; that area is expected to be filled by QEMU and encrypted as part of the initial SEV guest launch. This in turn makes the hashes part of the PSP measured content, and OVMF can trust these inputs if they match the hashes.

This series adds an SEV function to generate the table of hashes for OVMF and encrypt it (patch 1/2), and calls this function if SEV is enabled when the kernel/initrd/cmdline are prepared (patch 2/2).

Corresponding OVMF support was submitted to edk2-devel [1] (patch series "Measured SEV boot with kernel/initrd/cmdline"); it's still under review.

[1] https://edk2.groups.io/g/devel/topic/patch_v1_0_8_measured_sev/83074450

---

v3 changes:
- initrd hash is now mandatory; if no -initrd is passed, calculate the hash of the empty buffer. This is now aligned with the OVMF behaviour which verifies the empty initrd (correctly).
- make SevHashTable entries fixed: 3 entries for cmdline, initrd, and kernel.
- in sev_add_kernel_loader_hashes: first calculate all the hashes, only then fill-in the hashes table in the guest's memory.
- Use g_assert_not_reached in sev-stub.c.
- Use QEMU_PACKED attribute for structs.
- Use QemuUUID type for guids.
- in sev_add_kernel_loader_hashes: use ARRAY_SIZE(iov) instead of literal 2.

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v2 changes:
- Extract main functionality to sev.c (with empty stub in sev-stub.c)
- Use sev_enabled() instead of machine->cgs->ready to detect SEV guest
- Coding style changes

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Dov Murik (2):
sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux
boot
x86/sev: generate SEV kernel loader hashes in x86_load_linux

 target/i386/sev_i386.h |  12 ++++
 hw/i386/x86.c          |  25 +++++++-
 target/i386/sev-stub.c |   5 ++
 target/i386/sev.c      | 137 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)

base-commit: b22726abdfa54592d6ad88f65b0297c0e8b363e2

#6 lore raw
Quoted 6 lines · Connor Kuehl

Hi Paolo,

Please consider this series[1] for inclusion into your next pull request.

Just a note that this series has a companion series that is getting upstreamed into OVMF[2]

Shouldn't we get the OVMF part merged first?

Quoted 37 lines

[1] Patchwork link, if convenient: https://patchwork.kernel.org/project/qemu-devel/cover/[email protected]/
[2] https://bugzilla.tianocore.org/show_bug.cgi?id=3457#c6

Thank you,

Connor

Quoted 28 lines · Dov Murik

Currently booting with -kernel/-initrd/-append is not supported in SEV confidential guests, because the content of these blobs is not measured and therefore not trusted by the SEV guest.

However, in some cases the kernel, initrd, and cmdline are not secret but should not be modified by the host. In such a case, we want to verify inside the trusted VM that the kernel, initrd, and cmdline are indeed the ones expected by the Guest Owner, and only if that is the case go on and boot them up (removing the need for grub inside OVMF in that mode).

To support that, OVMF adds a special area for hashes of kernel/initrd/cmdline; that area is expected to be filled by QEMU and encrypted as part of the initial SEV guest launch. This in turn makes the hashes part of the PSP measured content, and OVMF can trust these inputs if they match the hashes.

This series adds an SEV function to generate the table of hashes for OVMF and encrypt it (patch 1/2), and calls this function if SEV is enabled when the kernel/initrd/cmdline are prepared (patch 2/2).

Corresponding OVMF support was submitted to edk2-devel [1] (patch series "Measured SEV boot with kernel/initrd/cmdline"); it's still under review.

[1] https://edk2.groups.io/g/devel/topic/patch_v1_0_8_measured_sev/83074450

---

#7 lore raw
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Connor Kuehl

Hi Paolo,

Please consider this series[1] for inclusion into your next pull request.

Just a note that this series has a companion series that is getting upstreamed into OVMF[2]

Shouldn't we get the OVMF part merged first?

The approach taken in the OVMF series doesn't seem very controversial, so I don't anticipate any breaking changes with the current state of those patches as far as QEMU is concerned.

However, I'm fine with erring on the side of caution.

Connor

author
#8 lore raw

Hi Paolo, Phil, Connor,

Quoted 10 lines · Connor Kuehl
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 6 lines · Connor Kuehl

Hi Paolo,

Please consider this series[1] for inclusion into your next pull request.

Just a note that this series has a companion series that is getting upstreamed into OVMF[2]

Shouldn't we get the OVMF part merged first?

The OVMF companion series has been reviewed by the new OVMF maintainer and merged to edk2 master branch as of edk2 commit 514b3aa08ece [1].

[1] https://github.com/tianocore/edk2/commit/514b3aa08ece

Thanks,
Dov

Quoted 9 lines

The approach taken in the OVMF series doesn't seem very controversial, so I don't anticipate any breaking changes with the current state of those patches as far as QEMU is concerned.

However, I'm fine with erring on the side of caution.

Connor

author
#10 lore raw
Quoted 9 lines · Connor Kuehl
Quoted 4 lines · Dov Murik

The OVMF companion series has been reviewed by the new OVMF maintainer and merged to edk2 master branch as of edk2 commit 514b3aa08ece [1].

[1] https://github.com/tianocore/edk2/commit/514b3aa08ece

Awesome! Unfortunately, it's looking like we'll have to wait[1] for QEMU to thaw before this series goes in.

Thanks for explaining this. Do I need to do anything after 6.1 is released? Ping? Rebase and re-send?

-Dov

Quoted 4 lines

Connor

[1] https://wiki.qemu.org/Planning/6.1

#11 lore raw
Quoted 8 lines · Dov Murik
Quoted 5 lines

Awesome! Unfortunately, it's looking like we'll have to wait[1] for QEMU to thaw before this series goes in.

Thanks for explaining this. Do I need to do anything after 6.1 is released? Ping? Rebase and re-send?

Rebase and re-send. I think your patches already have the Reviewed-by tags in the patch descriptions, but if that's not the case, make sure you add them for the re-send so it's obvious that the patches have already been reviewed.

Thank you,

Connor

Acked-by on the v3 series
#12 lore raw
In reply to Dov Murik
Quoted 21 lines · Dov Murik

Currently booting with -kernel/-initrd/-append is not supported in SEV confidential guests, because the content of these blobs is not measured and therefore not trusted by the SEV guest.

However, in some cases the kernel, initrd, and cmdline are not secret but should not be modified by the host. In such a case, we want to verify inside the trusted VM that the kernel, initrd, and cmdline are indeed the ones expected by the Guest Owner, and only if that is the case go on and boot them up (removing the need for grub inside OVMF in that mode).

To support that, OVMF adds a special area for hashes of kernel/initrd/cmdline; that area is expected to be filled by QEMU and encrypted as part of the initial SEV guest launch. This in turn makes the hashes part of the PSP measured content, and OVMF can trust these inputs if they match the hashes.

This series adds an SEV function to generate the table of hashes for OVMF and encrypt it (patch 1/2), and calls this function if SEV is enabled when the kernel/initrd/cmdline are prepared (patch 2/2).

Quoted 5 lines

Corresponding OVMF support was submitted to edk2-devel [1] (patch series "Measured SEV boot with kernel/initrd/cmdline"); it's still under review.

[1] https://edk2.groups.io/g/devel/topic/patch_v1_0_8_measured_sev/83074450

Now that I figured the measurement angle

Quoted 37 lines

---

v3 changes:
- initrd hash is now mandatory; if no -initrd is passed, calculate the hash of the empty buffer. This is now aligned with the OVMF behaviour which verifies the empty initrd (correctly).
- make SevHashTable entries fixed: 3 entries for cmdline, initrd, and kernel.
- in sev_add_kernel_loader_hashes: first calculate all the hashes, only then fill-in the hashes table in the guest's memory.
- Use g_assert_not_reached in sev-stub.c.
- Use QEMU_PACKED attribute for structs.
- Use QemuUUID type for guids.
- in sev_add_kernel_loader_hashes: use ARRAY_SIZE(iov) instead of literal 2.

v2: https://lore.kernel.org/qemu-devel/[email protected]/
v2 changes:
- Extract main functionality to sev.c (with empty stub in sev-stub.c)
- Use sev_enabled() instead of machine->cgs->ready to detect SEV guest
- Coding style changes

v1: https://lore.kernel.org/qemu-devel/[email protected]/

Dov Murik (2):
sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux
boot
x86/sev: generate SEV kernel loader hashes in x86_load_linux

 target/i386/sev_i386.h |  12 ++++
 hw/i386/x86.c          |  25 +++++++-
 target/i386/sev-stub.c |   5 ++
 target/i386/sev.c      | 137 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 178 insertions(+), 1 deletion(-)

base-commit: b22726abdfa54592d6ad88f65b0297c0e8b363e2

Signature · 1 line
2.25.1