From nobody Mon Feb 9 17:05:27 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.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 216.205.24.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=1602060964; cv=none; d=zohomail.com; s=zohoarc; b=VG6ycYaS1V2bKnUc5mMXkTCGwVfiLk/sFg6o40F3efnS2Azw4/h34j41si0p3Sqy+ccckDOSyD95AQ/oxYwyVW/VqoIHkcnBKjruYomInJypH0hTZza0GE+1z6O4PjJ/D734pM4zZbI0748LTHobp5VD7IdMmQy6e7g1IFnnBBE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1602060964; 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=mQYKA1aQUWlTUN7Dm16FyXpgk7NvmEGoaFJ/ITuRp8I=; b=NkIuKkgPIzgK3u5GEA4Cmr4dJ08ondJRJM2ywf2xwGI0mblf5oWNHzq6IEsupROl/2qLFooj9OffLSuyDhgkDm0BPx93dUgLAuqerp76XmkocsyuijUjRr7746tZpoR/NzcyzNrwsoUXmYUQNkaWRLszdTLY6sXb94mABitiDEg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.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 [216.205.24.124]) by mx.zohomail.com with SMTPS id 160206096487670.33290369702308; Wed, 7 Oct 2020 01:56:04 -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-551-TBxALMP_Mbq9vroaJmBRDg-1; Wed, 07 Oct 2020 04:55:31 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0B66780400D; Wed, 7 Oct 2020 08:55:26 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DEE806EF5E; Wed, 7 Oct 2020 08:55:25 +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 AD4EA181A06E; Wed, 7 Oct 2020 08:55:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 0978t6X4016898 for ; Wed, 7 Oct 2020 04:55:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 39F017664C; Wed, 7 Oct 2020 08:55:06 +0000 (UTC) Received: from work.redhat.com (ovpn-112-213.ams2.redhat.com [10.36.112.213]) by smtp.corp.redhat.com (Postfix) with ESMTP id 79C4376644; Wed, 7 Oct 2020 08:55:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1602060963; 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=mQYKA1aQUWlTUN7Dm16FyXpgk7NvmEGoaFJ/ITuRp8I=; b=HsPWFOa06Knd8kYfCgPtANjjJ/eW5d/mu9YFGk85tmhx1W/TMPs0/w1sJ6VEgxS2/RVPoi h45NXcgzJf4tBAMvSprJjZIJYbhIyA6jUq6fWWQvsH9Wv9JwE8ISwJ8SreTBV9Ruh/4XuE vYsJPZ3/YZX5M1M5pO0uRCMw+kVxqPs= X-MC-Unique: TBxALMP_Mbq9vroaJmBRDg-1 From: Tim Wiederhake To: libvir-list@redhat.com Subject: [libvirt PATCH v4 2/5] cpu: Wire in XML validation Date: Wed, 7 Oct 2020 10:54:55 +0200 Message-Id: <20201007085458.231179-3-twiederh@redhat.com> In-Reply-To: <20201007085458.231179-1-twiederh@redhat.com> References: <20201007085458.231179-1-twiederh@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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.13 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" This adds a new value to virConnectCompareCPUFlags, "VIR_CONNECT_CPU_VALIDATE_XML", that governs XML document validation in virCPUDefParseXML. In src/conf/cpu_conf.c, include configmake.h for PKGDATADIR and virfile.h for virFileFindResource. Signed-off-by: Tim Wiederhake Reviewed-by: Peter Krempa --- include/libvirt/libvirt-host.h | 2 ++ src/bhyve/bhyve_driver.c | 7 +++++-- src/conf/cpu_conf.c | 25 +++++++++++++++++++++---- 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 | 7 +++++-- src/qemu/qemu_domain.c | 5 +++-- src/qemu/qemu_driver.c | 18 +++++++++++++----- src/qemu/qemu_migration_cookie.c | 3 ++- tests/cputest.c | 5 +++-- 12 files changed, 65 insertions(+), 24 deletions(-) diff --git a/include/libvirt/libvirt-host.h b/include/libvirt/libvirt-host.h index 6972834175..4caed94a77 100644 --- a/include/libvirt/libvirt-host.h +++ b/include/libvirt/libvirt-host.h @@ -754,6 +754,8 @@ typedef enum { typedef enum { VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE =3D (1 << 0), /* treat incom= patible CPUs as failu= re */ + VIR_CONNECT_COMPARE_CPU_VALIDATE_XML =3D (1 << 1), /* validate the xml + document */ } virConnectCompareCPUFlags; =20 int virConnectCompareCPU(virConnectPtr conn, diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c index 91f41aa238..78c3241293 100644 --- a/src/bhyve/bhyve_driver.c +++ b/src/bhyve/bhyve_driver.c @@ -1441,14 +1441,17 @@ bhyveConnectCompareCPU(virConnectPtr conn, int ret =3D VIR_CPU_COMPARE_ERROR; virCapsPtr caps =3D NULL; bool failIncompatible; + bool validateXML; =20 - virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE, + virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE | + VIR_CONNECT_COMPARE_CPU_VALIDATE_XML, VIR_CPU_COMPARE_ERROR); =20 if (virConnectCompareCPUEnsureACL(conn) < 0) goto cleanup; =20 failIncompatible =3D !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATI= BLE); + validateXML =3D !!(flags & VIR_CONNECT_COMPARE_CPU_VALIDATE_XML); =20 if (!(caps =3D bhyveDriverGetCapabilities(driver))) goto cleanup; @@ -1464,7 +1467,7 @@ bhyveConnectCompareCPU(virConnectPtr conn, } } else { ret =3D virCPUCompareXML(caps->host.arch, caps->host.cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, validateXML); } =20 cleanup: diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c index dea950ce68..1910470836 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,19 @@ 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) + return -1; + } + def =3D virCPUDefNew(); =20 if (type =3D=3D VIR_CPU_TYPE_AUTO) { @@ -1146,7 +1163,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 175b632a38..c003b5c030 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -21917,7 +21917,8 @@ virDomainDefParseXML(xmlDocPtr xml, if (virDomainDefTunablesParse(def, ctxt, xmlopt, flags) < 0) goto error; =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 e28c649688..824ed60dfd 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -6529,19 +6529,22 @@ libxlConnectCompareCPU(virConnectPtr conn, libxlDriverConfigPtr cfg; int ret =3D VIR_CPU_COMPARE_ERROR; bool failIncompatible; + bool validateXML; =20 - virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE, + virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE | + VIR_CONNECT_COMPARE_CPU_VALIDATE_XML, VIR_CPU_COMPARE_ERROR); =20 if (virConnectCompareCPUEnsureACL(conn) < 0) return ret; =20 failIncompatible =3D !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATI= BLE); + validateXML =3D !!(flags & VIR_CONNECT_COMPARE_CPU_VALIDATE_XML); =20 cfg =3D libxlDriverConfigGet(driver); =20 ret =3D virCPUCompareXML(cfg->caps->host.arch, cfg->caps->host.cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, validateXML); =20 virObjectUnref(cfg); return ret; diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 0687f4584c..9623123d3c 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -3130,7 +3130,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= )", @@ -9997,7 +9998,7 @@ qemuDomainSaveCookieParse(xmlXPathContextPtr ctxt G_G= NUC_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 5aaa969409..8ef812cd94 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -12202,20 +12202,23 @@ qemuConnectCompareCPU(virConnectPtr conn, virQEMUDriverPtr driver =3D conn->privateData; g_autoptr(virCPUDef) cpu =3D NULL; bool failIncompatible; + bool validateXML; =20 - virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE, + virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE | + VIR_CONNECT_COMPARE_CPU_VALIDATE_XML, VIR_CPU_COMPARE_ERROR); =20 if (virConnectCompareCPUEnsureACL(conn) < 0) return VIR_CPU_COMPARE_ERROR; =20 failIncompatible =3D !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATI= BLE); + validateXML =3D !!(flags & VIR_CONNECT_COMPARE_CPU_VALIDATE_XML); =20 if (!(cpu =3D virQEMUDriverGetHostCPU(driver))) return VIR_CPU_COMPARE_ERROR; =20 return virCPUCompareXML(driver->hostarch, cpu, - xmlDesc, failIncompatible); + xmlDesc, failIncompatible, validateXML); } =20 =20 @@ -12270,18 +12273,21 @@ qemuConnectCompareHypervisorCPU(virConnectPtr con= n, g_autoptr(virQEMUDriverConfig) cfg =3D virQEMUDriverGetConfig(driver); g_autoptr(virQEMUCaps) qemuCaps =3D NULL; bool failIncompatible; + bool validateXML; virCPUDefPtr hvCPU; virCPUDefPtr cpu =3D NULL; virArch arch; virDomainVirtType virttype; =20 - virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE, + virCheckFlags(VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATIBLE | + VIR_CONNECT_COMPARE_CPU_VALIDATE_XML, VIR_CPU_COMPARE_ERROR); =20 if (virConnectCompareHypervisorCPUEnsureACL(conn) < 0) goto cleanup; =20 failIncompatible =3D !!(flags & VIR_CONNECT_COMPARE_CPU_FAIL_INCOMPATI= BLE); + validateXML =3D !!(flags & VIR_CONNECT_COMPARE_CPU_VALIDATE_XML); =20 qemuCaps =3D virQEMUCapsCacheLookupDefault(driver->qemuCapsCache, emulator, @@ -12305,10 +12311,12 @@ qemuConnectCompareHypervisorCPU(virConnectPtr con= n, } =20 if (ARCH_IS_X86(arch)) { - ret =3D virCPUCompareXML(arch, hvCPU, xmlCPU, failIncompatible); + ret =3D virCPUCompareXML(arch, hvCPU, xmlCPU, failIncompatible, + validateXML); } 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, + validateXML) < 0) goto cleanup; =20 if (!cpu->model) { diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_coo= kie.c index 91694000d8..abe797759d 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -1263,7 +1263,8 @@ qemuMigrationCookieXMLParse(qemuMigrationCookiePtr mi= g, return -1; =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) return -1; =20 if (flags & QEMU_MIGRATION_COOKIE_ALLOW_REBOOT && diff --git a/tests/cputest.c b/tests/cputest.c index 383da94938..78a7f2437a 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); @@ -120,7 +120,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