Skip to contentSkip to conversation

fix loading tiny kernels

PATCH In review René Rebe 4 participants 9 messages Feb 3, 2009

lore
Share & tools
lore

René Rebe

author
#0 lore raw

PATCH 1/1 fix loading tiny kernels

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes - for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
    /* load the kernel header */
    f = fopen(kernel_filename, "rb");
    if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192, kernel_size)) {
    fprintf(stderr, "qemu: could not load kernel '%s'\n",
        kernel_filename);
    exit(1);
Signature · 2 lines
 René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
 http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#1 lore raw
Quoted 11 lines · René Rebe

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) line 992

Quoted 5 lines
pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
   /* load the kernel header */
   f = fopen(kernel_filename, "rb");
   if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192, kernel_size))

That's wrong: 'header' size is 1024 bytes.

Laurent

author
#2 lore raw
In reply to René Rebe

I babbled:

Quoted 18 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes - for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
    /* load the kernel header */
    f = fopen(kernel_filename, "rb");
    if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192, 
 kernel_size)) {
    fprintf(stderr, "qemu: could not load kernel '%s'\n",
        kernel_filename);
Quoted 2 lines
    exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Sorry, will include that in the updated multi-boot patch.

Signature · 2 lines
 René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
 http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#3 lore raw
Quoted 9 lines · Ren? Rebe

I babbled:

Quoted 8 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

Quoted 15 lines
Quoted 11 lines
pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
   /* load the kernel header */
   f = fopen(kernel_filename, "rb");
   if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192, 
 kernel_size)) {
   fprintf(stderr, "qemu: could not load kernel '%s'\n",
       kernel_filename);
Quoted 2 lines
   exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Regardless, this code should not hardcode the size like this. It should use sizeof(header) instead of 1024 or 8192, thus avoiding the potential bug.

Regards,
Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
#4 lore raw
Quoted 26 lines · Daniel P. Berrange
Quoted 9 lines · Ren? Rebe

I babbled:

Quoted 8 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

Quoted 15 lines
Quoted 11 lines
pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
  /* load the kernel header */
  f = fopen(kernel_filename, "rb");
  if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192,
 kernel_size)) {
  fprintf(stderr, "qemu: could not load kernel '%s'\n",
      kernel_filename);
Quoted 2 lines
  exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Quoted 3 lines

Regardless, this code should not hardcode the size like this. It should use sizeof(header) instead of 1024 or 8192, thus avoiding the potential bug.

You don't really know sizeof(header), do you? Header could be the Linux header or the Multiboot header which is by definition allowed to sit somewhere within the first 8192 bytes.

Alex

author
#5 lore raw
Quoted 29 lines · Alexander Graf
Quoted 26 lines · Daniel P. Berrange
Quoted 24 lines · Ren? Rebe

I babbled:

Quoted 19 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
  /* load the kernel header */
  f = fopen(kernel_filename, "rb");
  if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192,
 kernel_size)) {
  fprintf(stderr, "qemu: could not load kernel '%s'\n",
      kernel_filename);
Quoted 2 lines
  exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Quoted 5 lines
Quoted 3 lines

Regardless, this code should not hardcode the size like this. It should use sizeof(header) instead of 1024 or 8192, thus avoiding the potential bug.

You don't really know sizeof(header), do you? Header could be the Linux header or the Multiboot header which is by definition allowed to sit somewhere within the first 8192 bytes.

Maybe he ment just sizeof(header) to avoid letting future changes of the code let the definition and code get out of sync if the header size to be read is changed again.

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name
#6 lore raw
Quoted 28 lines · Rene Rebe
Quoted 27 lines · Alexander Graf
Quoted 26 lines · Daniel P. Berrange
Quoted 24 lines · Ren? Rebe

I babbled:

Quoted 19 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
 /* load the kernel header */
 f = fopen(kernel_filename, "rb");
 if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192,
 kernel_size)) {
 fprintf(stderr, "qemu: could not load kernel '%s'\n",
     kernel_filename);
Quoted 2 lines

exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Quoted 8 lines
Quoted 4 lines
Quoted 3 lines

Regardless, this code should not hardcode the size like this. It should use sizeof(header) instead of 1024 or 8192, thus avoiding the potential bug.

You don't really know sizeof(header), do you? Header could be the Linux header or the Multiboot header which is by definition allowed to sit somewhere within the first 8192 bytes.

Maybe he ment just sizeof(header) to avoid letting future changes of the code let the definition and code get out of sync if the header size to be read is changed again.

Ah, right. Sorry for the fuss - sounds good.

Alex

#7 lore raw
In reply to Alexander Graf
Quoted 38 lines · Alexander Graf
Quoted 29 lines · Daniel P. Berrange
Quoted 11 lines · Ren? Rebe

I babbled:

Quoted 10 lines

Further testing / polishing the multi-boot kernel loading support I found
the existing code fails to load unusual small kernels, less than 8192
bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

Quoted 16 lines
Quoted 11 lines
pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
 /* load the kernel header */
 f = fopen(kernel_filename, "rb");
 if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192,
 kernel_size)) {
 fprintf(stderr, "qemu: could not load kernel '%s'\n",
     kernel_filename);
Quoted 2 lines

exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series
which increases the header read to 8192 bytes.

Quoted 5 lines

Regardless, this code should not hardcode the size like this. It should
use sizeof(header) instead of 1024 or 8192, thus avoiding the potential
bug.

Quoted 3 lines

You don't really know sizeof(header), do you? Header could be the Linux header or the Multiboot header which is by definition allowed to sit somewhere within the first 8192 bytes.

I meant in terms of making sure we didn't overflow the header variable which is allocated on the stack. So instead of

    uint8_t header[1024];
    ...
    fread(header, 1, 1024, f);

You'd have

    uint8_t header[1024];
    ...
    fread(header, 1, sizeof(header), f);

Daniel

Signature · 4 lines
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
author
#8 lore raw
Quoted 40 lines · Daniel P. Berrange
Quoted 27 lines · Alexander Graf
Quoted 25 lines · Daniel P. Berrange
Quoted 24 lines · Ren? Rebe

I babbled:

Quoted 19 lines

Further testing / polishing the multi-boot kernel loading support I found the existing code fails to load unusual small kernels, less than 8192 bytes -
for example the example multi-boot kernel shipped within GRUB that compiles to just 7121 bytes on my system.

pc.c (working copy) renamed from pc.c (revision 6501) +1 -1
@@ -554,7 +989,7 @@
 /* load the kernel header */
 f = fopen(kernel_filename, "rb");
 if (!f || !(kernel_size = get_file_size(f)) ||
-    fread(header, 1, 1024, f) != 1024) {
+    fread(header, 1, MIN(8192, kernel_size), f) != MIN(8192,
 kernel_size)) {
 fprintf(stderr, "qemu: could not load kernel '%s'\n",
     kernel_filename);
Quoted 2 lines

exit(1);

Ah, sorry - mix in the series. This only applies to the multi-boot series which increases the header read to 8192 bytes.

Quoted 4 lines
Quoted 3 lines

Regardless, this code should not hardcode the size like this. It should use sizeof(header) instead of 1024 or 8192, thus avoiding the potential bug.

You don't really know sizeof(header), do you? Header could be the Linux header or the Multiboot header which is by definition allowed to sit somewhere within the first 8192 bytes.

I meant in terms of making sure we didn't overflow the header variable which is allocated on the stack. So instead of

    uint8_t header[1024];
    ...
    fread(header, 1, 1024, f);
Quoted 7 lines

You'd have

    uint8_t header[1024];
    ...
    fread(header, 1, sizeof(header), f);

Daniel

Just preventing this in the case it's changed in the future and one place is forgotten.

I already changed the code to use the ARRAY_SIZE macro in my working copy:

http://svn.exactcode.de/t2/trunk/package/emulators/kvm/09-qemu-multiboot.patch

Signature · 2 lines
  René Rebe - ExactCODE GmbH - Europe, Germany, Berlin
  http://exactcode.de | http://t2-project.org | http://rene.rebe.name