Skip to contentSkip to conversation

x86: fix fallout from switching setup_data from kernel image to cmdline

PATCH In review Jason A. Donenfeld 2 participants 5 messages Feb 7, 2023 – Feb 8, 2023

lore
Share & tools
lore
#0 lore raw

cover x86: fix fallout from switching setup_data from kernel image to cmdline

With eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data"), the rng seed setup_data is now appended to the cmdline file rather than the kernel image file. In the process of doing that, two things were left out: the check for sev_enabled(), and resetting the RNG seed when selecting the cmdline. This short series fixes those up.

Cc "Michael S. Tsirkin" <[email protected]>
Cc Dov Murik <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>

Dov Murik (1):
x86: don't append setup_data to cmdline for SEV guests

Jason A. Donenfeld (1):
x86: reset rng seed when reading cmdline, not kernel image

 hw/i386/x86.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
Signature · 1 line
2.39.1
#1 lore raw

PATCH 1/2 x86: reset rng seed when reading cmdline, not kernel image

With eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data"), the rng seed setup_data is now appended to the cmdline file rather than the kernel image file. But in the process, the code to re-randomize the seed when selecting the image was left out. So, change the re-randomization over to trigger when selecting the cmdline, rather than the kernel image.

Fixes eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data")
---
 hw/i386/x86.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
hw/i386/x86.c +4 -4
index eaff4227bd..c6d7bf6db2 100644
@@ -1114,15 +1114,15 @@ void x86_load_linux(X86MachineState *x86ms,
         setup_data->len = cpu_to_le32(RNG_SEED_LENGTH);
         qemu_guest_getrandom_nofail(setup_data->data, RNG_SEED_LENGTH);
         qemu_register_reset_nosnapshotload(reset_rng_seed, setup_data);
-        fw_cfg_add_bytes_callback(fw_cfg, FW_CFG_KERNEL_DATA, reset_rng_seed, NULL,
-                                  setup_data, kernel, kernel_size, true);
+        fw_cfg_add_bytes_callback(fw_cfg, FW_CFG_CMDLINE_DATA, reset_rng_seed, NULL,
+                                  setup_data, kernel_cmdline, cmdline_size, true);
     } else {
-        fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
+        fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline, cmdline_size);
     }
 
+    fw_cfg_add_bytes(fw_cfg, FW_CFG_KERNEL_DATA, kernel, kernel_size);
     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_ADDR, cmdline_addr);
     fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, cmdline_size);
-    fw_cfg_add_bytes(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline, cmdline_size);
     sev_load_ctx.cmdline_data = (char *)kernel_cmdline;
     sev_load_ctx.cmdline_size = cmdline_size;
 
Signature · 1 line
2.39.1
#2 lore raw

PATCH 2/2 x86: don't append setup_data to cmdline for SEV guests

In reply to Jason A. Donenfeld

From: Dov Murik <[email protected]>

Modifying the cmdline by appending setup_data breaks measured boot with SEV and OVMF, and possibly signed boot. Previously this was disabled when appending to the kernel image, but with eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data"), this was changed to the cmdline file instead, with the sev_enabled() check left out.

Fixes eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data")
---
 hw/i386/x86.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/x86.c +2 -2
index c6d7bf6db2..80a1678acd 100644
@@ -1079,7 +1079,7 @@ void x86_load_linux(X86MachineState *x86ms,
     fclose(f);
 
     /* append dtb to kernel */
-    if (dtb_filename) {
+    if (dtb_filename && !sev_enabled()) {
         if (protocol < 0x209) {
             fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
             exit(1);
@@ -1103,7 +1103,7 @@ void x86_load_linux(X86MachineState *x86ms,
         load_image_size(dtb_filename, setup_data->data, dtb_size);
     }
 
-    if (!legacy_no_rng_seed && protocol >= 0x209) {
+    if (!legacy_no_rng_seed && protocol >= 0x209 && !sev_enabled()) {
         setup_data_offset = cmdline_size;
         cmdline_size += sizeof(SetupData) + RNG_SEED_LENGTH;
         kernel_cmdline = g_realloc(kernel_cmdline, cmdline_size);
Signature · 1 line
2.39.1
#3 lore raw

Re: [PATCH 2/2] x86: don't append setup_data to cmdline for SEV guests

Quoted 21 lines · Jason A. Donenfeld

From: Dov Murik <[email protected]>

Modifying the cmdline by appending setup_data breaks measured boot with SEV and OVMF, and possibly signed boot. Previously this was disabled when appending to the kernel image, but with eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data"), this was changed to the cmdline file instead, with the sev_enabled() check left out.

Fixes eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data")
---
  hw/i386/x86.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/x86.c +0 -0
index c6d7bf6db2..80a1678acd 100644

hw/i386/x86.c void x86_load_linux(X86MachineState *x86ms, · line 1082

Quoted 5 lines
hw/i386/x86.c +1 -1
@@ -1079,7 +1079,7 @@ void x86_load_linux(X86MachineState *x86ms,
      fclose(f);
 
      /* append dtb to kernel */
-    if (dtb_filename) {
+    if (dtb_filename && !sev_enabled()) {

Is this change necessary? From an SEV point of view, the DTB file should be "constant" and so a guest owner will be able to use that to correctly verify the SEV LAUNCH measurement. Additionally, it won't change from the time it was measured to the time OVMF validates everything. Of course, I don't really anticipate that a DTB file would be used with an SEV guest, anyway.

@Dov, does that make sense?

Thanks,
Tom

Quoted 12 lines
hw/i386/x86.c +1 -1
          if (protocol < 0x209) {
              fprintf(stderr, "qemu: Linux kernel too old to load a dtb\n");
              exit(1);
@@ -1103,7 +1103,7 @@ void x86_load_linux(X86MachineState *x86ms,
          load_image_size(dtb_filename, setup_data->data, dtb_size);
      }
 
-    if (!legacy_no_rng_seed && protocol >= 0x209) {
+    if (!legacy_no_rng_seed && protocol >= 0x209 && !sev_enabled()) {
          setup_data_offset = cmdline_size;
          cmdline_size += sizeof(SetupData) + RNG_SEED_LENGTH;
          kernel_cmdline = g_realloc(kernel_cmdline, cmdline_size);
#4 lore raw

Re: [PATCH 2/2] x86: don't append setup_data to cmdline for SEV guests

Quoted 25 lines · Tom Lendacky
Quoted 23 lines · Jason A. Donenfeld

From: Dov Murik <[email protected]>

Modifying the cmdline by appending setup_data breaks measured boot with SEV and OVMF, and possibly signed boot. Previously this was disabled when appending to the kernel image, but with eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data"), this was changed to the cmdline file instead, with the sev_enabled() check left out.

Fixes eac7a7791bb6 ("x86: don't let decompressed kernel image clobber setup_data")
---
  hw/i386/x86.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/i386/x86.c +0 -0
index c6d7bf6db2..80a1678acd 100644
@@ -1079,7 +1079,7 @@ void x86_load_linux(X86MachineState *x86ms,
      fclose(f);
 
Quoted 10 lines

hw/i386/x86.c void x86_load_linux(X86MachineState *x86ms, · line 1082

Quoted 3 lines
hw/i386/x86.c +1 -1
      /* append dtb to kernel */
-    if (dtb_filename) {
+    if (dtb_filename && !sev_enabled()) {

Is this change necessary? From an SEV point of view, the DTB file should be "constant" and so a guest owner will be able to use that to correctly verify the SEV LAUNCH measurement. Additionally, it won't change from the time it was measured to the time OVMF validates everything. Of course, I don't really anticipate that a DTB file would be used with an SEV guest, anyway.

Yes, it does make sense to do it like this 2/2 patch does here. (I made the change for the DTB.) The reason is that the first setup_data link is already conditionalized on sev:

/*
* 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() && first_setup_data) {
       SetupDataFixup *fixup = g_malloc(sizeof(*fixup));

       memcpy(setup, header, MIN(sizeof(header), setup_size));
       /* Offset 0x250 is a pointer to the first setup_data link. */