qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hw/i386/fw_cfg: Add etc/e820 to fw_cfg late
@ 2024-06-17 13:46 David Woodhouse
  2024-06-17 14:15 ` Peter Maydell
  0 siblings, 1 reply; 3+ messages in thread
From: David Woodhouse @ 2024-06-17 13:46 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael S. Tsirkin, Marcel Apfelbaum, Paolo Bonzini,
	Richard Henderson, Eduardo Habkost, Sergio Lopez, Peter Maydell

[-- Attachment #1: Type: text/plain, Size: 3937 bytes --]

From: David Woodhouse <dwmw@amazon.co.uk>

In e820_add_entry() the e820_table is reallocated with g_renew() to make
space for a new entry. However, fw_cfg_arch_create() just uses the existing
e820_table pointer.

This leads to a use-after-free if anything adds a new entry after fw_cfg
is set up. Shift the addition of the etc/e820 file to the machine done
notifier, and add a sanity check to ensure that e820_table isn't
modified after the pointer gets stashed.

Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
---
 hw/i386/e820_memory_layout.c | 8 ++++++++
 hw/i386/fw_cfg.c             | 7 ++++---
 hw/i386/microvm.c            | 5 +++--
 3 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/hw/i386/e820_memory_layout.c b/hw/i386/e820_memory_layout.c
index 06970ac44a..c96515909e 100644
--- a/hw/i386/e820_memory_layout.c
+++ b/hw/i386/e820_memory_layout.c
@@ -8,13 +8,20 @@
 
 #include "qemu/osdep.h"
 #include "qemu/bswap.h"
+#include "qemu/error-report.h"
 #include "e820_memory_layout.h"
 
 static size_t e820_entries;
 struct e820_entry *e820_table;
+static gboolean e820_done;
 
 int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
 {
+    if (e820_done) {
+        warn_report("warning: E820 modified after being consumed");
+        return -1;
+    }
+
     /* new "etc/e820" file -- include ram and reserved entries */
     e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
     e820_table[e820_entries].address = cpu_to_le64(address);
@@ -27,6 +34,7 @@ int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
 
 int e820_get_num_entries(void)
 {
+    e820_done = true;
     return e820_entries;
 }
 
diff --git a/hw/i386/fw_cfg.c b/hw/i386/fw_cfg.c
index 6e0d9945d0..e046ad1a54 100644
--- a/hw/i386/fw_cfg.c
+++ b/hw/i386/fw_cfg.c
@@ -102,6 +102,10 @@ void fw_cfg_build_smbios(PCMachineState *pcms, FWCfgState *fw_cfg,
                         smbios_anchor, smbios_anchor_len);
     }
 #endif
+
+    /* Add etc/e820 late, once all regions should be present */
+    fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
+                    sizeof(struct e820_entry) * e820_get_num_entries());
 }
 
 FWCfgState *fw_cfg_arch_create(MachineState *ms,
@@ -139,9 +143,6 @@ FWCfgState *fw_cfg_arch_create(MachineState *ms,
 #endif
     fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1);
 
-    fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
-                    sizeof(struct e820_entry) * e820_get_num_entries());
-
     fw_cfg_add_bytes(fw_cfg, FW_CFG_HPET, &hpet_cfg, sizeof(hpet_cfg));
     /* allocate memory for the NUMA channel: one (64bit) word for the number
      * of nodes, one word for each VCPU->node and one word for each node to
diff --git a/hw/i386/microvm.c b/hw/i386/microvm.c
index fec63cacfa..89b2abcebf 100644
--- a/hw/i386/microvm.c
+++ b/hw/i386/microvm.c
@@ -324,8 +324,6 @@ static void microvm_memory_init(MicrovmMachineState *mms)
     fw_cfg_add_i16(fw_cfg, FW_CFG_MAX_CPUS, machine->smp.max_cpus);
     fw_cfg_add_i64(fw_cfg, FW_CFG_RAM_SIZE, (uint64_t)machine->ram_size);
     fw_cfg_add_i32(fw_cfg, FW_CFG_IRQ0_OVERRIDE, 1);
-    fw_cfg_add_file(fw_cfg, "etc/e820", e820_table,
-                    sizeof(struct e820_entry) * e820_get_num_entries());
 
     rom_set_fw(fw_cfg);
 
@@ -586,9 +584,12 @@ static void microvm_machine_done(Notifier *notifier, void *data)
 {
     MicrovmMachineState *mms = container_of(notifier, MicrovmMachineState,
                                             machine_done);
+    X86MachineState *x86ms = X86_MACHINE(mms);
 
     acpi_setup_microvm(mms);
     dt_setup_microvm(mms);
+    fw_cfg_add_file(x86ms->fw_cfg, "etc/e820", e820_table,
+                    sizeof(struct e820_entry) * e820_get_num_entries());
 }
 
 static void microvm_powerdown_req(Notifier *notifier, void *data)
-- 
2.44.0



[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5965 bytes --]

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-06-17 14:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17 13:46 [PATCH] hw/i386/fw_cfg: Add etc/e820 to fw_cfg late David Woodhouse
2024-06-17 14:15 ` Peter Maydell
2024-06-17 14:36   ` David Woodhouse

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).