Skip to contentSkip to conversation

hw/i386/elfboot: allocate "header" in heap

PATCH Superseded [email protected] 3 participants 5 messages Nov 8, 2024 – Nov 18, 2024

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/i386/elfboot: allocate "header" in heap

From: Sergio Lopez <[email protected]>

In x86_load_linux(), we were using a stack-allocated array as data for fw_cfg_add_bytes(). Since the latter just takes a reference to the pointer instead of copying the data, it can happen that the contents have been overridden by the time the guest attempts to access them.

Instead of using the stack-allocated array, allocate some memory from the heap, copy the contents of the array, and use it for fw_cfg.

---
 hw/i386/x86-common.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/i386/x86-common.c +4 -1
index bc360a9ea4..d51ebad4d6 100644
@@ -697,9 +697,12 @@ void x86_load_linux(X86MachineState *x86ms,
                 strlen(kernel_cmdline) + 1);
             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
 
+            setup = g_malloc(sizeof(header));
+            memcpy(setup, header, sizeof(header));
+
             fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
             fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
-                             header, sizeof(header));
+                             setup, sizeof(header));
 
             /* load initrd */
             if (initrd_filename) {
Signature · 1 line
2.45.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · On Sat, Nov 09, 2024 at 12:03:14AM +0100, [email protected] wrote:

From: Sergio Lopez <[email protected]>

In x86_load_linux(), we were using a stack-allocated array as data for fw_cfg_add_bytes(). Since the latter just takes a reference to the pointer instead of copying the data, it can happen that the contents have been overridden by the time the guest attempts to access them.

Instead of using the stack-allocated array, allocate some memory from the heap, copy the contents of the array, and use it for fw_cfg.

Wow. How did this ever work?

Paolo, you queueing this?

Quoted 24 lines
---
 hw/i386/x86-common.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/i386/x86-common.c +4 -1
index bc360a9ea4..d51ebad4d6 100644
@@ -697,9 +697,12 @@ void x86_load_linux(X86MachineState *x86ms,
                 strlen(kernel_cmdline) + 1);
             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
 
+            setup = g_malloc(sizeof(header));
+            memcpy(setup, header, sizeof(header));
+
             fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
             fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
-                             header, sizeof(header));
+                             setup, sizeof(header));
 
             /* load initrd */
             if (initrd_filename) {
Signature · 1 line
2.45.2
#2 lore raw
Quoted 8 lines · Michael S. Tsirkin
Quoted 7 lines · On Sat, Nov 09, 2024 at 12:03:14AM +0100, [email protected] wrote:

From: Sergio Lopez <[email protected]>

In x86_load_linux(), we were using a stack-allocated array as data for fw_cfg_add_bytes(). Since the latter just takes a reference to the pointer instead of copying the data, it can happen that the contents have been overridden by the time the guest attempts to access them.

Quoted 7 lines
Quoted 4 lines

Instead of using the stack-allocated array, allocate some memory from the heap, copy the contents of the array, and use it for fw_cfg.

Wow. How did this ever work?

I guess, for quite a while, we were just lucky that memory region wasn't touched by the time the FW in the guest uses it (possibly helped by the fact this happens very early in the VM lifetime). In recent versions/builds this is no longer the case.

Sergio.

Reviewed-by on v1 1/1
#3 lore raw
In reply to [email protected]
Quoted 20 lines · On 8/11/24 23:03, [email protected] wrote:

From: Sergio Lopez <[email protected]>

In x86_load_linux(), we were using a stack-allocated array as data for fw_cfg_add_bytes(). Since the latter just takes a reference to the pointer instead of copying the data, it can happen that the contents have been overridden by the time the guest attempts to access them.

Instead of using the stack-allocated array, allocate some memory from the heap, copy the contents of the array, and use it for fw_cfg.

---
  hw/i386/x86-common.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/i386/x86-common.c +0 -0
index bc360a9ea4..d51ebad4d6 100644

hw/i386/x86-common.c void x86_load_linux(X86MachineState *x86ms, · line 705

Quoted 10 lines
hw/i386/x86-common.c +4 -1
@@ -697,9 +697,12 @@ void x86_load_linux(X86MachineState *x86ms,
                  strlen(kernel_cmdline) + 1);
              fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, kernel_cmdline);
 
+            setup = g_malloc(sizeof(header));
+            memcpy(setup, header, sizeof(header));
+
              fw_cfg_add_i32(fw_cfg, FW_CFG_SETUP_SIZE, sizeof(header));
              fw_cfg_add_bytes(fw_cfg, FW_CFG_SETUP_DATA,
-                             header, sizeof(header));
+                             setup, sizeof(header));

Preferably using g_memdup2(header, sizeof(header)),

Quoted 3 lines
hw/i386/x86-common.c +0 -0
              /* load initrd */
              if (initrd_filename) {
#4 lore raw
In reply to [email protected]
Quoted 7 lines · On 8/11/24 23:03, [email protected] wrote:

From: Sergio Lopez <[email protected]>

In x86_load_linux(), we were using a stack-allocated array as data for fw_cfg_add_bytes(). Since the latter just takes a reference to the pointer instead of copying the data, it can happen that the contents have been overridden by the time the guest attempts to access them.

Quoted 7 lines

Instead of using the stack-allocated array, allocate some memory from the heap, copy the contents of the array, and use it for fw_cfg.

---
  hw/i386/x86-common.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)

Patch queued to hw-misc, thanks.