diff mbox

hw/i386: fix unbounded stack for load_multiboot

Message ID 1457504091-31887-1-git-send-email-peterx@redhat.com
State New
Headers show

Commit Message

Peter Xu March 9, 2016, 6:14 a.m. UTC
Use heap rather than stack for kcmdline.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 hw/i386/multiboot.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

Comments

Fam Zheng March 9, 2016, 7:42 a.m. UTC | #1
On Wed, 03/09 14:14, Peter Xu wrote:
> Use heap rather than stack for kcmdline.
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  hw/i386/multiboot.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/i386/multiboot.c b/hw/i386/multiboot.c
> index 9e164e6..bc45394 100644
> --- a/hw/i386/multiboot.c
> +++ b/hw/i386/multiboot.c
> @@ -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));

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

Fam
Peter Xu March 9, 2016, 8:27 a.m. UTC | #2
On Wed, Mar 09, 2016 at 03:42:59PM +0800, Fam Zheng wrote:
> On Wed, 03/09 14:14, Peter Xu wrote:
> > Use heap rather than stack for kcmdline.
> > 
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  hw/i386/multiboot.c | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/hw/i386/multiboot.c b/hw/i386/multiboot.c
> > index 9e164e6..bc45394 100644
> > --- a/hw/i386/multiboot.c
> > +++ b/hw/i386/multiboot.c
> > @@ -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));
> 
> 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
diff mbox

Patch

diff --git a/hw/i386/multiboot.c b/hw/i386/multiboot.c
index 9e164e6..bc45394 100644
--- a/hw/i386/multiboot.c
+++ b/hw/i386/multiboot.c
@@ -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));