Blame SOURCES/0574-Enable-TDX-measurement-to-RTMR-register.patch

530103
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
530103
From: Lu Ken <ken.lu@intel.com>
530103
Date: Sat, 3 Jul 2021 10:50:37 -0400
530103
Subject: [PATCH] Enable TDX measurement to RTMR register
530103
530103
Intel Trust Domain Extensions(Intel TDX) refers to an Intel technology
530103
that extends Virtual Machine Extensions(VMX) and Multi-Key Total Memory
530103
Encryption(MK-TME) with a new kind of virtual machine guest called a
530103
Trust Domain(TD)[1]. A TD runs in a CPU mode that protects the confidentiality
530103
of its memory contents and its CPU state from any other software, including
530103
the hosting Virtual Machine Monitor (VMM).
530103
530103
Trust Domain Virtual Firmware (TDVF) is required to provide TD services to
530103
the TD guest OS.[2] Its reference code is available at https://github.com/tianocore/edk2-staging/tree/TDVF.
530103
530103
To support TD measurement/attestation, TDs provide 4 RTMR registers like
530103
TPM/TPM2 PCR as below:
530103
- RTMR[0] is for TDVF configuration
530103
- RTMR[1] is for the TD OS loader and kernel
530103
- RTMR[2] is for the OS application
530103
- RTMR[3] is reserved for special usage only
530103
530103
This patch adds TD Measurement protocol support along with TPM/TPM2 protocol.
530103
530103
References:
530103
[1] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-whitepaper-v4.pdf
530103
[2] https://software.intel.com/content/dam/develop/external/us/en/documents/tdx-virtual-firmware-design-guide-rev-1.pdf
530103
530103
Signed-off-by: Lu Ken <ken.lu@intel.com>
530103
(cherry picked from commit 841a0977397cf12a5498d439b8aaf8bf28ff8544)
530103
---
530103
 grub-core/Makefile.core.def |  1 +
530103
 grub-core/kern/efi/tdx.c    | 70 +++++++++++++++++++++++++++++++++++++++++++++
530103
 grub-core/kern/tpm.c        |  4 +++
530103
 include/grub/efi/tdx.h      | 26 +++++++++++++++++
530103
 include/grub/tdx.h          | 36 +++++++++++++++++++++++
530103
 5 files changed, 137 insertions(+)
530103
 create mode 100644 grub-core/kern/efi/tdx.c
530103
 create mode 100644 include/grub/efi/tdx.h
530103
 create mode 100644 include/grub/tdx.h
530103
530103
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
530103
index 637d7203e3..2787d59c52 100644
530103
--- a/grub-core/Makefile.core.def
530103
+++ b/grub-core/Makefile.core.def
530103
@@ -200,6 +200,7 @@ kernel = {
530103
   efi = kern/efi/acpi.c;
530103
   efi = kern/lockdown.c;
530103
   efi = lib/envblk.c;
530103
+  efi = kern/efi/tdx.c;
530103
   efi = kern/efi/tpm.c;
530103
   i386_coreboot = kern/i386/pc/acpi.c;
530103
   i386_multiboot = kern/i386/pc/acpi.c;
530103
diff --git a/grub-core/kern/efi/tdx.c b/grub-core/kern/efi/tdx.c
530103
new file mode 100644
530103
index 0000000000..3a49f8d117
530103
--- /dev/null
530103
+++ b/grub-core/kern/efi/tdx.c
530103
@@ -0,0 +1,70 @@
530103
+#include <grub/err.h>
530103
+#include <grub/i18n.h>
530103
+#include <grub/efi/api.h>
530103
+#include <grub/efi/efi.h>
530103
+#include <grub/efi/tpm.h>
530103
+#include <grub/efi/tdx.h>
530103
+#include <grub/mm.h>
530103
+#include <grub/tpm.h>
530103
+#include <grub/tdx.h>
530103
+
530103
+static grub_efi_guid_t tdx_guid = EFI_TDX_GUID;
530103
+
530103
+static inline grub_err_t grub_tdx_dprintf(grub_efi_status_t status)
530103
+{
530103
+  switch (status) {
530103
+  case GRUB_EFI_SUCCESS:
530103
+    return 0;
530103
+  case GRUB_EFI_DEVICE_ERROR:
530103
+    grub_dprintf ("tdx", "Command failed: 0x%"PRIxGRUB_EFI_STATUS"\n",
530103
+                  status);
530103
+    return GRUB_ERR_IO;
530103
+  case GRUB_EFI_INVALID_PARAMETER:
530103
+    grub_dprintf ("tdx", "Invalid parameter: 0x%"PRIxGRUB_EFI_STATUS"\n",
530103
+                  status);
530103
+    return GRUB_ERR_BAD_ARGUMENT;
530103
+  case GRUB_EFI_VOLUME_FULL:
530103
+    grub_dprintf ("tdx", "Volume is full: 0x%"PRIxGRUB_EFI_STATUS"\n",
530103
+                  status);
530103
+    return GRUB_ERR_BAD_ARGUMENT;
530103
+  case GRUB_EFI_UNSUPPORTED:
530103
+    grub_dprintf ("tdx", "TDX unavailable: 0x%"PRIxGRUB_EFI_STATUS"\n",
530103
+                  status);
530103
+    return GRUB_ERR_UNKNOWN_DEVICE;
530103
+  default:
530103
+    grub_dprintf ("tdx", "Unknown TDX error: 0x%"PRIxGRUB_EFI_STATUS"\n",
530103
+                  status);
530103
+    return GRUB_ERR_UNKNOWN_DEVICE;
530103
+  }
530103
+}
530103
+
530103
+grub_err_t
530103
+grub_tdx_log_event(unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
530103
+		   const char *description)
530103
+{
530103
+  EFI_TCG2_EVENT *event;
530103
+  grub_efi_status_t status;
530103
+  grub_efi_tdx_protocol_t *tdx;
530103
+
530103
+  tdx = grub_efi_locate_protocol (&tdx_guid, NULL);
530103
+
530103
+  if (!tdx)
530103
+    return 0;
530103
+
530103
+  event = grub_zalloc(sizeof (EFI_TCG2_EVENT) + grub_strlen(description) + 1);
530103
+  if (!event)
530103
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
530103
+		       N_("cannot allocate TCG2 event buffer"));
530103
+
530103
+  event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
530103
+  event->Header.HeaderVersion = 1;
530103
+  event->Header.PCRIndex = pcr;
530103
+  event->Header.EventType = EV_IPL;
530103
+  event->Size = sizeof(*event) - sizeof(event->Event) + grub_strlen(description) + 1;
530103
+  grub_memcpy(event->Event, description, grub_strlen(description) + 1);
530103
+
530103
+  status = efi_call_5 (tdx->hash_log_extend_event, tdx, 0, (unsigned long) buf,
530103
+		       (grub_uint64_t) size, event);
530103
+
530103
+  return grub_tdx_dprintf(status);
530103
+}
530103
\ No newline at end of file
530103
diff --git a/grub-core/kern/tpm.c b/grub-core/kern/tpm.c
530103
index e5e8fced62..71cc4252c1 100644
530103
--- a/grub-core/kern/tpm.c
530103
+++ b/grub-core/kern/tpm.c
530103
@@ -4,6 +4,7 @@
530103
 #include <grub/mm.h>
530103
 #include <grub/tpm.h>
530103
 #include <grub/term.h>
530103
+#include <grub/tdx.h>
530103
 
530103
 grub_err_t
530103
 grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
530103
@@ -13,6 +14,9 @@ grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
530103
   char *desc = grub_xasprintf("%s %s", kind, description);
530103
   if (!desc)
530103
     return GRUB_ERR_OUT_OF_MEMORY;
530103
+
530103
+  grub_tdx_log_event(buf, size, pcr, desc);
530103
+
530103
   ret = grub_tpm_log_event(buf, size, pcr, desc);
530103
   grub_free(desc);
530103
   return ret;
530103
diff --git a/include/grub/efi/tdx.h b/include/grub/efi/tdx.h
530103
new file mode 100644
530103
index 0000000000..9bdac2a275
530103
--- /dev/null
530103
+++ b/include/grub/efi/tdx.h
530103
@@ -0,0 +1,26 @@
530103
+/*
530103
+ *  GRUB  --  GRand Unified Bootloader
530103
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
530103
+ *
530103
+ *  GRUB is free software: you can redistribute it and/or modify
530103
+ *  it under the terms of the GNU General Public License as published by
530103
+ *  the Free Software Foundation, either version 3 of the License, or
530103
+ *  (at your option) any later version.
530103
+ *
530103
+ *  GRUB is distributed in the hope that it will be useful,
530103
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
530103
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
530103
+ *  GNU General Public License for more details.
530103
+ *
530103
+ *  You should have received a copy of the GNU General Public License
530103
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
530103
+ */
530103
+
530103
+#ifndef GRUB_EFI_TDX_HEADER
530103
+#define GRUB_EFI_TDX_HEADER 1
530103
+
530103
+#define EFI_TDX_GUID {0x96751a3d, 0x72f4, 0x41a6, {0xa7, 0x94, 0xed, 0x5d, 0x0e, 0x67, 0xae, 0x6b}};
530103
+
530103
+typedef grub_efi_tpm2_protocol_t grub_efi_tdx_protocol_t;
530103
+
530103
+#endif
530103
\ No newline at end of file
530103
diff --git a/include/grub/tdx.h b/include/grub/tdx.h
530103
new file mode 100644
530103
index 0000000000..4a98008e39
530103
--- /dev/null
530103
+++ b/include/grub/tdx.h
530103
@@ -0,0 +1,36 @@
530103
+/*
530103
+ *  GRUB  --  GRand Unified Bootloader
530103
+ *  Copyright (C) 2015  Free Software Foundation, Inc.
530103
+ *
530103
+ *  GRUB is free software: you can redistribute it and/or modify
530103
+ *  it under the terms of the GNU General Public License as published by
530103
+ *  the Free Software Foundation, either version 3 of the License, or
530103
+ *  (at your option) any later version.
530103
+ *
530103
+ *  GRUB is distributed in the hope that it will be useful,
530103
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
530103
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
530103
+ *  GNU General Public License for more details.
530103
+ *
530103
+ *  You should have received a copy of the GNU General Public License
530103
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
530103
+ */
530103
+
530103
+#ifndef GRUB_TDX_HEADER
530103
+#define GRUB_TDX_HEADER 1
530103
+
530103
+#if defined (GRUB_MACHINE_EFI)
530103
+grub_err_t grub_tdx_log_event(unsigned char *buf, grub_size_t size,
530103
+			      grub_uint8_t pcr, const char *description);
530103
+#else
530103
+static inline grub_err_t grub_tdx_log_event(
530103
+	unsigned char *buf __attribute__ ((unused)),
530103
+	grub_size_t size __attribute__ ((unused)),
530103
+	grub_uint8_t pcr __attribute__ ((unused)),
530103
+	const char *description __attribute__ ((unused)))
530103
+{
530103
+	return 0;
530103
+};
530103
+#endif
530103
+
530103
+#endif