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