Skip to contentSkip to conversation

ide: ahci: unparent children buses before freeing their memory

PATCH Applied Igor Mammedov 5 participants 5 messages Aug 28, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ide: ahci: unparent children buses before freeing their memory

Fixes read after freeing error reported
https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html
Message-Id: <[email protected]>

ich9-ahci device creates ide buses and attaches them as QOM children at realize time, however it forgets to properly clean them up at unrealize time and frees memory containing these children, with following call-chain:

   qdev_device_add()
     object_property_set_bool('realized', true)
       device_set_realized()
          ...
          pci_qdev_realize() -> pci_ich9_ahci_realize() -> ahci_realize()
               ...
               s->dev = g_new0(AHCIDevice, ports);
               ...
                  AHCIDevice *ad = &s->dev[i];
                  ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1);
                  ^^^ creates bus in memory allocated by above gnew()
                      and adds it as child propety to ahci device
          ...
          hotplug_handler_plug(); -> goto post_realize_fail;
          pci_qdev_unrealize() -> pci_ich9_uninit() -> ahci_uninit()
              ...
               g_free(s->dev);
               ^^^ free memory that holds children busses

return with error from device_set_realized()

As result later when qdev_device_add() tries to unparent ich9-ahci after failed device_set_realized(),

    object_unparent() -> object_property_del_child()

iterates over existing QOM children including buses added by ide_bus_new() and tries to unparent them, which causes access to freed memory where they where located.

---
 hw/ide/ahci.c | 1 +
 1 file changed, 1 insertion(+)
hw/ide/ahci.c +1 -0
index 406a1b5..ccbe091 100644
@@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s)
 
             ide_exit(s);
         }
+        object_unparent(OBJECT(&ad->port));
     }
 
     g_free(s->dev);
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 32 lines · Igor Mammedov

Fixes read after freeing error reported
https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html
Message-Id: <[email protected]>

ich9-ahci device creates ide buses and attaches them as QOM children at realize time, however it forgets to properly clean them up at unrealize time and frees memory containing these children, with following call-chain:

    qdev_device_add()
      object_property_set_bool('realized', true)
        device_set_realized()
           ...
           pci_qdev_realize() -> pci_ich9_ahci_realize() -> ahci_realize()
                ...
                s->dev = g_new0(AHCIDevice, ports);
                ...
                   AHCIDevice *ad = &s->dev[i];
                   ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1);
                   ^^^ creates bus in memory allocated by above gnew()
                       and adds it as child propety to ahci device
           ...
           hotplug_handler_plug(); -> goto post_realize_fail;
           pci_qdev_unrealize() -> pci_ich9_uninit() -> ahci_uninit()
               ...
                g_free(s->dev);
                ^^^ free memory that holds children busses

           return with error from device_set_realized()

As result later when qdev_device_add() tries to unparent ich9-ahci after failed device_set_realized(),

Quoted 7 lines
     object_unparent() -> object_property_del_child()

iterates over existing QOM children including buses added by ide_bus_new() and tries to unparent them, which causes access to freed memory where they where located.

Quoted 17 lines
---
  hw/ide/ahci.c | 1 +
  1 file changed, 1 insertion(+)
hw/ide/ahci.c +1 -0
index 406a1b5..ccbe091 100644
@@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s)
 
              ide_exit(s);
          }
+        object_unparent(OBJECT(&ad->port));
      }
 
      g_free(s->dev);
Tested-by on v1 1/1
#2 lore raw
In reply to Igor Mammedov
Quoted 49 lines · Igor Mammedov

Fixes read after freeing error reported
https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html
Message-Id: <[email protected]>

ich9-ahci device creates ide buses and attaches them as QOM children at realize time, however it forgets to properly clean them up at unrealize time and frees memory containing these children, with following call-chain:

   qdev_device_add()
     object_property_set_bool('realized', true)
       device_set_realized()
          ...
          pci_qdev_realize() -> pci_ich9_ahci_realize() -> ahci_realize()
               ...
               s->dev = g_new0(AHCIDevice, ports);
               ...
                  AHCIDevice *ad = &s->dev[i];
                  ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1);
                  ^^^ creates bus in memory allocated by above gnew()
                      and adds it as child propety to ahci device
          ...
          hotplug_handler_plug(); -> goto post_realize_fail;
          pci_qdev_unrealize() -> pci_ich9_uninit() -> ahci_uninit()
              ...
               g_free(s->dev);
               ^^^ free memory that holds children busses

return with error from device_set_realized()

As result later when qdev_device_add() tries to unparent ich9-ahci after failed device_set_realized(),

    object_unparent() -> object_property_del_child()

iterates over existing QOM children including buses added by ide_bus_new() and tries to unparent them, which causes access to freed memory where they where located.

---
 hw/ide/ahci.c | 1 +
 1 file changed, 1 insertion(+)
hw/ide/ahci.c +0 -0
index 406a1b5..ccbe091 100644

hw/ide/ahci.c void ahci_uninit(AHCIState *s) · line 1501

Quoted 7 lines
hw/ide/ahci.c +1 -0
@@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s)
 
             ide_exit(s);
         }
+        object_unparent(OBJECT(&ad->port));
     }
 
     g_free(s->dev);

Thanks, this fixes the problem for me with both, x86_64 and mips64el!

Reviewed-by on v1 1/1
#3 lore raw
In reply to Igor Mammedov
Quoted 32 lines · Igor Mammedov

Fixes read after freeing error reported
https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html
Message-Id: <[email protected]>

ich9-ahci device creates ide buses and attaches them as QOM children at realize time, however it forgets to properly clean them up at unrealize time and frees memory containing these children, with following call-chain:

   qdev_device_add()
     object_property_set_bool('realized', true)
       device_set_realized()
          ...
          pci_qdev_realize() -> pci_ich9_ahci_realize() -> ahci_realize()
               ...
               s->dev = g_new0(AHCIDevice, ports);
               ...
                  AHCIDevice *ad = &s->dev[i];
                  ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1);
                  ^^^ creates bus in memory allocated by above gnew()
                      and adds it as child propety to ahci device
          ...
          hotplug_handler_plug(); -> goto post_realize_fail;
          pci_qdev_unrealize() -> pci_ich9_uninit() -> ahci_uninit()
              ...
               g_free(s->dev);
               ^^^ free memory that holds children busses

return with error from device_set_realized()

As result later when qdev_device_add() tries to unparent ich9-ahci after failed device_set_realized(),

Quoted 7 lines
    object_unparent() -> object_property_del_child()

iterates over existing QOM children including buses added by ide_bus_new() and tries to unparent them, which causes access to freed memory where they where located.

Pls merge through ide tree.

Quoted 18 lines
---
 hw/ide/ahci.c | 1 +
 1 file changed, 1 insertion(+)
hw/ide/ahci.c +1 -0
index 406a1b5..ccbe091 100644
@@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s)
 
             ide_exit(s);
         }
+        object_unparent(OBJECT(&ad->port));
     }
 
     g_free(s->dev);
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1 Applied
#4 lore raw
In reply to Igor Mammedov
Quoted 49 lines · Igor Mammedov

Fixes read after freeing error reported
https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html
Message-Id: <[email protected]>

ich9-ahci device creates ide buses and attaches them as QOM children at realize time, however it forgets to properly clean them up at unrealize time and frees memory containing these children, with following call-chain:

   qdev_device_add()
     object_property_set_bool('realized', true)
       device_set_realized()
          ...
          pci_qdev_realize() -> pci_ich9_ahci_realize() -> ahci_realize()
               ...
               s->dev = g_new0(AHCIDevice, ports);
               ...
                  AHCIDevice *ad = &s->dev[i];
                  ide_bus_new(&ad->port, sizeof(ad->port), qdev, i, 1);
                  ^^^ creates bus in memory allocated by above gnew()
                      and adds it as child propety to ahci device
          ...
          hotplug_handler_plug(); -> goto post_realize_fail;
          pci_qdev_unrealize() -> pci_ich9_uninit() -> ahci_uninit()
              ...
               g_free(s->dev);
               ^^^ free memory that holds children busses

return with error from device_set_realized()

As result later when qdev_device_add() tries to unparent ich9-ahci after failed device_set_realized(),

    object_unparent() -> object_property_del_child()

iterates over existing QOM children including buses added by ide_bus_new() and tries to unparent them, which causes access to freed memory where they where located.

---
 hw/ide/ahci.c | 1 +
 1 file changed, 1 insertion(+)
hw/ide/ahci.c +0 -0
index 406a1b5..ccbe091 100644

hw/ide/ahci.c void ahci_uninit(AHCIState *s) · line 1501

Quoted 7 lines
hw/ide/ahci.c +1 -0
@@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s)
 
             ide_exit(s);
         }
+        object_unparent(OBJECT(&ad->port));
     }
 
     g_free(s->dev);

Nice, Thank you.

Thanks, applied to my IDE tree:

https://github.com/jnsnow/qemu/commits/ide
https://github.com/jnsnow/qemu.git

--js