From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54291) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUcyZ-0003OL-SL for qemu-devel@nongnu.org; Tue, 02 Aug 2016 12:55:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bUcyT-0001OK-Pr for qemu-devel@nongnu.org; Tue, 02 Aug 2016 12:55:30 -0400 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]:35804) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUcyT-0001O5-Ip for qemu-devel@nongnu.org; Tue, 02 Aug 2016 12:55:25 -0400 Received: by mail-wm0-x243.google.com with SMTP id i5so31940651wmg.2 for ; Tue, 02 Aug 2016 09:55:25 -0700 (PDT) Sender: Paolo Bonzini References: <1470134960-87851-1-git-send-email-imammedo@redhat.com> From: Paolo Bonzini Message-ID: Date: Tue, 2 Aug 2016 18:55:13 +0200 MIME-Version: 1.0 In-Reply-To: <1470134960-87851-1-git-send-email-imammedo@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v2] qdev: Fix use after free in qdev_init_nofail error path List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Igor Mammedov , qemu-devel@nongnu.org Cc: famz@redhat.com, jsnow@redhat.com, ehabkost@redhat.com On 02/08/2016 12:49, Igor Mammedov wrote: > 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(). 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 > Signed-off-by: Igor Mammedov > --- > hw/core/qdev.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/hw/core/qdev.c b/hw/core/qdev.c > index ee4a083..0139d67 100644 > --- a/hw/core/qdev.c > +++ b/hw/core/qdev.c > @@ -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) >