Skip to contentSkip to conversation

hw/ppc/e500.c: Handle qemu_find_file() failure

PATCH Applied Peter Maydell 3 participants 3 messages Mar 24, 2020 – Mar 26, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/ppc/e500.c: Handle qemu_find_file() failure

If qemu_find_file() doesn't find the BIOS it returns NULL; we were passing that unchecked through to load_elf(), which assumes a non-NULL pointer and may misbehave. In practice it fails with a weird message:

  $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
  Bad address
  qemu-system-ppc: could not load firmware '(null)'

Handle the failure case better:

  $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
  qemu-system-ppc: could not find firmware/kernel file 'nonesuch'

Spotted by Coverity (CID 1238954).

---
 hw/ppc/e500.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/ppc/e500.c +4 -0
index 854cd3ac465..0d1f41197cf 100644
@@ -1047,6 +1047,10 @@ void ppce500_init(MachineState *machine)
     }
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, payload_name);
+    if (!filename) {
+        error_report("could not find firmware/kernel file '%s'", payload_name);
+        exit(1);
+    }
 
     payload_size = load_elf(filename, NULL, NULL, NULL,
                             &bios_entry, &loadaddr, NULL, NULL,
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Peter Maydell

If qemu_find_file() doesn't find the BIOS it returns NULL; we were passing that unchecked through to load_elf(), which assumes a non-NULL pointer and may misbehave. In practice it fails with a weird message:

   $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
   Bad address
   qemu-system-ppc: could not load firmware '(null)'

Handle the failure case better:

   $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
   qemu-system-ppc: could not find firmware/kernel file 'nonesuch'

Spotted by Coverity (CID 1238954).

---
  hw/ppc/e500.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/ppc/e500.c +0 -0
index 854cd3ac465..0d1f41197cf 100644

hw/ppc/e500.c void ppce500_init(MachineState *machine) · line 1056

Quoted 10 lines
hw/ppc/e500.c +4 -0
@@ -1047,6 +1047,10 @@ void ppce500_init(MachineState *machine)
      }
 
      filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, payload_name);
+    if (!filename) {
+        error_report("could not find firmware/kernel file '%s'", payload_name);
+        exit(1);
+    }
 
      payload_size = load_elf(filename, NULL, NULL, NULL,
                              &bios_entry, &loadaddr, NULL, NULL,
Applied
#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

If qemu_find_file() doesn't find the BIOS it returns NULL; we were passing that unchecked through to load_elf(), which assumes a non-NULL pointer and may misbehave. In practice it fails with a weird message:

  $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
  Bad address
  qemu-system-ppc: could not load firmware '(null)'
Quoted 8 lines

Handle the failure case better:

  $ qemu-system-ppc -M ppce500 -display none -kernel nonesuch
  qemu-system-ppc: could not find firmware/kernel file 'nonesuch'

Spotted by Coverity (CID 1238954).

Applied to ppc-for-5.0, thanks.

Quoted 19 lines
---
 hw/ppc/e500.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/ppc/e500.c +4 -0
index 854cd3ac465..0d1f41197cf 100644
@@ -1047,6 +1047,10 @@ void ppce500_init(MachineState *machine)
     }
 
     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, payload_name);
+    if (!filename) {
+        error_report("could not find firmware/kernel file '%s'", payload_name);
+        exit(1);
+    }
 
     payload_size = load_elf(filename, NULL, NULL, NULL,
                             &bios_entry, &loadaddr, NULL, NULL,
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson