Skip to contentSkip to conversation

multiboot: Prevent loading of x86_64 images

PATCH In review Adam Lackorzynski 4 participants 14 messages Aug 19, 2010 – Aug 20, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 multiboot: Prevent loading of x86_64 images

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

---
 hw/multiboot.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)
hw/multiboot.c +6 -0
index dc980e6..e9dcbc9 100644
@@ -171,6 +171,12 @@ int load_multiboot(void *fw_cfg,
         uint64_t elf_low, elf_high;
         int kernel_size;
         fclose(f);
+
+        if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
+            fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
+            exit(1);
+        }
+
         kernel_size = load_elf(kernel_filename, NULL, NULL, &elf_entry,
                                &elf_low, &elf_high, 0, ELF_MACHINE, 0);
         if (kernel_size < 0) {
Signature · 7 lines
1.7.1


Adam
-- 
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#1 lore raw
Quoted 2 lines · Adam Lackorzynski

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Alex

#2 lore raw
Quoted 7 lines · Alexander Graf
Quoted 2 lines · Adam Lackorzynski

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#3 lore raw
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32

What does the spec say here? What does grub do?

Alex

#4 lore raw
Quoted 17 lines · Alexander Graf
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Quoted 8 lines

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32

What does the spec say here? What does grub do?

Grub starts the (multiboot-)OS in 32bit mode. Starting directly in 64bit mode would require to setup page-tables etc. which is not done. The Spec doesn't mention 64bit OS at all, and says that 32bit OSs are fine ("An OS image may be an ordinary 32-bit executable file in the standard format for that particular operating system, except that it may be linked at a non-default load address to avoid loading on top of the ...");

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#5 lore raw
Quoted 28 lines · Adam Lackorzynski
Quoted 17 lines · Alexander Graf
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Quoted 8 lines

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32

What does the spec say here? What does grub do?

Quoted 7 lines

Grub starts the (multiboot-)OS in 32bit mode. Starting directly in 64bit mode would require to setup page-tables etc. which is not done. The Spec doesn't mention 64bit OS at all, and says that 32bit OSs are fine ("An OS image may be an ordinary 32-bit executable file in the standard format for that particular operating system, except that it may be linked at a non-default load address to avoid loading on top of the ...");

I think we should do the same grub does here. If grub loads 64-bit elf binaries and runs them in 32-bit mode, we should too. If it refuses to load them, we should too.

Alex

#6 lore raw
Quoted 39 lines · Alexander Graf
Quoted 28 lines · Adam Lackorzynski
Quoted 17 lines · Alexander Graf
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Quoted 8 lines

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32

What does the spec say here? What does grub do?

Quoted 7 lines

Grub starts the (multiboot-)OS in 32bit mode. Starting directly in 64bit mode would require to setup page-tables etc. which is not done. The Spec doesn't mention 64bit OS at all, and says that 32bit OSs are fine ("An OS image may be an ordinary 32-bit executable file in the standard format for that particular operating system, except that it may be linked at a non-default load address to avoid loading on top of the ...");

Quoted 3 lines

I think we should do the same grub does here. If grub loads 64-bit elf binaries and runs them in 32-bit mode, we should too. If it refuses to load them, we should too.

grub1:

grub> kernel (nd)/tftpboot/adam/bootstrap.elf

Error 13: Invalid or unsupported executable format

grub>

grub2 loads it but then it crashes and reboots. Looks like a bug to me.

Adam

Signature · 2 lines
Adam                 [email protected]
  Lackorzynski         http://os.inf.tu-dresden.de/~adam/
#7 lore raw
Quoted 45 lines · Adam Lackorzynski
Quoted 39 lines · Alexander Graf
Quoted 28 lines · Adam Lackorzynski
Quoted 17 lines · Alexander Graf
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Quoted 8 lines

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:

Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
Class:                             ELF32

What does the spec say here? What does grub do?

Quoted 7 lines

Grub starts the (multiboot-)OS in 32bit mode. Starting directly in 64bit mode would require to setup page-tables etc. which is not done. The Spec doesn't mention 64bit OS at all, and says that 32bit OSs are fine ("An OS image may be an ordinary 32-bit executable file in the standard format for that particular operating system, except that it may be linked at a non-default load address to avoid loading on top of the ...");

Quoted 3 lines

I think we should do the same grub does here. If grub loads 64-bit elf binaries and runs them in 32-bit mode, we should too. If it refuses to load them, we should too.

Quoted 5 lines

grub1:

grub> kernel (nd)/tftpboot/adam/bootstrap.elf

Error 13: Invalid or unsupported executable format

Alright.

Alex

#8 lore raw
In reply to Adam Lackorzynski

Hi,

Quoted 47 lines · Adam Lackorzynski
Quoted 39 lines · Alexander Graf
Quoted 28 lines · Adam Lackorzynski
Quoted 17 lines · Alexander Graf
Quoted 5 lines · Adam Lackorzynski
Quoted 3 lines · Alexander Graf

On 19.08.2010, at 13:24, Adam Lackorzynski wrote:

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

According to the multiboot spec, this is the expected behavior, no? At least Xen does it that way...

Yes, but then the supplied ELF-image should say it's a 32bit one and switch to 64bit mode itself. That's at least how we do load a 64bit kernel.

Quoted 8 lines

Hrm - maybe you're right:

busu:~ # readelf -a /boot/xen
ELF Header:

Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
Class:                             ELF32

What does the spec say here? What does grub do?

Quoted 7 lines

Grub starts the (multiboot-)OS in 32bit mode. Starting directly in 64bit mode would require to setup page-tables etc. which is not done. The Spec doesn't mention 64bit OS at all, and says that 32bit OSs are fine ("An OS image may be an ordinary 32-bit executable file in the standard format for that particular operating system, except that it may be linked at a non-default load address to avoid loading on top of the ...");

Quoted 3 lines

I think we should do the same grub does here. If grub loads 64-bit elf binaries and runs them in 32-bit mode, we should too. If it refuses to load them, we should too.

grub1:

Quoted 8 lines

grub> kernel (nd)/tftpboot/adam/bootstrap.elf

Error 13: Invalid or unsupported executable format

grub>

grub2 loads it but then it crashes and reboots. Looks like a bug to me.

I once added some x86_64 patch to our grub at T2. I forgot for what, L4, Luvally? At that time it worked, ...

http://svn.exactcode.de/t2/trunk/package/x86/grub/grub-0.97-x86_64.patch

René

Signature · 2 lines
  René Rebe, ExactCODE GmbH, Jaegerstr. 67, DE-10117 Berlin
  http://exactcode.com | http://t2-project.org | http://rene.rebe.de
#9 lore raw
In reply to Adam Lackorzynski
Quoted 12 lines · Adam Lackorzynski

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

hw/multiboot.c +0 -0
index dc980e6..e9dcbc9 100644
@@ -171,6 +171,12 @@ int load_multiboot(void *fw_cfg,
          uint64_t elf_low, elf_high;
          int kernel_size;

hw/multiboot.c int load_multiboot(void *fw_cfg, · line 182

Quoted 10 lines
hw/multiboot.c +6 -0
          fclose(f);
+
+        if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
+            fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
+            exit(1);
+        }
+
          kernel_size = load_elf(kernel_filename, NULL, NULL,&elf_entry,
                                 &elf_low,&elf_high, 0, ELF_MACHINE, 0);
          if (kernel_size<  0) {

We rely on the existing behaviour in kvm-unit-tests.git. Tests (.flat files) are 64-bit elf binaries that are loaded in 32-bit more and switch immediately to 64-bit.

We can easily wrap them in a 32-bit elf, but that's a needless complication.

Signature · 1 line
error compiling committee.c: too many arguments to function
#10 lore raw
Quoted 26 lines · Avi Kivity
Quoted 12 lines · Adam Lackorzynski

A via -kernel supplied x86_64 ELF image is being started in 32bit mode. Detect and exit if a 64bit image has been supplied.

hw/multiboot.c +0 -0
index dc980e6..e9dcbc9 100644
@@ -171,6 +171,12 @@ int load_multiboot(void *fw_cfg,
         uint64_t elf_low, elf_high;
         int kernel_size;

hw/multiboot.c int load_multiboot(void *fw_cfg, · line 182

Quoted 10 lines
hw/multiboot.c +6 -0
         fclose(f);
+
+        if (((struct elf64_hdr*)header)->e_machine == EM_X86_64) {
+            fprintf(stderr, "Cannot load x86-64 image, give a 32bit one.\n");
+            exit(1);
+        }
+
         kernel_size = load_elf(kernel_filename, NULL, NULL,&elf_entry,
                                &elf_low,&elf_high, 0, ELF_MACHINE, 0);
         if (kernel_size<  0) {

We rely on the existing behaviour in kvm-unit-tests.git. Tests (.flat files) are 64-bit elf binaries that are loaded in 32-bit more and switch immediately to 64-bit.

Quoted 1 line

We can easily wrap them in a 32-bit elf, but that's a needless complication.

Well, but if they wouldn't work in grub that doesn't help too much, right? I'm in full sympathy to stick to whatever grub does, as that's the reference implementation.

Alex

#11 lore raw
Quoted 5 lines · Alexander Graf
Quoted 4 lines

We rely on the existing behaviour in kvm-unit-tests.git. Tests (.flat files) are 64-bit elf binaries that are loaded in 32-bit more and switch immediately to 64-bit.

We can easily wrap them in a 32-bit elf, but that's a needless complication.

Well, but if they wouldn't work in grub that doesn't help too much, right?

Since the processor vendors don't use kvm-unit-tests.git to test their silicon, most people use qemu -kernel to run the unit tests, not grub.

Quoted 1 line

I'm in full sympathy to stick to whatever grub does, as that's the reference implementation.

Copying reference implementations blindly is a bad idea as you just copy their bugs. In this case, however, the spec does agree with the implementation, so I'm fine with the change. I'm not so hot about the elf32 wrapper, but I accept it's the right thing.

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw
Quoted 8 lines · Avi Kivity
Quoted 5 lines · Alexander Graf
Quoted 4 lines

We rely on the existing behaviour in kvm-unit-tests.git. Tests (.flat files) are 64-bit elf binaries that are loaded in 32-bit more and switch immediately to 64-bit.

We can easily wrap them in a 32-bit elf, but that's a needless complication.

Well, but if they wouldn't work in grub that doesn't help too much, right?

Since the processor vendors don't use kvm-unit-tests.git to test their silicon, most people use qemu -kernel to run the unit tests, not grub.

It would potentially also help the unit tests, as running them in grub would allow for easy verification on real hardware too.

Quoted 4 lines
Quoted 2 lines

I'm in full sympathy to stick to whatever grub does, as that's the reference implementation.

Copying reference implementations blindly is a bad idea as you just copy their bugs. In this case, however, the spec does agree with the implementation, so I'm fine with the change. I'm not so hot about the elf32 wrapper, but I accept it's the right thing.

:)

Alex

#13 lore raw
Quoted 7 lines · Alexander Graf
Quoted 6 lines
Quoted 5 lines
Quoted 4 lines

We rely on the existing behaviour in kvm-unit-tests.git. Tests (.flat files) are 64-bit elf binaries that are loaded in 32-bit more and switch immediately to 64-bit.

We can easily wrap them in a 32-bit elf, but that's a needless complication.

Well, but if they wouldn't work in grub that doesn't help too much, right?

Since the processor vendors don't use kvm-unit-tests.git to test their silicon, most people use qemu -kernel to run the unit tests, not grub.

It would potentially also help the unit tests, as running them in grub would allow for easy verification on real hardware too.

True. We need to write an INT 10 driver and a serial driver for this to work.

Signature · 1 line
error compiling committee.c: too many arguments to function