Skip to contentSkip to conversation

hw/i386/pc: prompt not multboot or morden kernel image

PATCH In review liguang 3 participants 6 messages Apr 9, 2013 – Apr 12, 2013

lore
Share & tools
lore

liguang

author
#0 lore raw

PATCH v3 1/1 hw/i386/pc: prompt not multboot or morden kernel image

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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 |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +2 -0
index ebbf059..6b29c3f 100644
@@ -686,6 +686,8 @@ 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, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }
Signature · 1 line
1.7.2.5
#1 lore raw
Quoted 10 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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.

Yes, that message would definitely be misleading in this case.

Quoted 11 lines
---
 hw/i386/pc.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +0 -0
index ebbf059..6b29c3f 100644

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

Quoted 8 lines
hw/i386/pc.c +2 -0
@@ -686,6 +686,8 @@ 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, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }

My impression from reading through the code is that this branch can be legitimately triggered by an older kernel (protocol < 0x200). In that case, the error message above would also be misleading.

I think a better solution might be to simply validate that the arch specified in the ELF header matches the arch that qemu is emulating. I'll look into this more tomorrow morning. :)

Jesse Larrew
Software Engineer, KVM Team
IBM Linux Technology Center
Phone: (512) 973-2052 (T/L: 363-2052)
[email protected]

author
#2 lore raw
Quoted 28 lines · 在 2013-04-08一的 23:36 -0500,Jesse Larrew写道:
Quoted 10 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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.

Yes, that message would definitely be misleading in this case.

Quoted 14 lines
---
 hw/i386/pc.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +1 -0
index ebbf059..6b29c3f 100644
@@ -686,6 +686,8 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
Quoted 8 lines

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

Quoted 5 lines
hw/i386/pc.c +1 -0
+            fprintf(stderr, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }

My impression from reading through the code is that this branch can be legitimately triggered by an older kernel (protocol < 0x200).

right.

Quoted 1 line

In that case,the error message above would also be misleading.

the warning message is saying: hey, we prefer modern kernel image or multiboot, but you specify neither, what are you doing? do you want to boot an old(obsolete) kernel?

or, add a line like fprintf(stderr, "do you boot an old kernel?\n") :-)

Quoted 4 lines

I think a better solution might be to simply validate that the arch specified in the ELF header matches the arch that qemu is emulating.
I'll look into this more tomorrow morning. :)

that's fine

Quoted 7 lines

Jesse Larrew
Software Engineer, KVM Team
IBM Linux Technology Center
Phone: (512) 973-2052 (T/L: 363-2052)
[email protected]

#3 lore raw
Quoted 40 lines · li guang
Quoted 28 lines · 在 2013-04-08一的 23:36 -0500,Jesse Larrew写道:
Quoted 10 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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.

Yes, that message would definitely be misleading in this case.

Quoted 14 lines
---
 hw/i386/pc.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +1 -0
index ebbf059..6b29c3f 100644
@@ -686,6 +686,8 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
Quoted 8 lines

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

Quoted 5 lines
hw/i386/pc.c +1 -0
+            fprintf(stderr, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }

My impression from reading through the code is that this branch can be legitimately triggered by an older kernel (protocol < 0x200).

right.

Quoted 8 lines
Quoted 1 line

In that case,the error message above would also be misleading.

the warning message is saying: hey, we prefer modern kernel image or multiboot, but you specify neither, what are you doing? do you want to boot an old(obsolete) kernel?

or, add a line like fprintf(stderr, "do you boot an old kernel?\n") :-)

Ah, I understand now. Thanks for explaining that. :)

Quoted 7 lines
Quoted 4 lines

I think a better solution might be to simply validate that the arch specified in the ELF header matches the arch that qemu is emulating.
I'll look into this more tomorrow morning. :)

that's fine

Looking at Documentation/x86/boot.txt in the kernel source, it looks like there is a separate word of header magic that all x86 kernels should have -- even the oldest images. Adding a separate check for that magic might be a more complete solution:

---
 hw/i386/pc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/pc.c +8 -0
index 8d75b34..e120427 100644
@@ -686,6 +686,14 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            /* This is the closest thing old Linux kernels have to a
+               magic number. */
+            if (lduw_p(header+0x01FE) != 0xAA55) {
+                fprintf(stderr, "qemu: '%s' is not an x86 kernel image.\n",
+                        kernel_filename);
+                exit(1);
+            }
         }
         protocol = 0;
     }
Signature · 7 lines
1.7.11.7

Jesse Larrew
Software Engineer, KVM Team
IBM Linux Technology Center
Phone: (512) 973-2052 (T/L: 363-2052)
[email protected]
author
#4 lore raw
Quoted 74 lines · 在 2013-04-09二的 14:15 -0500,Jesse Larrew写道:
Quoted 40 lines · li guang
Quoted 28 lines · 在 2013-04-08一的 23:36 -0500,Jesse Larrew写道:
Quoted 10 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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.

Yes, that message would definitely be misleading in this case.

Quoted 14 lines
---
 hw/i386/pc.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +1 -0
index ebbf059..6b29c3f 100644
@@ -686,6 +686,8 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
Quoted 8 lines

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

Quoted 5 lines
hw/i386/pc.c +1 -0
+            fprintf(stderr, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }

My impression from reading through the code is that this branch can be legitimately triggered by an older kernel (protocol < 0x200).

right.

Quoted 8 lines
Quoted 1 line

In that case,the error message above would also be misleading.

the warning message is saying: hey, we prefer modern kernel image or multiboot, but you specify neither, what are you doing? do you want to boot an old(obsolete) kernel?

or, add a line like fprintf(stderr, "do you boot an old kernel?\n") :-)

Ah, I understand now. Thanks for explaining that. :)

Quoted 7 lines
Quoted 4 lines

I think a better solution might be to simply validate that the arch specified in the ELF header matches the arch that qemu is emulating.
I'll look into this more tomorrow morning. :)

that's fine

Looking at Documentation/x86/boot.txt in the kernel source, it looks like there is a separate word of header magic that all x86 kernels should have -- even the oldest images. Adding a separate check for that magic might be a more complete solution:

---
 hw/i386/pc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/pc.c +0 -0
index 8d75b34..e120427 100644

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

Quoted 7 lines
hw/i386/pc.c +4 -0
@@ -686,6 +686,14 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            /* This is the closest thing old Linux kernels have to a
+               magic number. */
+            if (lduw_p(header+0x01FE) != 0xAA55) {

maybe a good candidate,
but, AFAIK, 0xAA55 can hardly be a magic for
it's commonly used by many cases.

Quoted 7 lines
hw/i386/pc.c +4 -0
+                fprintf(stderr, "qemu: '%s' is not an x86 kernel image.\n",
+                        kernel_filename);
+                exit(1);
+            }
         }
         protocol = 0;
     }
#5 lore raw
Quoted 70 lines · li guang
Quoted 69 lines · 在 2013-04-09二的 14:15 -0500,Jesse Larrew写道:
Quoted 40 lines · li guang
Quoted 28 lines · 在 2013-04-08一的 23:36 -0500,Jesse Larrew写道:
Quoted 10 lines · liguang

if head magic is missing or wrong unexpectedly, we'd better to prompt memssage for this.
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.

Yes, that message would definitely be misleading in this case.

Quoted 14 lines
---
 hw/i386/pc.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
hw/i386/pc.c +1 -0
index ebbf059..6b29c3f 100644
@@ -686,6 +686,8 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
Quoted 8 lines

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

Quoted 5 lines
hw/i386/pc.c +1 -0
+            fprintf(stderr, "warn: invalid multiboot or modern kernel image\n");
         }
         protocol = 0;
     }

My impression from reading through the code is that this branch can be legitimately triggered by an older kernel (protocol < 0x200).

right.

Quoted 8 lines
Quoted 1 line

In that case,the error message above would also be misleading.

the warning message is saying: hey, we prefer modern kernel image or multiboot, but you specify neither, what are you doing? do you want to boot an old(obsolete) kernel?

or, add a line like fprintf(stderr, "do you boot an old kernel?\n") :-)

Ah, I understand now. Thanks for explaining that. :)

Quoted 7 lines
Quoted 4 lines

I think a better solution might be to simply validate that the arch specified in the ELF header matches the arch that qemu is emulating.
I'll look into this more tomorrow morning. :)

that's fine

Looking at Documentation/x86/boot.txt in the kernel source, it looks like there is a separate word of header magic that all x86 kernels should have -- even the oldest images. Adding a separate check for that magic might be a more complete solution:

---
 hw/i386/pc.c | 8 ++++++++
 1 file changed, 8 insertions(+)
Quoted 16 lines

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

Quoted 12 lines
hw/i386/pc.c +4 -0
index 8d75b34..e120427 100644
@@ -686,6 +686,14 @@ static void load_linux(void *fw_cfg,
         if (load_multiboot(fw_cfg, f, kernel_filename, initrd_filename,
                            kernel_cmdline, kernel_size, header)) {
             return;
+        } else {
+            /* This is the closest thing old Linux kernels have to a
+               magic number. */
+            if (lduw_p(header+0x01FE) != 0xAA55) {

maybe a good candidate,
but, AFAIK, 0xAA55 can hardly be a magic for
it's commonly used by many cases.

The problem is that older kernel images don't have a real magic number to check. We cannot break compatibility by rejecting them now.

Jesse's patch makes sense to me. Feel free to discuss more, I'll merge once there is consensus.

Stefan