Skip to contentSkip to conversation

hw/arm: do not free machine->fdt in arm_load_dtb()

PATCH Applied Markus Armbruster 4 participants 5 messages Mar 28, 2023 – Mar 30, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hw/arm: do not free machine->fdt in arm_load_dtb()

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

Since monitor command 'dumpdtb' was introduced a couple of releases ago, running it with any ARM machine that uses arm_load_dtb() will crash QEMU.

Let's enable all arm_load_dtb() callers to use dumpdtb properly. Instead of freeing 'fdt', assign it back to ms->fdt.

Cc Peter Maydell <[email protected]>
Fixes bf353ad55590f ("qmp/hmp, device_tree.c: introduce dumpdtb")
---
 hw/arm/boot.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/arm/boot.c +4 -1
index 50e5141116..54f6a3e0b3 100644
@@ -689,7 +689,10 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
                                        rom_ptr_for_as(as, addr, size));
 
-    g_free(fdt);
+    if (fdt != ms->fdt) {
+        g_free(ms->fdt);
+        ms->fdt = fdt;
+    }
 
     return size;
 
Signature · 1 line
2.39.2
Reviewed-by on v3 1/1
#1 lore raw
Quoted 14 lines · Markus Armbruster

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

Since monitor command 'dumpdtb' was introduced a couple of releases ago, running it with any ARM machine that uses arm_load_dtb() will crash QEMU.

Let's enable all arm_load_dtb() callers to use dumpdtb properly. Instead of freeing 'fdt', assign it back to ms->fdt.

Quoted 7 lines
Cc Peter Maydell <[email protected]>
Fixes bf353ad55590f ("qmp/hmp, device_tree.c: introduce dumpdtb")

---

Quoted 19 lines
  hw/arm/boot.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
hw/arm/boot.c +4 -1
index 50e5141116..54f6a3e0b3 100644
@@ -689,7 +689,10 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
      qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
                                         rom_ptr_for_as(as, addr, size));
 
-    g_free(fdt);
+    if (fdt != ms->fdt) {
+        g_free(ms->fdt);
+        ms->fdt = fdt;
+    }
 
      return size;
 
#2 lore raw
In reply to Markus Armbruster
Quoted 32 lines · Markus Armbruster

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

Since monitor command 'dumpdtb' was introduced a couple of releases ago, running it with any ARM machine that uses arm_load_dtb() will crash QEMU.

Let's enable all arm_load_dtb() callers to use dumpdtb properly. Instead of freeing 'fdt', assign it back to ms->fdt.

Cc Peter Maydell <[email protected]>
Fixes bf353ad55590f ("qmp/hmp, device_tree.c: introduce dumpdtb")
---
 hw/arm/boot.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/arm/boot.c +0 -0
index 50e5141116..54f6a3e0b3 100644
@@ -689,7 +689,10 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
     qemu_register_reset_nosnapshotload(qemu_fdt_randomize_seeds,
                                        rom_ptr_for_as(as, addr, size));

hw/arm/boot.c int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,

Quoted 10 lines
hw/arm/boot.c +4 -2
 
-    g_free(fdt);
+    if (fdt != ms->fdt) {
+        g_free(ms->fdt);
+        ms->fdt = fdt;
+    }
 
     return size;
 
--

Applied to target-arm.next, thanks.

-- PMM

#3 lore raw
In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

Since monitor command 'dumpdtb' was introduced a couple of releases ago, running it with any ARM machine that uses arm_load_dtb() will crash QEMU.

Quoted 7 lines

Let's enable all arm_load_dtb() callers to use dumpdtb properly. Instead of freeing 'fdt', assign it back to ms->fdt.

Cc Peter Maydell <[email protected]>
Fixes bf353ad55590f ("qmp/hmp, device_tree.c: introduce dumpdtb")

^ Seems like a typo

-Alex

#4 lore raw
Quoted 15 lines · Alexander Bulekov
Quoted 14 lines · Markus Armbruster

At this moment, arm_load_dtb() can free machine->fdt when binfo->dtb_filename is NULL. If there's no 'dtb_filename', 'fdt' will be retrieved by binfo->get_dtb(). If get_dtb() returns machine->fdt, as is the case of machvirt_dtb() from hw/arm/virt.c, fdt now has a pointer to machine->fdt. And, in that case, the existing g_free(fdt) at the end of arm_load_dtb() will make machine->fdt point to an invalid memory region.

Since monitor command 'dumpdtb' was introduced a couple of releases ago, running it with any ARM machine that uses arm_load_dtb() will crash QEMU.

Let's enable all arm_load_dtb() callers to use dumpdtb properly. Instead of freeing 'fdt', assign it back to ms->fdt.

Quoted 7 lines
Quoted 4 lines
Cc Peter Maydell <[email protected]>
Fixes bf353ad55590f ("qmp/hmp, device_tree.c: introduce dumpdtb")

^ Seems like a typo

-Alex

Yeah it is. Persisted ever since v2 :D

Nice catch!

Daniel