Skip to contentSkip to conversation

hw/i386/pc: reject to boot a wrong header magic kernel

PATCH Superseded liguang 2 participants 5 messages Mar 27, 2013 – Apr 1, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

liguang

author
#0 lore raw

PATCH 1/1 hw/i386/pc: reject to boot a wrong header magic kernel

if head magic is missing or wrong unexpectedly, we'd better to reject booting.
e.g.
I make a mistake to boot a vmlinuz for MIPS(which I think it's for x86) like this:
qemu-system-x86_64 -kernel vmlinuz -initrd demord then qemu report:
"qemu: linux kernel too old to load a ram disk" that's misleading.

---
 hw/i386/pc.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/i386/pc.c +3 -1
index b1e06fa..2b78dfc 100644
@@ -683,8 +683,10 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            fprintf(stderr, "please assure specicified kernel is for x86!\n");
+            exit(1);
         }
-        protocol = 0;
     }
 
     if (protocol < 0x200 || !(header[0x211] & 0x01)) {
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 20 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to reject booting.
e.g.
I make a mistake to boot a vmlinuz for MIPS(which I think it's for x86) like this:
qemu-system-x86_64 -kernel vmlinuz -initrd demord then qemu report:
"qemu: linux kernel too old to load a ram disk" that's misleading.

---
 hw/i386/pc.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
hw/i386/pc.c +0 -0
index b1e06fa..2b78dfc 100644

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 688

Quoted 6 lines
hw/i386/pc.c +3 -0
@@ -683,8 +683,10 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            fprintf(stderr, "please assure specicified kernel is for x86!\n");
+            exit(1);

load_multiboot() can fail for other reasons so this error messing is misleading. Giving QEMU a non-x86 kernel is just one scenario where this may fail.

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 690

Quoted 3 lines
hw/i386/pc.c +0 -1
         }
-        protocol = 0;
     }

Why did you drop protocol = 0?

Stefan

author
#2 lore raw
Quoted 16 lines · 在 2013-03-28四的 10:42 +0100,Stefan Hajnoczi写道:
Quoted 15 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to reject booting.
e.g.
I make a mistake to boot a vmlinuz for MIPS(which I think it's for x86) like this:
qemu-system-x86_64 -kernel vmlinuz -initrd demord then qemu report:
"qemu: linux kernel too old to load a ram disk" that's misleading.

---
 hw/i386/pc.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Quoted 15 lines

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 688

Quoted 11 lines
hw/i386/pc.c +3 -0
index b1e06fa..2b78dfc 100644
@@ -683,8 +683,10 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            fprintf(stderr, "please assure specicified kernel is for x86!\n");
+            exit(1);

load_multiboot() can fail for other reasons so this error messing is misleading. Giving QEMU a non-x86 kernel is just one scenario where this may fail.

according to my check of load_mutiboot function,
mostly it will return 0 if it's not multboot,
or 1 it's a multiboot, so print this message,
or can I just print "wrong kernel image!" ?

Quoted 6 lines

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 690

Quoted 4 lines
hw/i386/pc.c +0 -1
         }
-        protocol = 0;
     }

Why did you drop protocol = 0?

I think we only want either normal or multi boot linux kernel, I can't see meaning let other case go on.
so, here, if a normal kernel, OK, go on,
if multiboot, OK, go on,
others, NO, end up.
so, "protocol = 0" is meaningless here.

#3 lore raw
Quoted 33 lines · li guang
Quoted 16 lines · 在 2013-03-28四的 10:42 +0100,Stefan Hajnoczi写道:
Quoted 15 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to reject booting.
e.g.
I make a mistake to boot a vmlinuz for MIPS(which I think it's for x86) like this:
qemu-system-x86_64 -kernel vmlinuz -initrd demord then qemu report:
"qemu: linux kernel too old to load a ram disk" that's misleading.

---
 hw/i386/pc.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Quoted 15 lines

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 688

Quoted 11 lines
hw/i386/pc.c +3 -0
index b1e06fa..2b78dfc 100644
@@ -683,8 +683,10 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            fprintf(stderr, "please assure specicified kernel is for x86!\n");
+            exit(1);

load_multiboot() can fail for other reasons so this error messing is misleading. Giving QEMU a non-x86 kernel is just one scenario where this may fail.

Quoted 4 lines

according to my check of load_mutiboot function,
mostly it will return 0 if it's not multboot,
or 1 it's a multiboot, so print this message,
or can I just print "wrong kernel image!" ?

Yes, load_multiboot() fails if the image is not a valid multiboot image. An error message like "not a valid multiboot image" is good.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
hw/i386/pc.c +0 -1
         }
-        protocol = 0;
     }
Quoted 8 lines
Quoted 1 line

Why did you drop protocol = 0?

I think we only want either normal or multi boot linux kernel, I can't see meaning let other case go on.
so, here, if a normal kernel, OK, go on,
if multiboot, OK, go on,
others, NO, end up.
so, "protocol = 0" is meaningless here.

I looked in more detail now and here is why we cannot drop this line of code:

    uint16_t protocol;

Note that the variable is not initialized.

[...]

    if (ldl_p(header+0x202) == 0x53726448)
    protocol = lduw_p(header+0x206);
    else {
    /* 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))
            return;
    protocol = 0;

Set it to 0 here so it's initialized

    }
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {

Use variable here - so we *must* initialize it before use.

Stefan

author
#4 lore raw

OK, Thanks!
I will update this patch as your comment.

Quoted 81 lines · 在 2013-03-29五的 15:46 +0100,Stefan Hajnoczi写道:
Quoted 33 lines · li guang
Quoted 16 lines · 在 2013-03-28四的 10:42 +0100,Stefan Hajnoczi写道:
Quoted 15 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to reject booting.
e.g.
I make a mistake to boot a vmlinuz for MIPS(which I think it's for x86) like this:
qemu-system-x86_64 -kernel vmlinuz -initrd demord then qemu report:
"qemu: linux kernel too old to load a ram disk" that's misleading.

---
 hw/i386/pc.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
Quoted 15 lines

hw/i386/pc.c static void load_linux(void *fw_cfg, · line 688

Quoted 11 lines
hw/i386/pc.c +3 -0
index b1e06fa..2b78dfc 100644
@@ -683,8 +683,10 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            fprintf(stderr, "please assure specicified kernel is for x86!\n");
+            exit(1);

load_multiboot() can fail for other reasons so this error messing is misleading. Giving QEMU a non-x86 kernel is just one scenario where this may fail.

Quoted 4 lines

according to my check of load_mutiboot function,
mostly it will return 0 if it's not multboot,
or 1 it's a multiboot, so print this message,
or can I just print "wrong kernel image!" ?

Yes, load_multiboot() fails if the image is not a valid multiboot image. An error message like "not a valid multiboot image" is good.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
hw/i386/pc.c +0 -1
         }
-        protocol = 0;
     }
Quoted 8 lines
Quoted 1 line

Why did you drop protocol = 0?

I think we only want either normal or multi boot linux kernel, I can't see meaning let other case go on.
so, here, if a normal kernel, OK, go on,
if multiboot, OK, go on,
others, NO, end up.
so, "protocol = 0" is meaningless here.

I looked in more detail now and here is why we cannot drop this line of code:

    uint16_t protocol;

Note that the variable is not initialized.

[...]

    if (ldl_p(header+0x202) == 0x53726448)
    protocol = lduw_p(header+0x206);
    else {
    /* 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))
            return;
    protocol = 0;

Set it to 0 here so it's initialized

    }
    if (protocol < 0x200 || !(header[0x211] & 0x01)) {

Use variable here - so we *must* initialize it before use.

Stefan