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