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

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