982648
From 05b6b6d0d034e0a2ecb9b826bf5052a3bada218d Mon Sep 17 00:00:00 2001
982648
Message-Id: <05b6b6d0d034e0a2ecb9b826bf5052a3bada218d@dist-git>
982648
From: Erik Skultety <eskultet@redhat.com>
982648
Date: Mon, 20 Aug 2018 17:18:53 +0200
982648
Subject: [PATCH] qemu: caps: Format SEV platform data into qemuCaps cache
982648
MIME-Version: 1.0
982648
Content-Type: text/plain; charset=UTF-8
982648
Content-Transfer-Encoding: 8bit
982648
982648
Since we're not saving the platform-specific data into a cache, we're
982648
not going to populate the structure, which in turn will cause a crash
982648
upon calling virNodeGetSEVInfo because of a NULL pointer dereference.
982648
Ultimately, we should start caching this data along with host-specific
982648
capabilities like NUMA and SELinux stuff into a separate cache, but for
982648
the time being, this is a semi-proper fix for a potential crash.
982648
982648
Backtrace (requires libvirtd restart to load qemu caps from cache):
982648
    #0 qemuGetSEVInfoToParams
982648
    #1 qemuNodeGetSEVInfo
982648
    #2 virNodeGetSEVInfo
982648
    #3 remoteDispatchNodeGetSevInfo
982648
    #4 remoteDispatchNodeGetSevInfoHelper
982648
    #5 virNetServerProgramDispatchCall
982648
    #6 virNetServerProgramDispatch
982648
    #7 virNetServerProcessMsg
982648
    #8 virNetServerHandleJob
982648
    #9 virThreadPoolWorker
982648
    #10 virThreadHelper
982648
982648
https: //bugzilla.redhat.com/show_bug.cgi?id=1612009
982648
Signed-off-by: Erik Skultety <eskultet@redhat.com>
982648
Acked-by: Peter Krempa <pkrempa@redhat.com>
982648
Tested-by: Brijesh Singh <brijesh.singh@amd.com>
982648
(cherry picked from commit 77f51ab52049734d80a8ccb79b80189c7fb95c41)
982648
982648
https://bugzilla.redhat.com/show_bug.cgi?id=1612009
982648
https://bugzilla.redhat.com/show_bug.cgi?id=1619150
982648
982648
Amend:
982648
    - fixed the VIR_AUTOPTR bits which downstream doesn't support
982648
    and wouldn't compile
982648
982648
Signed-off-by: Erik Skultety <eskultet@redhat.com>
982648
Reviewed-by: Ján Tomko <jtomko@redhat.com>
982648
---
982648
 src/qemu/qemu_capabilities.c                  | 109 ++++++++++++++++++
982648
 .../qemu_2.12.0.x86_64.xml                    |   5 +-
982648
 .../caps_2.12.0.x86_64.xml                    |   6 +
982648
 3 files changed, 119 insertions(+), 1 deletion(-)
982648
982648
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
982648
index 470f25a5a6..4f2051a2bb 100644
982648
--- a/src/qemu/qemu_capabilities.c
982648
+++ b/src/qemu/qemu_capabilities.c
982648
@@ -1573,6 +1573,30 @@ virQEMUCapsHostCPUDataClear(virQEMUCapsHostCPUDataPtr cpuData)
982648
 }
982648
 
982648
 
982648
+static int
982648
+virQEMUCapsSEVInfoCopy(virSEVCapabilityPtr *dst,
982648
+                       virSEVCapabilityPtr src)
982648
+{
982648
+    int ret = -1;
982648
+    virSEVCapabilityPtr tmp = NULL;
982648
+
982648
+    if (VIR_ALLOC(tmp) < 0 ||
982648
+        VIR_STRDUP(tmp->pdh, src->pdh) < 0 ||
982648
+        VIR_STRDUP(tmp->cert_chain, src->cert_chain) < 0)
982648
+        goto cleanup;
982648
+
982648
+    tmp->cbitpos = src->cbitpos;
982648
+    tmp->reduced_phys_bits = src->reduced_phys_bits;
982648
+
982648
+    VIR_STEAL_PTR(*dst, tmp);
982648
+
982648
+    ret = 0;
982648
+ cleanup:
982648
+    virSEVCapabilitiesFree(tmp);
982648
+    return ret;
982648
+}
982648
+
982648
+
982648
 virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps)
982648
 {
982648
     virQEMUCapsPtr ret = virQEMUCapsNew();
982648
@@ -1635,6 +1659,11 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEMUCapsPtr qemuCaps)
982648
     for (i = 0; i < qemuCaps->ngicCapabilities; i++)
982648
         ret->gicCapabilities[i] = qemuCaps->gicCapabilities[i];
982648
 
982648
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_SEV_GUEST) &&
982648
+        virQEMUCapsSEVInfoCopy(&ret->sevCapabilities,
982648
+                               qemuCaps->sevCapabilities) < 0)
982648
+        goto error;
982648
+
982648
     return ret;
982648
 
982648
  error:
982648
@@ -3273,6 +3302,62 @@ virQEMUCapsCachePrivFree(void *privData)
982648
 }
982648
 
982648
 
982648
+static int
982648
+virQEMUCapsParseSEVInfo(virQEMUCapsPtr qemuCaps, xmlXPathContextPtr ctxt)
982648
+{
982648
+    int ret = -1;
982648
+    virSEVCapabilityPtr sev = NULL;
982648
+
982648
+    if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_SEV_GUEST))
982648
+        return 0;
982648
+
982648
+    if (virXPathBoolean("boolean(./sev)", ctxt) == 0) {
982648
+        virReportError(VIR_ERR_XML_ERROR, "%s",
982648
+                       _("missing SEV platform data in QEMU "
982648
+                         "capabilities cache"));
982648
+        return -1;
982648
+    }
982648
+
982648
+    if (VIR_ALLOC(sev) < 0)
982648
+        return -1;
982648
+
982648
+    if (virXPathUInt("string(./sev/cbitpos)", ctxt, &sev->cbitpos) < 0) {
982648
+        virReportError(VIR_ERR_XML_ERROR, "%s",
982648
+                       _("missing or malformed SEV cbitpos information "
982648
+                         "in QEMU capabilities cache"));
982648
+        goto cleanup;
982648
+    }
982648
+
982648
+    if (virXPathUInt("string(./sev/reducedPhysBits)", ctxt,
982648
+                     &sev->reduced_phys_bits) < 0) {
982648
+        virReportError(VIR_ERR_XML_ERROR, "%s",
982648
+                       _("missing or malformed SEV reducedPhysBits information "
982648
+                         "in QEMU capabilities cache"));
982648
+        goto cleanup;
982648
+    }
982648
+
982648
+    if (!(sev->pdh = virXPathString("string(./sev/pdh)", ctxt)))  {
982648
+        virReportError(VIR_ERR_XML_ERROR, "%s",
982648
+                       _("missing SEV pdh information "
982648
+                         "in QEMU capabilities cache"));
982648
+        goto cleanup;
982648
+    }
982648
+
982648
+    if (!(sev->cert_chain = virXPathString("string(./sev/certChain)", ctxt))) {
982648
+        virReportError(VIR_ERR_XML_ERROR, "%s",
982648
+                       _("missing SEV certChain information "
982648
+                         "in QEMU capabilities cache"));
982648
+        goto cleanup;
982648
+    }
982648
+
982648
+    VIR_STEAL_PTR(qemuCaps->sevCapabilities, sev);
982648
+    ret = 0;
982648
+ cleanup:
982648
+    virSEVCapabilitiesFree(sev);
982648
+    return ret;
982648
+}
982648
+
982648
+
982648
 /*
982648
  * Parsing a doc that looks like
982648
  *
982648
@@ -3521,6 +3606,9 @@ virQEMUCapsLoadCache(virArch hostArch,
982648
     }
982648
     VIR_FREE(nodes);
982648
 
982648
+    if (virQEMUCapsParseSEVInfo(qemuCaps, ctxt) < 0)
982648
+        goto cleanup;
982648
+
982648
     virQEMUCapsInitHostCPUModel(qemuCaps, hostArch, VIR_DOMAIN_VIRT_KVM);
982648
     virQEMUCapsInitHostCPUModel(qemuCaps, hostArch, VIR_DOMAIN_VIRT_QEMU);
982648
 
982648
@@ -3638,6 +3726,24 @@ virQEMUCapsFormatCPUModels(virQEMUCapsPtr qemuCaps,
982648
 }
982648
 
982648
 
982648
+static void
982648
+virQEMUCapsFormatSEVInfo(virQEMUCapsPtr qemuCaps, virBufferPtr buf)
982648
+{
982648
+    virSEVCapabilityPtr sev = virQEMUCapsGetSEVCapabilities(qemuCaps);
982648
+
982648
+    virBufferAddLit(buf, "<sev>\n");
982648
+    virBufferAdjustIndent(buf, 2);
982648
+    virBufferAsprintf(buf, "<cbitpos>%u</cbitpos>\n", sev->cbitpos);
982648
+    virBufferAsprintf(buf, "<reducedPhysBits>%u</reducedPhysBits>\n",
982648
+                      sev->reduced_phys_bits);
982648
+    virBufferEscapeString(buf, "<pdh>%s</pdh>\n", sev->pdh);
982648
+    virBufferEscapeString(buf, "<certChain>%s</certChain>\n",
982648
+                          sev->cert_chain);
982648
+    virBufferAdjustIndent(buf, -2);
982648
+    virBufferAddLit(buf, "</sev>\n");
982648
+}
982648
+
982648
+
982648
 char *
982648
 virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps)
982648
 {
982648
@@ -3719,6 +3825,9 @@ virQEMUCapsFormatCache(virQEMUCapsPtr qemuCaps)
982648
                           emulated ? "yes" : "no");
982648
     }
982648
 
982648
+    if (qemuCaps->sevCapabilities)
982648
+        virQEMUCapsFormatSEVInfo(qemuCaps, &buf;;
982648
+
982648
     virBufferAdjustIndent(&buf, -2);
982648
     virBufferAddLit(&buf, "</qemuCaps>\n");
982648
 
982648
diff --git a/tests/domaincapsschemadata/qemu_2.12.0.x86_64.xml b/tests/domaincapsschemadata/qemu_2.12.0.x86_64.xml
982648
index 7a1be4c093..a8d6a4d629 100644
982648
--- a/tests/domaincapsschemadata/qemu_2.12.0.x86_64.xml
982648
+++ b/tests/domaincapsschemadata/qemu_2.12.0.x86_64.xml
982648
@@ -142,6 +142,9 @@
982648
     <gic supported='no'/>
982648
     <vmcoreinfo supported='yes'/>
982648
     <genid supported='yes'/>
982648
-    <sev supported='no'/>
982648
+    <sev supported='yes'>
982648
+      <cbitpos>47</cbitpos>
982648
+      <reducedPhysBits>1</reducedPhysBits>
982648
+    </sev>
982648
   </features>
982648
 </domainCapabilities>
982648
diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml b/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml
982648
index 78889facce..f0dc082640 100644
982648
--- a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml
982648
+++ b/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml
982648
@@ -1254,4 +1254,10 @@
982648
   <machine name='pc-0.11' hotplugCpus='yes' maxCpus='255'/>
982648
   <machine name='pc-0.12' hotplugCpus='yes' maxCpus='255'/>
982648
   <machine name='pc-0.10' hotplugCpus='yes' maxCpus='255'/>
982648
+  <sev>
982648
+    <cbitpos>47</cbitpos>
982648
+    <reducedPhysBits>1</reducedPhysBits>
982648
+    <pdh>AQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAA</pdh>
982648
+    <certChain>AQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAAAQAAAAAOAAA</certChain>
982648
+  </sev>
982648
 </qemuCaps>
982648
-- 
982648
2.18.0
982648