From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47645) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dt1WE-0005Ad-6U for qemu-devel@nongnu.org; Fri, 15 Sep 2017 21:03:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dt1WB-000679-3B for qemu-devel@nongnu.org; Fri, 15 Sep 2017 21:03:38 -0400 Received: from mx1.redhat.com ([209.132.183.28]:57616) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dt1WA-00065r-T3 for qemu-devel@nongnu.org; Fri, 15 Sep 2017 21:03:35 -0400 From: John Snow Date: Fri, 15 Sep 2017 21:03:20 -0400 Message-Id: <20170916010330.10435-2-jsnow@redhat.com> In-Reply-To: <20170916010330.10435-1-jsnow@redhat.com> References: <20170916010330.10435-1-jsnow@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: [Qemu-devel] [PULL 01/11] ide: ahci: unparent children buses before freeing their memory List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, jsnow@redhat.com, Igor Mammedov From: Igor Mammedov Fixes read after freeing error reported https://lists.gnu.org/archive/html/qemu-devel/2017-08/msg04243.html Message-Id: <59a56959-ca12-ea75-33fa-ff07eba1b090@redhat.com> 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 =3D g_new0(AHCIDevice, ports); ... AHCIDevice *ad =3D &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. Reported-by: Thomas Huth Signed-off-by: Igor Mammedov Reviewed-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Michael S. Tsirkin Tested-by: Thomas Huth Reviewed-by: John Snow Message-id: 1503938085-169486-1-git-send-email-imammedo@redhat.com Signed-off-by: John Snow --- hw/ide/ahci.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hw/ide/ahci.c b/hw/ide/ahci.c index 406a1b5..ccbe091 100644 --- a/hw/ide/ahci.c +++ b/hw/ide/ahci.c @@ -1495,6 +1495,7 @@ void ahci_uninit(AHCIState *s) =20 ide_exit(s); } + object_unparent(OBJECT(&ad->port)); } =20 g_free(s->dev); --=20 2.9.5