fbe740
From 085b955a75c49585d94fd5a19d6ac37833bb05cb Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <085b955a75c49585d94fd5a19d6ac37833bb05cb@dist-git>
fbe740
From: Boris Fiuczynski <fiuczy@linux.ibm.com>
fbe740
Date: Wed, 24 Jun 2020 13:16:20 +0200
fbe740
Subject: [PATCH] tools: Secure guest check on s390 in virt-host-validate
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Add checking in virt-host-validate for secure guest support
fbe740
on s390 for IBM Secure Execution.
fbe740
fbe740
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
fbe740
Tested-by: Viktor Mihajlovski <mihajlov@linux.ibm.com>
fbe740
Reviewed-by: Paulo de Rezende Pinatti <ppinatti@linux.ibm.com>
fbe740
Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>
fbe740
Reviewed-by: Erik Skultety <eskultet@redhat.com>
fbe740
(cherry picked from commit 0254ceab82f5e1f7b505730586d8c8337ecc5920)
fbe740
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1848997
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1850351
fbe740
fbe740
Conflicts:
fbe740
	tools/virt-host-validate-common.c
fbe740
	tools/virt-host-validate-common.h
fbe740
            - unrelated commit 4653a5194c1 was not backported
fbe740
fbe740
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
fbe740
Message-Id: <744e4ceb0760fe10ab0d82de24a78c280482d90d.1592996194.git.jdenemar@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 tools/virt-host-validate-common.c | 64 ++++++++++++++++++++++++++++++-
fbe740
 tools/virt-host-validate-common.h |  4 ++
fbe740
 tools/virt-host-validate-qemu.c   |  4 ++
fbe740
 3 files changed, 70 insertions(+), 2 deletions(-)
fbe740
fbe740
diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-common.c
fbe740
index bce0f14917..e59469a49b 100644
fbe740
--- a/tools/virt-host-validate-common.c
fbe740
+++ b/tools/virt-host-validate-common.c
fbe740
@@ -39,7 +39,8 @@ VIR_ENUM_IMPL(virHostValidateCPUFlag,
fbe740
               VIR_HOST_VALIDATE_CPU_FLAG_LAST,
fbe740
               "vmx",
fbe740
               "svm",
fbe740
-              "sie");
fbe740
+              "sie",
fbe740
+              "158");
fbe740
 
fbe740
 static bool quiet;
fbe740
 
fbe740
@@ -209,7 +210,8 @@ virBitmapPtr virHostValidateGetCPUFlags(void)
fbe740
          * on the architecture, so check possible prefixes */
fbe740
         if (!STRPREFIX(line, "flags") &&
fbe740
             !STRPREFIX(line, "Features") &&
fbe740
-            !STRPREFIX(line, "features"))
fbe740
+            !STRPREFIX(line, "features") &&
fbe740
+            !STRPREFIX(line, "facilities"))
fbe740
             continue;
fbe740
 
fbe740
         /* fgets() includes the trailing newline in the output buffer,
fbe740
@@ -411,3 +413,61 @@ int virHostValidateIOMMU(const char *hvname,
fbe740
     virHostMsgPass();
fbe740
     return 0;
fbe740
 }
fbe740
+
fbe740
+
fbe740
+int virHostValidateSecureGuests(const char *hvname,
fbe740
+                                virHostValidateLevel level)
fbe740
+{
fbe740
+    virBitmapPtr flags;
fbe740
+    bool hasFac158 = false;
fbe740
+    virArch arch = virArchFromHost();
fbe740
+    g_autofree char *cmdline = NULL;
fbe740
+    static const char *kIBMValues[] = {"y", "Y", "on", "ON", "oN", "On", "1"};
fbe740
+
fbe740
+    flags = virHostValidateGetCPUFlags();
fbe740
+
fbe740
+    if (flags && virBitmapIsBitSet(flags, VIR_HOST_VALIDATE_CPU_FLAG_FACILITY_158))
fbe740
+        hasFac158 = true;
fbe740
+
fbe740
+    virBitmapFree(flags);
fbe740
+
fbe740
+    virHostMsgCheck(hvname, "%s", _("for secure guest support"));
fbe740
+    if (ARCH_IS_S390(arch)) {
fbe740
+        if (hasFac158) {
fbe740
+            if (!virFileIsDir("/sys/firmware/uv")) {
fbe740
+                virHostMsgFail(level, "IBM Secure Execution not supported by "
fbe740
+                                      "the currently used kernel");
fbe740
+                return 0;
fbe740
+            }
fbe740
+
fbe740
+            if (virFileReadValueString(&cmdline, "/proc/cmdline") < 0)
fbe740
+                return -1;
fbe740
+
fbe740
+            /* we're prefix matching rather than equality matching here, because
fbe740
+             * kernel would treat even something like prot_virt='yFOO' as
fbe740
+             * enabled
fbe740
+             */
fbe740
+            if (virKernelCmdlineMatchParam(cmdline, "prot_virt", kIBMValues,
fbe740
+                                           G_N_ELEMENTS(kIBMValues),
fbe740
+                                           VIR_KERNEL_CMDLINE_FLAGS_SEARCH_FIRST |
fbe740
+                                           VIR_KERNEL_CMDLINE_FLAGS_CMP_PREFIX)) {
fbe740
+                virHostMsgPass();
fbe740
+                return 1;
fbe740
+            } else {
fbe740
+                virHostMsgFail(level,
fbe740
+                               "IBM Secure Execution appears to be disabled "
fbe740
+                               "in kernel. Add prot_virt=1 to kernel cmdline "
fbe740
+                               "arguments");
fbe740
+            }
fbe740
+        } else {
fbe740
+            virHostMsgFail(level, "Hardware or firmware does not provide "
fbe740
+                                  "support for IBM Secure Execution");
fbe740
+        }
fbe740
+    } else {
fbe740
+        virHostMsgFail(level,
fbe740
+                       "Unknown if this platform has Secure Guest support");
fbe740
+        return -1;
fbe740
+    }
fbe740
+
fbe740
+    return 0;
fbe740
+}
fbe740
diff --git a/tools/virt-host-validate-common.h b/tools/virt-host-validate-common.h
fbe740
index 1b7e93e520..1653c9c95b 100644
fbe740
--- a/tools/virt-host-validate-common.h
fbe740
+++ b/tools/virt-host-validate-common.h
fbe740
@@ -38,6 +38,7 @@ typedef enum {
fbe740
     VIR_HOST_VALIDATE_CPU_FLAG_VMX = 0,
fbe740
     VIR_HOST_VALIDATE_CPU_FLAG_SVM,
fbe740
     VIR_HOST_VALIDATE_CPU_FLAG_SIE,
fbe740
+    VIR_HOST_VALIDATE_CPU_FLAG_FACILITY_158,
fbe740
 
fbe740
     VIR_HOST_VALIDATE_CPU_FLAG_LAST,
fbe740
 } virHostValidateCPUFlag;
fbe740
@@ -83,3 +84,6 @@ int virHostValidateCGroupControllers(const char *hvname,
fbe740
 
fbe740
 int virHostValidateIOMMU(const char *hvname,
fbe740
                          virHostValidateLevel level);
fbe740
+
fbe740
+int virHostValidateSecureGuests(const char *hvname,
fbe740
+                                virHostValidateLevel level);
fbe740
diff --git a/tools/virt-host-validate-qemu.c b/tools/virt-host-validate-qemu.c
fbe740
index ff3c1f0231..b38b0fbd6a 100644
fbe740
--- a/tools/virt-host-validate-qemu.c
fbe740
+++ b/tools/virt-host-validate-qemu.c
fbe740
@@ -112,5 +112,9 @@ int virHostValidateQEMU(void)
fbe740
                              VIR_HOST_VALIDATE_WARN) < 0)
fbe740
         ret = -1;
fbe740
 
fbe740
+    if (virHostValidateSecureGuests("QEMU",
fbe740
+                                    VIR_HOST_VALIDATE_WARN) < 0)
fbe740
+        ret = -1;
fbe740
+
fbe740
     return ret;
fbe740
 }
fbe740
-- 
fbe740
2.27.0
fbe740