404507
From ac8540f237280b2e302949f15d229e13477245c7 Mon Sep 17 00:00:00 2001
404507
Message-Id: <ac8540f237280b2e302949f15d229e13477245c7@dist-git>
4cd28d
From: Paolo Bonzini <pbonzini@redhat.com>
4cd28d
Date: Tue, 12 Dec 2017 16:23:41 +0100
4cd28d
Subject: [PATCH] util: introduce virHostCPUGetMicrocodeVersion
4cd28d
4cd28d
This new API reads host's CPU microcode version from /proc/cpuinfo.
4cd28d
4cd28d
Unfortunately, there is no other way of reading microcode version which
4cd28d
would be usable from both system and session daemon.
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
---
4cd28d
 src/libvirt_private.syms |  1 +
4cd28d
 src/util/virhostcpu.c    | 43 +++++++++++++++++++++++++++++++++++++++++++
4cd28d
 src/util/virhostcpu.h    |  2 ++
4cd28d
 3 files changed, 46 insertions(+)
4cd28d
4cd28d
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
404507
index 85b15314a9..57999e77b0 100644
4cd28d
--- a/src/libvirt_private.syms
4cd28d
+++ b/src/libvirt_private.syms
404507
@@ -1861,6 +1861,7 @@ virHostCPUGetCount;
4cd28d
 virHostCPUGetInfo;
4cd28d
 virHostCPUGetKVMMaxVCPUs;
4cd28d
 virHostCPUGetMap;
4cd28d
+virHostCPUGetMicrocodeVersion;
4cd28d
 virHostCPUGetOnline;
4cd28d
 virHostCPUGetOnlineBitmap;
4cd28d
 virHostCPUGetPresentBitmap;
4cd28d
diff --git a/src/util/virhostcpu.c b/src/util/virhostcpu.c
404507
index bd58aa43df..a7ae7cd4cb 100644
4cd28d
--- a/src/util/virhostcpu.c
4cd28d
+++ b/src/util/virhostcpu.c
404507
@@ -1211,3 +1211,46 @@ virHostCPUGetKVMMaxVCPUs(void)
4cd28d
     return -1;
4cd28d
 }
4cd28d
 #endif /* HAVE_LINUX_KVM_H */
4cd28d
+
4cd28d
+
4cd28d
+#ifdef __linux__
4cd28d
+
4cd28d
+unsigned int
4cd28d
+virHostCPUGetMicrocodeVersion(void)
4cd28d
+{
4cd28d
+    char *outbuf = NULL;
4cd28d
+    char *cur;
4cd28d
+    unsigned int version = 0;
4cd28d
+
4cd28d
+    if (virFileReadHeaderQuiet(CPUINFO_PATH, 4096, &outbuf) < 0) {
4cd28d
+        char ebuf[1024];
4cd28d
+        VIR_DEBUG("Failed to read microcode version from %s: %s",
4cd28d
+                  CPUINFO_PATH, virStrerror(errno, ebuf, sizeof(ebuf)));
4cd28d
+        return 0;
4cd28d
+    }
4cd28d
+
4cd28d
+    /* Account for format 'microcode    : XXXX'*/
4cd28d
+    if (!(cur = strstr(outbuf, "microcode")) ||
4cd28d
+        !(cur = strchr(cur, ':')))
4cd28d
+        goto cleanup;
4cd28d
+    cur++;
4cd28d
+
4cd28d
+    /* Linux places the microcode revision in a 32-bit integer, so
4cd28d
+     * ui is fine for us too.  */
4cd28d
+    if (virStrToLong_ui(cur, &cur, 0, &version) < 0)
4cd28d
+        goto cleanup;
4cd28d
+
4cd28d
+ cleanup:
4cd28d
+    VIR_FREE(outbuf);
4cd28d
+    return version;
4cd28d
+}
4cd28d
+
4cd28d
+#else
4cd28d
+
4cd28d
+unsigned int
4cd28d
+virHostCPUGetMicrocodeVersion(void)
4cd28d
+{
4cd28d
+    return 0;
4cd28d
+}
4cd28d
+
4cd28d
+#endif
4cd28d
diff --git a/src/util/virhostcpu.h b/src/util/virhostcpu.h
404507
index 67033de842..f9f3359288 100644
4cd28d
--- a/src/util/virhostcpu.h
4cd28d
+++ b/src/util/virhostcpu.h
4cd28d
@@ -66,4 +66,6 @@ virBitmapPtr virHostCPUGetSiblingsList(unsigned int cpu);
4cd28d
 
4cd28d
 int virHostCPUGetOnline(unsigned int cpu, bool *online);
4cd28d
 
4cd28d
+unsigned int virHostCPUGetMicrocodeVersion(void);
4cd28d
+
4cd28d
 #endif /* __VIR_HOSTCPU_H__*/
4cd28d
-- 
4cd28d
2.15.1
4cd28d