From nobody Mon Feb 9 11:47:01 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1600693768; cv=none; d=zohomail.com; s=zohoarc; b=RfDxqzxGYQDXm1wNnK1jPpIWK+7tIb3m8aeCr2Q+VzZDzhBp/LHNmt4cyk9LBR8Is31If7/KcAKVBE2ErCmWNKcAykFaiH7IzKz0/8yyWyiVtJSaGs/hk/rxnW7BGLUYs8kCyU/v8p0jPunPeipFFMsDMRe4e8iFMhKOcqiqxQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1600693768; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7UwKTcATGBifVzQV/lZfPIFFDvoEDK/5FU4ocEmvFKs=; b=j5VtywX2GZycY7ZstMfvIi/TVntf+o0XG+pCgoq39tWe5OxBjhnHDOAZbKkV/retepIq7aWugFwrIt6vtk00rPrBRGeO/9rPz7kZ6XfB5qL1Q7cFjVwEumx9GItBw5XpGPqG18+McUd/RHf3148zbsKzVf/uLdvO+3EMI2WJe8c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1600693768445430.9279657434712; Mon, 21 Sep 2020 06:09:28 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-424-VBymNKqKPgmQPJeUhjp0Mg-1; Mon, 21 Sep 2020 09:09:24 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 494F4802B79; Mon, 21 Sep 2020 13:09:19 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 209E068D6E; Mon, 21 Sep 2020 13:09:19 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D9675922E2; Mon, 21 Sep 2020 13:09:18 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 08LD7khl020124 for ; Mon, 21 Sep 2020 09:07:46 -0400 Received: by smtp.corp.redhat.com (Postfix) id 165B1277C7; Mon, 21 Sep 2020 13:07:46 +0000 (UTC) Received: from localhost.localdomain.com (ovpn-112-154.ams2.redhat.com [10.36.112.154]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B84D1A913; Mon, 21 Sep 2020 13:07:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600693767; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=7UwKTcATGBifVzQV/lZfPIFFDvoEDK/5FU4ocEmvFKs=; b=Rn6mwYX4eJM/+s5h8QAkD+nJoXEXMvFSNj0S/UPLmoFJ91uDdkwJGFuWe8xYpBb1Lu6TLR NMM0ngblvnTif90YLRTQ/Iof1tkzOqOlLV8FfE9FA6UlaZth2HjXfjJwWybCDwYA2v0nB1 DBQcX6uwbjFvazRe35W4H9EDziD/BAI= X-MC-Unique: VBymNKqKPgmQPJeUhjp0Mg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH 6/9] cpu: Validate XML Date: Mon, 21 Sep 2020 15:07:29 +0200 Message-Id: <20200921130732.305622-7-twiederh@redhat.com> In-Reply-To: <20200921130732.305622-1-twiederh@redhat.com> References: <20200921130732.305622-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Cc: Tim Wiederhake X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Signed-off-by: Tim Wiederhake --- src/bhyve/bhyve_driver.c | 2 +- src/conf/cpu_conf.c | 28 ++++++++++++++++++++++++---- src/conf/cpu_conf.h | 6 ++++-- src/conf/domain_conf.c | 3 ++- src/cpu/cpu.c | 5 +++-- src/cpu/cpu.h | 3 ++- src/libxl/libxl_driver.c | 2 +- src/qemu/qemu_domain.c | 5 +++-- src/qemu/qemu_driver.c | 6 +++--- src/qemu/qemu_migration_cookie.c | 3 ++- tests/cputest.c | 5 +++-- 11 files changed, 48 insertions(+), 20 deletions(-) diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index daa20bad40..fc57ccd504 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -1465,7 +1465,7 @@ bhyveConnectCompareCPU(virConnectPtr conn, } } else { ret =3D virCPUCompareXML(caps->host.arch, caps->host.cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, false); } =20 cleanup: diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index dea950ce68..40d8da4a8e 100644 --- a/src/conf/cpu_conf.c +++ b/src/conf/cpu_conf.c @@ -20,9 +20,11 @@ =20 #include =20 +#include "configmake.h" #include "virerror.h" #include "viralloc.h" #include "virbuffer.h" +#include "virfile.h" #include "cpu_conf.h" #include "domain_conf.h" #include "virstring.h" @@ -281,7 +283,8 @@ virCPUDefCopy(const virCPUDef *cpu) int virCPUDefParseXMLString(const char *xml, virCPUType type, - virCPUDefPtr *cpu) + virCPUDefPtr *cpu, + bool validateXML) { xmlDocPtr doc =3D NULL; xmlXPathContextPtr ctxt =3D NULL; @@ -295,7 +298,7 @@ virCPUDefParseXMLString(const char *xml, if (!(doc =3D virXMLParseStringCtxt(xml, _("(CPU_definition)"), &ctxt)= )) goto cleanup; =20 - if (virCPUDefParseXML(ctxt, NULL, type, cpu) < 0) + if (virCPUDefParseXML(ctxt, NULL, type, cpu, validateXML) < 0) goto cleanup; =20 ret =3D 0; @@ -323,7 +326,8 @@ int virCPUDefParseXML(xmlXPathContextPtr ctxt, const char *xpath, virCPUType type, - virCPUDefPtr *cpu) + virCPUDefPtr *cpu, + bool validateXML) { g_autoptr(virCPUDef) def =3D NULL; g_autofree xmlNodePtr *nodes =3D NULL; @@ -348,6 +352,22 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt, return -1; } =20 + if (validateXML) { + g_autofree char *schemafile =3D NULL; + + if (!(schemafile =3D virFileFindResource("cpu.rng", + abs_top_srcdir "/docs/schem= as", + PKGDATADIR "/schemas"))) + return -1; + + if (virXMLValidateNodeAgainstSchema(schemafile, ctxt->doc, + ctxt->node) < 0) { + virReportError(VIR_ERR_XML_ERROR, "%s", + _("'cpu' element is not valid")); + return -1; + } + } + def =3D virCPUDefNew(); =20 if (type =3D=3D VIR_CPU_TYPE_AUTO) { @@ -1146,7 +1166,7 @@ virCPUDefListParse(const char **xmlCPUs, if (!(doc =3D virXMLParseStringCtxt(xmlCPUs[i], _("(CPU_definition= )"), &ctxt))) goto error; =20 - if (virCPUDefParseXML(ctxt, NULL, cpuType, &cpus[i]) < 0) + if (virCPUDefParseXML(ctxt, NULL, cpuType, &cpus[i], false) < 0) goto error; =20 xmlXPathFreeContext(ctxt); diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h index 24c51e3a63..3ef14b7932 100644 --- a/src/conf/cpu_conf.h +++ b/src/conf/cpu_conf.h @@ -192,13 +192,15 @@ virCPUDefCopyWithoutModel(const virCPUDef *cpu); int virCPUDefParseXMLString(const char *xml, virCPUType type, - virCPUDefPtr *cpu); + virCPUDefPtr *cpu, + bool validateXML); =20 int virCPUDefParseXML(xmlXPathContextPtr ctxt, const char *xpath, virCPUType mode, - virCPUDefPtr *cpu); + virCPUDefPtr *cpu, + bool validateXML); =20 bool virCPUDefIsEqual(virCPUDefPtr src, diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 4d296f7bcb..2ae5329938 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -21583,7 +21583,8 @@ virDomainDefParseXML(xmlDocPtr xml, } VIR_FREE(nodes); =20 - if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu)= < 0) + if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &def->cpu, + false) < 0) goto error; =20 if (virDomainNumaDefParseXML(def->numa, ctxt) < 0) diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index 188c5d86b5..bf94811960 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -109,14 +109,15 @@ virCPUCompareResult virCPUCompareXML(virArch arch, virCPUDefPtr host, const char *xml, - bool failIncompatible) + bool failIncompatible, + bool validateXML) { g_autoptr(virCPUDef) cpu =3D NULL; =20 VIR_DEBUG("arch=3D%s, host=3D%p, xml=3D%s", virArchToString(arch), host, NULLSTR(xml)); =20 - if (virCPUDefParseXMLString(xml, VIR_CPU_TYPE_AUTO, &cpu) < 0) + if (virCPUDefParseXMLString(xml, VIR_CPU_TYPE_AUTO, &cpu, validateXML)= < 0) return VIR_CPU_COMPARE_ERROR; =20 return virCPUCompare(arch, host, cpu, failIncompatible); diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index ba8fdd07ba..cc2d132275 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -153,7 +153,8 @@ virCPUCompareResult virCPUCompareXML(virArch arch, virCPUDefPtr host, const char *xml, - bool failIncompatible); + bool failIncompatible, + bool validateXML); =20 virCPUCompareResult virCPUCompare(virArch arch, diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index 161a6882f3..72864c2dc9 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -6541,7 +6541,7 @@ libxlConnectCompareCPU(virConnectPtr conn, cfg =3D libxlDriverConfigGet(driver); =20 ret =3D virCPUCompareXML(cfg->caps->host.arch, cfg->caps->host.cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, false); =20 virObjectUnref(cfg); return ret; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index ac443c5ddc..887a88936d 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -3135,7 +3135,8 @@ qemuDomainObjPrivateXMLParse(xmlXPathContextPtr ctxt, =20 qemuDomainSetPrivatePathsOld(driver, vm); =20 - if (virCPUDefParseXML(ctxt, "./cpu", VIR_CPU_TYPE_GUEST, &priv->origCP= U) < 0) + if (virCPUDefParseXML(ctxt, "./cpu", VIR_CPU_TYPE_GUEST, &priv->origCP= U, + false) < 0) goto error; =20 priv->chardevStdioLogd =3D virXPathBoolean("boolean(./chardevStdioLogd= )", @@ -10003,7 +10004,7 @@ qemuDomainSaveCookieParse(xmlXPathContextPtr ctxt G= _GNUC_UNUSED, return -1; =20 if (virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, - &cookie->cpu) < 0) + &cookie->cpu, false) < 0) return -1; =20 cookie->slirpHelper =3D virXPathBoolean("boolean(./slirpHelper)", ctxt= ) > 0; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index ae715c01d7..a7961ad3f0 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -12240,7 +12240,7 @@ qemuConnectCompareCPU(virConnectPtr conn, return VIR_CPU_COMPARE_ERROR; =20 return virCPUCompareXML(driver->hostarch, cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, false); } =20 =20 @@ -12330,10 +12330,10 @@ qemuConnectCompareHypervisorCPU(virConnectPtr con= n, } =20 if (ARCH_IS_X86(arch)) { - ret =3D virCPUCompareXML(arch, hvCPU, xmlCPU, failIncompatible); + ret =3D virCPUCompareXML(arch, hvCPU, xmlCPU, failIncompatible, fa= lse); } else if (ARCH_IS_S390(arch) && virQEMUCapsGet(qemuCaps, QEMU_CAPS_QUERY_CPU_MODEL_COMPARIS= ON)) { - if (virCPUDefParseXMLString(xmlCPU, VIR_CPU_TYPE_AUTO, &cpu) < 0) + if (virCPUDefParseXMLString(xmlCPU, VIR_CPU_TYPE_AUTO, &cpu, false= ) < 0) goto cleanup; =20 ret =3D qemuConnectCPUModelComparison(qemuCaps, cfg->libDir, diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_coo= kie.c index 3ea46e1527..aef0b042e5 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -1342,7 +1342,8 @@ qemuMigrationCookieXMLParse(qemuMigrationCookiePtr mi= g, goto error; =20 if (flags & QEMU_MIGRATION_COOKIE_CPU && - virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &mig->cpu)= < 0) + virCPUDefParseXML(ctxt, "./cpu[1]", VIR_CPU_TYPE_GUEST, &mig->cpu, + false) < 0) goto error; =20 if (flags & QEMU_MIGRATION_COOKIE_ALLOW_REBOOT && diff --git a/tests/cputest.c b/tests/cputest.c index 83d63bf495..b40fd7f7f2 100644 --- a/tests/cputest.c +++ b/tests/cputest.c @@ -81,7 +81,7 @@ cpuTestLoadXML(virArch arch, const char *name) if (!(doc =3D virXMLParseFileCtxt(xml, &ctxt))) goto cleanup; =20 - virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu); + virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_AUTO, &cpu, false); =20 cleanup: xmlXPathFreeContext(ctxt); @@ -118,7 +118,8 @@ cpuTestLoadMultiXML(virArch arch, =20 for (i =3D 0; i < n; i++) { ctxt->node =3D nodes[i]; - if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i]) < 0) + if (virCPUDefParseXML(ctxt, NULL, VIR_CPU_TYPE_HOST, &cpus[i], + false) < 0) goto cleanup_cpus; } =20 --=20 2.26.2