Skip to contentSkip to conversation

hw/i386: fix unbounded stack for load_multiboot

PATCH Superseded Peter Xu 2 participants 3 messages Mar 9, 2016

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 hw/i386: fix unbounded stack for load_multiboot

Use heap rather than stack for kcmdline.

---
 hw/i386/multiboot.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/i386/multiboot.c +2 -3
index 9e164e6..bc45394 100644
@@ -324,10 +324,9 @@ int load_multiboot(FWCfgState *fw_cfg,
     }
 
     /* Commandline support */
-    char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
-    snprintf(kcmdline, sizeof(kcmdline), "%s %s",
-             kernel_filename, kernel_cmdline);
+    char *kcmdline = g_strdup_printf("%s %s", kernel_filename, kernel_cmdline);
     stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
+    g_free(kcmdline);
 
     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));
 
Signature · 1 line
2.4.3
#1 lore raw
Quoted 13 lines · Peter Xu

Use heap rather than stack for kcmdline.

---
 hw/i386/multiboot.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/i386/multiboot.c +0 -0
index 9e164e6..bc45394 100644
@@ -324,10 +324,9 @@ int load_multiboot(FWCfgState *fw_cfg,
     }

hw/i386/multiboot.c int load_multiboot(FWCfgState *fw_cfg, · line 331

Quoted 10 lines
hw/i386/multiboot.c +2 -3
 
     /* Commandline support */
-    char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
-    snprintf(kcmdline, sizeof(kcmdline), "%s %s",
-             kernel_filename, kernel_cmdline);
+    char *kcmdline = g_strdup_printf("%s %s", kernel_filename, kernel_cmdline);
     stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
+    g_free(kcmdline);
 
     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));

While at it, it's better to move the variable declaration to the beginning of function.

Fam

author
#2 lore raw
Quoted 23 lines · Fam Zheng
Quoted 22 lines · Peter Xu

Use heap rather than stack for kcmdline.

---
 hw/i386/multiboot.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
hw/i386/multiboot.c +2 -3
index 9e164e6..bc45394 100644
@@ -324,10 +324,9 @@ int load_multiboot(FWCfgState *fw_cfg,
     }
 
     /* Commandline support */
-    char kcmdline[strlen(kernel_filename) + strlen(kernel_cmdline) + 2];
-    snprintf(kcmdline, sizeof(kcmdline), "%s %s",
-             kernel_filename, kernel_cmdline);
+    char *kcmdline = g_strdup_printf("%s %s", kernel_filename, kernel_cmdline);
     stl_p(bootinfo + MBI_CMDLINE, mb_add_cmdline(&mbs, kcmdline));
+    g_free(kcmdline);
 
Quoted 4 lines

hw/i386/multiboot.c int load_multiboot(FWCfgState *fw_cfg, · line 331

Quoted 1 line
hw/i386/multiboot.c +0 -0
     stl_p(bootinfo + MBI_BOOTLOADER, mb_add_bootloader(&mbs, bootloader_name));

While at it, it's better to move the variable declaration to the beginning of function.

Yes, I can do that. Let me post v2.

Thanks.
Peter