95602a
From 44cbd79562ed55a8b0f2e5b5dc708265568ed9f8 Mon Sep 17 00:00:00 2001
95602a
From: Noah Meyerhans <nmeyerha@amazon.com>
95602a
Date: Fri, 30 Apr 2021 09:30:52 -0700
95602a
Subject: [PATCH] Use BIOS characteristics to distinguish EC2 bare-metal from
95602a
 VMs
95602a
95602a
DMI vendor information fields do not provide enough information for us to
95602a
distinguish between Amazon EC2 virtual machines and bare-metal instances.
95602a
SMBIOS provides a BIOS Information
95602a
table (https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.4.0.pdf
95602a
Ch. 7) that provides a field to indicate that the current machine is a virtual
95602a
machine.  On EC2 virtual machine instances, this field is set, while bare-metal
95602a
instances leave this unset, so we inspect the field via the kernel's
95602a
/sys/firemware/dmi/entries interface.
95602a
95602a
Fixes #18929
95602a
95602a
(cherry picked from commit ce35037928f4c4c931088256853f07804ec7d235)
95602a
95602a
Related: #2117948
95602a
---
95602a
 src/basic/virt.c | 65 +++++++++++++++++++++++++++++++++++++++++++++---
95602a
 1 file changed, 61 insertions(+), 4 deletions(-)
95602a
95602a
diff --git a/src/basic/virt.c b/src/basic/virt.c
95602a
index 6e4c702051..00d1c894e6 100644
95602a
--- a/src/basic/virt.c
95602a
+++ b/src/basic/virt.c
95602a
@@ -22,6 +22,12 @@
95602a
 #include "string-util.h"
95602a
 #include "virt.h"
95602a
 
95602a
+enum {
95602a
+      SMBIOS_VM_BIT_SET,
95602a
+      SMBIOS_VM_BIT_UNSET,
95602a
+      SMBIOS_VM_BIT_UNKNOWN,
95602a
+};
95602a
+
95602a
 static const char *const vm_table[_VIRTUALIZATION_MAX] = {
95602a
         [VIRTUALIZATION_XEN]       = "XenVMMXenVMM",
95602a
         [VIRTUALIZATION_KVM]       = "KVMKVMKVM",
95602a
@@ -131,9 +137,8 @@ static int detect_vm_device_tree(void) {
95602a
 #endif
95602a
 }
95602a
 
95602a
-static int detect_vm_dmi(void) {
95602a
 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
95602a
-
95602a
+static int detect_vm_dmi_vendor(void) {
95602a
         static const char *const dmi_vendors[] = {
95602a
                 "/sys/class/dmi/id/product_name", /* Test this before sys_vendor to detect KVM over QEMU */
95602a
                 "/sys/class/dmi/id/sys_vendor",
95602a
@@ -179,11 +184,63 @@ static int detect_vm_dmi(void) {
95602a
                                 return dmi_vendor_table[j].id;
95602a
                         }
95602a
         }
95602a
-#endif
95602a
+        return VIRTUALIZATION_NONE;
95602a
+}
95602a
+
95602a
+static int detect_vm_smbios(void) {
95602a
+        /* The SMBIOS BIOS Charateristics Extension Byte 2 (Section 2.1.2.2 of
95602a
+         * https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.4.0.pdf), specifies that
95602a
+         * the 4th bit being set indicates a VM. The BIOS Characteristics table is exposed via the kernel in
95602a
+         * /sys/firmware/dmi/entries/0-0. Note that in the general case, this bit being unset should not
95602a
+         * imply that the system is running on bare-metal.  For example, QEMU 3.1.0 (with or without KVM)
95602a
+         * with SeaBIOS does not set this bit. */
95602a
+        _cleanup_free_ char *s = NULL;
95602a
+        size_t readsize;
95602a
+        int r;
95602a
+
95602a
+        r = read_full_virtual_file("/sys/firmware/dmi/entries/0-0/raw", &s, &readsize);
95602a
+        if (r < 0) {
95602a
+                log_debug_errno(r, "Unable to read /sys/firmware/dmi/entries/0-0/raw, ignoring: %m");
95602a
+                return SMBIOS_VM_BIT_UNKNOWN;
95602a
+        }
95602a
+        if (readsize < 20 || s[1] < 20) {
95602a
+                /* The spec indicates that byte 1 contains the size of the table, 0x12 + the number of
95602a
+                 * extension bytes. The data we're interested in is in extension byte 2, which would be at
95602a
+                 * 0x13. If we didn't read that much data, or if the BIOS indicates that we don't have that
95602a
+                 * much data, we don't infer anything from the SMBIOS. */
95602a
+                log_debug("Only read %zu bytes from /sys/firmware/dmi/entries/0-0/raw (expected 20)", readsize);
95602a
+                return SMBIOS_VM_BIT_UNKNOWN;
95602a
+        }
95602a
 
95602a
-        log_debug("No virtualization found in DMI");
95602a
+        uint8_t byte = (uint8_t) s[19];
95602a
+        if (byte & (1U<<4)) {
95602a
+                log_debug("DMI BIOS Extension table indicates virtualization");
95602a
+                return SMBIOS_VM_BIT_SET;
95602a
+        }
95602a
+        log_debug("DMI BIOS Extension table does not indicate virtualization");
95602a
+        return SMBIOS_VM_BIT_UNSET;
95602a
+}
95602a
+#endif /* defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__) */
95602a
+
95602a
+static int detect_vm_dmi(void) {
95602a
+#if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__aarch64__)
95602a
+
95602a
+        int r;
95602a
+        r = detect_vm_dmi_vendor();
95602a
 
95602a
+        /* The DMI vendor tables in /sys/class/dmi/id don't help us distinguish between Amazon EC2
95602a
+         * virtual machines and bare-metal instances, so we need to look at SMBIOS. */
95602a
+        if (r == VIRTUALIZATION_AMAZON && detect_vm_smbios() == SMBIOS_VM_BIT_UNSET)
95602a
+                return VIRTUALIZATION_NONE;
95602a
+
95602a
+        /* If we haven't identified a VM, but the firmware indicates that there is one, indicate as much. We
95602a
+         * have no further information about what it is. */
95602a
+        if (r == VIRTUALIZATION_NONE && detect_vm_smbios() == SMBIOS_VM_BIT_SET)
95602a
+                return VIRTUALIZATION_VM_OTHER;
95602a
+        return r;
95602a
+#else
95602a
         return VIRTUALIZATION_NONE;
95602a
+#endif
95602a
 }
95602a
 
95602a
 static int detect_vm_xen(void) {