pc_init: Fail on bad kernel
PATCH 1/1 pc_init: Fail on bad kernel
When providing QEMU with a bad '-kernel' parameter, such as a file which is not really a kernel, QEMU will attempt to allocate a huge amount of memory and fail either with "Failed to allocate memory: Cannot allocate memory" or a GLib error: "GLib-ERROR **: gmem.c:170: failed to allocate 18446744073709529965 bytes"
This patch handles the case where the magic sig wasn't located in the provided kernel, and loading it as multiboot failed as well.
--- hw/pc.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
hw/pc.c +7 -1
@@ -691,8 +691,14 @@ static void load_linux(void *fw_cfg,/* This looks like a multiboot kernel. If it is, let's stop treating it like a Linux kernel. */ if (load_multiboot (fw_cfg, f, kernel_filename, initrd_filename, - kernel_cmdline, kernel_size, header)) + kernel_cmdline, kernel_size, header)) {return ; + }else { +fprintf (stderr,"qemu: could not load kernel '%s': %s\n" , + kernel_filename,strerror (errno)); +exit (1 ); + } + protocol =0 ; }
Signature · 1 line
1.7.6.1
Quoted 22 lines · Sasha Levin
When providing QEMU with a bad '-kernel' parameter, such as a file which is not really a kernel, QEMU will attempt to allocate a huge amount of memory and fail either with "Failed to allocate memory: Cannot allocate memory" or a GLib error: "GLib-ERROR **: gmem.c:170: failed to allocate 18446744073709529965 bytes"
This patch handles the case where the magic sig wasn't located in the provided kernel, and loading it as multiboot failed as well.
Cc Anthony Liguori<[email protected]>Signed-off-by Sasha Levin<[email protected]>--- hw/pc.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)hw/pc.c +0
-0@@ -691,8 +691,14 @@ static void load_linux(void *fw_cfg,/* This looks like a multiboot kernel. If it is, let's stop treating it like a Linux kernel. */
hw/pc.c static void load_linux(void *fw_cfg, · line 701
Quoted 10 lines
hw/pc.c +7
-1if (load_multiboot (fw_cfg, f, kernel_filename, initrd_filename, - kernel_cmdline, kernel_size, header)) + kernel_cmdline, kernel_size, header)) {return ; + }else { +fprintf (stderr,"qemu: could not load kernel '%s': %s\n" , + kernel_filename,strerror (errno)); +exit (1 ); + } +
There's trailing whitespace on this line.
But I also don't think this is the right fix. This change makes the line below unreachable. There is still code in this path attempting to handle protocols < 2.00. Admittedly, these would be ancient kernels that I doubt anyone would really use but the code is there to support it nonetheless.
I think a better fix would be to positively identify kernels that are older than this.
Perhaps hpa knows how we could positively identify a kernel that's older than protocol 200?
Regards,
Anthony Liguori
Quoted 3 lines
hw/pc.c +0
-0protocol =0 ; }
Ping?
Quoted 35 lines · Sasha Levin
When providing QEMU with a bad '-kernel' parameter, such as a file which is not really a kernel, QEMU will attempt to allocate a huge amount of memory and fail either with "Failed to allocate memory: Cannot allocate memory" or a GLib error: "GLib-ERROR **: gmem.c:170: failed to allocate 18446744073709529965 bytes"
This patch handles the case where the magic sig wasn't located in the provided kernel, and loading it as multiboot failed as well.
Cc Anthony Liguori <[email protected]>Signed-off-by Sasha Levin <[email protected]>--- hw/pc.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)hw/pc.c +7
-1@@ -691,8 +691,14 @@ static void load_linux(void *fw_cfg,/* This looks like a multiboot kernel. If it is, let's stop treating it like a Linux kernel. */ if (load_multiboot (fw_cfg, f, kernel_filename, initrd_filename, - kernel_cmdline, kernel_size, header)) + kernel_cmdline, kernel_size, header)) {return ; + }else { +fprintf (stderr,"qemu: could not load kernel '%s': %s\n" , + kernel_filename,strerror (errno)); +exit (1 ); + } + protocol =0 ; }
Signature · 1 line
Sasha.
Quoted 1 line · Sasha Levin
Ping?
I replied on 9/9 with comments.
Regards,
Anthony Liguori
Quoted 38 lines · Sasha Levin
Quoted 35 lines
When providing QEMU with a bad '-kernel' parameter, such as a file which is not really a kernel, QEMU will attempt to allocate a huge amount of memory and fail either with "Failed to allocate memory: Cannot allocate memory" or a GLib error: "GLib-ERROR **: gmem.c:170: failed to allocate 18446744073709529965 bytes"
This patch handles the case where the magic sig wasn't located in the provided kernel, and loading it as multiboot failed as well.
Cc Anthony Liguori<[email protected]>Signed-off-by Sasha Levin<[email protected]>--- hw/pc.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)hw/pc.c +7
-1@@ -691,8 +691,14 @@ static void load_linux(void *fw_cfg,/* This looks like a multiboot kernel. If it is, let's stop treating it like a Linux kernel. */ if (load_multiboot (fw_cfg, f, kernel_filename, initrd_filename, - kernel_cmdline, kernel_size, header)) + kernel_cmdline, kernel_size, header)) {return ; + }else { +fprintf (stderr,"qemu: could not load kernel '%s': %s\n" , + kernel_filename,strerror (errno)); +exit (1 ); + } + protocol =0 ; }