Skip to contentSkip to conversation

q800: fix segfault with invalid MacROM

PATCH Superseded Laurent Vivier 5 participants 7 messages Jan 6, 2022 – Jan 7, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 q800: fix segfault with invalid MacROM

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
 hw/m68k/q800.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +7 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
 
         /* Remove qtest_enabled() check once firmware files are in the tree */
         if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                 error_report("could not load MacROM '%s'", bios_name);
                 exit(1);
             }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
 
             ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
             stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */
Signature · 1 line
2.33.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Laurent Vivier

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault

Typo "crashes".

Quoted 10 lines

in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
Reviewed-by on v1 1/1
#2 lore raw
In reply to Laurent Vivier
Quoted 24 lines · Laurent Vivier

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +1 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
 
          /* Remove qtest_enabled() check once firmware files are in the tree */
          if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                  error_report("could not load MacROM '%s'", bios_name);
                  exit(1);

hw/m68k/q800.c static void q800_init(MachineState *machine) · line 687

Quoted 10 lines
hw/m68k/q800.c +6 -0
              }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
 
              ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */
#3 lore raw
In reply to Laurent Vivier
Quoted 24 lines · Laurent Vivier

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +1 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
 
          /* Remove qtest_enabled() check once firmware files are in the tree */
          if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                  error_report("could not load MacROM '%s'", bios_name);
                  exit(1);

hw/m68k/q800.c static void q800_init(MachineState *machine) · line 687

Quoted 10 lines
hw/m68k/q800.c +6 -0
              }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
 
              ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */

The patch does fix the issue, but it seems a little odd that you can't use -bios path/to/m68k-binary to boot with an arbitrary sized binary which could be useful for reproducers such as https://gitlab.com/qemu-project/qemu/-/issues/360.

How easy would it be to add the extra rom_ptr() NULL check instead?

ATB,

Mark.

#4 lore raw

Le 07/01/2022 à 09:15, Mark Cave-Ayland a écrit :

Quoted 37 lines · Laurent Vivier
Quoted 32 lines

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +7 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
           /* Remove qtest_enabled() check once firmware files are in the tree */
           if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                   error_report("could not load MacROM '%s'", bios_name);
                   exit(1);
               }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
               ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
Quoted 1 line

              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */

The patch does fix the issue, but it seems a little odd that you can't use -bios path/to/m68k-binary to boot with an arbitrary sized binary which could be useful for reproducers such as https://gitlab.com/qemu-project/qemu/-/issues/360.

Quoted 2 lines

How easy would it be to add the extra rom_ptr() NULL check instead?

I was thinking that a smaller binary can be padded to 1 MB for use because on a real hardware the size of the ROM cannot be arbitrary.

But it seems reasonable to check only for the NULL pointer rather than the size, I'm going to send a v2.

Thanks,
Laurent

#5 lore raw
Quoted 40 lines · Laurent Vivier

Le 07/01/2022 à 09:15, Mark Cave-Ayland a écrit :

Quoted 39 lines · Laurent Vivier
Quoted 32 lines

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +7 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
           /* Remove qtest_enabled() check once firmware files are in the tree */
           if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                   error_report("could not load MacROM '%s'", bios_name);
                   exit(1);
               }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
               ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
Quoted 1 line

              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */

The patch does fix the issue, but it seems a little odd that you can't use -bios path/to/m68k-binary to boot with an arbitrary sized binary which could be useful for reproducers such as https://gitlab.com/qemu-project/qemu/-/issues/360.

Quoted 6 lines
Quoted 2 lines

How easy would it be to add the extra rom_ptr() NULL check instead?

I was thinking that a smaller binary can be padded to 1 MB for use because on a real hardware the size of the ROM cannot be arbitrary.

But it seems reasonable to check only for the NULL pointer rather than the size, I'm going to send a v2.

Instead of adding !rom_ptr as well, isn't it enough to change to bios_size <= 0 in the existing check?

Regards,
BALATON Zoltan

#6 lore raw

Le 07/01/2022 à 10:47, BALATON Zoltan a écrit :

Quoted 42 lines · Laurent Vivier
Quoted 41 lines

Le 07/01/2022 à 09:15, Mark Cave-Ayland a écrit :

Quoted 39 lines · Laurent Vivier
Quoted 32 lines

"qemu-system-m68k -M q800 -bios /dev/null" crahses with a segfault in q800_init().
This happens because the code doesn't check that rom_ptr() returned a non-NULL pointer .

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/756

---
  hw/m68k/q800.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
hw/m68k/q800.c +7 -1
index e4c7c9b88ad0..6261716c8f7e 100644
@@ -672,10 +672,16 @@ static void q800_init(MachineState *machine)
           /* Remove qtest_enabled() check once firmware files are in the tree */
           if (!qtest_enabled()) {
-            if (bios_size < 0 || bios_size > MACROM_SIZE) {
+            if (bios_size == -1) {
                   error_report("could not load MacROM '%s'", bios_name);
                   exit(1);
               }
+            if (bios_size != MACROM_SIZE) {
+                error_report("Invalid size for MacROM '%s': %d bytes,"
+                             " expected %d bytes", bios_name, bios_size,
+                             MACROM_SIZE);
+                exit(1);
+            }
               ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
Quoted 1 line

              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */

The patch does fix the issue, but it seems a little odd that you can't use -bios path/to/m68k-binary to boot with an arbitrary sized binary which could be useful for reproducers such as https://gitlab.com/qemu-project/qemu/-/issues/360.

How easy would it be to add the extra rom_ptr() NULL check instead?

Quoted 7 lines
Quoted 3 lines

I was thinking that a smaller binary can be padded to 1 MB for use because on a real hardware the size of the ROM cannot be arbitrary.

But it seems reasonable to check only for the NULL pointer rather than the size, I'm going to send a v2.

Instead of adding !rom_ptr as well, isn't it enough to change to bios_size <= 0 in the existing check?

I agree. And to change rom_ptr(MACROM_ADDR, MACROM_SIZE) to rom_ptr(MACROM_ADDR, bios_size)

Thanks,
Laurent