Blame SOURCES/0317-ibmvtpm-Add-support-for-trusted-boot-using-a-vTPM-2..patch

d3c3ab
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d3c3ab
From: Stefan Berger <stefanb@linux.ibm.com>
fd0330
Date: Mon, 6 Feb 2023 10:03:25 -0500
d3c3ab
Subject: [PATCH] ibmvtpm: Add support for trusted boot using a vTPM 2.0
d3c3ab
d3c3ab
Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275
d3c3ab
PowerPC platform. With this patch grub now measures text and binary data
d3c3ab
into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform
d3c3ab
does.
d3c3ab
d3c3ab
This patch requires Daniel Axtens's patches for claiming more memory.
d3c3ab
fd0330
Note: The tpm_init() function cannot be called from GRUB_MOD_INIT() since
fd0330
it does not find the device nodes upon module initialization and
fd0330
therefore the call to tpm_init() must be deferred to grub_tpm_measure().
fd0330
d3c3ab
For vTPM support to work on PowerVM, system driver levels 1010.30
d3c3ab
or 1020.00 are required.
d3c3ab
d3c3ab
Note: Previous versions of firmware levels with the 2hash-ext-log
d3c3ab
API call have a bug that, once this API call is invoked, has the
d3c3ab
effect of disabling the vTPM driver under Linux causing an error
d3c3ab
message to be displayed in the Linux kernel log. Those users will
d3c3ab
have to update their machines to the firmware levels mentioned
d3c3ab
above.
d3c3ab
d3c3ab
Cc: Eric Snowberg <eric.snowberg@oracle.com>
d3c3ab
Signed-off-by: Stefan Berger <stefanb@linux.ibm.com>
fd0330
Signed-off-by: Daniel Axtens <dja@axtens.net>
fd0330
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
fd0330
(cherry picked from commit 2aa5ef83743dfea79377309ff4f5e9c9a55de355)
d3c3ab
---
d3c3ab
 grub-core/Makefile.core.def           |   7 ++
fd0330
 grub-core/commands/ieee1275/ibmvtpm.c | 155 ++++++++++++++++++++++++++++++++++
d3c3ab
 include/grub/ieee1275/ieee1275.h      |   3 +
d3c3ab
 docs/grub.texi                        |   3 +-
fd0330
 4 files changed, 167 insertions(+), 1 deletion(-)
d3c3ab
 create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
d3c3ab
d3c3ab
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
fd0330
index f21da23213..02ea718652 100644
d3c3ab
--- a/grub-core/Makefile.core.def
d3c3ab
+++ b/grub-core/Makefile.core.def
fd0330
@@ -1175,6 +1175,13 @@ module = {
d3c3ab
   enable = powerpc_ieee1275;
d3c3ab
 };
d3c3ab
 
d3c3ab
+module = {
d3c3ab
+  name = tpm;
d3c3ab
+  common = commands/tpm.c;
d3c3ab
+  ieee1275 = commands/ieee1275/ibmvtpm.c;
d3c3ab
+  enable = powerpc_ieee1275;
d3c3ab
+};
d3c3ab
+
d3c3ab
 module = {
d3c3ab
   name = terminal;
d3c3ab
   common = commands/terminal.c;
d3c3ab
diff --git a/grub-core/commands/ieee1275/ibmvtpm.c b/grub-core/commands/ieee1275/ibmvtpm.c
d3c3ab
new file mode 100644
fd0330
index 0000000000..239942d27e
d3c3ab
--- /dev/null
d3c3ab
+++ b/grub-core/commands/ieee1275/ibmvtpm.c
fd0330
@@ -0,0 +1,155 @@
d3c3ab
+/*
d3c3ab
+ *  GRUB  --  GRand Unified Bootloader
fd0330
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
fd0330
+ *  Copyright (C) 2022  IBM Corporation
d3c3ab
+ *
d3c3ab
+ *  GRUB is free software: you can redistribute it and/or modify
d3c3ab
+ *  it under the terms of the GNU General Public License as published by
d3c3ab
+ *  the Free Software Foundation, either version 3 of the License, or
d3c3ab
+ *  (at your option) any later version.
d3c3ab
+ *
d3c3ab
+ *  GRUB is distributed in the hope that it will be useful,
d3c3ab
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
d3c3ab
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d3c3ab
+ *  GNU General Public License for more details.
d3c3ab
+ *
d3c3ab
+ *  You should have received a copy of the GNU General Public License
d3c3ab
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
d3c3ab
+ *
d3c3ab
+ *  IBM vTPM support code.
d3c3ab
+ */
d3c3ab
+
d3c3ab
+#include <grub/err.h>
d3c3ab
+#include <grub/types.h>
d3c3ab
+#include <grub/tpm.h>
d3c3ab
+#include <grub/ieee1275/ieee1275.h>
d3c3ab
+#include <grub/mm.h>
d3c3ab
+#include <grub/misc.h>
d3c3ab
+
d3c3ab
+static grub_ieee1275_ihandle_t tpm_ihandle;
d3c3ab
+static grub_uint8_t tpm_version;
d3c3ab
+
fd0330
+#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t) 0)
d3c3ab
+
d3c3ab
+static void
d3c3ab
+tpm_get_tpm_version (void)
d3c3ab
+{
d3c3ab
+  grub_ieee1275_phandle_t vtpm;
d3c3ab
+  char buffer[20];
d3c3ab
+
d3c3ab
+  if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm) &&
d3c3ab
+      !grub_ieee1275_get_property (vtpm, "compatible", buffer,
d3c3ab
+				   sizeof (buffer), NULL) &&
d3c3ab
+      !grub_strcmp (buffer, "IBM,vtpm20"))
d3c3ab
+    tpm_version = 2;
d3c3ab
+}
d3c3ab
+
d3c3ab
+static grub_err_t
d3c3ab
+tpm_init (void)
d3c3ab
+{
d3c3ab
+  static int init_success = 0;
d3c3ab
+
d3c3ab
+  if (!init_success)
d3c3ab
+    {
fd0330
+      if (grub_ieee1275_open ("/vdevice/vtpm", &tpm_ihandle) < 0)
fd0330
+	{
fd0330
+	  tpm_ihandle = IEEE1275_IHANDLE_INVALID;
fd0330
+	  return GRUB_ERR_UNKNOWN_DEVICE;
fd0330
+	}
d3c3ab
+
d3c3ab
+      init_success = 1;
d3c3ab
+
d3c3ab
+      tpm_get_tpm_version ();
d3c3ab
+    }
d3c3ab
+
d3c3ab
+  return GRUB_ERR_NONE;
d3c3ab
+}
d3c3ab
+
d3c3ab
+static int
d3c3ab
+ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex,
d3c3ab
+		       grub_uint32_t eventtype,
d3c3ab
+		       const char *description,
d3c3ab
+		       grub_size_t description_size,
d3c3ab
+		       void *buf, grub_size_t size)
d3c3ab
+{
d3c3ab
+  struct tpm_2hash_ext_log
d3c3ab
+  {
d3c3ab
+    struct grub_ieee1275_common_hdr common;
d3c3ab
+    grub_ieee1275_cell_t method;
d3c3ab
+    grub_ieee1275_cell_t ihandle;
d3c3ab
+    grub_ieee1275_cell_t size;
d3c3ab
+    grub_ieee1275_cell_t buf;
d3c3ab
+    grub_ieee1275_cell_t description_size;
d3c3ab
+    grub_ieee1275_cell_t description;
d3c3ab
+    grub_ieee1275_cell_t eventtype;
d3c3ab
+    grub_ieee1275_cell_t pcrindex;
d3c3ab
+    grub_ieee1275_cell_t catch_result;
d3c3ab
+    grub_ieee1275_cell_t rc;
fd0330
+  };
fd0330
+  struct tpm_2hash_ext_log args;
d3c3ab
+
d3c3ab
+  INIT_IEEE1275_COMMON (&args.common, "call-method", 8, 2);
d3c3ab
+  args.method = (grub_ieee1275_cell_t) "2hash-ext-log";
d3c3ab
+  args.ihandle = tpm_ihandle;
d3c3ab
+  args.pcrindex = pcrindex;
d3c3ab
+  args.eventtype = eventtype;
d3c3ab
+  args.description = (grub_ieee1275_cell_t) description;
d3c3ab
+  args.description_size = description_size;
d3c3ab
+  args.buf = (grub_ieee1275_cell_t) buf;
d3c3ab
+  args.size = (grub_ieee1275_cell_t) size;
d3c3ab
+
d3c3ab
+  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
d3c3ab
+    return -1;
d3c3ab
+
d3c3ab
+  /*
d3c3ab
+   * catch_result is set if firmware does not support 2hash-ext-log
d3c3ab
+   * rc is GRUB_IEEE1275_CELL_FALSE (0) on failure
d3c3ab
+   */
d3c3ab
+  if ((args.catch_result) || args.rc == GRUB_IEEE1275_CELL_FALSE)
d3c3ab
+    return -1;
d3c3ab
+
d3c3ab
+  return 0;
d3c3ab
+}
d3c3ab
+
d3c3ab
+static grub_err_t
fd0330
+tpm2_log_event (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
d3c3ab
+		const char *description)
d3c3ab
+{
d3c3ab
+  static int error_displayed = 0;
fd0330
+  int rc;
d3c3ab
+
fd0330
+  rc = ibmvtpm_2hash_ext_log (pcr, EV_IPL,
fd0330
+			      description, grub_strlen(description) + 1,
fd0330
+			      buf, size);
fd0330
+  if (rc && !error_displayed)
d3c3ab
+    {
d3c3ab
+      error_displayed++;
d3c3ab
+      return grub_error (GRUB_ERR_BAD_DEVICE,
fd0330
+			 "2HASH-EXT-LOG failed: Firmware is likely too old.\n");
d3c3ab
+    }
d3c3ab
+
d3c3ab
+  return GRUB_ERR_NONE;
d3c3ab
+}
d3c3ab
+
d3c3ab
+grub_err_t
d3c3ab
+grub_tpm_measure (unsigned char *buf, grub_size_t size, grub_uint8_t pcr,
d3c3ab
+		  const char *description)
d3c3ab
+{
fd0330
+  /*
fd0330
+   * Call tpm_init() 'late' rather than from GRUB_MOD_INIT() so that device nodes
fd0330
+   * can be found.
fd0330
+   */
fd0330
+  grub_err_t err = tpm_init ();
d3c3ab
+
d3c3ab
+  /* Absence of a TPM isn't a failure. */
d3c3ab
+  if (err != GRUB_ERR_NONE)
d3c3ab
+    return GRUB_ERR_NONE;
d3c3ab
+
d3c3ab
+  grub_dprintf ("tpm", "log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE ", %s\n",
d3c3ab
+		pcr, size, description);
d3c3ab
+
d3c3ab
+  if (tpm_version == 2)
d3c3ab
+    return tpm2_log_event (buf, size, pcr, description);
d3c3ab
+
d3c3ab
+  return GRUB_ERR_NONE;
d3c3ab
+}
d3c3ab
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
fd0330
index 560c968460..27b9cf259b 100644
d3c3ab
--- a/include/grub/ieee1275/ieee1275.h
d3c3ab
+++ b/include/grub/ieee1275/ieee1275.h
d3c3ab
@@ -24,6 +24,9 @@
d3c3ab
 #include <grub/types.h>
d3c3ab
 #include <grub/machine/ieee1275.h>
d3c3ab
 
d3c3ab
+#define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
d3c3ab
+#define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
d3c3ab
+
d3c3ab
 struct grub_ieee1275_mem_region
d3c3ab
 {
d3c3ab
   unsigned int start;
d3c3ab
diff --git a/docs/grub.texi b/docs/grub.texi
fd0330
index 1750b72ee9..825278a7f3 100644
d3c3ab
--- a/docs/grub.texi
d3c3ab
+++ b/docs/grub.texi
fd0330
@@ -6235,7 +6235,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built
d3c3ab
 into @file{core.img} in order to avoid a potential gap in measurement between
d3c3ab
 @file{core.img} being loaded and the tpm module being loaded.
d3c3ab
 
d3c3ab
-Measured boot is currently only supported on EFI platforms.
d3c3ab
+Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC
d3c3ab
+platforms.
d3c3ab
 
d3c3ab
 @node Lockdown
d3c3ab
 @section Lockdown when booting on a secure setup