From patchwork Tue Aug 2 10:49:20 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Igor Mammedov X-Patchwork-Id: 9255457 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D9F656077C for ; Tue, 2 Aug 2016 10:49:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CBC9728506 for ; Tue, 2 Aug 2016 10:49:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C097928514; Tue, 2 Aug 2016 10:49:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 6390128506 for ; Tue, 2 Aug 2016 10:49:58 +0000 (UTC) Received: from localhost ([::1]:55306 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUXGn-00080t-Ho for patchwork-qemu-devel@patchwork.kernel.org; Tue, 02 Aug 2016 06:49:57 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:32808) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUXGM-0007vh-Gm for qemu-devel@nongnu.org; Tue, 02 Aug 2016 06:49:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bUXGG-0001NT-GW for qemu-devel@nongnu.org; Tue, 02 Aug 2016 06:49:29 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46564) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bUXGG-0001NP-Al for qemu-devel@nongnu.org; Tue, 02 Aug 2016 06:49:24 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D68B680082 for ; Tue, 2 Aug 2016 10:49:23 +0000 (UTC) Received: from dell-r430-03.lab.eng.brq.redhat.com (dell-r430-03.lab.eng.brq.redhat.com [10.34.112.60]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u72AnMLm016581; Tue, 2 Aug 2016 06:49:22 -0400 From: Igor Mammedov To: qemu-devel@nongnu.org Date: Tue, 2 Aug 2016 12:49:20 +0200 Message-Id: <1470134960-87851-1-git-send-email-imammedo@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 02 Aug 2016 10:49:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] qdev: Fix use after free in qdev_init_nofail error path X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: famz@redhat.com, jsnow@redhat.com, ehabkost@redhat.com, pbonzini@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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(). 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)