Skip to contentSkip to conversation

q800: fix segfault with invalid MacROM

PATCH In review Laurent Vivier 3 participants 3 messages Jan 7, 2022

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 q800: fix segfault with invalid MacROM

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

To avoid NULL pointer, don't allow 0 sized file and use bios_size with rom_ptr().

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

---
 hw/m68k/q800.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/m68k/q800.c +3 -2
index e4c7c9b88ad0..55dfe5036f40 100644
@@ -672,12 +672,13 @@ 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 <= 0 || bios_size > MACROM_SIZE) {
                 error_report("could not load MacROM '%s'", bios_name);
                 exit(1);
             }
 
-            ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
+            ptr = rom_ptr(MACROM_ADDR, bios_size);
+            assert(ptr != NULL);
             stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */
             stl_phys(cs->as, 4,
                      MACROM_ADDR + ldl_p(ptr + 4)); /* reset initial PC */
Signature · 1 line
2.33.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 25 lines · Laurent Vivier

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

To avoid NULL pointer, don't allow 0 sized file and use bios_size with rom_ptr().

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

---
  hw/m68k/q800.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/m68k/q800.c +1 -1
index e4c7c9b88ad0..55dfe5036f40 100644
@@ -672,12 +672,13 @@ 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 <= 0 || bios_size > MACROM_SIZE) {

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

Quoted 10 lines
hw/m68k/q800.c +2 -1
                  error_report("could not load MacROM '%s'", bios_name);
                  exit(1);
              }
 
-            ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
+            ptr = rom_ptr(MACROM_ADDR, bios_size);
+            assert(ptr != NULL);
              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */
              stl_phys(cs->as, 4,
                       MACROM_ADDR + ldl_p(ptr + 4)); /* reset initial PC */

Looks nicer than v1, indeed.

Reviewed-by on v2 1/1
#2 lore raw
In reply to Laurent Vivier
Quoted 25 lines · Laurent Vivier

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

To avoid NULL pointer, don't allow 0 sized file and use bios_size with rom_ptr().

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

---
  hw/m68k/q800.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/m68k/q800.c +1 -1
index e4c7c9b88ad0..55dfe5036f40 100644
@@ -672,12 +672,13 @@ 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 <= 0 || bios_size > MACROM_SIZE) {

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

Quoted 10 lines
hw/m68k/q800.c +2 -1
                  error_report("could not load MacROM '%s'", bios_name);
                  exit(1);
              }
 
-            ptr = rom_ptr(MACROM_ADDR, MACROM_SIZE);
+            ptr = rom_ptr(MACROM_ADDR, bios_size);
+            assert(ptr != NULL);
              stl_phys(cs->as, 0, ldl_p(ptr));    /* reset initial SP */
              stl_phys(cs->as, 4,
                       MACROM_ADDR + ldl_p(ptr + 4)); /* reset initial PC */

I think technically the second parameter of rom_ptr() is supposed to be the absolute upper bound of the accessible region so the existing MACROM_SIZE feels better being a constant rather than a variable such as bios_size. But then again bios_size should never be greater than MACROM_SIZE because of the check beforehand so it shouldn't matter in practice. Anyhow regardless of what you decide:

ATB,

Mark.