From 880bf81b173e53c53427a7b22ae577ecc0d48038 Mon Sep 17 00:00:00 2001
Message-Id: <880bf81b173e53c53427a7b22ae577ecc0d48038@dist-git>
From: Paolo Bonzini <pbonzini@redhat.com>
Date: Tue, 12 Dec 2017 16:23:41 +0100
Subject: [PATCH] qemu: capabilities: force update if the microcode version
 does not match

A microcode update can cause the CPUID bits to change; an example
from the past was the update that disabled TSX on several Haswell
and Broadwell machines.

Therefore, place microcode version in the virQEMUCaps struct and
XML, and rebuild the cache if the versions do not match.

CVE-2017-5715

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>

Conflicts:
	src/qemu/qemu_capabilities.c
	src/qemu/qemu_capabilities.h
            - QEMU capabilities cache was refactored since 7.3

	src/qemu/qemu_capspriv.h
            - context; more functions were added since 7.3

	tests/qemucapabilitiesdata/caps_2.7.0.x86_64.xml
            - context; different package version in 7.3

	tests/qemucapabilitiesdata/caps_2.10.0-gicv2.aarch64.xml
	tests/qemucapabilitiesdata/caps_2.10.0-gicv3.aarch64.xml
	tests/qemucapabilitiesdata/caps_2.10.0.ppc64.xml
	tests/qemucapabilitiesdata/caps_2.10.0.s390x.xml
	tests/qemucapabilitiesdata/caps_2.10.0.x86_64.xml
	tests/qemucapabilitiesdata/caps_2.7.0.s390x.xml
	tests/qemucapabilitiesdata/caps_2.8.0.s390x.xml
	tests/qemucapabilitiesdata/caps_2.8.0.x86_64.xml
	tests/qemucapabilitiesdata/caps_2.9.0.x86_64.xml
	tests/qemucapabilitiesdata/caps_2.9.0.ppc64.xml
	tests/qemucapabilitiesdata/caps_2.9.0.s390x.xml
            - missing in 7.3

	tests/qemucapabilitiestest.c
            - no explicit TCG caps probing for KVM binaries in 7.3 yet

	tests/qemucapsprobe.c
            - the caps parameter for virQEMUCapsNewForBinaryInternal
              added in 7.4 is missing in 7.3 (which matches 7.5 in this
              respect)

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/qemu/qemu_capabilities.c                       | 57 ++++++++++++++++++++--
 src/qemu/qemu_capspriv.h                           |  5 ++
 tests/qemucapabilitiesdata/caps_1.2.2.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_1.3.1.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_1.4.2.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_1.5.3.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_1.6.0.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_1.7.0.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_2.1.1.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_2.4.0.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_2.5.0.x86_64.xml   |  1 +
 .../caps_2.6.0-gicv2.aarch64.xml                   |  1 +
 .../caps_2.6.0-gicv3.aarch64.xml                   |  1 +
 tests/qemucapabilitiesdata/caps_2.6.0.ppc64le.xml  |  1 +
 tests/qemucapabilitiesdata/caps_2.6.0.x86_64.xml   |  1 +
 tests/qemucapabilitiesdata/caps_2.7.0.x86_64.xml   |  1 +
 tests/qemucapabilitiestest.c                       |  8 +++
 tests/qemucapsprobe.c                              |  2 +-
 18 files changed, 80 insertions(+), 6 deletions(-)

Index: libvirt-1.0.5.9/src/qemu/qemu_capabilities.c
===================================================================
--- libvirt-1.0.5.9.orig/src/qemu/qemu_capabilities.c
+++ libvirt-1.0.5.9/src/qemu/qemu_capabilities.c
@@ -245,6 +245,7 @@ struct _virQEMUCaps {
 
     unsigned int version;
     unsigned int kvmVersion;
+    unsigned int microcodeVersion;
 
     virArch arch;
 
@@ -263,6 +264,7 @@ struct _virQEMUCapsCache {
     char *runDir;
     uid_t runUid;
     gid_t runGid;
+    unsigned int microcodeVersion;
 };
 
 
@@ -1743,6 +1745,7 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEM
     ret->usedQMP = qemuCaps->usedQMP;
     ret->version = qemuCaps->version;
     ret->kvmVersion = qemuCaps->kvmVersion;
+    ret->microcodeVersion = qemuCaps->microcodeVersion;
     ret->arch = qemuCaps->arch;
 
     if (VIR_ALLOC_N(ret->cpuDefinitions, qemuCaps->ncpuDefinitions) < 0)
@@ -2649,7 +2652,8 @@ cleanup:
 virQEMUCapsPtr virQEMUCapsNewForBinary(const char *binary,
                                        const char *libDir,
                                        uid_t runUid,
-                                       gid_t runGid)
+                                       gid_t runGid,
+                                       unsigned int microcodeVersion)
 {
     virQEMUCapsPtr qemuCaps = virQEMUCapsNew();
     struct stat sb;
@@ -2684,6 +2688,10 @@ virQEMUCapsPtr virQEMUCapsNewForBinary(c
         virQEMUCapsInitHelp(qemuCaps, runUid, runGid) < 0)
         goto error;
 
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM) ||
+        virQEMUCapsGet(qemuCaps, QEMU_CAPS_ENABLE_KVM))
+        qemuCaps->microcodeVersion = microcodeVersion;
+
     return qemuCaps;
 
 no_memory:
@@ -2723,6 +2731,9 @@ virQEMUCapsCacheNew(const char *libDir,
 {
     virQEMUCapsCachePtr cache;
 
+    if (virQEMUCapsInitialize() < 0)
+        return NULL;
+
     if (VIR_ALLOC(cache) < 0) {
         virReportOOMError();
         return NULL;
@@ -2744,6 +2755,7 @@ virQEMUCapsCacheNew(const char *libDir,
 
     cache->runUid = runUid;
     cache->runGid = runGid;
+    cache->microcodeVersion = cpuMicrocodeVersion;
 
     return cache;
 
@@ -2770,7 +2782,8 @@ virQEMUCapsCacheLookup(virQEMUCapsCacheP
         VIR_DEBUG("Creating capabilities for %s",
                   binary);
         ret = virQEMUCapsNewForBinary(binary, cache->libDir,
-                                      cache->runUid, cache->runGid);
+                                      cache->runUid, cache->runGid,
+                                      cache->microcodeVersion);
         if (ret) {
             VIR_DEBUG("Caching capabilities %p for %s",
                       ret, binary);
@@ -2819,3 +2832,11 @@ virQEMUCapsUsedQMP(virQEMUCapsPtr qemuCa
 {
     return qemuCaps->usedQMP;
 }
+
+
+void
+virQEMUCapsSetMicrocodeVersion(virQEMUCapsPtr qemuCaps,
+                               unsigned int microcodeVersion)
+{
+    qemuCaps->microcodeVersion = microcodeVersion;
+}
Index: libvirt-1.0.5.9/src/qemu/qemu_capabilities.h
===================================================================
--- libvirt-1.0.5.9.orig/src/qemu/qemu_capabilities.h
+++ libvirt-1.0.5.9/src/qemu/qemu_capabilities.h
@@ -202,7 +202,8 @@ virQEMUCapsPtr virQEMUCapsNewCopy(virQEM
 virQEMUCapsPtr virQEMUCapsNewForBinary(const char *binary,
                                        const char *libDir,
                                        uid_t runUid,
-                                       gid_t runGid);
+                                       gid_t runGid,
+                                       unsigned int microcodeVersion);
 
 int virQEMUCapsProbeQMP(virQEMUCapsPtr qemuCaps,
                         qemuMonitorPtr mon);
