From patchwork Tue Mar 20 10:22:48 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laszlo Ersek X-Patchwork-Id: 147868 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 396DFB6FC3 for ; Wed, 21 Mar 2012 10:16:59 +1100 (EST) Received: from localhost ([::1]:57819 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SA85a-0007E1-Ok for incoming@patchwork.ozlabs.org; Tue, 20 Mar 2012 19:03:38 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36872) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S9wCZ-0006lU-3B for qemu-devel@nongnu.org; Tue, 20 Mar 2012 06:22:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S9wCS-00031j-S4 for qemu-devel@nongnu.org; Tue, 20 Mar 2012 06:22:02 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37279) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S9wCS-00031Q-KG for qemu-devel@nongnu.org; Tue, 20 Mar 2012 06:21:56 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q2KALrbH005156 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Tue, 20 Mar 2012 06:21:53 -0400 Received: from lacos-laptop.usersys.redhat.com (vpn1-4-246.ams2.redhat.com [10.36.4.246]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q2KALnFT004642; Tue, 20 Mar 2012 06:21:51 -0400 From: Laszlo Ersek To: qemu-devel@nongnu.org, pbonzini@redhat.com, lcapitulino@redhat.com, jcody@redhat.com, aliguori@us.ibm.com, mdroth@linux.vnet.ibm.com, lersek@redhat.com Date: Tue, 20 Mar 2012 11:22:48 +0100 Message-Id: <1332238969-5770-2-git-send-email-lersek@redhat.com> In-Reply-To: <20120320004332.GA25518@illuin> References: <20120320004332.GA25518@illuin> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 X-Mailman-Approved-At: Tue, 20 Mar 2012 19:02:54 -0400 Subject: [Qemu-devel] [PATCH v2 1/2] qapi: fix double free in qmp_output_visitor_cleanup() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Stack entries in QmpOutputVisitor are navigation links (weak references), except the bottom (ie. least recently added) entry, which owns the root QObject [1]. Make qmp_output_visitor_cleanup() drop the stack entries, then release the QObject tree by the root. Attempting to serialize an invalid enum inside a dictionary is an example for triggering the double free. [1] http://lists.nongnu.org/archive/html/qemu-devel/2012-03/msg03276.html Signed-off-by: Laszlo Ersek --- qapi/qmp-output-visitor.c | 8 +++++--- 1 files changed, 5 insertions(+), 3 deletions(-) diff --git a/qapi/qmp-output-visitor.c b/qapi/qmp-output-visitor.c index e0697b0..2bce9d5 100644 --- a/qapi/qmp-output-visitor.c +++ b/qapi/qmp-output-visitor.c @@ -199,14 +199,16 @@ void qmp_output_visitor_cleanup(QmpOutputVisitor *v) { QStackEntry *e, *tmp; + /* The bottom QStackEntry, if any, owns the root QObject. See the + * qmp_output_push_obj() invocations in qmp_output_add_obj(). */ + QObject *root = QTAILQ_EMPTY(&v->stack) ? NULL : qmp_output_first(v); + QTAILQ_FOREACH_SAFE(e, &v->stack, node, tmp) { QTAILQ_REMOVE(&v->stack, e, node); - if (e->value) { - qobject_decref(e->value); - } g_free(e); } + qobject_decref(root); g_free(v); }