Skip to contentSkip to conversation

qdev: Fix use after free in qdev_init_nofail error path

PATCH In review Igor Mammedov 2 participants 2 messages Aug 2, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 qdev: Fix use after free in qdev_init_nofail error path

Since 69382d8b (qdev: Fix object reference leak in case device.realize() fails), object_property_set_bool releases the device object in case realize's failed and device hasn't explicitly assigned parent. It happens due to object_unparent() on error handling path releases not only implicitly set parent but also calls device_unparent() which releases the remaining reference hold buy parent bus.

qdev_try_create() created device has only 1 reference hold by parent bus which makes impossible to unparent/re-parent object without destroying it.

Fix it by making caller of qdev_try_create()/qdev_init_nofail() own a reference from object_new() until qdev_init_nofail() is successfully executed so that uparenting won't destroy device until caller owns pointer returned by qdev_try_create().

---
 hw/core/qdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/qdev.c +1 -1
index ee4a083..0139d67 100644
@@ -151,7 +151,6 @@ DeviceState *qdev_try_create(BusState *bus, const char *type)
     }
 
     qdev_set_parent_bus(dev, bus);
-    object_unref(OBJECT(dev));
     return dev;
 }
 
@@ -360,6 +359,7 @@ void qdev_init_nofail(DeviceState *dev)
                           object_get_typename(OBJECT(dev)));
         exit(1);
     }
+    object_unref(OBJECT(dev));
 }
 
 void qdev_machine_creation_done(void)
Signature · 1 line
2.7.4
#1 lore raw
Quoted 8 lines · Igor Mammedov

Since 69382d8b (qdev: Fix object reference leak in case device.realize() fails), object_property_set_bool releases the device object in case realize's failed and device hasn't explicitly assigned parent. It happens due to object_unparent() on error handling path releases not only implicitly set parent but also calls device_unparent() which releases the remaining reference hold buy parent bus.

Quoted 8 lines

qdev_try_create() created device has only 1 reference hold by parent bus which makes impossible to unparent/re-parent object without destroying it.

Fix it by making caller of qdev_try_create()/qdev_init_nofail() own a reference from object_new() until qdev_init_nofail() is successfully executed so that uparenting won't destroy device until caller owns pointer returned by qdev_try_create().

Your patch is indeed nicer in theory, but in practice I'm not sure that there are no occurrences of object_new + qdev_init_nofail, or qdev_try_create + object_property_set_bool. These would be broken, so I'm going with Fam's.

Paolo

Quoted 26 lines
---
 hw/core/qdev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/core/qdev.c +1 -1
index ee4a083..0139d67 100644
@@ -151,7 +151,6 @@ DeviceState *qdev_try_create(BusState *bus, const char *type)
     }
 
     qdev_set_parent_bus(dev, bus);
-    object_unref(OBJECT(dev));
     return dev;
 }
 
@@ -360,6 +359,7 @@ void qdev_init_nofail(DeviceState *dev)
                           object_get_typename(OBJECT(dev)));
         exit(1);
     }
+    object_unref(OBJECT(dev));
 }
 
 void qdev_machine_creation_done(void)