qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] fix loading tiny kernels
@ 2009-02-03  8:59 René Rebe
  2009-02-03  9:05 ` Laurent Desnogues
  2009-02-03  9:06 ` René Rebe
  0 siblings, 2 replies; 9+ messages in thread
From: René Rebe @ 2009-02-03  8:59 UTC (permalink / raw)
  To: qemu-devel

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.

Signed-off-by: René Rebe <rene@exactcode.de>

--- hw/pc.c     (revision 6501)
+++ hw/pc.c     (working copy)
@@ -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);

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03  8:59 [Qemu-devel] [PATCH] fix loading tiny kernels René Rebe
@ 2009-02-03  9:05 ` Laurent Desnogues
  2009-02-03  9:06 ` René Rebe
  1 sibling, 0 replies; 9+ messages in thread
From: Laurent Desnogues @ 2009-02-03  9:05 UTC (permalink / raw)
  To: qemu-devel

On Tue, Feb 3, 2009 at 9:59 AM, René Rebe <rene@exactcode.de> wrote:
> 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.
>
> Signed-off-by: René Rebe <rene@exactcode.de>
>
> --- hw/pc.c     (revision 6501)
> +++ hw/pc.c     (working copy)
> @@ -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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03  8:59 [Qemu-devel] [PATCH] fix loading tiny kernels René Rebe
  2009-02-03  9:05 ` Laurent Desnogues
@ 2009-02-03  9:06 ` René Rebe
  2009-02-03 10:30   ` Daniel P. Berrange
  1 sibling, 1 reply; 9+ messages in thread
From: René Rebe @ 2009-02-03  9:06 UTC (permalink / raw)
  To: qemu-devel

I babbled:
> 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.
>
> Signed-off-by: René Rebe <rene@exactcode.de>
>
> --- hw/pc.c     (revision 6501)
> +++ hw/pc.c     (working copy)
> @@ -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);
>
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.

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03  9:06 ` René Rebe
@ 2009-02-03 10:30   ` Daniel P. Berrange
  2009-02-03 11:09     ` Alexander Graf
  0 siblings, 1 reply; 9+ messages in thread
From: Daniel P. Berrange @ 2009-02-03 10:30 UTC (permalink / raw)
  To: qemu-devel

On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
> I babbled:
> >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.
> >
> >Signed-off-by: René Rebe <rene@exactcode.de>
> >
> >--- hw/pc.c     (revision 6501)
> >+++ hw/pc.c     (working copy)
> >@@ -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);
> >
> 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
-- 
|: 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 :|

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03 10:30   ` Daniel P. Berrange
@ 2009-02-03 11:09     ` Alexander Graf
  2009-02-03 12:31       ` Rene Rebe
  2009-02-03 13:30       ` Daniel P. Berrange
  0 siblings, 2 replies; 9+ messages in thread
From: Alexander Graf @ 2009-02-03 11:09 UTC (permalink / raw)
  To: Daniel P. Berrange, qemu-devel


On 03.02.2009, at 11:30, Daniel P. Berrange wrote:

> On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
>> I babbled:
>>> 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.
>>>
>>> Signed-off-by: René Rebe <rene@exactcode.de>
>>>
>>> --- hw/pc.c     (revision 6501)
>>> +++ hw/pc.c     (working copy)
>>> @@ -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);
>>>
>> 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.

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03 11:09     ` Alexander Graf
@ 2009-02-03 12:31       ` Rene Rebe
  2009-02-03 12:33         ` Alexander Graf
  2009-02-03 13:30       ` Daniel P. Berrange
  1 sibling, 1 reply; 9+ messages in thread
From: Rene Rebe @ 2009-02-03 12:31 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alexander Graf

Alexander Graf wrote:
> 
> On 03.02.2009, at 11:30, Daniel P. Berrange wrote:
> 
>> On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
>>> I babbled:
>>>> 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.
>>>>
>>>> Signed-off-by: René Rebe <rene@exactcode.de>
>>>>
>>>> --- hw/pc.c     (revision 6501)
>>>> +++ hw/pc.c     (working copy)
>>>> @@ -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);
>>>>
>>> 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.
> 
> 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.

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03 12:31       ` Rene Rebe
@ 2009-02-03 12:33         ` Alexander Graf
  0 siblings, 0 replies; 9+ messages in thread
From: Alexander Graf @ 2009-02-03 12:33 UTC (permalink / raw)
  To: Rene Rebe; +Cc: qemu-devel


On 03.02.2009, at 13:31, Rene Rebe wrote:

> Alexander Graf wrote:
>> On 03.02.2009, at 11:30, Daniel P. Berrange wrote:
>>> On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
>>>> I babbled:
>>>>> 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.
>>>>>
>>>>> Signed-off-by: René Rebe <rene@exactcode.de>
>>>>>
>>>>> --- hw/pc.c     (revision 6501)
>>>>> +++ hw/pc.c     (working copy)
>>>>> @@ -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);
>>>>>
>>>> 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.
>> 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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03 11:09     ` Alexander Graf
  2009-02-03 12:31       ` Rene Rebe
@ 2009-02-03 13:30       ` Daniel P. Berrange
  2009-02-03 14:02         ` Rene Rebe
  1 sibling, 1 reply; 9+ messages in thread
From: Daniel P. Berrange @ 2009-02-03 13:30 UTC (permalink / raw)
  To: Alexander Graf; +Cc: qemu-devel

On Tue, Feb 03, 2009 at 12:09:42PM +0100, Alexander Graf wrote:
> 
> On 03.02.2009, at 11:30, Daniel P. Berrange wrote:
> 
> >On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
> >>I babbled:
> >>>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.
> >>>
> >>>Signed-off-by: René Rebe <rene@exactcode.de>
> >>>
> >>>--- hw/pc.c     (revision 6501)
> >>>+++ hw/pc.c     (working copy)
> >>>@@ -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);
> >>>
> >>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.
> 
> 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
-- 
|: 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 :|

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Qemu-devel] [PATCH] fix loading tiny kernels
  2009-02-03 13:30       ` Daniel P. Berrange
@ 2009-02-03 14:02         ` Rene Rebe
  0 siblings, 0 replies; 9+ messages in thread
From: Rene Rebe @ 2009-02-03 14:02 UTC (permalink / raw)
  To: Daniel P. Berrange, qemu-devel; +Cc: Alexander Graf

Daniel P. Berrange wrote:
> On Tue, Feb 03, 2009 at 12:09:42PM +0100, Alexander Graf wrote:
>> On 03.02.2009, at 11:30, Daniel P. Berrange wrote:
>>
>>> On Tue, Feb 03, 2009 at 10:06:10AM +0100, Ren? Rebe wrote:
>>>> I babbled:
>>>>> 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.
>>>>>
>>>>> Signed-off-by: René Rebe <rene@exactcode.de>
>>>>>
>>>>> --- hw/pc.c     (revision 6501)
>>>>> +++ hw/pc.c     (working copy)
>>>>> @@ -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);
>>>>>
>>>> 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.
>> 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

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

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-02-03 14:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-02-03  8:59 [Qemu-devel] [PATCH] fix loading tiny kernels René Rebe
2009-02-03  9:05 ` Laurent Desnogues
2009-02-03  9:06 ` René Rebe
2009-02-03 10:30   ` Daniel P. Berrange
2009-02-03 11:09     ` Alexander Graf
2009-02-03 12:31       ` Rene Rebe
2009-02-03 12:33         ` Alexander Graf
2009-02-03 13:30       ` Daniel P. Berrange
2009-02-03 14:02         ` Rene Rebe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).