404507
From 4f1684dd855d48517e04507edadba2fd454e4ada Mon Sep 17 00:00:00 2001
404507
Message-Id: <4f1684dd855d48517e04507edadba2fd454e4ada@dist-git>
4cd28d
From: Paolo Bonzini <pbonzini@redhat.com>
4cd28d
Date: Tue, 12 Dec 2017 16:23:40 +0100
4cd28d
Subject: [PATCH] conf: include x86 microcode version in virsh capabiltiies
4cd28d
4cd28d
A microcode update can cause the CPUID bits to change; an example
4cd28d
from the past was the update that disabled TSX on several Haswell and
4cd28d
Broadwell machines.
4cd28d
4cd28d
In order to track the x86 microcode version in the QEMU capabilities,
4cd28d
we have to fetch it and store it in the host CPU.  This also makes the
4cd28d
version visible in "virsh capabilities", which is a nice side effect.
4cd28d
4cd28d
CVE-2017-5715
4cd28d
4cd28d
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
4cd28d
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
4cd28d
---
404507
 src/conf/cpu_conf.c | 14 ++++++++++++++
404507
 src/conf/cpu_conf.h |  1 +
404507
 src/cpu/cpu_x86.c   |  9 +++++++++
404507
 3 files changed, 24 insertions(+)
4cd28d
4cd28d
diff --git a/src/conf/cpu_conf.c b/src/conf/cpu_conf.c
404507
index 669935acf8..7e00299c66 100644
4cd28d
--- a/src/conf/cpu_conf.c
4cd28d
+++ b/src/conf/cpu_conf.c
404507
@@ -130,6 +130,7 @@ virCPUDefCopyModelFilter(virCPUDefPtr dst,
4cd28d
         VIR_STRDUP(dst->vendor_id, src->vendor_id) < 0 ||
4cd28d
         VIR_ALLOC_N(dst->features, src->nfeatures) < 0)
4cd28d
         return -1;
4cd28d
+    dst->microcodeVersion = src->microcodeVersion;
4cd28d
     dst->nfeatures_max = src->nfeatures;
4cd28d
     dst->nfeatures = 0;
4cd28d
 
404507
@@ -181,6 +182,7 @@ virCPUDefStealModel(virCPUDefPtr dst,
4cd28d
 
4cd28d
     VIR_STEAL_PTR(dst->model, src->model);
4cd28d
     VIR_STEAL_PTR(dst->features, src->features);
4cd28d
+    dst->microcodeVersion = src->microcodeVersion;
4cd28d
     dst->nfeatures_max = src->nfeatures_max;
4cd28d
     src->nfeatures_max = 0;
4cd28d
     dst->nfeatures = src->nfeatures;
404507
@@ -382,6 +384,14 @@ virCPUDefParseXML(xmlXPathContextPtr ctxt,
4cd28d
             goto cleanup;
4cd28d
         }
4cd28d
         VIR_FREE(arch);
4cd28d
+
4cd28d
+        if (virXPathBoolean("boolean(./microcode[1]/@version)", ctxt) > 0 &&
4cd28d
+            virXPathUInt("string(./microcode[1]/@version)", ctxt,
4cd28d
+                         &def->microcodeVersion) < 0) {
4cd28d
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
4cd28d
+                           _("invalid microcode version"));
4cd28d
+            goto cleanup;
4cd28d
+        }
4cd28d
     }
4cd28d
 
4cd28d
     if (!(def->model = virXPathString("string(./model[1])", ctxt)) &&
4cd28d
@@ -720,6 +730,10 @@ virCPUDefFormatBuf(virBufferPtr buf,
4cd28d
     if (formatModel && def->vendor)
4cd28d
         virBufferEscapeString(buf, "<vendor>%s</vendor>\n", def->vendor);
4cd28d
 
4cd28d
+    if (def->type == VIR_CPU_TYPE_HOST && def->microcodeVersion)
4cd28d
+        virBufferAsprintf(buf, "<microcode version='%u'/>\n",
4cd28d
+                          def->microcodeVersion);
4cd28d
+
4cd28d
     if (def->sockets && def->cores && def->threads) {
4cd28d
         virBufferAddLit(buf, "
4cd28d
         virBufferAsprintf(buf, " sockets='%u'", def->sockets);
4cd28d
diff --git a/src/conf/cpu_conf.h b/src/conf/cpu_conf.h
404507
index d1983f5d4f..9f2e7ee264 100644
4cd28d
--- a/src/conf/cpu_conf.h
4cd28d
+++ b/src/conf/cpu_conf.h
4cd28d
@@ -133,6 +133,7 @@ struct _virCPUDef {
4cd28d
     char *vendor_id;    /* vendor id returned by CPUID in the guest */
4cd28d
     int fallback;       /* enum virCPUFallback */
4cd28d
     char *vendor;
4cd28d
+    unsigned int microcodeVersion;
4cd28d
     unsigned int sockets;
4cd28d
     unsigned int cores;
4cd28d
     unsigned int threads;
4cd28d
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
404507
index 693e571a3d..ad35532fc1 100644
4cd28d
--- a/src/cpu/cpu_x86.c
4cd28d
+++ b/src/cpu/cpu_x86.c
4cd28d
@@ -33,6 +33,7 @@
4cd28d
 #include "virbuffer.h"
4cd28d
 #include "virendian.h"
4cd28d
 #include "virstring.h"
4cd28d
+#include "virhostcpu.h"
4cd28d
 
4cd28d
 #define VIR_FROM_THIS VIR_FROM_CPU
4cd28d
 
4cd28d
@@ -153,6 +154,8 @@ struct _virCPUx86Map {
4cd28d
 };
4cd28d
 
4cd28d
 static virCPUx86MapPtr cpuMap;
4cd28d
+static unsigned int microcodeVersion;
4cd28d
+
4cd28d
 int virCPUx86DriverOnceInit(void);
4cd28d
 VIR_ONCE_GLOBAL_INIT(virCPUx86Driver);
4cd28d
 
404507
@@ -1409,6 +1412,8 @@ virCPUx86DriverOnceInit(void)
4cd28d
     if (!(cpuMap = virCPUx86LoadMap()))
4cd28d
         return -1;
4cd28d
 
4cd28d
+    microcodeVersion = virHostCPUGetMicrocodeVersion();
4cd28d
+
4cd28d
     return 0;
4cd28d
 }
4cd28d
 
404507
@@ -2424,6 +2429,9 @@ virCPUx86GetHost(virCPUDefPtr cpu,
4cd28d
     virCPUDataPtr cpuData = NULL;
4cd28d
     int ret = -1;
4cd28d
 
4cd28d
+    if (virCPUx86DriverInitialize() < 0)
4cd28d
+        goto cleanup;
4cd28d
+
4cd28d
     if (!(cpuData = virCPUDataNew(archs[0])))
4cd28d
         goto cleanup;
4cd28d
 
404507
@@ -2432,6 +2440,7 @@ virCPUx86GetHost(virCPUDefPtr cpu,
4cd28d
         goto cleanup;
4cd28d
 
404507
     ret = x86DecodeCPUData(cpu, cpuData, models);
4cd28d
+    cpu->microcodeVersion = microcodeVersion;
4cd28d
 
4cd28d
  cleanup:
4cd28d
     virCPUx86DataFree(cpuData);
4cd28d
-- 
4cd28d
2.15.1
4cd28d