From patchwork Fri Apr 25 18:29:44 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Luiz Capitulino X-Patchwork-Id: 342974 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)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 783E0140127 for ; Sat, 26 Apr 2014 04:47:02 +1000 (EST) Received: from localhost ([::1]:59570 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wdl9I-0006jq-3w for incoming@patchwork.ozlabs.org; Fri, 25 Apr 2014 14:47:00 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33210) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wdl7k-00053R-S8 for qemu-devel@nongnu.org; Fri, 25 Apr 2014 14:45:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Wdl7e-0005k1-59 for qemu-devel@nongnu.org; Fri, 25 Apr 2014 14:45:24 -0400 Received: from mx1.redhat.com ([209.132.183.28]:32063) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Wdl7d-0005jd-SN for qemu-devel@nongnu.org; Fri, 25 Apr 2014 14:45:18 -0400 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3PIj3eZ029716 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 25 Apr 2014 14:45:13 -0400 Received: from localhost (ovpn-113-123.phx2.redhat.com [10.3.113.123]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s3PIU724013914; Fri, 25 Apr 2014 14:30:07 -0400 From: Luiz Capitulino To: peter.maydell@linaro.org Date: Fri, 25 Apr 2014 14:29:44 -0400 Message-Id: <1398450587-24758-14-git-send-email-lcapitulino@redhat.com> In-Reply-To: <1398450587-24758-1-git-send-email-lcapitulino@redhat.com> References: <1398450587-24758-1-git-send-email-lcapitulino@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: qemu-devel@nongnu.org, anthony@codemonkey.ws Subject: [Qemu-devel] [PULL 13/16] qmp: object-add: Validate class before creating object 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 From: Eduardo Habkost Currently it is very easy to crash QEMU by issuing an object-add command using an abstract class or a class that doesn't support TYPE_USER_CREATABLE as parameter. Example: with the following QMP command: (QEMU) object-add qom-type=cpu id=foo QEMU aborts at: ERROR:qom/object.c:335:object_initialize_with_type: assertion failed: (type->abstract == false) This patch moves the check for TYPE_USER_CREATABLE before object_new(), and adds a check to prevent the code from trying to instantiate abstract classes. Signed-off-by: Eduardo Habkost Reviewed-by: Matthew Rosato Tested-by: Matthew Rosato Signed-off-by: Luiz Capitulino --- qmp.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/qmp.c b/qmp.c index 5e2a66c..74107be 100644 --- a/qmp.c +++ b/qmp.c @@ -540,14 +540,27 @@ void object_add(const char *type, const char *id, const QDict *qdict, Visitor *v, Error **errp) { Object *obj; + ObjectClass *klass; const QDictEntry *e; Error *local_err = NULL; - if (!object_class_by_name(type)) { + klass = object_class_by_name(type); + if (!klass) { error_setg(errp, "invalid class name"); return; } + if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) { + error_setg(errp, "object type '%s' isn't supported by object-add", + type); + return; + } + + if (object_class_is_abstract(klass)) { + error_setg(errp, "object type '%s' is abstract", type); + return; + } + obj = object_new(type); if (qdict) { for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) { @@ -558,12 +571,6 @@ void object_add(const char *type, const char *id, const QDict *qdict, } } - if (!object_dynamic_cast(obj, TYPE_USER_CREATABLE)) { - error_setg(&local_err, "object type '%s' isn't supported by object-add", - type); - goto out; - } - user_creatable_complete(obj, &local_err); if (local_err) { goto out;